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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add endpoints configuration options to AWS Secrets Backend Resource #1043

Merged
merged 1 commit into from
May 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 46 additions & 1 deletion vault/resource_aws_secret_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ func awsSecretBackendResource() *schema.Resource {
Computed: true,
Description: "The AWS region to make API calls against. Defaults to us-east-1.",
},
"iam_endpoint": {
Type: schema.TypeString,
Optional: true,
Description: "Specifies a custom HTTP IAM endpoint to use.",
},
"sts_endpoint": {
Type: schema.TypeString,
Optional: true,
Description: "Specifies a custom HTTP STS endpoint to use.",
},
},
}
}
Expand All @@ -89,6 +99,8 @@ func awsSecretBackendCreate(d *schema.ResourceData, meta interface{}) error {
accessKey := d.Get("access_key").(string)
secretKey := d.Get("secret_key").(string)
region := d.Get("region").(string)
iamEndpoint := d.Get("iam_endpoint").(string)
stsEndpoint := d.Get("sts_endpoint").(string)

d.Partial(true)
log.Printf("[DEBUG] Mounting AWS backend at %q", path)
Expand Down Expand Up @@ -119,6 +131,12 @@ func awsSecretBackendCreate(d *schema.ResourceData, meta interface{}) error {
if region != "" {
data["region"] = region
}
if iamEndpoint != "" {
data["iam_endpoint"] = iamEndpoint
}
if stsEndpoint != "" {
data["sts_endpoint"] = stsEndpoint
}
_, err = client.Logical().Write(path+"/config/root", data)
if err != nil {
return fmt.Errorf("error configuring root credentials for %q: %s", path, err)
Expand All @@ -130,6 +148,12 @@ func awsSecretBackendCreate(d *schema.ResourceData, meta interface{}) error {
d.Set("region", "us-east-1")
}
d.SetPartial("region")
if iamEndpoint != "" {
d.SetPartial("iam_endpoint")
}
if stsEndpoint != "" {
d.SetPartial("sts_endpoint")
}
d.Partial(false)

return awsSecretBackendRead(d, meta)
Expand Down Expand Up @@ -185,6 +209,13 @@ func awsSecretBackendRead(d *schema.ResourceData, meta interface{}) error {
} else {
d.Set("region", "us-east-1")
}

if v, ok := resp.Data["iam_endpoint"].(string); ok {
d.Set("iam_endpoint", v)
}
if v, ok := resp.Data["sts_endpoint"].(string); ok {
d.Set("sts_endpoint", v)
}
}

d.Set("path", path)
Expand Down Expand Up @@ -214,16 +245,24 @@ func awsSecretBackendUpdate(d *schema.ResourceData, meta interface{}) error {
d.SetPartial("default_lease_ttl_seconds")
d.SetPartial("max_lease_ttl_seconds")
}
if d.HasChange("access_key") || d.HasChange("secret_key") || d.HasChange("region") {
if d.HasChange("access_key") || d.HasChange("secret_key") || d.HasChange("region") || d.HasChange("iam_endpoint") || d.HasChange("sts_endpoint") {
log.Printf("[DEBUG] Updating root credentials at %q", path+"/config/root")
data := map[string]interface{}{
"access_key": d.Get("access_key").(string),
"secret_key": d.Get("secret_key").(string),
}
region := d.Get("region").(string)
iamEndpoint := d.Get("iam_endpoint").(string)
stsEndpoint := d.Get("sts_endpoint").(string)
if region != "" {
data["region"] = region
}
if iamEndpoint != "" {
data["iam_endpoint"] = iamEndpoint
}
if stsEndpoint != "" {
data["sts_endpoint"] = stsEndpoint
}
_, err := client.Logical().Write(path+"/config/root", data)
if err != nil {
return fmt.Errorf("error configuring root credentials for %q: %s", path, err)
Expand All @@ -235,6 +274,12 @@ func awsSecretBackendUpdate(d *schema.ResourceData, meta interface{}) error {
d.Set("region", "us-east-1")
}
d.SetPartial("region")
if iamEndpoint != "" {
d.SetPartial("iam_endpoint")
}
if stsEndpoint != "" {
d.SetPartial("sts_endpoint")
}
}
d.Partial(false)
return awsSecretBackendRead(d, meta)
Expand Down
9 changes: 9 additions & 0 deletions vault/resource_aws_secret_backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ func TestAccAWSSecretBackend_basic(t *testing.T) {
resource.TestCheckResourceAttr("vault_aws_secret_backend.test", "access_key", accessKey),
resource.TestCheckResourceAttr("vault_aws_secret_backend.test", "secret_key", secretKey),
resource.TestCheckResourceAttr("vault_aws_secret_backend.test", "region", "us-east-1"),
resource.TestCheckResourceAttr("vault_aws_secret_backend.test", "iam_endpoint", ""),
resource.TestCheckResourceAttr("vault_aws_secret_backend.test", "sts_endpoint", ""),
),
},
{
Expand All @@ -41,6 +43,8 @@ func TestAccAWSSecretBackend_basic(t *testing.T) {
resource.TestCheckResourceAttr("vault_aws_secret_backend.test", "access_key", accessKey),
resource.TestCheckResourceAttr("vault_aws_secret_backend.test", "secret_key", secretKey),
resource.TestCheckResourceAttr("vault_aws_secret_backend.test", "region", "us-west-1"),
resource.TestCheckResourceAttr("vault_aws_secret_backend.test", "iam_endpoint", "https://iam.amazonaws.com"),
resource.TestCheckResourceAttr("vault_aws_secret_backend.test", "sts_endpoint", "https://sts.us-west-1.amazonaws.com"),
),
},
{
Expand All @@ -53,6 +57,8 @@ func TestAccAWSSecretBackend_basic(t *testing.T) {
resource.TestCheckResourceAttr("vault_aws_secret_backend.test", "access_key", ""),
resource.TestCheckResourceAttr("vault_aws_secret_backend.test", "secret_key", ""),
resource.TestCheckResourceAttr("vault_aws_secret_backend.test", "region", "us-west-1"),
resource.TestCheckResourceAttr("vault_aws_secret_backend.test", "iam_endpoint", ""),
resource.TestCheckResourceAttr("vault_aws_secret_backend.test", "sts_endpoint", ""),
),
},
},
Expand Down Expand Up @@ -135,6 +141,9 @@ resource "vault_aws_secret_backend" "test" {
access_key = "%s"
secret_key = "%s"
region = "us-west-1"

iam_endpoint = "https://iam.amazonaws.com"
sts_endpoint = "https://sts.us-west-1.amazonaws.com"
}`, path, accessKey, secretKey)
}

Expand Down
4 changes: 4 additions & 0 deletions website/docs/r/aws_secret_backend.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ issued by this backend.
* `max_lease_ttl_seconds` - (Optional) The maximum TTL that can be requested
for credentials issued by this backend.

* `iam_endpoint` - (Optional) Specifies a custom HTTP IAM endpoint to use.

* `sts_endpoint` - (Optional) Specifies a custom HTTP STS endpoint to use.

## Attributes Reference

No additional attributes are exported by this resource.
Expand Down