nizhikov commented on code in PR #13786:
URL: https://github.com/apache/ignite/pull/13786#discussion_r4583590999


##########
modules/commons/src/main/java/org/apache/ignite/ssl/SslContextFactory.java:
##########
@@ -259,24 +330,80 @@ public static TrustManager getDisabledTrustManager() {
 
     /** {@inheritDoc} */
     @Override protected final KeyManager[] createKeyManagers() throws 
SSLException {
+        KeyManagerFactory keyMgrFactory;
+
         try {
-            KeyManagerFactory keyMgrFactory = 
KeyManagerFactory.getInstance(keyAlgorithm);
+            keyMgrFactory = KeyManagerFactory.getInstance(keyAlgorithm);
+        }
+        catch (NoSuchAlgorithmException e) {
+            throw new SSLException("Unsupported key algorithm: " + 
keyAlgorithm, e);
+        }
 
-            KeyStore keyStore = loadKeyStore(keyStoreType, keyStoreFilePath, 
keyStorePwd);
+        KeyStore keyStore = loadKeyStore(keyStoreType, keyStoreFilePath, 
keyStorePwd);
 
+        try {
             keyMgrFactory.init(keyStore, keyStorePwd);
 
+            pendingHotReloadTime = hotReloadEnabled ? hotReloadTime(keyStore) 
: 0;
+
             return keyMgrFactory.getKeyManagers();
         }
-        catch (NoSuchAlgorithmException e) {
-            throw new SSLException("Unsupported key algorithm: " + 
keyAlgorithm, e);
-        }
         catch (GeneralSecurityException e) {
             throw new SSLException("Failed to initialize Key Manager (security 
exception occurred) [type=" +
                 keyStoreType + ", keyStorePath=" + keyStoreFilePath + ']', e);
         }
     }
 
+    /**
+     * @param store Loaded key store.
+     * @return Start of the hot reload window of the chain that expires first.
+     * @throws GeneralSecurityException If a key or certificate cannot be read.
+     * @throws SSLException If there are no private keys or a private key has 
no X.509 certificate chain.
+     */
+    private long hotReloadTime(KeyStore store) throws 
GeneralSecurityException, SSLException {
+        long earliestExpiry = Long.MAX_VALUE;
+        long selectedStart = Long.MAX_VALUE;
+        boolean hasPrivateKeys = false;
+
+        for (Enumeration<String> aliases = store.aliases(); 
aliases.hasMoreElements(); ) {
+            String alias = aliases.nextElement();
+
+            if (!store.isKeyEntry(alias) || !(store.getKey(alias, keyStorePwd) 
instanceof PrivateKey))
+                continue;
+
+            hasPrivateKeys = true;
+
+            Certificate[] chain = store.getCertificateChain(alias);
+
+            if (chain == null || chain.length == 0)
+                throw new SSLException("Certificate chain is unavailable 
[alias=" + alias + "]. Remove the key or add its X.509 chain.");
+
+            long chainExpiry = Long.MAX_VALUE;
+
+            for (Certificate certificate : chain) {
+                if (!(certificate instanceof X509Certificate cert))
+                    throw new SSLException("Certificate chain is not X.509 
[alias=" + alias + "]. Remove the key or add its X.509 chain.");
+
+                chainExpiry = Math.min(chainExpiry, 
cert.getNotAfter().getTime());
+            }
+
+            long chainStart = 
((X509Certificate)chain[0]).getNotBefore().getTime();
+
+            if (chainExpiry < earliestExpiry || chainExpiry == earliestExpiry 
&& chainStart < selectedStart) {
+                earliestExpiry = chainExpiry;
+                selectedStart = chainStart;
+            }
+        }
+
+        if (!hasPrivateKeys)
+            throw new SSLException("Certificate hot reload requires a private 
key with an X.509 certificate chain.");
+
+        long time = earliestExpiry - (long)(Math.max(0, earliestExpiry - 
selectedStart) * HOT_RELOAD_WINDOW_FRACTION);

Review Comment:
   What if admin (script) miss hot reload time and update file right after time 
fires?
   Looks like with the current approach we just silently miss reload
   
   It seems with the file approach we can just check file modify time 
periodically (once in an hour?) and reload if something changed



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to