|
| 1 | +/* |
| 2 | + * Copyright 2025 LinkedIn Corp. Licensed under the BSD 2-Clause License (the "License").See License in the project root for license information. |
| 3 | + */ |
| 4 | + |
| 5 | +package com.linkedin.kafka.cruisecontrol.metricsreporter.utils; |
| 6 | + |
| 7 | +import com.linkedin.kafka.cruisecontrol.metricsreporter.utils.CCSslTestUtils.ConnectionMode; |
| 8 | +import org.apache.kafka.common.config.SslConfigs; |
| 9 | +import org.apache.kafka.common.config.types.Password; |
| 10 | +import org.bouncycastle.asn1.x500.X500Name; |
| 11 | +import javax.net.ssl.TrustManagerFactory; |
| 12 | +import java.io.File; |
| 13 | +import java.io.IOException; |
| 14 | +import java.security.GeneralSecurityException; |
| 15 | +import java.security.KeyPair; |
| 16 | +import java.security.cert.X509Certificate; |
| 17 | +import java.util.ArrayList; |
| 18 | +import java.util.HashMap; |
| 19 | +import java.util.List; |
| 20 | +import java.util.Locale; |
| 21 | +import java.util.Map; |
| 22 | + |
| 23 | +import static com.linkedin.kafka.cruisecontrol.metricsreporter.utils.CCSslTestUtils.createKeyStore; |
| 24 | +import static com.linkedin.kafka.cruisecontrol.metricsreporter.utils.CCSslTestUtils.createTrustStore; |
| 25 | +import static com.linkedin.kafka.cruisecontrol.metricsreporter.utils.CCSslTestUtils.generate; |
| 26 | +import static com.linkedin.kafka.cruisecontrol.metricsreporter.utils.CCSslTestUtils.generateKeyPair; |
| 27 | +import static com.linkedin.kafka.cruisecontrol.metricsreporter.utils.CCSslTestUtils.tempFile; |
| 28 | + |
| 29 | +public class CCSslConfigsBuilder { |
| 30 | + private final ConnectionMode _connectionMode; |
| 31 | + private String _tlsProtocol; |
| 32 | + private boolean _useClientCert; |
| 33 | + private boolean _createTrustStore; |
| 34 | + private File _trustStoreFile; |
| 35 | + private Password _trustStorePassword; |
| 36 | + private Password _keyStorePassword; |
| 37 | + private Password _keyPassword; |
| 38 | + private String _certAlias; |
| 39 | + private String _cn; |
| 40 | + private String _algorithm; |
| 41 | + |
| 42 | + public CCSslConfigsBuilder(ConnectionMode connectionMode) { |
| 43 | + this._connectionMode = connectionMode; |
| 44 | + this._tlsProtocol = SslConfigs.DEFAULT_SSL_PROTOCOL; |
| 45 | + this._trustStorePassword = new Password("TrustStorePassword"); |
| 46 | + this._keyStorePassword = connectionMode == CCSslTestUtils.ConnectionMode.SERVER ? new Password("ServerPassword") |
| 47 | + : new Password("ClientPassword"); |
| 48 | + this._keyPassword = this._keyStorePassword; |
| 49 | + this._cn = "localhost"; |
| 50 | + this._certAlias = connectionMode.name().toLowerCase(Locale.ROOT); |
| 51 | + this._algorithm = "RSA"; |
| 52 | + this._createTrustStore = true; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Set trust store file and create a new trust store to true. |
| 57 | + * |
| 58 | + * @param trustStoreFile Trust store file to set. |
| 59 | + * @return This. |
| 60 | + */ |
| 61 | + public CCSslConfigsBuilder createNewTrustStore(File trustStoreFile) { |
| 62 | + this._trustStoreFile = trustStoreFile; |
| 63 | + this._createTrustStore = true; |
| 64 | + return this; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Set trust store file and create a new trust store to false. |
| 69 | + * |
| 70 | + * @param trustStoreFile The existing trust store file to use. |
| 71 | + * @return This. |
| 72 | + */ |
| 73 | + public CCSslConfigsBuilder useExistingTrustStore(File trustStoreFile) { |
| 74 | + this._trustStoreFile = trustStoreFile; |
| 75 | + this._createTrustStore = false; |
| 76 | + return this; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Set use client cert. |
| 81 | + * |
| 82 | + * @param useClientCert Whether to use a client certificate. |
| 83 | + * @return This. |
| 84 | + */ |
| 85 | + public CCSslConfigsBuilder useClientCert(boolean useClientCert) { |
| 86 | + this._useClientCert = useClientCert; |
| 87 | + return this; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Set use cert alias. |
| 92 | + * |
| 93 | + * @param certAlias The alias for the certificate in the trust store. |
| 94 | + * @return This. |
| 95 | + */ |
| 96 | + public CCSslConfigsBuilder certAlias(String certAlias) { |
| 97 | + this._certAlias = certAlias; |
| 98 | + return this; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Set cn. |
| 103 | + * |
| 104 | + * @param cn The common name (CN) for the certificate. |
| 105 | + * @return This. |
| 106 | + */ |
| 107 | + public CCSslConfigsBuilder cn(String cn) { |
| 108 | + this._cn = cn; |
| 109 | + return this; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Builds the Java KeyStore (JKS)-based SSL configuration. |
| 114 | + * Depending on the connection mode (CLIENT or SERVER), this method generates the required |
| 115 | + * key pairs and self-signed certificates, creates keystores, and optionally creates a trust store. |
| 116 | + * |
| 117 | + * @return A map of SSL configuration properties suitable for use with Kafka or other TLS-based systems. |
| 118 | + * @throws IOException If file operations fail during keystore or trust store creation. |
| 119 | + * @throws GeneralSecurityException If a cryptographic error occurs while generating keys or certificates. |
| 120 | + */ |
| 121 | + public Map<String, Object> buildJks() throws IOException, GeneralSecurityException { |
| 122 | + Map<String, X509Certificate> certs = new HashMap<>(); |
| 123 | + File keyStoreFile = null; |
| 124 | + if (this._connectionMode == CCSslTestUtils.ConnectionMode.CLIENT && this._useClientCert) { |
| 125 | + keyStoreFile = tempFile("clientKS", ".jks"); |
| 126 | + KeyPair cKP = generateKeyPair(this._algorithm); |
| 127 | + X509Certificate cCert = generate(new X500Name("CN=" + _cn + ", O=A client"), cKP); |
| 128 | + createKeyStore(keyStoreFile.getPath(), this._keyStorePassword, this._keyPassword, "client", |
| 129 | + cKP.getPrivate(), cCert); |
| 130 | + certs.put(this._certAlias, cCert); |
| 131 | + } else if (this._connectionMode == CCSslTestUtils.ConnectionMode.SERVER) { |
| 132 | + keyStoreFile = tempFile("serverKS", ".jks"); |
| 133 | + KeyPair sKP = generateKeyPair(this._algorithm); |
| 134 | + X509Certificate sCert = generate(new X500Name("CN=" + _cn + ", O=A server"), sKP); |
| 135 | + createKeyStore(keyStoreFile.getPath(), this._keyStorePassword, this._keyPassword, "server", |
| 136 | + sKP.getPrivate(), sCert); |
| 137 | + certs.put(this._certAlias, sCert); |
| 138 | + keyStoreFile.deleteOnExit(); |
| 139 | + } |
| 140 | + |
| 141 | + if (this._createTrustStore) { |
| 142 | + createTrustStore(this._trustStoreFile.getPath(), this._trustStorePassword, certs); |
| 143 | + this._trustStoreFile.deleteOnExit(); |
| 144 | + } |
| 145 | + |
| 146 | + Map<String, Object> sslConfigs = new HashMap<>(); |
| 147 | + sslConfigs.put("ssl.protocol", this._tlsProtocol); |
| 148 | + if (this._connectionMode == CCSslTestUtils.ConnectionMode.SERVER || this._connectionMode == CCSslTestUtils.ConnectionMode.CLIENT |
| 149 | + && keyStoreFile != null) { |
| 150 | + sslConfigs.put("ssl.keystore.location", keyStoreFile.getPath()); |
| 151 | + sslConfigs.put("ssl.keystore.type", "JKS"); |
| 152 | + sslConfigs.put("ssl.keymanager.algorithm", TrustManagerFactory.getDefaultAlgorithm()); |
| 153 | + sslConfigs.put("ssl.keystore.password", this._keyStorePassword); |
| 154 | + sslConfigs.put("ssl.key.password", this._keyPassword); |
| 155 | + } |
| 156 | + |
| 157 | + sslConfigs.put("ssl.truststore.location", this._trustStoreFile.getPath()); |
| 158 | + sslConfigs.put("ssl.truststore.password", this._trustStorePassword); |
| 159 | + sslConfigs.put("ssl.truststore.type", "JKS"); |
| 160 | + sslConfigs.put("ssl.trustmanager.algorithm", TrustManagerFactory.getDefaultAlgorithm()); |
| 161 | + List<String> enabledProtocols = new ArrayList<>(); |
| 162 | + enabledProtocols.add(this._tlsProtocol); |
| 163 | + sslConfigs.put("ssl.enabled.protocols", enabledProtocols); |
| 164 | + return sslConfigs; |
| 165 | + } |
| 166 | +} |
0 commit comments