Skip to content

Commit 95ea44c

Browse files
committed
Replace hardcoded JKS keystore type with KeyStore.getDefaultType()
In-memory keystores used for TrustManagerFactory/KeyManagerFactory do not need a specific type. Using the JVM's default allows FIPS-configured JVMs to use their preferred keystore type (e.g. BCFKS) instead of failing on JKS.
1 parent 6b0e3cb commit 95ea44c

File tree

9 files changed

+25
-25
lines changed

9 files changed

+25
-25
lines changed

cluster-operator/src/main/java/io/strimzi/operator/cluster/operator/resource/KafkaAgentClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,14 @@ private HttpClient createHttpClient() {
8080
}
8181
String trustManagerFactoryAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
8282
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(trustManagerFactoryAlgorithm);
83-
trustManagerFactory.init(tlsPemIdentity.pemTrustSet().jksTrustStore());
83+
trustManagerFactory.init(tlsPemIdentity.pemTrustSet().trustStore());
8484

8585
if (tlsPemIdentity.pemAuthIdentity() == null) {
8686
throw new RuntimeException("Missing cluster operator authentication identity certificates required to create connection to Kafka Agent");
8787
}
8888
String keyManagerFactoryAlgorithm = KeyManagerFactory.getDefaultAlgorithm();
8989
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(keyManagerFactoryAlgorithm);
90-
keyManagerFactory.init(tlsPemIdentity.pemAuthIdentity().jksKeyStore(KEYSTORE_PASSWORD), KEYSTORE_PASSWORD);
90+
keyManagerFactory.init(tlsPemIdentity.pemAuthIdentity().keyStore(KEYSTORE_PASSWORD), KEYSTORE_PASSWORD);
9191

9292
SSLContext sslContext = SSLContext.getInstance("TLS");
9393
sslContext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);

