|
16 | 16 |
|
17 | 17 | package org.springframework.cloud.aws.autoconfigure.context;
|
18 | 18 |
|
19 |
| -import org.springframework.beans.factory.support.BeanDefinitionRegistry; |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +import com.amazonaws.auth.AWSCredentialsProvider; |
| 23 | +import com.amazonaws.auth.AWSCredentialsProviderChain; |
| 24 | +import com.amazonaws.auth.AWSStaticCredentialsProvider; |
| 25 | +import com.amazonaws.auth.BasicAWSCredentials; |
| 26 | +import com.amazonaws.auth.DefaultAWSCredentialsProviderChain; |
| 27 | +import com.amazonaws.auth.EC2ContainerCredentialsProviderWrapper; |
| 28 | +import com.amazonaws.auth.profile.ProfileCredentialsProvider; |
| 29 | + |
| 30 | +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
20 | 31 | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
21 |
| -import org.springframework.boot.context.properties.ConfigurationProperties; |
| 32 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
| 33 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
22 | 34 | import org.springframework.cloud.aws.autoconfigure.context.properties.AwsCredentialsProperties;
|
23 |
| -import org.springframework.cloud.aws.context.config.annotation.ContextDefaultConfigurationRegistrar; |
24 |
| -import org.springframework.cloud.aws.core.credentials.CredentialsProviderFactoryBean; |
25 |
| -import org.springframework.context.EnvironmentAware; |
26 | 35 | import org.springframework.context.annotation.Bean;
|
27 | 36 | import org.springframework.context.annotation.Configuration;
|
28 |
| -import org.springframework.context.annotation.Import; |
29 |
| -import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; |
30 |
| -import org.springframework.core.env.Environment; |
31 |
| -import org.springframework.core.type.AnnotationMetadata; |
32 | 37 | import org.springframework.util.StringUtils;
|
33 | 38 |
|
34 |
| -import static com.amazonaws.auth.profile.internal.AwsProfileNameLoader.DEFAULT_PROFILE_NAME; |
35 |
| -import static org.springframework.cloud.aws.context.config.support.ContextConfigurationUtils.registerCredentialsProvider; |
36 |
| -import static org.springframework.cloud.aws.context.config.support.ContextConfigurationUtils.registerDefaultAWSCredentialsProvider; |
| 39 | +import static org.springframework.cloud.aws.core.config.AmazonWebserviceClientConfigurationUtils.CREDENTIALS_PROVIDER_BEAN_NAME; |
37 | 40 |
|
38 | 41 | /**
|
| 42 | + * {@link EnableAutoConfiguration} for {@link AWSCredentialsProvider}. |
| 43 | + * |
39 | 44 | * @author Agim Emruli
|
| 45 | + * @author Maciej Walkowiak |
40 | 46 | */
|
41 | 47 | @Configuration(proxyBeanMethods = false)
|
42 |
| -@Import({ ContextDefaultConfigurationRegistrar.class, |
43 |
| - ContextCredentialsAutoConfiguration.Registrar.class }) |
44 |
| -@ConditionalOnClass(name = "com.amazonaws.auth.AWSCredentialsProvider") |
| 48 | +@EnableConfigurationProperties(AwsCredentialsProperties.class) |
| 49 | +@ConditionalOnClass(com.amazonaws.auth.AWSCredentialsProvider.class) |
45 | 50 | public class ContextCredentialsAutoConfiguration {
|
46 | 51 |
|
47 |
| - /** |
48 |
| - * The prefix used for AWS credentials related properties. |
49 |
| - */ |
50 |
| - public static final String AWS_CREDENTIALS_PROPERTY_PREFIX = "cloud.aws.credentials"; |
| 52 | + @Bean(name = CREDENTIALS_PROVIDER_BEAN_NAME) |
| 53 | + @ConditionalOnMissingBean(name = CREDENTIALS_PROVIDER_BEAN_NAME) |
| 54 | + public AWSCredentialsProvider awsCredentialsProvider( |
| 55 | + AwsCredentialsProperties properties) { |
51 | 56 |
|
52 |
| - /** |
53 |
| - * Bind AWS credentials related properties to a property instance. |
54 |
| - * @return An {@link AwsCredentialsProperties} instance |
55 |
| - */ |
56 |
| - @Bean |
57 |
| - @ConfigurationProperties(prefix = AWS_CREDENTIALS_PROPERTY_PREFIX) |
58 |
| - public AwsCredentialsProperties awsCredentialsProperties() { |
59 |
| - return new AwsCredentialsProperties(); |
60 |
| - } |
| 57 | + List<AWSCredentialsProvider> providers = resolveCredentialsProviders(properties); |
61 | 58 |
|
62 |
| - /** |
63 |
| - * Registrar for the credentials provider. |
64 |
| - */ |
65 |
| - public static class Registrar |
66 |
| - implements ImportBeanDefinitionRegistrar, EnvironmentAware { |
| 59 | + if (providers.isEmpty()) { |
| 60 | + return new DefaultAWSCredentialsProviderChain(); |
| 61 | + } |
| 62 | + else { |
| 63 | + return new AWSCredentialsProviderChain(providers); |
| 64 | + } |
| 65 | + } |
67 | 66 |
|
68 |
| - private Environment environment; |
| 67 | + private List<AWSCredentialsProvider> resolveCredentialsProviders( |
| 68 | + AwsCredentialsProperties properties) { |
| 69 | + List<AWSCredentialsProvider> providers = new ArrayList<>(); |
69 | 70 |
|
70 |
| - @Override |
71 |
| - public void setEnvironment(Environment environment) { |
72 |
| - this.environment = environment; |
| 71 | + if (StringUtils.hasText(properties.getAccessKey()) |
| 72 | + && StringUtils.hasText(properties.getSecretKey())) { |
| 73 | + providers.add(new AWSStaticCredentialsProvider(new BasicAWSCredentials( |
| 74 | + properties.getAccessKey(), properties.getSecretKey()))); |
73 | 75 | }
|
74 | 76 |
|
75 |
| - @Override |
76 |
| - public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, |
77 |
| - BeanDefinitionRegistry registry) { |
78 |
| - // Do not register a credentials provider if a bean with the same name is |
79 |
| - // already registered. |
80 |
| - if (registry.containsBeanDefinition( |
81 |
| - CredentialsProviderFactoryBean.CREDENTIALS_PROVIDER_BEAN_NAME)) { |
82 |
| - return; |
83 |
| - } |
| 77 | + if (properties.isInstanceProfile()) { |
| 78 | + providers.add(new EC2ContainerCredentialsProviderWrapper()); |
| 79 | + } |
84 | 80 |
|
85 |
| - Boolean useDefaultCredentialsChain = this.environment |
86 |
| - .getProperty( |
87 |
| - AWS_CREDENTIALS_PROPERTY_PREFIX |
88 |
| - + ".use-default-aws-credentials-chain", |
89 |
| - Boolean.class, false); |
90 |
| - String accessKey = this.environment |
91 |
| - .getProperty(AWS_CREDENTIALS_PROPERTY_PREFIX + ".access-key"); |
92 |
| - String secretKey = this.environment |
93 |
| - .getProperty(AWS_CREDENTIALS_PROPERTY_PREFIX + ".secret-key"); |
94 |
| - if (useDefaultCredentialsChain && (StringUtils.isEmpty(accessKey) |
95 |
| - || StringUtils.isEmpty(secretKey))) { |
96 |
| - registerDefaultAWSCredentialsProvider(registry); |
97 |
| - } |
98 |
| - else { |
99 |
| - registerCredentialsProvider(registry, accessKey, secretKey, |
100 |
| - this.environment.getProperty( |
101 |
| - AWS_CREDENTIALS_PROPERTY_PREFIX + ".instance-profile", |
102 |
| - Boolean.class, true) |
103 |
| - && !this.environment.containsProperty( |
104 |
| - AWS_CREDENTIALS_PROPERTY_PREFIX + ".access-key"), |
105 |
| - this.environment.getProperty( |
106 |
| - AWS_CREDENTIALS_PROPERTY_PREFIX + ".profile-name", |
107 |
| - DEFAULT_PROFILE_NAME), |
108 |
| - this.environment.getProperty( |
109 |
| - AWS_CREDENTIALS_PROPERTY_PREFIX + ".profile-path")); |
110 |
| - } |
| 81 | + if (properties.getProfileName() != null) { |
| 82 | + providers.add(properties.getProfilePath() != null |
| 83 | + ? new ProfileCredentialsProvider(properties.getProfilePath(), |
| 84 | + properties.getProfileName()) |
| 85 | + : new ProfileCredentialsProvider(properties.getProfileName())); |
111 | 86 | }
|
112 | 87 |
|
| 88 | + return providers; |
113 | 89 | }
|
114 | 90 |
|
115 | 91 | }
|
0 commit comments