Skip to content

Commit 63b0911

Browse files
committed
Add claims_matching_expression
1 parent a0c2e60 commit 63b0911

File tree

3 files changed

+65
-5
lines changed

3 files changed

+65
-5
lines changed

docs/resources/application_federated_identity_credential.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,13 @@ The following arguments are supported:
3939

4040
* `application_id` - (Required) The resource ID of the application for which this federated identity credential should be created. Changing this field forces a new resource to be created.
4141
* `audiences` - (Required) List of audiences that can appear in the external token. This specifies what should be accepted in the `aud` claim of incoming tokens.
42+
* `claims_matching_expression` - (Optional) The expression that subjects will be matched against.
4243
* `description` - (Optional) A description for the federated identity credential.
4344
* `display_name` - (Required) A unique display name for the federated identity credential. Changing this forces a new resource to be created.
4445
* `issuer` - (Required) The URL of the external identity provider, which must match the issuer claim of the external token being exchanged. The combination of the values of issuer and subject must be unique on the app.
45-
* `subject` - (Required) The identifier of the external software workload within the external identity provider. The combination of issuer and subject must be unique on the app.
46+
* `subject` - (Optional) The identifier of the external software workload within the external identity provider. The combination of issuer and subject must be unique on the app.
47+
48+
-> At least one of `subject` or `claims_matching_expression` must be specified.
4649

4750
## Attributes Reference
4851

internal/services/applications/application_federated_identity_credential_resource.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,17 @@ func applicationFederatedIdentityCredentialResource() *pluginsdk.Resource {
7878
},
7979

8080
"subject": {
81-
Description: "The identifier of the external software workload within the external identity provider. The combination of issuer and subject must be unique on the app.",
82-
Type: pluginsdk.TypeString,
83-
Required: true,
81+
Description: "The identifier of the external software workload within the external identity provider. The combination of issuer and subject must be unique on the app.",
82+
Type: pluginsdk.TypeString,
83+
Optional: true,
84+
ExactlyOneOf: []string{"subject", "claims_matching_expression"},
85+
},
86+
87+
"claims_matching_expression": {
88+
Description: "The expression that subjects will be matched against.",
89+
Type: pluginsdk.TypeString,
90+
Optional: true,
91+
ExactlyOneOf: []string{"subject", "claims_matching_expression"},
8492
},
8593

8694
"description": {
@@ -128,6 +136,13 @@ func applicationFederatedIdentityCredentialResourceCreate(ctx context.Context, d
128136
Subject: nullable.Value(d.Get("subject").(string)),
129137
}
130138

139+
if v, ok := d.GetOk("claims_matching_expression"); ok {
140+
credential.ClaimsMatchingExpression = pointer.To(beta.FederatedIdentityExpression{
141+
LanguageVersion: 1,
142+
Value: v.(string),
143+
})
144+
}
145+
131146
federatedIdentityCredentialResp, err := federatedIdentityCredentialClient.CreateFederatedIdentityCredential(ctx, *applicationId, credential, federatedidentitycredential.DefaultCreateFederatedIdentityCredentialOperationOptions())
132147
if err != nil {
133148
return tf.ErrorDiagF(err, "Adding federated identity credential for %s", applicationId)
@@ -203,6 +218,13 @@ func applicationFederatedIdentityCredentialResourceUpdate(ctx context.Context, d
203218
Name: d.Get("display_name").(string),
204219
}
205220

221+
if v, ok := d.GetOk("claims_matching_expression"); ok {
222+
credential.ClaimsMatchingExpression = pointer.To(beta.FederatedIdentityExpression{
223+
LanguageVersion: 1,
224+
Value: v.(string),
225+
})
226+
}
227+
206228
credentialId := beta.NewApplicationIdFederatedIdentityCredentialID(id.ObjectId, id.KeyId)
207229

208230
if _, err = federatedIdentityCredentialClient.UpdateFederatedIdentityCredential(ctx, credentialId, credential, federatedidentitycredential.DefaultUpdateFederatedIdentityCredentialOperationOptions()); err != nil {
@@ -245,7 +267,11 @@ func applicationFederatedIdentityCredentialResourceRead(ctx context.Context, d *
245267
tf.Set(d, "description", credential.Description.GetOrZero())
246268
tf.Set(d, "display_name", credential.Name)
247269
tf.Set(d, "issuer", credential.Issuer)
248-
tf.Set(d, "subject", credential.Subject)
270+
tf.Set(d, "subject", credential.Subject.GetOrZero())
271+
272+
if credential.ClaimsMatchingExpression != nil {
273+
tf.Set(d, "claims_matching_expression", credential.ClaimsMatchingExpression.Value)
274+
}
249275

250276
return nil
251277
}

internal/services/applications/application_federated_identity_credential_resource_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,22 @@ func TestAccApplicationFederatedIdentityCredential_complete(t *testing.T) {
5353
})
5454
}
5555

56+
func TestAccApplicationFederatedIdentityCredential_flexible(t *testing.T) {
57+
data := acceptance.BuildTestData(t, "azuread_application_federated_identity_credential", "test")
58+
r := ApplicationFederatedIdentityCredentialResource{}
59+
60+
data.ResourceTest(t, r, []acceptance.TestStep{
61+
{
62+
Config: r.flexible(data),
63+
Check: acceptance.ComposeTestCheckFunc(
64+
check.That(data.ResourceName).ExistsInAzure(r),
65+
check.That(data.ResourceName).Key("credential_id").Exists(),
66+
),
67+
},
68+
data.ImportStep(),
69+
})
70+
}
71+
5672
func TestAccApplicationFederatedIdentityCredential_update(t *testing.T) {
5773
data := acceptance.BuildTestData(t, "azuread_application_federated_identity_credential", "test")
5874
r := ApplicationFederatedIdentityCredentialResource{}
@@ -142,3 +158,18 @@ resource "azuread_application_federated_identity_credential" "test" {
142158
}
143159
`, r.template(data), data.RandomString, data.UUID())
144160
}
161+
162+
func (r ApplicationFederatedIdentityCredentialResource) flexible(data acceptance.TestData) string {
163+
return fmt.Sprintf(`
164+
%[1]s
165+
166+
resource "azuread_application_federated_identity_credential" "test" {
167+
application_id = azuread_application.test.id
168+
display_name = "hashitown.example.com-%[2]s"
169+
description = "Funtime tokens for HashiTown"
170+
audiences = ["api://AzureADTokenExchange"]
171+
issuer = "https://token.actions.githubusercontent.com"
172+
claims_matching_expression = "claims['sub'] matches 'repo:contoso/contoso-repo:ref:refs/heads/*'"
173+
}
174+
`, r.template(data), data.RandomString)
175+
}

0 commit comments

Comments
 (0)