-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Backport: better handling of intermediate CAs in datanode truststore (#…
…21656) * Better handling of intermediate CAs in datanode truststore * added changelog * removed dead code * fixed test --------- Co-authored-by: Matthias Oesterheld <33032967+moesterheld@users.noreply.github.com>
- Loading branch information
1 parent
5604191
commit 8d972b2
Showing
7 changed files
with
237 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
type = "c" | ||
message = "Better handling of intermediate CAs in datanode truststore" | ||
|
||
pulls = ["21062"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
graylog2-server/src/main/java/org/graylog2/security/TruststoreUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* Copyright (C) 2020 Graylog, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the Server Side Public License, version 1, | ||
* as published by MongoDB, Inc. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* Server Side Public License for more details. | ||
* | ||
* You should have received a copy of the Server Side Public License | ||
* along with this program. If not, see | ||
* <http://www.mongodb.com/licensing/server-side-public-license>. | ||
*/ | ||
package org.graylog2.security; | ||
|
||
import jakarta.annotation.Nonnull; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.security.KeyStore; | ||
import java.security.KeyStoreException; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.cert.CertificateException; | ||
import java.util.Optional; | ||
import java.util.stream.Stream; | ||
|
||
public class TruststoreUtils { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(TruststoreUtils.class); | ||
|
||
public static Optional<KeyStore> loadJvmTruststore() { | ||
return jvmTruststoreLocation().map(location -> { | ||
|
||
String password = jvmTruststorePassword(); | ||
String type = jvmTruststoreType(); | ||
|
||
LOG.info("Detected existing JVM truststore: " + location.toAbsolutePath() + " of type " + type); | ||
|
||
try { | ||
KeyStore trustStore = KeyStore.getInstance(type); | ||
try (InputStream is = Files.newInputStream(location)) { | ||
trustStore.load(is, password.toCharArray()); | ||
} | ||
return trustStore; | ||
} catch (KeyStoreException | CertificateException | IOException | NoSuchAlgorithmException e) { | ||
throw new RuntimeException(e); | ||
} | ||
}); | ||
} | ||
|
||
private static String jvmTruststoreType() { | ||
return Optional.ofNullable(System.getProperty("javax.net.ssl.trustStoreType")) | ||
.filter(type -> !type.isEmpty()) | ||
.orElseGet(KeyStore::getDefaultType); | ||
} | ||
|
||
private static String jvmTruststorePassword() { | ||
return Optional.ofNullable(System.getProperty("javax.net.ssl.trustStorePassword")) | ||
.filter(p -> !p.isEmpty()) | ||
.orElse("changeit"); | ||
} | ||
|
||
private static Optional<Path> jvmTruststoreLocation() { | ||
String javaHome = System.getProperty("java.home"); | ||
return Stream.of( | ||
truststoreSystemProperty(), | ||
libSecurityFile(javaHome, "jssecacerts"), | ||
libSecurityFile(javaHome, "cacerts") | ||
) | ||
.filter(Optional::isPresent) | ||
.map(Optional::get) | ||
.filter(TruststoreUtils::isReadableFile) | ||
.findFirst(); | ||
} | ||
|
||
@Nonnull | ||
private static Optional<Path> libSecurityFile(String javaHome, String filename) { | ||
return Optional.ofNullable(javaHome).map(home -> Paths.get(home, "lib", "security", filename)); | ||
} | ||
|
||
@Nonnull | ||
private static Optional<Path> truststoreSystemProperty() { | ||
return Optional.ofNullable(System.getProperty("javax.net.ssl.trustStore")).filter(p -> !p.isEmpty()).map(Paths::get); | ||
} | ||
|
||
private static boolean isReadableFile(Path path) { | ||
return Files.exists(path) && Files.isRegularFile(path) && Files.isReadable(path); | ||
} | ||
} |
Oops, something went wrong.