@@ -15,16 +15,8 @@ package aws
1515
1616import (
1717 "context"
18- "errors"
19- "fmt"
20- "strconv"
21- "time"
22-
23- "github.com/aws/aws-sdk-go-v2/config"
24- v2creds "github.com/aws/aws-sdk-go-v2/credentials"
25- "github.com/aws/aws-sdk-go-v2/feature/rds/auth"
18+
2619 "github.com/aws/aws-sdk-go/aws"
27- "github.com/jackc/pgx/v5"
2820 "github.com/jackc/pgx/v5/pgxpool"
2921
3022 "github.com/dapr/kit/logger"
@@ -34,16 +26,6 @@ type EnvironmentSettings struct {
3426 Metadata map [string ]string
3527}
3628
37- type AWSIAM struct {
38- // Ignored by metadata parser because included in built-in authentication profile
39- // Access key to use for accessing PostgreSQL.
40- AWSAccessKey string `json:"awsAccessKey" mapstructure:"awsAccessKey"`
41- // Secret key to use for accessing PostgreSQL.
42- AWSSecretKey string `json:"awsSecretKey" mapstructure:"awsSecretKey"`
43- // AWS region in which PostgreSQL is deployed.
44- AWSRegion string `json:"awsRegion" mapstructure:"awsRegion"`
45- }
46-
4729// TODO: Delete in Dapr 1.17 so we can move all IAM fields to use the defaults of:
4830// accessKey and secretKey and region as noted in the docs, and Options struct above.
4931type DeprecatedKafkaIAM struct {
@@ -55,14 +37,6 @@ type DeprecatedKafkaIAM struct {
5537 StsSessionName string `json:"awsStsSessionName" mapstructure:"awsStsSessionName"`
5638}
5739
58- type AWSIAMAuthOptions struct {
59- PoolConfig * pgxpool.Config `json:"poolConfig" mapstructure:"poolConfig"`
60- ConnectionString string `json:"connectionString" mapstructure:"connectionString"`
61- Region string `json:"region" mapstructure:"region"`
62- AccessKey string `json:"accessKey" mapstructure:"accessKey"`
63- SecretKey string `json:"secretKey" mapstructure:"secretKey"`
64- }
65-
6640type Options struct {
6741 Logger logger.Logger
6842 Properties map [string ]string
@@ -75,11 +49,20 @@ type Options struct {
7549 Region string `json:"region" mapstructure:"region" mapstructurealiases:"awsRegion"`
7650 AccessKey string `json:"accessKey" mapstructure:"accessKey"`
7751 SecretKey string `json:"secretKey" mapstructure:"secretKey"`
78- SessionName string `mapstructure:"sessionName"`
79- AssumeRoleARN string `mapstructure:"assumeRoleArn"`
52+ SessionName string `json:"sessionName" mapstructure:"sessionName"`
53+ AssumeRoleARN string `json:"assumeRoleArn" mapstructure:"assumeRoleArn"`
54+ SessionToken string `json:"sessionToken" mapstructure:"sessionToken"`
8055
81- Endpoint string
82- SessionToken string
56+ Endpoint string
57+ }
58+
59+ // TODO: Delete in Dapr 1.17 so we can move all IAM fields to use the defaults of:
60+ // accessKey and secretKey and region as noted in the docs, and Options struct above.
61+ type DeprecatedPostgresIAM struct {
62+ // Access key to use for accessing PostgreSQL.
63+ AccessKey string `json:"awsAccessKey" mapstructure:"awsAccessKey"`
64+ // Secret key to use for accessing PostgreSQL.
65+ SecretKey string `json:"awsSecretKey" mapstructure:"awsSecretKey"`
8366}
8467
8568func GetConfig (opts Options ) * aws.Config {
@@ -106,9 +89,14 @@ type Provider interface {
10689 ParameterStore () * ParameterStoreClients
10790 Kinesis () * KinesisClients
10891 Ses () * SesClients
109-
11092 Kafka (KafkaOptions ) (* KafkaClients , error )
11193
94+ // Postgres is an outlier to the others in the sense that we can update only it's config,
95+ // as we use a max connection time of 8 minutes.
96+ // This means that we can just update the config session credentials,
97+ // and then in 8 minutes it will update to a new session automatically for us.
98+ UpdatePostgres (context.Context , * pgxpool.Config )
99+
112100 Close () error
113101}
114102
@@ -128,69 +116,6 @@ func NewEnvironmentSettings(md map[string]string) (EnvironmentSettings, error) {
128116 return es , nil
129117}
130118
131- func (opts * Options ) GetAccessToken (ctx context.Context ) (string , error ) {
132- dbEndpoint := opts .PoolConfig .ConnConfig .Host + ":" + strconv .Itoa (int (opts .PoolConfig .ConnConfig .Port ))
133- var authenticationToken string
134-
135- // https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/UsingWithRDS.IAMDBAuth.Connecting.Go.html
136- // Default to load default config through aws credentials file (~/.aws/credentials)
137- awsCfg , err := config .LoadDefaultConfig (ctx )
138- // Note: in the event of an error with invalid config or failed to load config,
139- // then we fall back to using the access key and secret key.
140- switch {
141- case errors .Is (err , config.SharedConfigAssumeRoleError {}.Err ),
142- errors .Is (err , config.SharedConfigLoadError {}.Err ),
143- errors .Is (err , config.SharedConfigProfileNotExistError {}.Err ):
144- // Validate if access key and secret access key are provided
145- if opts .AccessKey == "" || opts .SecretKey == "" {
146- return "" , fmt .Errorf ("failed to load default configuration for AWS using accessKey and secretKey: %w" , err )
147- }
148-
149- // Set credentials explicitly
150- awsCfg := v2creds .NewStaticCredentialsProvider (opts .AccessKey , opts .SecretKey , "" )
151- authenticationToken , err = auth .BuildAuthToken (
152- ctx , dbEndpoint , opts .Region , opts .PoolConfig .ConnConfig .User , awsCfg )
153- if err != nil {
154- return "" , fmt .Errorf ("failed to create AWS authentication token: %w" , err )
155- }
156-
157- return authenticationToken , nil
158- case err != nil :
159- return "" , errors .New ("failed to load default AWS authentication configuration" )
160- }
161-
162- authenticationToken , err = auth .BuildAuthToken (
163- ctx , dbEndpoint , opts .Region , opts .PoolConfig .ConnConfig .User , awsCfg .Credentials )
164- if err != nil {
165- return "" , fmt .Errorf ("failed to create AWS authentication token: %w" , err )
166- }
167-
168- return authenticationToken , nil
169- }
170-
171- func (opts * Options ) InitiateAWSIAMAuth () error {
172- // Set max connection lifetime to 8 minutes in postgres connection pool configuration.
173- // Note: this will refresh connections before the 15 min expiration on the IAM AWS auth token,
174- // while leveraging the BeforeConnect hook to recreate the token in time dynamically.
175- opts .PoolConfig .MaxConnLifetime = time .Minute * 8
176-
177- // Setup connection pool config needed for AWS IAM authentication
178- opts .PoolConfig .BeforeConnect = func (ctx context.Context , pgConfig * pgx.ConnConfig ) error {
179- // Manually reset auth token with aws and reset the config password using the new iam token
180- pwd , errGetAccessToken := opts .GetAccessToken (ctx )
181- if errGetAccessToken != nil {
182- return fmt .Errorf ("failed to refresh access token for iam authentication with PostgreSQL: %w" , errGetAccessToken )
183- }
184-
185- pgConfig .Password = pwd
186- opts .PoolConfig .ConnConfig .Password = pwd
187-
188- return nil
189- }
190-
191- return nil
192- }
193-
194119// Coalesce is a helper function to return the first non-empty string from the inputs
195120// This helps us to migrate away from the deprecated duplicate aws auth profile metadata fields in Dapr 1.17.
196121func Coalesce (values ... string ) string {
0 commit comments