|
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;
|
@@ -91,8 +97,7 @@ public class EC2ResourceModelSource implements ResourceModelSource {
|
91 | 97 | final String externalId;
|
92 | 98 | int pageResults;
|
93 | 99 |
|
94 |
| - AWSCredentials credentials; |
95 |
| - ClientConfiguration clientConfiguration = new ClientConfiguration();; |
| 100 | + ClientConfiguration clientConfiguration = new ClientConfiguration(); |
96 | 101 |
|
97 | 102 | INodeSet iNodeSet;
|
98 | 103 | static final Properties defaultMapping = new Properties();
|
@@ -153,6 +158,7 @@ public class EC2ResourceModelSource implements ResourceModelSource {
|
153 | 158 | }
|
154 | 159 |
|
155 | 160 | public EC2ResourceModelSource(final Properties configuration, final Services services) {
|
| 161 | + this.services = services; |
156 | 162 | this.accessKey = configuration.getProperty(EC2ResourceModelSourceFactory.ACCESS_KEY);
|
157 | 163 | this.secretKey = configuration.getProperty(EC2ResourceModelSourceFactory.SECRET_KEY);
|
158 | 164 | this.region = configuration.getProperty(EC2ResourceModelSourceFactory.REGION);
|
@@ -202,53 +208,64 @@ public EC2ResourceModelSource(final Properties configuration, final Services ser
|
202 | 208 | EC2ResourceModelSourceFactory.RUNNING_ONLY));
|
203 | 209 | logger.info("[debug] runningOnly:" + runningOnly);
|
204 | 210 | }
|
205 |
| - if (null != accessKey && null != secretKeyStoragePath) { |
206 | 211 |
|
207 |
| - KeyStorageTree keyStorage = services.getService(KeyStorageTree.class); |
208 |
| - String secretKey = getPasswordFromKeyStorage(secretKeyStoragePath, keyStorage); |
209 | 212 |
|
210 |
| - credentials = new BasicAWSCredentials(accessKey.trim(), secretKey.trim()); |
211 |
| - }else if (null != accessKey && null != secretKey) { |
212 |
| - credentials = new BasicAWSCredentials(accessKey.trim(), secretKey.trim()); |
213 |
| - } |
214 | 213 | if (null != httpProxyHost && !"".equals(httpProxyHost)) {
|
215 |
| - clientConfiguration.setProxyHost(httpProxyHost); |
216 |
| - clientConfiguration.setProxyPort(httpProxyPort); |
217 |
| - clientConfiguration.setProxyUsername(httpProxyUser); |
218 |
| - clientConfiguration.setProxyPassword(httpProxyPass); |
| 214 | + this.clientConfiguration.setProxyHost(httpProxyHost); |
| 215 | + this.clientConfiguration.setProxyPort(httpProxyPort); |
| 216 | + this.clientConfiguration.setProxyUsername(httpProxyUser); |
| 217 | + this.clientConfiguration.setProxyPassword(httpProxyPass); |
219 | 218 | }
|
220 |
| - queryAsync = !("true".equals(configuration.getProperty(SYNCHRONOUS_LOAD)) || refreshInterval <= 0); |
221 | 219 |
|
222 |
| - initialize(); |
223 |
| - } |
| 220 | + queryAsync = !("true".equals(configuration.getProperty(SYNCHRONOUS_LOAD)) || refreshInterval <= 0); |
224 | 221 |
|
225 |
| - private void initialize() { |
226 | 222 | final ArrayList<String> params = new ArrayList<String>();
|
227 | 223 | if (null != filterParams) {
|
228 | 224 | Collections.addAll(params, filterParams.split(";"));
|
229 | 225 | }
|
230 | 226 | loadMapping();
|
231 | 227 |
|
232 |
| - if (this.credentials == null) { |
233 |
| - if(this.externalId != null && this.assumeRoleArnCombinedWithExtId != null){ |
234 |
| - this.credentials = createAwsCredentials(null, this.assumeRoleArnCombinedWithExtId, this.externalId); |
235 |
| - } |
| 228 | + mapper = new InstanceToNodeMapper(createEc2Supplier(), mapping, pageResults); |
| 229 | + mapper.setFilterParams(params); |
| 230 | + mapper.setEndpoint(endpoint); |
| 231 | + mapper.setRegion(region); |
| 232 | + mapper.setRunningStateOnly(runningOnly); |
| 233 | + } |
236 | 234 |
|
237 |
| - if(assumeRoleArn != null) { |
238 |
| - AWSCredentialsProvider provider = null; |
239 |
| - if(this.credentials != null){ |
240 |
| - provider = new AWSStaticCredentialsProvider(credentials); |
241 |
| - } |
242 | 235 |
|
243 |
| - credentials = createAwsCredentials(provider, assumeRoleArn, null); |
| 236 | + protected AWSCredentials createCredentials() { |
| 237 | + if (null != accessKey && null != secretKeyStoragePath) { |
| 238 | + KeyStorageTree keyStorage = services.getService(KeyStorageTree.class); |
| 239 | + String secretKey = getPasswordFromKeyStorage(secretKeyStoragePath, keyStorage); |
| 240 | + return new BasicAWSCredentials(accessKey.trim(), secretKey.trim()); |
| 241 | + } else if (null != accessKey && null != secretKey) { |
| 242 | + return new BasicAWSCredentials(accessKey.trim(), secretKey.trim()); |
| 243 | + } |
| 244 | + |
| 245 | + AWSCredentials credentials = null; |
| 246 | + if (this.externalId != null && this.assumeRoleArnCombinedWithExtId != null) { |
| 247 | + credentials = createAwsCredentials(null, this.assumeRoleArnCombinedWithExtId, this.externalId); |
| 248 | + } |
| 249 | + |
| 250 | + if (assumeRoleArn != null) { |
| 251 | + AWSCredentialsProvider provider = null; |
| 252 | + if (credentials != null) { |
| 253 | + provider = new AWSStaticCredentialsProvider(credentials); |
244 | 254 | }
|
| 255 | + |
| 256 | + return createAwsCredentials(provider, assumeRoleArn, null); |
245 | 257 | }
|
| 258 | + return credentials; |
| 259 | + } |
246 | 260 |
|
247 |
| - mapper = new InstanceToNodeMapper(this.credentials, mapping, clientConfiguration, pageResults); |
248 |
| - mapper.setFilterParams(params); |
249 |
| - mapper.setEndpoint(endpoint); |
250 |
| - mapper.setRegion(region); |
251 |
| - mapper.setRunningStateOnly(runningOnly); |
| 261 | + |
| 262 | + private EC2SupplierImpl createEc2Supplier() { |
| 263 | + return new EC2SupplierImpl( |
| 264 | + createCredentials(), |
| 265 | + clientConfiguration, |
| 266 | + // Use old default us-east-1 for AWS EC2, to maintain default behavior for existing configurations |
| 267 | + RegionUtils.getRegion(Regions.US_EAST_1.getName()) |
| 268 | + ); |
252 | 269 | }
|
253 | 270 |
|
254 | 271 | private AWSCredentials createAwsCredentials(AWSCredentialsProvider provider, String assumeRoleArn, String externalId) {
|
|
0 commit comments