4bc8262439c355fd341f25c7d18136c1eb09782b
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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
21 package org.openecomp.core.nosqldb.impl.cassandra;
22
23 import com.datastax.driver.core.Cluster;
24 import com.datastax.driver.core.SSLOptions;
25 import com.datastax.driver.core.Session;
26 import com.google.common.base.Optional;
27 import org.openecomp.core.nosqldb.util.CassandraUtils;
28 import org.openecomp.sdc.logging.api.Logger;
29 import org.openecomp.sdc.logging.api.LoggerFactory;
30
31 import java.io.FileInputStream;
32 import java.io.IOException;
33 import java.security.KeyManagementException;
34 import java.security.KeyStore;
35 import java.security.KeyStoreException;
36 import java.security.NoSuchAlgorithmException;
37 import java.security.SecureRandom;
38 import java.security.UnrecoverableKeyException;
39 import java.security.cert.CertificateException;
40 import javax.net.ssl.SSLContext;
41 import javax.net.ssl.TrustManagerFactory;
42
43 public class CassandraSessionFactory {
44
45   private static final Logger log = (Logger) LoggerFactory.getLogger
46       (CassandraSessionFactory.class.getName());
47
48   public static Session getSession() {
49     return ReferenceHolder.CASSANDRA;
50   }
51
52   /**
53    * New cassandra session session.
54    *
55    * @return the session
56    */
57   public static Session newCassandraSession() {
58     Cluster.Builder builder = Cluster.builder();
59     String[] addresses = CassandraUtils.getAddresses();
60     for (String address : addresses) {
61       builder.addContactPoint(address);
62     }
63
64     //Check if ssl
65     Boolean isSsl = CassandraUtils.isSsl();
66     if (isSsl) {
67       builder.withSSL(getSslOptions().get());
68     }
69     int port = CassandraUtils.getCassandraPort();
70     if (port > 0) {
71       builder.withPort(port);
72     }
73     //Check if user/pass
74     Boolean isAuthenticate = CassandraUtils.isAuthenticate();
75     if (isAuthenticate) {
76       builder.withCredentials(CassandraUtils.getUser(), CassandraUtils.getPassword());
77     }
78     Cluster cluster = builder.build();
79     String keyStore = CassandraUtils.getKeySpace();
80     return cluster.connect(keyStore);
81   }
82
83   private static Optional<SSLOptions> getSslOptions() {
84     Optional<String> truststorePath = Optional.of(CassandraUtils.getTruststore());
85     Optional<String> truststorePassword = Optional.of(CassandraUtils.getTruststorePassword());
86
87     if (truststorePath.isPresent() && truststorePassword.isPresent()) {
88       SSLContext context;
89       try {
90         context = getSslContext(truststorePath.get(), truststorePassword.get());
91       } catch (UnrecoverableKeyException | KeyManagementException
92           | NoSuchAlgorithmException | KeyStoreException | CertificateException
93           | IOException exception) {
94         throw new RuntimeException(exception);
95       }
96       String[] css = new String[]{"TLS_RSA_WITH_AES_128_CBC_SHA"};
97       return Optional.of(new SSLOptions(context, css));
98     }
99     return Optional.absent();
100   }
101
102   private static SSLContext getSslContext(String truststorePath, String truststorePassword)
103       throws NoSuchAlgorithmException, KeyStoreException, CertificateException, IOException,
104       UnrecoverableKeyException, KeyManagementException {
105     FileInputStream tsf = null;
106     SSLContext ctx = null;
107     try {
108       tsf = new FileInputStream(truststorePath);
109       ctx = SSLContext.getInstance("SSL");
110
111       KeyStore ts = KeyStore.getInstance("JKS");
112       ts.load(tsf, truststorePassword.toCharArray());
113       TrustManagerFactory tmf =
114           TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
115       tmf.init(ts);
116
117       ctx.init(null, tmf.getTrustManagers(), new SecureRandom());
118     } catch (Exception exception) {
119       log.debug("",exception);
120     } finally {
121       if (tsf != null) {
122         tsf.close();
123       }
124     }
125     return ctx;
126   }
127
128   private static class ReferenceHolder {
129     private static final Session CASSANDRA = newCassandraSession();
130   }
131
132
133 }