Skip to content

Commit 9c76ed7

Browse files
committed
This change adds 2 optional configuration properties that can be set to true when the keystore location string is actually a base64 encoded keystore string used in the PCF environments.
1 parent 3f3b070 commit 9c76ed7

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ public class SslConfigs {
9494
+ "This is optional for client and only needed if 'ssl.keystore.location' is configured. "
9595
+ "Key store password is not supported for PEM format.";
9696

97+
public static final String SSL_KEYSTORE_AS_STRING = "ssl.keystore.as.string";
98+
public static final String SSL_KEYSTORE_AS_STRING_DOC = "True when using a base64 encoded keystore string";
99+
100+
public static final String SSL_TRUSTSTORE_AS_STRING = "ssl.truststore.as.string";
101+
public static final String SSL_TRUSTSTORE_AS_STRING_DOC = "True when using a base64 encoded truststore string";
102+
97103
public static final String SSL_KEY_PASSWORD_CONFIG = "ssl.key.password";
98104
public static final String SSL_KEY_PASSWORD_DOC = "The password of the private key in the key store file or "
99105
+ "the PEM key specified in 'ssl.keystore.key'.";
@@ -154,7 +160,9 @@ public static void addClientSslSupport(ConfigDef config) {
154160
.define(SslConfigs.SSL_TRUSTMANAGER_ALGORITHM_CONFIG, ConfigDef.Type.STRING, SslConfigs.DEFAULT_SSL_TRUSTMANAGER_ALGORITHM, ConfigDef.Importance.LOW, SslConfigs.SSL_TRUSTMANAGER_ALGORITHM_DOC)
155161
.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)
156162
.define(SslConfigs.SSL_SECURE_RANDOM_IMPLEMENTATION_CONFIG, ConfigDef.Type.STRING, null, ConfigDef.Importance.LOW, SslConfigs.SSL_SECURE_RANDOM_IMPLEMENTATION_DOC)
157-
.define(SslConfigs.SSL_ENGINE_FACTORY_CLASS_CONFIG, ConfigDef.Type.CLASS, null, ConfigDef.Importance.LOW, SslConfigs.SSL_ENGINE_FACTORY_CLASS_DOC);
163+
.define(SslConfigs.SSL_ENGINE_FACTORY_CLASS_CONFIG, ConfigDef.Type.CLASS, null, ConfigDef.Importance.LOW, SslConfigs.SSL_ENGINE_FACTORY_CLASS_DOC)
164+
.define(SslConfigs.SSL_KEYSTORE_AS_STRING, ConfigDef.Type.STRING, null, ConfigDef.Importance.LOW,SslConfigs.SSL_KEYSTORE_AS_STRING_DOC)
165+
.define(SslConfigs.SSL_TRUSTSTORE_AS_STRING, ConfigDef.Type.STRING, null, ConfigDef.Importance.LOW,SslConfigs.SSL_TRUSTSTORE_AS_STRING_DOC);
158166
}
159167

160168
public static final Set<String> RECONFIGURABLE_CONFIGS = Utils.mkSet(
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.apache.kafka.common.security.ssl;
2+
3+
import java.io.BufferedInputStream;
4+
import java.io.File;
5+
import java.io.FileInputStream;
6+
import java.io.IOException;
7+
import java.security.GeneralSecurityException;
8+
import java.security.KeyStore;
9+
import java.security.cert.Certificate;
10+
import java.security.cert.CertificateFactory;
11+
12+
public class PcfTruststoreUtility {
13+
14+
public static final String CRT = "CRT";
15+
16+
public static KeyStore createTrustStore(String locationOfCerts, String trustStorePass) throws GeneralSecurityException, IOException {
17+
if(!new File(locationOfCerts).exists()){
18+
locationOfCerts = System.getenv(locationOfCerts);
19+
}
20+
KeyStore ks = KeyStore.getInstance("JKS");
21+
ks.load(null, trustStorePass.toCharArray());
22+
try (FileInputStream fis = new FileInputStream(locationOfCerts)) {
23+
try (BufferedInputStream bis = new BufferedInputStream(fis)) {
24+
CertificateFactory cf = CertificateFactory.getInstance("X.509");
25+
Certificate cert = null;
26+
27+
while (bis.available() > 0) {
28+
cert = cf.generateCertificate(bis);
29+
ks.setCertificateEntry(String.valueOf(bis.available()), cert);
30+
}
31+
ks.setCertificateEntry(String.valueOf(bis.available()), cert);
32+
return ks;
33+
}
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)