zstan commented on code in PR #13874: URL: https://github.com/apache/ignite/pull/13874#discussion_r4738948350
########## modules/rest-http/src/test/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/JettyRestHttpsReloadTest.java: ########## @@ -0,0 +1,602 @@ +/* + * 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.processors.rest.protocols.http.jetty; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.security.KeyStore; +import java.security.cert.Certificate; +import java.util.Deque; +import java.util.List; +import java.util.concurrent.ConcurrentLinkedDeque; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.Consumer; +import javax.net.ssl.SNIHostName; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLParameters; +import javax.net.ssl.SSLSocket; +import javax.net.ssl.TrustManagerFactory; +import org.apache.ignite.IgniteException; +import org.apache.ignite.configuration.ConnectorConfiguration; +import org.apache.ignite.configuration.IgniteConfiguration; +import org.apache.ignite.internal.IgniteEx; +import org.apache.ignite.internal.management.api.CommandInvoker; +import org.apache.ignite.internal.management.api.NoArg; +import org.apache.ignite.internal.management.ssl.SslReloadCommand; +import org.apache.ignite.internal.processors.ssl.ReloadableSslFactory; +import org.apache.ignite.ssl.TestCertificateAuthority; +import org.apache.ignite.ssl.jetty.AutoReloadJettySslFactory; +import org.apache.ignite.testframework.junits.WithSystemProperty; +import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; +import org.eclipse.jetty.util.resource.Resource; +import org.eclipse.jetty.util.ssl.SslContextFactory; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import static java.util.concurrent.TimeUnit.DAYS; +import static java.util.concurrent.TimeUnit.HOURS; +import static java.util.concurrent.TimeUnit.SECONDS; +import static org.apache.ignite.IgniteSystemProperties.IGNITE_JETTY_PORT; +import static org.apache.ignite.internal.IgniteNodeAttributes.ATTR_REST_JETTY_PORT; +import static org.apache.ignite.internal.IgniteNodeAttributes.ATTR_REST_TCP_PORT; +import static org.apache.ignite.internal.processors.rest.protocols.http.jetty.GridJettyRestProtocol.DFLT_JETTY_PORT; +import static org.apache.ignite.ssl.AbstractSslContextFactory.DFLT_AUTO_RELOAD_BEFORE_EXPIRY_MS; +import static org.apache.ignite.ssl.TestCertificateAuthority.PASSWORD; +import static org.apache.ignite.ssl.TestCertificateAuthority.save; +import static org.apache.ignite.testframework.GridTestUtils.assertThrows; +import static org.apache.ignite.testframework.GridTestUtils.assertThrowsAnyCause; +import static org.apache.ignite.testframework.GridTestUtils.waitForCondition; + +/** + * TLS reload of the HTTP REST connector configured in Jetty XML. The standard Jetty factory reads its key store from a file + * and requires an SNI host name ({@code jetty/https.xml}); {@code jetty/https-detector.xml} reads the same file through + * a connector that accepts HTTP and HTTPS on one port. The custom factory ({@code jetty/https-source.xml}) takes its + * key stores from {@link SourceSslContextFactory}; {@code jetty/https-source-manual.xml} configures the same source + * with automatic reload disabled. The connectors in these files set no port, so the tests set {@code IGNITE_JETTY_PORT}; + * otherwise the node would publish port 0. + */ +@WithSystemProperty(key = IGNITE_JETTY_PORT, value = DFLT_JETTY_PORT) +public class JettyRestHttpsReloadTest extends GridCommonAbstractTest { + /** Path read by the HTTPS connector in {@code jetty/https.xml}. */ + private static final String KEY_STORE_PATH = "ignite.test.JettyRestHttpsReloadTest.keyStorePath"; + + /** Host name in the server certificates and the HTTPS requests. */ + private static final String HOST = "localhost"; + + /** Upper bound for waiting for an event that must happen. */ + private static final long TIMEOUT_MS = 30_000; + + /** */ + @Rule public TemporaryFolder tmp = new TemporaryFolder(); + + /** Current time rounded down to seconds, the precision of certificate dates. */ + private final long now = System.currentTimeMillis() / 1_000 * 1_000; + + /** Issuer of all server certificates, which the HTTPS clients trust. */ + private TestCertificateAuthority ca; + + /** {@inheritDoc} */ + @Override protected void beforeTest() throws Exception { + super.beforeTest(); + + ca = new TestCertificateAuthority("ca", now - DAYS.toMillis(1), now + DAYS.toMillis(365)); + } + + /** {@inheritDoc} */ + @Override protected void afterTest() throws Exception { + stopAllGrids(); + SourceSslContextFactory.supply(); + + super.afterTest(); + } + + /** + * Given: HTTPS serves certificate A from a key store file of the standard Jetty factory. + * When: the file gets certificate B, and the operator runs the reload command. + * Then: the command reports HTTP REST as reloaded, and a new client sees B. + */ + @Test + @WithSystemProperty(key = KEY_STORE_PATH, value = "") + public void testReloadCommandAppliesReplacementKeyStore() throws Exception { + KeyStore a = fresh(); + KeyStore b = fresh(); + Path keys = keyStoreFile(a); + + IgniteEx node = startGrid(jettyConfig("jetty/https.xml")); + + assertEquals(a.getCertificate(HOST), serverCertificate(node)); + + save(b, keys); + + assertTrue(reload(node).contains(": HTTP REST: reloaded")); + assertEquals(b.getCertificate(HOST), serverCertificate(node)); + } + + /** + * Given: the Jetty connector accepts HTTP and HTTPS on one port, so its TLS factory sits inside + * a {@code DetectorConnectionFactory}, and HTTPS serves certificate A from a key store file. + * When: the file gets certificate B, and the operator runs the reload command. + * Then: the command reports HTTP REST as reloaded, and a new client sees B. + */ + @Test + @WithSystemProperty(key = KEY_STORE_PATH, value = "") + public void testReloadCommandFindsTlsFactoryInsideDetector() throws Exception { + KeyStore a = fresh(); + KeyStore b = fresh(); + Path keys = keyStoreFile(a); + + IgniteEx node = startGrid(jettyConfig("jetty/https-detector.xml")); + + assertEquals(a.getCertificate(HOST), serverCertificate(node)); + + save(b, keys); + + assertTrue(reload(node).contains(": HTTP REST: reloaded")); + assertEquals(b.getCertificate(HOST), serverCertificate(node)); + } + + /** + * Given: HTTPS serves certificate A from a key store file of the standard Jetty factory, which selects certificates + * by SNI host name. + * When: the file becomes unreadable and the operator runs the reload command, then the file gets certificate B and + * the operator runs the command again. + * Then: the first command reports the node and the cause, and a new client that sends the host name still sees A. + * After the second command, a new client sees B. + */ + @Test + @WithSystemProperty(key = KEY_STORE_PATH, value = "") + public void testFailedReloadKeepsLastWorkingKeys() throws Exception { + KeyStore a = fresh(); + KeyStore b = fresh(); + Path keys = keyStoreFile(a); + + IgniteEx node = startGrid(jettyConfig("jetty/https.xml")); + + Files.writeString(keys, "not a key store"); + + assertThrowsAnyCause(log, () -> reload(node), IgniteException.class, node.localNode().id() + ": failed: HTTP REST: "); + assertEquals(a.getCertificate(HOST), serverCertificate(node)); + + save(b, keys); + reload(node); + + assertEquals(b.getCertificate(HOST), serverCertificate(node)); + } + + /** + * Given: a custom Jetty factory starts with certificate A, which is due for automatic reload, and its source then supplies B. + * When: the node runs its automatic reload check at startup, then the source fails and the operator runs the reload command. + * Then: a new client sees B after the check. The command fails, and a new client still sees B. + */ + @Test + public void testAutoReloadedKeysSurviveFailedManualReload() throws Exception { Review Comment: yes, all right but we need to check a reload isn`t it ? -- 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]
