7c7433e960f8466f631e4b3904e8774f8fe3fda9
[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.ResourceFileUtil;
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                 String keyStore = env.getProperty("server.ssl.key-store");
64                 InputStream is = ResourceFileUtil.getResourceAsStream(keyStore.replaceAll("classpath:", ""));
65                 keystore.load(is, password.toCharArray());
66                 return keystore;
67             }
68
69             @Override
70             public KeyStore getTrustStore() throws KeyStoreException, 
71                     NoSuchAlgorithmException, CertificateException, IOException {
72                     KeyStore truststore = KeyStore.getInstance("JKS");
73                     String password = PassDecoder.decode(env.getProperty("server.ssl.trust-store-password"), 
74                             env.getProperty("clamp.config.keyFile"));
75                     truststore.load(
76                         Thread.currentThread().getContextClassLoader()
77                             .getResourceAsStream(env.getProperty("server.ssl.trust-store")
78                                 .replaceAll("classpath:", "")),
79                             password.toCharArray());
80                     return truststore;
81             }
82         });
83     }
84
85     @Bean
86     WebServerFactoryCustomizer<TomcatServletWebServerFactory> tomcatSslCustomizer(ServerProperties serverProperties,
87             ResourceLoader resourceLoader) {
88         return (tomcat) -> tomcat.setSsl(new Ssl() {
89             @Override
90             public String getKeyPassword() {
91                     String password = PassDecoder.decode(env.getProperty("server.ssl.key-password"), 
92                             env.getProperty("clamp.config.keyFile"));
93                     return password;
94             }
95         });
96     }
97 }