Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
40 changes: 38 additions & 2 deletions postgresql/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/sts"
"os"

"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
Expand Down Expand Up @@ -90,6 +92,13 @@
Description: "AWS region to use for IAM auth",
},

"aws_rds_iam_provider_role_arn": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "AWS IAM role to assume for IAM auth",
},

"azure_identity_auth": {
Type: schema.TypeBool,
Optional: true,
Expand Down Expand Up @@ -227,7 +236,7 @@
return
}

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

ctx := context.Background()
Expand All @@ -246,6 +255,32 @@
return "", err
}

if role != "" {
stsClient := sts.NewFromConfig(awscfg)
roleInput := &sts.AssumeRoleInput{
RoleArn: aws.String(role),
RoleSessionName: aws.String("TerraformPostgresqlProvider"),
}

roleOutput, err := stsClient.AssumeRole(ctx, roleInput)
if err != nil {
return "", fmt.Errorf("could not assume AWS role: %w", err)
}

awscfg, err = awsConfig.LoadDefaultConfig(ctx,
awsConfig.WithCredentialsProvider(
aws.NewCredentialsCache(credentials.NewStaticCredentialsProvider(
*roleOutput.Credentials.AccessKeyId,
*roleOutput.Credentials.SecretAccessKey,
*roleOutput.Credentials.SessionToken,
)),
),
)
if err != nil {
return "", fmt.Errorf("could not load AWS default config", err)

Check failure on line 280 in postgresql/provider.go

View workflow job for this annotation

GitHub Actions / lint

printf: fmt.Errorf call has arguments but no formatting directives (govet)

Check failure on line 280 in postgresql/provider.go

View workflow job for this annotation

GitHub Actions / test (16)

fmt.Errorf call has arguments but no formatting directives

Check failure on line 280 in postgresql/provider.go

View workflow job for this annotation

GitHub Actions / test (15)

fmt.Errorf call has arguments but no formatting directives

Check failure on line 280 in postgresql/provider.go

View workflow job for this annotation

GitHub Actions / test (14)

fmt.Errorf call has arguments but no formatting directives

Check failure on line 280 in postgresql/provider.go

View workflow job for this annotation

GitHub Actions / test (13)

fmt.Errorf call has arguments but no formatting directives

Check failure on line 280 in postgresql/provider.go

View workflow job for this annotation

GitHub Actions / test (12)

fmt.Errorf call has arguments but no formatting directives
}
}

token, err := auth.BuildAuthToken(ctx, endpoint, awscfg.Region, username, awscfg.Credentials)

return token, err
Expand Down Expand Up @@ -312,8 +347,9 @@
if d.Get("aws_rds_iam_auth").(bool) {
profile := d.Get("aws_rds_iam_profile").(string)
region := d.Get("aws_rds_iam_region").(string)
role := d.Get("aws_rds_iam_provider_role_arn").(string)
var err error
password, err = getRDSAuthToken(region, profile, username, host, port)
password, err = getRDSAuthToken(region, profile, role, username, host, port)
if err != nil {
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions website/docs/index.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ The following arguments are supported:
from the environment (or the given profile, see `aws_rds_iam_profile`)
* `aws_rds_iam_profile` - (Optional) The AWS IAM Profile to use while using AWS RDS IAM Auth.
* `aws_rds_iam_region` - (Optional) The AWS region to use while using AWS RDS IAM Auth.
* `aws_rds_iam_provider_role_arn` - (Optional) AWS IAM role to assume while using AWS RDS IAM Auth.
* `azure_identity_auth` - (Optional) If set to `true`, call the Azure OAuth token endpoint for temporary token
* `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)

Expand Down
Loading