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 java.nio.charset.StandardCharsets;
23 import org.eclipse.jetty.util.security.Password;
24 import org.springframework.context.ApplicationContextInitializer;
25 import org.springframework.context.ConfigurableApplicationContext;
26 import org.springframework.core.env.ConfigurableEnvironment;
27 import org.springframework.core.env.MapPropertySource;
28 import org.springframework.core.env.PropertySource;
30 import java.io.FileInputStream;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.util.LinkedHashMap;
35 import java.util.Properties;
37 public class PropertyPasswordConfiguration implements ApplicationContextInitializer<ConfigurableApplicationContext> {
39 private static final String PROP_KEY_STORE_PASS = "server.ssl.key-store-password";
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 String keystorePassword = null;
48 String truststorePassword = null;
50 if (certPath != null) {
51 try (InputStream passwordStream = new FileInputStream(certPath + ".password")) {
52 keystorePassword = new String(passwordStream.readAllBytes(), StandardCharsets.UTF_8);
53 keystorePassword = keystorePassword.trim();
54 sslProps.put(PROP_KEY_STORE_PASS, keystorePassword);
55 } catch (IOException e) {
56 keystorePassword = null;
58 try (InputStream passphrasesStream = new FileInputStream(certPath + ".passphrases");) {
59 Properties passphrasesProps = new Properties();
60 passphrasesProps.load(passphrasesStream);
61 truststorePassword = passphrasesProps.getProperty("cadi_truststore_password");
62 if (truststorePassword != null) {
63 truststorePassword = truststorePassword.trim();
65 sslProps.put("server.ssl.trust-store-password", truststorePassword);
66 } catch (IOException e) {
67 truststorePassword = null;
70 if (keystorePassword == null || keystorePassword.isEmpty()) {
71 keystorePassword = System.getProperty("KEY_STORE_PASSWORD");
72 if (keystorePassword != null && (!keystorePassword.isEmpty()) ) {
73 System.setProperty(PROP_KEY_STORE_PASS, new Password(keystorePassword).toString());
75 if (keystorePassword == null || keystorePassword.isEmpty()) {
76 throw new IllegalArgumentException("Mandatory property KEY_STORE_PASSWORD not set");
80 sslProps.put(PROP_KEY_STORE_PASS, keystorePassword);
82 if (truststorePassword != null && !truststorePassword.isEmpty()) {
83 sslProps.put("server.ssl.trust-store-password", truststorePassword);
85 if (!sslProps.isEmpty()) {
86 PropertySource<?> additionalProperties = new MapPropertySource("additionalProperties", sslProps);
87 environment.getPropertySources().addFirst(additionalProperties);