Skip to content

Commit c5e7afa

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 c5e7afa

2 files changed

+56
-33
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

+42-27
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package vault
66
import (
77
"fmt"
88
"strconv"
9+
"strings"
910
"testing"
1011

1112
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
@@ -22,6 +23,13 @@ func TestAccAWSAuthBackendSTSRole_import(t *testing.T) {
2223
accountID := strconv.Itoa(acctest.RandInt())
2324
arn := acctest.RandomWithPrefix("arn:aws:iam::" + accountID + ":role/test-role")
2425
externalID := "external-id"
26+
27+
importStateVerifyIgnore := make([]string, 0)
28+
// Ignore external_id if Vault version is < 1.17.0.
29+
if !provider.IsAPISupported(testProvider.Meta(), 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,13 @@ 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",
135148
}
149+
// Only check external_id if Vault version is >= 1.17.0
150+
if provider.IsAPISupported(testProvider.Meta(), provider.VaultVersion117) {
151+
attrs[consts.FieldExternalID] = consts.FieldExternalID
152+
}
153+
136154
for stateAttr, apiAttr := range attrs {
137155
if resp.Data[apiAttr] == nil && instanceState.Attributes[stateAttr] == "" {
138156
continue
@@ -146,30 +164,27 @@ func testAccAWSAuthBackendSTSRoleCheck_attrs(backend, accountID, stsRole string)
146164
}
147165

148166
func testAccAWSAuthBackendSTSRoleConfig_basic(backend, accountID, stsRole, externalID string) string {
149-
roleResource := fmt.Sprintf(`
150-
resource "vault_aws_auth_backend_sts_role" "role" {
151-
backend = vault_auth_backend.aws.path
152-
account_id = "%s"
153-
sts_role = "%s"
154-
}
155-
`, accountID, stsRole)
167+
backendResource := fmt.Sprintf(`
168+
resource "vault_auth_backend" "aws" {
169+
type = "aws"
170+
path = "%s"
171+
}`, backend)
156172

173+
roleResourceOptionalFields := ""
157174
if externalID != "" {
158-
roleResource = fmt.Sprintf(`
175+
roleResourceOptionalFields += fmt.Sprintf(`
176+
external_id = "%s"`, externalID)
177+
}
178+
179+
roleResource := fmt.Sprintf(`
159180
resource "vault_aws_auth_backend_sts_role" "role" {
160181
backend = vault_auth_backend.aws.path
161182
account_id = "%s"
162-
sts_role = "%s"
163-
external_id = "%s"
183+
sts_role = "%s"%s
164184
}
165-
`, accountID, stsRole, externalID)
166-
}
185+
`, accountID, stsRole, roleResourceOptionalFields)
167186

168-
return fmt.Sprintf(`
169-
resource "vault_auth_backend" "aws" {
170-
type = "aws"
171-
path = "%s"
172-
}
173-
%s
174-
`, backend, roleResource)
187+
resources := []string{backendResource, roleResource}
188+
189+
return strings.Join(resources, "\n")
175190
}

0 commit comments

Comments
 (0)