mateczagany commented on code in PR #29265: URL: https://github.com/apache/flink/pull/29265#discussion_r4061599092
########## flink-rpc/flink-rpc-akka/src/test/java/org/apache/flink/runtime/rpc/pekko/CustomSSLEngineProviderTest.java: ########## @@ -0,0 +1,180 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.runtime.rpc.pekko; + +import org.apache.flink.configuration.Configuration; +import org.apache.flink.configuration.SecurityOptions; +import org.apache.flink.runtime.concurrent.pekko.ScalaFutureUtils; + +import org.apache.flink.shaded.netty4.io.netty.bootstrap.Bootstrap; +import org.apache.flink.shaded.netty4.io.netty.bootstrap.ServerBootstrap; +import org.apache.flink.shaded.netty4.io.netty.channel.Channel; +import org.apache.flink.shaded.netty4.io.netty.channel.ChannelInitializer; +import org.apache.flink.shaded.netty4.io.netty.channel.EventLoopGroup; +import org.apache.flink.shaded.netty4.io.netty.channel.nio.NioEventLoopGroup; +import org.apache.flink.shaded.netty4.io.netty.channel.socket.nio.NioServerSocketChannel; +import org.apache.flink.shaded.netty4.io.netty.channel.socket.nio.NioSocketChannel; +import org.apache.flink.shaded.netty4.io.netty.handler.ssl.SslHandler; + +import org.apache.pekko.actor.ActorSystem; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +import javax.net.ssl.SSLEngine; +import javax.net.ssl.SSLSession; + +import java.net.InetSocketAddress; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; + +import static org.apache.flink.util.Preconditions.checkNotNull; +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests that {@link CustomSSLEngineProvider} correctly negotiates TLS when {@link + * SecurityOptions#SSL_PROTOCOL} is configured with a comma-separated protocol list. + * + * <p>This is a regression test for the fact that {@link + * org.apache.pekko.remote.transport.netty.ConfigSSLEngineProvider}, which {@link + * CustomSSLEngineProvider} extends, only supports a single protocol name: it feeds the configured + * string, unsplit, into both {@code SSLContext.getInstance(String)} and {@code + * SSLEngine#setEnabledProtocols(String[])}. + */ +class CustomSSLEngineProviderTest { + + private static final String KEY_STORE_PATH = + checkNotNull(CustomSSLEngineProviderTest.class.getResource("/rpc.keystore")).getFile(); + private static final String TRUST_STORE_PATH = + checkNotNull(CustomSSLEngineProviderTest.class.getResource("/rpc.truststore")) + .getFile(); + private static final String STORE_PASSWORD = "password"; + + private static final String TLS_12_CIPHER = + "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384"; + private static final String TLS_13_CIPHER = "TLS_AES_128_GCM_SHA256,TLS_AES_256_GCM_SHA384"; + + private ActorSystem actorSystem; + + @AfterEach + void shutdown() throws Exception { + if (actorSystem != null) { + ScalaFutureUtils.toJava(actorSystem.terminate()).get(10, TimeUnit.SECONDS); + } + } + + @Test + void negotiatesHighestCommonProtocolFromList() throws Exception { + SSLSession session = handshake("TLSv1.2,TLSv1.3", TLS_12_CIPHER + "," + TLS_13_CIPHER); + + assertThat(session.getProtocol()).isEqualTo("TLSv1.3"); + } + + @Test + void fallsBackToLowerListedProtocolWhenHigherOneHasNoUsableCipher() throws Exception { + SSLSession session = handshake("TLSv1.2,TLSv1.3", TLS_12_CIPHER); + + assertThat(session.getProtocol()).isEqualTo("TLSv1.2"); + } + + /** + * Builds a {@link CustomSSLEngineProvider} from the given protocol list and ciphers, performs a + * real, socket-based TLS handshake between a server and a client engine it creates, and returns + * the client's negotiated session. + */ + private SSLSession handshake(String protocolList, String ciphers) throws Exception { + final Configuration configuration = new Configuration(); + configuration.set(SecurityOptions.SSL_INTERNAL_ENABLED, true); + configuration.set(SecurityOptions.SSL_INTERNAL_KEYSTORE, KEY_STORE_PATH); + configuration.set(SecurityOptions.SSL_INTERNAL_KEYSTORE_PASSWORD, STORE_PASSWORD); + configuration.set(SecurityOptions.SSL_INTERNAL_KEY_PASSWORD, STORE_PASSWORD); + configuration.set(SecurityOptions.SSL_INTERNAL_TRUSTSTORE, TRUST_STORE_PATH); + configuration.set(SecurityOptions.SSL_INTERNAL_TRUSTSTORE_PASSWORD, STORE_PASSWORD); + configuration.set(SecurityOptions.SSL_PROTOCOL, protocolList); + configuration.set(SecurityOptions.SSL_ALGORITHMS, ciphers); + + actorSystem = + PekkoUtils.createActorSystem( + "CustomSSLEngineProviderTest", + PekkoUtils.getConfig(configuration, new HostAndPort("localhost", 0))); + + final CustomSSLEngineProvider provider = new CustomSSLEngineProvider(actorSystem); + + final EventLoopGroup group = new NioEventLoopGroup(2); Review Comment: `NioEventLoopGroup` is deprecated, let's use `new MultiThreadIoEventLoopGroup(2, NioIoHandler.newFactory())` here ########## flink-runtime/src/test/java/org/apache/flink/runtime/net/SSLUtilsTest.java: ########## @@ -425,6 +438,258 @@ void testCreateSSLEngineFactory(String sslProvider) throws Exception { assertThat(sslHandler.engine().getEnabledCipherSuites()).contains(sslAlgorithms); } + // -------------------- hostname verification (shared certificate) ------------------- + // + // Internal SSL uses one shared, mutually-trusted certificate across every node in the + // cluster (see docs/content/docs/deployment/security/security-ssl.md: "Because internal + // connections are mutually authenticated with shared certificates, Flink can skip hostname + // verification. This makes container-based setups easier."), e.g. a Kubernetes deployment + // with hundreds of dynamically-scheduled, differently-named pods sharing one cert. Simulate + // that: the client connects to a real "localhost" socket but is told to verify a peer + // identity the shared certificate was never meant to cover; negotiation must still succeed. + + @ParameterizedTest + @MethodSource("parameters") + void testInternalSSLIgnoresPeerHostMismatch(String sslProvider) throws Exception { + Configuration config = createInternalSslConfigWithKeyAndTrustStores(sslProvider); + + SSLSession session = + negotiate( + SSLUtils.createInternalServerSSLEngineFactory(config), + SSLUtils.createInternalClientSSLEngineFactory(config), + "some-taskmanager-42.flink-headless.flink.svc.cluster.local"); + + assertThat(session).isNotNull(); + } + + // -------------------- multi-protocol negotiation ----------------------- + // + // Regression coverage for SecurityOptions.SSL_PROTOCOL accepting a comma-separated protocol + // list: every consumer below is expected to negotiate the highest protocol both sides support, + // and to gracefully fall back rather than fail when the higher protocol has no usable cipher. + + private static final String TLS_13_CIPHERS = "TLS_AES_128_GCM_SHA256,TLS_AES_256_GCM_SHA384"; + + private static String tls12Ciphers(String sslProvider) { + // openSSL does not support the same set of cipher algorithms as the JDK provider, see + // testCreateSSLEngineFactory above. + return sslProvider.equalsIgnoreCase("OPENSSL") + ? "TLS_RSA_WITH_AES_128_GCM_SHA256,TLS_RSA_WITH_AES_256_GCM_SHA384" + : "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384"; + } + + @ParameterizedTest + @MethodSource("parameters") + void testInternalSSLNegotiatesTls13WhenBothSidesSupportIt(String sslProvider) throws Exception { + Configuration config = createInternalSslConfigWithKeyAndTrustStores(sslProvider); + config.set(SecurityOptions.SSL_PROTOCOL, "TLSv1.2,TLSv1.3"); + config.set( + SecurityOptions.SSL_ALGORITHMS, tls12Ciphers(sslProvider) + "," + TLS_13_CIPHERS); + + SSLSession session = + negotiate( + SSLUtils.createInternalServerSSLEngineFactory(config), + SSLUtils.createInternalClientSSLEngineFactory(config)); + + assertThat(session.getProtocol()).isEqualTo("TLSv1.3"); + } + + @ParameterizedTest + @MethodSource("parameters") + void testInternalSSLFallsBackToTls12WithoutTls13Cipher(String sslProvider) throws Exception { + Configuration config = createInternalSslConfigWithKeyAndTrustStores(sslProvider); + config.set(SecurityOptions.SSL_PROTOCOL, "TLSv1.2,TLSv1.3"); + config.set(SecurityOptions.SSL_ALGORITHMS, tls12Ciphers(sslProvider)); + + SSLSession session = + negotiate( + SSLUtils.createInternalServerSSLEngineFactory(config), + SSLUtils.createInternalClientSSLEngineFactory(config)); + + assertThat(session.getProtocol()).isEqualTo("TLSv1.2"); + } + + @ParameterizedTest + @MethodSource("parameters") + void testRestSSLNegotiatesTls13WhenBothSidesSupportIt(String sslProvider) throws Exception { + Configuration config = createRestSslConfigWithKeyAndTrustStores(sslProvider); + config.set(SecurityOptions.SSL_PROTOCOL, "TLSv1.2,TLSv1.3"); + config.set( + SecurityOptions.SSL_ALGORITHMS, tls12Ciphers(sslProvider) + "," + TLS_13_CIPHERS); + + SSLSession session = + negotiate( + SSLUtils.createRestServerSSLEngineFactory(config), + SSLUtils.createRestClientSSLEngineFactory(config)); + + assertThat(session.getProtocol()).isEqualTo("TLSv1.3"); + } + + @ParameterizedTest + @MethodSource("parameters") + void testRestSSLFallsBackToTls12WithoutTls13Cipher(String sslProvider) throws Exception { + Configuration config = createRestSslConfigWithKeyAndTrustStores(sslProvider); + config.set(SecurityOptions.SSL_PROTOCOL, "TLSv1.2,TLSv1.3"); + config.set(SecurityOptions.SSL_ALGORITHMS, tls12Ciphers(sslProvider)); + + SSLSession session = + negotiate( + SSLUtils.createRestServerSSLEngineFactory(config), + SSLUtils.createRestClientSSLEngineFactory(config)); + + assertThat(session.getProtocol()).isEqualTo("TLSv1.2"); + } + + /** + * The Blob server/client socket path ({@link SSLUtils#createSSLServerSocketFactory}) always + * uses the JDK provider regardless of {@link SecurityOptions#SSL_PROVIDER}, so this is not + * parameterized. + */ + @Test + void testBlobSocketSSLNegotiatesTls13WhenBothSidesSupportIt() throws Exception { + Configuration config = createInternalSslConfigWithKeyAndTrustStores("JDK"); + config.set(SecurityOptions.SSL_PROTOCOL, "TLSv1.2,TLSv1.3"); + config.set(SecurityOptions.SSL_ALGORITHMS, tls12Ciphers("JDK") + "," + TLS_13_CIPHERS); + + assertThat(negotiateViaSockets(config)).isEqualTo("TLSv1.3"); + } + + @Test + void testBlobSocketSSLFallsBackToTls12WithoutTls13Cipher() throws Exception { + Configuration config = createInternalSslConfigWithKeyAndTrustStores("JDK"); + config.set(SecurityOptions.SSL_PROTOCOL, "TLSv1.2,TLSv1.3"); + config.set(SecurityOptions.SSL_ALGORITHMS, tls12Ciphers("JDK")); + + assertThat(negotiateViaSockets(config)).isEqualTo("TLSv1.2"); + } + + /** + * Performs a real, socket-based TLS handshake between the given server and client {@link + * SSLHandlerFactory} and returns the client's negotiated session. + */ + private static SSLSession negotiate( + SSLHandlerFactory serverFactory, SSLHandlerFactory clientFactory) throws Exception { + return negotiate(serverFactory, clientFactory, "localhost"); + } + + /** + * Like {@link #negotiate(SSLHandlerFactory, SSLHandlerFactory)}, but the client engine is told + * to verify the given {@code clientPeerIdentity} instead of the real TCP destination + * ("localhost"), so a mismatch between the two can be simulated without breaking the actual + * connection. + */ + private static SSLSession negotiate( + SSLHandlerFactory serverFactory, + SSLHandlerFactory clientFactory, + String clientPeerIdentity) + throws Exception { + final EventLoopGroup group = new NioEventLoopGroup(2); Review Comment: `NioEventLoopGroup` is deprecated, let's use `new MultiThreadIoEventLoopGroup(2, NioIoHandler.newFactory())` here ########## flink-core/src/main/java/org/apache/flink/configuration/SecurityOptions.java: ########## @@ -503,15 +503,31 @@ public static Configuration forProvider(Configuration configuration, String prov // ------------------------ ssl parameters -------------------------------- - /** SSL protocol version to be supported. */ + /** SSL protocol version(s) to be supported. */ @Documentation.Section(Documentation.Sections.SECURITY_SSL) public static final ConfigOption<String> SSL_PROTOCOL = key("security.ssl.protocol") .stringType() .defaultValue("TLSv1.2") .withDescription( - "The SSL protocol version to be supported for the ssl transport. Note that it doesn’t" - + " support comma separated list."); + Description.builder() + .text( + "The comma separated list of SSL protocol versions to be supported for" + + " the ssl transport, e.g. %s. The highest protocol version" + + " supported by both communication endpoints is negotiated; if" + + " none of the listed protocols has a matching cipher suite (see" + + " %s), the connection falls back to a lower listed protocol" + + " instead of failing.", + code("TLSv1.2,TLSv1.3"), + code("security.ssl.algorithms")) + .linebreak() + .text( + "When %s is set to %s, a non-contiguous list also enables the protocol" + + " version(s) in between the lowest and highest listed one," + + " because the underlying engine only supports contiguous" + + " protocol ranges.", + code("security.ssl.provider"), code("OPENSSL")) Review Comment: I would add a test-case for this too: ``` @ParameterizedTest @MethodSource("parameters") void testInternalSSLNonContiguousProtocolList(String sslProvider) throws Exception { Configuration config = createInternalSslConfigWithKeyAndTrustStores(sslProvider); config.set(SecurityOptions.SSL_PROTOCOL, "TLSv1.1,TLSv1.3"); config.set(SecurityOptions.SSL_ALGORITHMS, tls12Ciphers(sslProvider)); final SSLHandlerFactory server = SSLUtils.createInternalServerSSLEngineFactory(config); final SSLHandlerFactory client = SSLUtils.createInternalClientSSLEngineFactory(config); if (sslProvider.equalsIgnoreCase("OPENSSL")) { assertThat(negotiate(server, client).getProtocol()).isEqualTo("TLSv1.2"); } else { assertThatThrownBy(() -> negotiate(server, client)) .hasCauseInstanceOf(SSLHandshakeException.class); } } ``` ########## flink-core/src/main/java/org/apache/flink/configuration/SecurityOptions.java: ########## @@ -503,15 +503,31 @@ public static Configuration forProvider(Configuration configuration, String prov // ------------------------ ssl parameters -------------------------------- - /** SSL protocol version to be supported. */ + /** SSL protocol version(s) to be supported. */ @Documentation.Section(Documentation.Sections.SECURITY_SSL) public static final ConfigOption<String> SSL_PROTOCOL = key("security.ssl.protocol") .stringType() .defaultValue("TLSv1.2") .withDescription( - "The SSL protocol version to be supported for the ssl transport. Note that it doesn’t" - + " support comma separated list."); + Description.builder() + .text( + "The comma separated list of SSL protocol versions to be supported for" + + " the ssl transport, e.g. %s. The highest protocol version" + + " supported by both communication endpoints is negotiated; if" + + " none of the listed protocols has a matching cipher suite (see" Review Comment: `If none of the listed protocols .... falls back to a lower listed protocol` Sounds like the description contradicts itself. - The connection fails if no protocol has a matching cipher suite. - If a higher listed protocol has no usable cipher suite, a lower listed protocol can be negotiated instead. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
