This is an automated email from the ASF dual-hosted git repository.
anton-vinogradov pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ignite.git
The following commit(s) were added to refs/heads/master by this push:
new 5d4ff8f299e IGNITE-29256 Use the factory's current SSL context for new
TCP connections (#13795)
5d4ff8f299e is described below
commit 5d4ff8f299ed5266675bf4f4b0cc64c3d7aa74ef
Author: Anton Vinogradov <[email protected]>
AuthorDate: Mon Nov 9 15:29:05 2026 +0300
IGNITE-29256 Use the factory's current SSL context for new TCP connections
(#13795)
---
.../processors/odbc/ClientListenerProcessor.java | 4 +-
.../rest/protocols/tcp/GridTcpRestProtocol.java | 84 ++---
.../apache/ignite/internal/util/IgniteUtils.java | 10 +-
.../tcp/internal/GridNioServerWrapper.java | 2 +-
.../ignite/spi/discovery/tcp/ServerImpl.java | 34 +-
.../ignite/spi/discovery/tcp/TcpDiscoverySpi.java | 41 ++-
.../internal/client/thin/ClientSslUtilsTest.java | 44 +++
.../internal/util/nio/GridNioSslSelfTest.java | 111 ++++++-
.../tcp/TcpDiscoverySslConnectionFailureTest.java | 171 +++++++++++
.../apache/ignite/ssl/SslContextReloadTest.java | 341 +++++++++++++++++++++
.../IgniteSpiDiscoverySelfTestSuite.java | 2 +
.../ignite/testsuites/SecurityTestSuite.java | 4 +
.../internal/util/nio/ssl/GridNioSslFilter.java | 23 +-
.../internal/client/thin/ClientSslUtils.java | 16 +-
.../GridNioClientConnectionMultiplexer.java | 13 +-
15 files changed, 798 insertions(+), 102 deletions(-)
diff --git
a/modules/core/src/main/java/org/apache/ignite/internal/processors/odbc/ClientListenerProcessor.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/odbc/ClientListenerProcessor.java
index 1186bc3427c..2ec17e552c1 100644
---
a/modules/core/src/main/java/org/apache/ignite/internal/processors/odbc/ClientListenerProcessor.java
+++
b/modules/core/src/main/java/org/apache/ignite/internal/processors/odbc/ClientListenerProcessor.java
@@ -537,8 +537,8 @@ public class ClientListenerProcessor extends
GridProcessorAdapter {
throw new IgniteCheckedException("Failed to create client
listener " +
"(SSL is enabled but factory is null). Check the
ClientConnectorConfiguration");
- GridNioSslFilter sslFilter = U.sslFilter(sslCtxFactory.create(),
- true, ByteOrder.nativeOrder(), log,
ctx.metric().registry(CLIENT_CONNECTOR_METRICS));
+ GridNioSslFilter sslFilter = U.sslFilter(sslCtxFactory, true,
ByteOrder.nativeOrder(), log,
+ ctx.metric().registry(CLIENT_CONNECTOR_METRICS));
sslFilter.directMode(true);
diff --git
a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/GridTcpRestProtocol.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/GridTcpRestProtocol.java
index b0d3502b250..bd36d384b82 100644
---
a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/GridTcpRestProtocol.java
+++
b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/GridTcpRestProtocol.java
@@ -41,14 +41,12 @@ import
org.apache.ignite.internal.processors.rest.client.message.GridClientMessa
import
org.apache.ignite.internal.processors.rest.protocols.GridRestProtocolAdapter;
import org.apache.ignite.internal.util.nio.GridNioCodecFilter;
import org.apache.ignite.internal.util.nio.GridNioFilter;
-import org.apache.ignite.internal.util.nio.GridNioParser;
import org.apache.ignite.internal.util.nio.GridNioServer;
import org.apache.ignite.internal.util.nio.GridNioServerListener;
import org.apache.ignite.internal.util.nio.ssl.GridNioSslFilter;
import org.apache.ignite.internal.util.typedef.internal.U;
import org.apache.ignite.plugin.PluginProvider;
import org.apache.ignite.spi.IgnitePortProtocol;
-import org.jetbrains.annotations.Nullable;
import static
org.apache.ignite.internal.processors.metric.impl.MetricUtils.metricName;
@@ -85,33 +83,17 @@ public class GridTcpRestProtocol extends
GridRestProtocolAdapter {
lsnr = new GridTcpRestNioListener(log, this, hnd, ctx);
- GridNioParser parser = new GridTcpRestParser(false);
-
try {
host = resolveRestTcpHost(ctx.config());
- SSLContext sslCtx = null;
-
- if (cfg.isSslEnabled()) {
- Factory<SSLContext> igniteFactory =
ctx.config().getSslContextFactory();
-
- Factory<SSLContext> factory = cfg.getSslFactory();
-
- if (factory == null && igniteFactory == null)
- // Thrown SSL exception instead of IgniteCheckedException
for writing correct warning message into log.
- throw new SSLException("SSL is enabled, but SSL context
factory is not specified.");
+ GridNioFilter[] filters = makeFilters(cfg);
- if (factory != null)
- sslCtx = factory.create();
- else
- sslCtx = igniteFactory.create();
- }
int startPort = cfg.getPort();
int portRange = cfg.getPortRange();
int lastPort = portRange == 0 ? startPort : startPort + portRange
- 1;
for (int port0 = startPort; port0 <= lastPort; port0++) {
- if (startTcpServer(host, port0, lsnr, parser, sslCtx, cfg)) {
+ if (startTcpServer(host, port0, lsnr, filters, cfg)) {
port = port0;
if (log.isInfoEnabled())
@@ -179,51 +161,51 @@ public class GridTcpRestProtocol extends
GridRestProtocolAdapter {
return U.resolveLocalHost(host);
}
+ /** */
+ private GridNioFilter[] makeFilters(ConnectorConfiguration cfg) throws
SSLException {
+ GridNioFilter codec = new GridNioCodecFilter(new
GridTcpRestParser(false), log, false);
+
+ if (!cfg.isSslEnabled())
+ return new GridNioFilter[] { codec };
+
+ Factory<SSLContext> sslCtxFactory = cfg.getSslFactory();
+
+ if (sslCtxFactory == null)
+ sslCtxFactory = ctx.config().getSslContextFactory();
+
+ if (sslCtxFactory == null)
+ // Thrown SSL exception instead of IgniteCheckedException for
writing correct warning message into log.
+ throw new SSLException("SSL is enabled, but SSL context factory is
not specified.");
+
+ GridNioSslFilter sslFilter = U.sslFilter(sslCtxFactory,
cfg.isDirectBuffer(), ByteOrder.nativeOrder(), log,
+ ctx.metric().registry(REST_CONNECTOR_METRIC_REGISTRY_NAME));
+
+ sslFilter.directMode(false);
+
+ boolean auth = cfg.isSslClientAuth();
+
+ sslFilter.wantClientAuth(auth);
+ sslFilter.needClientAuth(auth);
+
+ return new GridNioFilter[] { codec, sslFilter };
+ }
+
/**
* Tries to start server with given parameters.
*
* @param hostAddr Host on which server should be bound.
* @param port Port on which server should be bound.
* @param lsnr Server message listener.
- * @param parser Server message parser.
- * @param sslCtx SSL context in case if SSL is enabled.
+ * @param filters Protocol filters.
* @param cfg Configuration for other parameters.
* @return {@code True} if server successfully started, {@code false} if
port is used and
* server was unable to start.
*/
private boolean startTcpServer(InetAddress hostAddr, int port,
GridNioServerListener<GridClientMessage> lsnr,
- GridNioParser parser, @Nullable SSLContext sslCtx,
ConnectorConfiguration cfg) {
+ GridNioFilter[] filters, ConnectorConfiguration cfg) {
try {
- GridNioFilter codec = new GridNioCodecFilter(parser, log, false);
-
- GridNioFilter[] filters;
-
MetricRegistryImpl mreg =
ctx.metric().registry(REST_CONNECTOR_METRIC_REGISTRY_NAME);
- if (sslCtx != null) {
- GridNioSslFilter sslFilter = U.sslFilter(
- sslCtx,
- cfg.isDirectBuffer(),
- ByteOrder.nativeOrder(),
- log,
- mreg);
-
- sslFilter.directMode(false);
-
- boolean auth = cfg.isSslClientAuth();
-
- sslFilter.wantClientAuth(auth);
-
- sslFilter.needClientAuth(auth);
-
- filters = new GridNioFilter[] {
- codec,
- sslFilter
- };
- }
- else
- filters = new GridNioFilter[] { codec };
-
GridNioServer.Builder<GridClientMessage> builder =
GridNioServer.<GridClientMessage>builder()
.address(hostAddr)
.port(port)
diff --git
a/modules/core/src/main/java/org/apache/ignite/internal/util/IgniteUtils.java
b/modules/core/src/main/java/org/apache/ignite/internal/util/IgniteUtils.java
index cbcd98495d0..a60d3d75131 100755
---
a/modules/core/src/main/java/org/apache/ignite/internal/util/IgniteUtils.java
+++
b/modules/core/src/main/java/org/apache/ignite/internal/util/IgniteUtils.java
@@ -125,6 +125,7 @@ import java.util.zip.Deflater;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
+import javax.cache.configuration.Factory;
import javax.management.DynamicMBean;
import javax.management.JMException;
import javax.management.MBeanRegistrationException;
@@ -7730,7 +7731,7 @@ public abstract class IgniteUtils extends CommonUtils {
/**
* Creates an SSL NIO filter, wiring its metrics from the given registry.
*
- * @param sslCtx SSL context.
+ * @param sslCtxFactory Configured SSL context factory.
* @param directBuf Direct buffer flag.
* @param order Byte order.
* @param log Logger to use.
@@ -7738,12 +7739,15 @@ public abstract class IgniteUtils extends CommonUtils {
* @return SSL NIO filter.
*/
public static GridNioSslFilter sslFilter(
- SSLContext sslCtx,
+ Factory<SSLContext> sslCtxFactory,
boolean directBuf,
ByteOrder order,
IgniteLogger log,
@Nullable MetricRegistryImpl mreg
) {
+ // Initialize caching factories before NIO threads start, and fail
fast if the context cannot be created.
+ sslCtxFactory.create();
+
LongConsumer handshakeDuration = mreg == null ? null : mreg.histogram(
GridNioSslFilter.SSL_HANDSHAKE_DURATION_HISTOGRAM_METRIC_NAME,
new long[] {250, 500, 1000},
@@ -7753,6 +7757,6 @@ public abstract class IgniteUtils extends CommonUtils {
GridNioSslFilter.SSL_REJECTED_SESSIONS_CNT_METRIC_NAME,
"TCP sessions count that were rejected due to SSL
errors.")::increment;
- return new GridNioSslFilter(sslCtx, directBuf, order, log,
handshakeDuration, rejectedSesCnt);
+ return new GridNioSslFilter(sslCtxFactory, directBuf, order, log,
handshakeDuration, rejectedSesCnt);
}
}
diff --git
a/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/internal/GridNioServerWrapper.java
b/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/internal/GridNioServerWrapper.java
index 9368f9f8a83..00d36124b18 100644
---
a/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/internal/GridNioServerWrapper.java
+++
b/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/internal/GridNioServerWrapper.java
@@ -919,7 +919,7 @@ public class GridNioServerWrapper {
if (stateProvider.isSslEnabled()) {
GridNioSslFilter sslFilter = U.sslFilter(
- igniteCfg.getSslContextFactory().create(),
+ igniteCfg.getSslContextFactory(),
true,
ByteOrder.LITTLE_ENDIAN,
log,
diff --git
a/modules/core/src/main/java/org/apache/ignite/spi/discovery/tcp/ServerImpl.java
b/modules/core/src/main/java/org/apache/ignite/spi/discovery/tcp/ServerImpl.java
index 4c5e3c72a42..a2f00010a37 100644
---
a/modules/core/src/main/java/org/apache/ignite/spi/discovery/tcp/ServerImpl.java
+++
b/modules/core/src/main/java/org/apache/ignite/spi/discovery/tcp/ServerImpl.java
@@ -63,7 +63,6 @@ import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.net.ssl.SSLException;
-import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLSocket;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCheckedException;
@@ -6298,16 +6297,7 @@ class ServerImpl extends TcpDiscoveryImpl {
for (port = spi.locPort; port <= lastPort; port++) {
try {
- if (spi.isSslEnabled()) {
- SSLServerSocket sslSock =
(SSLServerSocket)spi.sslSrvSockFactory
- .createServerSocket(port, 0, spi.locHost);
-
- sslSock.setNeedClientAuth(true);
-
- srvrSock = sslSock;
- }
- else
- srvrSock = new ServerSocket(port, 0, spi.locHost);
+ srvrSock = new ServerSocket(port, 0, spi.locHost);
if (log.isInfoEnabled()) {
log.info("Successfully bound to TCP port [port=" +
port +
@@ -6353,6 +6343,28 @@ class ServerImpl extends TcpDiscoveryImpl {
blockingSectionEnd();
}
+ if (spi.isSslEnabled()) {
+ try {
+ SSLSocket sslSock =
(SSLSocket)spi.sslSocketFactory()
+ .createSocket(sock,
sock.getInetAddress().getHostAddress(), sock.getPort(), true);
+
+ sock = sslSock;
+
+ sslSock.setUseClientMode(false);
+ sslSock.setNeedClientAuth(true);
+ }
+ catch (IOException | RuntimeException e) {
+ LT.warn(log, "Failed to set up TLS on an accepted
discovery socket [rmtAddr=" +
+ sock.getRemoteSocketAddress() + ']', e);
+
+ U.closeQuiet(sock);
+
+ onIdle();
+
+ continue;
+ }
+ }
+
if (log.isInfoEnabled()) {
log.info("TCP discovery accepted incoming connection "
+
"[rmtAddr=" + sock.getInetAddress() + ", rmtPort="
+ sock.getPort() + ']');
diff --git
a/modules/core/src/main/java/org/apache/ignite/spi/discovery/tcp/TcpDiscoverySpi.java
b/modules/core/src/main/java/org/apache/ignite/spi/discovery/tcp/TcpDiscoverySpi.java
index 1ec99c1b038..de6d0ed6692 100644
---
a/modules/core/src/main/java/org/apache/ignite/spi/discovery/tcp/TcpDiscoverySpi.java
+++
b/modules/core/src/main/java/org/apache/ignite/spi/discovery/tcp/TcpDiscoverySpi.java
@@ -38,9 +38,9 @@ import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Pattern;
+import javax.cache.configuration.Factory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLException;
-import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.SSLSocketFactory;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteAuthenticationException;
@@ -416,11 +416,9 @@ public class TcpDiscoverySpi extends IgniteSpiAdapter
implements IgniteDiscovery
/** Node authenticator. */
protected DiscoverySpiNodeAuthenticator nodeAuth;
- /** SSL server socket factory. */
- protected SSLServerSocketFactory sslSrvSockFactory;
-
- /** SSL socket factory. */
- protected SSLSocketFactory sslSockFactory;
+ /** */
+ @GridToStringExclude
+ private Factory<SSLContext> sslCtxFactory;
/** SSL enable/disable flag. */
protected boolean sslEnable;
@@ -1660,7 +1658,7 @@ public class TcpDiscoverySpi extends IgniteSpiAdapter
implements IgniteDiscovery
try {
if (isSslEnabled())
- sock = sslSockFactory.createSocket();
+ sock = sslSocketFactory().createSocket();
else
sock = new Socket();
@@ -1670,14 +1668,22 @@ public class TcpDiscoverySpi extends IgniteSpiAdapter
implements IgniteDiscovery
return sock;
}
- catch (IOException e) {
+ catch (IOException | RuntimeException e) {
if (sock != null)
U.closeQuiet(sock);
- throw e;
+ if (e instanceof IOException)
+ throw (IOException)e;
+
+ throw new IOException("Failed to create discovery socket.", e);
}
}
+ /** */
+ SSLSocketFactory sslSocketFactory() {
+ return sslCtxFactory.create().getSocketFactory();
+ }
+
/**
* Writes raw data to the session socket.
*
@@ -2106,7 +2112,9 @@ public class TcpDiscoverySpi extends IgniteSpiAdapter
implements IgniteDiscovery
if (impl != null)
return;
- sslEnable = ignite.configuration().getSslContextFactory() != null;
+ Factory<SSLContext> sslFactory =
ignite.configuration().getSslContextFactory();
+
+ sslEnable = sslFactory != null;
initFailureDetectionTimeout();
@@ -2154,15 +2162,14 @@ public class TcpDiscoverySpi extends IgniteSpiAdapter
implements IgniteDiscovery
if (isSslEnabled()) {
try {
- SSLContext sslCtx =
ignite.configuration().getSslContextFactory().create();
-
- sslSockFactory = sslCtx.getSocketFactory();
- sslSrvSockFactory = sslCtx.getServerSocketFactory();
+ // Validate protocols and ciphers at startup, before binding
the plain listener.
+ sslFactory.create().getSocketFactory().createSocket().close();
}
- catch (IgniteException e) {
- throw new IgniteSpiException("Failed to create SSL context.
SSL factory: "
- + ignite.configuration().getSslContextFactory(), e);
+ catch (IOException | IgniteException e) {
+ throw new IgniteSpiException("Failed to initialize discovery
SSL. SSL factory: " + sslFactory, e);
}
+
+ sslCtxFactory = sslFactory;
}
try {
diff --git
a/modules/core/src/test/java/org/apache/ignite/internal/client/thin/ClientSslUtilsTest.java
b/modules/core/src/test/java/org/apache/ignite/internal/client/thin/ClientSslUtilsTest.java
new file mode 100644
index 00000000000..7896f40818b
--- /dev/null
+++
b/modules/core/src/test/java/org/apache/ignite/internal/client/thin/ClientSslUtilsTest.java
@@ -0,0 +1,44 @@
+/*
+ * 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.ignite.internal.client.thin;
+
+import org.apache.ignite.client.SslMode;
+import org.apache.ignite.configuration.ClientConfiguration;
+import org.junit.Test;
+
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertThrows;
+
+/** */
+public class ClientSslUtilsTest {
+ /** */
+ @Test
+ public void testContextFactoryFailureIsReportedBeforeConnecting() {
+ IllegalStateException failure = new IllegalStateException("Cannot load
client TLS credentials");
+
+ ClientConfiguration cfg = new ClientConfiguration()
+ .setSslMode(SslMode.REQUIRED)
+ .setSslContextFactory(() -> {
+ throw failure;
+ });
+
+ ClientError err = assertThrows(ClientError.class, () ->
ClientSslUtils.getSslContextFactory(cfg));
+
+ assertSame(failure, err.getCause());
+ }
+}
diff --git
a/modules/core/src/test/java/org/apache/ignite/internal/util/nio/GridNioSslSelfTest.java
b/modules/core/src/test/java/org/apache/ignite/internal/util/nio/GridNioSslSelfTest.java
index 9b2199f213e..73f28c97b2f 100644
---
a/modules/core/src/test/java/org/apache/ignite/internal/util/nio/GridNioSslSelfTest.java
+++
b/modules/core/src/test/java/org/apache/ignite/internal/util/nio/GridNioSslSelfTest.java
@@ -17,15 +17,29 @@
package org.apache.ignite.internal.util.nio;
+import java.net.InetSocketAddress;
import java.net.Socket;
+import java.nio.ByteBuffer;
import java.nio.ByteOrder;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import javax.cache.configuration.Factory;
import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLSocket;
import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.internal.util.nio.impl.MockNioSession;
import org.apache.ignite.internal.util.nio.ssl.GridNioSslFilter;
import org.apache.ignite.internal.util.typedef.internal.U;
import org.apache.ignite.testframework.GridTestUtils;
+import org.jetbrains.annotations.Nullable;
import org.junit.Test;
+import static org.junit.Assert.assertThrows;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+
/**
* Tests for new NIO server with SSL enabled.
*/
@@ -40,6 +54,101 @@ public class GridNioSslSelfTest extends GridNioSelfTest {
sslCtx = GridTestUtils.sslContext();
}
+ /** */
+ @Test
+ public void testFilterCreationReportsSslFactoryFailure() {
+ IgniteException err = new IgniteException("Test SSL context creation
failure");
+ Factory<SSLContext> sslCtxFactory = () -> {
+ throw err;
+ };
+
+ assertSame(err, assertThrows(IgniteException.class,
+ () -> U.sslFilter(sslCtxFactory, false, ByteOrder.nativeOrder(),
log, null)));
+ }
+
+ /** */
+ @Test
+ public void testContextLookupFailureCountsOneRejection() throws Exception {
+ Runnable rejectedSessions = mock(Runnable.class);
+ GridNioSslFilter filter = new GridNioSslFilter(() -> {
+ throw new IgniteException("Test SSL context lookup failure");
+ }, false, ByteOrder.nativeOrder(), log, null, rejectedSessions);
+ GridNioSession ses = new MockNioSession();
+
+ filter.previousFilter(mock(GridNioFilter.class));
+
+ assertThrows(IgniteCheckedException.class, () ->
filter.onSessionOpened(ses));
+
+ filter.onSessionClosed(ses);
+
+ verify(rejectedSessions).run();
+ }
+
+ /** */
+ @Test
+ public void testServerAcceptsConnectionAfterSslFactoryFailure() throws
Exception {
+ AtomicBoolean failNextLookup = new AtomicBoolean();
+ CountDownLatch serverConnected = new CountDownLatch(1);
+
+ Factory<SSLContext> sslCtxFactory = () -> {
+ if (failNextLookup.getAndSet(false))
+ throw new IgniteException("Test SSL context lookup failure");
+
+ return sslCtx;
+ };
+
+ GridNioServer<ByteBuffer> srv = GridNioServer.<ByteBuffer>builder()
+ .address(U.getLocalHost())
+ .port(GridTestUtils.getFreeCommPort())
+ .listener(new GridNioServerListenerAdapter<>() {
+ /** {@inheritDoc} */
+ @Override public void onConnected(GridNioSession ses) {
+ serverConnected.countDown();
+ }
+
+ /** {@inheritDoc} */
+ @Override public void onDisconnected(GridNioSession ses,
@Nullable Exception e) {
+ // No-op.
+ }
+
+ /** {@inheritDoc} */
+ @Override public void onMessage(GridNioSession ses, ByteBuffer
msg) {
+ // No-op.
+ }
+ })
+ .logger(log)
+ .selectorCount(1)
+ .filters(U.sslFilter(sslCtxFactory, false,
ByteOrder.nativeOrder(), log, null))
+ .build();
+
+ srv.start();
+
+ try {
+ failNextLookup.set(true);
+
+ InetSocketAddress addr = new InetSocketAddress(U.getLocalHost(),
srv.port());
+
+ try (Socket sock = new Socket()) {
+ sock.connect(addr, 5_000);
+ sock.setSoTimeout(5_000);
+
+ assertEquals(-1, sock.getInputStream().read());
+ assertFalse(failNextLookup.get());
+ }
+
+ try (SSLSocket sock = (SSLSocket)createSocket()) {
+ sock.connect(addr, 5_000);
+ sock.setSoTimeout(5_000);
+ sock.startHandshake();
+
+ assertTrue(serverConnected.await(5, TimeUnit.SECONDS));
+ }
+ }
+ finally {
+ srv.stop();
+ }
+ }
+
/** {@inheritDoc} */
@Override protected Socket createSocket() throws IgniteCheckedException {
try {
@@ -71,7 +180,7 @@ public class GridNioSslSelfTest extends GridNioSelfTest {
.sendQueueLimit(0)
.filters(
new GridNioCodecFilter(parser, log, false),
- new GridNioSslFilter(sslCtx, true, ByteOrder.nativeOrder(),
log, null, null));
+ new GridNioSslFilter(() -> sslCtx, true,
ByteOrder.nativeOrder(), log, null, null));
}
/** {@inheritDoc} */
diff --git
a/modules/core/src/test/java/org/apache/ignite/spi/discovery/tcp/TcpDiscoverySslConnectionFailureTest.java
b/modules/core/src/test/java/org/apache/ignite/spi/discovery/tcp/TcpDiscoverySslConnectionFailureTest.java
new file mode 100644
index 00000000000..c0dad1b0cdd
--- /dev/null
+++
b/modules/core/src/test/java/org/apache/ignite/spi/discovery/tcp/TcpDiscoverySslConnectionFailureTest.java
@@ -0,0 +1,171 @@
+/*
+ * 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.ignite.spi.discovery.tcp;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.net.Socket;
+import java.net.SocketTimeoutException;
+import java.util.concurrent.atomic.AtomicBoolean;
+import javax.cache.configuration.Factory;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLSocket;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.internal.IgniteEx;
+import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder;
+import org.apache.ignite.ssl.SslContextFactory;
+import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
+import org.junit.Test;
+
+import static
org.apache.ignite.testframework.GridTestUtils.assertThrowsAnyCause;
+import static org.apache.ignite.testframework.GridTestUtils.keyStorePassword;
+import static org.apache.ignite.testframework.GridTestUtils.keyStorePath;
+import static org.apache.ignite.testframework.GridTestUtils.sslTrustedFactory;
+import static org.junit.Assert.assertThrows;
+
+/** Tests discovery TLS startup and connection failures. */
+public class TcpDiscoverySslConnectionFailureTest extends
GridCommonAbstractTest {
+ /** */
+ private static final int SOCK_TIMEOUT = 10_000;
+
+ /** */
+ private final TcpDiscoveryVmIpFinder ipFinder = new
TcpDiscoveryVmIpFinder(true);
+
+ /** {@inheritDoc} */
+ @Override protected void afterTest() {
+ stopAllGrids();
+ }
+
+ /** */
+ @Test
+ public void testInvalidProtocolPreventsFirstNodeStartup() throws Exception
{
+ SslContextFactory factory =
(SslContextFactory)sslTrustedFactory("node01", "trustone");
+ factory.setProtocols("TestUnsupportedProtocol");
+
+ assertThrowsAnyCause(log, () -> startNode(factory),
IllegalArgumentException.class, "TestUnsupportedProtocol");
+ }
+
+ /** */
+ @Test
+ public void testDiscoveryAcceptsConnectionAfterSslFactoryFailure() throws
Exception {
+ SSLContext ctx = sslTrustedFactory("node01", "trustone").create();
+ AtomicBoolean failNextCreate = new AtomicBoolean();
+ Factory<SSLContext> sslCtxFactory = () -> {
+ if (failNextCreate.getAndSet(false))
+ throw new IgniteException("Test SSL context creation failure");
+
+ return ctx;
+ };
+ InetSocketAddress addr = discoveryAddress(startNode(sslCtxFactory));
+
+ failNextCreate.set(true);
+
+ connectAndAssertClosed(addr);
+
+ assertFalse(failNextCreate.get());
+
+ connectAndHandshake(addr, ctx);
+ }
+
+ /** */
+ @Test
+ public void testDiscoveryAcceptsConnectionAfterInvalidProtocolReload()
throws Exception {
+ ReloadableSslContextFactory factory = new
ReloadableSslContextFactory();
+ InetSocketAddress addr = discoveryAddress(startNode(factory));
+ SSLContext clientCtx = sslTrustedFactory("node01",
"trustone").create();
+
+ factory.reloadProtocols("TestUnsupportedProtocol");
+
+ IOException err = assertThrows(IOException.class, () ->
connectAndHandshake(addr, clientCtx));
+
+ assertFalse(err instanceof SocketTimeoutException);
+
+ factory.reloadProtocols("TLSv1.2");
+
+ connectAndHandshake(addr, clientCtx);
+ }
+
+ /** */
+ @Test
+ public void testOutgoingSocketSetupFailureIsReportedAsIoException() throws
Exception {
+ ReloadableSslContextFactory factory = new
ReloadableSslContextFactory();
+ IgniteEx node = startNode(factory);
+ TcpDiscoverySpi spi =
(TcpDiscoverySpi)node.configuration().getDiscoverySpi();
+
+ factory.reloadProtocols("TestUnsupportedProtocol");
+
+ IOException err = assertThrows(IOException.class, () ->
spi.createSocket().close());
+
+ assertTrue(err.getCause() instanceof IllegalArgumentException);
+ }
+
+ /** */
+ private IgniteEx startNode(Factory<SSLContext> factory) throws Exception {
+ return startGrid(0, cfg -> {
+ ((TcpDiscoverySpi)cfg.getDiscoverySpi()).setIpFinder(ipFinder);
+ cfg.setSslContextFactory(factory);
+ });
+ }
+
+ /** */
+ private static InetSocketAddress discoveryAddress(IgniteEx node) {
+ int port =
((TcpDiscoverySpi)node.configuration().getDiscoverySpi()).getLocalPort();
+
+ return new InetSocketAddress("127.0.0.1", port);
+ }
+
+ /** */
+ private static void connectAndAssertClosed(InetSocketAddress addr) throws
Exception {
+ try (Socket sock = new Socket()) {
+ sock.connect(addr, SOCK_TIMEOUT);
+ sock.setSoTimeout(SOCK_TIMEOUT);
+
+ assertEquals("Discovery must close the socket after TLS setup
fails", -1, sock.getInputStream().read());
+ }
+ }
+
+ /** */
+ private static void connectAndHandshake(InetSocketAddress addr, SSLContext
ctx) throws Exception {
+ try (SSLSocket sock =
(SSLSocket)ctx.getSocketFactory().createSocket()) {
+ sock.connect(addr, SOCK_TIMEOUT);
+ sock.setSoTimeout(SOCK_TIMEOUT);
+ sock.startHandshake();
+ }
+ }
+
+ /** */
+ private static class ReloadableSslContextFactory extends SslContextFactory
{
+ /** */
+ private static final long serialVersionUID = 0L;
+
+ /** */
+ private ReloadableSslContextFactory() {
+ setKeyStoreFilePath(keyStorePath("node01"));
+ setKeyStorePassword(keyStorePassword().toCharArray());
+ setTrustStoreFilePath(keyStorePath("trustone"));
+ setTrustStorePassword(keyStorePassword().toCharArray());
+ }
+
+ /** */
+ private void reloadProtocols(String... protocols) {
+ setProtocols(protocols);
+
+ init();
+ }
+ }
+}
diff --git
a/modules/core/src/test/java/org/apache/ignite/ssl/SslContextReloadTest.java
b/modules/core/src/test/java/org/apache/ignite/ssl/SslContextReloadTest.java
new file mode 100644
index 00000000000..c339b4fcdf8
--- /dev/null
+++ b/modules/core/src/test/java/org/apache/ignite/ssl/SslContextReloadTest.java
@@ -0,0 +1,341 @@
+/*
+ * 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.ignite.ssl;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.InetSocketAddress;
+import java.net.ServerSocket;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.StandardCopyOption;
+import java.security.KeyStore;
+import java.security.cert.Certificate;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import javax.cache.configuration.Factory;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLSocket;
+import javax.net.ssl.TrustManager;
+import javax.net.ssl.TrustManagerFactory;
+import org.apache.ignite.Ignition;
+import org.apache.ignite.client.ClientConnectionException;
+import org.apache.ignite.client.ClientRetryNonePolicy;
+import org.apache.ignite.client.IgniteClient;
+import org.apache.ignite.client.SslMode;
+import org.apache.ignite.configuration.ClientConfiguration;
+import org.apache.ignite.configuration.ClientConnectorConfiguration;
+import org.apache.ignite.configuration.ConnectorConfiguration;
+import org.apache.ignite.internal.IgniteEx;
+import org.apache.ignite.internal.IgniteInternalFuture;
+import
org.apache.ignite.internal.processors.rest.client.message.GridClientPingPacket;
+import org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi;
+import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
+import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder;
+import org.apache.ignite.testframework.GridTestUtils;
+import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
+import org.junit.Test;
+
+import static
org.apache.ignite.internal.IgniteNodeAttributes.ATTR_REST_TCP_PORT;
+import static org.apache.ignite.testframework.GridTestUtils.keyStorePassword;
+import static org.apache.ignite.testframework.GridTestUtils.keyStorePath;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertThrows;
+
+/** Tests TLS reload across TCP transports. */
+public class SslContextReloadTest extends GridCommonAbstractTest {
+ /** */
+ private static final int SOCKET_TIMEOUT = 10_000;
+
+ /** */
+ private static final String KEYS_1 = "node01";
+
+ /** */
+ private static final String KEYS_2 = "node02";
+
+ /** */
+ private static final String LOOPBACK_ADDRESS = "127.0.0.1";
+
+ /** Discovery addresses shared within one test. */
+ private final TcpDiscoveryVmIpFinder ipFinder = new
TcpDiscoveryVmIpFinder(true);
+
+ /** {@inheritDoc} */
+ @Override protected void afterTest() {
+ stopAllGrids();
+ }
+
+ /** */
+ @Test
+ public void testNewConnectionsUseReloadedContext() throws Exception {
+ SslContextFactory serverFactory = sslFactory(KEYS_1,
trustIssuersOf(KEYS_1));
+ IgniteEx node = startNode(serverFactory::create);
+
+ serverFactory.setKeyStoreFilePath(keyStorePath(KEYS_2));
+ serverFactory.setTrustManagers(trustIssuersOf(KEYS_2));
+ serverFactory.init();
+
+ SSLContext client = sslFactory(KEYS_2,
trustIssuersOf(KEYS_2)).create();
+
+ for (int port : tlsPorts(node))
+ connectAndHandshake(port, client);
+ }
+
+ /** */
+ @Test
+ public void testExistingConnectionSurvivesReload() throws Exception {
+ SslContextFactory serverFactory = sslFactory(KEYS_1,
trustIssuersOf(KEYS_1));
+ IgniteEx node = startNode(serverFactory);
+
+ ClientConfiguration cfg =
clientConfiguration(node).setSslContextFactory(sslFactory(KEYS_1,
trustIssuersOf(KEYS_1)));
+
+ try (IgniteClient client = Ignition.startClient(cfg)) {
+ client.cacheNames();
+
+ serverFactory.setKeyStoreFilePath(keyStorePath(KEYS_2));
+ serverFactory.setTrustManagers(trustIssuersOf(KEYS_2));
+ serverFactory.init();
+
+ client.cacheNames();
+ }
+ }
+
+ /** */
+ @Test
+ public void testNewConnectionsDoNotWaitForReload() throws Exception {
+ String replacementKeyStore = keyStorePath(KEYS_2);
+ CountDownLatch reloadStarted = new CountDownLatch(1);
+ CountDownLatch allowReload = new CountDownLatch(1);
+
+ SslContextFactory factory = new SslContextFactory() {
+ /** */
+ private static final long serialVersionUID = 0L;
+
+ /** {@inheritDoc} */
+ @Override protected InputStream openFileInputStream(String path)
throws IOException {
+ if (replacementKeyStore.equals(path)) {
+ reloadStarted.countDown();
+
+ try {
+ if (!allowReload.await(60, TimeUnit.SECONDS))
+ throw new IOException("Timed out waiting to open
the replacement key store");
+ }
+ catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+
+ throw new IOException(e);
+ }
+ }
+
+ return super.openFileInputStream(path);
+ }
+ };
+
+ TrustManager[] trustedCertificates = trustIssuersOf(KEYS_1);
+
+ factory.setKeyStoreFilePath(keyStorePath(KEYS_1));
+ factory.setKeyStorePassword(keyStorePassword().toCharArray());
+ factory.setTrustManagers(trustedCertificates);
+
+ IgniteEx node = startNode(factory);
+ SSLContext client = sslFactory(KEYS_1, trustedCertificates).create();
+
+ factory.setKeyStoreFilePath(replacementKeyStore);
+
+ IgniteInternalFuture<?> reload = GridTestUtils.runAsync(factory::init);
+
+ try {
+ assertTrue("Reload did not reach the replacement key store",
reloadStarted.await(30, TimeUnit.SECONDS));
+
+ for (int port : tlsPorts(node))
+ connectAndHandshake(port, client);
+ }
+ finally {
+ allowReload.countDown();
+ reload.get(30, TimeUnit.SECONDS);
+ }
+ }
+
+ /** */
+ @Test
+ public void testRestAcceptsConnectionAfterPortRetry() throws Exception {
+ SSLContext ctx = sslFactory(KEYS_1, trustIssuersOf(KEYS_1)).create();
+
+ try (ServerSocket occupied = new ServerSocket()) {
+ occupied.bind(new InetSocketAddress(LOOPBACK_ADDRESS,
GridTestUtils.getFreeCommPort()));
+
+ IgniteEx node = startGrid(0, cfg -> {
+ ((TcpDiscoverySpi)cfg.getDiscoverySpi()).setIpFinder(ipFinder);
+
+ cfg.setClientConnectorConfiguration(null)
+ .setConnectorConfiguration(new ConnectorConfiguration()
+ .setPort(occupied.getLocalPort())
+ .setSslEnabled(true)
+ .setSslFactory(() -> ctx));
+ });
+
+ int port = node.localNode().attribute(ATTR_REST_TCP_PORT);
+
+ assertTrue("REST did not retry the occupied port", port >
occupied.getLocalPort());
+
+ try (SSLSocket sock =
(SSLSocket)ctx.getSocketFactory().createSocket()) {
+ sock.connect(new InetSocketAddress(LOOPBACK_ADDRESS, port),
SOCKET_TIMEOUT);
+ sock.setSoTimeout(SOCKET_TIMEOUT);
+ sock.startHandshake();
+ sock.getOutputStream().write(GridClientPingPacket.PING_PACKET);
+
+ assertArrayEquals(GridClientPingPacket.PING_PACKET,
+
sock.getInputStream().readNBytes(GridClientPingPacket.PING_PACKET.length));
+ }
+ }
+ }
+
+ /** */
+ @Test
+ public void testThinClientReconnectUsesReloadedContext() throws Exception {
+ SslContextFactory clientFactory = sslFactory(KEYS_1,
trustIssuersOf(KEYS_1));
+ IgniteEx node = startNode(sslFactory(KEYS_1, trustIssuersOf(KEYS_1)));
+
+ ClientConfiguration cfg =
clientConfiguration(node).setSslContextFactory(clientFactory::create);
+
+ try (IgniteClient client = Ignition.startClient(cfg)) {
+ client.cacheNames();
+
+ stopGrid(0);
+
+ assertThrows(ClientConnectionException.class, client::cacheNames);
+
+ clientFactory.setKeyStoreFilePath(keyStorePath(KEYS_2));
+ clientFactory.setTrustManagers(trustIssuersOf(KEYS_2));
+ clientFactory.init();
+
+ startNode(sslFactory(KEYS_2, trustIssuersOf(KEYS_2)));
+
+ client.cacheNames();
+ }
+ }
+
+ /** */
+ @Test
+ public void testThinClientReconnectDoesNotReloadKeyStore() throws
Exception {
+ Factory<SSLContext> serverFactory = sslFactory(KEYS_1,
trustIssuersOf(KEYS_1));
+ IgniteEx node = startNode(serverFactory);
+ Path keyStore = Files.createTempFile("thin-client-keys", ".jks");
+ final String storeType = "JKS";
+
+ try {
+ Files.copy(Path.of(keyStorePath(KEYS_1)), keyStore,
StandardCopyOption.REPLACE_EXISTING);
+
+ ClientConfiguration cfg = clientConfiguration(node)
+ .setSslClientCertificateKeyStorePath(keyStore.toString())
+ .setSslClientCertificateKeyStoreType(storeType)
+ .setSslClientCertificateKeyStorePassword(keyStorePassword())
+ .setSslTrustCertificateKeyStorePath(keyStorePath(KEYS_1))
+ .setSslTrustCertificateKeyStoreType(storeType)
+ .setSslTrustCertificateKeyStorePassword(keyStorePassword());
+
+ try (IgniteClient client = Ignition.startClient(cfg)) {
+ client.cacheNames();
+
+ Files.delete(keyStore);
+ stopGrid(0);
+
+ assertThrows(ClientConnectionException.class,
client::cacheNames);
+
+ startNode(serverFactory);
+
+ client.cacheNames();
+ }
+ }
+ finally {
+ Files.deleteIfExists(keyStore);
+ }
+ }
+
+ /** */
+ private IgniteEx startNode(Factory<SSLContext> factory) throws Exception {
+ return startGrid(0, cfg -> {
+ ((TcpDiscoverySpi)cfg.getDiscoverySpi()).setIpFinder(ipFinder);
+
+ cfg.setSslContextFactory(factory)
+ .setClientConnectorConfiguration(new
ClientConnectorConfiguration()
+ .setSslEnabled(true)
+ .setSslClientAuth(true))
+ .setConnectorConfiguration(new
ConnectorConfiguration().setSslEnabled(true).setSslClientAuth(true));
+ });
+ }
+
+ /** */
+ private static int[] tlsPorts(IgniteEx node) {
+ return new int[] {
+
((TcpDiscoverySpi)node.configuration().getDiscoverySpi()).getLocalPort(),
+
((TcpCommunicationSpi)node.configuration().getCommunicationSpi()).boundPort(),
+ node.context().clientListener().port(),
+ node.localNode().<Integer>attribute(ATTR_REST_TCP_PORT)
+ };
+ }
+
+ /** */
+ private static ClientConfiguration clientConfiguration(IgniteEx node) {
+ return new ClientConfiguration()
+ .setAddresses(LOOPBACK_ADDRESS + ":" +
node.context().clientListener().port())
+ .setRequestTimeout(SOCKET_TIMEOUT)
+ .setSslMode(SslMode.REQUIRED)
+ .setRetryPolicy(new ClientRetryNonePolicy())
+ .setPartitionAwarenessEnabled(false)
+ .setClusterDiscoveryEnabled(false);
+ }
+
+ /** */
+ private static void connectAndHandshake(int port, SSLContext ctx) throws
IOException {
+ try (SSLSocket sock =
(SSLSocket)ctx.getSocketFactory().createSocket()) {
+ sock.connect(new InetSocketAddress(LOOPBACK_ADDRESS, port),
SOCKET_TIMEOUT);
+ sock.setSoTimeout(SOCKET_TIMEOUT);
+ sock.startHandshake();
+ }
+ }
+
+ /** */
+ private static SslContextFactory sslFactory(String keys, TrustManager[]
trustManagers) {
+ SslContextFactory factory = new SslContextFactory();
+
+ factory.setKeyStoreFilePath(keyStorePath(keys));
+ factory.setKeyStorePassword(keyStorePassword().toCharArray());
+ factory.setTrustManagers(trustManagers);
+
+ return factory;
+ }
+
+ /** */
+ private static TrustManager[] trustIssuersOf(String keys) throws Exception
{
+ KeyStore trustStore = KeyStore.getInstance("JKS");
+
+ trustStore.load(null, null);
+
+ KeyStore keyStore =
KeyStore.getInstance(Path.of(keyStorePath(keys)).toFile(),
keyStorePassword().toCharArray());
+ Certificate[] chain = keyStore.getCertificateChain(keys);
+
+ for (int i = 1; i < chain.length; i++)
+ trustStore.setCertificateEntry(keys + i, chain[i]);
+
+ TrustManagerFactory factory =
TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
+
+ factory.init(trustStore);
+
+ return factory.getTrustManagers();
+ }
+}
diff --git
a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteSpiDiscoverySelfTestSuite.java
b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteSpiDiscoverySelfTestSuite.java
index 517d528e3a0..54647aaf5ad 100644
---
a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteSpiDiscoverySelfTestSuite.java
+++
b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteSpiDiscoverySelfTestSuite.java
@@ -46,6 +46,7 @@ import
org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpiReconnectDelayTest;
import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpiSelfTest;
import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpiStartStopSelfTest;
import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpiWildcardSelfTest;
+import
org.apache.ignite.spi.discovery.tcp.TcpDiscoverySslConnectionFailureTest;
import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySslParametersTest;
import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySslSecuredUnsecuredTest;
import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySslTrustedSelfTest;
@@ -107,6 +108,7 @@ import static
org.apache.ignite.IgniteSystemProperties.IGNITE_OVERRIDE_MCAST_GRP
TcpDiscoverySslTrustedSelfTest.class,
TcpDiscoverySslSecuredUnsecuredTest.class,
TcpDiscoverySslParametersTest.class,
+ TcpDiscoverySslConnectionFailureTest.class,
// Disco cache reuse.
diff --git
a/modules/core/src/test/java/org/apache/ignite/testsuites/SecurityTestSuite.java
b/modules/core/src/test/java/org/apache/ignite/testsuites/SecurityTestSuite.java
index 4a2770a2064..22fec30c574 100644
---
a/modules/core/src/test/java/org/apache/ignite/testsuites/SecurityTestSuite.java
+++
b/modules/core/src/test/java/org/apache/ignite/testsuites/SecurityTestSuite.java
@@ -17,6 +17,7 @@
package org.apache.ignite.testsuites;
+import org.apache.ignite.internal.client.thin.ClientSslUtilsTest;
import
org.apache.ignite.internal.processors.security.IgniteSecurityProcessorTest;
import org.apache.ignite.internal.processors.security.InvalidServerTest;
import
org.apache.ignite.internal.processors.security.NodeConnectionCertificateCapturingTest;
@@ -81,6 +82,7 @@ import
org.apache.ignite.plugin.security.SecurityBasicPermissionSetSerialization
import org.apache.ignite.ssl.MultipleSSLContextsTest;
import org.apache.ignite.ssl.SSLContextWrapperTest;
import org.apache.ignite.ssl.SslContextFactoryInitTest;
+import org.apache.ignite.ssl.SslContextReloadTest;
import org.apache.ignite.tools.junit.JUnitTeamcityReporter;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
@@ -145,6 +147,8 @@ import org.junit.runners.Suite;
MultipleSSLContextsTest.class,
SSLContextWrapperTest.class,
SslContextFactoryInitTest.class,
+ SslContextReloadTest.class,
+ ClientSslUtilsTest.class,
MaintenanceModeNodeSecurityTest.class,
ServiceAuthorizationTest.class,
ServiceStaticConfigTest.class,
diff --git
a/modules/nio/src/main/java/org/apache/ignite/internal/util/nio/ssl/GridNioSslFilter.java
b/modules/nio/src/main/java/org/apache/ignite/internal/util/nio/ssl/GridNioSslFilter.java
index 9abcae2ad56..260dd6b76b0 100644
---
a/modules/nio/src/main/java/org/apache/ignite/internal/util/nio/ssl/GridNioSslFilter.java
+++
b/modules/nio/src/main/java/org/apache/ignite/internal/util/nio/ssl/GridNioSslFilter.java
@@ -21,6 +21,7 @@ import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.function.LongConsumer;
+import javax.cache.configuration.Factory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.SSLException;
@@ -68,8 +69,8 @@ public class GridNioSslFilter extends GridNioFilterAdapter {
/** Array of enabled protocols. */
private String[] enabledProtos;
- /** SSL context to use. */
- private SSLContext sslCtx;
+ /** */
+ private final Factory<SSLContext> sslCtxFactory;
/** Order. */
private ByteOrder order;
@@ -92,7 +93,7 @@ public class GridNioSslFilter extends GridNioFilterAdapter {
/**
* Creates SSL filter.
*
- * @param sslCtx SSL context.
+ * @param sslCtxFactory SSL context factory.
* @param directBuf Direct buffer flag.
* @param order Byte order.
* @param log Logger to use.
@@ -100,7 +101,7 @@ public class GridNioSslFilter extends GridNioFilterAdapter {
* @param rejectedSesCnt Increments the rejected-sessions counter, or
{@code null} if metrics disabled.
*/
public GridNioSslFilter(
- SSLContext sslCtx,
+ Factory<SSLContext> sslCtxFactory,
boolean directBuf,
ByteOrder order,
IgniteLogger log,
@@ -110,7 +111,7 @@ public class GridNioSslFilter extends GridNioFilterAdapter {
super("SSL filter");
this.log = log;
- this.sslCtx = sslCtx;
+ this.sslCtxFactory = sslCtxFactory;
this.directBuf = directBuf;
this.order = order;
this.handshakeDuration = handshakeDuration;
@@ -179,6 +180,18 @@ public class GridNioSslFilter extends GridNioFilterAdapter
{
GridSslMeta sslMeta = ses.meta(SSL_META.ordinal());
if (sslMeta == null) {
+ SSLContext sslCtx;
+
+ try {
+ sslCtx = sslCtxFactory.create();
+ }
+ catch (RuntimeException e) {
+ if (rejectedSesCnt != null && ses.meta(HANDSHAKE_FUT_META_KEY)
== null)
+ rejectedSesCnt.run();
+
+ throw new IgniteCheckedException("Failed to obtain SSL context
for a new connection.", e);
+ }
+
try {
engine = sslCtx.createSSLEngine();
}
diff --git
a/modules/thin-client/impl/src/main/java/org/apache/ignite/internal/client/thin/ClientSslUtils.java
b/modules/thin-client/impl/src/main/java/org/apache/ignite/internal/client/thin/ClientSslUtils.java
index 4264aa1c3a3..44a1fe9f5ce 100644
---
a/modules/thin-client/impl/src/main/java/org/apache/ignite/internal/client/thin/ClientSslUtils.java
+++
b/modules/thin-client/impl/src/main/java/org/apache/ignite/internal/client/thin/ClientSslUtils.java
@@ -71,12 +71,12 @@ public class ClientSslUtils {
};
/**
- * Gets SSL context for the given client configuration.
+ * Gets SSL context factory for the given client configuration.
*
* @param cfg Configuration.
- * @return {@link SSLContext} when SSL is enabled in the configuration;
null otherwise.
+ * @return SSL context factory when SSL is enabled in the configuration;
{@code null} otherwise.
*/
- public static SSLContext getSslContext(ClientConfiguration cfg) {
+ public static Factory<SSLContext> getSslContextFactory(ClientConfiguration
cfg) {
if (cfg.getSslMode() == SslMode.DISABLED)
return null;
@@ -84,11 +84,13 @@ public class ClientSslUtils {
if (sslCtxFactory != null) {
try {
- return sslCtxFactory.create();
+ sslCtxFactory.create();
}
catch (Exception e) {
throw new ClientError("SSL Context Factory failed", e);
}
+
+ return sslCtxFactory;
}
BiFunction<String, String, String> or = (val, dflt) -> val == null ||
val.isEmpty() ? dflt : val;
@@ -131,7 +133,9 @@ public class ClientSslUtils {
.allMatch(s -> s == null || s.isEmpty())
) {
try {
- return SSLContext.getDefault();
+ SSLContext sslCtx = SSLContext.getDefault();
+
+ return () -> sslCtx;
}
catch (NoSuchAlgorithmException e) {
throw new ClientError("Default SSL context cryptographic
algorithm is not available", e);
@@ -149,7 +153,7 @@ public class ClientSslUtils {
sslCtx.init(keyManagers, trustManagers, null);
- return sslCtx;
+ return () -> sslCtx;
}
catch (NoSuchAlgorithmException e) {
throw new ClientError("SSL context cryptographic algorithm is not
available", e);
diff --git
a/modules/thin-client/impl/src/main/java/org/apache/ignite/internal/client/thin/io/gridnioserver/GridNioClientConnectionMultiplexer.java
b/modules/thin-client/impl/src/main/java/org/apache/ignite/internal/client/thin/io/gridnioserver/GridNioClientConnectionMultiplexer.java
index f5091c31d94..568f2d1cc8b 100644
---
a/modules/thin-client/impl/src/main/java/org/apache/ignite/internal/client/thin/io/gridnioserver/GridNioClientConnectionMultiplexer.java
+++
b/modules/thin-client/impl/src/main/java/org/apache/ignite/internal/client/thin/io/gridnioserver/GridNioClientConnectionMultiplexer.java
@@ -25,6 +25,7 @@ import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
+import javax.cache.configuration.Factory;
import javax.net.ssl.SSLContext;
import org.apache.ignite.IgniteCheckedException;
import org.apache.ignite.IgniteException;
@@ -63,7 +64,7 @@ public class GridNioClientConnectionMultiplexer implements
ClientConnectionMulti
private final GridNioServer<ByteBuffer> srv;
/** */
- private final SSLContext sslCtx;
+ private final boolean sslEnabled;
/** */
private final ReadWriteLock rwLock = new ReentrantReadWriteLock();
@@ -83,10 +84,12 @@ public class GridNioClientConnectionMultiplexer implements
ClientConnectionMulti
GridNioFilter codecFilter = new GridNioCodecFilter(new
GridNioClientParser(), gridLog, false);
- sslCtx = ClientSslUtils.getSslContext(cfg);
+ Factory<SSLContext> sslCtxFactory =
ClientSslUtils.getSslContextFactory(cfg);
- if (sslCtx != null) {
- GridNioSslFilter sslFilter = new GridNioSslFilter(sslCtx, true,
ByteOrder.nativeOrder(), gridLog, null, null);
+ sslEnabled = sslCtxFactory != null;
+
+ if (sslEnabled) {
+ GridNioSslFilter sslFilter = new GridNioSslFilter(sslCtxFactory,
true, ByteOrder.nativeOrder(), gridLog, null, null);
sslFilter.directMode(false);
filters = new GridNioFilter[] {codecFilter, sslFilter};
}
@@ -189,7 +192,7 @@ public class GridNioClientConnectionMultiplexer implements
ClientConnectionMulti
GridFutureAdapter<?> sslHandshakeFut = null;
- if (sslCtx != null) {
+ if (sslEnabled) {
sslHandshakeFut = new GridFutureAdapter<>();
meta.put(GridNioSslFilter.HANDSHAKE_FUT_META_KEY, sslHandshakeFut);