bd8ce6ee576c63225d8acf27c244ad8fe3de1571
[aai/schema-service.git] /
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * <p>
11  * http://www.apache.org/licenses/LICENSE-2.0
12  * <p>
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.aai.schemaservice.config;
22
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.nio.charset.Charset;
28 import java.util.LinkedHashMap;
29 import java.util.Map;
30 import java.util.Properties;
31 import java.util.regex.Matcher;
32 import java.util.regex.Pattern;
33
34 import org.apache.commons.io.IOUtils;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.context.ApplicationContextInitializer;
38 import org.springframework.context.ConfigurableApplicationContext;
39 import org.springframework.core.env.ConfigurableEnvironment;
40 import org.springframework.core.env.EnumerablePropertySource;
41 import org.springframework.core.env.MapPropertySource;
42 import org.springframework.core.env.PropertySource;
43
44 public class PropertyPasswordConfiguration
45     implements ApplicationContextInitializer<ConfigurableApplicationContext> {
46
47     private static final Pattern decodePasswordPattern = Pattern.compile("password\\((.*?)\\)");
48     private PasswordDecoder passwordDecoder = new JettyPasswordDecoder();
49     private static final Logger logger =
50         LoggerFactory.getLogger(PropertyPasswordConfiguration.class.getName());
51
52     @Override
53     public void initialize(ConfigurableApplicationContext applicationContext) {
54
55         ConfigurableEnvironment environment = applicationContext.getEnvironment();
56         String certPath = environment.getProperty("server.certs.location");
57         Map<String, Object> sslProps = new LinkedHashMap<>();
58
59         // Override the passwords from application.properties if we find AAF certman files
60         if (certPath != null) {
61             File passwordFile = new File(certPath + ".password");
62             try (InputStream passwordStream = new FileInputStream(passwordFile)) {
63                 String keystorePassword = null;
64
65                 keystorePassword = IOUtils.toString(passwordStream, Charset.defaultCharset());
66                 if (keystorePassword != null) {
67                     keystorePassword = keystorePassword.trim();
68                     sslProps.put("server.ssl.key-store-password", keystorePassword);
69                 } else {
70                     logger.warn("Keystore password is null in AAF Certman password file");
71                 }
72             } catch (IOException e) {
73                 logger.warn("Not using AAF Certman password file " + passwordFile.getName() + " e="
74                     + e.getMessage());
75             }
76
77             File passphrasesFile = new File(certPath + ".passphrases");
78             try (InputStream passphrasesStream = new FileInputStream(passphrasesFile)) {
79                 String truststorePassword = null;
80                 Properties passphrasesProps = new Properties();
81                 passphrasesProps.load(passphrasesStream);
82                 truststorePassword = passphrasesProps.getProperty("cadi_truststore_password");
83                 if (truststorePassword != null) {
84                     truststorePassword = truststorePassword.trim();
85                     sslProps.put("server.ssl.trust-store-password", truststorePassword);
86                 } else {
87                     logger.warn("Truststore password is null in AAF Certman passphrases file");
88                 }
89             } catch (IOException e) {
90                 logger.warn("Not using AAF Certman passphrases file " + passphrasesFile.getName()
91                     + " e=" + e.getMessage());
92             }
93         }
94         for (PropertySource<?> propertySource : environment.getPropertySources()) {
95             Map<String, Object> propertyOverrides = new LinkedHashMap<>();
96             decodePasswords(propertySource, propertyOverrides);
97             if (!propertyOverrides.isEmpty()) {
98                 PropertySource<?> decodedProperties =
99                     new MapPropertySource("decoded " + propertySource.getName(), propertyOverrides);
100                 environment.getPropertySources().addBefore(propertySource.getName(),
101                     decodedProperties);
102             }
103
104         }
105         if (!sslProps.isEmpty()) {
106             logger.info("Using AAF Certman files");
107             PropertySource<?> additionalProperties =
108                 new MapPropertySource("additionalProperties", sslProps);
109             environment.getPropertySources().addFirst(additionalProperties);
110         }
111
112     }
113
114     private void decodePasswords(PropertySource<?> source, Map<String, Object> propertyOverrides) {
115         if (source instanceof EnumerablePropertySource) {
116             EnumerablePropertySource<?> enumerablePropertySource =
117                 (EnumerablePropertySource<?>) source;
118             for (String key : enumerablePropertySource.getPropertyNames()) {
119                 Object rawValue = source.getProperty(key);
120                 if (rawValue instanceof String) {
121                     String decodedValue = decodePasswordsInString((String) rawValue);
122                     propertyOverrides.put(key, decodedValue);
123                 }
124             }
125         }
126     }
127
128     private String decodePasswordsInString(String input) {
129         if (input == null) {
130             return null;
131         }
132         StringBuffer output = new StringBuffer();
133         Matcher matcher = decodePasswordPattern.matcher(input);
134         while (matcher.find()) {
135             String replacement = passwordDecoder.decode(matcher.group(1));
136             matcher.appendReplacement(output, replacement);
137         }
138         matcher.appendTail(output);
139         return output.toString();
140     }
141
142 }