Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sts_region for AWS auth backend #931

Merged
merged 6 commits into from
Dec 14, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions vault/resource_aws_auth_backend_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ func awsAuthBackendClientResource() *schema.Resource {
Optional: true,
Description: "URL to override the default generated endpoint for making AWS STS API calls.",
},
"sts_region": {
Type: schema.TypeString,
Optional: true,
Description: "Region to override the default region for making AWS STS API calls.",
},
"iam_server_id_header_value": {
Type: schema.TypeString,
Optional: true,
Expand All @@ -78,6 +83,8 @@ func awsAuthBackendWrite(d *schema.ResourceData, meta interface{}) error {
ec2Endpoint := d.Get("ec2_endpoint").(string)
iamEndpoint := d.Get("iam_endpoint").(string)
stsEndpoint := d.Get("sts_endpoint").(string)
stsRegion := d.Get("sts_region").(string)

iamServerIDHeaderValue := d.Get("iam_server_id_header_value").(string)

path := awsAuthBackendClientPath(backend)
Expand All @@ -95,6 +102,15 @@ func awsAuthBackendWrite(d *schema.ResourceData, meta interface{}) error {
data["secret_key"] = d.Get("secret_key").(string)
}

if stsRegion != "" {
data["sts_region"] = stsRegion
}
tvoran marked this conversation as resolved.
Show resolved Hide resolved

// sts_endpoint is required when sts_region is set
if stsEndpoint == "" && stsRegion != "" {
return fmt.Errorf("sts_endpoint must be set if sts_region is configured")
}
jasonodonnell marked this conversation as resolved.
Show resolved Hide resolved

log.Printf("[DEBUG] Writing AWS auth backend client config to %q", path)
_, err := client.Logical().Write(path, data)
if err != nil {
Expand Down Expand Up @@ -134,6 +150,7 @@ func awsAuthBackendRead(d *schema.ResourceData, meta interface{}) error {
d.Set("ec2_endpoint", secret.Data["endpoint"])
d.Set("iam_endpoint", secret.Data["iam_endpoint"])
d.Set("sts_endpoint", secret.Data["sts_endpoint"])
d.Set("sts_region", secret.Data["sts_region"])
d.Set("iam_server_id_header_value", secret.Data["iam_server_id_header_value"])
return nil
}
Expand Down
39 changes: 39 additions & 0 deletions vault/resource_aws_auth_backend_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package vault

import (
"fmt"
"regexp"
"testing"

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

func TestAccAWSAuthBackendClientStsRegionNoEndpoint(t *testing.T) {
backend := acctest.RandomWithPrefix("aws")
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testProviders,
CheckDestroy: testAccCheckAWSAuthBackendClientDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSAuthBackendClientConfigSTSRegionNoEndpoint(backend),
ExpectError: regexp.MustCompile("sts_endpoint must be set if sts_region is configured"),
},
},
})
}

func testAccCheckAWSAuthBackendClientDestroy(s *terraform.State) error {
client := testProvider.Meta().(*api.Client)

Expand Down Expand Up @@ -146,6 +162,7 @@ func testAccAWSAuthBackendClientCheck_attrs(backend string) resource.TestCheckFu
"ec2_endpoint": "endpoint",
"iam_endpoint": "iam_endpoint",
"sts_endpoint": "sts_endpoint",
"sts_region": "sts_region",
"iam_server_id_header_value": "iam_server_id_header_value",
}
for stateAttr, apiAttr := range attrs {
Expand Down Expand Up @@ -175,6 +192,7 @@ resource "vault_aws_auth_backend_client" "client" {
ec2_endpoint = "http://vault.test/ec2"
iam_endpoint = "http://vault.test/iam"
sts_endpoint = "http://vault.test/sts"
sts_region = "vault-test"
iam_server_id_header_value = "vault.test"
}
`, backend)
Expand All @@ -195,6 +213,7 @@ resource "vault_aws_auth_backend_client" "client" {
ec2_endpoint = "http://updated.vault.test/ec2"
iam_endpoint = "http://updated.vault.test/iam"
sts_endpoint = "http://updated.vault.test/sts"
sts_region = "updated-vault-test"
iam_server_id_header_value = "updated.vault.test"
}`, backend)
}
Expand All @@ -213,6 +232,7 @@ resource "vault_aws_auth_backend_client" "client" {
ec2_endpoint = "http://vault.test/ec2"
iam_endpoint = "http://vault.test/iam"
sts_endpoint = "http://vault.test/sts"
sts_region = "vault-test"
iam_server_id_header_value = "vault.test"
}`, backend)
}
Expand All @@ -231,6 +251,25 @@ resource "vault_aws_auth_backend_client" "client" {
ec2_endpoint = "http://updated2.vault.test/ec2"
iam_endpoint = "http://updated2.vault.test/iam"
sts_endpoint = "http://updated2.vault.test/sts"
sts_region = "updated-vault-test"
iam_server_id_header_value = "updated2.vault.test"
}`, backend)
}

func testAccAWSAuthBackendClientConfigSTSRegionNoEndpoint(backend string) string {
return fmt.Sprintf(`
resource "vault_auth_backend" "aws" {
path = "%s"
type = "aws"
description = "Test auth backend for AWS backend client config"
}

resource "vault_aws_auth_backend_client" "client" {
backend = "${vault_auth_backend.aws.path}"
access_key = "AWSACCESSKEY"
ec2_endpoint = "http://vault.test/ec2"
iam_endpoint = "http://vault.test/iam"
sts_region = "vault-test"
iam_server_id_header_value = "vault.test"
}`, backend)
}
3 changes: 3 additions & 0 deletions website/docs/r/aws_auth_backend_client.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ The following arguments are supported:
* `sts_endpoint` - (Optional) Override the URL Vault uses when making STS API
calls.

* `sts_region` - (Optional) Override the default region when making STS API
calls. The `sts_endpoint` argument must be set when using `sts_region`.

* `iam_server_id_header_value` - (Optional) The value to require in the
`X-Vault-AWS-IAM-Server-ID` header as part of `GetCallerIdentity` requests
that are used in the IAM auth method.
Expand Down