-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresource_application.go
651 lines (610 loc) · 16.6 KB
/
resource_application.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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
package davinci
import (
"context"
"fmt"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/pingidentity/terraform-provider-davinci/internal/sdk"
dv "github.com/samir-gandhi/davinci-client-go/davinci"
)
func ResourceApplication() *schema.Resource {
return &schema.Resource{
CreateContext: resourceApplicationCreate,
ReadContext: resourceApplicationRead,
UpdateContext: resourceApplicationUpdate,
DeleteContext: resourceApplicationDelete,
Schema: map[string]*schema.Schema{
"environment_id": {
Type: schema.TypeString,
Required: true,
Description: "PingOne environment id",
},
"application_id": {
Type: schema.TypeString,
Computed: true,
Description: "DaVinci generated identifier",
},
"customer_id": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "Application name",
},
"created_date": {
Type: schema.TypeInt,
Computed: true,
},
"api_key_enabled": {
Type: schema.TypeBool,
Optional: true,
Default: true,
Description: "Enabled by default in UI",
},
"api_keys": {
Type: schema.TypeMap,
Computed: true,
Description: "Appplication Api Key",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"metadata": {
Type: schema.TypeMap,
Computed: true,
Description: "Appplication Metadata",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"user_pools": {
Type: schema.TypeMap,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"user_portal": {
Type: schema.TypeSet,
Optional: true,
Description: "User Profile in UI",
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"up_title": {
Type: schema.TypeString,
Optional: true,
},
"add_auth_method_title": {
Type: schema.TypeString,
Optional: true,
},
"remove_auth_method_title": {
Type: schema.TypeString,
Optional: true,
},
"cred_page_title": {
Type: schema.TypeString,
Optional: true,
},
"cred_page_subtitle": {
Type: schema.TypeString,
Optional: true,
},
"name_auth_method_title": {
Type: schema.TypeString,
Optional: true,
},
"name_confirm_btn_text": {
Type: schema.TypeString,
Optional: true,
},
"update_message": {
Type: schema.TypeString,
Optional: true,
},
"update_body_message": {
Type: schema.TypeString,
Optional: true,
},
"remove_message": {
Type: schema.TypeString,
Optional: true,
},
"remove_body_message": {
Type: schema.TypeString,
Optional: true,
},
"remove_confirm_btn_text": {
Type: schema.TypeString,
Optional: true,
},
"remove_cancel_btn_text": {
Type: schema.TypeString,
Optional: true,
},
"flow_timeout_seconds": {
Type: schema.TypeInt,
Optional: true,
},
"show_user_info": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"show_mfa_button": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"show_variables": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"show_logout_button": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
},
},
},
"oauth": {
Type: schema.TypeSet,
Optional: true,
Description: "OIDC configuration",
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enabled": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"values": {
Type: schema.TypeSet,
Optional: true,
Description: "OIDC configuration",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enabled": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"client_secret": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
"enforce_signed_request_openid": {
Type: schema.TypeBool,
Optional: true,
},
"sp_jwks_url": {
Type: schema.TypeString,
Optional: true,
},
"sp_jwks_openid": {
Type: schema.TypeString,
Optional: true,
},
"redirect_uris": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"logout_uris": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"allowed_scopes": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ExactlyOneOf: []string{"openid", "profile", "flow_analytics"},
},
},
"allowed_grants": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ExactlyOneOf: []string{"authorizationCode", "clientCredentials", "implicit"},
},
},
},
},
},
},
},
},
"saml": {
Type: schema.TypeSet,
//TODO - Remove the need for this
// requiring this to accound for returned nil values.
Required: true,
Description: "SAML configuration",
MaxItems: 1,
// DefaultFunc: func() (interface{}, error) {
// smap := []map[string]interface{}{{
// "values": []map[string]interface{}{{
// "enabled": true,
// "enforce_signed_request": false,
// }},
// }}
// return smap, nil
// },
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"values": {
Type: schema.TypeSet,
Optional: true,
Description: "SAML configuration",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enabled": {
Type: schema.TypeBool,
Optional: true,
},
"redirect_uri": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
},
"enforce_signed_request": {
Type: schema.TypeBool,
Optional: true,
},
"audience": {
Type: schema.TypeString,
Optional: true,
},
"sp_cert": {
Type: schema.TypeString,
Optional: true,
},
},
},
},
},
},
},
"policies": {
Type: schema.TypeSet,
Optional: true,
Description: "Flow Policy Config",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"policy_flows": {
Type: schema.TypeSet,
Optional: true,
Description: "Weighted flows that this Application will use",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"flow_id": {
Type: schema.TypeString,
Optional: true,
},
"version_id": {
Type: schema.TypeInt,
Optional: true,
},
"weight": {
Type: schema.TypeInt,
Optional: true,
},
"success_nodes": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
"name": {
Type: schema.TypeString,
Optional: true,
},
"status": {
Type: schema.TypeString,
Optional: true,
Default: "enabled",
},
"policy_id": {
Type: schema.TypeString,
Computed: true,
},
"created_date": {
Type: schema.TypeInt,
Computed: true,
},
},
},
},
},
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
}
}
func resourceApplicationCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(*dv.APIClient)
var diags diag.Diagnostics
err := sdk.CheckAndRefreshAuth(ctx, c, d)
if err != nil {
return diag.FromErr(err)
}
app, err := expandApp(d)
if err != nil {
return diag.FromErr(err)
}
sdkRes, err := sdk.DoRetryable(ctx, func() (interface{}, error) {
return c.CreateInitializedApplication(&c.CompanyID, app)
}, nil)
if err != nil {
return diag.FromErr(err)
}
res, ok := sdkRes.(*dv.App)
if !ok || res.Name == "" {
err = fmt.Errorf("Unable to parse created app response from Davinci API")
return diag.FromErr(err)
}
d.SetId(res.AppID)
resourceApplicationRead(ctx, d, m)
return diags
}
func resourceApplicationRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(*dv.APIClient)
var diags diag.Diagnostics
err := sdk.CheckAndRefreshAuth(ctx, c, d)
if err != nil {
return diag.FromErr(err)
}
appId := d.Id()
skdRes, err := sdk.DoRetryable(ctx, func() (interface{}, error) {
return c.ReadApplication(&c.CompanyID, appId)
}, nil)
if err != nil {
return diag.FromErr(err)
}
resp, ok := skdRes.(*dv.App)
if !ok {
err = fmt.Errorf("failed to cast App type to response on Application with id: %s", appId)
return diag.FromErr(err)
}
if err != nil {
ep, err := c.ParseDvHttpError(err)
if ep.Status == 404 && strings.Contains(ep.Body, "App not found") {
d.SetId("")
// diags = append(diags, diag.Diagnostic{})
return diags
}
return diag.FromErr(err)
}
flatResp, err := flattenApp(resp)
for i, v := range flatResp {
d.Set(i, v)
}
return diags
}
func resourceApplicationUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(*dv.APIClient)
err := sdk.CheckAndRefreshAuth(ctx, c, d)
if err != nil {
return diag.FromErr(err)
}
// if a new policy is added it must be created
if d.HasChanges("policies") {
_, new := d.GetChange("policies")
pols := expandFlowPolicies(new)
for _, v := range pols {
appId := d.Get("application_id").(string)
if v.PolicyID == "" {
sdkRes, err := sdk.DoRetryable(ctx, func() (interface{}, error) {
return c.CreateFlowPolicy(&c.CompanyID, appId, v)
}, nil)
if err != nil {
return diag.FromErr(err)
}
res, ok := sdkRes.(*dv.App)
if !ok || res.Name == "" {
err = fmt.Errorf("failed to cast create policy response to Application on id: %s", appId)
return diag.FromErr(err)
}
} else {
sdkRes, err := sdk.DoRetryable(ctx, func() (interface{}, error) {
return c.UpdateFlowPolicy(&c.CompanyID, appId, v)
}, nil)
if err != nil {
return diag.FromErr(err)
}
res, ok := sdkRes.(*dv.App)
if !ok || res.Name == "" {
err = fmt.Errorf("failed to cast update policy response to Application on id: %s", appId)
return diag.FromErr(err)
}
}
}
}
if d.HasChanges("name", "api_key_enabled", "user_portal", "oauth", "saml") {
app, err := expandApp(d)
if err != nil {
return diag.FromErr(err)
}
app.AppID = d.Get("application_id").(string)
sdkRes, err := sdk.DoRetryable(ctx, func() (interface{}, error) {
return c.UpdateApplication(&c.CompanyID, app)
}, nil)
if err != nil {
return diag.FromErr(err)
}
res, ok := sdkRes.(*dv.App)
if !ok || res.Name == "" {
err = fmt.Errorf("failed to cast update application response to Application on id: %s", app.AppID)
return diag.FromErr(err)
}
}
return resourceApplicationRead(ctx, d, m)
}
func resourceApplicationDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(*dv.APIClient)
var diags diag.Diagnostics
err := sdk.CheckAndRefreshAuth(ctx, c, d)
if err != nil {
return diag.FromErr(err)
}
appId := d.Id()
sdkRes, err := sdk.DoRetryable(ctx, func() (interface{}, error) {
return c.DeleteApplication(&c.CompanyID, appId)
}, nil)
if err != nil {
return diag.FromErr(err)
}
res, ok := sdkRes.(*dv.Message)
if !ok || res.Message == "" {
err = fmt.Errorf("failed to delete update application response to Application on id: %s", appId)
return diag.FromErr(err)
}
d.SetId("")
return diags
}
func expandApp(d *schema.ResourceData) (*dv.AppUpdate, error) {
// Set Top layer.
a := dv.AppUpdate{
Name: d.Get("name").(string),
APIKeyEnabled: d.Get("api_key_enabled").(bool),
}
// Set OAuth
oa, ok := d.GetOk("oauth")
if ok {
oal := oa.(*schema.Set).List()
oaUpdate := &dv.Oauth{}
//Set OAuthEnabled
oam := oal[0].(map[string]interface{})
if oalmEnabled, ok := oam["enabled"].(bool); ok {
oaUpdate.Enabled = oalmEnabled
}
// Set OAuth Values
oamValues := oam["values"].(*schema.Set).List()
if len(oamValues) == 1 {
oamValuesMap := oamValues[0].(map[string]interface{})
oaUpdate.Values = &dv.OauthValues{
Enabled: oamValuesMap["enabled"].(bool),
ClientSecret: oamValuesMap["client_secret"].(string),
EnforceSignedRequestOpenid: oamValuesMap["enforce_signed_request_openid"].(bool),
SpjwksUrl: oamValuesMap["sp_jwks_url"].(string),
SpJwksOpenid: oamValuesMap["sp_jwks_openid"].(string),
}
slist := expandStringList(oamValuesMap["redirect_uris"].(*schema.Set).List())
oaUpdate.Values.RedirectUris = slist
slist = expandStringList(oamValuesMap["logout_uris"].(*schema.Set).List())
oaUpdate.Values.LogoutUris = slist
slist = expandStringList(oamValuesMap["allowed_scopes"].(*schema.Set).List())
oaUpdate.Values.AllowedScopes = slist
slist = expandStringList(oamValuesMap["allowed_grants"].(*schema.Set).List())
oaUpdate.Values.AllowedGrants = slist
}
if len(oamValues) > 1 {
return nil, fmt.Errorf("Only one set for OAuth Values allowed")
}
a.Oauth = oaUpdate
}
//Set Saml
saml, ok := d.GetOk("saml")
if ok {
sl := saml.(*schema.Set).List()
if len(sl) == 1 {
svUpdate := &dv.Saml{}
sm := sl[0].(map[string]interface{})
samlValues := sm["values"].(*schema.Set).List()
if len(samlValues) == 1 {
svMap := samlValues[0].(map[string]interface{})
svUpdate.Values = &dv.SamlValues{
Enabled: svMap["enabled"].(bool),
EnforceSignedRequest: svMap["enforce_signed_request"].(bool),
RedirectURI: svMap["redirect_uri"].(string),
Audience: svMap["audience"].(string),
SpCert: svMap["sp_cert"].(string),
}
a.Saml = svUpdate
}
if len(samlValues) > 1 {
return nil, fmt.Errorf("Only one set for Saml Values allowed")
}
}
if len(sl) > 1 {
return nil, fmt.Errorf("Only one set for Saml Values allowed")
}
}
//Set User Portal
uv, ok := d.GetOk("user_portal")
if ok {
upValues := uv.(*schema.Set).List()
if len(upValues) == 1 {
up := &dv.UserPortal{}
up.Values = upValues[0].(*dv.UserPortalValues)
a.UserPortal = up
}
if len(upValues) > 1 {
return nil, fmt.Errorf("Only one set for User Portal Values allowed")
}
}
//Set Flow Policies
fp, ok := d.GetOk("policies")
if ok {
fvUpdate := expandFlowPolicies(fp)
if len(fvUpdate) > 0 {
a.Policies = fvUpdate
}
}
return &a, nil
}
func expandStringList(configured []interface{}) []string {
vs := make([]string, 0, len(configured))
for _, v := range configured {
val, ok := v.(string)
if ok && val != "" {
vs = append(vs, v.(string))
}
}
return vs
}
func expandFlowPolicies(fp interface{}) []dv.Policy {
fl := fp.(*schema.Set).List()
fvUpdate := []dv.Policy{}
if len(fl) > 0 {
for _, v := range fl {
flMap := v.(map[string]interface{})
thisFvUpdate := dv.Policy{
Name: flMap["name"].(string),
Status: flMap["status"].(string),
PolicyID: flMap["policy_id"].(string),
}
thisPolicyFlows := flMap["policy_flows"].(*schema.Set).List()
for _, w := range thisPolicyFlows {
flPMap := w.(map[string]interface{})
thisFvPUpdate := dv.PolicyFlow{
FlowID: flPMap["flow_id"].(string),
VersionID: flPMap["version_id"].(int),
Weight: flPMap["weight"].(int),
}
thisFvUpdate.PolicyFlows = append(thisFvUpdate.PolicyFlows, thisFvPUpdate)
}
fvUpdate = append(fvUpdate, thisFvUpdate)
}
}
return fvUpdate
}