1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
38 private final EnvironmentService env;
39
40
41 private List<String> accessKeyProperties = ImmutableList.of("aws.accessKey", "aws.accessKeyId");
42 private List<String> secretKeyProperties = ImmutableList.of("aws.secretKey", "aws.secretAccessKey");
43
44
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
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 }