-
Notifications
You must be signed in to change notification settings - Fork 310
/
Copy pathapplication_resource.go
1754 lines (1542 loc) · 69.4 KB
/
application_resource.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
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package applications
import (
"context"
"errors"
"fmt"
"log"
"net/url"
"strings"
"time"
"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/lang/response"
applicationBeta "github.com/hashicorp/go-azure-sdk/microsoft-graph/applications/beta/application"
"github.com/hashicorp/go-azure-sdk/microsoft-graph/applications/stable/application"
"github.com/hashicorp/go-azure-sdk/microsoft-graph/applications/stable/logo"
"github.com/hashicorp/go-azure-sdk/microsoft-graph/applications/stable/owner"
"github.com/hashicorp/go-azure-sdk/microsoft-graph/applicationtemplates/stable/applicationtemplate"
"github.com/hashicorp/go-azure-sdk/microsoft-graph/common-types/beta"
"github.com/hashicorp/go-azure-sdk/microsoft-graph/common-types/stable"
"github.com/hashicorp/go-azure-sdk/microsoft-graph/serviceprincipals/stable/serviceprincipal"
"github.com/hashicorp/go-azure-sdk/sdk/nullable"
"github.com/hashicorp/go-azure-sdk/sdk/odata"
"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform-provider-azuread/internal/clients"
"github.com/hashicorp/terraform-provider-azuread/internal/helpers/applications"
"github.com/hashicorp/terraform-provider-azuread/internal/helpers/consistency"
"github.com/hashicorp/terraform-provider-azuread/internal/helpers/credentials"
"github.com/hashicorp/terraform-provider-azuread/internal/helpers/tf"
"github.com/hashicorp/terraform-provider-azuread/internal/helpers/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azuread/internal/helpers/tf/validation"
"github.com/hashicorp/terraform-provider-azuread/internal/services/applications/migrations"
applicationsValidate "github.com/hashicorp/terraform-provider-azuread/internal/services/applications/validate"
)
func applicationResource() *pluginsdk.Resource {
return &pluginsdk.Resource{
CreateContext: applicationResourceCreate,
ReadContext: applicationResourceRead,
UpdateContext: applicationResourceUpdate,
DeleteContext: applicationResourceDelete,
CustomizeDiff: applicationResourceCustomizeDiff,
Timeouts: &pluginsdk.ResourceTimeout{
Create: pluginsdk.DefaultTimeout(10 * time.Minute),
Read: pluginsdk.DefaultTimeout(5 * time.Minute),
Update: pluginsdk.DefaultTimeout(10 * time.Minute),
Delete: pluginsdk.DefaultTimeout(5 * time.Minute),
},
Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
if _, errs := stable.ValidateApplicationID(id, "id"); len(errs) > 0 {
out := ""
for _, err := range errs {
out += err.Error()
}
return fmt.Errorf(out)
}
return nil
}),
SchemaVersion: 2,
StateUpgraders: []pluginsdk.StateUpgrader{
{
Type: migrations.ResourceApplicationInstanceResourceV0().CoreConfigSchema().ImpliedType(),
Upgrade: migrations.ResourceApplicationInstanceStateUpgradeV0,
Version: 0,
},
{
Type: migrations.ResourceApplicationInstanceResourceV1().CoreConfigSchema().ImpliedType(),
Upgrade: migrations.ResourceApplicationInstanceStateUpgradeV1,
Version: 1,
},
},
Schema: map[string]*pluginsdk.Schema{
"display_name": {
Description: "The display name for the application",
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"api": {
Type: pluginsdk.TypeList,
Optional: true,
MaxItems: 1,
DiffSuppressFunc: applicationDiffSuppress,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"known_client_applications": {
Description: "Used for bundling consent if you have a solution that contains two parts: a client app and a custom web API app",
Type: pluginsdk.TypeSet,
Optional: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
ValidateFunc: validation.IsUUID,
},
},
"mapped_claims_enabled": {
Description: "Allows an application to use claims mapping without specifying a custom signing key",
Type: pluginsdk.TypeBool,
Optional: true,
},
"oauth2_permission_scope": {
Description: "One or more `oauth2_permission_scope` blocks to describe delegated permissions exposed by the web API represented by this application",
Type: pluginsdk.TypeSet,
Optional: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"id": {
Description: "The unique identifier of the delegated permission",
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.IsUUID,
},
"admin_consent_description": {
Description: "Delegated permission description that appears in all tenant-wide admin consent experiences, intended to be read by an administrator granting the permission on behalf of all users",
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"admin_consent_display_name": {
Description: "Display name for the delegated permission, intended to be read by an administrator granting the permission on behalf of all users",
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"enabled": {
Description: "Determines if the permission scope is enabled",
Type: pluginsdk.TypeBool,
Optional: true,
Default: true,
},
"type": {
Description: "Whether this delegated permission should be considered safe for non-admin users to consent to on behalf of themselves, or whether an administrator should be required for consent to the permissions",
Type: pluginsdk.TypeString,
Optional: true,
Default: PermissionScopeTypeUser,
ValidateFunc: validation.StringInSlice(possibleValuesForPermissionScopeType, false),
},
"user_consent_description": {
Description: "Delegated permission description that appears in the end user consent experience, intended to be read by a user consenting on their own behalf",
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"user_consent_display_name": {
Description: "Display name for the delegated permission that appears in the end user consent experience",
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"value": {
Description: "The value that is used for the `scp` claim in OAuth 2.0 access tokens",
Type: pluginsdk.TypeString,
Optional: true,
ValidateDiagFunc: applicationsValidate.RoleScopeClaimValue,
},
},
},
},
"requested_access_token_version": {
Description: "The access token version expected by this resource",
Type: pluginsdk.TypeInt,
Optional: true,
Default: 1,
ValidateDiagFunc: func(i interface{}, path cty.Path) (ret pluginsdk.Diagnostics) {
v, ok := i.(int)
if !ok {
ret = append(ret, pluginsdk.Diagnostic{
Severity: pluginsdk.DiagError,
Summary: "Expected an integer value",
AttributePath: path,
})
return
}
if v < 1 || v > 2 {
ret = append(ret, pluginsdk.Diagnostic{
Severity: pluginsdk.DiagError,
Summary: "Value must be one of: 1, 2",
AttributePath: path,
})
}
return
},
},
},
},
},
"app_role": {
Type: pluginsdk.TypeSet,
Optional: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"id": {
Description: "The unique identifier of the app role",
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.IsUUID,
},
"allowed_member_types": {
Description: "Specifies whether this app role definition can be assigned to users and groups by setting to `User`, or to other applications (that are accessing this application in a standalone scenario) by setting to `Application`, or to both",
Type: pluginsdk.TypeSet,
Required: true,
MinItems: 1,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
ValidateFunc: validation.StringInSlice(possibleValuesForAppRoleAllowedMemberType, false),
},
},
"description": {
Description: "Description of the app role that appears when the role is being assigned and, if the role functions as an application permissions, during the consent experiences",
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"display_name": {
Description: "Display name for the app role that appears during app role assignment and in consent experiences",
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"enabled": {
Description: "Determines if the app role is enabled",
Type: pluginsdk.TypeBool,
Optional: true,
Default: true,
},
"value": {
Description: "The value that is used for the `roles` claim in ID tokens and OAuth 2.0 access tokens that are authenticating an assigned service or user principal",
Type: pluginsdk.TypeString,
Optional: true,
ValidateDiagFunc: applicationsValidate.RoleScopeClaimValue,
},
},
},
},
"app_role_ids": {
Description: "Mapping of app role names to UUIDs",
Type: pluginsdk.TypeMap,
Computed: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
"description": {
Description: "Description of the application as shown to end users",
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validation.StringLenBetween(0, 1024),
},
"device_only_auth_enabled": {
Description: "Specifies whether this application supports device authentication without a user.",
Type: pluginsdk.TypeBool,
Optional: true,
},
"fallback_public_client_enabled": {
Description: "Specifies whether the application is a public client. Appropriate for apps using token grant flows that don't use a redirect URI",
Type: pluginsdk.TypeBool,
Optional: true,
},
"feature_tags": {
Description: "Block of features to configure for this application using tags",
Type: pluginsdk.TypeList,
Optional: true,
Computed: true,
ConflictsWith: []string{"tags"},
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"custom_single_sign_on": {
Description: "Whether this application represents a custom SAML application for linked service principals",
Type: pluginsdk.TypeBool,
Optional: true,
},
"enterprise": {
Description: "Whether this application represents an Enterprise Application for linked service principals",
Type: pluginsdk.TypeBool,
Optional: true,
},
"gallery": {
Description: "Whether this application represents a gallery application for linked service principals",
Type: pluginsdk.TypeBool,
Optional: true,
},
"hide": {
Description: "Whether this application is invisible to users in My Apps and Office 365 Launcher",
Type: pluginsdk.TypeBool,
Optional: true,
},
},
},
},
"group_membership_claims": {
Description: "Configures the `groups` claim issued in a user or OAuth 2.0 access token that the app expects",
Type: pluginsdk.TypeSet,
Optional: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
ValidateFunc: validation.StringInSlice(possibleValuesForGroupMembershipClaim, false),
},
},
"identifier_uris": {
Description: "The user-defined URI(s) that uniquely identify an application within its Azure AD tenant, or within a verified custom domain if the application is multi-tenant",
Type: pluginsdk.TypeSet,
Optional: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
// Extensive validation is intentionally avoided here, as the accepted values are undocumented, vary wildly and are
// different for each user depending on the tenant domain configuration, whether the application is used for SSO etc
ValidateFunc: validation.StringIsNotEmpty,
},
},
"logo_image": {
Description: "Base64 encoded logo image in gif, png or jpeg format",
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validation.StringIsBase64,
},
"marketing_url": {
Description: "URL of the application's marketing page",
Type: pluginsdk.TypeString,
Optional: true,
},
"notes": {
Description: "User-specified notes relevant for the management of the application",
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validation.StringIsNotEmpty,
},
// This is a top level attribute because d.SetNewComputed() doesn't work inside a block
"oauth2_permission_scope_ids": {
Description: "Mapping of OAuth2.0 permission scope names to UUIDs",
Type: pluginsdk.TypeMap,
Computed: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
"oauth2_post_response_required": {
Description: "Specifies whether, as part of OAuth 2.0 token requests, Azure AD allows POST requests, as opposed to GET requests.",
Type: pluginsdk.TypeBool,
Optional: true,
},
"optional_claims": {
Type: pluginsdk.TypeList,
Optional: true,
MaxItems: 1,
DiffSuppressFunc: applicationDiffSuppress,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"access_token": schemaOptionalClaims(),
"id_token": schemaOptionalClaims(),
"saml2_token": schemaOptionalClaims(),
},
},
},
"owners": {
Description: "A list of object IDs of principals that will be granted ownership of the application",
Type: pluginsdk.TypeSet,
Optional: true,
Set: pluginsdk.HashString,
MaxItems: 100,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
ValidateFunc: validation.IsUUID,
},
},
//lintignore:S018 // We are intentionally using TypeSet here to effect a replace-style representation in the diff for this block
"password": {
Description: "App password definition",
Type: pluginsdk.TypeSet,
Optional: true,
MaxItems: 1,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"display_name": {
Description: "A display name for the password",
Type: pluginsdk.TypeString,
Required: true,
},
"start_date": {
Description: "The start date from which the password is valid, formatted as an RFC3339 date string (e.g. `2018-01-01T01:02:03Z`). If this isn't specified, the current date is used",
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.IsRFC3339Time,
},
"end_date": {
Description: "The end date until which the password is valid, formatted as an RFC3339 date string (e.g. `2018-01-01T01:02:03Z`)",
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.IsRFC3339Time,
},
"key_id": {
Description: "A UUID used to uniquely identify this password credential",
Type: pluginsdk.TypeString,
Computed: true,
},
"value": {
Description: "The password for this application, which is generated by Azure Active Directory",
Type: pluginsdk.TypeString,
Computed: true,
Sensitive: true,
},
},
},
},
"privacy_statement_url": {
Description: "URL of the application's privacy statement",
Type: pluginsdk.TypeString,
Optional: true,
},
"public_client": {
Type: pluginsdk.TypeList,
Optional: true,
MaxItems: 1,
DiffSuppressFunc: applicationDiffSuppress,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"redirect_uris": {
Description: "The URLs where user tokens are sent for sign-in, or the redirect URIs where OAuth 2.0 authorization codes and access tokens are sent",
Type: pluginsdk.TypeSet,
Optional: true,
MaxItems: 256,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
ValidateFunc: validation.IsRedirectUriFunc(true, true),
},
},
},
},
},
"required_resource_access": {
Type: pluginsdk.TypeSet,
Optional: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"resource_app_id": {
Description: "",
Type: pluginsdk.TypeString,
Required: true,
},
"resource_access": {
Description: "",
Type: pluginsdk.TypeList,
Required: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"id": {
Description: "",
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.IsUUID,
},
"type": {
Description: "",
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice(possibleValuesForResourceAccessType, false),
},
},
},
},
},
},
},
"service_management_reference": {
Description: "References application or service contact information from a Service or Asset Management database",
Type: pluginsdk.TypeString,
Optional: true,
},
"sign_in_audience": {
Description: "The Microsoft account types that are supported for the current application",
Type: pluginsdk.TypeString,
Optional: true,
Default: SignInAudienceAzureADMyOrg,
ValidateFunc: validation.StringInSlice(possibleValuesForSignInAudience, false),
},
"single_page_application": {
Type: pluginsdk.TypeList,
Optional: true,
MaxItems: 1,
DiffSuppressFunc: applicationDiffSuppress,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"redirect_uris": {
Description: "The URLs where user tokens are sent for sign-in, or the redirect URIs where OAuth 2.0 authorization codes and access tokens are sent",
Type: pluginsdk.TypeSet,
Optional: true,
MaxItems: 256,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
ValidateFunc: validation.IsRedirectUriFunc(false, false),
},
},
},
},
},
"support_url": {
Description: "URL of the application's support page",
Type: pluginsdk.TypeString,
Optional: true,
},
"tags": {
Description: "A set of tags to apply to the application",
Type: pluginsdk.TypeSet,
Optional: true,
Computed: true,
Set: pluginsdk.HashString,
ConflictsWith: []string{"feature_tags"},
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
"template_id": {
Description: "Unique ID of the application template from which this application is created",
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: validation.IsUUID,
},
"terms_of_service_url": {
Description: "URL of the application's terms of service statement",
Type: pluginsdk.TypeString,
Optional: true,
},
"web": {
Type: pluginsdk.TypeList,
Optional: true,
MaxItems: 1,
DiffSuppressFunc: applicationDiffSuppress,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"homepage_url": {
Description: "Home page or landing page of the application",
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validation.IsHttpOrHttpsUrl,
},
"logout_url": {
Description: "The URL that will be used by Microsoft's authorization service to sign out a user using front-channel, back-channel or SAML logout protocols",
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validation.IsLogoutUrl,
},
"redirect_uris": {
Description: "The URLs where user tokens are sent for sign-in, or the redirect URIs where OAuth 2.0 authorization codes and access tokens are sent",
Type: pluginsdk.TypeSet,
Optional: true,
MaxItems: 256,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
ValidateFunc: validation.IsRedirectUriFunc(true, false),
},
},
"implicit_grant": {
Type: pluginsdk.TypeList,
Optional: true,
MaxItems: 1,
DiffSuppressFunc: applicationDiffSuppress,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"access_token_issuance_enabled": {
Description: "Whether this web application can request an access token using OAuth 2.0 implicit flow",
Type: pluginsdk.TypeBool,
Optional: true,
},
"id_token_issuance_enabled": {
Description: "Whether this web application can request an ID token using OAuth 2.0 implicit flow",
Type: pluginsdk.TypeBool,
Optional: true,
},
},
},
},
},
},
},
"client_id": {
Description: "The Client ID (also called Application ID)",
Type: pluginsdk.TypeString,
Computed: true,
},
"object_id": {
Description: "The application's object ID",
Type: pluginsdk.TypeString,
Computed: true,
},
"logo_url": {
Description: "CDN URL to the application's logo",
Type: pluginsdk.TypeString,
Computed: true,
},
"prevent_duplicate_names": {
Description: "If `true`, will return an error if an existing application is found with the same name",
Type: pluginsdk.TypeBool,
Optional: true,
Default: false,
},
"publisher_domain": {
Description: "The verified publisher domain for the application",
Type: pluginsdk.TypeString,
Computed: true,
},
"disabled_by_microsoft": {
Description: "Whether Microsoft has disabled the registered application",
Type: pluginsdk.TypeString,
Computed: true,
},
},
}
}
func applicationResourceCustomizeDiff(ctx context.Context, diff *pluginsdk.ResourceDiff, meta interface{}) error {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, 5*time.Minute)
defer cancel()
client := meta.(*clients.Client).Applications.ApplicationClient
oldDisplayName, newDisplayName := diff.GetChange("display_name")
if diff.Get("prevent_duplicate_names").(bool) && pluginsdk.ValueIsNotEmptyOrUnknown(newDisplayName) &&
(oldDisplayName.(string) == "" || oldDisplayName.(string) != newDisplayName.(string)) {
result, err := applicationFindByName(ctx, client, newDisplayName.(string))
if err != nil {
return fmt.Errorf("could not check for existing application(s): %+v", err)
}
if result != nil && len(*result) > 0 {
for _, existingApp := range *result {
if existingApp.Id == nil {
return fmt.Errorf("API error: application returned with nil object ID during duplicate name check")
}
if diff.Id() == "" || diff.Id() == *existingApp.Id {
return tf.ImportAsDuplicateError("azuread_application", *existingApp.Id, newDisplayName.(string))
}
}
}
}
// Validate roles and scopes to check for duplicate IDs or values
if err := applicationValidateRolesScopes(diff.Get("app_role").(*pluginsdk.Set).List(), diff.Get("api.0.oauth2_permission_scope").(*pluginsdk.Set).List()); err != nil {
return fmt.Errorf("checking for duplicate app roles / OAuth2.0 permission scopes: %v", err)
}
// If app roles or permission scopes have changed, the corresponding maps indexed by value will also change
if diff.HasChange("app_role") {
diff.SetNewComputed("app_role_ids")
}
if diff.HasChange("api.0.oauth2_permission_scope") {
diff.SetNewComputed("oauth2_permission_scope_ids")
}
// If the logo image changes, the CDN URL will change
if diff.HasChange("logo_image") {
diff.SetNewComputed("logo_url")
}
// The following validation is taken from https://docs.microsoft.com/en-gb/azure/active-directory/develop/supported-accounts-validation
// These apply only when personal account sign-ins are enabled for an application, and are enforced at plan time to avoid breaking existing
// applications that change from AAD (corporate) account sign-ins to personal account sign-ins
if s := diff.Get("sign_in_audience").(string); s == SignInAudienceAzureADandPersonalMicrosoftAccount || s == SignInAudiencePersonalMicrosoftAccount {
oauth2PermissionScopes := diff.Get("api.0.oauth2_permission_scope").(*pluginsdk.Set).List()
identifierUris := diff.Get("identifier_uris").(*pluginsdk.Set).List()
pubRedirectUris := diff.Get("public_client.0.redirect_uris").(*pluginsdk.Set).List()
spaRedirectUris := diff.Get("single_page_application.0.redirect_uris").(*pluginsdk.Set).List()
webRedirectUris := diff.Get("web.0.redirect_uris").(*pluginsdk.Set).List()
allRedirectUris := append(pubRedirectUris, append(spaRedirectUris, webRedirectUris...)...) //nolint:gocritic
// applications must use v2 access tokens with personal account sign-ins
if v, ok := diff.GetOk("api.0.requested_access_token_version"); !ok || v.(int) == 1 {
return fmt.Errorf("`requested_access_token_version` must be 2 when `sign_in_audience` is %q or %q",
SignInAudienceAzureADandPersonalMicrosoftAccount, SignInAudiencePersonalMicrosoftAccount)
}
// maximum number of scopes is 100 with personal account sign-ins
if len(oauth2PermissionScopes) > 100 {
return fmt.Errorf("maximum of 100 `oauth2_permission_scope` blocks are supported when `sign_in_audience` is %q or %q",
SignInAudienceAzureADandPersonalMicrosoftAccount, SignInAudiencePersonalMicrosoftAccount)
}
// scope name maximum length is 40 characters with personal account sign-ins
for _, raw := range oauth2PermissionScopes {
scope := raw.(map[string]interface{})
if v, ok := scope["value"]; ok {
if len(v.(string)) > 40 {
return fmt.Errorf("`value` property in the `oauth2_permission_scope` block must be 40 characters or less when `sign_in_audience` is %q or %q",
SignInAudienceAzureADandPersonalMicrosoftAccount, SignInAudiencePersonalMicrosoftAccount)
}
}
}
// maximum number of scopes is 100 with personal account sign-ins
if len(oauth2PermissionScopes) > 100 {
return fmt.Errorf("maximum of 100 `oauth2_permission_scope` blocks are supported when `sign_in_audience` is %q or %q",
SignInAudienceAzureADandPersonalMicrosoftAccount, SignInAudiencePersonalMicrosoftAccount)
}
// scope name maximum length is 40 characters with personal account sign-ins
for _, raw := range oauth2PermissionScopes {
scope := raw.(map[string]interface{})
if v, ok := scope["value"]; ok {
if len(v.(string)) > 40 {
return fmt.Errorf("`value` property in the `oauth2_permission_scope` block must be 40 characters or less when `sign_in_audience` is %q or %q",
SignInAudienceAzureADandPersonalMicrosoftAccount, SignInAudiencePersonalMicrosoftAccount)
}
}
}
// urn scheme not supported with personal account sign-ins
for _, v := range identifierUris {
if _, errs := validation.IsUriFunc([]string{"http", "https", "api", "ms-appx"}, false, false, false)(v, "identifier_uris"); len(errs) > 0 {
return fmt.Errorf("`identifier_uris` is invalid. The URN scheme is not supported when `sign_in_audience` is %q or %q",
SignInAudienceAzureADandPersonalMicrosoftAccount, SignInAudiencePersonalMicrosoftAccount)
}
}
// maximum of 50 identifier_uris with personal account sign-ins
if len(identifierUris) > 50 {
return fmt.Errorf("`identifier_uris` must have no more than 50 URIs when `sign_in_audience` is %q or %q",
SignInAudienceAzureADandPersonalMicrosoftAccount, SignInAudiencePersonalMicrosoftAccount)
}
// maximum of 100 redirect URIs are supported with personal account sign-ins
if len(pubRedirectUris) > 100 || len(spaRedirectUris) > 100 || len(webRedirectUris) > 100 {
return fmt.Errorf("`redirect_uris` must have no more than 100 URIs when `sign_in_audience` is %q or %q",
SignInAudienceAzureADandPersonalMicrosoftAccount, SignInAudiencePersonalMicrosoftAccount)
}
// redirect URIs containing wildcards not supported with personal account sign-ins
for _, v := range allRedirectUris {
u, err := url.Parse(v.(string))
if err == nil {
if strings.Contains(u.Host, "*") {
return fmt.Errorf("`redirect_uris` having wildcard hosts are not supported when `sign_in_audience` is %q or %q",
SignInAudienceAzureADandPersonalMicrosoftAccount, SignInAudiencePersonalMicrosoftAccount)
}
}
}
// requiredResourceAccess limitations with personal sign-ins:
// 50 resources per application
// 30 permissions per resource
// 200 permissions per application
requiredResourceAccess := diff.Get("required_resource_access").(*pluginsdk.Set).List()
if len(requiredResourceAccess) > 50 {
return fmt.Errorf("maximum of 50 `required_resource_access` blocks are supported when `sign_in_audience` is %q or %q",
SignInAudienceAzureADandPersonalMicrosoftAccount, SignInAudiencePersonalMicrosoftAccount)
}
totalPermissions := 0
for _, raw := range requiredResourceAccess {
v := raw.(map[string]interface{})
if resourceAccess, ok := v["resource_access"]; ok {
permissionCount := len(resourceAccess.([]interface{}))
if permissionCount > 30 {
return fmt.Errorf("maximum of 30 `resource_access` blocks for each `required_resource_access` block are supported when `sign_in_audience` is %q or %q",
SignInAudienceAzureADandPersonalMicrosoftAccount, SignInAudiencePersonalMicrosoftAccount)
}
totalPermissions += permissionCount
if totalPermissions > 200 {
return fmt.Errorf("maximum of 30 `resource_access` blocks per application are supported when `sign_in_audience` is %q or %q",
SignInAudienceAzureADandPersonalMicrosoftAccount, SignInAudiencePersonalMicrosoftAccount)
}
}
}
}
if s := diff.Get("sign_in_audience").(string); s == SignInAudienceAzureADandPersonalMicrosoftAccount || s == SignInAudiencePersonalMicrosoftAccount {
if v, ok := diff.GetOk("api.0.requested_access_token_version"); !ok || v.(int) == 1 {
return fmt.Errorf("`requested_access_token_version` must be 2 when `sign_in_audience` is %q or %q",
SignInAudienceAzureADandPersonalMicrosoftAccount, SignInAudiencePersonalMicrosoftAccount)
}
}
return nil
}
func applicationDiffSuppress(k, old, new string, d *pluginsdk.ResourceData) bool {
suppress := false
switch {
case k == "api.#" && old == "1" && new == "0":
apiRaw := d.Get("api").([]interface{})
if len(apiRaw) == 1 {
suppress = true
api := apiRaw[0].(map[string]interface{})
if v, ok := api["known_client_applications"]; ok && len(v.(*pluginsdk.Set).List()) > 0 {
suppress = false
}
if v, ok := api["mapped_claims_enabled"]; ok && v.(bool) {
suppress = false
}
if v, ok := api["oauth2_permission_scope"]; ok && len(v.(*pluginsdk.Set).List()) > 0 {
suppress = false
}
if v, ok := api["requested_access_token_version"]; ok && v.(int) > 1 {
suppress = false
}
}
case k == "optional_claims.#" && old == "1" && new == "0":
optionalClaimsRaw := d.Get("optional_claims").([]interface{})
if len(optionalClaimsRaw) == 1 {
suppress = true
optionalClaims := optionalClaimsRaw[0].(map[string]interface{})
if v, ok := optionalClaims["access_token"]; ok && len(v.([]interface{})) > 0 {
suppress = false
}
if v, ok := optionalClaims["id_token"]; ok && len(v.([]interface{})) > 0 {
suppress = false
}
if v, ok := optionalClaims["saml2_token"]; ok && len(v.([]interface{})) > 0 {
suppress = false
}
}
case k == "public_client.#" && old == "1" && new == "0":
publicClientRaw := d.Get("public_client").([]interface{})
if len(publicClientRaw) == 1 {
suppress = true
publicClient := publicClientRaw[0].(map[string]interface{})
if v, ok := publicClient["redirect_uris"]; ok && len(v.(*pluginsdk.Set).List()) > 0 {
suppress = false
}
}
case k == "single_page_application.#" && old == "1" && new == "0":
spaRaw := d.Get("single_page_application").([]interface{})
if len(spaRaw) == 1 {
suppress = true
spa := spaRaw[0].(map[string]interface{})
if v, ok := spa["redirect_uris"]; ok && len(v.(*pluginsdk.Set).List()) > 0 {
suppress = false
}
}
case k == "web.#" && old == "1" && new == "0":
webRaw := d.Get("web").([]interface{})
if len(webRaw) == 1 {
suppress = true
web := webRaw[0].(map[string]interface{})
if v, ok := web["redirect_uris"]; ok && len(v.(*pluginsdk.Set).List()) > 0 {
suppress = false
}
if b, ok := web["implicit_grant"]; ok {
if implicitGrantRaw := b.([]interface{}); len(implicitGrantRaw) > 0 {
implicitGrant := implicitGrantRaw[0].(map[string]interface{})
if v, ok := implicitGrant["access_token_issuance_enabled"]; ok && v.(bool) {
suppress = false
}
if v, ok := implicitGrant["id_token_issuance_enabled"]; ok && v.(bool) {
suppress = false
}
}
}
}
case k == "web.0.implicit_grant.#" && old == "1" && new == "0":
implicitGrantRaw := d.Get("web.0.implicit_grant").([]interface{})
if len(implicitGrantRaw) == 1 {
suppress = true
implicitGrant := implicitGrantRaw[0].(map[string]interface{})
if v, ok := implicitGrant["access_token_issuance_enabled"]; ok && v.(bool) {
suppress = false
}
if v, ok := implicitGrant["id_token_issuance_enabled"]; ok && v.(bool) {
suppress = false
}
}
}
return suppress
}
func applicationResourceCreate(ctx context.Context, d *pluginsdk.ResourceData, meta interface{}) pluginsdk.Diagnostics {
client := meta.(*clients.Client).Applications.ApplicationClient
clientBeta := meta.(*clients.Client).Applications.ApplicationClientBeta
appTemplateClient := meta.(*clients.Client).Applications.ApplicationTemplateClient
logoClient := meta.(*clients.Client).Applications.ApplicationLogoClient
ownerClient := meta.(*clients.Client).Applications.ApplicationOwnerClient
servicePrincipalsClient := meta.(*clients.Client).Applications.ServicePrincipalClient
displayName := d.Get("display_name").(string)
// Perform this check at apply time to catch any duplicate names created during the same apply
if d.Get("prevent_duplicate_names").(bool) {
result, err := applicationFindByName(ctx, client, displayName)
if err != nil {
return tf.ErrorDiagPathF(err, "name", "Could not check for existing application(s)")
}
if result != nil && len(*result) > 0 {
existingApp := (*result)[0]
if existingApp.Id == nil {
return tf.ErrorDiagF(errors.New("API returned application with nil object ID during duplicate name check"), "Bad API response")
}
return tf.ImportAsDuplicateDiag("azuread_application", *existingApp.Id, displayName)
}
}
var imageContentType string
var imageData []byte
if v, ok := d.GetOk("logo_image"); ok && v != "" {
var err error
imageContentType, imageData, err = applicationParseLogoImage(v.(string))
if err != nil {
return tf.ErrorDiagPathF(err, "image", "Could not decode image data")
}
}
var tags []string
if v, ok := d.GetOk("feature_tags"); ok {
tags = applications.ExpandFeatures(v.([]interface{}))
} else {
tags = tf.ExpandStringSlice(d.Get("tags").(*pluginsdk.Set).List())
}
if appTemplateId := d.Get("template_id").(string); appTemplateId != "" {
// Validate the template exists
templateId := stable.NewApplicationTemplateID(appTemplateId)
if resp, err := appTemplateClient.GetApplicationTemplate(ctx, templateId, applicationtemplate.DefaultGetApplicationTemplateOperationOptions()); err != nil {
if response.WasNotFound(resp.HttpResponse) {
return tf.ErrorDiagPathF(err, "template_id", "Could not find %s", templateId)
}
return tf.ErrorDiagF(err, "retrieving %s", templateId)
}
// Generate a temporary display name to assert uniqueness when handling buggy 404 when instantiating
uuid, err := uuid.GenerateUUID()
if err != nil {
return tf.ErrorDiagF(err, "Failed to generate a UUID")
}
tempDisplayName := fmt.Sprintf("TERRAFORM_INSTANTIATE_%s", uuid)
// Instantiate application from template gallery and return via the update function