|
3 | 3 | import static org.assertj.core.api.Assertions.assertThat; |
4 | 4 |
|
5 | 5 | import java.io.IOException; |
| 6 | +import java.nio.file.Files; |
| 7 | +import java.nio.file.Path; |
6 | 8 | import java.security.KeyStore; |
7 | 9 | import java.security.KeyStoreException; |
8 | 10 | import java.security.NoSuchAlgorithmException; |
9 | 11 | import java.security.cert.CertificateException; |
10 | 12 | import java.security.cert.X509Certificate; |
11 | 13 | import java.util.Collections; |
12 | 14 | import java.util.List; |
| 15 | +import java.util.Locale; |
13 | 16 | import java.util.stream.Collectors; |
14 | 17 | import java.util.stream.Stream; |
15 | 18 |
|
|
26 | 29 | import io.quarkus.test.QuarkusUnitTest; |
27 | 30 |
|
28 | 31 | public class JavaNetSslTlsBucketConfigTest { |
29 | | - |
30 | 32 | @RegisterExtension |
31 | | - static final QuarkusUnitTest config = new QuarkusUnitTest().setArchiveProducer( |
32 | | - () -> ShrinkWrap.create(JavaArchive.class)); |
| 33 | + static final QuarkusUnitTest config = createConfig(); |
| 34 | + |
| 35 | + static QuarkusUnitTest createConfig() { |
| 36 | + final Path tsPath = defaultTrustStorePath(); |
| 37 | + String tsType = System.getProperty("javax.net.ssl.trustStoreType", KeyStore.getDefaultType()) |
| 38 | + .toLowerCase(Locale.US); |
| 39 | + if (tsType.equals("pkcs12")) { |
| 40 | + tsType = "p12"; |
| 41 | + } |
| 42 | + final String password = System.getProperty("javax.net.ssl.trustStorePassword", "changeit"); |
| 43 | + |
| 44 | + return new QuarkusUnitTest() |
| 45 | + .setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)) |
| 46 | + .overrideConfigKey("quarkus.tls.javaNetSslLike.trust-store." + tsType + ".path", tsPath.toString()) |
| 47 | + .overrideConfigKey("quarkus.tls.javaNetSslLike.trust-store." + tsType + ".password", password); |
| 48 | + |
| 49 | + } |
| 50 | + |
| 51 | + static Path defaultTrustStorePath() { |
| 52 | + final String rawTsPath = System.getProperty("javax.net.ssl.trustStore"); |
| 53 | + if (rawTsPath != null && !rawTsPath.isEmpty()) { |
| 54 | + return Path.of(rawTsPath); |
| 55 | + } |
| 56 | + final String javaHome = System.getProperty("java.home"); |
| 57 | + if (javaHome == null || javaHome.isEmpty()) { |
| 58 | + throw new IllegalStateException( |
| 59 | + "Could not locate the default Java truststore because the 'java.home' property is not set"); |
| 60 | + } |
| 61 | + final Path javaHomePath = Path.of(javaHome); |
| 62 | + if (!Files.isDirectory(javaHomePath)) { |
| 63 | + throw new IllegalStateException("Could not locate the default Java truststore because the 'java.home' path '" |
| 64 | + + javaHome + "' is not a directory"); |
| 65 | + } |
| 66 | + final Path jssecacerts = javaHomePath.resolve("lib/security/jssecacerts"); |
| 67 | + if (Files.isRegularFile(jssecacerts)) { |
| 68 | + return jssecacerts; |
| 69 | + } |
| 70 | + final Path cacerts = javaHomePath.resolve("lib/security/cacerts"); |
| 71 | + if (Files.isRegularFile(cacerts)) { |
| 72 | + return cacerts; |
| 73 | + } |
| 74 | + throw new IllegalStateException( |
| 75 | + "Could not locate the default Java truststore. Tried javax.net.ssl.trustStore system property, " + jssecacerts |
| 76 | + + " and " + cacerts); |
| 77 | + } |
33 | 78 |
|
34 | 79 | @Inject |
35 | 80 | TlsConfigurationRegistry certificates; |
@@ -82,4 +127,17 @@ void test() throws KeyStoreException, IOException, NoSuchAlgorithmException, Cer |
82 | 127 | } |
83 | 128 | } |
84 | 129 | } |
| 130 | + |
| 131 | + @Test |
| 132 | + void checkDefaults() { |
| 133 | + /* |
| 134 | + * The javaNetSslLike named TLS bucket mimics what JavaNetSslTlsBucketConfig does programmatically. |
| 135 | + * By asserting that their SSLOptions are equal, we make sure that all defaults set programmatically |
| 136 | + * in JavaNetSslTlsBucketConfig are in sync with @WithDefault values defined in TlsBucketConfig |
| 137 | + */ |
| 138 | + final TlsConfiguration javaNetSsl = certificates.get("javax.net.ssl").orElseThrow(); |
| 139 | + final TlsConfiguration javaNetSslLike = certificates.get("javaNetSslLike").orElseThrow(); |
| 140 | + assertThat(javaNetSsl.getSSLOptions()).isEqualTo(javaNetSslLike.getSSLOptions()); |
| 141 | + |
| 142 | + } |
85 | 143 | } |
0 commit comments