cluster-operator/src/main/java/io/strimzi/operator/cluster/operator/resource/cruisecontrol/CruiseControlApiImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ private HttpClient buildHttpClient() {
145145
if (apiSslEnabled) {
146146
String trustManagerFactoryAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
147147
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(trustManagerFactoryAlgorithm);
148-
trustManagerFactory.init(pemTrustSet.jksTrustStore());
148+
trustManagerFactory.init(pemTrustSet.trustStore());
149149

150150
SSLContext sslContext = SSLContext.getInstance("TLS");
151151
sslContext.init(null, trustManagerFactory.getTrustManagers(), null);

kafka-agent/src/main/java/io/strimzi/kafka/agent/KafkaAgent.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,13 +268,13 @@ public boolean handle(Request request, Response response, Callback callback) thr
268268

269269
static SslContextFactory.Server getSSLContextFactory(Secret caCertSecret, Secret nodeCertSecret) throws GeneralSecurityException, IOException {
270270
SslContextFactory.Server sslContextFactory = new SslContextFactory.Server();
271-
sslContextFactory.setTrustStore(KafkaAgentUtils.jksTrustStore(caCertSecret));
271+
sslContextFactory.setTrustStore(KafkaAgentUtils.trustStore(caCertSecret));
272272

273273
byte[] random = new byte[24];
274274
RANDOM.nextBytes(random);
275275
String password = Base64.getUrlEncoder().withoutPadding().encodeToString(random).substring(0, 32);
276276

277-
sslContextFactory.setKeyStore(KafkaAgentUtils.jksKeyStore(nodeCertSecret, password.toCharArray()));
277+
sslContextFactory.setKeyStore(KafkaAgentUtils.keyStore(nodeCertSecret, password.toCharArray()));
278278
sslContextFactory.setKeyStorePassword(password);
279279
sslContextFactory.setNeedClientAuth(true);
280280
return sslContextFactory;

kafka-agent/src/main/java/io/strimzi/kafka/agent/KafkaAgentUtils.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ private KafkaAgentUtils() { }
3535
* throws an exception if it is not.
3636
*
3737
* @param secret Secret containing the TrustStore certificates
38-
* @return TrustStore file in JKS format
38+
* @return In-memory TrustStore using the JVM's default keystore type
3939
* @throws GeneralSecurityException if something goes wrong when creating the truststore
4040
* @throws IOException if there is an I/O or format problem with the data used to load the truststore.
4141
* This is not expected as the truststore is loaded with null parameter.
4242
*/
43-
static KeyStore jksTrustStore(Secret secret) throws GeneralSecurityException, IOException {
44-
KeyStore trustStore = KeyStore.getInstance("JKS");
43+
static KeyStore trustStore(Secret secret) throws GeneralSecurityException, IOException {
44+
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
4545
trustStore.load(null);
4646
int aliasIndex = 0;
4747
for (X509Certificate certificate : asX509Certificates(extractCerts(secret), secret.getMetadata().getName(), secret.getMetadata().getNamespace()).values()) {
@@ -56,11 +56,11 @@ static KeyStore jksTrustStore(Secret secret) throws GeneralSecurityException, IO
5656
*
5757
* @param secret Secret containing private key and certificate
5858
*
59-
* @return KeyStore file in JKS format
59+
* @return In-memory KeyStore using the JVM's default keystore type
6060
* @throws GeneralSecurityException if something goes wrong when creating the truststore
6161
* @throws IOException if there is an I/O or format problem with the data used to load the truststore.
6262
*/
63-
static KeyStore jksKeyStore(Secret secret, char[] password) throws GeneralSecurityException, IOException {
63+
static KeyStore keyStore(Secret secret, char[] password) throws GeneralSecurityException, IOException {
6464
String secretName = secret.getMetadata().getName();
6565
String strippedPrivateKey = new String(decodeBase64FieldFromSecret(secret, secretName + ".key"), StandardCharsets.US_ASCII)
6666
.replace("-----BEGIN PRIVATE KEY-----", "")
@@ -72,7 +72,7 @@ static KeyStore jksKeyStore(Secret secret, char[] password) throws GeneralSecuri
7272
final PrivateKey key = keyFactory.generatePrivate(keySpec);
7373

7474
X509Certificate certificateChain = x509Certificate(decodeBase64FieldFromSecret(secret, secretName + ".crt"));
75-
KeyStore nodeKeyStore = KeyStore.getInstance("JKS");
75+
KeyStore nodeKeyStore = KeyStore.getInstance(KeyStore.getDefaultType());
7676
nodeKeyStore.load(null);
7777
nodeKeyStore.setKeyEntry(secret.getMetadata().getName(), key, password, new Certificate[]{certificateChain});
7878
return nodeKeyStore;

kafka-agent/src/test/java/io/strimzi/kafka/agent/KafkaAgentTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,13 @@ public void setUp() throws URISyntaxException, GeneralSecurityException, IOExcep
9696

9797
private SSLContext getClientSSLContext(Secret caCertSecret, Secret nodeCertSecret) throws GeneralSecurityException, IOException {
9898
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
99-
tmf.init(KafkaAgentUtils.jksTrustStore(caCertSecret));
99+
tmf.init(KafkaAgentUtils.trustStore(caCertSecret));
100100

101101
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
102102
byte[] random = new byte[24];
103103
RANDOM.nextBytes(random);
104104
String password = Base64.getUrlEncoder().withoutPadding().encodeToString(random).substring(0, 32);
105-
kmf.init(KafkaAgentUtils.jksKeyStore(nodeCertSecret, password.toCharArray()), password.toCharArray());
105+
kmf.init(KafkaAgentUtils.keyStore(nodeCertSecret, password.toCharArray()), password.toCharArray());
106106

107107
SSLContext sslContext = SSLContext.getInstance("TLS");
108108
sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), RANDOM);

operator-common/src/main/java/io/strimzi/operator/common/auth/PemAuthIdentity.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,11 @@ public byte[] pemKeyStore() {
127127
*
128128
* @param password to use to secure the KeyStore
129129
*
130-
* @return KeyStore file in JKS format
130+
* @return In-memory KeyStore using the JVM's default keystore type
131131
* @throws GeneralSecurityException if something goes wrong when creating the truststore
132132
* @throws IOException if there is an I/O or format problem with the data used to load the truststore.
133133
*/
134-
public KeyStore jksKeyStore(char[] password) throws GeneralSecurityException, IOException {
134+
public KeyStore keyStore(char[] password) throws GeneralSecurityException, IOException {
135135
String strippedPrivateKey = privateKeyAsPem()
136136
.replace("-----BEGIN PRIVATE KEY-----", "")
137137
.replaceAll(System.lineSeparator(), "")
@@ -141,7 +141,7 @@ public KeyStore jksKeyStore(char[] password) throws GeneralSecurityException, IO
141141
final KeyFactory keyFactory = KeyFactory.getInstance("RSA");
142142
final PrivateKey key = keyFactory.generatePrivate(keySpec);
143143

144-
KeyStore coKeyStore = KeyStore.getInstance("JKS");
144+
KeyStore coKeyStore = KeyStore.getInstance(KeyStore.getDefaultType());
145145
coKeyStore.load(null);
146146
coKeyStore.setKeyEntry("cluster-operator", key, password, new Certificate[]{certificateChain()});
147147
return coKeyStore;

operator-common/src/main/java/io/strimzi/operator/common/auth/PemTrustSet.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,15 @@ public String trustedCertificatesString() {
6161
* TrustStore to use for TLS connections. This also validates each one is a valid certificate and
6262
* throws an exception if it is not.
6363
*
64-
* @return TrustStore file in JKS format
64+
* @return In-memory TrustStore using the JVM's default keystore type
6565
*
6666
* @throws GeneralSecurityException If something goes wrong when creating the truststore
6767
*
6868
* @throws IOException If there is an I/O or format problem with the data used to load the truststore.
6969
* This is not expected as the truststore is loaded with null parameter.
7070
*/
71-
public KeyStore jksTrustStore() throws GeneralSecurityException, IOException {
72-
KeyStore trustStore = KeyStore.getInstance("JKS");
71+
public KeyStore trustStore() throws GeneralSecurityException, IOException {
72+
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
7373
trustStore.load(null);
7474
int aliasIndex = 0;
7575
for (X509Certificate certificate : asX509Certificates()) {

operator-common/src/test/java/io/strimzi/operator/common/auth/PemAuthIdentityTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public void testSecretCorrupted() {
6060
"cluster-operator.password", "bm90YXBhc3N3b3Jk")) //notapassword
6161
.build();
6262
PemAuthIdentity pemAuthIdentity = PemAuthIdentity.clusterOperator(secretWithBadCertificate);
63-
Exception e = assertThrows(RuntimeException.class, () -> pemAuthIdentity.jksKeyStore(new char[]{}));
63+
Exception e = assertThrows(RuntimeException.class, () -> pemAuthIdentity.keyStore(new char[]{}));
6464
assertThat(e.getMessage(), is("Bad/corrupt certificate found in data.cluster-operator.crt of Secret testcluster-cluster-operator-certs in namespace testns"));
6565
}
6666

operator-common/src/test/java/io/strimzi/operator/common/auth/PemTrustSetTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public void testSecretCorrupted() {
3434
.withData(Map.of("ca.crt", "notacert"))
3535
.build();
3636
PemTrustSet pemTrustSet = new PemTrustSet(secretWithBadCertificate);
37-
Exception e = assertThrows(RuntimeException.class, pemTrustSet::jksTrustStore);
37+
Exception e = assertThrows(RuntimeException.class, pemTrustSet::trustStore);
3838
assertThat(e.getMessage(), is("Bad/corrupt certificate found in data.ca.crt of Secret testcluster-cluster-operator-certs in namespace testns"));
3939
}
4040

@@ -149,10 +149,10 @@ public void testCAChain() throws GeneralSecurityException, IOException {
149149

150150
PemTrustSet pemTrustSet = new PemTrustSet(secretWithCAChain);
151151

152-
// Test JKS conversion
153-
KeyStore jks = pemTrustSet.jksTrustStore();
154-
assertThat(jks.size(), is(1));
155-
X509Certificate cert = (X509Certificate) jks.getCertificate(jks.aliases().nextElement());
152+
// Test KeyStore conversion
153+
KeyStore trustStore = pemTrustSet.trustStore();
154+
assertThat(trustStore.size(), is(1));
155+
X509Certificate cert = (X509Certificate) trustStore.getCertificate(trustStore.aliases().nextElement());
156156
assertThat(cert.getSubjectX500Principal().getName(), is("CN=ClusterCA,O=Strimzi\\, Inc.,L=Prague,C=CZ"));
157157

158158
// Test PEM conversion

0 commit comments

Comments
 (0)