Skip to content

Commit 5a8a7e7

Browse files
committed
Changes to support selection of appropriate alias from a keystore with multiple alias entries
1 parent e94c54d commit 5a8a7e7

File tree

4 files changed

+17
-78
lines changed

4 files changed

+17
-78
lines changed

clients/src/main/java/org/apache/kafka/common/config/SslConfigs.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,6 @@ public class SslConfigs {
9999
+ "This is optional for client and only needed if 'ssl.keystore.location' is configured. "
100100
+ "Key store password is not supported for PEM format.";
101101

102-
public static final String SSL_KEYSTORE_AS_STRING = "ssl.keystore.as.string";
103-
public static final String SSL_KEYSTORE_AS_STRING_DOC = "True when using a base64 encoded keystore string";
104-
105-
public static final String SSL_TRUSTSTORE_AS_STRING = "ssl.truststore.as.string";
106-
public static final String SSL_TRUSTSTORE_AS_STRING_DOC = "True when using a base64 encoded truststore string";
107-
108102
public static final String SSL_KEY_PASSWORD_CONFIG = "ssl.key.password";
109103
public static final String SSL_KEY_PASSWORD_DOC = "The password of the private key in the key store file or "
110104
+ "the PEM key specified in 'ssl.keystore.key'.";
@@ -166,9 +160,7 @@ public static void addClientSslSupport(ConfigDef config) {
166160
.define(SslConfigs.SSL_TRUSTMANAGER_ALGORITHM_CONFIG, ConfigDef.Type.STRING, SslConfigs.DEFAULT_SSL_TRUSTMANAGER_ALGORITHM, ConfigDef.Importance.LOW, SslConfigs.SSL_TRUSTMANAGER_ALGORITHM_DOC)
167161
.define(SslConfigs.SSL_ENDPOINT_IDENTIFICATION_ALGORITHM_CONFIG, ConfigDef.Type.STRING, SslConfigs.DEFAULT_SSL_ENDPOINT_IDENTIFICATION_ALGORITHM, ConfigDef.Importance.LOW, SslConfigs.SSL_ENDPOINT_IDENTIFICATION_ALGORITHM_DOC)
168162
.define(SslConfigs.SSL_SECURE_RANDOM_IMPLEMENTATION_CONFIG, ConfigDef.Type.STRING, null, ConfigDef.Importance.LOW, SslConfigs.SSL_SECURE_RANDOM_IMPLEMENTATION_DOC)
169-
.define(SslConfigs.SSL_ENGINE_FACTORY_CLASS_CONFIG, ConfigDef.Type.CLASS, null, ConfigDef.Importance.LOW, SslConfigs.SSL_ENGINE_FACTORY_CLASS_DOC)
170-
.define(SslConfigs.SSL_KEYSTORE_AS_STRING, ConfigDef.Type.STRING, null, ConfigDef.Importance.LOW,SslConfigs.SSL_KEYSTORE_AS_STRING_DOC)
171-
.define(SslConfigs.SSL_TRUSTSTORE_AS_STRING, ConfigDef.Type.STRING, null, ConfigDef.Importance.LOW,SslConfigs.SSL_TRUSTSTORE_AS_STRING_DOC);
163+
.define(SslConfigs.SSL_ENGINE_FACTORY_CLASS_CONFIG, ConfigDef.Type.CLASS, null, ConfigDef.Importance.LOW, SslConfigs.SSL_ENGINE_FACTORY_CLASS_DOC);
172164
}
173165

174166
public static final Set<String> RECONFIGURABLE_CONFIGS = Utils.mkSet(

clients/src/main/java/org/apache/kafka/common/security/ssl/DefaultSslEngineFactory.java

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -190,14 +190,12 @@ public void configure(Map<String, ?> configs) {
190190
(Password) configs.get(SslConfigs.SSL_KEYSTORE_PASSWORD_CONFIG),
191191
(Password) configs.get(SslConfigs.SSL_KEY_PASSWORD_CONFIG),
192192
(Password) configs.get(SslConfigs.SSL_KEYSTORE_KEY_CONFIG),
193-
(Password) configs.get(SslConfigs.SSL_KEYSTORE_CERTIFICATE_CHAIN_CONFIG)),
194-
Boolean.parseBoolean((String) configs.get(SslConfigs.SSL_KEYSTORE_AS_STRING)));
193+
(Password) configs.get(SslConfigs.SSL_KEYSTORE_CERTIFICATE_CHAIN_CONFIG));
195194

196195
this.truststore = createTruststore((String) configs.get(SslConfigs.SSL_TRUSTSTORE_TYPE_CONFIG),
197196
(String) configs.get(SslConfigs.SSL_TRUSTSTORE_LOCATION_CONFIG),
198197
(Password) configs.get(SslConfigs.SSL_TRUSTSTORE_PASSWORD_CONFIG),
199-
(Password) configs.get(SslConfigs.SSL_TRUSTSTORE_CERTIFICATES_CONFIG)),
200-
Boolean.parseBoolean((String) configs.get(SslConfigs.SSL_TRUSTSTORE_AS_STRING)));
198+
(Password) configs.get(SslConfigs.SSL_TRUSTSTORE_CERTIFICATES_CONFIG));
201199

202200
this.sslContext = createSSLContext(keystore, truststore, configs);
203201
}
@@ -360,7 +358,7 @@ protected TrustManager[] getTrustManagers(SecurityStore truststore, String tmfAl
360358
}
361359

