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

feat: Add support for assuming an AWS IAM role from the provider. #486

Merged
merged 5 commits into from
Nov 7, 2024
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": {
zizzencs marked this conversation as resolved.
Show resolved Hide resolved
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
cyrilgdn marked this conversation as resolved.
Show resolved Hide resolved
}
}

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