forked from Ericsson/ecchronos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReloadingCertificateHandler.java
216 lines (190 loc) · 8.06 KB
/
ReloadingCertificateHandler.java
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
* Copyright 2020 Telefonaktiebolaget LM Ericsson
*
* 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 com.ericsson.bss.cassandra.ecchronos.application;
import com.datastax.oss.driver.api.core.metadata.EndPoint;
import com.ericsson.bss.cassandra.ecchronos.application.config.TLSConfig;
import com.ericsson.bss.cassandra.ecchronos.connection.CertificateHandler;
import io.netty.buffer.ByteBufAllocator;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.SSLParameters;
import javax.net.ssl.TrustManagerFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;
public class ReloadingCertificateHandler implements CertificateHandler
{
private static final Logger LOG = LoggerFactory.getLogger(ReloadingCertificateHandler.class);
private final AtomicReference<Context> currentContext = new AtomicReference<>();
private final Supplier<TLSConfig> tlsConfigSupplier;
public ReloadingCertificateHandler(final Supplier<TLSConfig> aTLSConfigSupplier)
{
this.tlsConfigSupplier = aTLSConfigSupplier;
}
@Override
public final SSLEngine newSslEngine(final EndPoint remoteEndpoint)
{
Context context = getContext();
TLSConfig tlsConfig = context.getTlsConfig();
SslContext sslContext = context.getSSLContext();
SSLEngine sslEngine;
if (remoteEndpoint != null)
{
InetSocketAddress socketAddress = (InetSocketAddress) remoteEndpoint.resolve();
sslEngine = sslContext.newEngine(ByteBufAllocator.DEFAULT, socketAddress.getHostName(),
socketAddress.getPort());
}
else
{
sslEngine = sslContext.newEngine(ByteBufAllocator.DEFAULT);
}
sslEngine.setUseClientMode(true);
if (tlsConfig.requiresEndpointVerification())
{
SSLParameters sslParameters = new SSLParameters();
sslParameters.setEndpointIdentificationAlgorithm("HTTPS");
sslEngine.setSSLParameters(sslParameters);
}
tlsConfig.getCipher_suites().ifPresent(sslEngine::setEnabledCipherSuites);
return sslEngine;
}
protected final Context getContext()
{
TLSConfig tlsConfig = tlsConfigSupplier.get();
Context context = currentContext.get();
try
{
while (context == null || !context.sameConfig(tlsConfig))
{
Context newContext = new Context(tlsConfig);
if (currentContext.compareAndSet(context, newContext))
{
context = newContext;
}
else
{
context = currentContext.get();
}
tlsConfig = tlsConfigSupplier.get();
}
}
catch (NoSuchAlgorithmException | IOException | UnrecoverableKeyException | CertificateException
| KeyStoreException | KeyManagementException e)
{
LOG.warn("Unable to create new SSL Context after configuration changed. Trying with the old one", e);
}
return context;
}
@Override
public void close() throws Exception
{
}
protected static final class Context
{
private final TLSConfig tlsConfig;
private final SslContext sslContext;
Context(final TLSConfig aTLSConfig) throws NoSuchAlgorithmException, IOException, UnrecoverableKeyException,
CertificateException, KeyStoreException, KeyManagementException
{
this.tlsConfig = aTLSConfig;
this.sslContext = createSSLContext(this.tlsConfig);
}
TLSConfig getTlsConfig()
{
return tlsConfig;
}
boolean sameConfig(final TLSConfig aTLSConfig)
{
return this.tlsConfig.equals(aTLSConfig);
}
SslContext getSSLContext()
{
return sslContext;
}
}
protected static SslContext createSSLContext(final TLSConfig tlsConfig) throws IOException,
NoSuchAlgorithmException,
KeyStoreException,
CertificateException,
UnrecoverableKeyException
{
SslContextBuilder builder = SslContextBuilder.forClient();
if (tlsConfig.getCertificate().isPresent()
&& tlsConfig.getCertificatePrivateKey().isPresent()
&& tlsConfig.getTrustCertificate().isPresent())
{
File certificateFile = new File(tlsConfig.getCertificate().get());
File certificatePrivateKeyFile = new File(tlsConfig.getCertificatePrivateKey().get());
File trustCertificateFile = new File(tlsConfig.getTrustCertificate().get());
builder.keyManager(certificateFile, certificatePrivateKeyFile);
builder.trustManager(trustCertificateFile);
}
else
{
KeyManagerFactory keyManagerFactory = getKeyManagerFactory(tlsConfig);
TrustManagerFactory trustManagerFactory = getTrustManagerFactory(tlsConfig);
builder.keyManager(keyManagerFactory);
builder.trustManager(trustManagerFactory);
}
if (tlsConfig.getCipher_suites().isPresent())
{
builder.ciphers(Arrays.asList(tlsConfig.getCipher_suites().get()));
}
return builder.protocols(tlsConfig.getProtocols()).build();
}
protected static KeyManagerFactory getKeyManagerFactory(final TLSConfig tlsConfig) throws IOException,
NoSuchAlgorithmException, KeyStoreException, CertificateException, UnrecoverableKeyException
{
String algorithm = tlsConfig.getAlgorithm().orElse(KeyManagerFactory.getDefaultAlgorithm());
char[] keystorePassword = tlsConfig.getKeystore_password().toCharArray();
try (InputStream keystoreFile = new FileInputStream(tlsConfig.getKeystore()))
{
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(algorithm);
KeyStore keyStore = KeyStore.getInstance(tlsConfig.getStore_type());
keyStore.load(keystoreFile, keystorePassword);
keyManagerFactory.init(keyStore, keystorePassword);
return keyManagerFactory;
}
}
protected static TrustManagerFactory getTrustManagerFactory(final TLSConfig tlsConfig)
throws IOException, NoSuchAlgorithmException, KeyStoreException, CertificateException
{
String algorithm = tlsConfig.getAlgorithm().orElse(TrustManagerFactory.getDefaultAlgorithm());
char[] truststorePassword = tlsConfig.getTruststore_password().toCharArray();
try (InputStream truststoreFile = new FileInputStream(tlsConfig.getTruststore()))
{
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(algorithm);
KeyStore keyStore = KeyStore.getInstance(tlsConfig.getStore_type());
keyStore.load(truststoreFile, truststorePassword);
trustManagerFactory.init(keyStore);
return trustManagerFactory;
}
}
}