Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/content.zh/docs/deployment/security/security-ssl.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,10 @@ For REST endpoints, by default the keystore is used by the server endpoint, and
to accept the server's certificate. In the case where the REST keystore has a self-signed certificate, the truststore must trust that certificate directly.
If the REST endpoint uses a certificate that is signed through a proper certification hierarchy, the roots of that hierarchy should
be in the trust store.
If no truststore is configured, the REST clients verify the server's certificate against the default truststore of the JDK.

If mutual authentication is enabled, the keystore and the truststore are used by both, the server endpoint and the REST clients as with internal connectivity.
The server endpoint uses the truststore to verify the certificates of the clients, so it requires a truststore in this case and fails to start without one.

```yaml
security.ssl.rest.keystore: /path/to/file.keystore
Expand Down
2 changes: 2 additions & 0 deletions docs/content/docs/deployment/security/security-ssl.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,10 @@ For REST endpoints, by default the keystore is used by the server endpoint, and
to accept the server's certificate. In the case where the REST keystore has a self-signed certificate, the truststore must trust that certificate directly.
If the REST endpoint uses a certificate that is signed through a proper certification hierarchy, the roots of that hierarchy should
be in the trust store.
If no truststore is configured, the REST clients verify the server's certificate against the default truststore of the JDK.

If mutual authentication is enabled, the keystore and the truststore are used by both, the server endpoint and the REST clients as with internal connectivity.
The server endpoint uses the truststore to verify the certificates of the clients, so it requires a truststore in this case and fails to start without one.

```yaml
security.ssl.rest.keystore: /path/to/file.keystore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,18 +199,21 @@ private static Optional<TrustManagerFactory> getTrustManagerFactory(
Configuration config, boolean internal)
throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException {

final ConfigOption<String> truststoreOption =
internal
? SecurityOptions.SSL_INTERNAL_TRUSTSTORE
: SecurityOptions.SSL_REST_TRUSTSTORE;
final ConfigOption<String> truststorePasswordOption =
internal
? SecurityOptions.SSL_INTERNAL_TRUSTSTORE_PASSWORD
: SecurityOptions.SSL_REST_TRUSTSTORE_PASSWORD;

String trustStoreFilePath =
config.get(
internal
? SecurityOptions.SSL_INTERNAL_TRUSTSTORE
: SecurityOptions.SSL_REST_TRUSTSTORE,
config.get(SecurityOptions.SSL_TRUSTSTORE));
config.get(truststoreOption, config.get(SecurityOptions.SSL_TRUSTSTORE));

String trustStorePassword =
config.get(
internal
? SecurityOptions.SSL_INTERNAL_TRUSTSTORE_PASSWORD
: SecurityOptions.SSL_REST_TRUSTSTORE_PASSWORD,
truststorePasswordOption,
config.get(SecurityOptions.SSL_TRUSTSTORE_PASSWORD));
// do not use getAndCheckOption here as there is no fallback option and a default is
// specified
Expand All @@ -225,18 +228,12 @@ private static Optional<TrustManagerFactory> getTrustManagerFactory(
return Optional.empty();
} else if (trustStoreFilePath == null) {
throw new IllegalConfigurationException(
"The config option "
+ SecurityOptions.SSL_INTERNAL_TRUSTSTORE.key()
+ " or "
+ SecurityOptions.SSL_TRUSTSTORE.key()
+ " is missing.");
"The config option %s or %s is missing.",
truststoreOption.key(), SecurityOptions.SSL_TRUSTSTORE.key());
} else if (trustStorePassword == null) {
throw new IllegalConfigurationException(
"The config option "
+ SecurityOptions.SSL_INTERNAL_TRUSTSTORE_PASSWORD.key()
+ " or "
+ SecurityOptions.SSL_TRUSTSTORE_PASSWORD
+ " is missing.");
"The config option %s or %s is missing.",
truststorePasswordOption.key(), SecurityOptions.SSL_TRUSTSTORE_PASSWORD.key());
}

