[
https://issues.apache.org/jira/browse/GEODE-9017?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17299098#comment-17299098
]
ASF GitHub Bot commented on GEODE-9017:
---------------------------------------
aaronlindsey commented on a change in pull request #6106:
URL: https://github.com/apache/geode/pull/6106#discussion_r591814680
##########
File path:
geode-core/src/main/java/org/apache/geode/internal/net/filewatch/FileWatchingX509ExtendedKeyManager.java
##########
@@ -0,0 +1,137 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
contributor license
+ * agreements. See the NOTICE file distributed with this work for additional
information regarding
+ * copyright ownership. The ASF licenses this file to You 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 org.apache.geode.internal.net.filewatch;
+
+import static java.util.concurrent.Executors.newSingleThreadExecutor;
+
+import java.net.Socket;
+import java.nio.file.Path;
+import java.security.Principal;
+import java.security.PrivateKey;
+import java.security.cert.X509Certificate;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.atomic.AtomicReference;
+
+import javax.net.ssl.KeyManager;
+import javax.net.ssl.SSLEngine;
+import javax.net.ssl.X509ExtendedKeyManager;
+
+import org.apache.logging.log4j.Logger;
+
+import org.apache.geode.annotations.VisibleForTesting;
+import org.apache.geode.logging.internal.log4j.api.LogService;
+
+/**
+ * Watches a key store file and updates the underlying key manager when the
file is changed.
+ */
+public final class FileWatchingX509ExtendedKeyManager extends
X509ExtendedKeyManager {
+
+ private static final Logger logger = LogService.getLogger();
+ private static final ConcurrentHashMap<Path,
FileWatchingX509ExtendedKeyManager> instances =
+ new ConcurrentHashMap<>();
+
+ private final AtomicReference<X509ExtendedKeyManager> keyManager = new
AtomicReference<>();
+ private final Path keyStorePath;
+ private final ThrowingSupplier<KeyManager[]> keyManagerSupplier;
+
+ @VisibleForTesting
+ FileWatchingX509ExtendedKeyManager(Path keystorePath,
+ ThrowingSupplier<KeyManager[]> keyManagerSupplier,
+ ExecutorService executor) {
+ this.keyStorePath = keystorePath;
+ this.keyManagerSupplier = keyManagerSupplier;
+
+ loadKeyManager();
+
+ executor.submit(new FileWatcher(this.keyStorePath, this::loadKeyManager));
+ }
+
+ /**
+ * Returns a {@link FileWatchingX509ExtendedKeyManager} for the given path.
A new instance will be
+ * created only if one does not already exist for that path.
+ *
+ * @param path The path to the key store file
+ * @param supplier A supplier which returns an {@link X509ExtendedKeyManager}
+ */
+ public static FileWatchingX509ExtendedKeyManager forPath(Path path,
+ ThrowingSupplier<KeyManager[]> supplier) {
+ return instances.computeIfAbsent(path,
+ (Path p) -> new FileWatchingX509ExtendedKeyManager(p, supplier,
newSingleThreadExecutor()));
+ }
+
+ @Override
+ public String chooseClientAlias(String[] strings, Principal[] principals,
Socket socket) {
+ return keyManager.get().chooseClientAlias(strings, principals, socket);
+ }
+
+ @Override
+ public String chooseEngineClientAlias(String[] strings, Principal[]
principals,
+ SSLEngine sslEngine) {
+ return keyManager.get().chooseEngineClientAlias(strings, principals,
sslEngine);
+ }
+
+ @Override
+ public String chooseEngineServerAlias(String s, Principal[] principals,
SSLEngine sslEngine) {
+ return keyManager.get().chooseEngineServerAlias(s, principals, sslEngine);
+ }
+
+ @Override
+ public String chooseServerAlias(String s, Principal[] principals, Socket
socket) {
+ return keyManager.get().chooseServerAlias(s, principals, socket);
+ }
+
+ @Override
+ public X509Certificate[] getCertificateChain(String s) {
+ return keyManager.get().getCertificateChain(s);
+ }
+
+ @Override
+ public String[] getClientAliases(String s, Principal[] principals) {
+ return keyManager.get().getClientAliases(s, principals);
+ }
+
+ @Override
+ public PrivateKey getPrivateKey(String s) {
+ return keyManager.get().getPrivateKey(s);
+ }
+
+ @Override
+ public String[] getServerAliases(String s, Principal[] principals) {
+ return keyManager.get().getServerAliases(s, principals);
+ }
+
+ private void loadKeyManager() {
+ KeyManager[] keyManagers;
+ try {
+ keyManagers = keyManagerSupplier.get();
+ } catch (Throwable t) {
+ throw new RuntimeException("Unable to load KeyManager", t);
+ }
Review comment:
Thanks! I will change this.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
> Reload key store and trust store upon change
> --------------------------------------------
>
> Key: GEODE-9017
> URL: https://issues.apache.org/jira/browse/GEODE-9017
> Project: Geode
> Issue Type: New Feature
> Reporter: Aaron Lindsey
> Assignee: Aaron Lindsey
> Priority: Major
> Labels: pull-request-available
>
> [Link to
> RFC|https://cwiki.apache.org/confluence/display/GEODE/Make+key+and+trust+stores+reload+automatically+upon+change]
> (The below text is copied from the RFC document.)
> h3. Problem
> Currently, in order to rotate certificates each member of the cluster needs
> to be restarted to load new certs and trust. It would be preferable if
> certificates can be rotated without having to restart members.
> h3. Solution
> When starting up a cluster member we currently read the TLS configuration
> which, when TLS is enabled has key and trust store files defined. In case
> those files are defined they are read, and the information inside them is
> loaded into the key and trust manager objects that are loaded into the
> SSLContext.
> This solution will introduce wrapper objects for the key and trust managers
> and file/directory watcher(s) that can detect changes to the key and trust
> store files. When key and trust store files are changed this will trigger a
> reload into key and trust managers and through the wrapper objects these new
> key and trust managers will be injected into the SSLContext so that the
> context can start using the new key and trust managers in process.
--
This message was sent by Atlassian Jira
(v8.3.4#803005)