You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by nk...@apache.org on 2019/06/15 23:03:03 UTC
[flink] 03/09: [FLINK-12838][network] netty-fy SSL configuration
This is an automated email from the ASF dual-hosted git repository.
nkruber pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git
commit 65e822b67ec93e5cf50e6303c838c3b30935834a
Author: Nico Kruber <ni...@ververica.com>
AuthorDate: Mon Feb 11 15:23:59 2019 +0100
[FLINK-12838][network] netty-fy SSL configuration
Refactor the SSL configuration done for Netty to have it more like the way
Netty intends it to be: using its SslContextBuilder. This will make it much
easier to set a different Netty SSL engine provider.
[hotfix][network] extract key and trust manager factory creation
---
.../flink/mesos/util/MesosArtifactServer.java | 4 +-
.../webmonitor/utils/WebFrontendBootstrap.java | 3 +-
.../runtime/io/network/netty/NettyClient.java | 1 +
.../runtime/io/network/netty/NettyServer.java | 3 +-
.../io/network/netty/SSLHandlerFactory.java | 52 +---
.../flink/runtime/net/RedirectingSslHandler.java | 2 +-
.../org/apache/flink/runtime/net/SSLUtils.java | 280 +++++++++++----------
.../org/apache/flink/runtime/rest/RestClient.java | 2 +-
.../org/apache/flink/runtime/net/SSLUtilsTest.java | 3 +-
.../runtime/rest/RestServerEndpointITCase.java | 2 +-
10 files changed, 176 insertions(+), 176 deletions(-)
diff --git a/flink-mesos/src/main/java/org/apache/flink/mesos/util/MesosArtifactServer.java b/flink-mesos/src/main/java/org/apache/flink/mesos/util/MesosArtifactServer.java
index 89a32f1..af928a0 100644
--- a/flink-mesos/src/main/java/org/apache/flink/mesos/util/MesosArtifactServer.java
+++ b/flink-mesos/src/main/java/org/apache/flink/mesos/util/MesosArtifactServer.java
@@ -126,7 +126,6 @@ public class MesosArtifactServer implements MesosArtifactResolver {
router = new Router();
- final Configuration sslConfig = config;
ChannelInitializer<SocketChannel> initializer = new ChannelInitializer<SocketChannel>() {
@Override
@@ -135,7 +134,8 @@ public class MesosArtifactServer implements MesosArtifactResolver {
// SSL should be the first handler in the pipeline
if (sslFactory != null) {
- ch.pipeline().addLast("ssl", sslFactory.createNettySSLHandler());
+ ch.pipeline().addLast("ssl",
+ sslFactory.createNettySSLHandler(ch.alloc()));
}
ch.pipeline()
diff --git a/flink-runtime-web/src/main/java/org/apache/flink/runtime/webmonitor/utils/WebFrontendBootstrap.java b/flink-runtime-web/src/main/java/org/apache/flink/runtime/webmonitor/utils/WebFrontendBootstrap.java
index cf30fac..82ed405 100644
--- a/flink-runtime-web/src/main/java/org/apache/flink/runtime/webmonitor/utils/WebFrontendBootstrap.java
+++ b/flink-runtime-web/src/main/java/org/apache/flink/runtime/webmonitor/utils/WebFrontendBootstrap.java
@@ -79,7 +79,8 @@ public class WebFrontendBootstrap {
// SSL should be the first handler in the pipeline
if (serverSSLFactory != null) {
- ch.pipeline().addLast("ssl", serverSSLFactory.createNettySSLHandler());
+ ch.pipeline().addLast("ssl",
+ serverSSLFactory.createNettySSLHandler(ch.alloc()));
}
ch.pipeline()
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/io/network/netty/NettyClient.java b/flink-runtime/src/main/java/org/apache/flink/runtime/io/network/netty/NettyClient.java
index d7cad7f..21ef4ca 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/io/network/netty/NettyClient.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/io/network/netty/NettyClient.java
@@ -179,6 +179,7 @@ class NettyClient {
// SSL handler should be added first in the pipeline
if (clientSSLFactory != null) {
SslHandler sslHandler = clientSSLFactory.createNettySSLHandler(
+ channel.alloc(),
serverSocketAddress.getAddress().getCanonicalHostName(),
serverSocketAddress.getPort());
channel.pipeline().addLast("ssl", sslHandler);
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/io/network/netty/NettyServer.java b/flink-runtime/src/main/java/org/apache/flink/runtime/io/network/netty/NettyServer.java
index 8bbda5b..3fe62c7 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/io/network/netty/NettyServer.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/io/network/netty/NettyServer.java
@@ -147,7 +147,8 @@ class NettyServer {
@Override
public void initChannel(SocketChannel channel) throws Exception {
if (sslHandlerFactory != null) {
- channel.pipeline().addLast("ssl", sslHandlerFactory.createNettySSLHandler());
+ channel.pipeline().addLast("ssl",
+ sslHandlerFactory.createNettySSLHandler(channel.alloc()));
}
channel.pipeline().addLast(protocol.getServerChannelHandlers());
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/io/network/netty/SSLHandlerFactory.java b/flink-runtime/src/main/java/org/apache/flink/runtime/io/network/netty/SSLHandlerFactory.java
index 583f038..48b9460 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/io/network/netty/SSLHandlerFactory.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/io/network/netty/SSLHandlerFactory.java
@@ -18,9 +18,10 @@
package org.apache.flink.runtime.io.network.netty;
+import org.apache.flink.shaded.netty4.io.netty.buffer.ByteBufAllocator;
+import org.apache.flink.shaded.netty4.io.netty.handler.ssl.SslContext;
import org.apache.flink.shaded.netty4.io.netty.handler.ssl.SslHandler;
-import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
import static java.util.Objects.requireNonNull;
@@ -30,15 +31,7 @@ import static java.util.Objects.requireNonNull;
*/
public class SSLHandlerFactory {
- private final SSLContext sslContext;
-
- private final String[] enabledProtocols;
-
- private final String[] enabledCipherSuites;
-
- private final boolean clientMode;
-
- private final boolean clientAuthentication;
+ private final SslContext sslContext;
private final int handshakeTimeoutMs;
@@ -54,29 +47,21 @@ public class SSLHandlerFactory {
* default)
*/
public SSLHandlerFactory(
- final SSLContext sslContext,
- final String[] enabledProtocols,
- final String[] enabledCipherSuites,
- final boolean clientMode,
- final boolean clientAuthentication,
+ final SslContext sslContext,
final int handshakeTimeoutMs,
final int closeNotifyFlushTimeoutMs) {
this.sslContext = requireNonNull(sslContext, "sslContext must not be null");
- this.enabledProtocols = requireNonNull(enabledProtocols, "enabledProtocols must not be null");
- this.enabledCipherSuites = requireNonNull(enabledCipherSuites, "cipherSuites must not be null");
- this.clientMode = clientMode;
- this.clientAuthentication = clientAuthentication;
this.handshakeTimeoutMs = handshakeTimeoutMs;
this.closeNotifyFlushTimeoutMs = closeNotifyFlushTimeoutMs;
}
- public SslHandler createNettySSLHandler() {
- return createNettySSLHandler(createSSLEngine());
+ public SslHandler createNettySSLHandler(ByteBufAllocator allocator) {
+ return createNettySSLHandler(createSSLEngine(allocator));
}
- public SslHandler createNettySSLHandler(String hostname, int port) {
- return createNettySSLHandler(createSSLEngine(hostname, port));
+ public SslHandler createNettySSLHandler(ByteBufAllocator allocator, String hostname, int port) {
+ return createNettySSLHandler(createSSLEngine(allocator, hostname, port));
}
private SslHandler createNettySSLHandler(SSLEngine sslEngine) {
@@ -91,24 +76,11 @@ public class SSLHandlerFactory {
return sslHandler;
}
- private SSLEngine createSSLEngine() {
- final SSLEngine sslEngine = sslContext.createSSLEngine();
- configureSSLEngine(sslEngine);
- return sslEngine;
+ private SSLEngine createSSLEngine(ByteBufAllocator allocator) {
+ return sslContext.newEngine(allocator);
}
- private SSLEngine createSSLEngine(String hostname, int port) {
- final SSLEngine sslEngine = sslContext.createSSLEngine(hostname, port);
- configureSSLEngine(sslEngine);
- return sslEngine;
- }
-
- private void configureSSLEngine(SSLEngine sslEngine) {
- sslEngine.setEnabledProtocols(enabledProtocols);
- sslEngine.setEnabledCipherSuites(enabledCipherSuites);
- sslEngine.setUseClientMode(clientMode);
- if (!clientMode) {
- sslEngine.setNeedClientAuth(clientAuthentication);
- }
+ private SSLEngine createSSLEngine(ByteBufAllocator allocator, String hostname, int port) {
+ return sslContext.newEngine(allocator, hostname, port);
}
}
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/net/RedirectingSslHandler.java b/flink-runtime/src/main/java/org/apache/flink/runtime/net/RedirectingSslHandler.java
index 0e6103d..bb0f1b3 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/net/RedirectingSslHandler.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/net/RedirectingSslHandler.java
@@ -78,7 +78,7 @@ public class RedirectingSslHandler extends ByteToMessageDecoder {
}
private void handleSsl(ChannelHandlerContext context) {
- SslHandler sslHandler = sslHandlerFactory.createNettySSLHandler();
+ SslHandler sslHandler = sslHandlerFactory.createNettySSLHandler(context.alloc());
try {
context.pipeline().replace(this, SSL_HANDLER_NAME, sslHandler);
} catch (Throwable t) {
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/net/SSLUtils.java b/flink-runtime/src/main/java/org/apache/flink/runtime/net/SSLUtils.java
index c756fab..dd9e7e7 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/net/SSLUtils.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/net/SSLUtils.java
@@ -18,21 +18,26 @@
package org.apache.flink.runtime.net;
+import org.apache.flink.annotation.VisibleForTesting;
import org.apache.flink.configuration.ConfigOption;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.configuration.IllegalConfigurationException;
import org.apache.flink.configuration.SecurityOptions;
import org.apache.flink.runtime.io.network.netty.SSLHandlerFactory;
+import org.apache.flink.shaded.netty4.io.netty.handler.ssl.ClientAuth;
+import org.apache.flink.shaded.netty4.io.netty.handler.ssl.JdkSslContext;
+import org.apache.flink.shaded.netty4.io.netty.handler.ssl.SslContext;
+import org.apache.flink.shaded.netty4.io.netty.handler.ssl.SslContextBuilder;
+import org.apache.flink.shaded.netty4.io.netty.handler.ssl.SslProvider;
+
import javax.annotation.Nullable;
import javax.net.ServerSocketFactory;
import javax.net.SocketFactory;
-import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
-import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import java.io.File;
@@ -42,6 +47,12 @@ import java.net.InetAddress;
import java.net.ServerSocket;
import java.nio.file.Files;
import java.security.KeyStore;
+import java.security.KeyStoreException;
+import java.security.NoSuchAlgorithmException;
+import java.security.UnrecoverableKeyException;
+import java.security.cert.CertificateException;
+import java.util.Arrays;
+import java.util.List;
import static org.apache.flink.util.Preconditions.checkNotNull;
@@ -82,7 +93,7 @@ public class SSLUtils {
* SSL Server Sockets are always part of internal communication.
*/
public static ServerSocketFactory createSSLServerSocketFactory(Configuration config) throws Exception {
- SSLContext sslContext = createInternalSSLContext(config);
+ SSLContext sslContext = createInternalSSLContext(config, false);
if (sslContext == null) {
throw new IllegalConfigurationException("SSL is not enabled");
}
@@ -99,7 +110,7 @@ public class SSLUtils {
* SSL Client Sockets are always part of internal communication.
*/
public static SocketFactory createSSLClientSocketFactory(Configuration config) throws Exception {
- SSLContext sslContext = createInternalSSLContext(config);
+ SSLContext sslContext = createInternalSSLContext(config, true);
if (sslContext == null) {
throw new IllegalConfigurationException("SSL is not enabled");
}
@@ -111,17 +122,13 @@ public class SSLUtils {
* Creates a SSLEngineFactory to be used by internal communication server endpoints.
*/
public static SSLHandlerFactory createInternalServerSSLEngineFactory(final Configuration config) throws Exception {
- SSLContext sslContext = createInternalSSLContext(config);
+ SslContext sslContext = createInternalNettySSLContext(config, false);
if (sslContext == null) {
throw new IllegalConfigurationException("SSL is not enabled for internal communication.");
}
return new SSLHandlerFactory(
sslContext,
- getEnabledProtocols(config),
- getEnabledCipherSuites(config),
- false,
- true,
config.getInteger(SecurityOptions.SSL_INTERNAL_HANDSHAKE_TIMEOUT),
config.getInteger(SecurityOptions.SSL_INTERNAL_CLOSE_NOTIFY_FLUSH_TIMEOUT));
}
@@ -130,17 +137,13 @@ public class SSLUtils {
* Creates a SSLEngineFactory to be used by internal communication client endpoints.
*/
public static SSLHandlerFactory createInternalClientSSLEngineFactory(final Configuration config) throws Exception {
- SSLContext sslContext = createInternalSSLContext(config);
+ SslContext sslContext = createInternalNettySSLContext(config, true);
if (sslContext == null) {
throw new IllegalConfigurationException("SSL is not enabled for internal communication.");
}
return new SSLHandlerFactory(
sslContext,
- getEnabledProtocols(config),
- getEnabledCipherSuites(config),
- true,
- true,
config.getInteger(SecurityOptions.SSL_INTERNAL_HANDSHAKE_TIMEOUT),
config.getInteger(SecurityOptions.SSL_INTERNAL_CLOSE_NOTIFY_FLUSH_TIMEOUT));
}
@@ -151,17 +154,14 @@ public class SSLUtils {
* @param config The application configuration.
*/
public static SSLHandlerFactory createRestServerSSLEngineFactory(final Configuration config) throws Exception {
- SSLContext sslContext = createRestServerSSLContext(config);
+ ClientAuth clientAuth = isRestSSLAuthenticationEnabled(config) ? ClientAuth.REQUIRE : ClientAuth.NONE;
+ SslContext sslContext = createRestNettySSLContext(config, false, clientAuth);
if (sslContext == null) {
throw new IllegalConfigurationException("SSL is not enabled for REST endpoints.");
}
return new SSLHandlerFactory(
sslContext,
- getEnabledProtocols(config),
- getEnabledCipherSuites(config),
- false,
- isRestSSLAuthenticationEnabled(config),
-1,
-1);
}
@@ -172,17 +172,14 @@ public class SSLUtils {
* @param config The application configuration.
*/
public static SSLHandlerFactory createRestClientSSLEngineFactory(final Configuration config) throws Exception {
- SSLContext sslContext = createRestClientSSLContext(config);
+ ClientAuth clientAuth = isRestSSLAuthenticationEnabled(config) ? ClientAuth.REQUIRE : ClientAuth.NONE;
+ SslContext sslContext = createRestNettySSLContext(config, true, clientAuth);
if (sslContext == null) {
throw new IllegalConfigurationException("SSL is not enabled for REST endpoints.");
}
return new SSLHandlerFactory(
sslContext,
- getEnabledProtocols(config),
- getEnabledCipherSuites(config),
- true,
- isRestSSLAuthenticationEnabled(config),
-1,
-1);
}
@@ -197,159 +194,186 @@ public class SSLUtils {
return config.getString(SecurityOptions.SSL_ALGORITHMS).split(",");
}
- /**
- * Creates the SSL Context for internal SSL, if internal SSL is configured.
- * For internal SSL, the client and server side configuration are identical, because
- * of mutual authentication.
- */
- @Nullable
- private static SSLContext createInternalSSLContext(Configuration config) throws Exception {
- checkNotNull(config, "config");
+ private static TrustManagerFactory getTrustManagerFactory(Configuration config, boolean internal)
+ throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException {
+ String trustStoreFilePath = getAndCheckOption(
+ config,
+ internal ? SecurityOptions.SSL_INTERNAL_TRUSTSTORE : SecurityOptions.SSL_REST_TRUSTSTORE,
+ SecurityOptions.SSL_TRUSTSTORE);
- if (!isInternalSSLEnabled(config)) {
- return null;
+ String trustStorePassword = getAndCheckOption(
+ config,
+ internal ? SecurityOptions.SSL_INTERNAL_TRUSTSTORE_PASSWORD : SecurityOptions.SSL_REST_TRUSTSTORE_PASSWORD,
+ SecurityOptions.SSL_TRUSTSTORE_PASSWORD);
+
+ KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
+ try (InputStream trustStoreFile = Files
+ .newInputStream(new File(trustStoreFilePath).toPath())) {
+ trustStore.load(trustStoreFile, trustStorePassword.toCharArray());
}
- String keystoreFilePath = getAndCheckOption(
- config, SecurityOptions.SSL_INTERNAL_KEYSTORE, SecurityOptions.SSL_KEYSTORE);
- String keystorePassword = getAndCheckOption(
- config, SecurityOptions.SSL_INTERNAL_KEYSTORE_PASSWORD, SecurityOptions.SSL_KEYSTORE_PASSWORD);
+ TrustManagerFactory tmf =
+ TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
+ tmf.init(trustStore);
- String certPassword = getAndCheckOption(
- config, SecurityOptions.SSL_INTERNAL_KEY_PASSWORD, SecurityOptions.SSL_KEY_PASSWORD);
+ return tmf;
+ }
- String trustStoreFilePath = getAndCheckOption(
- config, SecurityOptions.SSL_INTERNAL_TRUSTSTORE, SecurityOptions.SSL_TRUSTSTORE);
+ private static KeyManagerFactory getKeyManagerFactory(Configuration config, boolean internal)
+ throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException,
+ UnrecoverableKeyException {
+ String keystoreFilePath = getAndCheckOption(
+ config,
+ internal ? SecurityOptions.SSL_INTERNAL_KEYSTORE : SecurityOptions.SSL_REST_KEYSTORE,
+ SecurityOptions.SSL_KEYSTORE);
- String trustStorePassword = getAndCheckOption(
- config, SecurityOptions.SSL_INTERNAL_TRUSTSTORE_PASSWORD, SecurityOptions.SSL_TRUSTSTORE_PASSWORD);
+ String keystorePassword = getAndCheckOption(
+ config,
+ internal ? SecurityOptions.SSL_INTERNAL_KEYSTORE_PASSWORD : SecurityOptions.SSL_REST_KEYSTORE_PASSWORD,
+ SecurityOptions.SSL_KEYSTORE_PASSWORD);
- String sslProtocolVersion = config.getString(SecurityOptions.SSL_PROTOCOL);
- int sessionCacheSize = config.getInteger(SecurityOptions.SSL_INTERNAL_SESSION_CACHE_SIZE);
- int sessionTimeoutMs = config.getInteger(SecurityOptions.SSL_INTERNAL_SESSION_TIMEOUT);
+ String certPassword = getAndCheckOption(
+ config,
+ internal ? SecurityOptions.SSL_INTERNAL_KEY_PASSWORD : SecurityOptions.SSL_REST_KEY_PASSWORD,
+ SecurityOptions.SSL_KEY_PASSWORD);
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
try (InputStream keyStoreFile = Files.newInputStream(new File(keystoreFilePath).toPath())) {
keyStore.load(keyStoreFile, keystorePassword.toCharArray());
}
- KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
- try (InputStream trustStoreFile = Files.newInputStream(new File(trustStoreFilePath).toPath())) {
- trustStore.load(trustStoreFile, trustStorePassword.toCharArray());
- }
-
- KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
+ KeyManagerFactory kmf =
+ KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(keyStore, certPassword.toCharArray());
- TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
- tmf.init(trustStore);
+ return kmf;
+ }
- SSLContext sslContext = SSLContext.getInstance(sslProtocolVersion);
- sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
- if (sessionCacheSize >= 0) {
- sslContext.getClientSessionContext().setSessionCacheSize(sessionCacheSize);
- }
- if (sessionTimeoutMs >= 0) {
- sslContext.getClientSessionContext().setSessionTimeout(sessionTimeoutMs / 1000);
+ /**
+ * Creates the SSL Context for internal SSL, if internal SSL is configured.
+ * For internal SSL, the client and server side configuration are identical, because
+ * of mutual authentication.
+ */
+ @Nullable
+ private static SSLContext createInternalSSLContext(Configuration config, boolean clientMode) throws Exception {
+ JdkSslContext nettySSLContext =
+ (JdkSslContext) createInternalNettySSLContext(config, clientMode, SslProvider.JDK);
+ if (nettySSLContext != null) {
+ return nettySSLContext.context();
+ } else {
+ return null;
}
-
- return sslContext;
}
- private enum RestSSLContextConfigMode {
- CLIENT,
- SERVER,
- MUTUAL
+ @Nullable
+ private static SslContext createInternalNettySSLContext(Configuration config, boolean clientMode)
+ throws Exception {
+ return createInternalNettySSLContext(config, clientMode, SslProvider.JDK);
}
/**
- * Creates an SSL context for the external REST SSL.
- * If mutual authentication is configured the client and the server side configuration are identical.
+ * Creates the SSL Context for internal SSL, if internal SSL is configured.
+ * For internal SSL, the client and server side configuration are identical, because
+ * of mutual authentication.
*/
@Nullable
- private static SSLContext createRestSSLContext(Configuration config, RestSSLContextConfigMode configMode) throws Exception {
+ private static SslContext createInternalNettySSLContext(
+ Configuration config, boolean clientMode, SslProvider provider)
+ throws Exception {
checkNotNull(config, "config");
- if (!isRestSSLEnabled(config)) {
+ if (!isInternalSSLEnabled(config)) {
return null;
}
- KeyManager[] keyManagers = null;
- if (configMode == RestSSLContextConfigMode.SERVER || configMode == RestSSLContextConfigMode.MUTUAL) {
- String keystoreFilePath = getAndCheckOption(
- config, SecurityOptions.SSL_REST_KEYSTORE, SecurityOptions.SSL_KEYSTORE);
-
- String keystorePassword = getAndCheckOption(
- config, SecurityOptions.SSL_REST_KEYSTORE_PASSWORD, SecurityOptions.SSL_KEYSTORE_PASSWORD);
-
- String certPassword = getAndCheckOption(
- config, SecurityOptions.SSL_REST_KEY_PASSWORD, SecurityOptions.SSL_KEY_PASSWORD);
-
- KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
- try (InputStream keyStoreFile = Files.newInputStream(new File(keystoreFilePath).toPath())) {
- keyStore.load(keyStoreFile, keystorePassword.toCharArray());
- }
-
- KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
- kmf.init(keyStore, certPassword.toCharArray());
-
- keyManagers = kmf.getKeyManagers();
- }
-
- TrustManager[] trustManagers = null;
- if (configMode == RestSSLContextConfigMode.CLIENT || configMode == RestSSLContextConfigMode.MUTUAL) {
- String trustStoreFilePath = getAndCheckOption(
- config, SecurityOptions.SSL_REST_TRUSTSTORE, SecurityOptions.SSL_TRUSTSTORE);
-
- String trustStorePassword = getAndCheckOption(
- config, SecurityOptions.SSL_REST_TRUSTSTORE_PASSWORD, SecurityOptions.SSL_TRUSTSTORE_PASSWORD);
-
- KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
- try (InputStream trustStoreFile = Files.newInputStream(new File(trustStoreFilePath).toPath())) {
- trustStore.load(trustStoreFile, trustStorePassword.toCharArray());
- }
+ String[] sslProtocols = getEnabledProtocols(config);
+ List<String> ciphers = Arrays.asList(getEnabledCipherSuites(config));
+ int sessionCacheSize = config.getInteger(SecurityOptions.SSL_INTERNAL_SESSION_CACHE_SIZE);
+ int sessionTimeoutMs = config.getInteger(SecurityOptions.SSL_INTERNAL_SESSION_TIMEOUT);
- TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
- tmf.init(trustStore);
+ KeyManagerFactory kmf = getKeyManagerFactory(config, true);
+ TrustManagerFactory tmf = getTrustManagerFactory(config, true);
+ ClientAuth clientAuth = ClientAuth.REQUIRE;
- trustManagers = tmf.getTrustManagers();
+ final SslContextBuilder sslContextBuilder;
+ if (clientMode) {
+ sslContextBuilder = SslContextBuilder.forClient().keyManager(kmf);
+ } else {
+ sslContextBuilder = SslContextBuilder.forServer(kmf);
}
- String sslProtocolVersion = config.getString(SecurityOptions.SSL_PROTOCOL);
- SSLContext sslContext = SSLContext.getInstance(sslProtocolVersion);
- sslContext.init(keyManagers, trustManagers, null);
-
- return sslContext;
+ return sslContextBuilder
+ .sslProvider(provider)
+ .protocols(sslProtocols)
+ .ciphers(ciphers)
+ .trustManager(tmf)
+ .clientAuth(clientAuth)
+ .sessionCacheSize(sessionCacheSize)
+ .sessionTimeout(sessionTimeoutMs / 1000)
+ .build();
}
/**
- * Creates an SSL context for the external REST endpoint server.
+ * Creates an SSL context for clients against the external REST endpoint.
*/
@Nullable
- public static SSLContext createRestServerSSLContext(Configuration config) throws Exception {
- final RestSSLContextConfigMode configMode;
- if (isRestSSLAuthenticationEnabled(config)) {
- configMode = RestSSLContextConfigMode.MUTUAL;
+ @VisibleForTesting
+ public static SSLContext createRestSSLContext(Configuration config, boolean clientMode) throws Exception {
+ ClientAuth clientAuth = isRestSSLAuthenticationEnabled(config) ? ClientAuth.REQUIRE : ClientAuth.NONE;
+ JdkSslContext nettySSLContext =
+ (JdkSslContext) createRestNettySSLContext(config, clientMode, clientAuth, SslProvider.JDK);
+ if (nettySSLContext != null) {
+ return nettySSLContext.context();
} else {
- configMode = RestSSLContextConfigMode.SERVER;
+ return null;
}
+ }
- return createRestSSLContext(config, configMode);
+ @Nullable
+ private static SslContext createRestNettySSLContext(
+ Configuration config, boolean clientMode, ClientAuth clientAuth)
+ throws Exception {
+ return createRestNettySSLContext(config, clientMode, clientAuth, SslProvider.JDK);
}
/**
- * Creates an SSL context for clients against the external REST endpoint.
+ * Creates an SSL context for the external REST SSL.
+ * If mutual authentication is configured the client and the server side configuration are identical.
*/
@Nullable
- public static SSLContext createRestClientSSLContext(Configuration config) throws Exception {
- final RestSSLContextConfigMode configMode;
- if (isRestSSLAuthenticationEnabled(config)) {
- configMode = RestSSLContextConfigMode.MUTUAL;
+ public static SslContext createRestNettySSLContext(
+ Configuration config, boolean clientMode, ClientAuth clientAuth, SslProvider provider)
+ throws Exception {
+ checkNotNull(config, "config");
+
+ if (!isRestSSLEnabled(config)) {
+ return null;
+ }
+
+ String[] sslProtocols = getEnabledProtocols(config);
+
+ final SslContextBuilder sslContextBuilder;
+ if (clientMode) {
+ sslContextBuilder = SslContextBuilder.forClient();
+ if (clientAuth != ClientAuth.NONE) {
+ KeyManagerFactory kmf = getKeyManagerFactory(config, false);
+ sslContextBuilder.keyManager(kmf);
+ }
} else {
- configMode = RestSSLContextConfigMode.CLIENT;
+ KeyManagerFactory kmf = getKeyManagerFactory(config, false);
+ sslContextBuilder = SslContextBuilder.forServer(kmf);
+ }
+
+ if (clientMode || clientAuth != ClientAuth.NONE) {
+ TrustManagerFactory tmf = getTrustManagerFactory(config, false);
+ sslContextBuilder.trustManager(tmf);
}
- return createRestSSLContext(config, configMode);
+ return sslContextBuilder
+ .sslProvider(provider)
+ .protocols(sslProtocols)
+ .clientAuth(clientAuth)
+ .build();
}
// ------------------------------------------------------------------------
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/rest/RestClient.java b/flink-runtime/src/main/java/org/apache/flink/runtime/rest/RestClient.java
index c478cf5..524197e 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/rest/RestClient.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/rest/RestClient.java
@@ -124,7 +124,7 @@ public class RestClient implements AutoCloseableAsync {
try {
// SSL should be the first handler in the pipeline
if (sslHandlerFactory != null) {
- socketChannel.pipeline().addLast("ssl", sslHandlerFactory.createNettySSLHandler());
+ socketChannel.pipeline().addLast("ssl", sslHandlerFactory.createNettySSLHandler(socketChannel.alloc()));
}
socketChannel.pipeline()
diff --git a/flink-runtime/src/test/java/org/apache/flink/runtime/net/SSLUtilsTest.java b/flink-runtime/src/test/java/org/apache/flink/runtime/net/SSLUtilsTest.java
index 5031acc..17d9ec8 100644
--- a/flink-runtime/src/test/java/org/apache/flink/runtime/net/SSLUtilsTest.java
+++ b/flink-runtime/src/test/java/org/apache/flink/runtime/net/SSLUtilsTest.java
@@ -24,6 +24,7 @@ import org.apache.flink.configuration.SecurityOptions;
import org.apache.flink.runtime.io.network.netty.SSLHandlerFactory;
import org.apache.flink.util.TestLogger;
+import org.apache.flink.shaded.netty4.io.netty.buffer.UnpooledByteBufAllocator;
import org.apache.flink.shaded.netty4.io.netty.handler.ssl.SslHandler;
import org.junit.Test;
@@ -386,7 +387,7 @@ public class SSLUtilsTest extends TestLogger {
serverConfig.setString(SecurityOptions.SSL_ALGORITHMS, "TLS_DHE_RSA_WITH_AES_128_CBC_SHA,TLS_DHE_RSA_WITH_AES_128_CBC_SHA256");
final SSLHandlerFactory serverSSLHandlerFactory = SSLUtils.createInternalServerSSLEngineFactory(serverConfig);
- final SslHandler sslHandler = serverSSLHandlerFactory.createNettySSLHandler();
+ final SslHandler sslHandler = serverSSLHandlerFactory.createNettySSLHandler(UnpooledByteBufAllocator.DEFAULT);
assertEquals(1, sslHandler.engine().getEnabledProtocols().length);
assertEquals("TLSv1", sslHandler.engine().getEnabledProtocols()[0]);
diff --git a/flink-runtime/src/test/java/org/apache/flink/runtime/rest/RestServerEndpointITCase.java b/flink-runtime/src/test/java/org/apache/flink/runtime/rest/RestServerEndpointITCase.java
index 51d1d2e..a99ec44 100644
--- a/flink-runtime/src/test/java/org/apache/flink/runtime/rest/RestServerEndpointITCase.java
+++ b/flink-runtime/src/test/java/org/apache/flink/runtime/rest/RestServerEndpointITCase.java
@@ -184,7 +184,7 @@ public class RestServerEndpointITCase extends TestLogger {
defaultSSLContext = SSLContext.getDefault();
defaultSSLSocketFactory = HttpsURLConnection.getDefaultSSLSocketFactory();
- final SSLContext sslClientContext = SSLUtils.createRestClientSSLContext(config);
+ final SSLContext sslClientContext = SSLUtils.createRestSSLContext(config, true);
if (sslClientContext != null) {
SSLContext.setDefault(sslClientContext);
HttpsURLConnection.setDefaultSSLSocketFactory(sslClientContext.getSocketFactory());