This repository has been archived by the owner on Aug 5, 2020. It is now read-only.
forked from okta/terraform-provider-okta
/
app.go
462 lines (404 loc) · 13.3 KB
/
app.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
package okta
import (
"encoding/json"
"sync"
"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"
)
var appUserResource = &schema.Resource{
Schema: map[string]*schema.Schema{
"scope": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Scope of application user.",
},
"id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "User ID.",
},
"username": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "Username for user.",
},
"password": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "Password for user application.",
},
},
}
var baseAppSchema = map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "name of app.",
},
"label": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "Pretty name of app.",
},
"sign_on_mode": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Sign on mode of application.",
},
"users": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: appUserResource,
Description: "Users associated with the application",
},
"groups": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "Groups associated with the application",
},
"status": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "ACTIVE",
ValidateFunc: validation.StringInSlice([]string{"ACTIVE", "INACTIVE"}, false),
Description: "Status of application.",
},
}
var appVisibilitySchema = map[string]*schema.Schema{
"auto_submit_toolbar": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Display auto submit toolbar",
},
"hide_ios": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Do not display application icon on mobile app",
},
"hide_web": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Do not display application icon to users",
},
}
var baseappSwaSchema = map[string]*schema.Schema{
"accessibility_self_service": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Enable self service",
},
"accessibility_error_redirect_url": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "Custom error page URL",
ValidateFunc: validateIsURL,
},
"auto_submit_toolbar": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Display auto submit toolbar",
},
"hide_ios": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Do not display application icon on mobile app",
},
"hide_web": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Do not display application icon to users",
},
"user_name_template": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Username template",
},
"user_name_template_type": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Username template type",
},
}
// Wish there was some better polymorphism that could make these similarities easier to deal with
func appRead(d *schema.ResourceData, name, status, signOn, label string, accy *okta.ApplicationAccessibility, vis *okta.ApplicationVisibility) {
d.Set("name", name)
d.Set("status", status)
d.Set("sign_on_mode", signOn)
d.Set("label", label)
d.Set("accessibility_self_service", accy.SelfService)
d.Set("accessibility_error_redirect_url", accy.ErrorRedirectUrl)
d.Set("auto_submit_toolbar", vis.AutoSubmitToolbar)
d.Set("hide_ios", vis.Hide.IOS)
d.Set("hide_web", vis.Hide.Web)
}
func buildAppSchema(appSchema map[string]*schema.Schema) map[string]*schema.Schema {
return buildSchema(baseAppSchema, appSchema)
}
func buildAppSchemaWithVisibility(appSchema map[string]*schema.Schema) map[string]*schema.Schema {
schema := buildSchema(baseAppSchema, appSchema)
return buildSchema(appVisibilitySchema, schema)
}
func buildSchemeCreds(d *schema.ResourceData) *okta.SchemeApplicationCredentials {
revealPass := d.Get("reveal_password").(bool)
return &okta.SchemeApplicationCredentials{
RevealPassword: &revealPass,
Scheme: d.Get("credentials_scheme").(string),
UserName: d.Get("shared_username").(string),
Password: &okta.PasswordCredential{
Value: d.Get("shared_password").(string),
},
}
}
func buildAppSwaSchema(appSchema map[string]*schema.Schema) map[string]*schema.Schema {
s := buildAppSchema(appSchema)
return buildSchema(baseappSwaSchema, s)
}
func buildVisibility(d *schema.ResourceData) *okta.ApplicationVisibility {
autoSubmit := d.Get("auto_submit_toolbar").(bool)
hideMobile := d.Get("hide_ios").(bool)
hideWeb := d.Get("hide_web").(bool)
return &okta.ApplicationVisibility{
AutoSubmitToolbar: &autoSubmit,
Hide: &okta.ApplicationVisibilityHide{
IOS: &hideMobile,
Web: &hideWeb,
},
}
}
func fetchApp(d *schema.ResourceData, m interface{}, app okta.App) error {
return fetchAppById(d.Id(), m, app)
}
func fetchAppById(id string, m interface{}, app okta.App) error {
client := getOktaClientFromMetadata(m)
_, response, err := client.Application.GetApplication(id, app, nil)
// We don't want to consider a 404 an error in some cases and thus the delineation
if response != nil && response.StatusCode == 404 {
app = nil
return nil
}
return responseErr(response, err)
}
func updateAppById(id string, m interface{}, app okta.App) error {
client := getOktaClientFromMetadata(m)
_, response, err := client.Application.UpdateApplication(id, app)
// We don't want to consider a 404 an error in some cases and thus the delineation
if response.StatusCode == 404 {
app = nil
return nil
}
return responseErr(response, err)
}
func handleAppGroups(id string, d *schema.ResourceData, client *okta.Client) []func() error {
existingGroup, _, _ := client.Application.ListApplicationGroupAssignments(id, &query.Params{})
var (
asyncActionList []func() error
groupIdList []string
)
if arr, ok := d.GetOk("groups"); ok {
rawArr := arr.(*schema.Set).List()
groupIdList = make([]string, len(rawArr))
for i, gID := range rawArr {
groupID := gID.(string)
groupIdList[i] = groupID
if !containsGroup(existingGroup, groupID) {
asyncActionList = append(asyncActionList, func() error {
_, resp, err := client.Application.CreateApplicationGroupAssignment(id, groupID, okta.ApplicationGroupAssignment{})
return responseErr(resp, err)
})
}
}
}
for _, group := range existingGroup {
if !contains(groupIdList, group.Id) {
groupID := group.Id
asyncActionList = append(asyncActionList, func() error {
return suppressErrorOn404(client.Application.DeleteApplicationGroupAssignment(id, groupID))
})
}
}
return asyncActionList
}
func containsGroup(groupList []*okta.ApplicationGroupAssignment, id string) bool {
for _, group := range groupList {
if group.Id == id {
return true
}
}
return false
}
func containsAppUser(userList []*okta.AppUser, id string) bool {
for _, user := range userList {
if user.Id == id && user.Scope == "USER" {
return true
}
}
return false
}
// Handles the assigning of groups and users to Applications. Does so asynchronously.
func handleAppGroupsAndUsers(id string, d *schema.ResourceData, m interface{}) error {
var wg sync.WaitGroup
resultChan := make(chan []*result, 1)
client := getOktaClientFromMetadata(m)
groupHandlers := handleAppGroups(id, d, client)
userHandlers := handleAppUsers(id, d, client)
con := getParallelismFromMetadata(m)
promiseAll(con, &wg, resultChan, append(groupHandlers, userHandlers...)...)
wg.Wait()
return getPromiseError(<-resultChan, "failed to associate user or groups with application")
}
func handleAppUsers(id string, d *schema.ResourceData, client *okta.Client) []func() error {
// Looking upstream for existing user's, rather then the config for accuracy.
existingUsers, _, _ := client.Application.ListApplicationUsers(id, &query.Params{})
var (
asyncActionList []func() error
users []interface{}
userIDList []string
)
if set, ok := d.GetOk("users"); ok {
users = set.(*schema.Set).List()
userIDList = make([]string, len(users))
for i, user := range users {
userProfile := user.(map[string]interface{})
uID := userProfile["id"].(string)
userIDList[i] = uID
if !containsAppUser(existingUsers, uID) {
username := userProfile["username"].(string)
// Not required
password, _ := userProfile["password"].(string)
asyncActionList = append(asyncActionList, func() error {
_, _, err := client.Application.AssignUserToApplication(id, okta.AppUser{
Id: uID,
Credentials: &okta.AppUserCredentials{
UserName: username,
Password: &okta.AppUserPasswordCredential{
Value: password,
},
},
})
return err
})
}
}
}
for _, user := range existingUsers {
if user.Scope == "USER" {
if !contains(userIDList, user.Id) {
userID := user.Id
asyncActionList = append(asyncActionList, func() error {
return suppressErrorOn404(client.Application.DeleteApplicationUser(id, userID, nil))
})
}
}
}
return asyncActionList
}
func resourceAppExists(d *schema.ResourceData, m interface{}) (bool, error) {
app := okta.NewApplication()
err := fetchApp(d, m, app)
// Not sure if a non-nil app with an empty ID is possible but checking to avoid false positives.
return app != nil && app.Id != "", err
}
func setAppStatus(d *schema.ResourceData, client *okta.Client, status string, desiredStatus string) error {
if status != desiredStatus {
if desiredStatus == "INACTIVE" {
return responseErr(client.Application.DeactivateApplication(d.Id()))
} else if desiredStatus == "ACTIVE" {
return responseErr(client.Application.ActivateApplication(d.Id()))
}
}
return nil
}
func syncGroupsAndUsers(id string, d *schema.ResourceData, m interface{}) error {
client := getOktaClientFromMetadata(m)
// Temporary high limit to avoid issues short term. Need to support pagination here
userList, _, err := client.Application.ListApplicationUsers(id, &query.Params{Limit: 200})
if err != nil {
return err
}
// Temporary high limit to avoid issues short term. Need to support pagination here
groupList, _, err := client.Application.ListApplicationGroupAssignments(id, &query.Params{Limit: 200})
if err != nil {
return err
}
flatGroupList := make([]interface{}, len(groupList))
for i, g := range groupList {
flatGroupList[i] = g.Id
}
var flattenedUserList []interface{}
for _, user := range userList {
if user.Scope == "USER" {
flattenedUserList = append(flattenedUserList, map[string]interface{}{
"id": user.Id,
"username": user.Credentials.UserName,
})
}
}
flatMap := map[string]interface{}{}
if len(flattenedUserList) > 0 {
flatMap["users"] = schema.NewSet(schema.HashResource(appUserResource), flattenedUserList)
}
if len(flatGroupList) > 0 {
flatMap["groups"] = schema.NewSet(schema.HashString, flatGroupList)
}
return setNonPrimitives(d, flatMap)
}
// setAppSettings available preconfigured SAML and OAuth applications vary wildly on potential app settings, thus
// it is a generic map. This logic simply weeds out any empty string values.
func setAppSettings(d *schema.ResourceData, settings *okta.ApplicationSettingsApplication) error {
flatMap := map[string]interface{}{}
for key, val := range *settings {
if str, ok := val.(string); ok {
if str != "" {
flatMap[key] = str
}
} else if val != nil {
flatMap[key] = val
}
}
payload, err := json.Marshal(flatMap)
if err != nil {
return err
}
return d.Set("app_settings_json", string(payload))
}
func syncSamlSettings(d *schema.ResourceData, set *okta.SamlApplicationSettings) error {
d.Set("default_relay_state", set.SignOn.DefaultRelayState)
d.Set("sso_url", set.SignOn.SsoAcsUrl)
d.Set("recipient", set.SignOn.Recipient)
d.Set("destination", set.SignOn.Destination)
d.Set("audience", set.SignOn.Audience)
d.Set("idp_issuer", set.SignOn.IdpIssuer)
d.Set("subject_name_id_template", set.SignOn.SubjectNameIdTemplate)
d.Set("subject_name_id_format", set.SignOn.SubjectNameIdFormat)
d.Set("response_signed", set.SignOn.ResponseSigned)
d.Set("assertion_signed", set.SignOn.AssertionSigned)
d.Set("signature_algorithm", set.SignOn.SignatureAlgorithm)
d.Set("digest_algorithm", set.SignOn.DigestAlgorithm)
d.Set("honor_force_authn", set.SignOn.HonorForceAuthn)
d.Set("authn_context_class_ref", set.SignOn.AuthnContextClassRef)
attrStatements := set.SignOn.AttributeStatements
arr := make([]map[string]interface{}, len(attrStatements))
for i, st := range attrStatements {
arr[i] = map[string]interface{}{
"name": st.Name,
"namespace": st.Namespace,
"type": st.Type,
"values": st.Values,
"filter_type": st.FilterType,
"filter_value": st.FilterValue,
}
}
return setNonPrimitives(d, map[string]interface{}{
"attribute_statements": arr,
})
}