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;
23 import org.apache.commons.io.IOUtils;
24 import org.eclipse.jetty.util.security.Password;
25 import org.onap.aai.exceptions.AAIException;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 import java.io.FileInputStream;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.nio.charset.Charset;
34 import java.util.Properties;
35 import java.util.UUID;
36 import java.util.regex.Matcher;
37 import java.util.regex.Pattern;
39 public class AAIApplicationConfig {
41 private static final Logger LOGGER = LoggerFactory.getLogger(AAIApplicationConfig.class);
42 private static String GLOBAL_PROP_FILE_NAME = "application.properties";
43 private static final String SERVER_CERTS_LOCATION_PROP_NAME = "server.certs.location";
44 private static final String PASSPHRASSES_FILENAME = ".passphrases";
45 private static final String PASSWORD_FILENAME = ".password";
46 private static final String TRUSTSTORE_PASSWORD_PROP_NAME = "cadi_truststore_password";
47 private static final String SERVER_SSL_KEYSTORE_PROP_NAME = "server.ssl.key-store";
48 private static final String SERVER_SSL_KEYSTORE_PKCS12_PROP_NAME = "server.ssl.key-store.pkcs12";
49 private static final String SERVER_SSL_TRUSTSTORE_PROP_NAME = "server.ssl.trust-store";
50 private static Properties serverProps;
51 private static boolean propsInitialized = false;
52 private static String TRUSTSTORE_PASSWORD = null;
53 private static String KEYSTORE_PASSWORD = null;
54 private static final String PROPERTY_REGEX = "\\$\\{([^\\$\\{\\}]+)\\}";
57 * Instantiates a new AAI config.
60 private AAIApplicationConfig() {
66 * @throws AAIException the AAI exception
68 public synchronized static void init() {
69 /*LoggingContext.save();
70 LoggingContext.component("config");
71 LoggingContext.partnerName("NA");
72 LoggingContext.targetEntity("AAI");
73 LoggingContext.requestId(UUID.randomUUID().toString());
74 LoggingContext.serviceName("AAI");
75 LoggingContext.targetServiceName("init");
76 LoggingContext.statusCode(StatusCode.COMPLETE);*/
78 LOGGER.info("Initializing AAIApplicationConfig");
80 AAIApplicationConfig.reloadConfig();
82 //LoggingContext.restore();
88 public synchronized static void reloadConfig() {
90 Properties newServerProps = new Properties();
91 LOGGER.debug("Reloading config from " + GLOBAL_PROP_FILE_NAME);
94 InputStream is = AAIApplicationConfig.class.getClassLoader().getResourceAsStream(GLOBAL_PROP_FILE_NAME);
95 newServerProps.load(is);
96 propsInitialized = true;
97 serverProps = newServerProps;
98 TRUSTSTORE_PASSWORD = retrieveTruststorePassword();
99 KEYSTORE_PASSWORD = retrieveKeystorePassword();
100 } catch (Exception fnfe) {
101 final InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("application.properties");
102 LOGGER.info("Unable to find the application.properties from filesystem so using file in jar");
105 newServerProps.load(is);
106 serverProps = newServerProps;
107 TRUSTSTORE_PASSWORD = retrieveTruststorePassword();
108 KEYSTORE_PASSWORD = retrieveKeystorePassword();
109 } catch (IOException e) {
110 LOGGER.warn("Encountered IO Exception during loading of props from inputstream", e);
113 LOGGER.error("Expected to find the properties file in the jar but unable to find it");
122 * @param defaultValue the default value
125 public static String get(String key, String defaultValue) {
126 String result = defaultValue;
129 } catch (AAIException a) {
131 if (result == null || result.isEmpty()) {
132 result = defaultValue;
142 * @throws AAIException the AAI exception
144 public static String get(String key) throws AAIException {
145 String response = null;
147 if (!propsInitialized || (serverProps == null)) {
151 if ((key.endsWith("password") || key.endsWith("passwd") || key.endsWith("apisecret"))
152 && serverProps.containsKey(key + ".x")) {
153 String valx = serverProps.getProperty(key + ".x");
154 return Password.deobfuscate(valx);
157 if (!serverProps.containsKey(key)) {
158 throw new AAIException("AAI_4005", "Property key " + key + " cannot be found");
160 response = serverProps.getProperty(key);
161 if (response == null || response.isEmpty()) {
162 throw new AAIException("AAI_4005", "Property key " + key + " is null or empty");
164 response = replaceProperties(response);
170 * Gets the keystore path
173 * @throws AAIException the AAI exception
175 public static String getKeystore() throws AAIException {
176 return (get(SERVER_SSL_KEYSTORE_PROP_NAME));
179 * Gets the PKCS12 keystore path
182 * @throws AAIException the AAI exception
184 public static String getKeystorePkcs12() throws AAIException {
185 return (get(SERVER_SSL_KEYSTORE_PKCS12_PROP_NAME));
188 * Gets the keystore path
191 * @throws AAIException the AAI exception
193 public static String getTruststore() throws AAIException {
194 return (get(SERVER_SSL_TRUSTSTORE_PROP_NAME));
198 * Retrieve the keystore password
200 * @return the password
202 private static String retrieveKeystorePassword() {
203 String certPath = serverProps.getProperty(SERVER_CERTS_LOCATION_PROP_NAME);
204 if (certPath == null) {
208 certPath = replaceProperties(certPath);
210 catch (AAIException e) {
214 File passwordFile = null;
215 InputStream passwordStream = null;
216 String keystorePassword = null;
218 // Override the passwords from application.properties if we find AAF certman files
220 passwordFile = new File(certPath + PASSWORD_FILENAME);
221 passwordStream = new FileInputStream(passwordFile);
222 keystorePassword = IOUtils.toString(passwordStream, Charset.defaultCharset());
223 if (keystorePassword != null) {
224 keystorePassword = keystorePassword.trim();
227 } catch (IOException e) {
228 LOGGER.warn("Not using AAF Certman password file, e=" + e.getMessage());
229 } catch (NullPointerException n) {
230 LOGGER.warn("Not using AAF Certman passphrases file, e=" + n.getMessage());
232 if (passwordStream != null) {
234 passwordStream.close();
235 } catch (Exception e) {
239 return keystorePassword;
243 * Get the keystore password
245 * @return the password
247 public static String getKeystorePassword() {
248 return (KEYSTORE_PASSWORD);
252 * Gets the truststore password
254 * @return the password
256 private static String retrieveTruststorePassword() {
257 String certPath = serverProps.getProperty(SERVER_CERTS_LOCATION_PROP_NAME);
258 if (certPath == null) {
262 certPath = replaceProperties(certPath);
264 catch (AAIException e) {
267 File passphrasesFile = null;
268 InputStream passphrasesStream = null;
269 String truststorePassword = null;
271 passphrasesFile = new File(certPath + PASSPHRASSES_FILENAME);
272 passphrasesStream = new FileInputStream(passphrasesFile);
275 Properties passphrasesProps = new Properties();
276 passphrasesProps.load(passphrasesStream);
277 truststorePassword = passphrasesProps.getProperty(TRUSTSTORE_PASSWORD_PROP_NAME);
278 if (truststorePassword != null) {
279 truststorePassword = truststorePassword.trim();
282 } catch (IOException e) {
283 LOGGER.warn("Not using AAF Certman passphrases file, e=" + e.getMessage());
284 } catch (NullPointerException n) {
285 LOGGER.warn("Not using AAF Certman passphrases file, e=" + n.getMessage());
287 if (passphrasesStream != null) {
289 passphrasesStream.close();
290 } catch (Exception e) {
295 return truststorePassword;
299 * Get the trustore password
301 * @return the password
303 public static String getTruststorePassword() {
304 return (TRUSTSTORE_PASSWORD);
308 * Gets the int value for the key.
312 * @throws AAIException the AAI exception
314 public static int getInt(String key) throws AAIException {
315 return Integer.parseInt(AAIApplicationConfig.get(key));
324 public static int getInt(String key, String value) {
325 return Integer.parseInt(AAIApplicationConfig.get(key, value));
329 * Gets the server props.
331 * @return the server props
333 public static Properties getServerProps() {
338 * Check if a null or an Empty string is passed in.
343 public static boolean isEmpty(String s) {
344 return (s == null || s.length() == 0);
347 private static String replaceProperties(String originalValue) throws AAIException {
348 final Pattern p = Pattern.compile(PROPERTY_REGEX);
349 Matcher m = p.matcher(originalValue);
350 /*if (!m.matches()) {
351 return originalValue;
353 StringBuffer sb = new StringBuffer();
355 String text = m.group(1);
356 String replacement = get(text);
357 m.appendReplacement(sb, replacement);
360 return(sb.toString());