@@ -446,6 +446,7 @@ func (p *Plugin) createS3Client() *s3.S3 {
446446 S3ForcePathStyle : aws .Bool (p .PathStyle ),
447447 }
448448
449+ // Create initial session
449450 sess , err := session .NewSession (conf )
450451 if err != nil {
451452 log .Fatalf ("failed to create AWS session: %v" , err )
@@ -465,29 +466,30 @@ func (p *Plugin) createS3Client() *s3.S3 {
465466 log .Warn ("AWS Key and/or Secret not provided (falling back to ec2 instance profile)" )
466467 }
467468
468- client := s3 .New (sess , conf )
469469
470+ // Create session with primary credentials
471+ sess , err = session .NewSession (conf )
472+ if err != nil {
473+ log .Fatalf ("failed to create AWS session: %v" , err )
474+ }
475+
476+ // Initialize client with the session
477+ client := s3 .New (sess )
478+
479+ // Handle secondary role assumption if UserRoleArn is provided
470480 if len (p .UserRoleArn ) > 0 {
471481 log .WithField ("UserRoleArn" , p .UserRoleArn ).Info ("Using user role ARN" )
472- // Create new credentials by assuming the UserRoleArn (with ExternalID when provided)
482+
483+ // Create credentials using the existing session for role assumption
484+ // by assuming the UserRoleArn (with ExternalID when provided)
473485 creds := stscreds .NewCredentials (sess , p .UserRoleArn , func (provider * stscreds.AssumeRoleProvider ) {
474486 if p .UserRoleExternalID != "" {
475487 provider .ExternalID = aws .String (p .UserRoleExternalID )
476488 }
477489 })
478490
479- // Create a new session with the new credentials
480- confWithUserRole := & aws.Config {
481- Region : aws .String (p .Region ),
482- Credentials : creds ,
483- }
484-
485- sessWithUserRole , err := session .NewSession (confWithUserRole )
486- if err != nil {
487- log .Fatalf ("failed to create AWS session with user role: %v" , err )
488- }
489-
490- client = s3 .New (sessWithUserRole )
491+ // Create new client with same config but updated credentials
492+ client = s3 .New (sess , & aws.Config {Credentials : creds })
491493 }
492494
493495 return client
0 commit comments