Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tls): make Keystore password optional #534

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 22 additions & 12 deletions src/main/java/io/cryostat/agent/MainModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -477,34 +477,37 @@ public static Optional<SSLContext> provideServerSslContext(
@Named(ConfigModule.CRYOSTAT_AGENT_WEBSERVER_TLS_CERT_TYPE) String certType) {
boolean ssl =
(keyStoreFilePath.isPresent() || keyFilePath.isPresent())
&& keyStorePassFile.isPresent()
&& certFilePath.isPresent();
if (!ssl) {
if (keyStorePassFile.isPresent()
|| keyFilePath.isPresent()
if (keyFilePath.isPresent()
|| keyStoreFilePath.isPresent()
|| certFilePath.isPresent()) {
throw new IllegalArgumentException(
"The file paths for the keystore or key file, keystore password, and"
+ " certificate must ALL be provided to set up HTTPS connections."
+ " Otherwise, make sure they are all unset to use an HTTP server.");
"The file paths for the keystore or key file, and certificate must ALL be"
+ " provided to set up HTTPS connections. Otherwise, make sure they are"
+ " all unset to use an HTTP server.");
}
return Optional.empty();
}

InputStream keystore = null;
try (InputStream pass = new FileInputStream(keyStorePassFile.get());
InputStream certFile = new FileInputStream(certFilePath.get())) {
InputStream pass = null;
try (InputStream certFile = new FileInputStream(certFilePath.get())) {
SSLContext sslContext = SSLContext.getInstance(serverTlsVersion);
if (keyStoreFilePath.isPresent()) {
keystore = new FileInputStream(keyStoreFilePath.get());
}
char[] storePass = null;
if (keyStorePassFile.isPresent()) {
pass = new FileInputStream(keyStorePassFile.get());
String password = IOUtils.toString(pass, Charset.forName(passFileCharset));
password = password.substring(0, password.length() - 1);
storePass = password.toCharArray();
}

// initialize keystore
String password = IOUtils.toString(pass, Charset.forName(passFileCharset));
password = password.substring(0, password.length() - 1);
KeyStore ks = KeyStore.getInstance(keyStoreType);
ks.load(keystore, password.toCharArray());
ks.load(keystore, storePass);

// set up certificate factory
CertificateFactory cf = CertificateFactory.getInstance(certType);
Expand Down Expand Up @@ -561,7 +564,7 @@ public static Optional<SSLContext> provideServerSslContext(
// set up key manager factory
KeyManagerFactory kmf =
KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(ks, password.toCharArray());
kmf.init(ks, storePass);

// set up trust manager factory
TrustManagerFactory tmf =
Expand All @@ -588,6 +591,13 @@ public static Optional<SSLContext> provideServerSslContext(
throw new RuntimeException(ioe);
}
}
if (pass != null) {
try {
pass.close();
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
}
}
}

Expand Down
Loading