362360
// Visibility to override for testing
363-
protected SecurityStore createKeystore(String type, String path, Password password, Password keyPassword, Password privateKey, Password certificateChain, boolean pathAsBase64EncodedString) {
361+
protected SecurityStore createKeystore(String type, String path, Password password, Password keyPassword, Password privateKey, Password certificateChain) {
364362
if (privateKey != null) {
365363
if (!PEM_TYPE.equals(type))
366364
throw new InvalidConfigurationException("SSL private key can be specified only for PEM, but key store type is " + type + ".");
@@ -384,12 +382,12 @@ else if (password != null)
384382
} else if (path != null && password == null) {
385383
throw new InvalidConfigurationException("SSL key store is specified, but key store password is not specified.");
386384
} else if (path != null && password != null) {
387-
return new FileBasedStore(type, path, password, keyPassword, true, pathAsBase64EncodedString);
385+
return new FileBasedStore(type, path, password, keyPassword, true);
388386
} else
389387
return null; // path == null, clients may use this path with brokers that don't require client auth
390388
}
391389

392-
private static SecurityStore createTruststore(String type, String path, Password password, Password trustStoreCerts, boolean pathAsBase64EncodedString) {
390+
private static SecurityStore createTruststore(String type, String path, Password password, Password trustStoreCerts) {
393391
if (trustStoreCerts != null) {
394392
if (!PEM_TYPE.equals(type))
395393
throw new InvalidConfigurationException("SSL trust store certs can be specified only for PEM, but trust store type is " + type + ".");
@@ -407,7 +405,7 @@ else if (password != null)
407405
} else if (path == null && password != null) {
408406
throw new InvalidConfigurationException("SSL trust store is not specified, but trust store password is specified.");
409407
} else if (path != null) {
410-
return new FileBasedStore(type, path, password, null, false, pathAsBase64EncodedString);
408+
return new FileBasedStore(type, path, password, null, false);
411409
} else
412410
return null;
413411
}
@@ -428,15 +426,14 @@ static class FileBasedStore implements SecurityStore {
428426
private final KeyStore keyStore;
429427
private final boolean pathAsBase64EncodedString;
430428

431-
FileBasedStore(String type, String path, Password password, Password keyPassword, boolean isKeyStore, boolean pathAsBase64EncodedString) {
429+
FileBasedStore(String type, String path, Password password, Password keyPassword, boolean isKeyStore) {
432430
Objects.requireNonNull(type, "type must not be null");
433431
this.type = type;
434432
this.path = path;
435433
this.password = password;
436434
this.keyPassword = keyPassword;
437435
fileLastModifiedMs = lastModifiedMs(path);
438436
this.keyStore = load(isKeyStore);
439-
this.pathAsBase64EncodedString = pathAsBase64EncodedString;
440437
}
441438

442439
@Override
@@ -457,28 +454,15 @@ public char[] keyPassword() {
457454
* using the specified configs (e.g. if the password or keystore type is invalid)
458455
*/
459456
protected KeyStore load(boolean isKeyStore) {
460-
if (path == null) {
461-
throw new KafkaException("Failed to load SSL keystore: path was null");
462-
}
463-
InputStream in;
464-
try {
465-
if (pathAsBase64EncodedString) {
466-
String encodedKeyStore = System.getenv(path);
467-
in = new ByteArrayInputStream(Base64.decoder().decode(encodedKeyStore));
468-
} else if (type.equalsIgnoreCase(TruststoreUtility.CRT)) {
469-
return TruststoreUtility.createTrustStore(path, password.value());
470-
} else {
471-
in = new FileInputStream(path);
457+
try (InputStream in = Files.newInputStream(Paths.get(path))) {
458+
KeyStore ks = KeyStore.getInstance(type);
459+
// If a password is not set access to the truststore is still available, but integrity checking is disabled.
460+
char[] passwordChars = password != null ? password.value().toCharArray() : null;
461+
ks.load(in, passwordChars);
462+
return ks;
463+
} catch (GeneralSecurityException | IOException e) {
464+
throw new KafkaException("Failed to load SSL keystore " + path + " of type " + type, e);
472465
}
473-
KeyStore ks = KeyStore.getInstance(type);
474-
// If a password is not set access to the truststore is still available, but integrity checking is disabled.
475-
char[] passwordChars = password != null ? password.value().toCharArray() : null;
476-
ks.load(in, passwordChars);
477-
in.close();
478-
return ks;
479-
} catch (GeneralSecurityException | IOException e) {
480-
throw new KafkaException("Failed to load SSL keystore " + path + " of type " + type, e);
481-
}
482466
}
483467

484468
private Long lastModifiedMs(String path) {

clients/src/main/java/org/apache/kafka/common/security/ssl/TruststoreUtility.java

Lines changed: 0 additions & 36 deletions
This file was deleted.

clients/src/test/java/org/apache/kafka/common/security/ssl/SslFactoryTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -591,8 +591,7 @@ private KeyStore sslKeyStore(Map<String, Object> sslConfig) {
591591
(String) sslConfig.get(SslConfigs.SSL_KEYSTORE_LOCATION_CONFIG),
592592
(Password) sslConfig.get(SslConfigs.SSL_KEYSTORE_PASSWORD_CONFIG),
593593
(Password) sslConfig.get(SslConfigs.SSL_KEY_PASSWORD_CONFIG),
594-
true,
595-
Boolean.parseBoolean((String) sslConfig.get(SslConfigs.SSL_KEYSTORE_AS_STRING))
594+
true
596595
);
597596
} else {
598597
store = new PemStore(

0 commit comments

Comments
 (0)