|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package com.datastax.oss.driver.internal.core.ssl; |
| 19 | + |
| 20 | +import com.datastax.oss.driver.shaded.guava.common.annotations.VisibleForTesting; |
| 21 | +import java.io.ByteArrayInputStream; |
| 22 | +import java.io.IOException; |
| 23 | +import java.io.InputStream; |
| 24 | +import java.net.Socket; |
| 25 | +import java.nio.file.Files; |
| 26 | +import java.nio.file.Path; |
| 27 | +import java.security.KeyStore; |
| 28 | +import java.security.KeyStoreException; |
| 29 | +import java.security.MessageDigest; |
| 30 | +import java.security.NoSuchAlgorithmException; |
| 31 | +import java.security.Principal; |
| 32 | +import java.security.PrivateKey; |
| 33 | +import java.security.Provider; |
| 34 | +import java.security.UnrecoverableKeyException; |
| 35 | +import java.security.cert.CertificateException; |
| 36 | +import java.security.cert.X509Certificate; |
| 37 | +import java.time.Duration; |
| 38 | +import java.util.Arrays; |
| 39 | +import java.util.concurrent.Executors; |
| 40 | +import java.util.concurrent.ScheduledExecutorService; |
| 41 | +import java.util.concurrent.TimeUnit; |
| 42 | +import java.util.concurrent.atomic.AtomicReference; |
| 43 | +import javax.net.ssl.KeyManager; |
| 44 | +import javax.net.ssl.KeyManagerFactory; |
| 45 | +import javax.net.ssl.KeyManagerFactorySpi; |
| 46 | +import javax.net.ssl.ManagerFactoryParameters; |
| 47 | +import javax.net.ssl.SSLEngine; |
| 48 | +import javax.net.ssl.X509ExtendedKeyManager; |
| 49 | +import org.slf4j.Logger; |
| 50 | +import org.slf4j.LoggerFactory; |
| 51 | + |
| 52 | +public class ReloadingKeyManagerFactory extends KeyManagerFactory implements AutoCloseable { |
| 53 | + private static final Logger logger = LoggerFactory.getLogger(ReloadingKeyManagerFactory.class); |
| 54 | + private static final String KEYSTORE_TYPE = "JKS"; |
| 55 | + private Path keystorePath; |
| 56 | + private String keystorePassword; |
| 57 | + private ScheduledExecutorService executor; |
| 58 | + private final Spi spi; |
| 59 | + |
| 60 | + // We're using a single thread executor so this shouldn't need to be volatile, since all updates |
| 61 | + // to lastDigest should come from the same thread |
| 62 | + private volatile byte[] lastDigest; |
| 63 | + |
| 64 | + /** |
| 65 | + * Create a new {@link ReloadingKeyManagerFactory} with the given keystore file and password, |
| 66 | + * reloading from the file's content at the given interval. This function will do an initial |
| 67 | + * reload before returning, to confirm that the file exists and is readable. |
| 68 | + * |
| 69 | + * @param keystorePath the keystore file to reload |
| 70 | + * @param keystorePassword the keystore password |
| 71 | + * @param reloadInterval the duration between reload attempts. Set to {@link |
| 72 | + * java.time.Duration#ZERO} to disable scheduled reloading. |
| 73 | + * @return |
| 74 | + */ |
| 75 | + public static ReloadingKeyManagerFactory create( |
| 76 | + Path keystorePath, String keystorePassword, Duration reloadInterval) { |
| 77 | + KeyManagerFactory kmf; |
| 78 | + try { |
| 79 | + kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); |
| 80 | + } catch (NoSuchAlgorithmException e) { |
| 81 | + throw new RuntimeException(e); |
| 82 | + } |
| 83 | + |
| 84 | + KeyStore ks; |
| 85 | + try (InputStream ksf = Files.newInputStream(keystorePath)) { |
| 86 | + ks = KeyStore.getInstance(KEYSTORE_TYPE); |
| 87 | + ks.load(ksf, keystorePassword.toCharArray()); |
| 88 | + } catch (IOException | CertificateException | KeyStoreException | NoSuchAlgorithmException e) { |
| 89 | + throw new RuntimeException(e); |
| 90 | + } |
| 91 | + try { |
| 92 | + kmf.init(ks, keystorePassword.toCharArray()); |
| 93 | + } catch (KeyStoreException | NoSuchAlgorithmException | UnrecoverableKeyException e) { |
| 94 | + throw new RuntimeException(e); |
| 95 | + } |
| 96 | + |
| 97 | + ReloadingKeyManagerFactory reloadingKeyManagerFactory = new ReloadingKeyManagerFactory(kmf); |
| 98 | + reloadingKeyManagerFactory.start(keystorePath, keystorePassword, reloadInterval); |
| 99 | + return reloadingKeyManagerFactory; |
| 100 | + } |
| 101 | + |
| 102 | + @VisibleForTesting |
| 103 | + protected ReloadingKeyManagerFactory(KeyManagerFactory initial) { |
| 104 | + this( |
| 105 | + new Spi((X509ExtendedKeyManager) initial.getKeyManagers()[0]), |
| 106 | + initial.getProvider(), |
| 107 | + initial.getAlgorithm()); |
| 108 | + } |
| 109 | + |
| 110 | + private ReloadingKeyManagerFactory(Spi spi, Provider provider, String algorithm) { |
| 111 | + super(spi, provider, algorithm); |
| 112 | + this.spi = spi; |
| 113 | + } |
| 114 | + |
| 115 | + private void start(Path keystorePath, String keystorePassword, Duration reloadInterval) { |
| 116 | + this.keystorePath = keystorePath; |
| 117 | + this.keystorePassword = keystorePassword; |
| 118 | + this.executor = |
| 119 | + Executors.newScheduledThreadPool( |
| 120 | + 1, |
| 121 | + runnable -> { |
| 122 | + Thread t = Executors.defaultThreadFactory().newThread(runnable); |
| 123 | + t.setDaemon(true); |
| 124 | + return t; |
| 125 | + }); |
| 126 | + |
| 127 | + // Ensure that reload is called once synchronously, to make sure the file exists etc. |
| 128 | + reload(); |
| 129 | + |
| 130 | + if (!reloadInterval.isZero()) |
| 131 | + this.executor.scheduleWithFixedDelay( |
| 132 | + this::reload, |
| 133 | + reloadInterval.toMillis(), |
| 134 | + reloadInterval.toMillis(), |
| 135 | + TimeUnit.MILLISECONDS); |
| 136 | + } |
| 137 | + |
| 138 | + @VisibleForTesting |
| 139 | + void reload() { |
| 140 | + try { |
| 141 | + reload0(); |
| 142 | + } catch (Exception e) { |
| 143 | + logger.warn("Failed to reload", e); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + private synchronized void reload0() |
| 148 | + throws NoSuchAlgorithmException, IOException, KeyStoreException, CertificateException, |
| 149 | + UnrecoverableKeyException { |
| 150 | + logger.debug("Checking KeyStore file {} for updates", keystorePath); |
| 151 | + |
| 152 | + final byte[] keyStoreBytes = Files.readAllBytes(keystorePath); |
| 153 | + final byte[] newDigest = digest(keyStoreBytes); |
| 154 | + if (lastDigest != null && Arrays.equals(lastDigest, digest(keyStoreBytes))) { |
| 155 | + logger.debug("KeyStore file content has not changed; skipping update"); |
| 156 | + return; |
| 157 | + } |
| 158 | + |
| 159 | + final KeyStore keyStore = KeyStore.getInstance(KEYSTORE_TYPE); |
| 160 | + try (InputStream inputStream = new ByteArrayInputStream(keyStoreBytes)) { |
| 161 | + keyStore.load(inputStream, keystorePassword.toCharArray()); |
| 162 | + } |
| 163 | + KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); |
| 164 | + kmf.init(keyStore, keystorePassword.toCharArray()); |
| 165 | + logger.info("Detected updates to KeyStore file {}", keystorePath); |
| 166 | + |
| 167 | + this.spi.keyManager.set((X509ExtendedKeyManager) kmf.getKeyManagers()[0]); |
| 168 | + this.lastDigest = newDigest; |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public void close() throws Exception { |
| 173 | + if (executor != null) { |
| 174 | + executor.shutdown(); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + private static byte[] digest(byte[] payload) throws NoSuchAlgorithmException { |
| 179 | + final MessageDigest digest = MessageDigest.getInstance("SHA-256"); |
| 180 | + return digest.digest(payload); |
| 181 | + } |
| 182 | + |
| 183 | + private static class Spi extends KeyManagerFactorySpi { |
| 184 | + DelegatingKeyManager keyManager; |
| 185 | + |
| 186 | + Spi(X509ExtendedKeyManager initial) { |
| 187 | + this.keyManager = new DelegatingKeyManager(initial); |
| 188 | + } |
| 189 | + |
| 190 | + @Override |
| 191 | + protected void engineInit(KeyStore ks, char[] password) { |
| 192 | + throw new UnsupportedOperationException(); |
| 193 | + } |
| 194 | + |
| 195 | + @Override |
| 196 | + protected void engineInit(ManagerFactoryParameters spec) { |
| 197 | + throw new UnsupportedOperationException(); |
| 198 | + } |
| 199 | + |
| 200 | + @Override |
| 201 | + protected KeyManager[] engineGetKeyManagers() { |
| 202 | + return new KeyManager[] {keyManager}; |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + private static class DelegatingKeyManager extends X509ExtendedKeyManager { |
| 207 | + AtomicReference<X509ExtendedKeyManager> delegate; |
| 208 | + |
| 209 | + DelegatingKeyManager(X509ExtendedKeyManager initial) { |
| 210 | + delegate = new AtomicReference<>(initial); |
| 211 | + } |
| 212 | + |
| 213 | + void set(X509ExtendedKeyManager keyManager) { |
| 214 | + delegate.set(keyManager); |
| 215 | + } |
| 216 | + |
| 217 | + @Override |
| 218 | + public String chooseEngineClientAlias(String[] keyType, Principal[] issuers, SSLEngine engine) { |
| 219 | + return delegate.get().chooseEngineClientAlias(keyType, issuers, engine); |
| 220 | + } |
| 221 | + |
| 222 | + @Override |
| 223 | + public String chooseEngineServerAlias(String keyType, Principal[] issuers, SSLEngine engine) { |
| 224 | + return delegate.get().chooseEngineServerAlias(keyType, issuers, engine); |
| 225 | + } |
| 226 | + |
| 227 | + @Override |
| 228 | + public String[] getClientAliases(String keyType, Principal[] issuers) { |
| 229 | + return delegate.get().getClientAliases(keyType, issuers); |
| 230 | + } |
| 231 | + |
| 232 | + @Override |
| 233 | + public String chooseClientAlias(String[] keyType, Principal[] issuers, Socket socket) { |
| 234 | + return delegate.get().chooseClientAlias(keyType, issuers, socket); |
| 235 | + } |
| 236 | + |
| 237 | + @Override |
| 238 | + public String[] getServerAliases(String keyType, Principal[] issuers) { |
| 239 | + return delegate.get().getServerAliases(keyType, issuers); |
| 240 | + } |
| 241 | + |
| 242 | + @Override |
| 243 | + public String chooseServerAlias(String keyType, Principal[] issuers, Socket socket) { |
| 244 | + return delegate.get().chooseServerAlias(keyType, issuers, socket); |
| 245 | + } |
| 246 | + |
| 247 | + @Override |
| 248 | + public X509Certificate[] getCertificateChain(String alias) { |
| 249 | + return delegate.get().getCertificateChain(alias); |
| 250 | + } |
| 251 | + |
| 252 | + @Override |
| 253 | + public PrivateKey getPrivateKey(String alias) { |
| 254 | + return delegate.get().getPrivateKey(alias); |
| 255 | + } |
| 256 | + } |
| 257 | +} |
0 commit comments