View Javadoc
1   /**
2    * Copyright 2004-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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    * http://www.opensource.org/licenses/ecl2.php
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  package org.kuali.common.aws.auth;
17  
18  import java.util.List;
19  
20  import org.kuali.common.aws.model.ImmutableAWSCredentials;
21  import org.kuali.common.util.Assert;
22  import org.kuali.common.util.spring.SpringUtils;
23  import org.kuali.common.util.spring.env.EnvironmentService;
24  
25  import com.amazonaws.auth.AWSCredentials;
26  import com.amazonaws.auth.AWSCredentialsProvider;
27  import com.google.common.collect.ImmutableList;
28  
29  public final class EnvCredentialsProvider implements AWSCredentialsProvider {
30  
31  	private final EnvironmentService env;
32  	private final List<String> accessKeyProperties;
33  	private final List<String> secretKeyProperties;
34  
35  	public static class Builder {
36  
37  		// Required
38  		private final EnvironmentService env;
39  
40  		// Optional (default values are usually good enough)
41  		private List<String> accessKeyProperties = ImmutableList.of("aws.accessKey", "aws.accessKeyId");
42  		private List<String> secretKeyProperties = ImmutableList.of("aws.secretKey", "aws.secretAccessKey");
43  
44  		// Allow them to override which system properties / environment variables get examined
45  		private static final String ACCESS_KEY_PROPERTIES = "aws.accessKeyProperties";
46  		private static final String SECRET_KEY_PROPERTIES = "aws.secretKeyProperties";
47  
48  		public Builder(EnvironmentService env) {
49  			this.env = env;
50  		}
51  
52  		public Builder accessKeyProperties(List<String> accessKeyProperties) {
53  			this.accessKeyProperties = accessKeyProperties;
54  			return this;
55  		}
56  
57  		public Builder secretKeyProperties(List<String> secretKeyProperties) {
58  			this.secretKeyProperties = secretKeyProperties;
59  			return this;
60  		}
61  
62  		private void validate(EnvCredentialsProvider provider) {
63  			Assert.noNulls(provider.getEnv(), provider.getAccessKeyProperties(), provider.getSecretKeyProperties());
64  			Assert.isTrue(provider.getAccessKeyProperties().size() > 0, "Must provide at least one property to examine for AWS Access Key ID");
65  			Assert.isTrue(provider.getSecretKeyProperties().size() > 0, "Must provide at least one property to examine for AWS Secret Key");
66  		}
67  
68  		private void override() {
69  			accessKeyProperties(SpringUtils.getStrings(env, ACCESS_KEY_PROPERTIES, accessKeyProperties));
70  			secretKeyProperties(SpringUtils.getStrings(env, SECRET_KEY_PROPERTIES, secretKeyProperties));
71  		}
72  
73  		private void finish() {
74  			override();
75  			this.accessKeyProperties = ImmutableList.copyOf(accessKeyProperties);
76  			this.secretKeyProperties = ImmutableList.copyOf(secretKeyProperties);
77  		}
78  
79  		public EnvCredentialsProvider build() {
80  			finish();
81  			EnvCredentialsProvider provider = new EnvCredentialsProvider(this);
82  			validate(provider);
83  			return provider;
84  		}
85  
86  	}
87  
88  	private EnvCredentialsProvider(Builder builder) {
89  		this.env = builder.env;
90  		this.accessKeyProperties = builder.accessKeyProperties;
91  		this.secretKeyProperties = builder.secretKeyProperties;
92  	}
93  
94  	@Override
95  	public AWSCredentials getCredentials() {
96  		String accessKey = SpringUtils.getString(env, accessKeyProperties);
97  		String secretKey = SpringUtils.getString(env, accessKeyProperties);
98  		return new ImmutableAWSCredentials(accessKey, secretKey);
99  	}
100 
101 	@Override
102 	public void refresh() {
103 		// noop
104 	}
105 
106 	public EnvironmentService getEnv() {
107 		return env;
108 	}
109 
110 	public List<String> getAccessKeyProperties() {
111 		return accessKeyProperties;
112 	}
113 
114 	public List<String> getSecretKeyProperties() {
115 		return secretKeyProperties;
116 	}
117 
118 }