2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.util;
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.Properties;
29 import java.util.regex.Matcher;
30 import java.util.regex.Pattern;
32 import org.apache.commons.io.IOUtils;
33 import org.eclipse.jetty.util.security.Password;
34 import org.onap.aai.exceptions.AAIException;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 public class AAIApplicationConfig {
40 private static final Logger LOGGER = LoggerFactory.getLogger(AAIApplicationConfig.class);
41 private static String GLOBAL_PROP_FILE_NAME = "application.properties";
42 private static final String SERVER_CERTS_LOCATION_PROP_NAME = "server.certs.location";
43 private static final String PASSPHRASSES_FILENAME = ".passphrases";
44 private static final String PASSWORD_FILENAME = ".password";
45 private static final String TRUSTSTORE_PASSWORD_PROP_NAME = "cadi_truststore_password";
46 public static final String SERVER_SSL_KEYSTORE_PROP_NAME = "server.ssl.key-store";
47 public static final String SERVER_SSL_KEYSTORE_PKCS12_PROP_NAME = "server.ssl.key-store.pkcs12";
48 public static final String SERVER_SSL_TRUSTSTORE_PROP_NAME = "server.ssl.trust-store";
49 public static final String TRUSTSTORE_PASSWORD_NAME = "server.ssl.trust-store-password";
50 public static final String KEYSTORE_PASSWORD_NAME = "server.ssl.key-store-password";
51 private static Properties serverProps;
52 private static boolean propsInitialized = false;
53 private static String TRUSTSTORE_PASSWORD = null;
54 private static String KEYSTORE_PASSWORD = null;
55 private static final String PROPERTY_REGEX = "\\$\\{([^\\$\\{\\}]+)\\}";
58 * Instantiates a new AAI config.
61 private AAIApplicationConfig() {
67 * @throws AAIException the AAI exception
69 public synchronized static void init() {
71 LOGGER.info("Initializing AAIApplicationConfig");
72 AAIApplicationConfig.reloadConfig();
78 public synchronized static void reloadConfig() {
80 Properties newServerProps = new Properties();
81 LOGGER.debug("Reloading config from " + GLOBAL_PROP_FILE_NAME);
84 InputStream is = AAIApplicationConfig.class.getClassLoader().getResourceAsStream(GLOBAL_PROP_FILE_NAME);
85 newServerProps.load(is);
86 propsInitialized = true;
87 serverProps = newServerProps;
88 TRUSTSTORE_PASSWORD = retrieveTruststorePassword();
89 KEYSTORE_PASSWORD = retrieveKeystorePassword();
90 } catch (Exception fnfe) {
91 final InputStream is =
92 Thread.currentThread().getContextClassLoader().getResourceAsStream("application.properties");
93 LOGGER.info("Unable to find the application.properties from filesystem so using file in jar");
96 newServerProps.load(is);
97 serverProps = newServerProps;
98 TRUSTSTORE_PASSWORD = retrieveTruststorePassword();
99 KEYSTORE_PASSWORD = retrieveKeystorePassword();
100 } catch (IOException e) {
101 LOGGER.warn("Encountered IO Exception during loading of props from inputstream", e);
104 LOGGER.error("Expected to find the properties file in the jar but unable to find it");
113 * @param defaultValue the default value
116 public static String get(String key, String defaultValue) {
117 String result = defaultValue;
120 } catch (AAIException a) {
122 if (result == null || result.isEmpty()) {
123 result = defaultValue;
133 * @throws AAIException the AAI exception
135 public static String get(String key) throws AAIException {
136 String response = null;
138 if (!propsInitialized || (serverProps == null)) {
142 if ((key.endsWith("password") || key.endsWith("passwd") || key.endsWith("apisecret"))
143 && serverProps.containsKey(key + ".x")) {
144 String valx = serverProps.getProperty(key + ".x");
145 return Password.deobfuscate(valx);
148 if (!serverProps.containsKey(key)) {
149 throw new AAIException("AAI_4005", "Property key " + key + " cannot be found");
151 response = serverProps.getProperty(key);
152 if (response == null || response.isEmpty()) {
153 throw new AAIException("AAI_4005", "Property key " + key + " is null or empty");
155 response = replaceProperties(response);
161 * Gets the keystore path
164 * @throws AAIException the AAI exception
166 public static String getKeystore() throws AAIException {
167 return (get(SERVER_SSL_KEYSTORE_PROP_NAME));
171 * Gets the PKCS12 keystore path
174 * @throws AAIException the AAI exception
176 public static String getKeystorePkcs12() throws AAIException {
177 return (get(SERVER_SSL_KEYSTORE_PKCS12_PROP_NAME));
181 * Gets the keystore path
184 * @throws AAIException the AAI exception
186 public static String getTruststore() throws AAIException {
187 return (get(SERVER_SSL_TRUSTSTORE_PROP_NAME));
191 * Retrieve the keystore password
193 * @return the password
195 private static String retrieveKeystorePassword() {
196 String certPath = serverProps.getProperty(SERVER_CERTS_LOCATION_PROP_NAME);
197 if (certPath == null) {
201 certPath = replaceProperties(certPath);
202 } catch (AAIException e) {
205 return (retrieveKeystorePasswordWithCertPath(certPath));
209 * Retrieve the keystore password
211 * @return the password
213 private static String retrieveKeystorePasswordWithCertPath(String certPath) {
215 File passwordFile = null;
216 InputStream passwordStream = null;
217 String keystorePassword = null;
219 // Override the passwords from application.properties if we find AAF certman files
221 passwordFile = new File(certPath + PASSWORD_FILENAME);
222 passwordStream = new FileInputStream(passwordFile);
223 keystorePassword = IOUtils.toString(passwordStream, Charset.defaultCharset());
224 if (keystorePassword != null) {
225 keystorePassword = keystorePassword.trim();
228 } catch (IOException e) {
229 LOGGER.warn("Not using AAF Certman password file, e=" + e.getMessage());
230 } catch (NullPointerException n) {
231 LOGGER.warn("Not using AAF Certman passphrases file, e=" + n.getMessage());
233 if (passwordStream != null) {
235 passwordStream.close();
236 } catch (Exception e) {
240 return keystorePassword;
244 * Get the keystore password
246 * @return the password
248 public static String getKeystorePassword() {
249 return (KEYSTORE_PASSWORD);
253 * Gets the truststore password
255 * @return the password
257 private static String retrieveTruststorePasswordWithCertPath(String certPath) {
259 File passphrasesFile = null;
260 InputStream passphrasesStream = null;
261 String truststorePassword = null;
263 passphrasesFile = new File(certPath + PASSPHRASSES_FILENAME);
264 passphrasesStream = new FileInputStream(passphrasesFile);
266 Properties passphrasesProps = new Properties();
267 passphrasesProps.load(passphrasesStream);
268 truststorePassword = passphrasesProps.getProperty(TRUSTSTORE_PASSWORD_PROP_NAME);
269 if (truststorePassword != null) {
270 truststorePassword = truststorePassword.trim();
273 } catch (IOException e) {
274 LOGGER.warn("Not using AAF Certman passphrases file, e=" + e.getMessage());
275 } catch (NullPointerException n) {
276 LOGGER.warn("Not using AAF Certman passphrases file, e=" + n.getMessage());
278 if (passphrasesStream != null) {
280 passphrasesStream.close();
281 } catch (Exception e) {
286 return truststorePassword;
290 * Gets the truststore password
292 * @return the password
294 private static String retrieveTruststorePassword() {
295 String certPath = serverProps.getProperty(SERVER_CERTS_LOCATION_PROP_NAME);
296 if (certPath == null) {
300 certPath = replaceProperties(certPath);
301 } catch (AAIException e) {
304 return (retrieveTruststorePasswordWithCertPath(certPath));
308 * Get the trustore password
310 * @return the password
312 public static String getTruststorePassword() {
313 return (TRUSTSTORE_PASSWORD);
317 * Gets the int value for the key.
321 * @throws AAIException the AAI exception
323 public static int getInt(String key) throws AAIException {
324 return Integer.parseInt(AAIApplicationConfig.get(key));
333 public static int getInt(String key, String value) {
334 return Integer.parseInt(AAIApplicationConfig.get(key, value));
338 * Gets the server props.
340 * @return the server props
342 public static Properties getServerProps() {
347 * Check if a null or an Empty string is passed in.
352 public static boolean isEmpty(String s) {
353 return (s == null || s.length() == 0);
356 private static String replaceProperties(String originalValue) throws AAIException {
357 final Pattern p = Pattern.compile(PROPERTY_REGEX);
358 Matcher m = p.matcher(originalValue);
360 * if (!m.matches()) {
361 * return originalValue;
364 StringBuffer sb = new StringBuffer();
366 String text = m.group(1);
367 String replacement = get(text);
368 m.appendReplacement(sb, replacement);
371 return (sb.toString());
374 public static Properties retrieveKeystoreProps() throws AAIException {
376 Properties props = new Properties();
377 String truststorePath = System.getProperty(SERVER_SSL_TRUSTSTORE_PROP_NAME);
378 String truststorePassword = System.getProperty(TRUSTSTORE_PASSWORD_NAME);
379 String keystorePath = System.getProperty(SERVER_SSL_KEYSTORE_PKCS12_PROP_NAME);
380 String keystorePassword = System.getProperty(KEYSTORE_PASSWORD_NAME);
381 String certLocation = System.getProperty(SERVER_CERTS_LOCATION_PROP_NAME);
383 if (truststorePath == null || truststorePath.isEmpty()) {
384 truststorePath = AAIApplicationConfig.getTruststore();
386 if (truststorePath != null) {
387 props.setProperty(SERVER_SSL_TRUSTSTORE_PROP_NAME, truststorePath);
389 if (truststorePassword == null || truststorePassword.isEmpty()) {
390 if (certLocation != null && (!certLocation.isEmpty())) {
391 truststorePassword = AAIApplicationConfig.retrieveTruststorePasswordWithCertPath(certLocation);
393 truststorePassword = AAIApplicationConfig.getTruststorePassword();
397 if (truststorePassword != null) {
398 props.setProperty(TRUSTSTORE_PASSWORD_NAME, truststorePassword);
400 if (keystorePath == null || keystorePath.isEmpty()) {
401 keystorePath = AAIApplicationConfig.getKeystorePkcs12();
403 if (keystorePath != null) {
404 props.setProperty(SERVER_SSL_KEYSTORE_PKCS12_PROP_NAME, keystorePath);
406 if (keystorePassword == null || keystorePassword.isEmpty()) {
407 if (certLocation != null && (!certLocation.isEmpty())) {
408 keystorePassword = AAIApplicationConfig.retrieveKeystorePasswordWithCertPath(certLocation);
410 keystorePassword = AAIApplicationConfig.getKeystorePassword();
413 if (keystorePassword != null) {
414 props.setProperty(KEYSTORE_PASSWORD_NAME, keystorePassword);