This repository has been archived by the owner on Jun 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
/
resource_okta_user.go
542 lines (485 loc) 路 15.2 KB
/
resource_okta_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
package okta
import (
"errors"
"fmt"
"log"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/okta/okta-sdk-golang/okta"
"github.com/okta/okta-sdk-golang/okta/query"
)
// All profile properties here so we can do a diff against the config to see if any have changed before making the
// request or before erring due to an update on a user that is DEPROVISIONED. Since we have core user props coupled
// with group/user membership a few change requests go out in the Update function.
var profileKeys = []string{
"city",
"cost_center",
"country_code",
"custom_profile_attributes",
"department",
"display_name",
"division",
"email",
"employee_number",
"first_name",
"honorific_prefix",
"honorific_suffix",
"last_name",
"locale",
"login",
"manager",
"manager_id",
"middle_name",
"mobile_phone",
"nick_name",
"organization",
"postal_address",
"preferred_language",
"primary_phone",
"profile_url",
"second_email",
"state",
"street_address",
"timezone",
"title",
"user_type",
"zip_code",
"password",
"recovery_question",
"recovery_answer",
}
func resourceUser() *schema.Resource {
return &schema.Resource{
Create: resourceUserCreate,
Read: resourceUserRead,
Update: resourceUserUpdate,
Delete: resourceUserDelete,
Exists: resourceUserExists,
Importer: &schema.ResourceImporter{
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
// Supporting id and email based imports
client := getOktaClientFromMetadata(meta)
user, _, err := client.User.GetUser(d.Id())
if err != nil {
return nil, err
}
d.SetId(user.Id)
return []*schema.ResourceData{d}, nil
},
},
Schema: map[string]*schema.Schema{
"admin_roles": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Description: "User Okta admin roles - ie. ['APP_ADMIN', 'USER_ADMIN']",
Elem: &schema.Schema{Type: schema.TypeString},
},
"city": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "User city",
},
"cost_center": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "User cost center",
},
"country_code": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "User country code",
},
"custom_profile_attributes": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateDataJSON,
StateFunc: normalizeDataJSON,
Default: "{}",
Description: "JSON formatted custom attributes for a user. It must be JSON due to various types Okta allows.",
},
"department": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "User department",
},
"display_name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "User display name, suitable to show end users",
},
"division": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "User division",
},
"email": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "User primary email address",
ValidateFunc: matchEmailRegexp,
},
"employee_number": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "User employee number",
},
"first_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "User first name",
},
"group_memberships": {
Type: schema.TypeSet,
Optional: true,
Description: "The groups that you want this user to be a part of. This can also be done via the group using the `users` property.",
Elem: &schema.Schema{Type: schema.TypeString},
},
"honorific_prefix": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "User honorific prefix",
},
"honorific_suffix": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "User honorific suffix",
},
"last_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "User last name",
},
"locale": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "User default location",
},
"login": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "User Okta login",
ForceNew: true,
},
"manager": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "Manager of User",
},
"manager_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "Manager ID of User",
},
"middle_name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "User middle name",
},
"mobile_phone": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "User mobile phone number",
},
"nick_name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "User nickname",
},
"organization": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "User organization",
},
"postal_address": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "User mailing address",
},
"preferred_language": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "User preferred language",
},
"primary_phone": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "User primary phone number",
},
"profile_url": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "User online profile (web page)",
},
"second_email": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "User secondary email address, used for account recovery",
},
"state": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "User state or region",
},
"status": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "The status of the User in Okta - remove to set user back to active/provisioned",
Default: "ACTIVE",
ValidateFunc: validation.StringInSlice([]string{"ACTIVE", "STAGED", "DEPROVISIONED", "SUSPENDED"}, false),
// ignore diff changing to ACTIVE if state is set to PROVISIONED or PASSWORD_EXPIRED
// since this is a similar status in Okta terms
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return old == "PROVISIONED" && new == "ACTIVE" || old == "PASSWORD_EXPIRED" && new == "ACTIVE"
},
},
"raw_status": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The raw status of the User in Okta - (status is mapped)",
},
"street_address": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "User street address",
},
"timezone": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "User default timezone",
},
"title": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "User title",
},
"user_type": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "User employee type",
},
"zip_code": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "User zipcode or postal code",
},
"password": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Sensitive: true,
Description: "User Password",
},
"recovery_question": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "User Password Recovery Question",
},
"recovery_answer": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringLenBetween(4, 100), // Hope no one uses > 10
Description: "User Password Recovery Answer",
},
},
}
}
func mapStatus(currentStatus string) string {
// PASSWORD_EXPIRED is effectively ACTIVE for our purposes
if currentStatus == "PASSWORD_EXPIRED" || currentStatus == "RECOVERY" {
return "ACTIVE"
}
return currentStatus
}
func resourceUserCreate(d *schema.ResourceData, m interface{}) error {
log.Printf("[INFO] Create User for %v", d.Get("login").(string))
client := m.(*Config).oktaClient
profile := populateUserProfile(d)
qp := query.NewQueryParams()
// setting activate to false on user creation will leave the user with a status of STAGED
if d.Get("status").(string) == "STAGED" {
qp = query.NewQueryParams(query.WithActivate(false))
}
password := d.Get("password").(string)
recoveryQuestion := d.Get("recovery_question").(string)
recoveryAnswer := d.Get("recovery_answer").(string)
if recoveryQuestion != "" && len(recoveryAnswer) < 4 {
return fmt.Errorf("[ERROR] Okta does not allow security answers with less than 4 characters")
}
uc := &okta.UserCredentials{
Password: &okta.PasswordCredential{
Value: password,
},
}
if recoveryQuestion != "" {
uc.RecoveryQuestion = &okta.RecoveryQuestionCredential{
Question: recoveryQuestion,
Answer: recoveryAnswer,
}
}
userBody := okta.User{
Profile: profile,
Credentials: uc,
}
user, _, err := client.User.CreateUser(userBody, qp)
if err != nil {
return fmt.Errorf("[ERROR] Error Creating User from Okta: %v", err)
}
// set the user id into state before setting roles and status in case they fail
d.SetId(user.Id)
// role assigning can only happen after the user is created so order matters here
roles := convertInterfaceToStringSetNullable(d.Get("admin_roles"))
if roles != nil {
if err = assignAdminRolesToUser(user.Id, roles, client); err != nil {
return err
}
}
// Only sync when there is opt in, consumers can chose which route they want to take
if _, exists := d.GetOkExists("group_memberships"); exists {
groups := convertInterfaceToStringSetNullable(d.Get("group_memberships"))
if err = assignGroupsToUser(user.Id, groups, client); err != nil {
return err
}
}
// status changing can only happen after user is created as well
if d.Get("status").(string) == "SUSPENDED" || d.Get("status").(string) == "DEPROVISIONED" {
err := updateUserStatus(user.Id, d.Get("status").(string), client)
if err != nil {
return fmt.Errorf("[ERROR] Error Updating Status for User: %v", err)
}
}
return resourceUserRead(d, m)
}
func resourceUserRead(d *schema.ResourceData, m interface{}) error {
log.Printf("[INFO] List User %v", d.Get("login").(string))
client := getOktaClientFromMetadata(m)
user, resp, err := client.User.GetUser(d.Id())
if is404(resp.StatusCode) {
d.SetId("")
return nil
}
if err != nil {
return fmt.Errorf("[ERROR] Error Getting User from Okta: %v", err)
}
d.Set("status", mapStatus(user.Status))
d.Set("raw_status", user.Status)
rawMap, err := flattenUser(user, d)
if err != nil {
return err
}
if err = setNonPrimitives(d, rawMap); err != nil {
return err
}
if err = setAdminRoles(d, client); err != nil {
return err
}
// Only sync when it is outlined, an empty list will remove all membership
if _, exists := d.GetOkExists("group_memberships"); exists {
return setGroups(d, client)
}
return nil
}
func resourceUserUpdate(d *schema.ResourceData, m interface{}) error {
log.Printf("[INFO] Update User %v", d.Get("login").(string))
status := d.Get("status").(string)
statusChange := d.HasChange("status")
if status == "STAGED" && statusChange {
return fmt.Errorf("[ERROR] Okta will not allow a user to be updated to STAGED. Can set to STAGED on user creation only.")
}
client := getOktaClientFromMetadata(m)
// There are a few requests here so just making sure the state gets updated per successful downstream change
d.Partial(true)
roleChange := d.HasChange("admin_roles")
groupChange := d.HasChange("group_memberships")
userChange := hasProfileChange(d)
passwordChange := d.HasChange("password")
// run the update status func first so a user that was previously deprovisioned
// can be updated further if it's status changed in it's terraform configs
if statusChange {
err := updateUserStatus(d.Id(), status, client)
if err != nil {
return fmt.Errorf("[ERROR] Error Updating Status for User: %v", err)
}
d.SetPartial("status")
}
if status == "DEPROVISIONED" && (userChange || roleChange || groupChange) {
return errors.New("[ERROR] Only the status of a DEPROVISIONED user can be updated, we detected other change")
}
if userChange {
profile := populateUserProfile(d)
userBody := okta.User{Profile: profile}
_, _, err := client.User.UpdateUser(d.Id(), userBody, nil)
if err != nil {
return fmt.Errorf("[ERROR] Error Updating User in Okta: %v", err)
}
}
if roleChange {
roles := convertInterfaceToStringSet(d.Get("admin_roles"))
if err := updateAdminRolesOnUser(d.Id(), roles, client); err != nil {
return err
}
d.SetPartial("admin_roles")
}
if groupChange {
groups := convertInterfaceToStringSet(d.Get("group_memberships"))
if err := updateGroupsOnUser(d.Id(), groups, client); err != nil {
return err
}
d.SetPartial("group_memberships")
}
if passwordChange {
oldPassword, newPassword := d.GetChange("password")
op := &okta.PasswordCredential{
Value: oldPassword.(string),
}
np := &okta.PasswordCredential{
Value: newPassword.(string),
}
npr := &okta.ChangePasswordRequest{
OldPassword: op,
NewPassword: np,
}
_, _, err := client.User.ChangePassword(d.Id(), *npr, nil)
if err != nil {
return fmt.Errorf("[ERROR] Error Updating User password in Okta: %v", err)
}
}
d.Partial(false)
return resourceUserRead(d, m)
}
// Checks whether any profile keys have changed, this is necessary since the profile is not nested. Also, necessary
// to give a sensible user readable error when they attempt to update a DEPROVISIONED user. Previously
// this error always occurred when you set a user's status to DEPROVISIONED.
func hasProfileChange(d *schema.ResourceData) bool {
for _, k := range profileKeys {
if d.HasChange(k) {
return true
}
}
return false
}
func resourceUserDelete(d *schema.ResourceData, m interface{}) error {
return ensureUserDelete(d.Id(), d.Get("status").(string), getOktaClientFromMetadata(m))
}
func ensureUserDelete(id, status string, client *okta.Client) error {
// only deprovisioned users can be deleted fully from okta
// make two passes on the user if they aren't deprovisioned already to deprovision them first
passes := 2
if status == "DEPROVISIONED" {
passes = 1
}
for i := 0; i < passes; i++ {
_, err := client.User.DeactivateOrDeleteUser(id, nil)
if err != nil {
return fmt.Errorf("Failed to deprovision or delete user from Okta: %v", err)
}
}
return nil
}
func resourceUserExists(d *schema.ResourceData, m interface{}) (bool, error) {
log.Printf("[INFO] Checking Exists for User %v", d.Get("login").(string))
client := m.(*Config).oktaClient
_, resp, err := client.User.GetUser(d.Id())
if is404(resp.StatusCode) {
return false, nil
}
return err == nil, err
}