-
Notifications
You must be signed in to change notification settings - Fork 72
/
AWSCredentialsImpl.java
360 lines (304 loc) · 15.9 KB
/
AWSCredentialsImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
* The MIT License
*
* Copyright (c) 2015, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
package com.cloudbees.jenkins.plugins.awscredentials;
import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.auth.BasicSessionCredentials;
import com.amazonaws.regions.DefaultAwsRegionProviderChain;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.AmazonEC2Client;
import com.amazonaws.services.ec2.model.DescribeAvailabilityZonesResult;
import com.amazonaws.services.securitytoken.AWSSecurityTokenService;
import com.amazonaws.services.securitytoken.AWSSecurityTokenServiceClientBuilder;
import com.amazonaws.services.securitytoken.model.AssumeRoleRequest;
import com.amazonaws.services.securitytoken.model.AssumeRoleResult;
import com.cloudbees.plugins.credentials.CredentialsDescriptor;
import com.cloudbees.plugins.credentials.CredentialsScope;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.ProxyConfiguration;
import hudson.Util;
import hudson.util.FormValidation;
import hudson.util.Secret;
import jenkins.model.Jenkins;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.verb.POST;
import java.net.HttpURLConnection;
import java.util.logging.Level;
import java.util.logging.Logger;
public class AWSCredentialsImpl extends BaseAmazonWebServicesCredentials {
private static final long serialVersionUID = -3167989896315282034L;
private static final Logger LOGGER = Logger.getLogger(BaseAmazonWebServicesCredentials.class.getName());
public static final int STS_CREDENTIALS_DURATION_SECONDS = 3600;
private final String accessKey;
private final Secret secretKey;
private final String iamRoleArn;
private final String iamExternalId;
private final String iamMfaSerialNumber;
private volatile Integer stsTokenDuration;
// Old data bound constructor. It is maintained to keep binary compatibility with clients that were using it directly.
public AWSCredentialsImpl(@CheckForNull CredentialsScope scope, @CheckForNull String id,
@CheckForNull String accessKey, @CheckForNull String secretKey, @CheckForNull String description) {
this(scope, id, accessKey, secretKey, description, null, null, null);
}
// Old data bound constructor. It is maintained to keep binary compatibility with clients that were using it directly.
public AWSCredentialsImpl(@CheckForNull CredentialsScope scope, @CheckForNull String id,
@CheckForNull String accessKey, @CheckForNull String secretKey, @CheckForNull String description,
@CheckForNull String iamRoleArn, @CheckForNull String iamMfaSerialNumber) {
this(scope, id, accessKey, secretKey, description, iamRoleArn, iamMfaSerialNumber, null);
}
@DataBoundConstructor
public AWSCredentialsImpl(@CheckForNull CredentialsScope scope, @CheckForNull String id,
@CheckForNull String accessKey, @CheckForNull String secretKey, @CheckForNull String description,
@CheckForNull String iamRoleArn, @CheckForNull String iamMfaSerialNumber,
String iamExternalId) {
super(scope, id, description);
this.accessKey = Util.fixNull(accessKey);
this.secretKey = Secret.fromString(secretKey);
this.iamRoleArn = Util.fixNull(iamRoleArn);
this.iamExternalId = Util.fixNull(iamExternalId);
this.iamMfaSerialNumber = Util.fixNull(iamMfaSerialNumber);
}
public String getAccessKey() {
return accessKey;
}
public Secret getSecretKey() {
return secretKey;
}
public String getIamRoleArn() {
return iamRoleArn;
}
public String getIamExternalId() {
return iamExternalId;
}
public String getIamMfaSerialNumber() {
return iamMfaSerialNumber;
}
@NonNull
public Integer getStsTokenDuration() {
return stsTokenDuration == null ? DescriptorImpl.DEFAULT_STS_TOKEN_DURATION : stsTokenDuration;
}
@DataBoundSetter
public void setStsTokenDuration(Integer stsTokenDuration) {
this.stsTokenDuration = stsTokenDuration == null || stsTokenDuration.equals(DescriptorImpl.DEFAULT_STS_TOKEN_DURATION) ? null : stsTokenDuration;
}
public boolean requiresToken() {
return !StringUtils.isBlank(iamMfaSerialNumber);
}
public AWSCredentials getCredentials() {
AWSCredentials initialCredentials = new BasicAWSCredentials(accessKey, secretKey.getPlainText());
if (StringUtils.isBlank(iamRoleArn)) {
return initialCredentials;
} else {
AWSCredentialsProvider baseProvider;
// Handle the case of delegation to instance profile
if (StringUtils.isBlank(accessKey) && StringUtils.isBlank(secretKey.getPlainText())) {
baseProvider = null;
} else {
baseProvider = new AWSStaticCredentialsProvider(initialCredentials);
}
AWSSecurityTokenService client = buildStsClient(baseProvider);
AssumeRoleRequest assumeRequest = createAssumeRoleRequest(iamRoleArn, iamExternalId)
.withDurationSeconds(this.getStsTokenDuration());
AssumeRoleResult assumeResult = client.assumeRole(assumeRequest);
return new BasicSessionCredentials(
assumeResult.getCredentials().getAccessKeyId(),
assumeResult.getCredentials().getSecretAccessKey(),
assumeResult.getCredentials().getSessionToken());
}
}
private static String determineClientRegion() {
// Check for available region from the SDK, otherwise specify default
String clientRegion = null;
DefaultAwsRegionProviderChain sdkRegionLookup = new DefaultAwsRegionProviderChain();
try {
clientRegion = sdkRegionLookup.getRegion();
} catch (RuntimeException e) {
LOGGER.log(Level.WARNING, "Could not find default region using SDK lookup.", e);
}
if (clientRegion == null) {
clientRegion = Regions.DEFAULT_REGION.getName();
}
return clientRegion;
}
public AWSCredentials getCredentials(String mfaToken) {
AWSCredentials initialCredentials = new BasicAWSCredentials(accessKey, secretKey.getPlainText());
AssumeRoleRequest assumeRequest = createAssumeRoleRequest(iamRoleArn, iamExternalId)
.withSerialNumber(iamMfaSerialNumber)
.withTokenCode(mfaToken)
.withDurationSeconds(this.getStsTokenDuration());
AWSSecurityTokenService awsSecurityTokenService = getAWSSecurityTokenService(initialCredentials);
AssumeRoleResult assumeResult = awsSecurityTokenService.assumeRole(assumeRequest);
return new BasicSessionCredentials(
assumeResult.getCredentials().getAccessKeyId(),
assumeResult.getCredentials().getSecretAccessKey(),
assumeResult.getCredentials().getSessionToken());
}
public void refresh() {
// no-op
}
public String getDisplayName() {
if (StringUtils.isBlank(iamRoleArn)) {
return accessKey;
}
return accessKey + ":" + iamRoleArn;
}
/*package*/ static AWSSecurityTokenService buildStsClient(AWSCredentialsProvider provider) {
// Check for available region from the SDK, otherwise specify default
String clientRegion = determineClientRegion();
AWSSecurityTokenServiceClientBuilder builder = AWSSecurityTokenServiceClientBuilder.standard()
.withRegion(clientRegion)
.withClientConfiguration(getClientConfiguration());
if (provider != null) {
builder = builder.withCredentials(provider);
}
return builder.build();
}
private static AssumeRoleRequest createAssumeRoleRequest(String iamRoleArn, String iamExternalId) {
AssumeRoleRequest retval = new AssumeRoleRequest()
.withRoleArn(iamRoleArn)
.withRoleSessionName("Jenkins");
if (iamExternalId != null && !iamExternalId.isEmpty()) {
return retval.withExternalId(iamExternalId);
}
return retval;
}
/**
* Provides the {@link AWSSecurityTokenService} for a given {@link AWSCredentials}
*
* @param awsCredentials
* @return {@link AWSSecurityTokenService}
*/
private static AWSSecurityTokenService getAWSSecurityTokenService(AWSCredentials awsCredentials) {
ClientConfiguration clientConfiguration = getClientConfiguration();
String clientRegion = determineClientRegion();
return AWSSecurityTokenServiceClientBuilder.standard()
.withRegion(clientRegion)
.withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
.withClientConfiguration(clientConfiguration)
.build();
}
/**
* Provides the {@link ClientConfiguration}
*
* @return {@link ClientConfiguration}
*/
private static ClientConfiguration getClientConfiguration() {
Jenkins instance = Jenkins.getInstanceOrNull();
ProxyConfiguration proxy = instance != null ? instance.proxy : null;
ClientConfiguration clientConfiguration = new ClientConfiguration();
if (proxy != null && proxy.name != null && !proxy.name.isEmpty()) {
clientConfiguration.setProxyHost(proxy.name);
clientConfiguration.setProxyPort(proxy.port);
clientConfiguration.setProxyUsername(proxy.getUserName());
clientConfiguration.setProxyPassword(proxy.getPassword());
}
return clientConfiguration;
}
@Extension
public static class DescriptorImpl extends CredentialsDescriptor {
@Override
public String getDisplayName() {
return Messages.AWSCredentialsImpl_DisplayName();
}
public static final Integer DEFAULT_STS_TOKEN_DURATION = STS_CREDENTIALS_DURATION_SECONDS;
@POST
public FormValidation doCheckSecretKey(@QueryParameter("accessKey") final String accessKey,
@QueryParameter("iamRoleArn") final String iamRoleArn,
@QueryParameter("iamExternalId") final String iamExternalId,
@QueryParameter("iamMfaSerialNumber") final String iamMfaSerialNumber,
@QueryParameter("iamMfaToken") final String iamMfaToken,
@QueryParameter("stsTokenDuration") final Integer stsTokenDuration,
@QueryParameter final String secretKey) {
if (!Jenkins.get().hasPermission(Jenkins.ADMINISTER)) {
// for security reasons, do not perform any check if the user is not an admin
return FormValidation.ok();
}
if (StringUtils.isBlank(accessKey) && StringUtils.isBlank(secretKey)) {
return FormValidation.ok();
}
if (StringUtils.isBlank(accessKey)) {
return FormValidation.error(Messages.AWSCredentialsImpl_SpecifyAccessKeyId());
}
if (StringUtils.isBlank(secretKey)) {
return FormValidation.error(Messages.AWSCredentialsImpl_SpecifySecretAccessKey());
}
AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, Secret.fromString(secretKey).getPlainText());
// If iamRoleArn is specified, swap out the credentials.
if (!StringUtils.isBlank(iamRoleArn)) {
AssumeRoleRequest assumeRequest = createAssumeRoleRequest(iamRoleArn, iamExternalId)
.withDurationSeconds(stsTokenDuration);
if (!StringUtils.isBlank(iamMfaSerialNumber)) {
if (StringUtils.isBlank(iamMfaToken)) {
return FormValidation.error(Messages.AWSCredentialsImpl_SpecifyMFAToken());
}
assumeRequest = assumeRequest
.withSerialNumber(iamMfaSerialNumber)
.withTokenCode(iamMfaToken);
}
try {
AWSSecurityTokenService awsSecurityTokenService = getAWSSecurityTokenService(awsCredentials);
AssumeRoleResult assumeResult = awsSecurityTokenService.assumeRole(assumeRequest);
awsCredentials = new BasicSessionCredentials(
assumeResult.getCredentials().getAccessKeyId(),
assumeResult.getCredentials().getSecretAccessKey(),
assumeResult.getCredentials().getSessionToken());
} catch (RuntimeException e) {
LOGGER.log(Level.WARNING, "Unable to assume role [" + iamRoleArn + "] with request [" + assumeRequest + "]", e);
return FormValidation.error(Messages.AWSCredentialsImpl_NotAbleToAssumeRole() + " Check the Jenkins log for more details");
}
}
AmazonEC2 ec2 = new AmazonEC2Client(awsCredentials, getClientConfiguration());
// TODO better/smarter validation of the credentials instead of verifying the permission on EC2.READ in us-east-1
String region = "us-east-1";
try {
DescribeAvailabilityZonesResult zonesResult = ec2.describeAvailabilityZones();
return FormValidation
.ok(Messages.AWSCredentialsImpl_CredentialsValidWithAccessToNZones(
zonesResult.getAvailabilityZones().size()));
} catch (AmazonServiceException e) {
if (HttpURLConnection.HTTP_UNAUTHORIZED == e.getStatusCode()) {
return FormValidation.warning(Messages.AWSCredentialsImpl_CredentialsInValid(e.getMessage()));
} else if (HttpURLConnection.HTTP_FORBIDDEN == e.getStatusCode()) {
return FormValidation.ok(Messages.AWSCredentialsImpl_CredentialsValidWithoutAccessToAwsServiceInZone(e.getServiceName(), region, e.getErrorMessage() + " (" + e.getErrorCode() + ")"));
} else {
return FormValidation.error(e.getMessage());
}
} catch (AmazonClientException e) {
return FormValidation.error(e.getMessage());
}
}
}
}