alopresto commented on a change in pull request #4263: URL: https://github.com/apache/nifi/pull/4263#discussion_r424712012
########## File path: nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/test/groovy/org/apache/nifi/controller/queue/clustered/server/ConnectionLoadBalanceServerTest.groovy ########## @@ -0,0 +1,185 @@ +/* + * 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.nifi.controller.queue.clustered.server + +import org.apache.nifi.events.EventReporter +import org.apache.nifi.reporting.Severity +import org.apache.nifi.security.util.CertificateUtils +import org.apache.nifi.security.util.KeyStoreUtils +import org.apache.nifi.security.util.KeystoreType +import org.apache.nifi.security.util.SslContextFactory +import org.apache.nifi.security.util.TlsConfiguration +import org.bouncycastle.jce.provider.BouncyCastleProvider +import org.junit.After +import org.junit.Before +import org.junit.BeforeClass +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 +import org.slf4j.Logger +import org.slf4j.LoggerFactory + +import javax.net.ssl.SSLContext +import javax.net.ssl.SSLPeerUnverifiedException +import javax.net.ssl.SSLServerSocket +import java.security.Security + +@RunWith(JUnit4.class) +class ConnectionLoadBalanceServerTest extends GroovyTestCase { + private static final Logger logger = LoggerFactory.getLogger(ConnectionLoadBalanceServerTest.class) + + private static final String KEYSTORE_PATH = "src/test/resources/localhost-ks.jks" + private static final String KEYSTORE_PASSWORD = "OI7kMpWzzVNVx/JGhTL/0uO4+PWpGJ46uZ/pfepbkwI" + private static final KeystoreType KEYSTORE_TYPE = KeystoreType.JKS + + private static final String TRUSTSTORE_PATH = "src/test/resources/localhost-ts.jks" + private static final String TRUSTSTORE_PASSWORD = "wAOR0nQJ2EXvOP0JZ2EaqA/n7W69ILS4sWAHghmIWCc" + private static final KeystoreType TRUSTSTORE_TYPE = KeystoreType.JKS + + private static final String HOSTNAME = "localhost" + private static final int PORT = 54321 + private static final int NUM_THREADS = 1 + private static final int TIMEOUT_MS = 1000 + + private static TlsConfiguration tlsConfiguration + private static SSLContext sslContext + + private ConnectionLoadBalanceServer lbServer + + @BeforeClass + static void setUpOnce() throws Exception { + Security.addProvider(new BouncyCastleProvider()) + + logger.metaClass.methodMissing = { String name, args -> + logger.info("[${name?.toUpperCase()}] ${(args as List).join(" ")}") + } + + tlsConfiguration = new TlsConfiguration(KEYSTORE_PATH, KEYSTORE_PASSWORD, KEYSTORE_TYPE, TRUSTSTORE_PATH, TRUSTSTORE_PASSWORD, TRUSTSTORE_TYPE) + sslContext = SslContextFactory.createSslContext(tlsConfiguration) + } + + @Before + void setUp() { + } + + @After + void tearDown() { + if (lbServer) { + lbServer.stop() + } + } + + /** + * Asserts that the protocol versions in the parameters object are correct. In recent versions of Java, this enforces order as well, but in older versions, it just enforces presence. + * + * @param enabledProtocols the actual protocols, either in {@code String[]} or {@code Collection<String>} form + * @param expectedProtocols the specific protocol versions to be present (ordered as desired) + */ + void assertProtocolVersions(def enabledProtocols, def expectedProtocols) { + if (CertificateUtils.getJavaVersion() > 8) { + assert enabledProtocols == expectedProtocols as String[] + } else { + assert enabledProtocols as Set == expectedProtocols as Set + } + } + + @Test + void testRequestPeerListShouldUseTLS() { + // Arrange + logger.info("Creating SSL Context from TLS Configuration: ${tlsConfiguration}") + SSLContext sslContext = SslContextFactory.createSslContext(tlsConfiguration, SslContextFactory.ClientAuth.NONE) + logger.info("Created SSL Context: ${KeyStoreUtils.sslContextToString(sslContext)}") + + def mockLBP = [ + receiveFlowFiles: { Socket s, InputStream i, OutputStream o -> null } + ] as LoadBalanceProtocol + def mockER = [:] as EventReporter + + lbServer = new ConnectionLoadBalanceServer(HOSTNAME, PORT, sslContext, NUM_THREADS, mockLBP, mockER, TIMEOUT_MS) + + // Act + lbServer.start() + + // Assert + + // Assert that the default parameters (which can't be modified) still have legacy protocols and no client auth + def defaultSSLParameters = sslContext.defaultSSLParameters + logger.info("Default SSL Parameters: ${KeyStoreUtils.sslParametersToString(defaultSSLParameters)}" as String) + assertProtocolVersions(defaultSSLParameters.protocols, ["TLSv1.2", "TLSv1.1", "TLSv1"]) + assert !defaultSSLParameters.needClientAuth + + // Assert that the actual socket is set correctly due to the override in the LB server + SSLServerSocket socket = lbServer.serverSocket as SSLServerSocket + logger.info("Created SSL server socket: ${KeyStoreUtils.sslServerSocketToString(socket)}" as String) + assertProtocolVersions(socket.enabledProtocols, CertificateUtils.getCurrentSupportedTlsProtocolVersions()) + assert socket.needClientAuth + + // Clean up + lbServer.stop() + } + + @Test Review comment: May want to change to IT as GHA can encounter delays which might invalidate the assertions in this test. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected]
