This is an automated email from the ASF dual-hosted git repository.
roryqi pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/gravitino.git
The following commit(s) were added to refs/heads/main by this push:
new ef7fe4b1ce [#14900] feat(client-java): Add TLS convenience helpers and
protocol/cipher configuration (#14934)
ef7fe4b1ce is described below
commit ef7fe4b1ce9e23eba571571a45f564f2d78e8182
Author: Octavio Herrera Contreras <[email protected]>
AuthorDate: Thu Nov 12 23:30:50 2026 -0800
[#14900] feat(client-java): Add TLS convenience helpers and protocol/cipher
configuration (#14934)
[#14900] feat(client): Add TLS convenience helpers and protocol/cipher
configuration
### What changes were proposed in this pull request?
This PR adds convenience helpers to TLSConfigurers for common Java
client TLS configurations.
- Added withTrustStore(), for configuring a custom truststore.
- Added withKeyStore(), for client certificate authentication using the
default JVM truststore.
- Added withMutualTls(), for mutual TLS authentication using a custom
truststore and client keystore.
- Added protocols() and cipherSuites() builder methods to restrict
supported TLS protocols and cipher suites.
- Added input validation and descriptive error messages for invalid TLS
configurations.
- Extended TLS integration tests to cover the convenience helpers,
certificate validation, and protocol/cipher-suite restrictions.
### Why are the changes needed?
These changes simplify the common TLS CUJs by allowing users to
configure TLS without directly interacting with JSSE types such as
SSLContext or KeyStore.
They also provide additional flexibility by supporting protocol and
cipher-suite restrictions, as well as mutual TLS authentication using
the default JVM truststore.
### Does this PR introduce _any_ user-facing change?
Yes the Java client introduces the following public APIs
TLSConfigurers.withTrustStore()
TLSConfigurers.withKeyStore()
TLSConfigurers.withMutualTls()
TLSConfigurers.builder().protocols()
TLSConfigurers.builder().cipherSuites()
As will be described in the documentation
### How was this patch tested?
The following commands passed:
./gradlew :clients:client-java:test --tests
"org.apache.gravitino.client.TestHTTPClientTLS"
Passed
./gradlew rat
Passed
The integration tests cover TLS and mTLS certificate verification,
invalid truststore configurations, and successful and rejected
protocol/cipher-suite handshakes.
Related issue: #14900
---------
Co-authored-by: roryqi <[email protected]>
---
.../apache/gravitino/client/TLSConfigurers.java | 164 +++++++++++++--
.../apache/gravitino/client/TestHTTPClientTLS.java | 231 ++++++++++++++++++---
docs/how-to-use-gravitino-client.md | 132 ++++++++----
docs/security/how-to-use-https.md | 97 +++++----
.../gravitino/server/web/TestTlsServerUtils.java | 24 ++-
5 files changed, 520 insertions(+), 128 deletions(-)
diff --git
a/clients/client-java/src/main/java/org/apache/gravitino/client/TLSConfigurers.java
b/clients/client-java/src/main/java/org/apache/gravitino/client/TLSConfigurers.java
index c68512e233..4d6a171257 100644
---
a/clients/client-java/src/main/java/org/apache/gravitino/client/TLSConfigurers.java
+++
b/clients/client-java/src/main/java/org/apache/gravitino/client/TLSConfigurers.java
@@ -25,8 +25,10 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
+import java.util.Arrays;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLParameters;
import javax.net.ssl.TrustManagerFactory;
/** Provides utility methods for creating {@link TLSConfigurer} instances. */
@@ -45,6 +47,50 @@ public final class TLSConfigurers {
return new Builder();
}
+ /**
+ * Creates a TLS configurer using a custom truststore.
+ *
+ * @param path path to the truststore
+ * @param password truststore password
+ * @return the configured TLS configurer
+ */
+ public static TLSConfigurer withTrustStore(Path path, String password) {
+ return builder().trustStore(path, password).build();
+ }
+
+ /**
+ * Creates a TLS configurer using the default JVM truststore and a custom
client keystore.
+ *
+ * <p>The server certificate must already be trusted by the JVM. If the
server uses a private CA
+ * or a self-signed certificate that the JVM does not trust, use {@link
#withMutualTls(Path,
+ * String, Path, String)} instead.
+ *
+ * @param path path to the client keystore
+ * @param password keystore password
+ * @return the configured TLS configurer
+ */
+ public static TLSConfigurer withKeyStore(Path path, String password) {
+ return builder().keyStore(path, password).build();
+ }
+
+ /**
+ * Creates a TLS configurer using a custom truststore and client keystore
for mutual TLS
+ * authentication.
+ *
+ * @param trustStorePath path to the truststore
+ * @param trustPassword truststore password
+ * @param keyStorePath path to the client keystore
+ * @param keyPassword keystore password
+ * @return the configured TLS configurer
+ */
+ public static TLSConfigurer withMutualTls(
+ Path trustStorePath, String trustPassword, Path keyStorePath, String
keyPassword) {
+ return builder()
+ .trustStore(trustStorePath, trustPassword)
+ .keyStore(keyStorePath, keyPassword)
+ .build();
+ }
+
/** Builder for creating a {@link TLSConfigurer} from truststore and
keystore files. */
public static final class Builder {
private Path trustStorePath;
@@ -53,16 +99,85 @@ public final class TLSConfigurers {
private String keyStorePassword;
private String storeType = DEFAULT_STORE_TYPE;
+ private String[] protocols = new String[0];
+ private String[] cipherSuites = new String[0];
+
private Builder() {}
+ /**
+ * Configures the allowed TLS protocols.
+ *
+ * <p>If no protocols are specified, the default TLS protocols are used.
Unsupported protocols
+ * are rejected when {@link #build()} is called.
+ *
+ * @param protocols allowed TLS protocols, such as {@code TLSv1.3}
+ * @return this builder
+ */
+ public Builder protocols(String... protocols) {
+ Preconditions.checkArgument(protocols != null, "Protocols must not be
null");
+
+ for (String protocol : protocols) {
+ Preconditions.checkArgument(
+ protocol != null && !protocol.isEmpty(), "Protocol must not be
null or empty");
+ }
+
+ this.protocols = protocols.clone();
+ return this;
+ }
+
+ /**
+ * Configures the allowed TLS cipher suites.
+ *
+ * <p>If no cipher suites are specified, the default TLS cipher suites are
used. Unsupported
+ * cipher suites are rejected when {@link #build()} is called.
+ *
+ * @param cipherSuites allowed TLS cipher suites
+ * @return this builder
+ */
+ public Builder cipherSuites(String... cipherSuites) {
+ Preconditions.checkArgument(cipherSuites != null, "Cipher suites must
not be null");
+
+ for (String cipherSuite : cipherSuites) {
+ Preconditions.checkArgument(
+ cipherSuite != null && !cipherSuite.isEmpty(),
+ "Cipher suite must not be null or empty");
+ }
+
+ this.cipherSuites = cipherSuites.clone();
+ return this;
+ }
+
+ private void validateTlsSettings(SSLContext sslContext) {
+ SSLParameters supportedParameters =
sslContext.getSupportedSSLParameters();
+
+ for (String protocol : protocols) {
+ Preconditions.checkArgument(
+
Arrays.asList(supportedParameters.getProtocols()).contains(protocol),
+ "Unsupported TLS protocol: %s",
+ protocol);
+ }
+
+ for (String cipherSuite : cipherSuites) {
+ Preconditions.checkArgument(
+
Arrays.asList(supportedParameters.getCipherSuites()).contains(cipherSuite),
+ "Unsupported TLS cipher suite: %s",
+ cipherSuite);
+ }
+ }
+
/**
* Configures the truststore used to verify the server certificate.
*
+ * <p>An empty password is permitted if supported by the truststore format.
+ *
* @param path path to the truststore
* @param password password for the truststore
* @return this builder
*/
public Builder trustStore(Path path, String password) {
+ Preconditions.checkArgument(path != null, "Truststore path must not be
null");
+ Preconditions.checkArgument(password != null, "Truststore password must
not be null");
+
this.trustStorePath = path;
this.trustStorePassword = password;
return this;
@@ -71,11 +186,16 @@ public final class TLSConfigurers {
/**
* Configures the client keystore used for mutual TLS authentication.
*
+ * <p>An empty password is permitted if supported by the keystore format.
+ *
* @param path path to the client keystore
* @param password password for the client keystore
* @return this builder
*/
public Builder keyStore(Path path, String password) {
+ Preconditions.checkArgument(path != null, "Keystore path must not be
null");
+ Preconditions.checkArgument(password != null, "Keystore password must
not be null");
+
this.keyStorePath = path;
this.keyStorePassword = password;
return this;
@@ -90,6 +210,8 @@ public final class TLSConfigurers {
* @return this builder
*/
public Builder storeType(String storeType) {
+ Preconditions.checkArgument(
+ storeType != null && !storeType.isEmpty(), "Store type must not be
null or empty");
this.storeType = storeType;
return this;
}
@@ -98,44 +220,58 @@ public final class TLSConfigurers {
* Builds a {@link TLSConfigurer} using the configured truststore and
optional client keystore.
*
* @return the configured TLS configurer
- * @throws IllegalArgumentException if required configuration is missing
or the stores cannot be
- * loaded
+ * @throws IllegalArgumentException if the TLS configuration is invalid,
unsupported, or cannot
+ * be initialized
*/
public TLSConfigurer build() {
- Preconditions.checkArgument(trustStorePath != null, "Truststore path
must be provided");
- Preconditions.checkArgument(
- trustStorePassword != null, "Truststore password must be provided");
- Preconditions.checkArgument(storeType != null, "Store type must be
provided");
- Preconditions.checkArgument(
- keyStorePath == null || keyStorePassword != null,
- "Keystore password must be provided when a keystore is configured");
-
SSLContext sslContext = buildSslContext();
+ validateTlsSettings(sslContext);
+
+ String[] configuredProtocols = protocols.clone();
+ String[] configuredCipherSuites = cipherSuites.clone();
+
return new TLSConfigurer() {
@Override
public SSLContext sslContext() {
return sslContext;
}
+
+ @Override
+ public String[] supportedProtocols() {
+ return configuredProtocols.clone();
+ }
+
+ @Override
+ public String[] supportedCipherSuites() {
+ return configuredCipherSuites.clone();
+ }
};
}
/**
- * Creates an SSL context from the configured truststore and optional
client keystore.
+ * Creates an SSL context from the configured or default truststore and
optional client
+ * keystore.
*
* @return the configured SSL context
* @throws IllegalArgumentException if the SSL context cannot be created
*/
private SSLContext buildSslContext() {
try {
- KeyStore trustStore = loadStore(trustStorePath, trustStorePassword,
"truststore");
-
TrustManagerFactory trustManagerFactory =
TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
- trustManagerFactory.init(trustStore);
+
+ // Use the configured truststore, or fall back to the default JVM
truststore.
+ if (trustStorePath != null) {
+ KeyStore trustStore = loadStore(trustStorePath, trustStorePassword,
"truststore");
+ trustManagerFactory.init(trustStore);
+ } else {
+ trustManagerFactory.init((KeyStore) null);
+ }
SSLContext sslContext = SSLContext.getInstance("TLS");
+ // Configure client certificate authentication if a keystore is
provided.
if (keyStorePath == null) {
sslContext.init(null, trustManagerFactory.getTrustManagers(), null);
return sslContext;
diff --git
a/clients/client-java/src/test/java/org/apache/gravitino/client/TestHTTPClientTLS.java
b/clients/client-java/src/test/java/org/apache/gravitino/client/TestHTTPClientTLS.java
index b77752709d..879ca8c850 100644
---
a/clients/client-java/src/test/java/org/apache/gravitino/client/TestHTTPClientTLS.java
+++
b/clients/client-java/src/test/java/org/apache/gravitino/client/TestHTTPClientTLS.java
@@ -19,6 +19,7 @@
package org.apache.gravitino.client;
import static
org.apache.gravitino.server.web.TestTlsServerUtils.TEST_STORE_PASSWORD;
+import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import com.google.common.collect.ImmutableMap;
@@ -37,6 +38,9 @@ import org.junit.jupiter.api.Test;
public class TestHTTPClientTLS {
private static final String TEST_SERVLET_PATH = "/tls-test";
private static final String TEST_CLIENT_PATH = "tls-test";
+ private static final String TEST_CLIENT_TRUSTSTORE =
"test-client-truststore.p12";
+ private static final String TEST_TRUSTED_CLIENT_KEYSTORE =
"test-trusted-client-keystore.p12";
+ private static final String TEST_UNTRUSTED_CLIENT_KEYSTORE =
"test-untrusted-client-keystore.p12";
private JettyServer jettyServer;
@@ -59,13 +63,11 @@ public class TestHTTPClientTLS {
jettyServer, true, createTestServlet(), TEST_SERVLET_PATH);
TLSConfigurer tlsConfigurer =
- TLSConfigurers.builder()
- .trustStore(
- TestTlsServerUtils.testResource("test-client-truststore.p12"),
TEST_STORE_PASSWORD)
- .keyStore(
-
TestTlsServerUtils.testResource("test-trusted-client-keystore.p12"),
- TEST_STORE_PASSWORD)
- .build();
+ TLSConfigurers.withMutualTls(
+ TestTlsServerUtils.testResource(TEST_CLIENT_TRUSTSTORE),
+ TEST_STORE_PASSWORD,
+ TestTlsServerUtils.testResource(TEST_TRUSTED_CLIENT_KEYSTORE),
+ TEST_STORE_PASSWORD);
try (HTTPClient client = createHttpsClient(port, tlsConfigurer)) {
client.head(TEST_CLIENT_PATH, ImmutableMap.of(), response -> {});
@@ -78,11 +80,11 @@ public class TestHTTPClientTLS {
TestTlsServerUtils.startHttpsServer(
jettyServer, true, createTestServlet(), TEST_SERVLET_PATH);
+ // Creates a TLSConfigurer that only has the trust store, but no client
key store, to simulate a
+ // client that does not present a certificate.
TLSConfigurer tlsConfigurer =
- TLSConfigurers.builder()
- .trustStore(
- TestTlsServerUtils.testResource("test-client-truststore.p12"),
TEST_STORE_PASSWORD)
- .build();
+ TLSConfigurers.withTrustStore(
+ TestTlsServerUtils.testResource(TEST_CLIENT_TRUSTSTORE),
TEST_STORE_PASSWORD);
try (HTTPClient client = createHttpsClient(port, tlsConfigurer)) {
RESTException exception =
@@ -101,13 +103,11 @@ public class TestHTTPClientTLS {
jettyServer, true, createTestServlet(), TEST_SERVLET_PATH);
TLSConfigurer tlsConfigurer =
- TLSConfigurers.builder()
- .trustStore(
- TestTlsServerUtils.testResource("test-client-truststore.p12"),
TEST_STORE_PASSWORD)
- .keyStore(
-
TestTlsServerUtils.testResource("test-untrusted-client-keystore.p12"),
- TEST_STORE_PASSWORD)
- .build();
+ TLSConfigurers.withMutualTls(
+ TestTlsServerUtils.testResource(TEST_CLIENT_TRUSTSTORE),
+ TEST_STORE_PASSWORD,
+ TestTlsServerUtils.testResource(TEST_UNTRUSTED_CLIENT_KEYSTORE),
+ TEST_STORE_PASSWORD);
try (HTTPClient client = createHttpsClient(port, tlsConfigurer)) {
RESTException exception =
@@ -126,10 +126,8 @@ public class TestHTTPClientTLS {
jettyServer, false, createTestServlet(), TEST_SERVLET_PATH);
TLSConfigurer tlsConfigurer =
- TLSConfigurers.builder()
- .trustStore(
- TestTlsServerUtils.testResource("test-client-truststore.p12"),
TEST_STORE_PASSWORD)
- .build();
+ TLSConfigurers.withTrustStore(
+ TestTlsServerUtils.testResource(TEST_CLIENT_TRUSTSTORE),
TEST_STORE_PASSWORD);
try (HTTPClient client = createHttpsClient(port, tlsConfigurer)) {
client.head(TEST_CLIENT_PATH, ImmutableMap.of(), response -> {});
@@ -143,11 +141,9 @@ public class TestHTTPClientTLS {
jettyServer, false, createTestServlet(), TEST_SERVLET_PATH);
TLSConfigurer tlsConfigurer =
- TLSConfigurers.builder()
- .trustStore(
-
TestTlsServerUtils.testResource("test-untrusted-server-truststore.p12"),
- TEST_STORE_PASSWORD)
- .build();
+ TLSConfigurers.withTrustStore(
+
TestTlsServerUtils.testResource("test-untrusted-server-truststore.p12"),
+ TEST_STORE_PASSWORD);
try (HTTPClient client = createHttpsClient(port, tlsConfigurer)) {
RESTException exception =
@@ -159,6 +155,78 @@ public class TestHTTPClientTLS {
}
}
+ /*
+ * The current test server uses a self-signed certificate that is not
trusted by the JVM's
+ * default truststore. Therefore, a successful keystore-only mTLS handshake
cannot be
+ * demonstrated with the existing test fixtures.
+ *
+ * This test verifies that withKeyStore() preserves default JVM certificate
verification
+ * and rejects the untrusted server certificate.
+ *
+ * A future test fixture using a server certificate trusted by the test JVM
should allow
+ * a successful handshake to be tested without configuring a custom client
truststore.
+ */
+
+ /*
+ * TODO: Add a successful keystore-only mTLS test when a test server
certificate
+ * trusted by the default JVM truststore is available.
+ *
+ * The future test should:
+ * 1. Start an HTTPS server requiring client authentication with a
certificate
+ * trusted by the test JVM.
+ * 2. Configure the client using only withKeyStore().
+ * 3. Verify that the HTTPS request succeeds.
+ *
+ * Example client configuration:
+ *
+ * TLSConfigurer tlsConfigurer =
+ * TLSConfigurers.withKeyStore(
+ * TestTlsServerUtils.testResource(TEST_TRUSTED_CLIENT_KEYSTORE),
+ * TEST_STORE_PASSWORD);
+ *
+ * try (HTTPClient client = createHttpsClient(port, tlsConfigurer)) {
+ * client.head(TEST_CLIENT_PATH, ImmutableMap.of(), response -> {});
+ * }
+ */
+
+ @Test
+ public void testMutualTLSWithDefaultTrustStoreRejectsUntrustedServer()
throws Exception {
+ int port =
+ TestTlsServerUtils.startHttpsServer(
+ jettyServer, true, createTestServlet(), TEST_SERVLET_PATH);
+
+ TLSConfigurer tlsConfigurer =
+ TLSConfigurers.withKeyStore(
+ TestTlsServerUtils.testResource(TEST_TRUSTED_CLIENT_KEYSTORE),
TEST_STORE_PASSWORD);
+
+ try (HTTPClient client = createHttpsClient(port, tlsConfigurer)) {
+ RESTException exception =
+ assertThrows(
+ RESTException.class,
+ () -> client.head(TEST_CLIENT_PATH, ImmutableMap.of(), response
-> {}));
+
+ TestTlsServerUtils.assertHandshakeFailure(exception);
+ }
+ }
+
+ @Test
+ public void testHttpsWithDefaultTlsRejectsUntrustedServer() throws Exception
{
+ int port =
+ TestTlsServerUtils.startHttpsServer(
+ jettyServer, false, createTestServlet(), TEST_SERVLET_PATH);
+
+ try (HTTPClient client =
+ HTTPClient.builder(ImmutableMap.of()).uri("https://localhost:" +
port).build()) {
+
+ RESTException exception =
+ assertThrows(
+ RESTException.class,
+ () -> client.head(TEST_CLIENT_PATH, ImmutableMap.of(), response
-> {}));
+
+ TestTlsServerUtils.assertHandshakeFailure(exception);
+ }
+ }
+
@Test
public void testHttpWithoutCustomTlsConfiguration() throws Exception {
int port =
@@ -188,6 +256,115 @@ public class TestHTTPClientTLS {
() -> TLSConfigurers.builder().trustStore(trustStore,
"wrong-password").build());
}
+ @Test
+ public void testTlsProtocolRestrictionAllowsMatchingProtocol() throws
Exception {
+ int port =
+ TestTlsServerUtils.startHttpsServer(
+ jettyServer, false, createTestServlet(), TEST_SERVLET_PATH,
"TLSv1.2", null);
+
+ TLSConfigurer allowedConfigurer =
+ TLSConfigurers.builder()
+ .trustStore(
+ TestTlsServerUtils.testResource(TEST_CLIENT_TRUSTSTORE),
TEST_STORE_PASSWORD)
+ .protocols("TLSv1.2")
+ .build();
+
+ try (HTTPClient client = createHttpsClient(port, allowedConfigurer)) {
+ client.head(TEST_CLIENT_PATH, ImmutableMap.of(), response -> {});
+ }
+ }
+
+ @Test
+ public void testTlsProtocolRestrictionRejectsMismatchedProtocol() throws
Exception {
+ int port =
+ TestTlsServerUtils.startHttpsServer(
+ jettyServer, false, createTestServlet(), TEST_SERVLET_PATH,
"TLSv1.2", null);
+
+ TLSConfigurer restrictedConfigurer =
+ TLSConfigurers.builder()
+ .trustStore(
+ TestTlsServerUtils.testResource(TEST_CLIENT_TRUSTSTORE),
TEST_STORE_PASSWORD)
+ .protocols("TLSv1.3")
+ .build();
+
+ try (HTTPClient client = createHttpsClient(port, restrictedConfigurer)) {
+ RESTException exception =
+ assertThrows(
+ RESTException.class,
+ () -> client.head(TEST_CLIENT_PATH, ImmutableMap.of(), response
-> {}));
+
+ TestTlsServerUtils.assertHandshakeFailure(exception);
+ }
+ }
+
+ @Test
+ public void testTlsCipherSuiteRestrictionAllowsMatchingCipher() throws
Exception {
+ String serverCipher = "TLS_AES_128_GCM_SHA256";
+
+ int port =
+ TestTlsServerUtils.startHttpsServer(
+ jettyServer, false, createTestServlet(), TEST_SERVLET_PATH,
"TLSv1.3", serverCipher);
+
+ TLSConfigurer allowedConfigurer =
+ TLSConfigurers.builder()
+ .trustStore(
+ TestTlsServerUtils.testResource(TEST_CLIENT_TRUSTSTORE),
TEST_STORE_PASSWORD)
+ .protocols("TLSv1.3")
+ .cipherSuites(serverCipher)
+ .build();
+
+ try (HTTPClient client = createHttpsClient(port, allowedConfigurer)) {
+ client.head(TEST_CLIENT_PATH, ImmutableMap.of(), response -> {});
+ }
+ }
+
+ @Test
+ public void testTlsCipherSuiteRestrictionRejectsMismatchedCipher() throws
Exception {
+ String serverCipher = "TLS_AES_128_GCM_SHA256";
+ String clientCipher = "TLS_AES_256_GCM_SHA384";
+
+ int port =
+ TestTlsServerUtils.startHttpsServer(
+ jettyServer, false, createTestServlet(), TEST_SERVLET_PATH,
"TLSv1.3", serverCipher);
+
+ TLSConfigurer restrictedConfigurer =
+ TLSConfigurers.builder()
+ .trustStore(
+ TestTlsServerUtils.testResource(TEST_CLIENT_TRUSTSTORE),
TEST_STORE_PASSWORD)
+ .protocols("TLSv1.3")
+ .cipherSuites(clientCipher)
+ .build();
+
+ try (HTTPClient client = createHttpsClient(port, restrictedConfigurer)) {
+ RESTException exception =
+ assertThrows(
+ RESTException.class,
+ () -> client.head(TEST_CLIENT_PATH, ImmutableMap.of(), response
-> {}));
+
+ TestTlsServerUtils.assertHandshakeFailure(exception);
+ }
+ }
+
+ @Test
+ public void testUnsupportedTlsProtocolFailsAtBuild() {
+ IllegalArgumentException exception =
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> TLSConfigurers.builder().protocols("TLSv9.9").build());
+
+ assertEquals("Unsupported TLS protocol: TLSv9.9", exception.getMessage());
+ }
+
+ @Test
+ public void testUnsupportedTlsCipherSuiteFailsAtBuild() {
+ IllegalArgumentException exception =
+ assertThrows(
+ IllegalArgumentException.class,
+ () ->
TLSConfigurers.builder().cipherSuites("INVALID_CIPHER").build());
+
+ assertEquals("Unsupported TLS cipher suite: INVALID_CIPHER",
exception.getMessage());
+ }
+
private static HTTPClient createHttpsClient(int port, TLSConfigurer
tlsConfigurer) {
return HTTPClient.builder(ImmutableMap.of())
.uri("https://localhost:" + port)
diff --git a/docs/how-to-use-gravitino-client.md
b/docs/how-to-use-gravitino-client.md
index b774f4fcc9..6c7dc2a4b6 100644
--- a/docs/how-to-use-gravitino-client.md
+++ b/docs/how-to-use-gravitino-client.md
@@ -11,8 +11,8 @@ license: "This software is licensed under the Apache License
version 2."
Use Gravitino Java client library with Spark, Spring and other Java
environment or
use Gravitino Python client library with Spark, PyTorch, Tensorflow, Ray and
Python environment.
-First of all, you must have a Gravitino server set up and run, you can refer
document of
-[how to install Gravitino](./how-to-install.md) to build Gravitino server from
source code and
+First of all, you must have a Gravitino server set up and run, you can refer
document of
+[how to install Gravitino](./how-to-install.md) to build Gravitino server from
source code and
install it in your local.
## Java Client
@@ -21,37 +21,42 @@ Customize the Gravitino Java client by using
`withClientConfig` like this:
```java
Map<String, String> properties =
- ImmutableMap.of(
- "gravitino.client.connectionTimeoutMs", "10",
- "gravitino.client.socketTimeoutMs", "10"
- );
+ ImmutableMap.of(
+ "gravitino.client.connectionTimeoutMs", "10",
+ "gravitino.client.socketTimeoutMs", "10");
GravitinoClient gravitinoClient =
GravitinoClient.builder("http://localhost:8090")
- .withMetalake("metalake")
- .withClientConfig(properties) // add custom client config (optional)
- .build();
+ .withMetalake("metalake")
+ .withClientConfig(properties) // add custom client config (optional)
+ .build();
GravitinoAdminClient gravitinoAdminClient =
GravitinoAdminClient.builder("http://localhost:8090")
- .withClientConfig(properties) // add custom client config (optional)
- .build();
+ .withClientConfig(properties) // add custom client config (optional)
+ .build();
// ...
```
### Java Client Configuration
-| Configuration item | Description
| Default value | Required |
-|----------------------------------------|------------------------------------------------------|---------------------|----------|
-| `gravitino.client.connectionTimeoutMs` | An optional http connection timeout
in milliseconds. | `180000`(3 minutes) | No |
+| Configuration item | Description | Default value | Required |
+|--------------------|-------------|---------------|----------|
+| `gravitino.client.connectionTimeoutMs` | An optional http connection timeout
in milliseconds. | `180000`(3 minutes) | No |
| `gravitino.client.retryTimes` | Maximum automatic HTTP retries after the
initial request. Must be a nonnegative integer; `0` disables retries. | `1` |
No |
-| `gravitino.client.socketTimeoutMs` | An optional http socket timeout in
milliseconds. | `180000`(3 minutes) | No |
+| `gravitino.client.socketTimeoutMs` | An optional http socket timeout in
milliseconds. | `180000`(3 minutes) | No |
**Note:** Invalid configuration properties will result in exceptions.
### Java Client TLS Configuration
-To connect to a Gravitino server over HTTPS using a private certificate
authority or a custom trust store, configure a `TLSConfigurer` and pass it to
the client builder:
+The Gravitino Java client supports custom TLS configuration through
`TLSConfigurers`. This includes custom truststores, mutual TLS (mTLS)
authentication, and restrictions on TLS protocols and cipher suites.
+
+For HTTPS connections using the default JVM truststore without client
certificate authentication, no additional TLS configuration is required.
+
+#### Custom Truststore
+
+To connect to a server using a private certificate authority (CA) or a
self-signed certificate, use `withTrustStore()`:
```java
import java.nio.file.Path;
@@ -61,11 +66,9 @@ import org.apache.gravitino.client.TLSConfigurer;
import org.apache.gravitino.client.TLSConfigurers;
TLSConfigurer tlsConfigurer =
- TLSConfigurers.builder()
- .trustStore(
- Path.of("/path/to/client-truststore.p12"),
- "truststore-password")
- .build();
+ TLSConfigurers.withTrustStore(
+ Path.of("/path/to/truststore.p12"),
+ "truststore-password");
GravitinoClient gravitinoClient =
GravitinoClient.builder("https://localhost:8433")
@@ -79,29 +82,73 @@ GravitinoAdminClient gravitinoAdminClient =
.build();
```
-The trust store contains certificates that the client trusts when verifying
the Gravitino server.
+**Note:** Configuring a custom truststore through `withTrustStore()`,
`withMutualTls()`, or `builder().trustStore(...)` replaces the JVM's default
trust configuration for this client rather than merging with it. Certificates
trusted only by the JVM's default truststore will no longer be trusted
automatically. If the client needs to connect to servers using both private and
public certificate authorities, the custom truststore must contain the
necessary trusted certificates.
+
+#### Mutual TLS Authentication
+
+To authenticate with a client certificate while using a custom truststore to
verify the server certificate, use `withMutualTls()`:
+
+```java
+TLSConfigurer tlsConfigurer =
+ TLSConfigurers.withMutualTls(
+ Path.of("/path/to/truststore.p12"),
+ "truststore-password",
+ Path.of("/path/to/keystore.p12"),
+ "keystore-password");
+```
+
+The truststore contains certificates used to verify the Gravitino server,
while the client keystore contains the client certificate and private key used
for authentication.
+
+#### Mutual TLS with Default JVM Truststore
+
+If the server certificate is already trusted by the default JVM truststore,
only a client keystore is required:
+
+```java
+TLSConfigurer tlsConfigurer =
+ TLSConfigurers.withKeyStore(
+ Path.of("/path/to/keystore.p12"),
+ "keystore-password");
+```
+
+The default JVM truststore is used to verify the server certificate, while the
client keystore provides the certificate used for client authentication.
+
+Use `withKeyStore()` when the server certificate is already trusted by the
JVM. If the server uses a private CA or a self-signed certificate that is not
trusted by the JVM, use `withMutualTls()` to configure both the client keystore
and a custom truststore.
+
+#### TLS Protocol and Cipher Suite Configuration
+
+Use the builder to restrict the TLS protocols and cipher suites that the
client may negotiate:
+
+```java
+TLSConfigurer tlsConfigurer =
+ TLSConfigurers.builder()
+ .protocols("TLSv1.3")
+ .cipherSuites("TLS_AES_128_GCM_SHA256")
+ .build();
+```
-For mutual TLS (mTLS), configure both a trust store and a client key store:
+These settings can also be combined with custom truststores and client
keystores:
```java
TLSConfigurer tlsConfigurer =
TLSConfigurers.builder()
.trustStore(
- Path.of("/path/to/client-truststore.p12"),
+ Path.of("/path/to/truststore.p12"),
"truststore-password")
.keyStore(
- Path.of("/path/to/client-keystore.p12"),
+ Path.of("/path/to/keystore.p12"),
"keystore-password")
- .build();
-
-GravitinoClient gravitinoClient =
- GravitinoClient.builder("https://localhost:8433")
- .withMetalake("metalake")
- .withTlsConfigurer(tlsConfigurer)
+ .protocols("TLSv1.3")
+ .cipherSuites("TLS_AES_128_GCM_SHA256")
.build();
```
-The key store contains the client certificate and private key used when the
Gravitino server requires client certificate authentication.
+If no protocol or cipher-suite restrictions are specified, the TLS
implementation's defaults are used.
+
+By default, the TLS helper uses the `PKCS12` store type for both truststores
and keystores. Use `storeType()` to configure a different store type.
+
+All examples can be used with `GravitinoClient` or `GravitinoAdminClient`
through `withTlsConfigurer()`.
+
+For instructions on configuring the Gravitino HTTPS server and creating a
local truststore, see [HTTPS](./security/how-to-use-https.md).
## Python Client
@@ -109,8 +156,8 @@ Customize the Gravitino Python client with config
properties like this:
```python
gravitino_admin_client = GravitinoAdminClient(
- uri="http://localhost:8090",
- client_config={"gravitino_client_request_timeout": 60},
+ uri="http://localhost:8090",
+ client_config={"gravitino_client_request_timeout": 60},
)
try:
# ...
@@ -118,9 +165,9 @@ finally:
gravitino_admin_client.close()
gravitino_client = GravitinoClient(
- uri="http://localhost:8090",
- metalake_name="test",
- client_config={"gravitino_client_request_timeout": 60},
+ uri="http://localhost:8090",
+ metalake_name="test",
+ client_config={"gravitino_client_request_timeout": 60},
)
try:
# ...
@@ -130,11 +177,11 @@ finally:
### Python Client Configuration
-| Configuration item | Description
| Default value | Required |
-|------------------------------------|----------------------------------------|---------------|----------|
-| `gravitino_client_request_timeout` | An optional client timeout in seconds.
| `10` | No |
+| Configuration item | Description | Default value | Required |
+|--------------------|-------------|---------------|----------|
+| `gravitino_client_request_timeout` | An optional client timeout in seconds.
| `10` | No |
-**Note:** Invalid configuration properties will result in exceptions.
+**Note:** Invalid configuration properties will result in exceptions.
## Retrying concurrent metadata changes
@@ -143,6 +190,7 @@ or a model-version link, the server may return HTTP 409
with error code `1012`.
rename wins. A model-version read may also return this conflict if concurrent
changes prevent
obtaining consistent metadata within the server's bounded read attempts. The
Java and Python
clients raise `OptimisticLockException` for error code `1012`.
+
Import the exception in Java:
```java
@@ -158,4 +206,4 @@ from gravitino.exceptions.base import
OptimisticLockException
Catch this exception, reload the latest metadata, reconsider your intended
change, and
retry with a bounded number of attempts. Do not replay a stale update
unchanged or
retry every HTTP 409 response, since other conflicts may require a different
action.
-For a model-version read conflict, retry the read with a bounded number of
attempts.
+For a model-version read conflict, retry the read with a bounded number of
attempts.
\ No newline at end of file
diff --git a/docs/security/how-to-use-https.md
b/docs/security/how-to-use-https.md
index e82d101098..569f4eda41 100644
--- a/docs/security/how-to-use-https.md
+++ b/docs/security/how-to-use-https.md
@@ -18,20 +18,20 @@ A server instance serves one protocol. Enabling HTTPS stops
the plain HTTP liste
The Gravitino server and the Iceberg REST service are configured separately
with the same property names under different prefixes. Use
`gravitino.server.webserver.` for the Gravitino server and
`gravitino.iceberg-rest.` for the Iceberg REST service.
-| Property Name | Description |
Default Value | Required |
-|---------------------------|------------------------------------------------|---------------|--------------------------------|
-| `enableHttps` | Enables HTTPS |
`false` | No |
-| `httpsPort` | HTTPS port for the Jetty web server |
`8433` and `9433` | No |
-| `keyStorePath` | Path to the key store file |
(none) | Yes |
-| `keyStorePassword` | Password for the key store |
(none) | Yes |
-| `managerPassword` | Manager password for the key store |
(none) | Yes |
-| `keyStoreType` | Key store type |
`JKS` | No |
-| `tlsProtocol` | TLS protocol to use, which the JVM must support|
(none) | No |
-| `enableCipherAlgorithms` | Cipher algorithms to enable |
(empty) | No |
-| `enableClientAuth` | Requires clients to authenticate with a
certificate | `false` | No |
-| `trustStorePath` | Path to the trust store file |
(none) | Yes with client authentication |
-| `trustStorePassword` | Password for the trust store |
(none) | Yes with client authentication |
-| `trustStoreType` | Trust store type |
`JKS` | No |
+| Property Name | Description | Default Value | Required |
+|---------------|-------------|---------------|----------|
+| `enableHttps` | Enables HTTPS | `false` | No |
+| `httpsPort` | HTTPS port for the Jetty web server | `8433` and `9433` | No |
+| `keyStorePath` | Path to the key store file | (none) | Yes |
+| `keyStorePassword` | Password for the key store | (none) | Yes |
+| `managerPassword` | Manager password for the key store | (none) | Yes |
+| `keyStoreType` | Key store type | `JKS` | No |
+| `tlsProtocol` | TLS protocol to use, which the JVM must support | (none) |
No |
+| `enableCipherAlgorithms` | Cipher algorithms to enable | (empty) | No |
+| `enableClientAuth` | Requires clients to authenticate with a certificate |
`false` | No |
+| `trustStorePath` | Path to the trust store file | (none) | Yes with client
authentication |
+| `trustStorePassword` | Password for the trust store | (none) | Yes with
client authentication |
+| `trustStoreType` | Trust store type | `JKS` | No |
The default HTTPS port is `8433` for the Gravitino server and `9433` for the
Iceberg REST service. Everything in the Required column applies once
`enableHttps` is `true`.
@@ -39,47 +39,54 @@ For the values `tlsProtocol` and `enableCipherAlgorithms`
accept, see the "Addit
## Local Development Example
-The following produces a self-signed certificate so you can exercise an HTTPS
endpoint on one machine. It is not a production setup, since self-signed
certificates are generally not how certificates are managed in a real
deployment.
+The following example generates a self-signed certificate for testing HTTPS
locally. It uses a dedicated client truststore to establish trust without
modifying the JVM's default truststore. This setup is intended for local
development rather than production deployments.
**1. Generate a key store.**
```shell
-cd $JAVA_HOME
-bin/keytool -genkeypair -alias localhost \
+mkdir -p gravitino-tls-test
+cd gravitino-tls-test
+
+"$JAVA_HOME/bin/keytool" -genkeypair -alias localhost \
-keyalg RSA -keysize 4096 -keypass {key_password} \
-sigalg SHA256withRSA \
-keystore localhost.jks -storetype JKS -storepass {store_password} \
-dname "cn=localhost,ou=localhost,o=localhost,l=beijing,st=beijing,c=cn" \
+ -ext "SAN=dns:localhost" \
-validity 36500
```
**2. Export the certificate.**
```shell
-bin/keytool -export -alias localhost -keystore localhost.jks \
+"$JAVA_HOME/bin/keytool" -export -alias localhost -keystore localhost.jks \
-file localhost.crt -storepass {store_password}
```
-**3. Import it into a client trust store** so the Gravitino Java client can
trust the server certificate without modifying the JVM-wide trust store.
+**3. Create a separate trust store** so a local Java client will accept the
server certificate.
```shell
-bin/keytool -importcert -alias localhost \
- -keystore client-truststore.p12 -storetype PKCS12 \
+"$JAVA_HOME/bin/keytool" -importcert -alias localhost \
-file localhost.crt \
- -storepass {truststore_password} -noprompt
+ -keystore client-truststore.p12 \
+ -storetype PKCS12 \
+ -storepass {truststore_password} \
+ -noprompt
```
-**4. Configure the server.** Append the following to `conf/gravitino.conf`,
then start Gravitino. Configuration files do not resolve environment variables,
so write the expanded path rather than `${JAVA_HOME}`.
+Replace `{truststore_password}` with your chosen truststore password. Use the
same password when configuring the Java client in step 5.
+
+**4. Configure the server.** Append the following to `conf/gravitino.conf`,
then start Gravitino. Configuration files do not resolve environment variables,
so specify the absolute path to the keystore generated in step 1.
```properties
gravitino.server.webserver.host = localhost
gravitino.server.webserver.enableHttps = true
-gravitino.server.webserver.keyStorePath = {java_home}/localhost.jks
+gravitino.server.webserver.keyStorePath =
/absolute/path/to/gravitino-tls-test/localhost.jks
gravitino.server.webserver.keyStorePassword = {store_password}
gravitino.server.webserver.managerPassword = {key_password}
```
-**5. Connect.** From Java, configure the client to trust the server
certificate using the client trust store:
+**5. Connect.** From Java, configure the client to trust the server
certificate using the dedicated client truststore:
```java
import java.nio.file.Path;
@@ -90,31 +97,32 @@ import org.apache.gravitino.client.TLSConfigurer;
import org.apache.gravitino.client.TLSConfigurers;
public class Main {
- public static void main(String[] args) {
- String uri = "https://localhost:8433";
- TLSConfigurer tlsConfigurer =
- TLSConfigurers.builder()
- .trustStore(
- Path.of("/path/to/client-truststore.p12"),
- "{truststore_password}")
- .build();
-
- GravitinoClient client =
- GravitinoClient.builder(uri)
- .withMetalake("metalake")
- .withTlsConfigurer(tlsConfigurer)
- .build();
-
- GravitinoVersion gravitinoVersion = client.getVersion();
- System.out.println(gravitinoVersion);
- }
+ public static void main(String[] args) {
+ String uri = "https://localhost:8433";
+
+ TLSConfigurer tlsConfigurer =
+ TLSConfigurers.withTrustStore(
+
Path.of("/absolute/path/to/gravitino-tls-test/client-truststore.p12"),
+ "truststore_password");
+
+ GravitinoClient client =
+ GravitinoClient.builder(uri)
+ .withMetalake("metalake")
+ .withTlsConfigurer(tlsConfigurer)
+ .build();
+
+ GravitinoVersion gravitinoVersion = client.getVersion();
+ System.out.println(gravitinoVersion);
+ }
}
```
+Replace `"truststore_password"` with the password configured in step 3.
+
From `curl`, convert the certificate to PEM first:
```shell
-openssl x509 -inform der -in $JAVA_HOME/localhost.crt -out certificate.pem
+openssl x509 -inform der -in localhost.crt -out certificate.pem
curl -v -X GET --cacert ./certificate.pem \
-H "Accept: application/vnd.gravitino.v1+json" \
https://localhost:8433/api/version
@@ -124,3 +132,4 @@ curl -v -X GET --cacert ./certificate.pem \
- [Configurations](../gravitino-server-config.md) for the rest of the web
server settings
- [How to Authenticate](how-to-authenticate.md) for the authentication methods
HTTPS protects
+- [Java Client](../how-to-use-gravitino-client.md) for custom TLS
configurations, mutual TLS, and protocol/cipher-suite restrictions
diff --git
a/server-common/src/test/java/org/apache/gravitino/server/web/TestTlsServerUtils.java
b/server-common/src/test/java/org/apache/gravitino/server/web/TestTlsServerUtils.java
index 7682442a1e..39ec446689 100644
---
a/server-common/src/test/java/org/apache/gravitino/server/web/TestTlsServerUtils.java
+++
b/server-common/src/test/java/org/apache/gravitino/server/web/TestTlsServerUtils.java
@@ -26,6 +26,7 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Objects;
+import java.util.Optional;
import javax.net.ssl.SSLHandshakeException;
import javax.servlet.http.HttpServlet;
import org.apache.gravitino.Config;
@@ -38,7 +39,12 @@ public final class TestTlsServerUtils {
private TestTlsServerUtils() {}
public static int startHttpsServer(
- JettyServer server, boolean clientAuth, HttpServlet servlet, String
servletPath)
+ JettyServer server,
+ boolean clientAuth,
+ HttpServlet servlet,
+ String servletPath,
+ String protocol,
+ String cipherSuite)
throws Exception {
int port = RESTUtils.findAvailablePort(6000, 7000);
@@ -61,6 +67,16 @@ public final class TestTlsServerUtils {
config.set(JettyServerConfig.SSL_TRUST_STORE_PASSWORD,
TEST_STORE_PASSWORD);
}
+ // Configure TLS protocol when provided.
+ if (protocol != null) {
+ config.set(JettyServerConfig.SSL_PROTOCOL, Optional.of(protocol));
+ }
+
+ // Configure allowed cipher suites when provided.
+ if (cipherSuite != null) {
+ config.set(JettyServerConfig.ENABLE_CIPHER_ALGORITHMS, cipherSuite);
+ }
+
server.initialize(JettyServerConfig.fromConfig(config), "test", false);
server.start();
server.addServlet(servlet, servletPath);
@@ -68,6 +84,12 @@ public final class TestTlsServerUtils {
return port;
}
+ public static int startHttpsServer(
+ JettyServer server, boolean clientAuth, HttpServlet servlet, String
servletPath)
+ throws Exception {
+ return startHttpsServer(server, clientAuth, servlet, servletPath, null,
null);
+ }
+
public static int startHttpServer(JettyServer server, HttpServlet servlet,
String servletPath)
throws Exception {