Skip to content

Commit 01872e8

Browse files
Fix GCS Keystore Handling in FIPS Mode (#75028)
In FIPS mode loading the `.p12` keystore used by the new SDK version is not supported because of "PBE AlgorithmParameters not available". Fortunately, the SDK still includes the old jks trust store so we can just manually load it the same way it was loaded by the previous version to fix things. Also, fixed `SocketAccess` to properly rethrow this kind of exception and not run into a class cast issue. Closes #75023 relates googleapis/google-api-java-client#1738
1 parent 0f5a0e7 commit 01872e8

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

plugins/repository-gcs/src/main/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageService.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.google.api.client.http.HttpRequestInitializer;
1313
import com.google.api.client.http.HttpTransport;
1414
import com.google.api.client.http.javanet.NetHttpTransport;
15+
import com.google.api.client.util.SecurityUtils;
1516
import com.google.auth.oauth2.GoogleCredentials;
1617
import com.google.auth.oauth2.ServiceAccountCredentials;
1718
import com.google.cloud.ServiceOptions;
@@ -34,6 +35,7 @@
3435
import java.net.HttpURLConnection;
3536
import java.net.URI;
3637
import java.net.URL;
38+
import java.security.KeyStore;
3739
import java.util.Map;
3840

3941
import static java.nio.charset.StandardCharsets.UTF_8;
@@ -126,7 +128,13 @@ private Storage createClient(GoogleCloudStorageClientSettings clientSettings,
126128
final NetHttpTransport.Builder builder = new NetHttpTransport.Builder();
127129
// requires java.lang.RuntimePermission "setFactory"
128130
// Pin the TLS trust certificates.
129-
builder.trustCertificates(GoogleUtils.getCertificateTrustStore());
131+
// We manually load the key store from jks instead of using GoogleUtils.getCertificateTrustStore() because that uses a .p12
132+
// store format not compatible with FIPS mode.
133+
final KeyStore certTrustStore = SecurityUtils.getJavaKeyStore();
134+
try (InputStream keyStoreStream = GoogleUtils.class.getResourceAsStream("google.jks")) {
135+
SecurityUtils.loadKeyStore(certTrustStore, keyStoreStream, "notasecret");
136+
}
137+
builder.trustCertificates(certTrustStore);
130138
return builder.build();
131139
});
132140

plugins/repository-gcs/src/main/java/org/elasticsearch/repositories/gcs/SocketAccess.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static <T> T doPrivilegedIOException(PrivilegedExceptionAction<T> operati
3232
try {
3333
return AccessController.doPrivileged(operation);
3434
} catch (PrivilegedActionException e) {
35-
throw (IOException) e.getCause();
35+
throw causeAsIOException(e);
3636
}
3737
}
3838

@@ -44,7 +44,18 @@ public static void doPrivilegedVoidIOException(CheckedRunnable<IOException> acti
4444
return null;
4545
});
4646
} catch (PrivilegedActionException e) {
47-
throw (IOException) e.getCause();
47+
throw causeAsIOException(e);
4848
}
4949
}
50+
51+
private static IOException causeAsIOException(PrivilegedActionException e) {
52+
final Throwable cause = e.getCause();
53+
if (cause instanceof IOException) {
54+
return (IOException) cause;
55+
}
56+
if (cause instanceof RuntimeException) {
57+
throw (RuntimeException) cause;
58+
}
59+
throw new RuntimeException(cause);
60+
}
5061
}

0 commit comments

Comments
 (0)