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=========================================================
20 package org.onap.aai.babel.config;
22 import org.apache.commons.io.IOUtils;
23 import org.eclipse.jetty.util.security.Password;
24 import org.onap.aai.babel.logging.LogHelper;
25 import org.springframework.context.ApplicationContextInitializer;
26 import org.springframework.context.ConfigurableApplicationContext;
27 import org.springframework.core.env.ConfigurableEnvironment;
28 import org.springframework.core.env.MapPropertySource;
29 import org.springframework.core.env.PropertySource;
32 import java.io.FileInputStream;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.util.LinkedHashMap;
37 import java.util.Properties;
39 public class PropertyPasswordConfiguration implements ApplicationContextInitializer<ConfigurableApplicationContext> {
42 public void initialize(ConfigurableApplicationContext applicationContext) {
44 Map<String, Object> sslProps = new LinkedHashMap<>();
45 ConfigurableEnvironment environment = applicationContext.getEnvironment();
46 String certPath = environment.getProperty("server.certs.location");
47 File passwordFile = null;
48 File passphrasesFile = null;
49 InputStream passwordStream = null;
50 InputStream passphrasesStream = null;
51 String keystorePassword = null;
52 String truststorePassword = null;
54 if (certPath != null) {
56 passwordFile = new File(certPath + ".password");
57 passwordStream = new FileInputStream(passwordFile);
59 if (passwordStream != null) {
60 keystorePassword = IOUtils.toString(passwordStream);
61 if (keystorePassword != null) {
62 keystorePassword = keystorePassword.trim();
64 sslProps.put("server.ssl.key-store-password", keystorePassword);
66 } catch (IOException e) {
68 if (passwordStream != null) {
70 passwordStream.close();
71 } catch (Exception e) {
76 passphrasesFile = new File(certPath + ".passphrases");
77 passphrasesStream = new FileInputStream(passphrasesFile);
79 if (passphrasesStream != 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();
86 sslProps.put("server.ssl.trust-store-password", truststorePassword);
89 } catch (IOException e) {
91 if (passphrasesStream != null) {
93 passphrasesStream.close();
94 } catch (Exception e) {
99 if (keystorePassword == null || keystorePassword.isEmpty()) {
100 keystorePassword = System.getProperty("KEY_STORE_PASSWORD");
101 if (keystorePassword != null && (!keystorePassword.isEmpty()) ) {
102 System.setProperty("server.ssl.key-store-password", new Password(keystorePassword).toString());
104 if (keystorePassword == null || keystorePassword.isEmpty()) {
105 throw new IllegalArgumentException("Mandatory property KEY_STORE_PASSWORD not set");
109 sslProps.put("server.ssl.key-store-password", keystorePassword);
111 if (truststorePassword == null || truststorePassword.isEmpty()) {
114 sslProps.put("server.ssl.trust-store-password", truststorePassword);
116 if (!sslProps.isEmpty()) {
117 PropertySource<?> additionalProperties = new MapPropertySource("additionalProperties", sslProps);
118 environment.getPropertySources().addFirst(additionalProperties);