Skip to content

Commit 7e201fb

Browse files
author
Nathan Dines
committed
Added Runtime Configuration of STS Token Duration
By enabling runtime configuration of the STS token duration, this enables a shorter token duration to be defined by default on a Jenkins master, but enables users to explicilty request a longer session token for longer running jobs. This helps to ensure that the credential lifetime is only as long as necessary.
1 parent 4f6e283 commit 7e201fb

File tree

6 files changed

+57
-6
lines changed

6 files changed

+57
-6
lines changed

src/main/java/com/cloudbees/jenkins/plugins/awscredentials/AWSCredentialsImpl.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ public boolean requiresToken() {
126126
}
127127

128128
public AWSCredentials getCredentials() {
129+
return getCredentials(this.getStsTokenDuration());
130+
}
131+
132+
public AWSCredentials getCredentials(int stsTokenDuration) {
129133
AWSCredentials initialCredentials = new BasicAWSCredentials(accessKey, secretKey.getPlainText());
130134

131135
if (StringUtils.isBlank(iamRoleArn)) {
@@ -158,7 +162,7 @@ public AWSCredentials getCredentials() {
158162
}
159163

160164
AssumeRoleRequest assumeRequest = createAssumeRoleRequest(iamRoleArn)
161-
.withDurationSeconds(this.getStsTokenDuration());
165+
.withDurationSeconds(stsTokenDuration);
162166

163167
AssumeRoleResult assumeResult = client.assumeRole(assumeRequest);
164168

src/main/java/com/cloudbees/jenkins/plugins/awscredentials/AmazonWebServicesCredentials.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public interface AmazonWebServicesCredentials extends StandardCredentials, AWSCr
4545
String getDisplayName();
4646

4747
AWSCredentials getCredentials(String mfaToken);
48+
AWSCredentials getCredentials(int stsTokenDuration);
4849

4950
/**
5051
* Our name provider.
@@ -61,5 +62,4 @@ public String getName(@NonNull AmazonWebServicesCredentials c) {
6162
return c.getDisplayName() + (description != null ? " (" + description + ")" : "");
6263
}
6364
}
64-
6565
}

src/main/java/com/cloudbees/jenkins/plugins/awscredentials/AmazonWebServicesCredentialsBinding.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@
2727

2828
import com.amazonaws.auth.AWSCredentials;
2929
import com.amazonaws.auth.AWSSessionCredentials;
30-
import com.amazonaws.auth.BasicSessionCredentials;
3130

32-
import edu.umd.cs.findbugs.annotations.CheckForNull;
3331
import edu.umd.cs.findbugs.annotations.NonNull;
3432
import edu.umd.cs.findbugs.annotations.Nullable;
3533
import hudson.Extension;
@@ -41,6 +39,7 @@
4139
import org.jenkinsci.plugins.credentialsbinding.BindingDescriptor;
4240
import org.jenkinsci.plugins.credentialsbinding.MultiBinding;
4341
import org.kohsuke.stapler.DataBoundConstructor;
42+
import org.kohsuke.stapler.DataBoundSetter;
4443

4544
import javax.annotation.Nonnull;
4645
import java.io.IOException;
@@ -63,6 +62,8 @@ public class AmazonWebServicesCredentialsBinding extends MultiBinding<AmazonWebS
6362
private final String accessKeyVariable;
6463
@NonNull
6564
private final String secretKeyVariable;
65+
@Nullable
66+
private volatile Integer stsTokenDuration;
6667

6768
/**
6869
*
@@ -87,14 +88,30 @@ public String getSecretKeyVariable() {
8788
return secretKeyVariable;
8889
}
8990

91+
@Nullable
92+
public Integer getStsTokenDuration() {
93+
return this.stsTokenDuration;
94+
}
95+
96+
@DataBoundSetter
97+
public void setStsTokenDuration(@Nullable Integer stsTokenDuration) {
98+
this.stsTokenDuration = stsTokenDuration;
99+
}
100+
90101
@Override
91102
protected Class<AmazonWebServicesCredentials> type() {
92103
return AmazonWebServicesCredentials.class;
93104
}
94105

95106
@Override
96107
public MultiEnvironment bind(@Nonnull Run<?, ?> build, FilePath workspace, Launcher launcher, TaskListener listener) throws IOException, InterruptedException {
97-
AWSCredentials credentials = getCredentials(build).getCredentials();
108+
AmazonWebServicesCredentials credentialRetriever = getCredentials(build);
109+
AWSCredentials credentials = null;
110+
if (stsTokenDuration != null) {
111+
credentials = credentialRetriever.getCredentials(stsTokenDuration);
112+
} else {
113+
credentials = credentialRetriever.getCredentials();
114+
}
98115
Map<String,String> m = new HashMap<String,String>();
99116
m.put(accessKeyVariable, credentials.getAWSAccessKeyId());
100117
m.put(secretKeyVariable, credentials.getAWSSecretKey());

src/main/resources/com/cloudbees/jenkins/plugins/awscredentials/AWSCredentialsImpl/credentials.jelly

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
<f:entry title="${%MFA Token}" field="iamMfaToken">
4444
<f:textbox/>
4545
</f:entry>
46-
<f:entry title="${%STS Token Duration (sec)}" field="stsTokenDuration">
46+
<f:entry title="${%Default STS Token Duration (sec)}" field="stsTokenDuration">
4747
<f:textbox default="${descriptor.DEFAULT_STS_TOKEN_DURATION}"/>
4848
</f:entry>
4949
</f:advanced>

src/main/resources/com/cloudbees/jenkins/plugins/awscredentials/AmazonWebServicesCredentialsBinding/config-variables.jelly

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,7 @@ THE SOFTWARE.
3030
<f:entry title="${%Secret Key Variable}" field="secretKeyVariable">
3131
<f:textbox default="AWS_SECRET_ACCESS_KEY"/>
3232
</f:entry>
33+
<f:entry title="${%STS Token Duration Override}" field="stsTokenDuration">
34+
<f:textbox />
35+
</f:entry>
3336
</j:jelly>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!--
2+
~ The MIT License
3+
~
4+
~ Copyright (c) 2015, CloudBees, Inc.
5+
~
6+
~ Permission is hereby granted, free of charge, to any person obtaining a copy
7+
~ of this software and associated documentation files (the "Software"), to deal
8+
~ in the Software without restriction, including without limitation the rights
9+
~ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
~ copies of the Software, and to permit persons to whom the Software is
11+
~ furnished to do so, subject to the following conditions:
12+
~
13+
~ The above copyright notice and this permission notice shall be included in
14+
~ all copies or substantial portions of the Software.
15+
~
16+
~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
~ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
~ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
~ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
~ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
~ THE SOFTWARE.
23+
~
24+
-->
25+
<div>
26+
Override the STS Token Duration for these credentials. <i>(optional)</i>
27+
</div>

0 commit comments

Comments
 (0)