Skip to content

Commit 0e459e0

Browse files
committed
Add Scylla Cloud configuration classes
Similarly as in java driver 3.x adds classes representing Scylla Cloud configuration details along with methods that allow for using them in driver code. Adds necessary dependencies to pom files.
1 parent f86d43c commit 0e459e0

File tree

8 files changed

+689
-0
lines changed

8 files changed

+689
-0
lines changed

core/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@
115115
<groupId>com.fasterxml.jackson.core</groupId>
116116
<artifactId>jackson-databind</artifactId>
117117
</dependency>
118+
<dependency>
119+
<groupId>com.fasterxml.jackson.dataformat</groupId>
120+
<artifactId>jackson-dataformat-yaml</artifactId>
121+
</dependency>
118122
<dependency>
119123
<groupId>org.reactivestreams</groupId>
120124
<artifactId>reactive-streams</artifactId>
@@ -192,6 +196,14 @@
192196
<artifactId>wiremock</artifactId>
193197
<scope>test</scope>
194198
</dependency>
199+
<dependency>
200+
<groupId>org.bouncycastle</groupId>
201+
<artifactId>bcprov-jdk18on</artifactId>
202+
</dependency>
203+
<dependency>
204+
<groupId>org.bouncycastle</groupId>
205+
<artifactId>bcpkix-jdk18on</artifactId>
206+
</dependency>
195207
</dependencies>
196208
<build>
197209
<resources>
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package com.datastax.oss.driver.internal.core.config.scyllacloud;
2+
3+
import com.datastax.oss.driver.api.core.ssl.SslEngineFactory;
4+
import com.datastax.oss.driver.internal.core.ssl.SniSslEngineFactory;
5+
import java.io.File;
6+
import java.io.FileOutputStream;
7+
import java.io.IOException;
8+
import java.io.OutputStream;
9+
import java.security.GeneralSecurityException;
10+
import java.security.KeyStore;
11+
import java.security.KeyStoreException;
12+
import java.security.NoSuchAlgorithmException;
13+
import java.security.SecureRandom;
14+
import java.security.cert.CertificateException;
15+
import java.security.cert.X509Certificate;
16+
import javax.net.ssl.KeyManagerFactory;
17+
import javax.net.ssl.SSLContext;
18+
import javax.net.ssl.TrustManager;
19+
import javax.net.ssl.TrustManagerFactory;
20+
import javax.net.ssl.X509TrustManager;
21+
22+
public class ConfigurationBundle {
23+
private final KeyStore identity;
24+
private final KeyStore trustStore;
25+
26+
public ConfigurationBundle(KeyStore identity, KeyStore trustStore) {
27+
this.identity = identity;
28+
this.trustStore = trustStore;
29+
}
30+
31+
public KeyStore getIdentity() {
32+
return identity;
33+
}
34+
35+
public KeyStore getTrustStore() {
36+
return trustStore;
37+
}
38+
39+
private void writeKeystore(String path, KeyStore ks, char[] password)
40+
throws IOException, CertificateException, KeyStoreException, NoSuchAlgorithmException {
41+
File file = new File(path);
42+
OutputStream os = new FileOutputStream(file);
43+
ks.store(os, password);
44+
os.close();
45+
}
46+
47+
public void writeIdentity(String path, char[] password)
48+
throws CertificateException, IOException, KeyStoreException, NoSuchAlgorithmException {
49+
writeKeystore(path, identity, password);
50+
}
51+
52+
public void writeTrustStore(String path, char[] password)
53+
throws CertificateException, IOException, KeyStoreException, NoSuchAlgorithmException {
54+
writeKeystore(path, trustStore, password);
55+
}
56+
57+
protected SSLContext getSSLContext() throws IOException, GeneralSecurityException {
58+
KeyManagerFactory kmf = createKeyManagerFactory(identity);
59+
TrustManagerFactory tmf = createTrustManagerFactory(trustStore);
60+
SSLContext sslContext = SSLContext.getInstance("SSL");
61+
sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), new SecureRandom());
62+
return sslContext;
63+
}
64+
65+
protected SSLContext getInsecureSSLContext() throws IOException, GeneralSecurityException {
66+
KeyManagerFactory kmf = createKeyManagerFactory(identity);
67+
SSLContext sslContext = SSLContext.getInstance("SSL");
68+
TrustManager[] trustManager =
69+
new TrustManager[] {
70+
new X509TrustManager() {
71+
@Override
72+
public void checkClientTrusted(X509Certificate[] x509Certificates, String s)
73+
throws CertificateException {}
74+
75+
@Override
76+
public void checkServerTrusted(X509Certificate[] x509Certificates, String s)
77+
throws CertificateException {}
78+
79+
@Override
80+
public X509Certificate[] getAcceptedIssuers() {
81+
return new X509Certificate[0];
82+
}
83+
}
84+
};
85+
86+
sslContext.init(kmf.getKeyManagers(), trustManager, new SecureRandom());
87+
return sslContext;
88+
}
89+
90+
protected KeyManagerFactory createKeyManagerFactory(KeyStore ks)
91+
throws IOException, GeneralSecurityException {
92+
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
93+
kmf.init(ks, "cassandra".toCharArray());
94+
return kmf;
95+
}
96+
97+
protected TrustManagerFactory createTrustManagerFactory(KeyStore ts)
98+
throws IOException, GeneralSecurityException {
99+
TrustManagerFactory tmf =
100+
TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
101+
tmf.init(ts);
102+
return tmf;
103+
}
104+
105+
public SslEngineFactory getSSLEngineFactory() throws GeneralSecurityException, IOException {
106+
return new SniSslEngineFactory(getSSLContext());
107+
}
108+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.datastax.oss.driver.internal.core.config.scyllacloud;
2+
3+
import com.datastax.oss.driver.api.core.ConsistencyLevel;
4+
import com.datastax.oss.driver.api.core.DefaultConsistencyLevel;
5+
import com.fasterxml.jackson.annotation.JsonCreator;
6+
import com.fasterxml.jackson.annotation.JsonProperty;
7+
8+
@SuppressWarnings("unused")
9+
public class Parameters {
10+
private final ConsistencyLevel defaultConsistency;
11+
private final ConsistencyLevel defaultSerialConsistency;
12+
13+
@JsonCreator
14+
public Parameters(
15+
@JsonProperty(value = "defaultConsistency") DefaultConsistencyLevel defaultConsistency,
16+
@JsonProperty(value = "defaultSerialConsistency")
17+
DefaultConsistencyLevel defaultSerialConsistency) {
18+
this.defaultConsistency = defaultConsistency;
19+
this.defaultSerialConsistency = defaultSerialConsistency;
20+
}
21+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.datastax.oss.driver.internal.core.config.scyllacloud;
2+
3+
import com.fasterxml.jackson.annotation.JsonCreator;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import java.io.File;
6+
7+
public class ScyllaCloudAuthInfo {
8+
private final byte[] clientCertificateData;
9+
private final String clientCertificatePath;
10+
private final byte[] clientKeyData;
11+
private final String clientKeyPath;
12+
private final String username;
13+
private final String password;
14+
15+
@JsonCreator
16+
public ScyllaCloudAuthInfo(
17+
@JsonProperty(value = "clientCertificateData") byte[] clientCertificateData,
18+
@JsonProperty(value = "clientCertificatePath") String clientCertificatePath,
19+
@JsonProperty(value = "clientKeyData") byte[] clientKeyData,
20+
@JsonProperty(value = "clientKeyPath") String clientKeyPath,
21+
@JsonProperty(value = "username") String username,
22+
@JsonProperty(value = "password") String password) {
23+
this.clientCertificateData = clientCertificateData;
24+
this.clientCertificatePath = clientCertificatePath;
25+
this.clientKeyData = clientKeyData;
26+
this.clientKeyPath = clientKeyPath;
27+
this.username = username;
28+
this.password = password;
29+
}
30+
31+
public void validate() {
32+
if (clientCertificateData == null) {
33+
if (clientCertificatePath == null) {
34+
throw new IllegalArgumentException(
35+
"Either clientCertificateData or clientCertificatePath has to be provided for authInfo.");
36+
}
37+
File file = new File(clientCertificatePath);
38+
if (!file.canRead()) {
39+
throw new IllegalArgumentException(
40+
"Cannot read file at given clientCertificatePath (" + clientCertificatePath + ").");
41+
}
42+
}
43+
44+
if (clientKeyData == null) {
45+
if (clientKeyPath == null) {
46+
throw new IllegalArgumentException(
47+
"Either clientKeyData or clientKeyPath has to be provided for authInfo.");
48+
}
49+
File file = new File(clientKeyPath);
50+
if (!file.canRead()) {
51+
throw new IllegalArgumentException(
52+
"Cannot read file at given clientKeyPath (" + clientKeyPath + ").");
53+
}
54+
}
55+
}
56+
57+
public byte[] getClientCertificateData() {
58+
return clientCertificateData;
59+
}
60+
61+
public String getClientCertificatePath() {
62+
return clientCertificatePath;
63+
}
64+
65+
public byte[] getClientKeyData() {
66+
return clientKeyData;
67+
}
68+
69+
public String getClientKeyPath() {
70+
return clientKeyPath;
71+
}
72+
73+
public String getUsername() {
74+
return username;
75+
}
76+
77+
public String getPassword() {
78+
return password;
79+
}
80+
}

0 commit comments

Comments
 (0)