This repository has been archived by the owner on Feb 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
ReloadingKeyManager.java
265 lines (237 loc) · 9.05 KB
/
ReloadingKeyManager.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package jetpack.ssl;
import com.google.common.base.Predicate;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;
import com.google.common.io.Closer;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.Principal;
import java.security.PrivateKey;
import java.security.UnrecoverableKeyException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.X509ExtendedKeyManager;
import org.joda.time.Duration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.concurrent.TimeUnit.SECONDS;
/** X509KeyManager which periodically looks for a newer key and transparently reloads. */
public class ReloadingKeyManager extends X509ExtendedKeyManager {
public static final Duration DEFAULT_RELOAD_INTERVAL = Duration.standardHours(2);
private static final Logger logger = LoggerFactory.getLogger(ReloadingKeyManager.class);
private final ThreadFactory namedThreadFactory =
new ThreadFactoryBuilder().setNameFormat("reloading-key-manager").build();
private final ScheduledExecutorService reloadScheduler =
Executors.newSingleThreadScheduledExecutor(namedThreadFactory);
private final FileResolver fileResolver;
private final char[] pin;
private final String keyStoreType;
private final Duration reloadInterval;
private volatile KeyStore keyStore;
private volatile String keyName;
/**
* @param fileResolver which resolves a {@link java.io.File} for a {@link java.security.KeyStore}.
* @param pin KeyStore password.
* @param keyStoreType KeyStore type.
*/
public ReloadingKeyManager(FileResolver fileResolver, char[] pin, String keyStoreType) {
this(fileResolver, pin, keyStoreType, DEFAULT_RELOAD_INTERVAL);
}
/**
* @param fileResolver which resolves a {@link File} for a {@link KeyStore}.
* @param pin KeyStore password.
* @param keyStoreType KeyStore type.
* @param reloadInterval custom interval to re-resolve and reload the KeyStore.
* If null or Duration.ZERO, the KeyStore is loaded once.
*/
public ReloadingKeyManager(FileResolver fileResolver, char[] pin, String keyStoreType,
Duration reloadInterval) {
this.fileResolver = checkNotNull(fileResolver);
this.pin = checkNotNull(pin);
this.keyStoreType = checkNotNull(keyStoreType);
this.reloadInterval = reloadInterval;
loadKeyStore();
reloadKeyStoreOnInterval();
}
/** @return interval between keystore reloads. */
public Duration getReloadInterval() {
return reloadInterval;
}
@Override
public String[] getClientAliases(String string, Principal[] principals) {
return new String[] {keyName};
}
@Override
public String chooseClientAlias(String[] strings, Principal[] principals, Socket socket) {
return keyName;
}
@Override
public String[] getServerAliases(String string, Principal[] principals) {
return new String[] {keyName};
}
@Override
public String chooseServerAlias(String string, Principal[] principals, Socket socket) {
return keyName;
}
@Override
public X509Certificate[] getCertificateChain(String alias) {
checkArgument(!alias.isEmpty());
try {
Certificate[] certs = keyStore.getCertificateChain(alias);
if (certs == null) {
logger.error("No certificate chain is found for alias: %s", alias);
throw new KeyStoreException("No certificate chain is found for alias: " + alias);
}
List<X509Certificate> x509Certs = Lists.newArrayList();
for (Certificate cert : certs) {
if (cert instanceof X509Certificate) {
x509Certs.add((X509Certificate) cert);
}
}
return x509Certs.toArray(new X509Certificate[x509Certs.size()]);
} catch (KeyStoreException e) {
throw Throwables.propagate(e);
}
}
@Override
public PrivateKey getPrivateKey(String keyAlias) {
checkArgument(!keyAlias.isEmpty());
try {
return (PrivateKey) keyStore.getKey(keyAlias, pin);
} catch (KeyStoreException e) {
// KeyStore is initialized at construction, so this should not occur.
throw new AssertionError(e);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("Algorithm for reading key not available", e);
} catch (UnrecoverableKeyException e) {
throw new RuntimeException("Invalid password for reading key " + keyAlias, e);
}
}
@Override
public String chooseEngineClientAlias(String[] strings, Principal[] principals, SSLEngine sslEngine) {
return keyName;
}
@Override
public String chooseEngineServerAlias(String s, Principal[] principals, SSLEngine sslEngine) {
return keyName;
}
private void reloadKeyStoreOnInterval() {
if (Duration.ZERO.isEqual(reloadInterval)) return;
Runnable loader = new Runnable() {
public void run() {
logger.info("Checking for new keystore to load at path {}", fileResolver.getSearchPath());
try {
loadKeyStore();
logger.info("Completed reloading keystore.");
} catch (Exception e) {
logger.error("Keystore reload failed", e);
}
}
};
reloadScheduler.scheduleAtFixedRate(loader,
reloadInterval.getStandardSeconds(),
reloadInterval.getStandardSeconds(),
SECONDS);
}
private void loadKeyStore() {
File latest;
try {
latest = fileResolver.resolve();
} catch (FileNotFoundException e) {
throw Throwables.propagate(e);
}
logger.info("Resolved latest keystore {}", latest);
try {
keyStore = loadKeyStore(latest, keyStoreType, new String(pin));
} catch (IOException e) {
throw Throwables.propagate(e);
}
List<String> keyAliases = findAliasesOfType(KeyStore.PrivateKeyEntry.class, keyStore);
keyName = Iterables.getOnlyElement(keyAliases);
}
/**
* Finds all aliases present in the keystore of a given entry type.
*
* @param entryClass one of {@link KeyStore.PrivateKeyEntry}, {@link KeyStore.SecretKeyEntry}, or
* {@link KeyStore.TrustedCertificateEntry}.
* @param keyStore keystore containing all the entries.
* @return List of aliases present of the given type.
*/
private static List<String> findAliasesOfType(final Class<? extends KeyStore.Entry> entryClass,
final KeyStore keyStore) {
Iterator<String> aliasIterator;
try {
aliasIterator = Iterators.forEnumeration(keyStore.aliases());
} catch (KeyStoreException e) {
throw new RuntimeException("KeyStore not initialized.", e);
}
Iterator<String> results = Iterators.filter(aliasIterator,
new Predicate<String>() {
@Override public boolean apply(String alias) {
try {
return keyStore.entryInstanceOf(alias, entryClass);
} catch (KeyStoreException e) {
throw Throwables.propagate(e);
}
}
});
return ImmutableList.copyOf(results);
}
/**
* Loads an existing keystore from a file.
*
* @param keyStoreFile file to load from
* @param type keystore type
* @param password optional password to protect keys and keystore with
* @return the loaded keystore
* @throws IOException on I/O errors
*/
public static KeyStore loadKeyStore(File keyStoreFile, String type, String password)
throws IOException {
checkNotNull(keyStoreFile);
checkNotNull(type);
KeyStore keyStore;
try {
keyStore = KeyStore.getInstance(type);
} catch (KeyStoreException e) {
throw new RuntimeException("no provider exists for the keystore type " + type, e);
}
Closer closer = Closer.create();
InputStream stream = closer.register(new BufferedInputStream(new FileInputStream(keyStoreFile)));
try {
keyStore.load(stream, (password == null) ? null : password.toCharArray());
} catch (CertificateException e) {
throw new RuntimeException("some certificates could not be loaded", e);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("integrity check algorithm is unavailable", e);
} catch (IOException e) {
throw new RuntimeException("I/O error or a bad password", e);
} catch (Throwable e) { // Prescribed Closer pattern.
throw closer.rethrow(e);
} finally {
closer.close();
}
return keyStore;
}
}