Skip to content

Commit 25b3a7f

Browse files
Add sts_region for AWS auth backend (#931)
* Add sts_region for AWS auth backend * Add sts_region doc * Add sts_region to read * Update vault/resource_aws_auth_backend_client.go Co-authored-by: Theron Voran <tvoran@users.noreply.github.com> * Fix test * Remove sts_region check Co-authored-by: Theron Voran <tvoran@users.noreply.github.com>
1 parent 904551f commit 25b3a7f

3 files changed

Lines changed: 56 additions & 0 deletions

File tree

vault/resource_aws_auth_backend_client.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ func awsAuthBackendClientResource() *schema.Resource {
6060
Optional: true,
6161
Description: "URL to override the default generated endpoint for making AWS STS API calls.",
6262
},
63+
"sts_region": {
64+
Type: schema.TypeString,
65+
Optional: true,
66+
Description: "Region to override the default region for making AWS STS API calls.",
67+
},
6368
"iam_server_id_header_value": {
6469
Type: schema.TypeString,
6570
Optional: true,
@@ -78,6 +83,8 @@ func awsAuthBackendWrite(d *schema.ResourceData, meta interface{}) error {
7883
ec2Endpoint := d.Get("ec2_endpoint").(string)
7984
iamEndpoint := d.Get("iam_endpoint").(string)
8085
stsEndpoint := d.Get("sts_endpoint").(string)
86+
stsRegion := d.Get("sts_region").(string)
87+
8188
iamServerIDHeaderValue := d.Get("iam_server_id_header_value").(string)
8289

8390
path := awsAuthBackendClientPath(backend)
@@ -86,6 +93,7 @@ func awsAuthBackendWrite(d *schema.ResourceData, meta interface{}) error {
8693
"endpoint": ec2Endpoint,
8794
"iam_endpoint": iamEndpoint,
8895
"sts_endpoint": stsEndpoint,
96+
"sts_region": stsRegion,
8997
"iam_server_id_header_value": iamServerIDHeaderValue,
9098
}
9199

@@ -95,6 +103,11 @@ func awsAuthBackendWrite(d *schema.ResourceData, meta interface{}) error {
95103
data["secret_key"] = d.Get("secret_key").(string)
96104
}
97105

106+
// sts_endpoint and sts_region are required to be set together
107+
if (stsEndpoint == "") != (stsRegion == "") {
108+
return fmt.Errorf("both sts_endpoint and sts_region need to be set")
109+
}
110+
98111
log.Printf("[DEBUG] Writing AWS auth backend client config to %q", path)
99112
_, err := client.Logical().Write(path, data)
100113
if err != nil {
@@ -134,6 +147,7 @@ func awsAuthBackendRead(d *schema.ResourceData, meta interface{}) error {
134147
d.Set("ec2_endpoint", secret.Data["endpoint"])
135148
d.Set("iam_endpoint", secret.Data["iam_endpoint"])
136149
d.Set("sts_endpoint", secret.Data["sts_endpoint"])
150+
d.Set("sts_region", secret.Data["sts_region"])
137151
d.Set("iam_server_id_header_value", secret.Data["iam_server_id_header_value"])
138152
return nil
139153
}

vault/resource_aws_auth_backend_client_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package vault
22

33
import (
44
"fmt"
5+
"regexp"
56
"testing"
67

78
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
@@ -96,6 +97,21 @@ func TestAccAWSAuthBackendClient_withoutSecretKey(t *testing.T) {
9697
})
9798
}
9899

100+
func TestAccAWSAuthBackendClientStsRegionNoEndpoint(t *testing.T) {
101+
backend := acctest.RandomWithPrefix("aws")
102+
resource.Test(t, resource.TestCase{
103+
PreCheck: func() { testAccPreCheck(t) },
104+
Providers: testProviders,
105+
CheckDestroy: testAccCheckAWSAuthBackendClientDestroy,
106+
Steps: []resource.TestStep{
107+
{
108+
Config: testAccAWSAuthBackendClientConfigSTSRegionNoEndpoint(backend),
109+
ExpectError: regexp.MustCompile("both sts_endpoint and sts_region need to be set"),
110+
},
111+
},
112+
})
113+
}
114+
99115
func testAccCheckAWSAuthBackendClientDestroy(s *terraform.State) error {
100116
client := testProvider.Meta().(*api.Client)
101117

@@ -146,6 +162,7 @@ func testAccAWSAuthBackendClientCheck_attrs(backend string) resource.TestCheckFu
146162
"ec2_endpoint": "endpoint",
147163
"iam_endpoint": "iam_endpoint",
148164
"sts_endpoint": "sts_endpoint",
165+
"sts_region": "sts_region",
149166
"iam_server_id_header_value": "iam_server_id_header_value",
150167
}
151168
for stateAttr, apiAttr := range attrs {
@@ -175,6 +192,7 @@ resource "vault_aws_auth_backend_client" "client" {
175192
ec2_endpoint = "http://vault.test/ec2"
176193
iam_endpoint = "http://vault.test/iam"
177194
sts_endpoint = "http://vault.test/sts"
195+
sts_region = "vault-test"
178196
iam_server_id_header_value = "vault.test"
179197
}
180198
`, backend)
@@ -195,6 +213,7 @@ resource "vault_aws_auth_backend_client" "client" {
195213
ec2_endpoint = "http://updated.vault.test/ec2"
196214
iam_endpoint = "http://updated.vault.test/iam"
197215
sts_endpoint = "http://updated.vault.test/sts"
216+
sts_region = "updated-vault-test"
198217
iam_server_id_header_value = "updated.vault.test"
199218
}`, backend)
200219
}
@@ -213,6 +232,7 @@ resource "vault_aws_auth_backend_client" "client" {
213232
ec2_endpoint = "http://vault.test/ec2"
214233
iam_endpoint = "http://vault.test/iam"
215234
sts_endpoint = "http://vault.test/sts"
235+
sts_region = "vault-test"
216236
iam_server_id_header_value = "vault.test"
217237
}`, backend)
218238
}
@@ -231,6 +251,25 @@ resource "vault_aws_auth_backend_client" "client" {
231251
ec2_endpoint = "http://updated2.vault.test/ec2"
232252
iam_endpoint = "http://updated2.vault.test/iam"
233253
sts_endpoint = "http://updated2.vault.test/sts"
254+
sts_region = "updated-vault-test"
234255
iam_server_id_header_value = "updated2.vault.test"
235256
}`, backend)
236257
}
258+
259+
func testAccAWSAuthBackendClientConfigSTSRegionNoEndpoint(backend string) string {
260+
return fmt.Sprintf(`
261+
resource "vault_auth_backend" "aws" {
262+
path = "%s"
263+
type = "aws"
264+
description = "Test auth backend for AWS backend client config"
265+
}
266+
267+
resource "vault_aws_auth_backend_client" "client" {
268+
backend = "${vault_auth_backend.aws.path}"
269+
access_key = "AWSACCESSKEY"
270+
ec2_endpoint = "http://vault.test/ec2"
271+
iam_endpoint = "http://vault.test/iam"
272+
sts_region = "vault-test"
273+
iam_server_id_header_value = "vault.test"
274+
}`, backend)
275+
}

website/docs/r/aws_auth_backend_client.html.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ The following arguments are supported:
6060
* `sts_endpoint` - (Optional) Override the URL Vault uses when making STS API
6161
calls.
6262

63+
* `sts_region` - (Optional) Override the default region when making STS API
64+
calls. The `sts_endpoint` argument must be set when using `sts_region`.
65+
6366
* `iam_server_id_header_value` - (Optional) The value to require in the
6467
`X-Vault-AWS-IAM-Server-ID` header as part of `GetCallerIdentity` requests
6568
that are used in the IAM auth method.

0 commit comments

Comments
 (0)