This repository has been archived by the owner on Aug 5, 2020. It is now read-only.
forked from okta/terraform-provider-okta
/
user.go
552 lines (469 loc) · 13.2 KB
/
user.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
package okta
import (
"encoding/json"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/okta/okta-sdk-golang/okta"
"github.com/terraform-providers/terraform-provider-okta/sdk"
"log"
"net/http"
"reflect"
"time"
)
var userProfileDataSchema = map[string]*schema.Schema{
"admin_roles": &schema.Schema{
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"city": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"cost_center": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"country_code": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"custom_profile_attributes": &schema.Schema{
Type: schema.TypeString,
StateFunc: normalizeDataJSON,
Computed: true,
},
"department": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"display_name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"division": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"email": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"employee_number": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"first_name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"group_memberships": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"honorific_prefix": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"honorific_suffix": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"last_name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"locale": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"login": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"manager": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"manager_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"middle_name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"mobile_phone": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"nick_name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"organization": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"postal_address": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"preferred_language": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"primary_phone": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"profile_url": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"second_email": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"state": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"status": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"street_address": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"timezone": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"title": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"user_type": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"zip_code": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
}
func buildUserDataSourceSchema(target map[string]*schema.Schema) map[string]*schema.Schema {
return buildSchema(userProfileDataSchema, target)
}
func assignAdminRolesToUser(u string, r []string, c *okta.Client) error {
for _, role := range r {
if contains(sdk.ValidAdminRoles, role) {
roleStruct := okta.Role{Type: role}
_, _, err := c.User.AddRoleToUser(u, roleStruct)
if err != nil {
return fmt.Errorf("[ERROR] Error Assigning Admin Roles to User: %v", err)
}
} else {
return fmt.Errorf("[ERROR] %v is not a valid Okta role", role)
}
}
return nil
}
func assignGroupsToUser(u string, g []string, c *okta.Client) error {
for _, group := range g {
_, err := c.Group.AddUserToGroup(group, u)
if err != nil {
return fmt.Errorf("[ERROR] Error Assigning Group to User: %v", err)
}
}
return nil
}
func populateUserProfile(d *schema.ResourceData) *okta.UserProfile {
profile := okta.UserProfile{}
if rawAttrs, ok := d.GetOk("custom_profile_attributes"); ok {
var attrs map[string]interface{}
str := rawAttrs.(string)
// We validate the JSON, no need to check error
json.Unmarshal([]byte(str), &attrs)
for k, v := range attrs {
profile[k] = v
}
}
profile["firstName"] = d.Get("first_name").(string)
profile["lastName"] = d.Get("last_name").(string)
profile["login"] = d.Get("login").(string)
profile["email"] = d.Get("email").(string)
if _, ok := d.GetOk("city"); ok {
profile["city"] = d.Get("city").(string)
}
if _, ok := d.GetOk("cost_center"); ok {
profile["costCenter"] = d.Get("cost_center").(string)
}
if _, ok := d.GetOk("country_code"); ok {
profile["countryCode"] = d.Get("country_code").(string)
}
if _, ok := d.GetOk("department"); ok {
profile["department"] = d.Get("department").(string)
}
if _, ok := d.GetOk("display_name"); ok {
profile["displayName"] = d.Get("display_name").(string)
}
if _, ok := d.GetOk("division"); ok {
profile["division"] = d.Get("division").(string)
}
if _, ok := d.GetOk("employee_number"); ok {
profile["employeeNumber"] = d.Get("employee_number").(string)
}
if _, ok := d.GetOk("honorific_prefix"); ok {
profile["honorificPrefix"] = d.Get("honorific_prefix").(string)
}
if _, ok := d.GetOk("honorific_suffix"); ok {
profile["honorificSuffix"] = d.Get("honorific_suffix").(string)
}
if _, ok := d.GetOk("locale"); ok {
profile["locale"] = d.Get("locale").(string)
}
if _, ok := d.GetOk("manager"); ok {
profile["manager"] = d.Get("manager").(string)
}
if _, ok := d.GetOk("manager_id"); ok {
profile["managerId"] = d.Get("manager_id").(string)
}
if _, ok := d.GetOk("middle_name"); ok {
profile["middleName"] = d.Get("middle_name").(string)
}
if _, ok := d.GetOk("mobile_phone"); ok {
profile["mobilePhone"] = d.Get("mobile_phone").(string)
}
if _, ok := d.GetOk("nick_name"); ok {
profile["nickName"] = d.Get("nick_name").(string)
}
if _, ok := d.GetOk("organization"); ok {
profile["organization"] = d.Get("organization").(string)
}
// need to set profile.postalAddress to nil explicitly if not set because of a bug with this field
// have a support ticket open with okta about it
if _, ok := d.GetOk("postal_address"); ok {
profile["postalAddress"] = d.Get("postal_address").(string)
} else {
profile["postalAddress"] = nil
}
if _, ok := d.GetOk("preferred_language"); ok {
profile["preferredLanguage"] = d.Get("preferred_language").(string)
}
if _, ok := d.GetOk("primary_phone"); ok {
profile["primaryPhone"] = d.Get("primary_phone").(string)
}
if _, ok := d.GetOk("profile_url"); ok {
profile["profileUrl"] = d.Get("profile_url").(string)
}
if _, ok := d.GetOk("second_email"); ok {
profile["secondEmail"] = d.Get("second_email").(string)
}
if _, ok := d.GetOk("state"); ok {
profile["state"] = d.Get("state").(string)
}
if _, ok := d.GetOk("street_address"); ok {
profile["streetAddress"] = d.Get("street_address").(string)
}
if _, ok := d.GetOk("timezone"); ok {
profile["timezone"] = d.Get("timezone").(string)
}
if _, ok := d.GetOk("title"); ok {
profile["title"] = d.Get("title").(string)
}
if _, ok := d.GetOk("user_type"); ok {
profile["userType"] = d.Get("user_type").(string)
}
if _, ok := d.GetOk("zip_code"); ok {
profile["zipCode"] = d.Get("zip_code").(string)
}
return &profile
}
func listUserOnlyRoles(c *okta.Client, userId string) (userOnlyRoles []*okta.Role, resp *okta.Response, err error) {
roles, resp, err := c.User.ListAssignedRoles(userId, nil)
if err != nil {
return
}
for _, role := range roles {
if role.AssignmentType == "USER" {
userOnlyRoles = append(userOnlyRoles, role)
}
}
return
}
func setAdminRoles(d *schema.ResourceData, c *okta.Client) error {
roleTypes := make([]interface{}, 0)
// set all roles currently attached to user in state
roles, resp, err := listUserOnlyRoles(c, d.Id())
if err != nil {
if resp.StatusCode == http.StatusForbidden {
log.Printf("[INFO] Insufficient permissions to get Admin Roles, skipping.")
} else {
return err
}
} else {
for _, role := range roles {
roleTypes = append(roleTypes, role.Type)
}
}
// set the custom_profile_attributes values
return setNonPrimitives(d, map[string]interface{}{
"admin_roles": schema.NewSet(schema.HashString, roleTypes),
})
}
func setGroups(d *schema.ResourceData, c *okta.Client) error {
// set all groups currently attached to user in state
groups, _, err := c.User.ListUserGroups(d.Id(), nil)
if err != nil {
return err
}
groupIds := make([]interface{}, 0)
// ignore saving the Everyone group into state so we don't end up with perpetual diffs
for _, group := range groups {
if group.Profile.Name != "Everyone" {
groupIds = append(groupIds, group.Id)
}
}
// set the custom_profile_attributes values
return setNonPrimitives(d, map[string]interface{}{
"group_memberships": schema.NewSet(schema.HashString, groupIds),
})
}
func isCustomUserAttr(key string) bool {
return !contains(profileKeys, key)
}
func flattenUser(u *okta.User, d *schema.ResourceData) (map[string]interface{}, error) {
customAttributes := make(map[string]interface{})
attrs := map[string]interface{}{}
for k, v := range *u.Profile {
if v != nil {
attrKey := camelCaseToUnderscore(k)
if isCustomUserAttr(attrKey) {
// Supporting any potential type
ref := reflect.ValueOf(v)
switch ref.Kind() {
case reflect.String:
customAttributes[k] = ref.String()
case reflect.Float64:
customAttributes[k] = ref.Float()
case reflect.Int:
customAttributes[k] = ref.Int()
case reflect.Bool:
customAttributes[k] = ref.Bool()
case reflect.Slice:
rawArr := v.([]interface{})
customAttributes[k] = rawArr
case reflect.Map:
rawMap := v.(map[string]interface{})
customAttributes[k] = rawMap
}
} else {
attrs[attrKey] = v
}
}
}
data, err := json.Marshal(customAttributes)
if err != nil {
return nil, fmt.Errorf("Failed to load custom_attributes to JSON")
}
attrs["custom_profile_attributes"] = string(data)
return attrs, nil
}
// need to remove from all current admin roles and reassign based on terraform configs when a change is detected
func updateAdminRolesOnUser(u string, r []string, c *okta.Client) error {
roles, _, err := listUserOnlyRoles(c, u)
if err != nil {
return fmt.Errorf("[ERROR] Error Updating Admin Roles On User: %v", err)
}
for _, role := range roles {
_, err := c.User.RemoveRoleFromUser(u, role.Id)
if err != nil {
return fmt.Errorf("[ERROR] Error Updating Admin Roles On User: %v", err)
}
}
err = assignAdminRolesToUser(u, r, c)
if err != nil {
return err
}
return nil
}
// need to remove from all current groups and reassign based on terraform configs when a change is detected
func updateGroupsOnUser(u string, g []string, c *okta.Client) error {
groups, _, err := c.User.ListUserGroups(u, nil)
if err != nil {
return fmt.Errorf("[ERROR] Error Updating Groups On User: %v", err)
}
for _, group := range groups {
if group.Profile.Name != "Everyone" {
_, err := c.Group.RemoveGroupUser(group.Id, u)
if err != nil {
return fmt.Errorf("[ERROR] Error Updating Groups On User: %v", err)
}
}
}
if err = assignGroupsToUser(u, g, c); err != nil {
return err
}
return nil
}
// handle setting of user status based on what the current status is because okta
// only allows transitions to certain statuses from other statuses - consult okta User API docs for more info
// https://developer.okta.com/docs/api/resources/users#lifecycle-operations
func updateUserStatus(uid string, desiredStatus string, c *okta.Client) error {
user, _, err := c.User.GetUser(uid)
if err != nil {
return err
}
var statusErr error
switch desiredStatus {
case "SUSPENDED":
_, statusErr = c.User.SuspendUser(uid)
case "DEPROVISIONED":
_, statusErr = c.User.DeactivateUser(uid, nil)
case "ACTIVE":
switch user.Status {
case "SUSPENDED":
_, statusErr = c.User.UnsuspendUser(uid)
break
case "PASSWORD_EXPIRED":
// Ignore password expired status. This status is already activated.
return nil
case "LOCKED_OUT":
_, statusErr = c.User.UnlockUser(uid)
default:
_, _, statusErr = c.User.ActivateUser(uid, nil)
}
}
if statusErr != nil {
return statusErr
}
err = waitForStatusTransition(uid, c)
if err != nil {
return err
}
return nil
}
// need to wait for user.TransitioningToStatus field to be empty before allowing Terraform to continue
// so the proper current status gets set in the state during the Read operation after a Status update
func waitForStatusTransition(u string, c *okta.Client) error {
user, _, err := c.User.GetUser(u)
if err != nil {
return err
}
for {
if user.TransitioningToStatus == "" {
return nil
} else {
log.Printf("[INFO] Transitioning to status = %v; waiting for 5 more seconds...", user.TransitioningToStatus)
time.Sleep(5 * time.Second)
user, _, err = c.User.GetUser(u)
if err != nil {
return err
}
}
}
}