Skip to content

Commit c5c5253

Browse files
authored
fix: resolve credentials last such that other environment config is respected (#4781)
1 parent 4ff0cb4 commit c5c5253

File tree

2 files changed

+32
-29
lines changed

2 files changed

+32
-29
lines changed

CHANGELOG_PENDING.md

+3
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@
33
### SDK Enhancements
44

55
### SDK Bugs
6+
7+
* `aws/session`: Fix `AWS_USE_FIPS_ENDPOINT` not being inferred on resolved credentials.
8+
* Defer resolving default credentials chain until after other config is resolved.

aws/session/session.go

+29-29
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@ const (
174174

175175
// Options provides the means to control how a Session is created and what
176176
// configuration values will be loaded.
177-
//
178177
type Options struct {
179178
// Provides config values for the SDK to use when creating service clients
180179
// and making API requests to services. Any value set in with this field
@@ -322,24 +321,24 @@ type Options struct {
322321
// credentials file. Enabling the Shared Config will also allow the Session
323322
// to be built with retrieving credentials with AssumeRole set in the config.
324323
//
325-
// // Equivalent to session.New
326-
// sess := session.Must(session.NewSessionWithOptions(session.Options{}))
324+
// // Equivalent to session.New
325+
// sess := session.Must(session.NewSessionWithOptions(session.Options{}))
327326
//
328-
// // Specify profile to load for the session's config
329-
// sess := session.Must(session.NewSessionWithOptions(session.Options{
330-
// Profile: "profile_name",
331-
// }))
327+
// // Specify profile to load for the session's config
328+
// sess := session.Must(session.NewSessionWithOptions(session.Options{
329+
// Profile: "profile_name",
330+
// }))
332331
//
333-
// // Specify profile for config and region for requests
334-
// sess := session.Must(session.NewSessionWithOptions(session.Options{
335-
// Config: aws.Config{Region: aws.String("us-east-1")},
336-
// Profile: "profile_name",
337-
// }))
332+
// // Specify profile for config and region for requests
333+
// sess := session.Must(session.NewSessionWithOptions(session.Options{
334+
// Config: aws.Config{Region: aws.String("us-east-1")},
335+
// Profile: "profile_name",
336+
// }))
338337
//
339-
// // Force enable Shared Config support
340-
// sess := session.Must(session.NewSessionWithOptions(session.Options{
341-
// SharedConfigState: session.SharedConfigEnable,
342-
// }))
338+
// // Force enable Shared Config support
339+
// sess := session.Must(session.NewSessionWithOptions(session.Options{
340+
// SharedConfigState: session.SharedConfigEnable,
341+
// }))
343342
func NewSessionWithOptions(opts Options) (*Session, error) {
344343
var envCfg envConfig
345344
var err error
@@ -375,7 +374,7 @@ func NewSessionWithOptions(opts Options) (*Session, error) {
375374
// This helper is intended to be used in variable initialization to load the
376375
// Session and configuration at startup. Such as:
377376
//
378-
// var sess = session.Must(session.NewSession())
377+
// var sess = session.Must(session.NewSession())
379378
func Must(sess *Session, err error) *Session {
380379
if err != nil {
381380
panic(err)
@@ -780,16 +779,6 @@ func mergeConfigSrcs(cfg, userCfg *aws.Config,
780779
cfg.EndpointResolver = wrapEC2IMDSEndpoint(cfg.EndpointResolver, ec2IMDSEndpoint, endpointMode)
781780
}
782781

783-
// Configure credentials if not already set by the user when creating the
784-
// Session.
785-
if cfg.Credentials == credentials.AnonymousCredentials && userCfg.Credentials == nil {
786-
creds, err := resolveCredentials(cfg, envCfg, sharedCfg, handlers, sessOpts)
787-
if err != nil {
788-
return err
789-
}
790-
cfg.Credentials = creds
791-
}
792-
793782
cfg.S3UseARNRegion = userCfg.S3UseARNRegion
794783
if cfg.S3UseARNRegion == nil {
795784
cfg.S3UseARNRegion = &envCfg.S3UseARNRegion
@@ -812,6 +801,17 @@ func mergeConfigSrcs(cfg, userCfg *aws.Config,
812801
}
813802
}
814803

804+
// Configure credentials if not already set by the user when creating the Session.
805+
// Credentials are resolved last such that all _resolved_ config values are propagated to credential providers.
806+
// ticket: P83606045
807+
if cfg.Credentials == credentials.AnonymousCredentials && userCfg.Credentials == nil {
808+
creds, err := resolveCredentials(cfg, envCfg, sharedCfg, handlers, sessOpts)
809+
if err != nil {
810+
return err
811+
}
812+
cfg.Credentials = creds
813+
}
814+
815815
return nil
816816
}
817817

@@ -845,8 +845,8 @@ func initHandlers(s *Session) {
845845
// and handlers. If any additional configs are provided they will be merged
846846
// on top of the Session's copied config.
847847
//
848-
// // Create a copy of the current Session, configured for the us-west-2 region.
849-
// sess.Copy(&aws.Config{Region: aws.String("us-west-2")})
848+
// // Create a copy of the current Session, configured for the us-west-2 region.
849+
// sess.Copy(&aws.Config{Region: aws.String("us-west-2")})
850850
func (s *Session) Copy(cfgs ...*aws.Config) *Session {
851851
newSession := &Session{
852852
Config: s.Config.Copy(cfgs...),

0 commit comments

Comments
 (0)