1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.util.encrypt.provider;
17
18 import static com.google.common.base.Optional.absent;
19 import static com.google.common.collect.Lists.newArrayList;
20
21 import java.util.List;
22
23 import org.kuali.common.util.encrypt.EncryptionContext;
24
25 import com.google.common.base.Optional;
26 import com.google.common.collect.ImmutableList;
27
28 public final class DefaultEncryptionContextProviderChain implements EncryptionContextProvider {
29
30 private static final String ENC_PASSWORD_KEY = "enc.password";
31 private static final String ENC_STRENGTH_KEY = "enc.strength";
32
33
34 private static final String ENC_MAVEN_ALTERNATE_PASSWORD_KEY = "enc.pwd";
35
36 public DefaultEncryptionContextProviderChain() {
37 List<EncryptionContextProvider> providers = newArrayList();
38 providers.add(new SystemPropertiesEncryptionContextProvider(ENC_PASSWORD_KEY, ENC_STRENGTH_KEY));
39 providers.add(new EnvironmentVariableEncryptionContextProvider(toEnvKey(ENC_PASSWORD_KEY), toEnvKey(ENC_STRENGTH_KEY)));
40 providers.add(new FileEncryptionContextProvider());
41 providers.add(new MavenEncryptionContextProvider(ENC_MAVEN_ALTERNATE_PASSWORD_KEY, ENC_STRENGTH_KEY));
42 providers.add(new MavenEncryptionContextProvider(ENC_PASSWORD_KEY, ENC_STRENGTH_KEY));
43 this.providers = ImmutableList.copyOf(providers);
44 }
45
46 public DefaultEncryptionContextProviderChain(EncryptionContextProvider... providers) {
47 this(ImmutableList.copyOf(providers));
48 }
49
50 public DefaultEncryptionContextProviderChain(List<EncryptionContextProvider> providers) {
51 this.providers = ImmutableList.copyOf(providers);
52 }
53
54 private final List<EncryptionContextProvider> providers;
55
56 public List<EncryptionContextProvider> getProviders() {
57 return providers;
58 }
59
60 @Override
61 public Optional<EncryptionContext> getEncryptionContext() {
62 for (EncryptionContextProvider provider : providers) {
63 Optional<EncryptionContext> context = provider.getEncryptionContext();
64 if (context.isPresent()) {
65 return context;
66 }
67 }
68 return absent();
69 }
70
71 protected static String toEnvKey(String key) {
72 return key.replace('.', '_').toUpperCase();
73 }
74
75 }