This repository has been archived by the owner on May 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
198 lines (176 loc) · 4.74 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"github.com/hashicorp/go-retryablehttp"
"log"
"net/http"
"net/url"
"os"
"runtime"
"strings"
"sync"
"time"
)
type Result struct {
Results []Profile `json:"results"`
}
type Login struct {
Username string `json:"username"`
Password string `json:"password"`
Salt string `json:"salt"`
Md5 string `json:"md5"`
}
type Profile struct {
Name Name `json:"name"`
Email string `json:"email"`
Login Login `json:"login"`
Phone string `json:"phone"`
Nat string `json:"nat"`
}
type Name struct {
FamilyName string `json:"familyName"`
GivenName string `json:"givenName"`
HonorificPrefix string `json:"honorificPrefix"`
}
type TokenResponse struct {
Token string `json:"access_token"`
}
/*SCIM*/
type Email struct {
Value string `json:"value"`
}
type PhoneNumber struct {
Value string `json:"value"`
}
type ScimRequest struct {
Schemas []string `json:"schemas"`
UserName string `json:"userName"`
Name Name `json:"name"`
Active bool `json:"active"`
Emails []Email `json:"emails"`
PhoneNumbers []PhoneNumber `json:"phoneNumbers"`
}
var (
scimProfileSchema = "urn:ietf:params:scim:schemas:core:2.0:Profile"
randomUserApiUrl = "https://randomuser.me/api"
iamUrl = "https://localhost"
tokenUrl = "https://localhost/id/connect/token"
scimUrl = fmt.Sprintf("%s/admin/api/scim/v2/Profiles", iamUrl)
)
func main() {
profileCount := flag.Int("profile", 100, "Profiles count to be created")
flag.Parse()
if *profileCount == 0 {
flag.PrintDefaults()
os.Exit(0)
return
}
log.Printf("INFORMATION: %d profiles will be created", *profileCount)
start := time.Now()
var wg sync.WaitGroup
CreateProfiles(&wg, *profileCount)
wg.Wait()
elapsed := time.Since(start)
log.Printf("Took %s", elapsed)
}
func CreateProfiles(wg *sync.WaitGroup, profileCount int) {
processorCount := runtime.NumCPU()
iterationCount, remainder := divMod(int64(profileCount), int64(processorCount))
log.Printf("Processor Count: %d, Iteration Count: %d", processorCount, iterationCount)
for i := 1; i <= processorCount; i++ {
wg.Add(1)
if i == processorCount {
iterationCount += remainder
}
go func(iteration int64) {
defer wg.Done()
for j := int64(0); j < iteration; j++ {
profile, err := getRandomProfile()
if err != nil {
log.Printf("Random api had problem: %s", err.Error())
}
err = createProfile(profile)
if err != nil {
log.Printf("ERROR: Create profile %s", err.Error())
}
}
}(iterationCount)
}
}
func divMod(numerator, denominator int64) (quotient, remainder int64) {
quotient = numerator / denominator
remainder = numerator % denominator
return
}
func createProfile(profile Profile) error {
request := ScimRequest{
Schemas: []string{scimProfileSchema},
UserName: profile.Login.Username,
Name: Name{
FamilyName: profile.Name.FamilyName,
GivenName: profile.Name.GivenName,
HonorificPrefix: profile.Name.HonorificPrefix,
},
Active: true,
Emails: []Email{{Value: profile.Email}},
PhoneNumbers: []PhoneNumber{{Value: profile.Phone}},
}
requestBytes, err := json.Marshal(request)
requestBuffer := bytes.NewBuffer(requestBytes)
if err != nil {
log.Println(err.Error())
}
accessToken, err := GetAccessToken()
if err != nil {
log.Println(err.Error())
return err
}
req, _ := http.NewRequest(http.MethodPost, scimUrl, requestBuffer)
req.Header.Add("Authorization", "Bearer "+accessToken)
req.Header.Add("Content-Type", "application/json")
client := &http.Client{}
response, err := client.Do(req)
if err != nil {
log.Println(err.Error())
return err
}
if response != nil && response.StatusCode == 200 {
log.Printf("Profile created!")
}
return nil
}
func GetAccessToken() (token string, err error) {
var tokenResponse TokenResponse
data := url.Values{}
data.Set("client_id", "user-generator")
data.Set("client_secret", "password")
data.Set("grant_type", "client_credentials")
data.Set("scope", "iam.scim.admin")
response, err := http.Post(tokenUrl, "application/x-www-form-urlencoded", strings.NewReader(data.Encode()))
if err != nil {
return "", err
}
if response.StatusCode != 200 {
return "", err
}
_ = json.NewDecoder(response.Body).Decode(&tokenResponse)
return tokenResponse.Token, nil
}
func getRandomProfile() (Profile, error) {
var result Result
client := retryablehttp.NewClient()
client.Logger = nil
response, err := client.Get(randomUserApiUrl)
if err != nil {
log.Printf("There was a problem while getting the random user")
return Profile{}, err
}
err = json.NewDecoder(response.Body).Decode(&result)
if err != nil {
return Profile{}, err
}
return result.Results[0], nil
}