From f48be2ca75bb56858d6cdaca0195a2ea77a43163 Mon Sep 17 00:00:00 2001 From: Star Poon Date: Wed, 25 Dec 2024 13:01:46 +0900 Subject: [PATCH] Load default Java truststore for --use-system-truststore --- .../main/java/io/trino/client/OkHttpUtil.java | 27 ++++++++++++------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/client/trino-client/src/main/java/io/trino/client/OkHttpUtil.java b/client/trino-client/src/main/java/io/trino/client/OkHttpUtil.java index 12b8b807f357..d37d329eb379 100644 --- a/client/trino-client/src/main/java/io/trino/client/OkHttpUtil.java +++ b/client/trino-client/src/main/java/io/trino/client/OkHttpUtil.java @@ -334,17 +334,29 @@ private static KeyStore loadTrustStore(File trustStorePath, Optional tru private static KeyStore loadSystemKeyStore(Optional keyStoreType) throws IOException, GeneralSecurityException { - return loadSystemStore(keyStoreType, KEYSTORE_MACOS, KEYSTORE_WINDOWS_MY); + Optional systemStoreType = getSystemStoreType(keyStoreType, KEYSTORE_WINDOWS_MY); + KeyStore store = KeyStore.getInstance(systemStoreType.orElseGet(KeyStore::getDefaultType)); + store.load(null, null); + return store; } private static KeyStore loadSystemTrustStore(Optional trustStoreType) throws IOException, GeneralSecurityException { - return loadSystemStore(trustStoreType, KEYSTORE_MACOS, KEYSTORE_WINDOWS_ROOT); + Optional systemStoreType = getSystemStoreType(trustStoreType, KEYSTORE_WINDOWS_ROOT); + if (systemStoreType.isPresent()) { + KeyStore store = KeyStore.getInstance(systemStoreType.get()); + store.load(null, null); + return store; + } + else { + // return null if trustStoreType isn't specified and osName is unknown + // trustManagerFactory.init(null) will try to load the default Java trustStore + return null; + } } - private static KeyStore loadSystemStore(Optional storeType, String mac, String windows) - throws IOException, GeneralSecurityException + private static Optional getSystemStoreType(Optional storeType, String windows) { String osName = Optional.ofNullable(StandardSystemProperty.OS_NAME.value()).orElse(""); Optional systemStoreType = storeType; @@ -353,13 +365,10 @@ private static KeyStore loadSystemStore(Optional storeType, String mac, systemStoreType = Optional.of(windows); } else if (osName.contains("Mac")) { - systemStoreType = Optional.of(mac); + systemStoreType = Optional.of(KEYSTORE_MACOS); } } - - KeyStore store = KeyStore.getInstance(systemStoreType.orElseGet(KeyStore::getDefaultType)); - store.load(null, null); - return store; + return systemStoreType; } public static void setupKerberos(