|
23 | 23 | */
|
24 | 24 | package com.dtolabs.rundeck.plugin.resources.ec2;
|
25 | 25 |
|
26 |
| -import com.amazonaws.auth.*; |
27 | 26 | import com.amazonaws.ClientConfiguration;
|
| 27 | +import com.amazonaws.auth.*; |
| 28 | +import com.amazonaws.regions.RegionUtils; |
| 29 | +import com.amazonaws.regions.Regions; |
28 | 30 | import com.amazonaws.services.securitytoken.AWSSecurityTokenService;
|
29 | 31 | import com.amazonaws.services.securitytoken.AWSSecurityTokenServiceClientBuilder;
|
30 |
| -import com.amazonaws.services.securitytoken.model.*; |
31 |
| -import com.dtolabs.rundeck.core.common.*; |
| 32 | +import com.amazonaws.services.securitytoken.model.AssumeRoleRequest; |
| 33 | +import com.amazonaws.services.securitytoken.model.AssumeRoleResult; |
| 34 | +import com.amazonaws.services.securitytoken.model.Credentials; |
| 35 | +import com.dtolabs.rundeck.core.common.INodeSet; |
32 | 36 | import com.dtolabs.rundeck.core.plugins.configuration.ConfigurationException;
|
33 | 37 | import com.dtolabs.rundeck.core.resources.ResourceModelSource;
|
34 | 38 | import com.dtolabs.rundeck.core.resources.ResourceModelSourceException;
|
|
40 | 44 | import org.slf4j.LoggerFactory;
|
41 | 45 |
|
42 | 46 | import java.io.*;
|
43 |
| -import java.util.*; |
| 47 | +import java.util.ArrayList; |
| 48 | +import java.util.Collections; |
| 49 | +import java.util.Properties; |
44 | 50 | import java.util.concurrent.ExecutionException;
|
45 | 51 | import java.util.concurrent.ExecutorService;
|
46 | 52 | import java.util.concurrent.Executors;
|
@@ -92,8 +98,7 @@ public class EC2ResourceModelSource implements ResourceModelSource {
|
92 | 98 | final String externalId;
|
93 | 99 | int pageResults;
|
94 | 100 |
|
95 |
| - AWSCredentials credentials; |
96 |
| - ClientConfiguration clientConfiguration = new ClientConfiguration();; |
| 101 | + ClientConfiguration clientConfiguration = new ClientConfiguration(); |
97 | 102 |
|
98 | 103 | INodeSet iNodeSet;
|
99 | 104 | static final Properties defaultMapping = new Properties();
|
@@ -154,6 +159,7 @@ public class EC2ResourceModelSource implements ResourceModelSource {
|
154 | 159 | }
|
155 | 160 |
|
156 | 161 | public EC2ResourceModelSource(final Properties configuration, final Services services) {
|
| 162 | + this.services = services; |
157 | 163 | this.accessKey = configuration.getProperty(EC2ResourceModelSourceFactory.ACCESS_KEY);
|
158 | 164 | this.secretKey = configuration.getProperty(EC2ResourceModelSourceFactory.SECRET_KEY);
|
159 | 165 | this.region = configuration.getProperty(EC2ResourceModelSourceFactory.REGION);
|
@@ -203,54 +209,66 @@ public EC2ResourceModelSource(final Properties configuration, final Services ser
|
203 | 209 | EC2ResourceModelSourceFactory.RUNNING_ONLY));
|
204 | 210 | logger.info("[debug] runningOnly:" + runningOnly);
|
205 | 211 | }
|
206 |
| - if (null != accessKey && null != secretKeyStoragePath) { |
207 | 212 |
|
208 |
| - KeyStorageTree keyStorage = services.getService(KeyStorageTree.class); |
209 |
| - String secretKey = getPasswordFromKeyStorage(secretKeyStoragePath, keyStorage); |
210 | 213 |
|
211 |
| - credentials = new BasicAWSCredentials(accessKey.trim(), secretKey.trim()); |
212 |
| - }else if (null != accessKey && null != secretKey) { |
213 |
| - credentials = new BasicAWSCredentials(accessKey.trim(), secretKey.trim()); |
214 |
| - } |
215 | 214 | if (null != httpProxyHost && !"".equals(httpProxyHost)) {
|
216 |
| - clientConfiguration.setProxyHost(httpProxyHost); |
217 |
| - clientConfiguration.setProxyPort(httpProxyPort); |
218 |
| - clientConfiguration.setProxyUsername(httpProxyUser); |
219 |
| - clientConfiguration.setProxyPassword(httpProxyPass); |
| 215 | + this.clientConfiguration.setProxyHost(httpProxyHost); |
| 216 | + this.clientConfiguration.setProxyPort(httpProxyPort); |
| 217 | + this.clientConfiguration.setProxyUsername(httpProxyUser); |
| 218 | + this.clientConfiguration.setProxyPassword(httpProxyPass); |
220 | 219 | }
|
| 220 | + |
221 | 221 | queryAsync = !("true".equals(configuration.getProperty(SYNCHRONOUS_LOAD)) || refreshInterval <= 0);
|
222 | 222 |
|
223 | 223 | this.queryNodeInstancesInParallel = Boolean.parseBoolean(configuration.getProperty(EC2ResourceModelSourceFactory.QUERY_NODE_INSTANCES_IN_PARALLEL, "false"));
|
224 |
| - initialize(); |
225 |
| - } |
226 | 224 |
|
227 |
| - private void initialize() { |
228 | 225 | final ArrayList<String> params = new ArrayList<String>();
|
229 | 226 | if (null != filterParams) {
|
230 | 227 | Collections.addAll(params, filterParams.split(";"));
|
231 | 228 | }
|
232 | 229 | loadMapping();
|
233 | 230 |
|
234 |
| - if (this.credentials == null) { |
235 |
| - if(this.externalId != null && this.assumeRoleArnCombinedWithExtId != null){ |
236 |
| - this.credentials = createAwsCredentials(null, this.assumeRoleArnCombinedWithExtId, this.externalId); |
237 |
| - } |
| 231 | + mapper = new InstanceToNodeMapper(createEc2Supplier(), mapping, pageResults); |
| 232 | + mapper.setFilterParams(params); |
| 233 | + mapper.setEndpoint(endpoint); |
| 234 | + mapper.setRegion(region); |
| 235 | + mapper.setRunningStateOnly(runningOnly); |
| 236 | + } |
238 | 237 |
|
239 |
| - if(assumeRoleArn != null) { |
240 |
| - AWSCredentialsProvider provider = null; |
241 |
| - if(this.credentials != null){ |
242 |
| - provider = new AWSStaticCredentialsProvider(credentials); |
243 |
| - } |
244 | 238 |
|
245 |
| - credentials = createAwsCredentials(provider, assumeRoleArn, null); |
| 239 | + protected AWSCredentials createCredentials() { |
| 240 | + if (null != accessKey && null != secretKeyStoragePath) { |
| 241 | + KeyStorageTree keyStorage = services.getService(KeyStorageTree.class); |
| 242 | + String secretKey = getPasswordFromKeyStorage(secretKeyStoragePath, keyStorage); |
| 243 | + return new BasicAWSCredentials(accessKey.trim(), secretKey.trim()); |
| 244 | + } else if (null != accessKey && null != secretKey) { |
| 245 | + return new BasicAWSCredentials(accessKey.trim(), secretKey.trim()); |
| 246 | + } |
| 247 | + |
| 248 | + AWSCredentials credentials = null; |
| 249 | + if (this.externalId != null && this.assumeRoleArnCombinedWithExtId != null) { |
| 250 | + credentials = createAwsCredentials(null, this.assumeRoleArnCombinedWithExtId, this.externalId); |
| 251 | + } |
| 252 | + |
| 253 | + if (assumeRoleArn != null) { |
| 254 | + AWSCredentialsProvider provider = null; |
| 255 | + if (credentials != null) { |
| 256 | + provider = new AWSStaticCredentialsProvider(credentials); |
246 | 257 | }
|
| 258 | + |
| 259 | + return createAwsCredentials(provider, assumeRoleArn, null); |
247 | 260 | }
|
| 261 | + return credentials; |
| 262 | + } |
248 | 263 |
|
249 |
| - mapper = new InstanceToNodeMapper(this.credentials, mapping, clientConfiguration, pageResults); |
250 |
| - mapper.setFilterParams(params); |
251 |
| - mapper.setEndpoint(endpoint); |
252 |
| - mapper.setRegion(region); |
253 |
| - mapper.setRunningStateOnly(runningOnly); |
| 264 | + |
| 265 | + private EC2SupplierImpl createEc2Supplier() { |
| 266 | + return new EC2SupplierImpl( |
| 267 | + createCredentials(), |
| 268 | + clientConfiguration, |
| 269 | + // Use old default us-east-1 for AWS EC2, to maintain default behavior for existing configurations |
| 270 | + RegionUtils.getRegion(Regions.US_EAST_1.getName()) |
| 271 | + ); |
254 | 272 | }
|
255 | 273 |
|
256 | 274 | private AWSCredentials createAwsCredentials(AWSCredentialsProvider provider, String assumeRoleArn, String externalId) {
|
|
0 commit comments