1 /*
2  * Copyright 2013-2019 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      https://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.cloud.aws.autoconfigure.context;
18
19 import org.springframework.beans.factory.support.BeanDefinitionRegistry;
20 import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
21 import org.springframework.boot.context.properties.ConfigurationProperties;
22 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 import org.springframework.context.annotation.Bean;
27 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 import org.springframework.util.StringUtils;
33
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;
37
38 /**
39  * @author Agim Emruli
40  */

41 @Configuration(proxyBeanMethods = false)
42 @Import({ ContextDefaultConfigurationRegistrar.class,
43         ContextCredentialsAutoConfiguration.Registrar.class })
44 @ConditionalOnClass(name = "com.amazonaws.auth.AWSCredentialsProvider")
45 public class ContextCredentialsAutoConfiguration {
46
47     /**
48      * The prefix used for AWS credentials related properties.
49      */

50     public static final String AWS_CREDENTIALS_PROPERTY_PREFIX = "cloud.aws.credentials";
51
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     }
61
62     /**
63      * Registrar for the credentials provider.
64      */

65     public static class Registrar
66             implements ImportBeanDefinitionRegistrar, EnvironmentAware {
67
68         private Environment environment;
69
70         @Override
71         public void setEnvironment(Environment environment) {
72             this.environment = environment;
73         }
74
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             }
84
85             Boolean useDefaultCredentialsChain = this.environment
86                     .getProperty(
87                             AWS_CREDENTIALS_PROPERTY_PREFIX
88                                     + ".use-default-aws-credentials-chain",
89                             Boolean.classfalse);
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.classtrue)
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             }
111         }
112
113     }
114
115 }
116