Skip to content

Commit c2d1520

Browse files
committed
only support external_id on vault versions >= 1.17
external_id support for aws auth sts configuration added in 1.17.0: hashicorp/vault#26628
1 parent 651c058 commit c2d1520

2 files changed

+41
-14
lines changed

vault/resource_aws_auth_backend_sts_role.go

+14-6
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,11 @@ func awsAuthBackendSTSRoleCreate(d *schema.ResourceData, meta interface{}) error
7676
path := awsAuthBackendSTSRolePath(backend, accountID)
7777

7878
data := map[string]interface{}{
79-
"sts_role": stsRole,
80-
consts.FieldExternalID: externalID,
79+
"sts_role": stsRole,
80+
}
81+
82+
if provider.IsAPISupported(meta, provider.VaultVersion117) {
83+
data[consts.FieldExternalID] = externalID
8184
}
8285

8386
log.Printf("[DEBUG] Writing STS role %q to AWS auth backend", path)
@@ -128,8 +131,10 @@ func awsAuthBackendSTSRoleRead(d *schema.ResourceData, meta interface{}) error {
128131
d.Set("account_id", accountID)
129132
d.Set("sts_role", resp.Data["sts_role"])
130133

131-
if v, ok := resp.Data[consts.FieldExternalID]; ok {
132-
d.Set(consts.FieldExternalID, v)
134+
if provider.IsAPISupported(meta, provider.VaultVersion117) {
135+
if v, ok := resp.Data[consts.FieldExternalID]; ok {
136+
d.Set(consts.FieldExternalID, v)
137+
}
133138
}
134139

135140
return nil
@@ -147,8 +152,11 @@ func awsAuthBackendSTSRoleUpdate(d *schema.ResourceData, meta interface{}) error
147152
path := d.Id()
148153

149154
data := map[string]interface{}{
150-
"sts_role": stsRole,
151-
consts.FieldExternalID: externalID,
155+
"sts_role": stsRole,
156+
}
157+
158+
if provider.IsAPISupported(meta, provider.VaultVersion117) {
159+
data[consts.FieldExternalID] = externalID
152160
}
153161

154162
log.Printf("[DEBUG] Updating STS role %q in AWS auth backend", path)

vault/resource_aws_auth_backend_sts_role_test.go

+27-8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ func TestAccAWSAuthBackendSTSRole_import(t *testing.T) {
2222
accountID := strconv.Itoa(acctest.RandInt())
2323
arn := acctest.RandomWithPrefix("arn:aws:iam::" + accountID + ":role/test-role")
2424
externalID := "external-id"
25+
26+
importStateVerifyIgnore := make([]string, 0)
27+
meta := testProvider.Meta().(*provider.ProviderMeta)
28+
// Ignore external_id if Vault version is < 1.17.0.
29+
if !meta.IsAPISupported(provider.VaultVersion117) {
30+
importStateVerifyIgnore = append(importStateVerifyIgnore, consts.FieldExternalID)
31+
}
32+
2533
resource.Test(t, resource.TestCase{
2634
PreCheck: func() { testutil.TestAccPreCheck(t) },
2735
ProviderFactories: providerFactories,
@@ -32,9 +40,10 @@ func TestAccAWSAuthBackendSTSRole_import(t *testing.T) {
3240
Check: testAccAWSAuthBackendSTSRoleCheck_attrs(backend, accountID, arn),
3341
},
3442
{
35-
ResourceName: "vault_aws_auth_backend_sts_role.role",
36-
ImportState: true,
37-
ImportStateVerify: true,
43+
ResourceName: "vault_aws_auth_backend_sts_role.role",
44+
ImportState: true,
45+
ImportStateVerify: true,
46+
ImportStateVerifyIgnore: importStateVerifyIgnore,
3847
},
3948
},
4049
})
@@ -56,13 +65,18 @@ func TestAccAWSAuthBackendSTSRole_basic(t *testing.T) {
5665
Config: testAccAWSAuthBackendSTSRoleConfig_basic(backend, accountID, arn, ""),
5766
Check: testAccAWSAuthBackendSTSRoleCheck_attrs(backend, accountID, arn),
5867
},
68+
{
69+
// Update ARN.
70+
Config: testAccAWSAuthBackendSTSRoleConfig_basic(backend, accountID, updatedArn, ""),
71+
Check: testAccAWSAuthBackendSTSRoleCheck_attrs(backend, accountID, updatedArn),
72+
},
5973
{
6074
// Add external ID.
61-
Config: testAccAWSAuthBackendSTSRoleConfig_basic(backend, accountID, arn, externalID),
62-
Check: testAccAWSAuthBackendSTSRoleCheck_attrs(backend, accountID, arn),
75+
Config: testAccAWSAuthBackendSTSRoleConfig_basic(backend, accountID, updatedArn, externalID),
76+
Check: testAccAWSAuthBackendSTSRoleCheck_attrs(backend, accountID, updatedArn),
6377
},
6478
{
65-
// Update ARN and external ID.
79+
// Update external ID.
6680
Config: testAccAWSAuthBackendSTSRoleConfig_basic(backend, accountID, updatedArn, updatedExternalID),
6781
Check: testAccAWSAuthBackendSTSRoleCheck_attrs(backend, accountID, updatedArn),
6882
},
@@ -130,9 +144,14 @@ func testAccAWSAuthBackendSTSRoleCheck_attrs(backend, accountID, stsRole string)
130144
}
131145

132146
attrs := map[string]string{
133-
"sts_role": "sts_role",
134-
consts.FieldExternalID: consts.FieldExternalID,
147+
"sts_role": "sts_role",
148+
}
149+
meta := testProvider.Meta().(*provider.ProviderMeta)
150+
// Only check external_id if Vault version is >= 1.17.0
151+
if meta.IsAPISupported(provider.VaultVersion117) {
152+
attrs[consts.FieldExternalID] = consts.FieldExternalID
135153
}
154+
136155
for stateAttr, apiAttr := range attrs {
137156
if resp.Data[apiAttr] == nil && instanceState.Attributes[stateAttr] == "" {
138157
continue

0 commit comments

Comments
 (0)