001/**
002 * Copyright 2010-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.common.util.encrypt.provider;
017
018import static com.google.common.base.Optional.absent;
019import static com.google.common.collect.Lists.newArrayList;
020
021import java.util.List;
022
023import org.kuali.common.util.encrypt.EncryptionContext;
024
025import com.google.common.base.Optional;
026import com.google.common.collect.ImmutableList;
027
028public final class DefaultEncryptionContextProviderChain implements EncryptionContextProvider {
029
030        private static final String ENC_PASSWORD_KEY = "enc.password";
031        private static final String ENC_STRENGTH_KEY = "enc.strength";
032
033        // Need this key for backwards compatibility. "enc.password" contains a different password for some existing Maven processes
034        private static final String ENC_MAVEN_ALTERNATE_PASSWORD_KEY = "enc.pwd";
035
036        public DefaultEncryptionContextProviderChain() {
037                List<EncryptionContextProvider> providers = newArrayList();
038                providers.add(new SystemPropertiesEncryptionContextProvider(ENC_PASSWORD_KEY, ENC_STRENGTH_KEY));
039                providers.add(new EnvironmentVariableEncryptionContextProvider(toEnvKey(ENC_PASSWORD_KEY), toEnvKey(ENC_STRENGTH_KEY)));
040                providers.add(new FileEncryptionContextProvider());
041                providers.add(new MavenEncryptionContextProvider(ENC_MAVEN_ALTERNATE_PASSWORD_KEY, ENC_STRENGTH_KEY));
042                providers.add(new MavenEncryptionContextProvider(ENC_PASSWORD_KEY, ENC_STRENGTH_KEY));
043                this.providers = ImmutableList.copyOf(providers);
044        }
045
046        public DefaultEncryptionContextProviderChain(EncryptionContextProvider... providers) {
047                this(ImmutableList.copyOf(providers));
048        }
049
050        public DefaultEncryptionContextProviderChain(List<EncryptionContextProvider> providers) {
051                this.providers = ImmutableList.copyOf(providers);
052        }
053
054        private final List<EncryptionContextProvider> providers;
055
056        public List<EncryptionContextProvider> getProviders() {
057                return providers;
058        }
059
060        @Override
061        public Optional<EncryptionContext> getEncryptionContext() {
062                for (EncryptionContextProvider provider : providers) {
063                        Optional<EncryptionContext> context = provider.getEncryptionContext();
064                        if (context.isPresent()) {
065                                return context;
066                        }
067                }
068                return absent();
069        }
070
071        protected static String toEnvKey(String key) {
072                return key.replace('.', '_').toUpperCase();
073        }
074
075}