Skip to content

Commit 95d8d6d

Browse files
zizzencscyrilgdn
andauthored
feat: Add support for assuming an AWS IAM role from the provider. (#486)
This pull request proposes a solution for #263 --------- Co-authored-by: Cyril Gaudin <[email protected]>
1 parent b202448 commit 95d8d6d

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

Diff for: postgresql/provider.go

+38-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package postgresql
33
import (
44
"context"
55
"fmt"
6+
"github.com/aws/aws-sdk-go-v2/credentials"
7+
"github.com/aws/aws-sdk-go-v2/service/sts"
68
"os"
79

810
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
@@ -90,6 +92,13 @@ func Provider() *schema.Provider {
9092
Description: "AWS region to use for IAM auth",
9193
},
9294

95+
"aws_rds_iam_provider_role_arn": {
96+
Type: schema.TypeString,
97+
Optional: true,
98+
Default: "",
99+
Description: "AWS IAM role to assume for IAM auth",
100+
},
101+
93102
"azure_identity_auth": {
94103
Type: schema.TypeBool,
95104
Optional: true,
@@ -227,7 +236,7 @@ func validateExpectedVersion(v interface{}, key string) (warnings []string, erro
227236
return
228237
}
229238

230-
func getRDSAuthToken(region string, profile string, username string, host string, port int) (string, error) {
239+
func getRDSAuthToken(region string, profile string, role string, username string, host string, port int) (string, error) {
231240
endpoint := fmt.Sprintf("%s:%d", host, port)
232241

233242
ctx := context.Background()
@@ -246,6 +255,32 @@ func getRDSAuthToken(region string, profile string, username string, host string
246255
return "", err
247256
}
248257

258+
if role != "" {
259+
stsClient := sts.NewFromConfig(awscfg)
260+
roleInput := &sts.AssumeRoleInput{
261+
RoleArn: aws.String(role),
262+
RoleSessionName: aws.String("TerraformPostgresqlProvider"),
263+
}
264+
265+
roleOutput, err := stsClient.AssumeRole(ctx, roleInput)
266+
if err != nil {
267+
return "", fmt.Errorf("could not assume AWS role: %w", err)
268+
}
269+
270+
awscfg, err = awsConfig.LoadDefaultConfig(ctx,
271+
awsConfig.WithCredentialsProvider(
272+
aws.NewCredentialsCache(credentials.NewStaticCredentialsProvider(
273+
*roleOutput.Credentials.AccessKeyId,
274+
*roleOutput.Credentials.SecretAccessKey,
275+
*roleOutput.Credentials.SessionToken,
276+
)),
277+
),
278+
)
279+
if err != nil {
280+
return "", fmt.Errorf("could not load AWS default config: %w", err)
281+
}
282+
}
283+
249284
token, err := auth.BuildAuthToken(ctx, endpoint, awscfg.Region, username, awscfg.Credentials)
250285

251286
return token, err
@@ -312,8 +347,9 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
312347
if d.Get("aws_rds_iam_auth").(bool) {
313348
profile := d.Get("aws_rds_iam_profile").(string)
314349
region := d.Get("aws_rds_iam_region").(string)
350+
role := d.Get("aws_rds_iam_provider_role_arn").(string)
315351
var err error
316-
password, err = getRDSAuthToken(region, profile, username, host, port)
352+
password, err = getRDSAuthToken(region, profile, role, username, host, port)
317353
if err != nil {
318354
return nil, err
319355
}

Diff for: website/docs/index.html.markdown

+1
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ The following arguments are supported:
183183
from the environment (or the given profile, see `aws_rds_iam_profile`)
184184
* `aws_rds_iam_profile` - (Optional) The AWS IAM Profile to use while using AWS RDS IAM Auth.
185185
* `aws_rds_iam_region` - (Optional) The AWS region to use while using AWS RDS IAM Auth.
186+
* `aws_rds_iam_provider_role_arn` - (Optional) AWS IAM role to assume while using AWS RDS IAM Auth.
186187
* `azure_identity_auth` - (Optional) If set to `true`, call the Azure OAuth token endpoint for temporary token
187188
* `azure_tenant_id` - (Optional) (Required if `azure_identity_auth` is `true`) Azure tenant ID [read more](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/client_config.html)
188189

0 commit comments

Comments
 (0)