@@ -441,33 +441,65 @@ func (p *Plugin) createS3Client() *s3.S3 {
441441 S3ForcePathStyle : aws .Bool (p .PathStyle ),
442442 }
443443
444- sess , err := session .NewSession (conf )
445- if err != nil {
446- log .Fatalf ("failed to create AWS session: %v" , err )
447- }
448-
444+ // Set credentials before creating the session
449445 if p .Key != "" && p .Secret != "" {
450446 conf .Credentials = credentials .NewStaticCredentials (p .Key , p .Secret , "" )
451447 } else if p .IdToken != "" && p .AssumeRole != "" {
452- creds , err := assumeRoleWithWebIdentity (sess , p .AssumeRole , p .AssumeRoleSessionName , p .IdToken )
448+ // Create a temporary session for assuming the role
449+ tempSess , err := session .NewSession (conf )
450+ if err != nil {
451+ log .Fatalf ("failed to create temporary AWS session: %v" , err )
452+ }
453+
454+ creds , err := assumeRoleWithWebIdentity (tempSess , p .AssumeRole , p .AssumeRoleSessionName , p .IdToken )
453455 if err != nil {
454456 log .Fatalf ("failed to assume role with web identity: %v" , err )
455457 }
458+
459+ // Update the credentials in the config
456460 conf .Credentials = creds
457461 } else if p .AssumeRole != "" {
458- conf .Credentials = assumeRole (p .AssumeRole , p .AssumeRoleSessionName , p .ExternalID )
462+ // Create a temporary session for assuming the role
463+ tempSess , err := session .NewSession (conf )
464+ if err != nil {
465+ log .Fatalf ("failed to create temporary AWS session: %v" , err )
466+ }
467+
468+ creds := assumeRole (p .AssumeRole , p .AssumeRoleSessionName , p .ExternalID )
469+
470+ // Update the credentials in the config
471+ conf .Credentials = creds
459472 } else {
460- log .Warn ("AWS Key and/or Secret not provided (falling back to ec2 instance profile)" )
473+ log .Warn ("AWS Key and/or Secret not provided (falling back to EC2 instance profile or environment variables)" )
474+ }
475+
476+ // Now create the session with the credentials
477+ sess , err := session .NewSession (conf )
478+ if err != nil {
479+ log .Fatalf ("failed to create AWS session: %v" , err )
461480 }
462481
463- client := s3 .New (sess , conf )
482+ // Create the S3 client using the session
483+ client := s3 .New (sess )
464484
485+ // Optionally assume another role if UserRoleArn is provided
465486 if len (p .UserRoleArn ) > 0 {
466- confRoleArn := aws.Config {
487+ log .WithFields (log.Fields {
488+ "UserRoleArn" : p .UserRoleArn ,
489+ }).Info ("Assuming user role ARN" )
490+
491+ creds := stscreds .NewCredentials (sess , p .UserRoleArn )
492+ // Create a new session with the new credentials
493+ confWithUserRole := & aws.Config {
467494 Region : aws .String (p .Region ),
468- Credentials : stscreds .NewCredentials (sess , p .UserRoleArn ),
495+ Credentials : creds ,
496+ }
497+ sessWithUserRole , err := session .NewSession (confWithUserRole )
498+ if err != nil {
499+ log .Fatalf ("failed to create AWS session with user role: %v" , err )
469500 }
470- client = s3 .New (sess , & confRoleArn )
501+
502+ client = s3 .New (sessWithUserRole )
471503 }
472504
473505 return client
@@ -485,4 +517,4 @@ func assumeRoleWithWebIdentity(sess *session.Session, roleArn, roleSessionName,
485517 log .Fatalf ("failed to assume role with web identity: %v" , err )
486518 }
487519 return credentials .NewStaticCredentials (* result .Credentials .AccessKeyId , * result .Credentials .SecretAccessKey , * result .Credentials .SessionToken ), nil
488- }
520+ }
0 commit comments