summaryrefslogtreecommitdiffstats
path: root/zusammen-lib/src/main/java/org/onap/sdc/common/zusammen/config/ZusammenConfig.java
blob: ce89ade4df5e1d2d057dbf8ee7979dedf6f3dc6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
 * Copyright © 2018 European Support Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onap.sdc.common.zusammen.config;

import com.datastax.driver.core.ConsistencyLevel;
import com.datastax.driver.core.RemoteEndpointAwareJdkSSLOptions;
import com.datastax.driver.core.SSLOptions;
import java.io.FileInputStream;
import java.security.KeyStore;
import java.security.SecureRandom;
import javax.annotation.PostConstruct;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.cassandra.ClusterBuilderCustomizer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ZusammenConfig {

    private static final String[] CIPHER_SUITES = {"TLS_RSA_WITH_AES_128_CBC_SHA"};
    private static final String KEYSTORE_TYPE = "JKS";
    private static final String SECURE_SOCKET_PROTOCOL = "SSL";
    private static final String KEYSPACE = "zusammen";

    private final ZusammenConfigProvider provider;


    @Autowired
    public ZusammenConfig(ZusammenConfigProvider provider) {
        this.provider = provider;
    }

    @PostConstruct
    public void init() {
        System.setProperty("cassandra.consistency.level", ConsistencyLevel.LOCAL_QUORUM.name());
        System.setProperty("cassandra.nodes", provider.getCassandraAddresses());
        System.setProperty("cassandra.port", provider.getCassandraPort());
        System.setProperty("cassandra.keyspace", KEYSPACE);

        System.setProperty("cassandra.authenticate", Boolean.toString(Boolean.valueOf(provider.getCassandraAuth())));
        System.setProperty("cassandra.user", provider.getCassandraUser());
        System.setProperty("cassandra.password", provider.getCassandraPassword());

        System.setProperty("cassandra.ssl", Boolean.toString(Boolean.valueOf(provider.getCassandraSSL())));
        System.setProperty("cassandra.truststore", provider.getCassandraTrustStorePath());
        System.setProperty("cassandra.truststore.password", provider.getCassandraTrustStorePassword());
    }

    @Bean
    @ConditionalOnProperty("cassandra.ssl")
    ClusterBuilderCustomizer clusterBuilderCustomizer() {
        SSLOptions sslOptions = RemoteEndpointAwareJdkSSLOptions
                                        .builder()
                                        .withSSLContext(getSslContext())
                                        .withCipherSuites(CIPHER_SUITES).build();
        return builder -> builder.withSSL(sslOptions);
    }

    private SSLContext getSslContext() {
        try (FileInputStream tsf = new FileInputStream(provider.getCassandraTrustStorePath())) {
            SSLContext ctx = SSLContext.getInstance(SECURE_SOCKET_PROTOCOL);
            KeyStore ts = KeyStore.getInstance(KEYSTORE_TYPE);
            ts.load(tsf, provider.getCassandraTrustStorePassword().toCharArray());
            TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
            tmf.init(ts);
            ctx.init(null, tmf.getTrustManagers(), new SecureRandom());
            return ctx;
        } catch (Exception ex) {
            throw new BeanCreationException(ex.getMessage(), ex);
        }
    }
}