Update FProxy to separate truststore and keystore
[aaf/cadi.git] / sidecar / fproxy / src / main / java / org / onap / aaf / cadi / sidecar / fproxy / FProxyApplication.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aaf
4  * ================================================================================
5  * Copyright © 2018 European Software Marketing Ltd.
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  *
11  *       http://www.apache.org/licenses/LICENSE-2.0
12  *
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 package org.onap.aaf.cadi.sidecar.fproxy;
21
22 import java.util.HashMap;
23 import java.util.Optional;
24 import javax.annotation.PostConstruct;
25 import org.eclipse.jetty.util.security.Password;
26 import org.springframework.beans.factory.annotation.Autowired;
27 import org.springframework.boot.autoconfigure.SpringBootApplication;
28 import org.springframework.boot.builder.SpringApplicationBuilder;
29 import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
30 import org.springframework.context.annotation.PropertySource;
31 import org.springframework.core.env.Environment;
32
33 @SpringBootApplication
34 @PropertySource("file:${CONFIG_HOME}/fproxy.properties")
35 public class FProxyApplication extends SpringBootServletInitializer {
36
37     @Autowired
38     private Environment env;
39
40     @FunctionalInterface
41     public interface AppProperty {
42         String getProperty(String p);
43     }
44
45     /**
46      * Spring Boot initialization.
47      *
48      * @param args main args
49      */
50     public static void main(String[] args) {
51         AppProperty appProp = (String propertyName) -> Optional.ofNullable(System.getProperty(propertyName))
52                 .orElseThrow(() -> new IllegalArgumentException("Env property " + propertyName + " not set"));
53
54         HashMap<String, Object> props = new HashMap<>();
55         props.put("server.ssl.key-store-password", Password.deobfuscate(appProp.getProperty("KEY_STORE_PASSWORD")));
56         props.put("server.ssl.trust-store-password", Password.deobfuscate(appProp.getProperty("TRUST_STORE_PASSWORD")));
57         new FProxyApplication().configure(new SpringApplicationBuilder(FProxyApplication.class).properties(props))
58                 .run(args);
59     }
60
61     /**
62      * Set required trust and key store system properties using values from application.properties
63      */
64     @PostConstruct
65     public void setSystemProperties() {
66         AppProperty appProp = (String propertyName) -> Optional.ofNullable(env.getProperty(propertyName))
67                 .orElseThrow(() -> new IllegalArgumentException("Env property " + propertyName + " not set"));
68
69         System.setProperty("javax.net.ssl.keyStore", appProp.getProperty("server.ssl.key-store"));
70         System.setProperty("javax.net.ssl.keyStorePassword", appProp.getProperty("server.ssl.key-store-password"));
71         System.setProperty("javax.net.ssl.trustStore", appProp.getProperty("server.ssl.trust-store"));
72         System.setProperty("javax.net.ssl.trustStorePassword", appProp.getProperty("server.ssl.trust-store-password"));
73     }
74
75 }