Changes for backend to support SSL
[sdc/sdc-workflow-designer.git] / workflow-designer-be / src / main / java / org / onap / sdc / workflow / server / config / ZusammenConfig.java
1 /*
2  * Copyright © 2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.sdc.workflow.server.config;
18
19 import com.datastax.driver.core.RemoteEndpointAwareJdkSSLOptions;
20 import com.datastax.driver.core.SSLOptions;
21 import java.io.FileInputStream;
22 import java.security.KeyStore;
23 import java.security.SecureRandom;
24 import javax.annotation.PostConstruct;
25 import javax.net.ssl.SSLContext;
26 import javax.net.ssl.TrustManagerFactory;
27 import org.springframework.beans.factory.BeanCreationException;
28 import org.springframework.beans.factory.annotation.Value;
29 import org.springframework.boot.autoconfigure.cassandra.ClusterBuilderCustomizer;
30 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
31 import org.springframework.context.annotation.Bean;
32 import org.springframework.context.annotation.Configuration;
33
34 @Configuration
35 public class ZusammenConfig {
36
37     @Value("${spring.data.cassandra.keyspace-name}")
38     private String tenant;
39     @Value("${spring.data.cassandra.contact-points}")
40     private String cassandraAddress;
41     @Value("${spring.data.cassandra.username}")
42     private String cassandraUser;
43     @Value("${spring.data.cassandra.password}")
44     private String cassandraPassword;
45     @Value("${zusammen.cassandra.isAuthenticate}")
46     private String cassandraAuth;
47     @Value("${spring.data.cassandra.ssl}")
48     private String cassandraSSL;
49     @Value("${zusammen.cassandra.trustStorePath}")
50     private String cassandraTrustStorePath;
51     @Value("${zusammen.cassandra.trustStorePassword}")
52     private String cassandraTrustStorePassword;
53
54     private static final String[] CIPHER_SUITES = {"TLS_RSA_WITH_AES_128_CBC_SHA"};
55     private static final String KEYSTORE_TYPE = "JKS";
56     private static final String SECURE_SOCKET_PROTOCOL = "SSL";
57
58     @PostConstruct
59     public void init() {
60         System.setProperty("cassandra.nodes", cassandraAddress);
61         System.setProperty("cassandra.user", cassandraUser);
62         System.setProperty("cassandra.password", cassandraPassword);
63         System.setProperty("cassandra.authenticate", Boolean.toString(Boolean.valueOf(cassandraAuth)));
64         System.setProperty("cassandra.ssl", Boolean.toString(Boolean.valueOf(cassandraSSL)));
65         System.setProperty("cassandra.truststore", cassandraTrustStorePath);
66         System.setProperty("cassandra.truststore.password", cassandraTrustStorePassword);
67     }
68
69     public String getTenant() {
70         return tenant;
71     }
72
73     @Bean
74     @ConditionalOnProperty("spring.data.cassandra.ssl")
75     ClusterBuilderCustomizer clusterBuilderCustomizer() {
76         SSLOptions sslOptions = RemoteEndpointAwareJdkSSLOptions
77                                         .builder()
78                                         .withSSLContext(getSslContext())
79                                         .withCipherSuites(CIPHER_SUITES).build();
80         return builder -> builder.withSSL(sslOptions);
81     }
82
83     private SSLContext getSslContext() {
84         try (FileInputStream tsf = new FileInputStream(cassandraTrustStorePath)) {
85             SSLContext ctx = SSLContext.getInstance(SECURE_SOCKET_PROTOCOL);
86             KeyStore ts = KeyStore.getInstance(KEYSTORE_TYPE);
87             ts.load(tsf, cassandraTrustStorePassword.toCharArray());
88             TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
89             tmf.init(ts);
90             ctx.init(null, tmf.getTrustManagers(), new SecureRandom());
91             return ctx;
92         } catch (Exception ex) {
93             throw new BeanCreationException(ex.getMessage(), ex);
94         }
95     }
96 }