2727
2828import com .amazonaws .auth .AWSCredentials ;
2929import com .amazonaws .auth .AWSSessionCredentials ;
30+ import com .amazonaws .auth .AWSCredentialsProvider ;
31+ import com .amazonaws .auth .AWSSessionCredentialsProvider ;
32+ import com .amazonaws .auth .STSAssumeRoleSessionCredentialsProvider ;
33+ import com .amazonaws .services .securitytoken .AWSSecurityTokenService ;
3034import edu .umd .cs .findbugs .annotations .NonNull ;
3135import edu .umd .cs .findbugs .annotations .Nullable ;
3236import hudson .Extension ;
3943import org .jenkinsci .plugins .credentialsbinding .MultiBinding ;
4044import org .jenkinsci .Symbol ;
4145import org .kohsuke .stapler .DataBoundConstructor ;
46+ import org .kohsuke .stapler .DataBoundSetter ;
4247
4348import javax .annotation .Nonnull ;
4449import java .io .IOException ;
@@ -62,6 +67,10 @@ public class AmazonWebServicesCredentialsBinding extends MultiBinding<AmazonWebS
6267 @ NonNull
6368 private final String secretKeyVariable ;
6469
70+ private String roleArn ;
71+ private String roleSessionName ;
72+ private int roleSessionDurationSeconds ;
73+
6574 /**
6675 *
6776 * @param accessKeyVariable if {@code null}, {@value DEFAULT_ACCESS_KEY_ID_VARIABLE_NAME} will be used.
@@ -85,14 +94,35 @@ public String getSecretKeyVariable() {
8594 return secretKeyVariable ;
8695 }
8796
97+ @ DataBoundSetter
98+ public void setRoleArn (String roleArn ) {
99+ this .roleArn = roleArn ;
100+ }
101+
102+ @ DataBoundSetter
103+ public void setRoleSessionName (String roleSessionName ) {
104+ this .roleSessionName = roleSessionName ;
105+ }
106+
107+ @ DataBoundSetter
108+ public void setRoleSessionDurationSeconds (int roleSessionDurationSeconds ) {
109+ this .roleSessionDurationSeconds = roleSessionDurationSeconds ;
110+ }
111+
88112 @ Override
89113 protected Class <AmazonWebServicesCredentials > type () {
90114 return AmazonWebServicesCredentials .class ;
91115 }
92116
93117 @ Override
94118 public MultiEnvironment bind (@ Nonnull Run <?, ?> build , FilePath workspace , Launcher launcher , TaskListener listener ) throws IOException , InterruptedException {
95- AWSCredentials credentials = getCredentials (build ).getCredentials ();
119+ AWSCredentialsProvider provider = getCredentials (build );
120+ if (!StringUtils .isEmpty (this .roleArn )) {
121+ provider = this .assumeRoleProvider (provider );
122+ }
123+
124+ AWSCredentials credentials = provider .getCredentials ();
125+
96126 Map <String ,String > m = new HashMap <String ,String >();
97127 m .put (accessKeyVariable , credentials .getAWSAccessKeyId ());
98128 m .put (secretKeyVariable , credentials .getAWSSecretKey ());
@@ -104,9 +134,26 @@ public MultiEnvironment bind(@Nonnull Run<?, ?> build, FilePath workspace, Launc
104134 return new MultiEnvironment (m );
105135 }
106136
137+ private AWSSessionCredentialsProvider assumeRoleProvider (AWSCredentialsProvider baseProvider ) {
138+ AWSSecurityTokenService stsClient = AWSCredentialsImpl .buildStsClient (baseProvider );
139+
140+ String roleSessionName = StringUtils .defaultIfBlank (this .roleSessionName , "Jenkins" );
141+
142+ STSAssumeRoleSessionCredentialsProvider .Builder assumeRoleProviderBuilder =
143+ new STSAssumeRoleSessionCredentialsProvider .Builder (this .roleArn , roleSessionName )
144+ .withStsClient (stsClient );
145+
146+ if (this .roleSessionDurationSeconds > 0 ) {
147+ assumeRoleProviderBuilder = assumeRoleProviderBuilder
148+ .withRoleSessionDurationSeconds (this .roleSessionDurationSeconds );
149+ }
150+
151+ return assumeRoleProviderBuilder .build ();
152+ }
153+
107154 @ Override
108155 public Set <String > variables () {
109- return new HashSet <String >(Arrays .asList (accessKeyVariable , secretKeyVariable ));
156+ return new HashSet <String >(Arrays .asList (accessKeyVariable , secretKeyVariable , SESSION_TOKEN_VARIABLE_NAME ));
110157 }
111158
112159 @ Symbol ("aws" )
0 commit comments