GeezerVison is showing github.com — util: update AdvancedTlsX509KeyManager to support key alias for reloa… · kannanjgithub/grpc-java@d2760cc
File treeExpand file treeCollapse file tree Open diff view settings netty/src/test/java/io/grpc/netty
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: netty/src/test/java/io/grpc/netty/AdvancedTlsTest.java +6-9Lines changed: 6 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -436,16 +436,13 @@ public void onFileReloadingTrustManagerBadInitialContentTest() throws Exception | 436 | 436 | } | 437 | 437 |
| 438 | 438 | @Test | 439 | | - public void keyManagerAliasesTest() { | | 439 | + public void keyManagerAliasesTest() throws Exception { | 440 | 440 | AdvancedTlsX509KeyManager km = new AdvancedTlsX509KeyManager(); | 441 | | - assertArrayEquals( | 442 | | - new String[] {"default"}, km.getClientAliases("", null)); | 443 | | - assertEquals( | 444 | | - "default", km.chooseClientAlias(new String[] {"default"}, null, null)); | 445 | | - assertArrayEquals( | 446 | | - new String[] {"default"}, km.getServerAliases("", null)); | 447 | | - assertEquals( | 448 | | - "default", km.chooseServerAlias("default", null, null)); | | 441 | + km.updateIdentityCredentials(serverCert0, serverKey0); | | 442 | + assertArrayEquals(new String[] {"key-1"}, km.getClientAliases("", null)); | | 443 | + assertEquals("key-1", km.chooseClientAlias(new String[] {"key-1"}, null, null)); | | 444 | + assertArrayEquals(new String[] {"key-1"}, km.getServerAliases("", null)); | | 445 | + assertEquals("key-1", km.chooseServerAlias("key-1", null, null)); | 449 | 446 | } | 450 | 447 |
| 451 | 448 | @Test | |
Collapse file Copy file name to clipboardExpand all lines: util/src/main/java/io/grpc/util/AdvancedTlsX509KeyManager.java +57-16Lines changed: 57 additions & 16 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 32 | 32 | import java.util.concurrent.ScheduledExecutorService; | 33 | 33 | import java.util.concurrent.ScheduledFuture; | 34 | 34 | import java.util.concurrent.TimeUnit; | | 35 | +import java.util.concurrent.atomic.AtomicInteger; | 35 | 36 | import java.util.logging.Level; | 36 | 37 | import java.util.logging.Logger; | 37 | 38 | import javax.net.ssl.SSLEngine; | | 40 | 41 | /** | 41 | 42 | * AdvancedTlsX509KeyManager is an {@code X509ExtendedKeyManager} that allows users to configure | 42 | 43 | * advanced TLS features, such as private key and certificate chain reloading. | | 44 | + * | | 45 | + * <p>The alias increments on every credential load (e.g. {@code "key-1"}, {@code "key-2"}, ...), | | 46 | + * so the same alias always maps to the same key material. The previous alias is retained for one | | 47 | + * rotation to allow in-progress handshakes to complete, ensuring alias-to-key-material consistency | | 48 | + * across credential reloads. | 43 | 49 | */ | 44 | 50 | public final class AdvancedTlsX509KeyManager extends X509ExtendedKeyManager { | 45 | 51 | private static final Logger log = Logger.getLogger(AdvancedTlsX509KeyManager.class.getName()); | 46 | 52 | // Minimum allowed period for refreshing files with credential information. | 47 | | - private static final int MINIMUM_REFRESH_PERIOD_IN_MINUTES = 1 ; | 48 | | - // The credential information to be sent to peers to prove our identity. | 49 | | - private volatile KeyInfo keyInfo; | | 53 | + private static final int MINIMUM_REFRESH_PERIOD_IN_MINUTES = 1; | | 54 | + // Prefix for the key material alias; revision counter appended on each credential load. | | 55 | + static final String ALIAS_PREFIX = "key-"; | | 56 | + | | 57 | + private final AtomicInteger revision = new AtomicInteger(0); | | 58 | + // Snapshot of current and previous KeyInfo; previous is retained for in-progress handshakes | | 59 | + // after one rotation. | | 60 | + private volatile KeyInfoSnapshot snapshot = new KeyInfoSnapshot(null, null); | | 61 | + | | 62 | + public AdvancedTlsX509KeyManager() {} | | 63 | + | | 64 | + private String alias() { | | 65 | + KeyInfo curr = this.snapshot.current; | | 66 | + return curr != null ? curr.alias : null; | | 67 | + } | 50 | 68 |
| 51 | 69 | @Override | 52 | 70 | public PrivateKey getPrivateKey(String alias) { | 53 | | - if (alias.equals("default")) { | 54 | | - return this.keyInfo.key; | | 71 | + KeyInfoSnapshot snap = this.snapshot; | | 72 | + if (snap.current != null && snap.current.alias.equals(alias)) { | | 73 | + return snap.current.key; | | 74 | + } | | 75 | + if (snap.previous != null && snap.previous.alias.equals(alias)) { | | 76 | + return snap.previous.key; | 55 | 77 | } | 56 | 78 | return null; | 57 | 79 | } | 58 | 80 |
| 59 | 81 | @Override | 60 | 82 | public X509Certificate[] getCertificateChain(String alias) { | 61 | | - if (alias.equals("default")) { | 62 | | - return Arrays.copyOf(this.keyInfo.certs, this.keyInfo.certs.length); | | 83 | + KeyInfoSnapshot snap = this.snapshot; | | 84 | + if (snap.current != null && snap.current.alias.equals(alias)) { | | 85 | + return Arrays.copyOf(snap.current.certs, snap.current.certs.length); | | 86 | + } | | 87 | + if (snap.previous != null && snap.previous.alias.equals(alias)) { | | 88 | + return Arrays.copyOf(snap.previous.certs, snap.previous.certs.length); | 63 | 89 | } | 64 | 90 | return null; | 65 | 91 | } | 66 | 92 |
| 67 | 93 | @Override | 68 | 94 | public String[] getClientAliases(String keyType, Principal[] issuers) { | 69 | | - return new String[] {"default"}; | | 95 | + String alias = alias(); | | 96 | + return alias != null ? new String[] {alias} : null; | 70 | 97 | } | 71 | 98 |
| 72 | 99 | @Override | 73 | 100 | public String chooseClientAlias(String[] keyType, Principal[] issuers, Socket socket) { | 74 | | - return "default"; | | 101 | + return alias(); | 75 | 102 | } | 76 | 103 |
| 77 | 104 | @Override | 78 | 105 | public String chooseEngineClientAlias(String[] keyType, Principal[] issuers, SSLEngine engine) { | 79 | | - return "default"; | | 106 | + return alias(); | 80 | 107 | } | 81 | 108 |
| 82 | 109 | @Override | 83 | 110 | public String[] getServerAliases(String keyType, Principal[] issuers) { | 84 | | - return new String[] {"default"}; | | 111 | + String alias = alias(); | | 112 | + return alias != null ? new String[] {alias} : null; | 85 | 113 | } | 86 | 114 |
| 87 | 115 | @Override | 88 | 116 | public String chooseServerAlias(String keyType, Principal[] issuers, Socket socket) { | 89 | | - return "default"; | | 117 | + return alias(); | 90 | 118 | } | 91 | 119 |
| 92 | 120 | @Override | 93 | 121 | public String chooseEngineServerAlias(String keyType, Principal[] issuers, | 94 | 122 | SSLEngine engine) { | 95 | | - return "default"; | | 123 | + return alias(); | 96 | 124 | } | 97 | 125 |
| 98 | 126 | /** | @@ -116,7 +144,9 @@ public void updateIdentityCredentials(PrivateKey key, X509Certificate[] certs) { | 116 | 144 | * @param key the private key that is going to be used | 117 | 145 | */ | 118 | 146 | public void updateIdentityCredentials(X509Certificate[] certs, PrivateKey key) { | 119 | | - this.keyInfo = new KeyInfo(checkNotNull(certs, "certs"), checkNotNull(key, "key")); | | 147 | + KeyInfo newInfo = new KeyInfo(checkNotNull(certs, "certs"), checkNotNull(key, "key"), | | 148 | + ALIAS_PREFIX + revision.incrementAndGet()); | | 149 | + this.snapshot = new KeyInfoSnapshot(newInfo, this.snapshot.current); | 120 | 150 | } | 121 | 151 |
| 122 | 152 | /** | @@ -218,10 +248,22 @@ private static class KeyInfo { | 218 | 248 | // The private key and the cert chain we will use to send to peers to prove our identity. | 219 | 249 | final X509Certificate[] certs; | 220 | 250 | final PrivateKey key; | | 251 | + final String alias; | 221 | 252 |
| 222 | | - public KeyInfo(X509Certificate[] certs, PrivateKey key) { | | 253 | + public KeyInfo(X509Certificate[] certs, PrivateKey key, String alias) { | 223 | 254 | this.certs = certs; | 224 | 255 | this.key = key; | | 256 | + this.alias = alias; | | 257 | + } | | 258 | + } | | 259 | + | | 260 | + private static class KeyInfoSnapshot { | | 261 | + final KeyInfo current; | | 262 | + final KeyInfo previous; | | 263 | + | | 264 | + KeyInfoSnapshot(KeyInfo current, KeyInfo previous) { | | 265 | + this.current = current; | | 266 | + this.previous = previous; | 225 | 267 | } | 226 | 268 | } | 227 | 269 |
| @@ -309,4 +351,3 @@ public interface Closeable extends java.io.Closeable { | 309 | 351 | void close(); | 310 | 352 | } | 311 | 353 | } | 312 | | - | |
Collapse file Copy file name to clipboardExpand all lines: util/src/test/java/io/grpc/util/AdvancedTlsX509KeyManagerTest.java +51-11Lines changed: 51 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 18 | 18 |
| 19 | 19 | import static org.junit.Assert.assertArrayEquals; | 20 | 20 | import static org.junit.Assert.assertEquals; | | 21 | +import static org.junit.Assert.assertNull; | 21 | 22 | import static org.junit.Assert.assertThrows; | 22 | 23 | import static org.junit.Assert.assertTrue; | 23 | 24 | import static org.junit.Assert.fail; | @@ -48,7 +49,6 @@ public class AdvancedTlsX509KeyManagerTest { | 48 | 49 | private static final String SERVER_0_PEM_FILE = "server0.pem"; | 49 | 50 | private static final String CLIENT_0_KEY_FILE = "client.key"; | 50 | 51 | private static final String CLIENT_0_PEM_FILE = "client.pem"; | 51 | | - private static final String ALIAS = "default"; | 52 | 52 |
| 53 | 53 | private ScheduledExecutorService executor; | 54 | 54 |
| @@ -79,22 +79,62 @@ public void setUp() throws Exception { | 79 | 79 | public void updateTrustCredentials_replacesIssuers() throws Exception { | 80 | 80 | // Overall happy path checking of public API. | 81 | 81 | AdvancedTlsX509KeyManager serverKeyManager = new AdvancedTlsX509KeyManager(); | | 82 | + | 82 | 83 | serverKeyManager.updateIdentityCredentials(serverCert0, serverKey0); | 83 | | - assertEquals(serverKey0, serverKeyManager.getPrivateKey(ALIAS)); | 84 | | - assertArrayEquals(serverCert0, serverKeyManager.getCertificateChain(ALIAS)); | | 84 | + String alias1 = serverKeyManager.chooseEngineServerAlias(null, null, null); | | 85 | + assertEquals(AdvancedTlsX509KeyManager.ALIAS_PREFIX + "1", alias1); | | 86 | + assertEquals(serverKey0, serverKeyManager.getPrivateKey(alias1)); | | 87 | + assertArrayEquals(serverCert0, serverKeyManager.getCertificateChain(alias1)); | 85 | 88 |
| 86 | 89 | serverKeyManager.updateIdentityCredentials(clientCert0File, clientKey0File); | 87 | | - assertEquals(clientKey0, serverKeyManager.getPrivateKey(ALIAS)); | 88 | | - assertArrayEquals(clientCert0, serverKeyManager.getCertificateChain(ALIAS)); | 89 | | - | 90 | | - serverKeyManager.updateIdentityCredentials(serverCert0File, serverKey0File,1, | | 90 | + String alias2 = serverKeyManager.chooseEngineServerAlias(null, null, null); | | 91 | + assertEquals(AdvancedTlsX509KeyManager.ALIAS_PREFIX + "2", alias2); | | 92 | + assertEquals(clientKey0, serverKeyManager.getPrivateKey(alias2)); | | 93 | + assertArrayEquals(clientCert0, serverKeyManager.getCertificateChain(alias2)); | | 94 | + // Previous alias still resolves — retained to allow in-progress handshakes to complete. | | 95 | + assertEquals(serverKey0, serverKeyManager.getPrivateKey(alias1)); | | 96 | + assertArrayEquals(serverCert0, serverKeyManager.getCertificateChain(alias1)); | | 97 | + | | 98 | + serverKeyManager.updateIdentityCredentials(serverCert0File, serverKey0File, 1, | 91 | 99 | TimeUnit.MINUTES, executor); | 92 | | - assertEquals(serverKey0, serverKeyManager.getPrivateKey(ALIAS)); | 93 | | - assertArrayEquals(serverCert0, serverKeyManager.getCertificateChain(ALIAS)); | | 100 | + String alias3 = serverKeyManager.chooseEngineServerAlias(null, null, null); | | 101 | + assertEquals(serverKey0, serverKeyManager.getPrivateKey(alias3)); | | 102 | + assertArrayEquals(serverCert0, serverKeyManager.getCertificateChain(alias3)); | | 103 | + // alias1 is now two rotations back — no longer retained. | | 104 | + assertNull(serverKeyManager.getPrivateKey(alias1)); | 94 | 105 |
| 95 | 106 | serverKeyManager.updateIdentityCredentials(serverCert0, serverKey0); | 96 | | - assertEquals(serverKey0, serverKeyManager.getPrivateKey(ALIAS)); | 97 | | - assertArrayEquals(serverCert0, serverKeyManager.getCertificateChain(ALIAS)); | | 107 | + String alias4 = serverKeyManager.chooseEngineServerAlias(null, null, null); | | 108 | + assertEquals(serverKey0, serverKeyManager.getPrivateKey(alias4)); | | 109 | + assertArrayEquals(serverCert0, serverKeyManager.getCertificateChain(alias4)); | | 110 | + } | | 111 | + | | 112 | + @Test | | 113 | + public void allAliasMethods_returnNullBeforeCredentialsLoaded() { | | 114 | + AdvancedTlsX509KeyManager keyManager = new AdvancedTlsX509KeyManager(); | | 115 | + | | 116 | + assertNull(keyManager.chooseClientAlias(null, null, null)); | | 117 | + assertNull(keyManager.chooseServerAlias(null, null, null)); | | 118 | + assertNull(keyManager.chooseEngineClientAlias(null, null, null)); | | 119 | + assertNull(keyManager.chooseEngineServerAlias(null, null, null)); | | 120 | + assertNull(keyManager.getClientAliases(null, null)); | | 121 | + assertNull(keyManager.getServerAliases(null, null)); | | 122 | + assertNull(keyManager.getPrivateKey("key-1")); | | 123 | + assertNull(keyManager.getCertificateChain("key-1")); | | 124 | + } | | 125 | + | | 126 | + @Test | | 127 | + public void allAliasMethods_agreeAfterCredentialLoad() throws Exception { | | 128 | + AdvancedTlsX509KeyManager keyManager = new AdvancedTlsX509KeyManager(); | | 129 | + keyManager.updateIdentityCredentials(serverCert0, serverKey0); | | 130 | + | | 131 | + String expectedAlias = AdvancedTlsX509KeyManager.ALIAS_PREFIX + "1"; | | 132 | + assertEquals(expectedAlias, keyManager.chooseClientAlias(null, null, null)); | | 133 | + assertEquals(expectedAlias, keyManager.chooseServerAlias(null, null, null)); | | 134 | + assertEquals(expectedAlias, keyManager.chooseEngineClientAlias(null, null, null)); | | 135 | + assertEquals(expectedAlias, keyManager.chooseEngineServerAlias(null, null, null)); | | 136 | + assertArrayEquals(new String[]{expectedAlias}, keyManager.getClientAliases(null, null)); | | 137 | + assertArrayEquals(new String[]{expectedAlias}, keyManager.getServerAliases(null, null)); | 98 | 138 | } | 99 | 139 |
| 100 | 140 | @Test | |
|