Skip to content

Commit f70aa1c

Browse files
committed
refactor privateKey parsing
1 parent f3d5c59 commit f70aa1c

File tree

3 files changed

+71
-570
lines changed

3 files changed

+71
-570
lines changed

libs/ssl-config/src/main/java/org/opensearch/common/ssl/PemKeyConfig.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232

3333
package org.opensearch.common.ssl;
3434

35+
import org.bouncycastle.pkcs.PKCSException;
36+
3537
import javax.net.ssl.KeyManagerFactory;
3638
import javax.net.ssl.X509ExtendedKeyManager;
3739

@@ -82,7 +84,12 @@ public X509ExtendedKeyManager createKeyManager() {
8284

8385
private PrivateKey getPrivateKey() {
8486
try {
85-
final PrivateKey privateKey = PemUtils.readPrivateKey(key, () -> keyPassword);
87+
final PrivateKey privateKey = PemUtils.readPrivateKey(key, () -> {
88+
if (keyPassword.length == 0) {
89+
throw new SslConfigException("cannot read encrypted key [" + key.toAbsolutePath() + "] without a password");
90+
}
91+
return keyPassword;
92+
});
8693
if (privateKey == null) {
8794
throw new SslConfigException("could not load ssl private key file [" + key + "]");
8895
}
@@ -91,7 +98,7 @@ private PrivateKey getPrivateKey() {
9198
throw new SslConfigException("the configured ssl private key file [" + key.toAbsolutePath() + "] does not exist", e);
9299
} catch (IOException e) {
93100
throw new SslConfigException("the configured ssl private key file [" + key.toAbsolutePath() + "] cannot be read", e);
94-
} catch (GeneralSecurityException e) {
101+
} catch (PKCSException e) {
95102
throw new SslConfigException("cannot load ssl private key file [" + key.toAbsolutePath() + "]", e);
96103
}
97104
}

0 commit comments

Comments
 (0)