KeyStore trustStore = KeyStore.getInstance(truststoreType);
Expand Down Expand Up @@ -444,15 +441,23 @@ public static SslContext createRestNettySSLContext(

if (clientMode || clientAuth != ClientAuth.NONE) {
Optional<TrustManagerFactory> tmf = getTrustManagerFactory(config, false);
tmf.map(
// Use specific ciphers and protocols if SSL is configured with self-signed
// certificates (user-supplied truststore)
tm ->
sslContextBuilder
.trustManager(tm)
.protocols(sslProtocols)
.ciphers(ciphers)
.clientAuth(clientAuth));
if (tmf.isPresent()) {
// Use specific ciphers and protocols if SSL is configured with self-signed
// certificates (user-supplied truststore)
sslContextBuilder.trustManager(tmf.get()).protocols(sslProtocols).ciphers(ciphers);
} else if (!clientMode) {
// Only a client may fall back to the default truststore of the JDK. A server cannot
// verify the client certificates without a truststore, and must not skip the
// authentication silently.
throw new IllegalConfigurationException(
"Mutual authentication is enabled for the REST endpoints (%s), but no "
+ "truststore is configured to verify the client certificates. "
+ "The config option %s or %s is missing.",
SecurityOptions.SSL_REST_AUTHENTICATION_ENABLED.key(),
SecurityOptions.SSL_REST_TRUSTSTORE.key(),
SecurityOptions.SSL_TRUSTSTORE.key());
}
sslContextBuilder.clientAuth(clientAuth);
}

return sslContextBuilder.sslProvider(provider).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,10 @@ void testRESTClientSSLMissingTrustStore() {
config.set(SecurityOptions.SSL_REST_TRUSTSTORE_PASSWORD, "some password");

assertThatThrownBy(() -> SSLUtils.createRestClientSSLEngineFactory(config))
.isInstanceOf(IllegalConfigurationException.class);
.isInstanceOf(IllegalConfigurationException.class)
.hasMessage(
"The config option security.ssl.rest.truststore "
+ "or security.ssl.truststore is missing.");
}

/** Tests that REST Client SSL creation fails with bad SSL configuration. */
Expand All @@ -150,7 +153,10 @@ void testRESTClientSSLMissingPassword() {
config.set(SecurityOptions.SSL_REST_TRUSTSTORE, TRUST_STORE_PATH);

assertThatThrownBy(() -> SSLUtils.createRestClientSSLEngineFactory(config))
.isInstanceOf(IllegalConfigurationException.class);
.isInstanceOf(IllegalConfigurationException.class)
.hasMessage(
"The config option security.ssl.rest.truststore-password "
+ "or security.ssl.truststore-password is missing.");
}

/** Tests that REST Client SSL creation fails with bad SSL configuration. */
Expand Down Expand Up @@ -246,6 +252,101 @@ void testRESTServerSSLBadKeystoreType(String sslProvider) {
.isInstanceOf(KeyStoreException.class);
}

// ------------------- REST server mutual authentication ---------------------

/**
* Tests that REST mutual authentication without a truststore fails instead of being skipped.
*/
@ParameterizedTest
@MethodSource("parameters")
void testRESTServerSSLMutualAuthWithoutTrustStore(String sslProvider) {
Configuration serverConfig = createRestSslConfigWithKeyStore(sslProvider);
serverConfig.set(SecurityOptions.SSL_REST_AUTHENTICATION_ENABLED, true);

assertThatThrownBy(() -> SSLUtils.createRestServerSSLEngineFactory(serverConfig))
.isInstanceOf(IllegalConfigurationException.class)
.hasMessage(
"Mutual authentication is enabled for the REST endpoints "
+ "(security.ssl.rest.authentication-enabled), but no truststore is "
+ "configured to verify the client certificates. The config option "
+ "security.ssl.rest.truststore or security.ssl.truststore is missing.");
}

/** Tests that a REST server with mutual authentication requires a client certificate. */
@ParameterizedTest
@MethodSource("parameters")
void testRESTServerSSLMutualAuthEnforcesClientCertificate(String sslProvider) throws Exception {
Configuration serverConfig = createRestSslConfigWithKeyAndTrustStores(sslProvider);
serverConfig.set(SecurityOptions.SSL_REST_AUTHENTICATION_ENABLED, true);

assertRESTMutualAuthEnforced(serverConfig, sslProvider);
}

/** Tests that the generic truststore options are accepted for REST mutual authentication. */
@ParameterizedTest
@MethodSource("parameters")
void testRESTServerSSLMutualAuthWithGenericTrustStore(String sslProvider) throws Exception {
Configuration serverConfig = createRestSslConfigWithKeyStore(sslProvider);
serverConfig.set(SecurityOptions.SSL_REST_AUTHENTICATION_ENABLED, true);
serverConfig.set(SecurityOptions.SSL_TRUSTSTORE, TRUST_STORE_PATH);
serverConfig.set(SecurityOptions.SSL_TRUSTSTORE_PASSWORD, TRUST_STORE_PASSWORD);

assertRESTMutualAuthEnforced(serverConfig, sslProvider);
}

/**
* Tests that only the REST server needs a truststore for mutual authentication: a REST client
* without one is created and verifies the server against the default truststore of the JDK.
*/
@ParameterizedTest
@MethodSource("parameters")
void testRESTClientSSLMutualAuthWithoutTrustStore(String sslProvider) throws Exception {
Configuration serverConfig =
withTlsCiphers(createRestSslConfigWithKeyAndTrustStores(sslProvider), sslProvider);
serverConfig.set(SecurityOptions.SSL_REST_AUTHENTICATION_ENABLED, true);
Configuration clientConfig = createRestSslConfigWithKeyStore(sslProvider);
clientConfig.set(SecurityOptions.SSL_REST_AUTHENTICATION_ENABLED, true);

SSLHandlerFactory server = SSLUtils.createRestServerSSLEngineFactory(serverConfig);
SSLHandlerFactory client = SSLUtils.createRestClientSSLEngineFactory(clientConfig);

// the default truststore of the JDK does not contain the self-signed test certificate
assertThatThrownBy(() -> negotiate(server, client))
.hasCauseInstanceOf(SSLHandshakeException.class);
}

/**
* Asserts that a REST server with mutual authentication rejects a client without a certificate
* and accepts a client with a certificate it trusts.
*/
private void assertRESTMutualAuthEnforced(Configuration serverConfig, String sslProvider)
throws Exception {
SSLHandlerFactory server =
SSLUtils.createRestServerSSLEngineFactory(
withTlsCiphers(serverConfig, sslProvider));

// trusts the server, but has no certificate of its own
Configuration withoutCertificate =
withTlsCiphers(createRestSslConfigWithTrustStore(sslProvider), sslProvider);
SSLHandlerFactory clientWithoutCertificate =
SSLUtils.createRestClientSSLEngineFactory(withoutCertificate);
assertThatThrownBy(() -> negotiate(server, clientWithoutCertificate))
.hasCauseInstanceOf(SSLHandshakeException.class);

Configuration withCertificate =
withTlsCiphers(createRestSslConfigWithKeyAndTrustStores(sslProvider), sslProvider);
withCertificate.set(SecurityOptions.SSL_REST_AUTHENTICATION_ENABLED, true);
SSLSession session =
negotiate(server, SSLUtils.createRestClientSSLEngineFactory(withCertificate));
assertThat(session.getLocalCertificates()).hasSize(1);
}

private static Configuration withTlsCiphers(Configuration config, String sslProvider) {
config.set(
SecurityOptions.SSL_ALGORITHMS, tls12Ciphers(sslProvider) + "," + TLS_13_CIPHERS);
return config;
}

// ----------------------- mutual auth contexts --------------------------

@ParameterizedTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
package org.apache.flink.runtime.rest;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.configuration.IllegalConfigurationException;
import org.apache.flink.configuration.RestOptions;
import org.apache.flink.configuration.SecurityOptions;
import org.apache.flink.configuration.WebOptions;
import org.apache.flink.util.ConfigurationException;

Expand All @@ -28,7 +30,9 @@

import java.io.File;

import static org.apache.flink.util.Preconditions.checkNotNull;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

/** Tests for the {@link RestServerEndpointConfiguration}. */
class RestServerEndpointConfigurationTest {
Expand All @@ -38,6 +42,12 @@ class RestServerEndpointConfigurationTest {
private static final String BIND_PORT = "7282";
private static final int CONTENT_LENGTH = 1234;

private static final String KEY_STORE_PATH =
checkNotNull(
RestServerEndpointConfigurationTest.class.getResource(
"/local127.keystore"))
.getFile();

@Test
void testBasicMapping(@TempDir File file) throws ConfigurationException {
Configuration originalConfig = new Configuration();
Expand All @@ -56,4 +66,23 @@ void testBasicMapping(@TempDir File file) throws ConfigurationException {
assertThat(result.getUploadDir().toAbsolutePath().toString())
.contains(file.getAbsolutePath());
}

@Test
void testMutualAuthWithoutTrustStore(@TempDir File file) {
Configuration config = new Configuration();
config.set(RestOptions.ADDRESS, ADDRESS);
config.set(WebOptions.TMP_DIR, file.getAbsolutePath());
config.set(SecurityOptions.SSL_REST_ENABLED, true);
config.set(SecurityOptions.SSL_REST_AUTHENTICATION_ENABLED, true);
config.set(SecurityOptions.SSL_REST_KEYSTORE, KEY_STORE_PATH);
config.set(SecurityOptions.SSL_REST_KEYSTORE_PASSWORD, "password");
config.set(SecurityOptions.SSL_REST_KEY_PASSWORD, "password");

assertThatThrownBy(() -> RestServerEndpointConfiguration.fromConfiguration(config))
.isInstanceOf(ConfigurationException.class)
.hasMessage("Failed to initialize SSLEngineFactory for REST server endpoint.")
.cause()
.isInstanceOf(IllegalConfigurationException.class)
.hasMessageContaining("no truststore is configured");
}
}