View Javadoc
1   /**
2    * Copyright 2010-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.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  	// Need this key for backwards compatibility. "enc.password" contains a different password for some existing Maven processes
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  }