16af4469509af5c8f4b14ea8f2df7c2e035c784b
[clamp.git] / src / main / java / org / onap / clamp / clds / config / SslConfig.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.clds.config;
25
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.security.KeyStore;
29 import java.security.KeyStoreException;
30 import java.security.NoSuchAlgorithmException;
31 import java.security.cert.CertificateException;
32
33 import org.onap.clamp.clds.util.ResourceFileUtils;
34 import org.onap.clamp.util.PassDecoder;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.boot.autoconfigure.web.ServerProperties;
37 import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
38 import org.springframework.boot.web.server.Ssl;
39 import org.springframework.boot.web.server.SslStoreProvider;
40 import org.springframework.boot.web.server.WebServerFactoryCustomizer;
41 import org.springframework.context.annotation.Bean;
42 import org.springframework.context.annotation.Configuration;
43 import org.springframework.context.annotation.Profile;
44 import org.springframework.core.env.Environment;
45 import org.springframework.core.io.ResourceLoader;
46
47 @Configuration
48 @Profile("clamp-ssl-config")
49 public class SslConfig {
50     @Autowired
51     private Environment env;
52
53     @Bean
54     WebServerFactoryCustomizer<TomcatServletWebServerFactory> tomcatCustomizer(ServerProperties serverProperties,
55                                                                                ResourceLoader resourceLoader) {
56         return (tomcat) -> tomcat.setSslStoreProvider(new SslStoreProvider() {
57             @Override
58             public KeyStore getKeyStore() throws KeyStoreException,
59                     NoSuchAlgorithmException, CertificateException, IOException {
60                 KeyStore keystore = KeyStore.getInstance(env.getProperty("server.ssl.key-store-type"));
61                 String password = PassDecoder.decode(env.getProperty("server.ssl.key-store-password"),
62                         env.getProperty("clamp.config.keyFile"));
63                 keystore.load(ResourceFileUtils.getResourceAsStream(env.getProperty("server.ssl.key-store")),
64                         password.toCharArray());
65                 return keystore;
66             }
67
68             @Override
69             public KeyStore getTrustStore() throws KeyStoreException,
70                     NoSuchAlgorithmException, CertificateException, IOException {
71                 KeyStore truststore = KeyStore.getInstance("JKS");
72                 String password = PassDecoder.decode(env.getProperty("server.ssl.trust-store-password"),
73                         env.getProperty("clamp.config.keyFile"));
74                 truststore.load(
75                         ResourceFileUtils.getResourceAsStream(env.getProperty("server.ssl.trust-store")),
76                         password.toCharArray());
77                 return truststore;
78             }
79         });
80     }
81
82     @Bean
83     WebServerFactoryCustomizer<TomcatServletWebServerFactory> tomcatSslCustomizer(ServerProperties serverProperties,
84                                                                                   ResourceLoader resourceLoader) {
85         return (tomcat) -> tomcat.setSsl(new Ssl() {
86             @Override
87             public String getKeyPassword() {
88                 String password = PassDecoder.decode(env.getProperty("server.ssl.key-password"),
89                         env.getProperty("clamp.config.keyFile"));
90                 return password;
91             }
92         });
93     }
94 }