NIFI-11603 Refactored Socket-based tests to use Dynamic Ports

- Removed NetworkUtils methods for getting available ports
- Updated Socket-based components to support using 0 to listen on a random available port for improved test reliability

This closes #7299

Signed-off-by: David Handermann <exceptionfactory@apache.org>
This commit is contained in:
Mark Payne 2023-05-25 10:15:15 -04:00 committed by exceptionfactory
parent b042eb01e8
commit 50811660d0
No known key found for this signature in database
GPG Key ID: 29B6A52D2AAE8DBA
76 changed files with 699 additions and 971 deletions

View File

@ -23,7 +23,6 @@ import org.apache.nifi.bootstrap.notification.NotificationType;
import org.apache.nifi.bootstrap.notification.email.EmailNotificationService;
import org.apache.nifi.components.PropertyDescriptor;
import org.apache.nifi.components.PropertyValue;
import org.apache.nifi.remote.io.socket.NetworkUtils;
import org.apache.nifi.util.MockPropertyValue;
import org.junit.jupiter.api.Test;
@ -103,8 +102,7 @@ public class EmailNotificationServiceTest {
properties.put(EmailNotificationService.SMTP_HOSTNAME, new MockPropertyValue(LOCALHOST_ADDRESS));
final int port = NetworkUtils.getAvailableTcpPort();
properties.put(EmailNotificationService.SMTP_PORT, new MockPropertyValue(Integer.toString(port)));
properties.put(EmailNotificationService.SMTP_PORT, new MockPropertyValue("0"));
properties.put(EmailNotificationService.SMTP_AUTH, new MockPropertyValue(Boolean.FALSE.toString()));
properties.put(EmailNotificationService.FROM, new MockPropertyValue(ADDRESS));
properties.put(EmailNotificationService.TO, new MockPropertyValue(ADDRESS));

View File

@ -33,7 +33,6 @@ import io.netty.handler.codec.Delimiters;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.ssl.SslHandler;
import org.apache.nifi.remote.io.socket.NetworkUtils;
import org.apache.nifi.security.util.SslContextFactory;
import org.apache.nifi.security.util.TemporaryKeyStoreBuilder;
import org.apache.nifi.security.util.TlsConfiguration;
@ -118,8 +117,7 @@ public class SSLSocketChannelTest {
@Test
public void testClientConnectFailed() throws IOException {
final int port = NetworkUtils.getAvailableTcpPort();
final SSLSocketChannel sslSocketChannel = new SSLSocketChannel(sslContext, LOCALHOST, port, null, CLIENT_CHANNEL);
final SSLSocketChannel sslSocketChannel = new SSLSocketChannel(sslContext, "this-host-does-not-exist", 1, null, CLIENT_CHANNEL);
sslSocketChannel.setTimeout(CHANNEL_FAILURE_TIMEOUT);
assertThrows(Exception.class, sslSocketChannel::connect);
}
@ -131,8 +129,8 @@ public class SSLSocketChannelTest {
final EventLoopGroup group = new NioEventLoopGroup(GROUP_THREADS);
try (final SocketChannel socketChannel = SocketChannel.open()) {
final int port = NetworkUtils.getAvailableTcpPort();
startServer(group, port, enabledProtocol, getSingleCountDownLatch());
final Channel serverChannel = startServer(group, enabledProtocol, getSingleCountDownLatch());
final int port = getListeningPort(serverChannel);
socketChannel.connect(new InetSocketAddress(LOCALHOST, port));
final SSLEngine sslEngine = createSslEngine(enabledProtocol, CLIENT_CHANNEL);
@ -183,15 +181,17 @@ public class SSLSocketChannelTest {
}
private void assertServerChannelConnectedReadClosed(final String enabledProtocol) throws IOException, InterruptedException {
final int port = NetworkUtils.getAvailableTcpPort();
final ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
final SocketAddress socketAddress = new InetSocketAddress(LOCALHOST, port);
final SocketAddress socketAddress = new InetSocketAddress(LOCALHOST, 0);
serverSocketChannel.bind(socketAddress);
final Executor executor = Executors.newSingleThreadExecutor();
final EventLoopGroup group = new NioEventLoopGroup(GROUP_THREADS);
try {
final Channel channel = startClient(group, port, enabledProtocol);
final SocketAddress serverLocalAddress = serverSocketChannel.getLocalAddress();
final int listeningPort = (serverLocalAddress instanceof InetSocketAddress) ? ((InetSocketAddress) serverLocalAddress).getPort() : 0;
final Channel channel = startClient(group, listeningPort, enabledProtocol);
try {
final SocketChannel socketChannel = serverSocketChannel.accept();
@ -300,8 +300,8 @@ public class SSLSocketChannelTest {
final EventLoopGroup group = new NioEventLoopGroup(GROUP_THREADS);
try {
final int port = NetworkUtils.getAvailableTcpPort();
startServer(group, port, enabledProtocol, countDownLatch);
final Channel channel = startServer(group, enabledProtocol, countDownLatch);
final int port = getListeningPort(channel);
final SSLSocketChannel sslSocketChannel = new SSLSocketChannel(sslContext, LOCALHOST, port, null, CLIENT_CHANNEL);
sslSocketChannel.setTimeout(CHANNEL_TIMEOUT);
channelConsumer.accept(sslSocketChannel);
@ -310,6 +310,15 @@ public class SSLSocketChannelTest {
}
}
private int getListeningPort(final Channel serverChannel) {
final SocketAddress address = serverChannel.localAddress();
if (address instanceof InetSocketAddress) {
return ((InetSocketAddress) address).getPort();
}
return 0;
}
private Channel startClient(final EventLoopGroup group, final int port, final String enabledProtocol) {
final Bootstrap bootstrap = new Bootstrap();
bootstrap.group(group);
@ -325,7 +334,7 @@ public class SSLSocketChannelTest {
return bootstrap.connect(LOCALHOST, port).syncUninterruptibly().channel();
}
private void startServer(final EventLoopGroup group, final int port, final String enabledProtocol, final CountDownLatch countDownLatch) {
private Channel startServer(final EventLoopGroup group, final String enabledProtocol, final CountDownLatch countDownLatch) {
final ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(group);
bootstrap.channel(NioServerSocketChannel.class);
@ -345,8 +354,9 @@ public class SSLSocketChannelTest {
}
});
final ChannelFuture bindFuture = bootstrap.bind(LOCALHOST, port);
final ChannelFuture bindFuture = bootstrap.bind(LOCALHOST, 0);
bindFuture.syncUninterruptibly();
return bindFuture.channel();
}
private SSLEngine createSslEngine(final String enabledProtocol, final boolean useClientMode) {

View File

@ -37,7 +37,6 @@ import java.text.NumberFormat;
import java.text.ParseException;
import java.time.Instant;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
@ -151,7 +150,7 @@ public class StandardValidators {
}
};
public static final Validator PORT_VALIDATOR = createLongValidator(1, 65535, true);
public static final Validator PORT_VALIDATOR = createLongValidator(0, 65535, true);
/**
* {@link Validator} that ensures that value's length > 0
@ -180,21 +179,25 @@ public class StandardValidators {
* {@link Validator} that ensures that value is a non-empty comma separated list of hostname:port
*/
public static final Validator HOSTNAME_PORT_LIST_VALIDATOR = new Validator() {
private final Validator NON_ZERO_PORT_VALIDATOR = createLongValidator(1, 65535, true);
@Override
public ValidationResult validate(String subject, String input, ValidationContext context) {
// expression language
if (context.isExpressionLanguageSupported(subject) && context.isExpressionLanguagePresent(input)) {
return new ValidationResult.Builder().subject(subject).input(input).explanation("Expression Language Present").valid(true).build();
}
// not empty
ValidationResult nonEmptyValidatorResult = StandardValidators.NON_EMPTY_VALIDATOR.validate(subject, input, context);
final ValidationResult nonEmptyValidatorResult = StandardValidators.NON_EMPTY_VALIDATOR.validate(subject, input, context);
if (!nonEmptyValidatorResult.isValid()) {
return nonEmptyValidatorResult;
}
// check format
final List<String> hostnamePortList = Arrays.asList(input.split(","));
final String[] hostnamePortList = input.split(",");
for (String hostnamePort : hostnamePortList) {
String[] addresses = hostnamePort.split(":");
final String[] addresses = hostnamePort.split(":");
// Protect against invalid input like http://127.0.0.1:9300 (URL scheme should not be there)
if (addresses.length != 2) {
return new ValidationResult.Builder().subject(subject).input(input).explanation(
@ -202,12 +205,13 @@ public class StandardValidators {
}
// Validate the port
String port = addresses[1].trim();
ValidationResult portValidatorResult = StandardValidators.PORT_VALIDATOR.validate(subject, port, context);
final String port = addresses[1].trim();
final ValidationResult portValidatorResult = NON_ZERO_PORT_VALIDATOR.validate(subject, port, context);
if (!portValidatorResult.isValid()) {
return portValidatorResult;
}
}
return new ValidationResult.Builder().subject(subject).input(input).explanation("Valid cluster definition").valid(true).build();
}
};

View File

@ -16,83 +16,12 @@
*/
package org.apache.nifi.remote.io.socket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class NetworkUtils {
/**
* Get Available TCP Port
*
* @return Available TCP Port
*/
public static int availablePort() {
return getAvailableTcpPort();
}
/**
* Get Available TCP Port using ServerSocket
*
* @return Available TCP Port
*/
public static int getAvailableTcpPort() {
try (final ServerSocket socket = new ServerSocket(0)) {
socket.setReuseAddress(true);
return socket.getLocalPort();
} catch (final Exception e) {
throw new IllegalArgumentException("Available TCP Port not found", e);
}
}
/**
* Get Available UDP Port using DatagramSocket
*
* @return Available UDP Port
*/
public static int getAvailableUdpPort() {
try (final DatagramSocket socket = new DatagramSocket()) {
return socket.getLocalPort();
} catch (final Exception e) {
throw new IllegalArgumentException("Available UDP Port not found", e);
}
}
public static boolean isListening(final String hostname, final int port) {
try (final Socket s = new Socket(hostname, port)) {
return s.isConnected();
} catch (final Exception ignore) {}
return false;
}
public static boolean isListening(final String hostname, final int port, final int timeoutMillis) {
Boolean result = false;
final ExecutorService executor = Executors.newSingleThreadExecutor();
try {
result = executor.submit(() -> {
while(!isListening(hostname, port)) {
try {
Thread.sleep(100);
} catch (final Exception ignore) {}
}
return true;
}).get(timeoutMillis, TimeUnit.MILLISECONDS);
} catch (final Exception ignore) {} finally {
try {
executor.shutdown();
} catch (final Exception ignore) {}
}
return (result != null && result);
}
/**
* Get Interface Address using interface name eg. en0, eth0
*
@ -108,4 +37,5 @@ public class NetworkUtils {
}
return interfaceAddress;
}
}

View File

@ -111,7 +111,6 @@ public class ListenBeats extends AbstractProcessor {
private static final Set<Relationship> RELATIONSHIPS = Collections.singleton(REL_SUCCESS);
protected volatile int port;
protected volatile BlockingQueue<BatchMessage> events;
protected volatile BlockingQueue<BatchMessage> errorEvents;
protected volatile EventServer eventServer;
@ -135,7 +134,7 @@ public class ListenBeats extends AbstractProcessor {
final String networkInterface = context.getProperty(ListenerProperties.NETWORK_INTF_NAME).evaluateAttributeExpressions().getValue();
final InetAddress address = NetworkUtils.getInterfaceAddress(networkInterface);
final Charset charset = Charset.forName(context.getProperty(ListenerProperties.CHARSET).getValue());
port = context.getProperty(ListenerProperties.PORT).evaluateAttributeExpressions().asInteger();
final int port = context.getProperty(ListenerProperties.PORT).evaluateAttributeExpressions().asInteger();
events = new LinkedBlockingQueue<>(context.getProperty(ListenerProperties.MAX_MESSAGE_QUEUE_SIZE).asInteger());
errorEvents = new LinkedBlockingQueue<>();
final String msgDemarcator = getMessageDemarcator(context);
@ -165,6 +164,10 @@ public class ListenBeats extends AbstractProcessor {
}
}
public int getListeningPort() {
return eventServer.getListeningPort();
}
@Override
public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException {
EventBatcher<BatchMessage> eventBatcher = getEventBatcher();
@ -207,7 +210,7 @@ public class ListenBeats extends AbstractProcessor {
private String getTransitUri(final FlowFileEventBatch<BatchMessage> batch) {
final List<BatchMessage> events = batch.getEvents();
final String sender = events.get(0).getSender();
return String.format("beats://%s:%d", sender, port);
return String.format("beats://%s:%d", sender, getListeningPort());
}
private Map<String, String> getAttributes(final FlowFileEventBatch<BatchMessage> batch) {
@ -216,7 +219,7 @@ public class ListenBeats extends AbstractProcessor {
final String sender = events.get(0).getSender();
final Map<String, String> attributes = new LinkedHashMap<>();
attributes.put(BeatsAttributes.SENDER.key(), sender);
attributes.put(BeatsAttributes.PORT.key(), String.valueOf(port));
attributes.put(BeatsAttributes.PORT.key(), String.valueOf(getListeningPort()));
attributes.put(CoreAttributes.MIME_TYPE.key(), "application/json");
if (events.size() == 1) {

View File

@ -21,7 +21,6 @@ import org.apache.nifi.processors.beats.protocol.FrameType;
import org.apache.nifi.processors.beats.protocol.ProtocolVersion;
import org.apache.nifi.provenance.ProvenanceEventRecord;
import org.apache.nifi.provenance.ProvenanceEventType;
import org.apache.nifi.remote.io.socket.NetworkUtils;
import org.apache.nifi.util.MockFlowFile;
import org.apache.nifi.util.TestRunner;
import org.apache.nifi.util.TestRunners;
@ -62,23 +61,19 @@ class ListenBeatsTest {
TestRunner runner;
@BeforeEach
void setRunner() {
void createRunner() {
runner = TestRunners.newTestRunner(ListenBeats.class);
}
@Timeout(10)
@Test
void testRunSingleJsonMessage() throws Exception {
final int port = NetworkUtils.getAvailableTcpPort();
runner.setProperty(ListenerProperties.PORT, Integer.toString(port));
final int port = startServer();
startServer();
try (
final Socket socket = new Socket(LOCALHOST, port);
try (final Socket socket = new Socket(LOCALHOST, port);
final InputStream inputStream = socket.getInputStream();
final OutputStream outputStream = socket.getOutputStream()
) {
final OutputStream outputStream = socket.getOutputStream()) {
sendMessage(outputStream, FIRST_SEQUENCE_NUMBER);
assertAckPacketMatched(inputStream, FIRST_SEQUENCE_NUMBER);
}
@ -90,16 +85,12 @@ class ListenBeatsTest {
@Timeout(10)
@Test
void testRunWindowSizeJsonMessages() throws Exception {
final int port = NetworkUtils.getAvailableTcpPort();
runner.setProperty(ListenerProperties.PORT, Integer.toString(port));
final int port = startServer();
startServer();
try (
final Socket socket = new Socket(LOCALHOST, port);
try (final Socket socket = new Socket(LOCALHOST, port);
final InputStream inputStream = socket.getInputStream();
final OutputStream outputStream = socket.getOutputStream()
) {
final OutputStream outputStream = socket.getOutputStream()) {
sendWindowSize(outputStream);
for (int sequenceNumber = FIRST_SEQUENCE_NUMBER; sequenceNumber <= WINDOWED_MESSAGES; sequenceNumber++) {
@ -116,16 +107,12 @@ class ListenBeatsTest {
@Timeout(10)
@Test
void testRunWindowSizeCompressedJsonMessages() throws Exception {
final int port = NetworkUtils.getAvailableTcpPort();
runner.setProperty(ListenerProperties.PORT, Integer.toString(port));
final int port = startServer();
startServer();
try (
final Socket socket = new Socket(LOCALHOST, port);
try (final Socket socket = new Socket(LOCALHOST, port);
final InputStream inputStream = socket.getInputStream();
final OutputStream outputStream = socket.getOutputStream()
) {
final OutputStream outputStream = socket.getOutputStream()) {
sendWindowSize(outputStream);
final ByteArrayOutputStream compressedOutputStream = new ByteArrayOutputStream();
@ -146,8 +133,13 @@ class ListenBeatsTest {
assertReceiveEventFound(port);
}
private void startServer() {
private int startServer() {
runner.setProperty(ListenerProperties.PORT, "0");
runner.run(1, false, true);
final int port = ((ListenBeats) runner.getProcessor()).getListeningPort();
return port;
}
private void assertReceiveEventFound(final int port) {

View File

@ -16,19 +16,6 @@
*/
package org.apache.nifi.processors.email;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import org.apache.nifi.annotation.behavior.InputRequirement;
import org.apache.nifi.annotation.behavior.TriggerSerially;
import org.apache.nifi.annotation.behavior.WritesAttribute;
@ -58,6 +45,20 @@ import org.subethamail.smtp.MessageContext;
import org.subethamail.smtp.MessageHandlerFactory;
import org.subethamail.smtp.server.SMTPServer;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@Tags({"listen", "email", "smtp"})
@TriggerSerially
@InputRequirement(InputRequirement.Requirement.INPUT_FORBIDDEN)
@ -189,6 +190,10 @@ public class ListenSMTP extends AbstractSessionFactoryProcessor {
context.yield();//nothing really to do here since threading managed by smtp server sessions
}
public int getListeningPort() {
return smtp == null ? 0 : smtp.getPort();
}
@OnStopped
public void stop() {
try {

View File

@ -16,7 +16,6 @@
*/
package org.apache.nifi.processors.email;
import org.apache.nifi.remote.io.socket.NetworkUtils;
import org.apache.nifi.reporting.InitializationException;
import org.apache.nifi.security.util.ClientAuth;
import org.apache.nifi.security.util.SslContextFactory;
@ -27,6 +26,7 @@ import org.apache.nifi.ssl.RestrictedSSLContextService;
import org.apache.nifi.ssl.SSLContextService;
import org.apache.nifi.util.TestRunner;
import org.apache.nifi.util.TestRunners;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
@ -37,12 +37,12 @@ import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.net.ssl.SSLContext;
import java.net.Socket;
import java.security.GeneralSecurityException;
import java.util.Properties;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@ -80,11 +80,11 @@ public class TestListenSMTP {
}
@Test
public void testListenSMTP() throws MessagingException {
final int port = NetworkUtils.availablePort();
final TestRunner runner = newTestRunner(port);
public void testListenSMTP() throws MessagingException, InterruptedException {
final TestRunner runner = newTestRunner();
runner.run(1, false);
final int port = ((ListenSMTP) runner.getProcessor()).getListeningPort();
assertPortListening(port);
final Session session = getSession(port);
@ -98,8 +98,7 @@ public class TestListenSMTP {
@Test
public void testListenSMTPwithTLSCurrentVersion() throws Exception {
final int port = NetworkUtils.availablePort();
final TestRunner runner = newTestRunner(port);
final TestRunner runner = newTestRunner();
configureSslContextService(runner);
runner.setProperty(ListenSMTP.SSL_CONTEXT_SERVICE, SSL_SERVICE_IDENTIFIER);
@ -107,6 +106,8 @@ public class TestListenSMTP {
runner.assertValid();
runner.run(1, false);
final int port = ((ListenSMTP) runner.getProcessor()).getListeningPort();
assertPortListening(port);
final Session session = getSessionTls(port, tlsConfiguration.getProtocol());
@ -120,8 +121,7 @@ public class TestListenSMTP {
@Test
public void testListenSMTPwithTLSLegacyProtocolException() throws Exception {
final int port = NetworkUtils.availablePort();
final TestRunner runner = newTestRunner(port);
final TestRunner runner = newTestRunner();
configureSslContextService(runner);
runner.setProperty(ListenSMTP.SSL_CONTEXT_SERVICE, SSL_SERVICE_IDENTIFIER);
@ -129,6 +129,8 @@ public class TestListenSMTP {
runner.assertValid();
runner.run(1, false);
final int port = ((ListenSMTP) runner.getProcessor()).getListeningPort();
assertPortListening(port);
final Session session = getSessionTls(port, TlsConfiguration.TLS_1_0_PROTOCOL);
@ -140,12 +142,14 @@ public class TestListenSMTP {
}
@Test
public void testListenSMTPwithTooLargeMessage() {
final int port = NetworkUtils.availablePort();
final TestRunner runner = newTestRunner(port);
public void testListenSMTPwithTooLargeMessage() throws InterruptedException {
final TestRunner runner = newTestRunner();
runner.setProperty(ListenSMTP.SMTP_MAXIMUM_MSG_SIZE, "10 B");
runner.run(1, false);
final int port = ((ListenSMTP) runner.getProcessor()).getListeningPort();
assertPortListening(port);
final Session session = getSession(port);
@ -155,17 +159,28 @@ public class TestListenSMTP {
runner.assertAllFlowFilesTransferred(ListenSMTP.REL_SUCCESS, 0);
}
private TestRunner newTestRunner(final int port) {
private TestRunner newTestRunner() {
final ListenSMTP processor = new ListenSMTP();
final TestRunner runner = TestRunners.newTestRunner(processor);
runner.setProperty(ListenSMTP.SMTP_PORT, String.valueOf(port));
runner.setProperty(ListenSMTP.SMTP_PORT, "0");
runner.setProperty(ListenSMTP.SMTP_MAXIMUM_CONNECTIONS, "3");
return runner;
}
private void assertPortListening(final int port) {
assertTrue(NetworkUtils.isListening("localhost", port, 5000), String.format("expected server listening on %s:%d", "localhost", port));
private void assertPortListening(final int port) throws InterruptedException {
final long endTime = System.currentTimeMillis() + 5_000L;
while (System.currentTimeMillis() <= endTime) {
try (final Socket socket = new Socket("localhost", port)) {
if (socket.isConnected()) {
return;
}
} catch (final Exception e) {
Thread.sleep(10L);
}
}
Assertions.fail(String.format("expected server listening on %s:%d", "localhost", port));
}
private Session getSession(final int port) {

View File

@ -102,15 +102,6 @@ public abstract class AbstractListenEventProcessor<E extends Event> extends Abst
// Putting these properties here so sub-classes don't have to redefine them, but they are
// not added to the properties by default since not all processors may need them
public static final PropertyDescriptor MAX_CONNECTIONS = new PropertyDescriptor.Builder()
.name("Max Number of TCP Connections")
.description("The maximum number of concurrent TCP connections to accept.")
.addValidator(StandardValidators.createLongValidator(1, 65535, true))
.defaultValue("2")
.required(true)
.build();
public static final Relationship REL_SUCCESS = new Relationship.Builder()
.name("success")
.description("Messages received successfully will be sent out this relationship.")
@ -155,7 +146,7 @@ public abstract class AbstractListenEventProcessor<E extends Event> extends Abst
* @return a list of relationships
*/
protected List<Relationship> getAdditionalRelationships() {
return Collections.EMPTY_LIST;
return Collections.emptyList();
}
/**
@ -164,7 +155,7 @@ public abstract class AbstractListenEventProcessor<E extends Event> extends Abst
* @return a list of properties
*/
protected List<PropertyDescriptor> getAdditionalProperties() {
return Collections.EMPTY_LIST;
return Collections.emptyList();
}
@Override
@ -180,7 +171,7 @@ public abstract class AbstractListenEventProcessor<E extends Event> extends Abst
@OnScheduled
public void onScheduled(final ProcessContext context) throws IOException {
charset = Charset.forName(context.getProperty(CHARSET).getValue());
port = context.getProperty(PORT).evaluateAttributeExpressions().asInteger();
final int specifiedPort = context.getProperty(PORT).evaluateAttributeExpressions().asInteger();
eventsCapacity = context.getProperty(MAX_MESSAGE_QUEUE_SIZE).asInteger();
events = new TrackingLinkedBlockingQueue<>(eventsCapacity);
final String interfaceName = context.getProperty(NETWORK_INTF_NAME).evaluateAttributeExpressions().getValue();
@ -189,7 +180,8 @@ public abstract class AbstractListenEventProcessor<E extends Event> extends Abst
final int maxChannelBufferSize = context.getProperty(MAX_SOCKET_BUFFER_SIZE).asDataSize(DataUnit.B).intValue();
// create the dispatcher and call open() to bind to the given port
dispatcher = createDispatcher(context, events);
dispatcher.open(interfaceAddress, port, maxChannelBufferSize);
dispatcher.open(interfaceAddress, specifiedPort, maxChannelBufferSize);
port = dispatcher.getPort();
// start a thread to run the dispatcher
final Thread readerThread = new Thread(dispatcher);
@ -198,6 +190,10 @@ public abstract class AbstractListenEventProcessor<E extends Event> extends Abst
readerThread.start();
}
public int getListeningPort() {
return port;
}
/**
* @param context the ProcessContext to retrieve property values from
* @return a ChannelDispatcher to handle incoming connections

View File

@ -25,11 +25,10 @@ import org.apache.nifi.processor.Processor;
import org.apache.nifi.processor.exception.ProcessException;
import org.apache.nifi.processor.util.listen.event.EventFactoryUtil;
import org.apache.nifi.processor.util.listen.event.StandardNetworkEventFactory;
import org.apache.nifi.remote.io.socket.NetworkUtils;
import org.apache.nifi.util.MockProcessSession;
import org.apache.nifi.util.SharedSessionState;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import java.net.InetSocketAddress;
@ -72,8 +71,8 @@ public class EventBatcherTest {
@Test
public void testGetBatches() throws InterruptedException {
String sender1 = new InetSocketAddress(NetworkUtils.getAvailableTcpPort()).toString();
String sender2 = new InetSocketAddress(NetworkUtils.getAvailableTcpPort()).toString();
String sender1 = new InetSocketAddress(0).toString();
String sender2 = new InetSocketAddress(2).toString();
final Map<String, String> sender1Metadata = EventFactoryUtil.createMapWithSender(sender1);
final Map<String, String> sender2Metadata = EventFactoryUtil.createMapWithSender(sender2);
events.put(eventFactory.create(MESSAGE_DATA_1.getBytes(StandardCharsets.UTF_8), sender1Metadata));

View File

@ -25,4 +25,10 @@ public interface EventServer {
* Shutdown Event Server and close resources
*/
void shutdown();
/**
* Returns the port that the server is listening on, if available. If unable to determine the port, will return 0.
* @return the port that the server is listening on, or 0 if unable to determine the port
*/
int getListeningPort();
}

View File

@ -24,6 +24,8 @@ import org.apache.nifi.event.transport.EventServer;
import org.apache.nifi.event.transport.configuration.ShutdownQuietPeriod;
import org.apache.nifi.event.transport.configuration.ShutdownTimeout;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
@ -83,4 +85,14 @@ class NettyEventServer implements EventServer {
.awaitUninterruptibly(shutdownTimeout.toMillis(), TimeUnit.MILLISECONDS);
}
}
@Override
public int getListeningPort() {
final SocketAddress socketAddress = channel.localAddress();
if (socketAddress instanceof InetSocketAddress) {
return ((InetSocketAddress) socketAddress).getPort();
}
return 0;
}
}

View File

@ -23,7 +23,6 @@ import org.apache.nifi.event.transport.EventSender;
import org.apache.nifi.event.transport.configuration.ShutdownQuietPeriod;
import org.apache.nifi.event.transport.configuration.ShutdownTimeout;
import org.apache.nifi.event.transport.configuration.TransportProtocol;
import org.apache.nifi.remote.io.socket.NetworkUtils;
import org.junit.jupiter.api.Test;
import javax.net.ssl.SSLContext;
@ -40,8 +39,7 @@ public class NettyEventSenderFactoryTest {
@Test
public void testSendEventTcpException() throws Exception {
final int port = NetworkUtils.getAvailableTcpPort();
final NettyEventSenderFactory<ByteBuf> factory = new NettyEventSenderFactory<>(ADDRESS, port, TransportProtocol.TCP);
final NettyEventSenderFactory<ByteBuf> factory = new NettyEventSenderFactory<>(ADDRESS, 0, TransportProtocol.TCP);
factory.setTimeout(DEFAULT_TIMEOUT);
factory.setWorkerThreads(SINGLE_THREAD);
factory.setShutdownQuietPeriod(ShutdownQuietPeriod.QUICK.getDuration());
@ -56,8 +54,7 @@ public class NettyEventSenderFactoryTest {
@Test
public void testSendEventCloseUdp() throws Exception {
final int port = NetworkUtils.getAvailableUdpPort();
final NettyEventSenderFactory<ByteBuf> factory = new NettyEventSenderFactory<>(ADDRESS, port, TransportProtocol.UDP);
final NettyEventSenderFactory<ByteBuf> factory = new NettyEventSenderFactory<>(ADDRESS, 29102, TransportProtocol.UDP);
factory.setTimeout(DEFAULT_TIMEOUT);
factory.setWorkerThreads(SINGLE_THREAD);
factory.setShutdownQuietPeriod(ShutdownQuietPeriod.QUICK.getDuration());

View File

@ -19,7 +19,6 @@ package org.apache.nifi.event.transport.netty;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.pool.ChannelPool;
import io.netty.util.concurrent.Future;
import org.apache.nifi.remote.io.socket.NetworkUtils;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
@ -49,7 +48,7 @@ public class NettyEventSenderTest {
@Test
public void testClose() {
final SocketAddress socketAddress = InetSocketAddress.createUnresolved(LOCALHOST, NetworkUtils.getAvailableTcpPort());
final SocketAddress socketAddress = InetSocketAddress.createUnresolved(LOCALHOST, 0);
final NettyEventSender<?> sender = new NettyEventSender<>(group, channelPool, socketAddress, false);
doReturn(shutdownFuture).when(group).shutdownGracefully(anyLong(), anyLong(), eq(TimeUnit.MILLISECONDS));
sender.close();

View File

@ -26,7 +26,6 @@ import org.apache.nifi.event.transport.configuration.ShutdownTimeout;
import org.apache.nifi.event.transport.configuration.TransportProtocol;
import org.apache.nifi.event.transport.message.ByteArrayMessage;
import org.apache.nifi.logging.ComponentLog;
import org.apache.nifi.remote.io.socket.NetworkUtils;
import org.apache.nifi.security.util.ClientAuth;
import org.apache.nifi.security.util.SslContextFactory;
import org.apache.nifi.security.util.TemporaryKeyStoreBuilder;
@ -94,12 +93,10 @@ public class StringNettyEventSenderFactoryTest {
@Test
public void testSendEventTcpEventException() throws Exception {
final int port = NetworkUtils.getAvailableTcpPort();
final BlockingQueue<ByteArrayMessage> messages = new LinkedBlockingQueue<>();
final NettyEventServerFactory serverFactory = getEventServerFactory(port, messages);
final NettyEventServerFactory serverFactory = getEventServerFactory(0, messages);
final EventServer eventServer = serverFactory.getEventServer();
final NettyEventSenderFactory<String> senderFactory = getEventSenderFactory(port);
final NettyEventSenderFactory<String> senderFactory = getEventSenderFactory(eventServer.getListeningPort());
try (final EventSender<String> sender = senderFactory.getEventSender()) {
eventServer.shutdown();
assertThrows(EventException.class, () -> sender.sendEvent(MESSAGE));
@ -110,12 +107,10 @@ public class StringNettyEventSenderFactoryTest {
@Test
public void testSendEventTcp() throws Exception {
final int port = NetworkUtils.getAvailableTcpPort();
final BlockingQueue<ByteArrayMessage> messages = new LinkedBlockingQueue<>();
final NettyEventServerFactory serverFactory = getEventServerFactory(port, messages);
final NettyEventServerFactory serverFactory = getEventServerFactory(0, messages);
final EventServer eventServer = serverFactory.getEventServer();
final NettyEventSenderFactory<String> senderFactory = getEventSenderFactory(port);
final NettyEventSenderFactory<String> senderFactory = getEventSenderFactory(eventServer.getListeningPort());
try (final EventSender<String> sender = senderFactory.getEventSender()) {
sender.sendEvent(MESSAGE);
} finally {
@ -127,12 +122,10 @@ public class StringNettyEventSenderFactoryTest {
@Test
public void testSendEventTcpEventDropped() throws Exception {
final int port = NetworkUtils.getAvailableTcpPort();
final BlockingQueue<ByteArrayMessage> messages = new LinkedBlockingQueue<>(SINGLE_MESSAGE);
final NettyEventServerFactory serverFactory = getEventServerFactory(port, messages);
final NettyEventServerFactory serverFactory = getEventServerFactory(0, messages);
final EventServer eventServer = serverFactory.getEventServer();
final NettyEventSenderFactory<String> senderFactory = getEventSenderFactory(port);
final NettyEventSenderFactory<String> senderFactory = getEventSenderFactory(eventServer.getListeningPort());
try (final EventSender<String> sender = senderFactory.getEventSender()) {
sender.sendEvent(MESSAGE);
assertMessageMatched(messages.take());
@ -155,18 +148,17 @@ public class StringNettyEventSenderFactoryTest {
@Test
public void testSendEventTcpSslContextConfigured() throws Exception {
final int port = NetworkUtils.getAvailableTcpPort();
final NettyEventSenderFactory<String> senderFactory = getEventSenderFactory(port);
final SSLContext sslContext = getSslContext();
senderFactory.setSslContext(sslContext);
final BlockingQueue<ByteArrayMessage> messages = new LinkedBlockingQueue<>();
final NettyEventServerFactory serverFactory = getEventServerFactory(port, messages);
final NettyEventServerFactory serverFactory = getEventServerFactory(0, messages);
serverFactory.setSslContext(sslContext);
serverFactory.setClientAuth(ClientAuth.NONE);
final EventServer eventServer = serverFactory.getEventServer();
final NettyEventSenderFactory<String> senderFactory = getEventSenderFactory(eventServer.getListeningPort());
senderFactory.setSslContext(sslContext);
try (final EventSender<String> eventSender = senderFactory.getEventSender()) {
eventSender.sendEvent(MESSAGE);
} finally {

View File

@ -29,7 +29,6 @@ import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.ssl.SslHandler;
import org.apache.nifi.remote.io.socket.NetworkUtils;
import org.apache.nifi.security.util.SslContextFactory;
import org.apache.nifi.security.util.TemporaryKeyStoreBuilder;
import org.apache.nifi.security.util.TlsConfiguration;
@ -45,6 +44,7 @@ import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
@ -163,13 +163,15 @@ public class TestPeerChannel {
final Socket socket = socketChannel.socket();
socket.setSoTimeout(SOCKET_TIMEOUT);
final InetSocketAddress serverSocketAddress = getServerSocketAddress();
startServer(group, serverSocketAddress.getPort(), enabledProtocol);
final InetSocketAddress serverSocketAddress = new InetSocketAddress(LOCALHOST, 0);
final Channel serverChannel = startServer(group, serverSocketAddress.getPort(), enabledProtocol);
final int port = getLocalPort(serverChannel);
socketChannel.connect(serverSocketAddress);
final InetSocketAddress clientSocketAddress = new InetSocketAddress(LOCALHOST, port);
socketChannel.connect(clientSocketAddress);
final SSLEngine sslEngine = createSslEngine(enabledProtocol, CLIENT_CHANNEL);
final PeerChannel peerChannel = new PeerChannel(socketChannel, sslEngine, serverSocketAddress.toString());
final PeerChannel peerChannel = new PeerChannel(socketChannel, sslEngine, clientSocketAddress.toString());
assertConnectedOpen(peerChannel);
socketChannel.configureBlocking(false);
@ -182,6 +184,14 @@ public class TestPeerChannel {
}
}
private int getLocalPort(final Channel serverChannel) {
final SocketAddress address = serverChannel.localAddress();
if (address instanceof InetSocketAddress) {
return ((InetSocketAddress) address).getPort();
}
return 0;
}
private void assertConnectedOpen(final PeerChannel peerChannel) {
assertTrue(peerChannel.isConnected(), "Channel not connected");
assertTrue(peerChannel.isOpen(), "Channel not open");
@ -192,7 +202,7 @@ public class TestPeerChannel {
assertFalse(peerChannel.isOpen(), "Channel open");
}
private void startServer(final EventLoopGroup group, final int port, final String enabledProtocol) {
private Channel startServer(final EventLoopGroup group, final int port, final String enabledProtocol) {
final ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(group);
bootstrap.channel(NioServerSocketChannel.class);
@ -225,6 +235,7 @@ public class TestPeerChannel {
final ChannelFuture bindFuture = bootstrap.bind(LOCALHOST, port);
bindFuture.syncUninterruptibly();
return bindFuture.channel();
}
private SSLEngine createSslEngine(final String enabledProtocol, final boolean useClientMode) {
@ -243,11 +254,6 @@ public class TestPeerChannel {
group.shutdownGracefully(SHUTDOWN_TIMEOUT, SHUTDOWN_TIMEOUT, TimeUnit.MILLISECONDS).syncUninterruptibly();
}
private InetSocketAddress getServerSocketAddress() {
final int port = NetworkUtils.getAvailableTcpPort();
return new InetSocketAddress(LOCALHOST, port);
}
private String getEnabledProtocol() {
return isTls13Supported() ? TLS_1_3 : TLS_1_2;
}

View File

@ -17,7 +17,6 @@
package org.apache.nifi.controller.queue.clustered.server;
import org.apache.nifi.events.EventReporter;
import org.apache.nifi.remote.io.socket.NetworkUtils;
import org.apache.nifi.security.util.SslContextFactory;
import org.apache.nifi.security.util.TemporaryKeyStoreBuilder;
import org.apache.nifi.security.util.TlsConfiguration;
@ -112,10 +111,9 @@ class ConnectionLoadBalanceServerTest {
}
private ConnectionLoadBalanceServer getServer(final LoadBalanceProtocol loadBalanceProtocol) {
final int port = NetworkUtils.getAvailableTcpPort();
return new ConnectionLoadBalanceServer(
LOCALHOST,
port,
0,
sslContext,
SERVER_THREADS,
loadBalanceProtocol,

View File

@ -207,6 +207,10 @@ public class ListenGRPC extends AbstractSessionFactoryProcessor {
this.server = serverBuilder.build().start();
}
public int getListeningPort() {
return server == null ? 0 : server.getPort();
}
@OnStopped
public void stopServer(final ProcessContext context) {
if (this.server != null) {

View File

@ -16,7 +16,6 @@
*/
package org.apache.nifi.processors.grpc;
import org.apache.nifi.remote.io.socket.NetworkUtils;
import org.apache.nifi.reporting.InitializationException;
import org.apache.nifi.security.util.SslContextFactory;
import org.apache.nifi.security.util.TemporaryKeyStoreBuilder;
@ -75,21 +74,18 @@ class TestListenGRPC {
@Test
void testRunSocketListening() throws IOException {
final int port = NetworkUtils.getAvailableTcpPort();
runner.setProperty(ListenGRPC.PROP_SERVICE_PORT, Integer.toString(port));
runner.setProperty(ListenGRPC.PROP_SERVICE_PORT, "0");
runner.assertValid();
runner.run(1, false);
final int port = ((ListenGRPC) runner.getProcessor()).getListeningPort();
assertSocketConnected(port, SocketFactory.getDefault());
}
@Test
void testRunSocketListeningSslContextService() throws IOException, InitializationException {
final int port = NetworkUtils.getAvailableTcpPort();
runner.setProperty(ListenGRPC.PROP_SERVICE_PORT, Integer.toString(port));
runner.setProperty(ListenGRPC.PROP_SERVICE_PORT, "0");
when(sslContextService.getIdentifier()).thenReturn(SSL_SERVICE_ID);
when(sslContextService.createTlsConfiguration()).thenReturn(tlsConfiguration);
@ -105,6 +101,7 @@ class TestListenGRPC {
runner.run(1, false);
final int port = ((ListenGRPC) runner.getProcessor()).getListeningPort();
assertSocketConnectedProtocolNegotiated(port, sslContext.getSocketFactory());
}

View File

@ -44,7 +44,7 @@ public abstract class AbstractHazelcastCacheManagerTest {
testRunner.shutdown();
}
protected void givenHazelcastMapCacheClient() throws Exception {
protected void setupHazelcastMapCacheClient() throws Exception {
hazelcastMapCacheClient = new HazelcastMapCacheClient();
testRunner.addControllerService("hazelcast-map-cache-client", hazelcastMapCacheClient);
@ -55,7 +55,7 @@ public abstract class AbstractHazelcastCacheManagerTest {
testRunner.setProperty(TestHazelcastProcessor.TEST_HAZELCAST_MAP_CACHE_CLIENT, "hazelcast-map-cache-client");
}
protected void givenServicesAreEnabled() {
protected void enableServices() {
testRunner.enableControllerService(testSubject);
assertTrue(testSubject.isEnabled());
@ -63,12 +63,12 @@ public abstract class AbstractHazelcastCacheManagerTest {
assertTrue(hazelcastMapCacheClient.isEnabled());
}
protected void whenExecuting() {
protected void triggerProcessor() {
testRunner.enqueue("trigger");
testRunner.run();
}
protected void thenProcessingIsSuccessful() {
protected void assertSuccessfulTransfer() {
testRunner.assertAllFlowFilesTransferred(TestHazelcastProcessor.REL_SUCCESS, 1);
}
}

View File

@ -25,11 +25,11 @@ public class EmbeddedHazelcastCacheManagerTest extends AbstractHazelcastCacheMan
testSubject = new EmbeddedHazelcastCacheManager();
testRunner.addControllerService("hazelcast-connection-service", testSubject);
givenHazelcastMapCacheClient();
givenServicesAreEnabled();
setupHazelcastMapCacheClient();
enableServices();
whenExecuting();
triggerProcessor();
thenProcessingIsSuccessful();
assertSuccessfulTransfer();
}
}

View File

@ -19,21 +19,23 @@ package org.apache.nifi.hazelcast.services.cachemanager;
import com.hazelcast.config.Config;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import org.apache.nifi.remote.io.socket.NetworkUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class ExternalHazelcastCacheManagerTest extends AbstractHazelcastCacheManagerTest {
private HazelcastInstance hazelcastInstance;
private int port;
@BeforeEach
public void setUp() {
port = NetworkUtils.availablePort();
final Config config = new Config();
config.getNetworkConfig().setPort(port);
config.getNetworkConfig().setPort(0);
config.setClusterName("nifi");
hazelcastInstance = Hazelcast.newHazelcastInstance(config);
@ -50,13 +52,17 @@ public class ExternalHazelcastCacheManagerTest extends AbstractHazelcastCacheMan
public void testExecution() throws Exception {
testSubject = new ExternalHazelcastCacheManager();
testRunner.addControllerService("hazelcast-connection-service", testSubject);
testRunner.setProperty(testSubject, ExternalHazelcastCacheManager.HAZELCAST_SERVER_ADDRESS, String.format("localhost:%d", port));
givenHazelcastMapCacheClient();
givenServicesAreEnabled();
final SocketAddress localAddress = hazelcastInstance.getLocalEndpoint().getSocketAddress();
assertTrue(localAddress instanceof InetSocketAddress);
final int port = ((InetSocketAddress) localAddress).getPort();
testRunner.setProperty(testSubject, ExternalHazelcastCacheManager.HAZELCAST_SERVER_ADDRESS, "localhost:" + port);
whenExecuting();
setupHazelcastMapCacheClient();
enableServices();
thenProcessingIsSuccessful();
triggerProcessor();
assertSuccessfulTransfer();
}
}

View File

@ -16,7 +16,6 @@
*/
package org.apache.nifi.ldap.tenants;
import org.apache.nifi.parameter.ParameterLookup;
import org.apache.nifi.attribute.expression.language.StandardPropertyValue;
import org.apache.nifi.authorization.AuthorizerConfigurationContext;
import org.apache.nifi.authorization.Group;
@ -25,7 +24,7 @@ import org.apache.nifi.authorization.UserGroupProviderInitializationContext;
import org.apache.nifi.authorization.exception.AuthorizerCreationException;
import org.apache.nifi.ldap.LdapAuthenticationStrategy;
import org.apache.nifi.ldap.ReferralStrategy;
import org.apache.nifi.remote.io.socket.NetworkUtils;
import org.apache.nifi.parameter.ParameterLookup;
import org.apache.nifi.util.NiFiProperties;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
@ -83,9 +82,9 @@ public class LdapUserGroupProviderTest {
public void setup() {
server = new UnboundIdContainer("o=nifi", "classpath:nifi-example.ldif");
server.setApplicationContext(new GenericApplicationContext());
serverPort = NetworkUtils.availablePort();
server.setPort(serverPort);
server.setPort(0);
server.afterPropertiesSet();
serverPort = server.getPort();
final UserGroupProviderInitializationContext initializationContext = mock(UserGroupProviderInitializationContext.class);
when(initializationContext.getIdentifier()).thenReturn("identifier");

View File

@ -30,7 +30,8 @@ public class SNMPManagerFactory {
final int port = configuration.getManagerPort();
final Snmp snmpManager;
try {
snmpManager = new Snmp(new DefaultUdpTransportMapping(new UdpAddress(port)));
final DefaultUdpTransportMapping transportMapping = new DefaultUdpTransportMapping(new UdpAddress(port));
snmpManager = new Snmp(transportMapping);
snmpManager.listen();
} catch (IOException e) {
throw new ProcessException(e);

View File

@ -24,16 +24,20 @@ import org.apache.nifi.snmp.factory.core.SNMPManagerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.mp.MPv3;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.security.SecurityModels;
import org.snmp4j.security.SecurityProtocols;
import org.snmp4j.security.USM;
import org.snmp4j.security.UsmUser;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.Integer32;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.TransportIpAddress;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
public class SNMPTrapReceiverHandler {
@ -51,6 +55,19 @@ public class SNMPTrapReceiverHandler {
snmpManager = new SNMPManagerFactory().createSnmpManagerInstance(configuration);
}
public int getListeningPort() {
final Collection<TransportMapping> transportMappings = snmpManager.getMessageDispatcher().getTransportMappings();
if (transportMappings == null || transportMappings.isEmpty()) {
return 0;
}
final Address address = transportMappings.iterator().next().getListenAddress();
if (address instanceof TransportIpAddress) {
return ((org.snmp4j.smi.TransportIpAddress) address).getPort();
}
return 0;
}
public void createTrapReceiver(final ProcessSessionFactory processSessionFactory, final ComponentLog logger) {
addUsmUsers();
SNMPTrapReceiver trapReceiver = new SNMPTrapReceiver(processSessionFactory, logger);

View File

@ -206,6 +206,14 @@ public class ListenTrapSNMP extends AbstractSessionFactoryProcessor implements V
snmpTrapReceiverHandler = new SNMPTrapReceiverHandler(configuration, usmUsers);
}
public int getListeningPort() {
if (snmpTrapReceiverHandler == null || !snmpTrapReceiverHandler.isStarted()) {
return 0;
}
return snmpTrapReceiverHandler.getListeningPort();
}
@Override
public void onTrigger(final ProcessContext context, final ProcessSessionFactory processSessionFactory) {
if (!snmpTrapReceiverHandler.isStarted()) {

View File

@ -16,7 +16,6 @@
*/
package org.apache.nifi.snmp.configuration;
import org.apache.nifi.remote.io.socket.NetworkUtils;
import org.junit.jupiter.api.Test;
import org.snmp4j.mp.SnmpConstants;
@ -32,7 +31,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
class SNMPConfigurationTest {
private static final int MANAGER_PORT = NetworkUtils.getAvailableUdpPort();
private static final int MANAGER_PORT = 0;
private static final String TARGET_PORT = "55556";
private static final int RETRIES = 3;
private static final int VERSION = SnmpConstants.version3;

View File

@ -16,18 +16,18 @@
*/
package org.apache.nifi.snmp.factory.core;
import org.apache.nifi.remote.io.socket.NetworkUtils;
import org.apache.nifi.snmp.configuration.SNMPConfiguration;
import org.snmp4j.security.SecurityLevel;
import java.net.BindException;
import java.util.function.Function;
import static org.apache.nifi.snmp.helper.configurations.SNMPV3ConfigurationFactory.SECURITY_NAME;
import static org.apache.nifi.snmp.helper.configurations.SNMPConfigurationFactory.LOCALHOST;
import static org.apache.nifi.snmp.helper.configurations.SNMPV3ConfigurationFactory.AUTH_PASSPHRASE;
import static org.apache.nifi.snmp.helper.configurations.SNMPV3ConfigurationFactory.AUTH_PROTOCOL;
import static org.apache.nifi.snmp.helper.configurations.SNMPV3ConfigurationFactory.PRIV_PASSPHRASE;
import static org.apache.nifi.snmp.helper.configurations.SNMPV3ConfigurationFactory.PRIV_PROTOCOL;
import static org.apache.nifi.snmp.helper.configurations.SNMPV3ConfigurationFactory.SECURITY_NAME;
public class SNMPSocketSupport {
@ -52,7 +52,7 @@ public class SNMPSocketSupport {
int attempts = 0;
while (attempts < retries) {
try {
return runnable.apply(getSnmpConfiguration(NetworkUtils.getAvailableUdpPort(), String.valueOf(NetworkUtils.getAvailableUdpPort())));
return runnable.apply(getSnmpConfiguration(0, "0"));
} catch (Exception e) {
if (isBindException(e)) {
attempts++;

View File

@ -16,7 +16,6 @@
*/
package org.apache.nifi.snmp.factory.core;
import org.apache.nifi.remote.io.socket.NetworkUtils;
import org.apache.nifi.snmp.configuration.SNMPConfiguration;
import org.apache.nifi.util.StringUtils;
import org.junit.jupiter.api.Test;
@ -28,8 +27,8 @@ import org.snmp4j.security.SecurityLevel;
import static org.apache.nifi.snmp.helper.configurations.SNMPConfigurationFactory.LOCALHOST;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
@ -60,9 +59,7 @@ class V1V2cSNMPFactoryTest extends SNMPSocketSupport {
@Test
void testFactoryCreatesResourceHandler() {
final V1V2cSNMPFactory snmpFactory = spy(V1V2cSNMPFactory.class);
final int managerPort = NetworkUtils.getAvailableUdpPort();
final String targetPort = String.valueOf(NetworkUtils.getAvailableUdpPort());
final SNMPConfiguration snmpConfiguration = getSnmpConfiguration(managerPort, targetPort);
final SNMPConfiguration snmpConfiguration = getSnmpConfiguration(0, "48");
snmpFactory.createSNMPResourceHandler(snmpConfiguration);

View File

@ -16,7 +16,6 @@
*/
package org.apache.nifi.snmp.factory.core;
import org.apache.nifi.remote.io.socket.NetworkUtils;
import org.apache.nifi.snmp.configuration.SNMPConfiguration;
import org.junit.jupiter.api.Test;
import org.snmp4j.Snmp;
@ -64,9 +63,7 @@ class V3SNMPFactoryTest extends SNMPSocketSupport {
@Test
void testFactoryCreatesResourceHandler() {
final V3SNMPFactory snmpFactory = spy(V3SNMPFactory.class);
final int managerPort = NetworkUtils.getAvailableUdpPort();
final String targetPort = String.valueOf(NetworkUtils.getAvailableUdpPort());
final SNMPConfiguration snmpConfiguration = getSnmpConfiguration(managerPort, targetPort);
final SNMPConfiguration snmpConfiguration = getSnmpConfiguration(0, "48");
snmpFactory.createSNMPResourceHandler(snmpConfiguration);
verify(snmpFactory).createTargetInstance(snmpConfiguration);

View File

@ -17,7 +17,6 @@
package org.apache.nifi.snmp.operations;
import org.apache.nifi.processor.ProcessSessionFactory;
import org.apache.nifi.remote.io.socket.NetworkUtils;
import org.apache.nifi.snmp.configuration.SNMPConfiguration;
import org.apache.nifi.snmp.utils.JsonFileUsmReader;
import org.apache.nifi.util.MockComponentLog;
@ -51,16 +50,14 @@ class SNMPTrapReceiverHandlerTest {
final ProcessSessionFactory mockProcessSessionFactory = mock(ProcessSessionFactory.class);
final MockComponentLog mockComponentLog = new MockComponentLog("componentId", new Object());
final Snmp mockSnmpManager = mock(Snmp.class);
when(snmpConfiguration.getManagerPort()).thenReturn(NetworkUtils.getAvailableUdpPort());
when(snmpConfiguration.getManagerPort()).thenReturn(0);
when(snmpConfiguration.getVersion()).thenReturn(SnmpConstants.version1);
final SNMPTrapReceiverHandler trapReceiverHandler = new SNMPTrapReceiverHandler(snmpConfiguration, null);
trapReceiverHandler.setSnmpManager(mockSnmpManager);
trapReceiverHandler.createTrapReceiver(mockProcessSessionFactory, mockComponentLog);
verify(mockSnmpManager).addCommandResponder(any(SNMPTrapReceiver.class));
assertTrue(trapReceiverHandler.isStarted());
}
@ -73,7 +70,7 @@ class SNMPTrapReceiverHandlerTest {
final Snmp mockSnmpManager = mock(Snmp.class);
when(mockSnmpManager.getUSM()).thenReturn(mockUsm);
when(snmpConfiguration.getManagerPort()).thenReturn(NetworkUtils.getAvailableUdpPort());
when(snmpConfiguration.getManagerPort()).thenReturn(0);
when(snmpConfiguration.getVersion()).thenReturn(SnmpConstants.version1);
final SNMPTrapReceiverHandler trapReceiverHandler = new SNMPTrapReceiverHandler(snmpConfiguration, null);
@ -92,7 +89,7 @@ class SNMPTrapReceiverHandlerTest {
final List<UsmUser> usmUsers = new JsonFileUsmReader(USERS_JSON).readUsm();
final SNMPConfiguration snmpConfiguration = SNMPConfiguration.builder()
.setManagerPort(NetworkUtils.getAvailableUdpPort())
.setManagerPort(0)
.setVersion(SnmpConstants.version3)
.build();

View File

@ -16,7 +16,6 @@
*/
package org.apache.nifi.snmp.processors;
import org.apache.nifi.remote.io.socket.NetworkUtils;
import org.apache.nifi.snmp.dto.SNMPSingleResponse;
import org.apache.nifi.snmp.dto.SNMPValue;
import org.apache.nifi.snmp.helper.testrunners.SNMPV1TestRunnerFactory;
@ -53,7 +52,7 @@ class AbstractSNMPProcessorTest {
@BeforeEach
public void init() {
getTestRunner = new SNMPV1TestRunnerFactory().createSnmpGetTestRunner(NetworkUtils.getAvailableUdpPort(), TEST_OID, "GET");
getTestRunner = new SNMPV1TestRunnerFactory().createSnmpGetTestRunner(0, TEST_OID, "GET");
getSNMP = (GetSNMP) getTestRunner.getProcessor();
mockProcessContext = new MockProcessContext(getSNMP);
mockProcessSession = new MockProcessSession(new SharedSessionState(getSNMP, new AtomicLong(0L)), getSNMP);

View File

@ -16,7 +16,6 @@
*/
package org.apache.nifi.snmp.processors;
import org.apache.nifi.remote.io.socket.NetworkUtils;
import org.apache.nifi.snmp.helper.testrunners.SNMPV1TestRunnerFactory;
import org.apache.nifi.util.MockProcessSession;
import org.apache.nifi.util.SharedSessionState;
@ -36,7 +35,7 @@ class GetSNMPTest {
@Test
void testOnTriggerWithGetStrategyPerformsSnmpGet() {
final TestRunner getSnmpTestRunner = new SNMPV1TestRunnerFactory().createSnmpGetTestRunner(NetworkUtils.getAvailableUdpPort(), OID, "GET");
final TestRunner getSnmpTestRunner = new SNMPV1TestRunnerFactory().createSnmpGetTestRunner(0, OID, "GET");
final GetSNMP spyGetSNMP = spy((GetSNMP) getSnmpTestRunner.getProcessor());
final MockProcessSession mockProcessSession = new MockProcessSession(new SharedSessionState(spyGetSNMP, new AtomicLong(0L)), spyGetSNMP);
@ -49,7 +48,7 @@ class GetSNMPTest {
@Test
void testOnTriggerWithWalkStrategyPerformsSnmpWalk() {
final TestRunner getSnmpTestRunner = new SNMPV1TestRunnerFactory().createSnmpGetTestRunner(NetworkUtils.getAvailableUdpPort(), OID, "WALK");
final TestRunner getSnmpTestRunner = new SNMPV1TestRunnerFactory().createSnmpGetTestRunner(0, OID, "WALK");
final GetSNMP spyGetSNMP = spy((GetSNMP) getSnmpTestRunner.getProcessor());
final MockProcessSession mockProcessSession = new MockProcessSession(new SharedSessionState(spyGetSNMP, new AtomicLong(0L)), spyGetSNMP);

View File

@ -16,7 +16,6 @@
*/
package org.apache.nifi.snmp.processors;
import org.apache.nifi.remote.io.socket.NetworkUtils;
import org.apache.nifi.snmp.configuration.V1TrapConfiguration;
import org.apache.nifi.snmp.configuration.V2TrapConfiguration;
import org.apache.nifi.snmp.helper.TrapConfigurationFactory;
@ -26,10 +25,13 @@ import org.apache.nifi.snmp.helper.testrunners.SNMPV2cTestRunnerFactory;
import org.apache.nifi.snmp.utils.SNMPUtils;
import org.apache.nifi.util.MockFlowFile;
import org.apache.nifi.util.TestRunner;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.snmp4j.mp.SnmpConstants;
import java.util.Collections;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@ -40,15 +42,16 @@ class TrapSNMPIT {
@Test
void testSendReceiveV1Trap() throws InterruptedException {
final int listenPort = NetworkUtils.getAvailableUdpPort();
final V1TrapConfiguration v1TrapConfiguration = TrapConfigurationFactory.getV1TrapConfiguration();
final SNMPTestRunnerFactory v1TestRunnerFactory = new SNMPV1TestRunnerFactory();
final TestRunner sendTrapTestRunner = v1TestRunnerFactory.createSnmpSendTrapTestRunner(listenPort, SYSTEM_DESCRIPTION_OID, SYSTEM_DESCRIPTION_OID_VALUE);
final TestRunner listenTrapTestRunner = v1TestRunnerFactory.createSnmpListenTrapTestRunner(listenPort);
final TestRunner listenTrapTestRunner = v1TestRunnerFactory.createSnmpListenTrapTestRunner(0);
listenTrapTestRunner.run(1, false);
final ListenTrapSNMP listenTrapSNMP = (ListenTrapSNMP) listenTrapTestRunner.getProcessor();
final int listenPort = listenTrapSNMP.getListeningPort();
final TestRunner sendTrapTestRunner = v1TestRunnerFactory.createSnmpSendTrapTestRunner(listenPort, SYSTEM_DESCRIPTION_OID, SYSTEM_DESCRIPTION_OID_VALUE);
sendTrapTestRunner.run(1);
Thread.sleep(50);
@ -71,21 +74,30 @@ class TrapSNMPIT {
}
@Test
@Timeout(10)
void testSendReceiveV2Trap() throws InterruptedException {
final int listenPort = NetworkUtils.getAvailableUdpPort();
final V2TrapConfiguration v2TrapConfiguration = TrapConfigurationFactory.getV2TrapConfiguration();
final SNMPTestRunnerFactory v2cTestRunnerFactory = new SNMPV2cTestRunnerFactory();
final TestRunner sendTrapTestRunner = v2cTestRunnerFactory.createSnmpSendTrapTestRunner(listenPort, SYSTEM_DESCRIPTION_OID, SYSTEM_DESCRIPTION_OID_VALUE);
final TestRunner listenTrapTestRunner = v2cTestRunnerFactory.createSnmpListenTrapTestRunner(listenPort);
final TestRunner listenTrapTestRunner = v2cTestRunnerFactory.createSnmpListenTrapTestRunner(0);
listenTrapTestRunner.run(1, false);
sendTrapTestRunner.run(1);
final ListenTrapSNMP listenTrapSNMP = ((ListenTrapSNMP) listenTrapTestRunner.getProcessor());
final int listenPort = listenTrapSNMP.getListeningPort();
Thread.sleep(50);
final TestRunner sendTrapTestRunner = v2cTestRunnerFactory.createSnmpSendTrapTestRunner(listenPort, SYSTEM_DESCRIPTION_OID, SYSTEM_DESCRIPTION_OID_VALUE);
final MockFlowFile successFF = listenTrapTestRunner.getFlowFilesForRelationship(GetSNMP.REL_SUCCESS).get(0);
sendTrapTestRunner.run();
List<MockFlowFile> successFlowFiles = Collections.emptyList();
while (successFlowFiles.isEmpty()) {
successFlowFiles = listenTrapTestRunner.getFlowFilesForRelationship(GetSNMP.REL_SUCCESS);
if (successFlowFiles.isEmpty()) {
Thread.sleep(10L);
}
}
final MockFlowFile successFF = successFlowFiles.get(0);
assertNotNull(successFF);
assertEquals("Success", successFF.getAttribute(SNMPUtils.SNMP_PROP_PREFIX + "errorStatusText"));
@ -98,11 +110,4 @@ class TrapSNMPIT {
listenTrapTestRunner.shutdown();
}
@Disabled("The ListenTrapSNMP and SendTrapSNMP processors use the same SecurityProtocols instance" +
" and same USM (the USM is stored in a map by version), hence this case shall be manually tested." +
" Check assertByVersion() to see what the trap payload must contain.")
@Test
void testReceiveV3Trap() {
}
}

View File

@ -16,7 +16,6 @@
*/
package org.apache.nifi.snmp.testagents;
import org.apache.nifi.remote.io.socket.NetworkUtils;
import org.snmp4j.TransportMapping;
import org.snmp4j.agent.BaseAgent;
import org.snmp4j.agent.CommandProcessor;
@ -45,11 +44,10 @@ public abstract class TestAgent extends BaseAgent {
protected static final String BOOT_COUNTER_NAME_TEMPLATE = "target/bootCounter%s_%s.agent";
protected static final String CONFIG_NAME_TEMPLATE = "target/conf%s_%s.agent";
protected final String address;
protected final int port;
private final int port = 56291;
public TestAgent(final File bootCounterFile, final File configFile, final CommandProcessor commandProcessor, final String host) {
super(bootCounterFile, configFile, commandProcessor);
port = NetworkUtils.getAvailableUdpPort();
this.address = String.format("udp:%s/%d", host, port);
}

View File

@ -90,7 +90,4 @@ public class TestSNMPV2cAgent extends TestAgent {
communityMIB.getSnmpCommunityEntry().addRow(row);
}
public int getPort() {
return port;
}
}

View File

@ -23,7 +23,6 @@ import org.apache.nifi.event.transport.configuration.TransportProtocol;
import org.apache.nifi.event.transport.message.ByteArrayMessage;
import org.apache.nifi.event.transport.netty.ByteArrayMessageNettyEventServerFactory;
import org.apache.nifi.event.transport.netty.NettyEventServerFactory;
import org.apache.nifi.remote.io.socket.NetworkUtils;
import org.apache.nifi.util.MockFlowFile;
import org.apache.nifi.util.TestRunner;
import org.apache.nifi.util.TestRunners;
@ -267,7 +266,8 @@ public class TestPutSplunk {
@Timeout(value = DEFAULT_TEST_TIMEOUT_PERIOD, unit = TimeUnit.MILLISECONDS)
public void testUnableToCreateConnectionShouldRouteToFailure() {
// Set an unreachable port
runner.setProperty(PutSplunk.PORT, String.valueOf(NetworkUtils.getAvailableUdpPort()));
runner.setProperty(PutSplunk.PORT, "482");
runner.setProperty(PutSplunk.HOSTNAME, "host-that-does-not-exist");
final String message = "This is one message, should send the whole FlowFile";
@ -276,24 +276,17 @@ public class TestPutSplunk {
runner.assertAllFlowFilesTransferred(PutSplunk.REL_FAILURE, 1);
}
private void createTestServer(final TransportProtocol protocol) {
if (protocol == TransportProtocol.UDP) {
createTestServer(NetworkUtils.getAvailableUdpPort(), protocol);
} else {
createTestServer(NetworkUtils.getAvailableTcpPort(), protocol);
}
}
private void createTestServer(final int port, final TransportProtocol protocol) {
private void createTestServer(final TransportProtocol protocol) {
messages = new LinkedBlockingQueue<>();
runner.setProperty(PutSplunk.PROTOCOL, protocol.name());
runner.setProperty(PutSplunk.PORT, String.valueOf(port));
final byte[] delimiter = OUTGOING_MESSAGE_DELIMITER.getBytes(CHARSET);
NettyEventServerFactory serverFactory = new ByteArrayMessageNettyEventServerFactory(runner.getLogger(), getListenAddress(), port, protocol, delimiter, VALID_LARGE_FILE_SIZE, messages);
NettyEventServerFactory serverFactory = new ByteArrayMessageNettyEventServerFactory(runner.getLogger(), getListenAddress(), 0, protocol, delimiter, VALID_LARGE_FILE_SIZE, messages);
serverFactory.setShutdownQuietPeriod(ShutdownQuietPeriod.QUICK.getDuration());
serverFactory.setShutdownTimeout(ShutdownTimeout.QUICK.getDuration());
eventServer = serverFactory.getEventServer();
runner.setProperty(PutSplunk.PORT, String.valueOf(eventServer.getListeningPort()));
}
private void checkReceivedAllData(final String... sentData) throws Exception {

View File

@ -161,7 +161,7 @@ public class HandleHttpRequest extends AbstractProcessor {
.name("Listening Port")
.description("The Port to listen on for incoming HTTP requests")
.required(true)
.addValidator(StandardValidators.createLongValidator(0L, 65535L, true))
.addValidator(StandardValidators.PORT_VALIDATOR)
.expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY)
.defaultValue("80")
.build();

View File

@ -55,6 +55,7 @@ import org.apache.nifi.stream.io.StreamThrottler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import javax.net.ssl.SSLContext;
@ -137,7 +138,7 @@ public class ListenHTTP extends AbstractSessionFactoryProcessor {
.description("The Port to listen on for incoming connections")
.required(true)
.expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY)
.addValidator(StandardValidators.POSITIVE_INTEGER_VALIDATOR)
.addValidator(StandardValidators.PORT_VALIDATOR)
.build();
public static final PropertyDescriptor HEALTH_CHECK_PORT = new PropertyDescriptor.Builder()
.name("health-check-port")
@ -387,7 +388,7 @@ public class ListenHTTP extends AbstractSessionFactoryProcessor {
}
synchronized private void createHttpServerFromService(final ProcessContext context) throws Exception {
if(initialized.get()) {
if (initialized.get()) {
return;
}
runOnPrimary.set(context.getExecutionNode().equals(ExecutionNode.PRIMARY));
@ -436,15 +437,18 @@ public class ListenHTTP extends AbstractSessionFactoryProcessor {
final boolean securityEnabled = sslContextService != null;
final ServletContextHandler contextHandler = new ServletContextHandler(server, "/", true, securityEnabled);
final List<Servlet> servlets = new ArrayList<>();
for (final Class<? extends Servlet> cls : getServerClasses()) {
final Path path = cls.getAnnotation(Path.class);
// Note: servlets must have a path annotation - this will NPE otherwise
// also, servlets other than ListenHttpServlet must have a path starting with /
if (basePath.isEmpty() && !path.value().isEmpty()) {
// Note: this is to handle the condition of an empty uri, otherwise pathSpec would start with //
contextHandler.addServlet(cls, path.value());
final ServletHolder holder = contextHandler.addServlet(cls, path.value());
servlets.add(holder.getServlet());
} else {
contextHandler.addServlet(cls, "/" + basePath + path.value());
final ServletHolder holder = contextHandler.addServlet(cls, "/" + basePath + path.value());
servlets.add(holder.getServlet());
}
}
@ -466,6 +470,7 @@ public class ListenHTTP extends AbstractSessionFactoryProcessor {
if (context.getProperty(HEADERS_AS_ATTRIBUTES_REGEX).isSet()) {
contextHandler.setAttribute(CONTEXT_ATTRIBUTE_HEADER_PATTERN, Pattern.compile(context.getProperty(HEADERS_AS_ATTRIBUTES_REGEX).getValue()));
}
try {
server.start();
} catch (Exception e) {
@ -473,6 +478,15 @@ public class ListenHTTP extends AbstractSessionFactoryProcessor {
throw e;
}
// If Port is set to 0, we need to notify the ListenHTTPServlet of the actual port being used. But this isn't available until after
// the server has been started, and at that point it is too late to set it in the configuration for the context handler so we set it afterwards.
for (final ServletHolder holder : contextHandler.getServletHandler().getServlets()) {
final Servlet servlet = holder.getServlet();
if (servlet instanceof ListenHTTPServlet) {
((ListenHTTPServlet) servlet).setPort(connector.getLocalPort());
}
}
this.server = server;
initialized.set(true);
}

View File

@ -140,6 +140,10 @@ public class ListenRELP extends AbstractProcessor {
}
}
public int getListeningPort() {
return eventServer == null ? 0 : eventServer.getListeningPort();
}
@OnStopped
public void stopped() {
if (eventServer != null) {

View File

@ -313,6 +313,10 @@ public class ListenSyslog extends AbstractSyslogProcessor {
eventServer = factory.getEventServer();
}
public int getListeningPort() {
return eventServer == null ? 0 : eventServer.getListeningPort();
}
@OnStopped
public void shutdownEventServer() {
if (eventServer != null) {
@ -448,7 +452,7 @@ public class ListenSyslog extends AbstractSyslogProcessor {
}
private Map<String, String> getDefaultAttributes(final ProcessContext context) {
final String port = context.getProperty(PORT).evaluateAttributeExpressions().getValue();
final String port = String.valueOf(getListeningPort());
final String protocol = context.getProperty(PROTOCOL).getValue();
final Map<String, String> defaultAttributes = new HashMap<>();

View File

@ -228,6 +228,10 @@ public class ListenTCP extends AbstractProcessor {
}
}
public int getListeningPort() {
return eventServer == null ? 0 : eventServer.getListeningPort();
}
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
processTrackingLog();

View File

@ -16,32 +16,6 @@
*/
package org.apache.nifi.processors.standard;
import static org.apache.nifi.processor.util.listen.ListenerProperties.NETWORK_INTF_NAME;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.SocketTimeoutException;
import java.nio.channels.ServerSocketChannel;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLSession;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.nifi.annotation.behavior.InputRequirement;
@ -81,6 +55,32 @@ import org.apache.nifi.serialization.record.RecordSchema;
import org.apache.nifi.ssl.RestrictedSSLContextService;
import org.apache.nifi.ssl.SSLContextService;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLSession;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.SocketTimeoutException;
import java.nio.channels.ServerSocketChannel;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import static org.apache.nifi.processor.util.listen.ListenerProperties.NETWORK_INTF_NAME;
@SupportsBatching
@InputRequirement(InputRequirement.Requirement.INPUT_FORBIDDEN)
@Tags({"listen", "tcp", "record", "tls", "ssl"})
@ -316,6 +316,10 @@ public class ListenTCPRecord extends AbstractProcessor {
readerThread.start();
}
public int getListeningPort() {
return dispatcher.getPort();
}
@OnStopped
public void onStopped() {
if (dispatcher != null) {

View File

@ -141,6 +141,10 @@ public class ListenHTTPServlet extends HttpServlet {
this.writerFactory = processContext.getProperty(ListenHTTP.RECORD_WRITER).asControllerService(RecordSetWriterFactory.class);
}
public void setPort(final int port) {
this.port = port;
}
@Override
protected void doHead(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
if (request.getLocalPort() == port) {

View File

@ -23,6 +23,7 @@ import org.apache.ftpserver.ftplet.FileSystemView;
import org.apache.ftpserver.ftplet.FtpException;
import org.apache.ftpserver.ftplet.FtpFile;
import org.apache.ftpserver.ftplet.UserManager;
import org.apache.ftpserver.listener.Listener;
import org.apache.ftpserver.listener.ListenerFactory;
import org.apache.ftpserver.usermanager.ClearTextPasswordEncryptor;
import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;
@ -31,7 +32,6 @@ import org.apache.nifi.flowfile.attributes.CoreAttributes;
import org.apache.nifi.processor.Processor;
import org.apache.nifi.processor.util.list.AbstractListProcessor;
import org.apache.nifi.processors.standard.util.FTPTransfer;
import org.apache.nifi.remote.io.socket.NetworkUtils;
import org.apache.nifi.util.MockFlowFile;
import org.apache.nifi.util.TestRunner;
import org.apache.nifi.util.TestRunners;
@ -49,6 +49,7 @@ import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
@ -80,8 +81,6 @@ public class FTPCharsetIT {
private static FtpServer FTP_SERVER;
private static final String USE_UTF8 = Boolean.TRUE.toString();
private static final String HOSTNAME = "localhost";
private static final String PORT = Integer.toString(NetworkUtils.getAvailableTcpPort());
private static final String USER = "ftpuser";
private static final String PASSWORD = "admin";
private static final String TIMEOUT = "3 secs";
@ -91,14 +90,8 @@ public class FTPCharsetIT {
@TempDir
private static File FOLDER_USER_PROPERTIES;
public static Arguments serverParametersProvider() {
final String override = System.getProperty(FTPCharsetIT.class.getSimpleName());
if (override == null) {
return arguments(HOSTNAME, PORT, USER, PASSWORD);
} else {
return arguments((Object[]) override.split(","));
}
}
private static int listeningPort;
public static Stream<Arguments> folderNamesProvider() {
return Stream.of(
@ -125,7 +118,10 @@ public class FTPCharsetIT {
@BeforeAll
static void startEmbeddedServer() throws IOException, FtpException {
if (EMBED_FTP_SERVER) {
if (!EMBED_FTP_SERVER) {
return;
}
// setup ftp user
final Properties userProperties = new Properties();
userProperties.setProperty("ftpserver.user.ftpuser.idletime", "0");
@ -142,6 +138,7 @@ public class FTPCharsetIT {
userManagerFactory.setPasswordEncryptor(new ClearTextPasswordEncryptor());
final UserManager userManager = userManagerFactory.createUserManager();
final BaseUser ftpuser = (BaseUser) userManager.getUserByName(USER);
// setup embedded ftp server
final FtpServerFactory serverFactory = new FtpServerFactory();
serverFactory.setUserManager(userManager);
@ -151,12 +148,15 @@ public class FTPCharsetIT {
final Object physicalFile = workingDirectory.getPhysicalFile();
assertInstanceOf(File.class, physicalFile);
assertEquals(FOLDER_FTP.getAbsolutePath(), ((File) physicalFile).getAbsolutePath());
final ListenerFactory factory = new ListenerFactory();
factory.setPort(Integer.parseInt(PORT));
factory.setPort(0);
serverFactory.addListener("default", factory.createListener());
FTP_SERVER = serverFactory.createServer();
FTP_SERVER.start();
}
final Collection<Listener> listeners = serverFactory.getListeners().values();
listeningPort = listeners.isEmpty() ? 0 : listeners.iterator().next().getPort();
}
@AfterAll
@ -289,12 +289,21 @@ public class FTPCharsetIT {
private static TestRunner provisionTestRunner(final Class<? extends Processor> processorClass) {
final TestRunner runner = TestRunners.newTestRunner(processorClass);
final Object[] serverParameters = serverParametersProvider().get();
int i = -1;
runner.setProperty(FTPTransfer.HOSTNAME, serverParameters[++i].toString());
runner.setProperty(FTPTransfer.PORT, serverParameters[++i].toString());
runner.setProperty(FTPTransfer.USERNAME, serverParameters[++i].toString());
runner.setProperty(FTPTransfer.PASSWORD, serverParameters[++i].toString());
final String valueOverrides = System.getProperty(FTPCharsetIT.class.getSimpleName());
if (valueOverrides == null) {
runner.setProperty(FTPTransfer.HOSTNAME, "localhost");
runner.setProperty(FTPTransfer.PORT, String.valueOf(listeningPort));
runner.setProperty(FTPTransfer.USERNAME, USER);
runner.setProperty(FTPTransfer.PASSWORD, PASSWORD);
} else {
final String[] serverParameters = valueOverrides.split(",");
runner.setProperty(FTPTransfer.HOSTNAME, serverParameters[0]);
runner.setProperty(FTPTransfer.PORT, serverParameters[1]);
runner.setProperty(FTPTransfer.USERNAME, serverParameters[2]);
runner.setProperty(FTPTransfer.PASSWORD, serverParameters[3]);
}
runner.setProperty(FTPTransfer.UTF8_ENCODING, USE_UTF8);
runner.setProperty(FTPTransfer.CONNECTION_TIMEOUT, TIMEOUT);
runner.setProperty(FTPTransfer.DATA_TIMEOUT, TIMEOUT);

View File

@ -17,7 +17,6 @@
package org.apache.nifi.processors.standard;
import org.apache.nifi.http.HttpContextMap;
import org.apache.nifi.remote.io.socket.NetworkUtils;
import org.apache.nifi.reporting.InitializationException;
import org.apache.nifi.util.TestRunner;
import org.apache.nifi.util.TestRunners;
@ -67,9 +66,7 @@ class HandleHttpRequestTest {
void testRun() {
runner.setProperty(HandleHttpRequest.HTTP_CONTEXT_MAP, CONTEXT_MAP_ID);
runner.setProperty(HandleHttpRequest.MAXIMUM_THREADS, MINIMUM_THREADS);
final int port = NetworkUtils.getAvailableTcpPort();
runner.setProperty(HandleHttpRequest.PORT, Integer.toString(port));
runner.setProperty(HandleHttpRequest.PORT, "0");
runner.run();

View File

@ -16,13 +16,40 @@
*/
package org.apache.nifi.processors.standard;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.apache.nifi.annotation.notification.PrimaryNodeState;
import org.apache.nifi.controller.AbstractControllerService;
import org.apache.nifi.http.HttpContextMap;
import org.apache.nifi.processor.ProcessContext;
import org.apache.nifi.processors.standard.util.HTTPUtils;
import org.apache.nifi.reporting.InitializationException;
import org.apache.nifi.scheduling.ExecutionNode;
import org.apache.nifi.security.util.TlsException;
import org.apache.nifi.ssl.RestrictedSSLContextService;
import org.apache.nifi.util.MockFlowFile;
import org.apache.nifi.util.TestRunner;
import org.apache.nifi.util.TestRunners;
import org.apache.nifi.web.util.ssl.SslContextUtils;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.mockito.Mockito;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.servlet.AsyncContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
@ -42,41 +69,13 @@ import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.servlet.AsyncContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.apache.nifi.annotation.notification.PrimaryNodeState;
import org.apache.nifi.controller.AbstractControllerService;
import org.apache.nifi.http.HttpContextMap;
import org.apache.nifi.processor.ProcessContext;
import org.apache.nifi.processors.standard.util.HTTPUtils;
import org.apache.nifi.remote.io.socket.NetworkUtils;
import org.apache.nifi.reporting.InitializationException;
import org.apache.nifi.scheduling.ExecutionNode;
import org.apache.nifi.security.util.TlsException;
import org.apache.nifi.ssl.RestrictedSSLContextService;
import org.apache.nifi.util.MockFlowFile;
import org.apache.nifi.util.TestRunner;
import org.apache.nifi.util.TestRunners;
import org.apache.nifi.web.util.ssl.SslContextUtils;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.mockito.Mockito;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
public class ITestHandleHttpRequest {
@ -511,8 +510,7 @@ public class ITestHandleHttpRequest {
public void testOnPrimaryNodeChangePrimaryNodeRevoked() throws Exception {
processor = new HandleHttpRequest();
final TestRunner runner = TestRunners.newTestRunner(processor);
final int port = NetworkUtils.getAvailableTcpPort();
runner.setProperty(HandleHttpRequest.PORT, Integer.toString(port));
runner.setProperty(HandleHttpRequest.PORT, "0");
final MockHttpContextMap contextMap = new MockHttpContextMap();
final String contextMapId = MockHttpContextMap.class.getSimpleName();
@ -526,7 +524,7 @@ public class ITestHandleHttpRequest {
final OkHttpClient client = new OkHttpClient.Builder().build();
final String url = String.format("http://localhost:%d", port);
final String url = String.format("http://localhost:%d", processor.getPort());
final ExecutorService executorService = Executors.newSingleThreadExecutor();
final CountDownLatch requestCompleted = new CountDownLatch(1);

View File

@ -16,6 +16,50 @@
*/
package org.apache.nifi.processors.standard;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okio.BufferedSink;
import okio.GzipSink;
import okio.Okio;
import org.apache.nifi.processors.standard.http.ContentEncodingStrategy;
import org.apache.nifi.processors.standard.http.HttpProtocolStrategy;
import org.apache.nifi.reporting.InitializationException;
import org.apache.nifi.security.util.SslContextFactory;
import org.apache.nifi.security.util.StandardTlsConfiguration;
import org.apache.nifi.security.util.TemporaryKeyStoreBuilder;
import org.apache.nifi.security.util.TlsConfiguration;
import org.apache.nifi.security.util.TlsPlatform;
import org.apache.nifi.serialization.record.MockRecordParser;
import org.apache.nifi.serialization.record.MockRecordWriter;
import org.apache.nifi.serialization.record.RecordFieldType;
import org.apache.nifi.ssl.RestrictedSSLContextService;
import org.apache.nifi.ssl.SSLContextService;
import org.apache.nifi.util.MockFlowFile;
import org.apache.nifi.util.TestRunner;
import org.apache.nifi.util.TestRunners;
import org.apache.nifi.web.util.ssl.SslContextUtils;
import org.eclipse.jetty.server.NetworkConnector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.thread.ThreadPool;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIf;
import org.mockito.Mockito;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLHandshakeException;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
@ -33,51 +77,6 @@ import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.Random;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLHandshakeException;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import javax.servlet.http.HttpServletResponse;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okio.BufferedSink;
import okio.GzipSink;
import okio.Okio;
import org.apache.nifi.processor.ProcessContext;
import org.apache.nifi.processor.ProcessSessionFactory;
import org.apache.nifi.processors.standard.http.ContentEncodingStrategy;
import org.apache.nifi.processors.standard.http.HttpProtocolStrategy;
import org.apache.nifi.remote.io.socket.NetworkUtils;
import org.apache.nifi.reporting.InitializationException;
import org.apache.nifi.security.util.SslContextFactory;
import org.apache.nifi.security.util.StandardTlsConfiguration;
import org.apache.nifi.security.util.TemporaryKeyStoreBuilder;
import org.apache.nifi.security.util.TlsConfiguration;
import org.apache.nifi.security.util.TlsPlatform;
import org.apache.nifi.serialization.record.MockRecordParser;
import org.apache.nifi.serialization.record.MockRecordWriter;
import org.apache.nifi.serialization.record.RecordFieldType;
import org.apache.nifi.ssl.RestrictedSSLContextService;
import org.apache.nifi.ssl.SSLContextService;
import org.apache.nifi.util.MockFlowFile;
import org.apache.nifi.util.TestRunner;
import org.apache.nifi.util.TestRunners;
import org.apache.nifi.web.util.ssl.SslContextUtils;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.thread.ThreadPool;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIf;
import org.mockito.Mockito;
import static org.apache.nifi.processors.standard.ListenHTTP.RELATIONSHIP_SUCCESS;
import static org.junit.jupiter.api.Assertions.assertEquals;
@ -119,7 +118,6 @@ public class TestListenHTTP {
private ListenHTTP proc;
private TestRunner runner;
private int availablePort;
static boolean isTls13Supported() {
return TLS_1_3.equals(TlsPlatform.getLatestProtocol());
@ -186,10 +184,12 @@ public class TestListenHTTP {
@BeforeEach
public void setup() throws IOException {
proc = new ListenHTTP();
runner = TestRunners.newTestRunner(proc);
availablePort = NetworkUtils.availablePort();
runner.setVariable(PORT_VARIABLE, Integer.toString(availablePort));
runner.setVariable(PORT_VARIABLE, "0");
runner.setVariable(BASEPATH_VARIABLE, HTTP_BASE_PATH);
runner.setProperty(ListenHTTP.PORT, "0");
runner.setProperty(ListenHTTP.BASE_PATH, HTTP_BASE_PATH);
}
@AfterEach
@ -199,7 +199,6 @@ public class TestListenHTTP {
@Test
public void testPOSTRequestsReceivedWithoutEL() throws Exception {
runner.setProperty(ListenHTTP.PORT, Integer.toString(availablePort));
runner.setProperty(ListenHTTP.BASE_PATH, HTTP_BASE_PATH);
testPOSTRequestsReceived(HttpServletResponse.SC_OK, false, false);
@ -207,7 +206,6 @@ public class TestListenHTTP {
@Test
public void testPOSTRequestsReceivedReturnCodeWithoutEL() throws Exception {
runner.setProperty(ListenHTTP.PORT, Integer.toString(availablePort));
runner.setProperty(ListenHTTP.BASE_PATH, HTTP_BASE_PATH);
runner.setProperty(ListenHTTP.RETURN_CODE, Integer.toString(HttpServletResponse.SC_NO_CONTENT));
@ -237,7 +235,6 @@ public class TestListenHTTP {
public void testSecurePOSTRequestsReceivedWithoutELHttp2AndHttp1() throws Exception {
configureProcessorSslContextService(ListenHTTP.ClientAuthentication.AUTO, serverNoTruststoreConfiguration);
runner.setProperty(ListenHTTP.PORT, Integer.toString(availablePort));
runner.setProperty(ListenHTTP.BASE_PATH, HTTP_BASE_PATH);
runner.setProperty(ListenHTTP.HTTP_PROTOCOL_STRATEGY, HttpProtocolStrategy.H2_HTTP_1_1.getValue());
runner.assertValid();
@ -249,7 +246,6 @@ public class TestListenHTTP {
public void testSecurePOSTRequestsReturnCodeReceivedWithoutELHttp2() throws Exception {
configureProcessorSslContextService(ListenHTTP.ClientAuthentication.AUTO, serverNoTruststoreConfiguration);
runner.setProperty(ListenHTTP.PORT, Integer.toString(availablePort));
runner.setProperty(ListenHTTP.BASE_PATH, HTTP_BASE_PATH);
runner.setProperty(ListenHTTP.RETURN_CODE, Integer.toString(HttpServletResponse.SC_NO_CONTENT));
runner.setProperty(ListenHTTP.HTTP_PROTOCOL_STRATEGY, HttpProtocolStrategy.H2.getValue());
@ -273,7 +269,6 @@ public class TestListenHTTP {
public void testSecurePOSTRequestsReturnCodeReceivedWithEL() throws Exception {
configureProcessorSslContextService(ListenHTTP.ClientAuthentication.AUTO, serverNoTruststoreConfiguration);
runner.setProperty(ListenHTTP.PORT, Integer.toString(availablePort));
runner.setProperty(ListenHTTP.BASE_PATH, HTTP_BASE_PATH);
runner.setProperty(ListenHTTP.RETURN_CODE, Integer.toString(HttpServletResponse.SC_NO_CONTENT));
runner.assertValid();
@ -285,7 +280,6 @@ public class TestListenHTTP {
public void testSecureTwoWaySslPOSTRequestsReceivedWithoutEL() throws Exception {
configureProcessorSslContextService(ListenHTTP.ClientAuthentication.REQUIRED, serverConfiguration);
runner.setProperty(ListenHTTP.PORT, Integer.toString(availablePort));
runner.setProperty(ListenHTTP.BASE_PATH, HTTP_BASE_PATH);
runner.assertValid();
@ -296,7 +290,6 @@ public class TestListenHTTP {
public void testSecureTwoWaySslPOSTRequestsReceivedWithUnauthorizedSubjectDn() throws Exception {
configureProcessorSslContextService(ListenHTTP.ClientAuthentication.REQUIRED, serverConfiguration);
runner.setProperty(ListenHTTP.PORT, Integer.toString(availablePort));
runner.setProperty(ListenHTTP.AUTHORIZED_DN_PATTERN, "CN=other");
runner.setProperty(ListenHTTP.BASE_PATH, HTTP_BASE_PATH);
runner.assertValid();
@ -308,7 +301,6 @@ public class TestListenHTTP {
public void testSecureTwoWaySslPOSTRequestsReceivedWithAuthorizedIssuerDn() throws Exception {
configureProcessorSslContextService(ListenHTTP.ClientAuthentication.REQUIRED, serverConfiguration);
runner.setProperty(ListenHTTP.PORT, Integer.toString(availablePort));
runner.setProperty(ListenHTTP.AUTHORIZED_DN_PATTERN, LOCALHOST_DN);
runner.setProperty(ListenHTTP.AUTHORIZED_ISSUER_DN_PATTERN, LOCALHOST_DN);
runner.setProperty(ListenHTTP.BASE_PATH, HTTP_BASE_PATH);
@ -321,7 +313,6 @@ public class TestListenHTTP {
public void testSecureTwoWaySslPOSTRequestsReceivedWithUnauthorizedIssuerDn() throws Exception {
configureProcessorSslContextService(ListenHTTP.ClientAuthentication.REQUIRED, serverConfiguration);
runner.setProperty(ListenHTTP.PORT, Integer.toString(availablePort));
runner.setProperty(ListenHTTP.AUTHORIZED_DN_PATTERN, LOCALHOST_DN); // Although subject is authorized, issuer is not
runner.setProperty(ListenHTTP.AUTHORIZED_ISSUER_DN_PATTERN, "CN=other");
runner.setProperty(ListenHTTP.BASE_PATH, HTTP_BASE_PATH);
@ -334,7 +325,6 @@ public class TestListenHTTP {
public void testSecureTwoWaySslPOSTRequestsReturnCodeReceivedWithoutEL() throws Exception {
configureProcessorSslContextService(ListenHTTP.ClientAuthentication.REQUIRED, serverConfiguration);
runner.setProperty(ListenHTTP.PORT, Integer.toString(availablePort));
runner.setProperty(ListenHTTP.BASE_PATH, HTTP_BASE_PATH);
runner.setProperty(ListenHTTP.RETURN_CODE, Integer.toString(HttpServletResponse.SC_NO_CONTENT));
runner.assertValid();
@ -357,7 +347,6 @@ public class TestListenHTTP {
public void testSecureTwoWaySslPOSTRequestsReturnCodeReceivedWithEL() throws Exception {
configureProcessorSslContextService(ListenHTTP.ClientAuthentication.REQUIRED, serverConfiguration);
runner.setProperty(ListenHTTP.PORT, Integer.toString(availablePort));
runner.setProperty(ListenHTTP.BASE_PATH, HTTP_BASE_PATH);
runner.setProperty(ListenHTTP.RETURN_CODE, Integer.toString(HttpServletResponse.SC_NO_CONTENT));
runner.assertValid();
@ -368,10 +357,10 @@ public class TestListenHTTP {
@Test
public void testSecureServerSupportsCurrentTlsProtocolVersion() throws Exception {
configureProcessorSslContextService(ListenHTTP.ClientAuthentication.AUTO, serverNoTruststoreConfiguration);
startSecureServer();
final int listeningPort = startSecureServer();
final SSLSocketFactory sslSocketFactory = trustStoreSslContext.getSocketFactory();
try (final SSLSocket sslSocket = (SSLSocket) sslSocketFactory.createSocket(LOCALHOST, availablePort)) {
try (final SSLSocket sslSocket = (SSLSocket) sslSocketFactory.createSocket(LOCALHOST, listeningPort)) {
final String currentProtocol = serverNoTruststoreConfiguration.getProtocol();
sslSocket.setEnabledProtocols(new String[]{currentProtocol});
@ -384,15 +373,15 @@ public class TestListenHTTP {
@Test
public void testSecureServerTrustStoreConfiguredClientAuthenticationRequired() throws Exception {
configureProcessorSslContextService(ListenHTTP.ClientAuthentication.REQUIRED, serverConfiguration);
startSecureServer();
assertThrows(IOException.class, () -> postMessage(null, true, false));
final int port = startSecureServer();
assertThrows(IOException.class, () -> postMessage(null, true, port, false));
}
@Test
public void testSecureServerTrustStoreNotConfiguredClientAuthenticationNotRequired() throws Exception {
configureProcessorSslContextService(ListenHTTP.ClientAuthentication.AUTO, serverNoTruststoreConfiguration);
startSecureServer();
final int responseCode = postMessage(null, true, true);
final int port = startSecureServer();
final int responseCode = postMessage(null, true, port, true);
assertEquals(HttpServletResponse.SC_NO_CONTENT, responseCode);
}
@ -401,14 +390,13 @@ public class TestListenHTTP {
public void testSecureServerRejectsUnsupportedTlsProtocolVersion() throws Exception {
configureProcessorSslContextService(ListenHTTP.ClientAuthentication.AUTO, serverTls_1_3_Configuration);
runner.setProperty(ListenHTTP.PORT, Integer.toString(availablePort));
runner.setProperty(ListenHTTP.BASE_PATH, HTTP_BASE_PATH);
runner.setProperty(ListenHTTP.RETURN_CODE, Integer.toString(HttpServletResponse.SC_NO_CONTENT));
runner.assertValid();
startWebServer();
final int listeningPort = startWebServer();
final SSLSocketFactory sslSocketFactory = trustStoreSslContext.getSocketFactory();
try (final SSLSocket sslSocket = (SSLSocket) sslSocketFactory.createSocket(LOCALHOST, availablePort)) {
try (final SSLSocket sslSocket = (SSLSocket) sslSocketFactory.createSocket(LOCALHOST, listeningPort)) {
sslSocket.setEnabledProtocols(new String[]{TLS_1_2});
assertThrows(SSLHandshakeException.class, sslSocket::startHandshake);
@ -417,7 +405,6 @@ public class TestListenHTTP {
@Test
public void testMaxThreadPoolSizeTooLow() {
runner.setProperty(ListenHTTP.PORT, Integer.toString(availablePort));
runner.setProperty(ListenHTTP.BASE_PATH, HTTP_BASE_PATH);
runner.setProperty(ListenHTTP.MAX_THREAD_POOL_SIZE, "7");
@ -426,7 +413,6 @@ public class TestListenHTTP {
@Test
public void testMaxThreadPoolSizeTooHigh() {
runner.setProperty(ListenHTTP.PORT, Integer.toString(availablePort));
runner.setProperty(ListenHTTP.BASE_PATH, HTTP_BASE_PATH);
runner.setProperty(ListenHTTP.MAX_THREAD_POOL_SIZE, "1001");
@ -435,7 +421,6 @@ public class TestListenHTTP {
@Test
public void testMaxThreadPoolSizeOkLowerBound() {
runner.setProperty(ListenHTTP.PORT, Integer.toString(availablePort));
runner.setProperty(ListenHTTP.BASE_PATH, HTTP_BASE_PATH);
runner.setProperty(ListenHTTP.MAX_THREAD_POOL_SIZE, "8");
@ -444,7 +429,6 @@ public class TestListenHTTP {
@Test
public void testMaxThreadPoolSizeOkUpperBound() {
runner.setProperty(ListenHTTP.PORT, Integer.toString(availablePort));
runner.setProperty(ListenHTTP.BASE_PATH, HTTP_BASE_PATH);
runner.setProperty(ListenHTTP.MAX_THREAD_POOL_SIZE, "1000");
@ -454,7 +438,6 @@ public class TestListenHTTP {
@Test
public void testMaxThreadPoolSizeSpecifiedInThePropertyIsSetInTheServerInstance() {
int maxThreadPoolSize = 201;
runner.setProperty(ListenHTTP.PORT, Integer.toString(availablePort));
runner.setProperty(ListenHTTP.BASE_PATH, HTTP_BASE_PATH);
runner.setProperty(ListenHTTP.MAX_THREAD_POOL_SIZE, Integer.toString(maxThreadPoolSize));
@ -519,15 +502,14 @@ public class TestListenHTTP {
@Test
public void testPostContentEncodingGzipAccepted() throws IOException {
runner.setProperty(ListenHTTP.PORT, Integer.toString(availablePort));
runner.setProperty(ListenHTTP.BASE_PATH, HTTP_BASE_PATH);
runner.setProperty(ListenHTTP.RETURN_CODE, Integer.toString(HttpServletResponse.SC_NO_CONTENT));
startWebServer();
final int port = startWebServer();
final OkHttpClient okHttpClient = getOkHttpClient(false, false);
final Request.Builder requestBuilder = new Request.Builder();
final String url = buildUrl(false);
final String url = buildUrl(false, port);
requestBuilder.url(url);
final String message = String.class.getSimpleName();
@ -557,26 +539,27 @@ public class TestListenHTTP {
runner.addControllerService("mockRecordParser", parser);
runner.setProperty(ListenHTTP.RECORD_READER, "mockRecordParser");
runner.setProperty(ListenHTTP.PORT, Integer.toString(availablePort));
runner.setProperty(ListenHTTP.BASE_PATH, HTTP_BASE_PATH);
runner.addControllerService("mockRecordWriter", writer);
runner.setProperty(ListenHTTP.RECORD_WRITER, "mockRecordWriter");
runner.enableControllerService(parser);
runner.enableControllerService(writer);
return parser;
}
private void startSecureServer() {
runner.setProperty(ListenHTTP.PORT, Integer.toString(availablePort));
private int startSecureServer() {
runner.setProperty(ListenHTTP.BASE_PATH, HTTP_BASE_PATH);
runner.setProperty(ListenHTTP.RETURN_CODE, Integer.toString(HttpServletResponse.SC_NO_CONTENT));
runner.assertValid();
startWebServer();
return startWebServer();
}
private int postMessage(String message, boolean secure, boolean clientAuthRequired) throws IOException {
private int postMessage(final String message, boolean secure, final int port, boolean clientAuthRequired) throws IOException {
final OkHttpClient okHttpClient = getOkHttpClient(secure, clientAuthRequired);
final Request.Builder requestBuilder = new Request.Builder();
final String url = buildUrl(secure);
final String url = buildUrl(secure, port);
requestBuilder.url(url);
final byte[] bytes = message == null ? new byte[]{} : message.getBytes(StandardCharsets.UTF_8);
@ -603,8 +586,8 @@ public class TestListenHTTP {
return builder.build();
}
private String buildUrl(final boolean secure) {
return String.format("%s://localhost:%s/%s", secure ? "https" : "http", availablePort, HTTP_BASE_PATH);
private String buildUrl(final boolean secure, final int port) {
return String.format("%s://localhost:%s/%s", secure ? "https" : "http", port, HTTP_BASE_PATH);
}
private void testPOSTRequestsReceived(int returnCode, boolean secure, boolean twoWaySsl) throws Exception {
@ -633,13 +616,11 @@ public class TestListenHTTP {
}
}
private void startWebServer() {
final ProcessSessionFactory processSessionFactory = runner.getProcessSessionFactory();
final ProcessContext context = runner.getProcessContext();
proc.onTrigger(context, processSessionFactory);
private int startWebServer() {
runner.run(1, false);
final int listeningPort = ((NetworkConnector) proc.getServer().getConnectors()[0]).getLocalPort();
final int port = context.getProperty(ListenHTTP.PORT).evaluateAttributeExpressions().asInteger();
final InetSocketAddress socketAddress = new InetSocketAddress(LOCALHOST, port);
final InetSocketAddress socketAddress = new InetSocketAddress(LOCALHOST, listeningPort);
final Socket socket = new Socket();
boolean connected = false;
long elapsed = 0;
@ -659,16 +640,18 @@ public class TestListenHTTP {
}
if (!connected) {
final String message = String.format("HTTP Server Port [%d] not listening after %d ms", port, SERVER_START_TIMEOUT);
final String message = String.format("HTTP Server Port [%d] not listening after %d ms", listeningPort, SERVER_START_TIMEOUT);
throw new IllegalStateException(message);
}
return listeningPort;
}
private void startWebServerAndSendMessages(final List<String> messages, final int expectedStatusCode, final boolean secure, final boolean clientAuthRequired) throws Exception {
startWebServer();
final int port = startWebServer();
for (final String message : messages) {
final int statusCode = postMessage(message, secure, clientAuthRequired);
final int statusCode = postMessage(message, secure, port, clientAuthRequired);
assertEquals(expectedStatusCode, statusCode, "HTTP Status Code not matched");
}
}
@ -694,19 +677,18 @@ public class TestListenHTTP {
@Test
public void testMultipartFormDataRequest() throws IOException {
runner.setProperty(ListenHTTP.PORT, Integer.toString(availablePort));
runner.setProperty(ListenHTTP.BASE_PATH, HTTP_BASE_PATH);
runner.setProperty(ListenHTTP.RETURN_CODE, Integer.toString(HttpServletResponse.SC_OK));
runner.setProperty(ListenHTTP.MULTIPART_READ_BUFFER_SIZE, "10 bytes");
runner.setProperty(ListenHTTP.MULTIPART_READ_BUFFER_SIZE, "10 b");
final SSLContextService sslContextService = runner.getControllerService(SSL_CONTEXT_SERVICE_IDENTIFIER, SSLContextService.class);
final boolean isSecure = (sslContextService != null);
startWebServer();
final int port = startWebServer();
File file1 = createTextFile("Hello", "World");
File file2 = createTextFile("{ \"name\":\"John\", \"age\":30 }");
final File file1 = createTextFile("Hello", "World");
final File file2 = createTextFile("{ \"name\":\"John\", \"age\":30 }");
MultipartBody multipartBody = new MultipartBody.Builder().setType(MultipartBody.FORM)
final MultipartBody multipartBody = new MultipartBody.Builder().setType(MultipartBody.FORM)
.addFormDataPart("p1", "v1")
.addFormDataPart("p2", "v2")
.addFormDataPart("file1", "my-file-text.txt", RequestBody.create(file1, MediaType.parse("text/plain")))
@ -714,9 +696,8 @@ public class TestListenHTTP {
.addFormDataPart("file3", "my-file-binary.bin", RequestBody.create(generateRandomBinaryData(), MediaType.parse("application/octet-stream")))
.build();
Request request =
new Request.Builder()
.url(buildUrl(isSecure))
final Request request = new Request.Builder()
.url(buildUrl(isSecure, port))
.post(multipartBody)
.build();

View File

@ -31,7 +31,6 @@ import org.apache.nifi.processors.standard.relp.frame.RELPEncoder;
import org.apache.nifi.processors.standard.relp.frame.RELPFrame;
import org.apache.nifi.provenance.ProvenanceEventRecord;
import org.apache.nifi.provenance.ProvenanceEventType;
import org.apache.nifi.remote.io.socket.NetworkUtils;
import org.apache.nifi.ssl.RestrictedSSLContextService;
import org.apache.nifi.ssl.SSLContextService;
import org.apache.nifi.util.MockFlowFile;
@ -198,7 +197,7 @@ public class TestListenRELP {
MockListenRELP mockListenRELP = new MockListenRELP(mockEvents);
runner = TestRunners.newTestRunner(mockListenRELP);
runner.setProperty(ListenerProperties.PORT, Integer.toString(NetworkUtils.availablePort()));
runner.setProperty(ListenerProperties.PORT, "0");
runner.setProperty(ListenerProperties.MAX_BATCH_SIZE, "10");
runner.run();
@ -207,10 +206,12 @@ public class TestListenRELP {
}
private void run(final List<RELPFrame> frames, final int flowFiles, final SSLContext sslContext) throws Exception {
final int port = NetworkUtils.availablePort();
runner.setProperty(ListenerProperties.PORT, Integer.toString(port));
// Run Processor and start Dispatcher without shutting down
runner.setProperty(ListenerProperties.PORT, "0");
runner.run(1, false, true);
final int port = ((ListenRELP) runner.getProcessor()).getListeningPort();
// Run Processor and start Dispatcher without shutting down
final byte[] relpMessages = getRELPMessages(frames);
sendMessages(port, relpMessages, sslContext);
runner.run(flowFiles, false, false);

View File

@ -25,7 +25,6 @@ import org.apache.nifi.event.transport.netty.StringNettyEventSenderFactory;
import org.apache.nifi.flowfile.attributes.CoreAttributes;
import org.apache.nifi.provenance.ProvenanceEventRecord;
import org.apache.nifi.provenance.ProvenanceEventType;
import org.apache.nifi.remote.io.socket.NetworkUtils;
import org.apache.nifi.syslog.attributes.SyslogAttributes;
import org.apache.nifi.util.MockFlowFile;
import org.apache.nifi.util.TestRunner;
@ -76,26 +75,25 @@ public class TestListenSyslog {
@Test
public void testRunTcp() throws Exception {
final int port = NetworkUtils.getAvailableTcpPort();
final TransportProtocol protocol = TransportProtocol.TCP;
runner.setProperty(ListenSyslog.PROTOCOL, protocol.toString());
runner.setProperty(ListenSyslog.PORT, Integer.toString(port));
runner.setProperty(ListenSyslog.PORT, "0");
runner.setProperty(ListenSyslog.SOCKET_KEEP_ALIVE, Boolean.FALSE.toString());
assertSendSuccess(protocol, port);
assertSendSuccess(protocol);
}
@Test
public void testRunTcpBatchParseDisabled() throws Exception {
final int port = NetworkUtils.getAvailableTcpPort();
final TransportProtocol protocol = TransportProtocol.TCP;
runner.setProperty(ListenSyslog.PROTOCOL, protocol.toString());
runner.setProperty(ListenSyslog.PORT, Integer.toString(port));
runner.setProperty(ListenSyslog.PORT, "0");
runner.setProperty(ListenSyslog.SOCKET_KEEP_ALIVE, Boolean.FALSE.toString());
runner.setProperty(ListenSyslog.PARSE_MESSAGES, Boolean.FALSE.toString());
runner.setProperty(ListenSyslog.MAX_BATCH_SIZE, "2");
runner.run(1, STOP_ON_FINISH_DISABLED);
final int port = ((ListenSyslog) runner.getProcessor()).getListeningPort();
final String batchedWithEmptyMessages = String.format("%s\n\n%s\n", VALID_MESSAGE, VALID_MESSAGE);
sendMessages(protocol, port, LineEnding.NONE, batchedWithEmptyMessages);
@ -114,20 +112,18 @@ public class TestListenSyslog {
@Test
public void testRunUdp() throws Exception {
final int port = NetworkUtils.getAvailableUdpPort();
final TransportProtocol protocol = TransportProtocol.UDP;
runner.setProperty(ListenSyslog.PROTOCOL, protocol.toString());
runner.setProperty(ListenSyslog.PORT, Integer.toString(port));
runner.setProperty(ListenSyslog.PORT, "0");
assertSendSuccess(protocol, port);
assertSendSuccess(protocol);
}
@Test
public void testRunUdpBatch() throws Exception {
final int port = NetworkUtils.getAvailableUdpPort();
final TransportProtocol protocol = TransportProtocol.UDP;
runner.setProperty(ListenSyslog.PROTOCOL, protocol.toString());
runner.setProperty(ListenSyslog.PORT, Integer.toString(port));
runner.setProperty(ListenSyslog.PORT, "0");
final String[] messages = new String[]{VALID_MESSAGE, VALID_MESSAGE};
@ -135,7 +131,9 @@ public class TestListenSyslog {
runner.setProperty(ListenSyslog.PARSE_MESSAGES, Boolean.FALSE.toString());
runner.run(1, STOP_ON_FINISH_DISABLED);
sendMessages(protocol, port, LineEnding.NONE, messages);
final int listeningPort = ((ListenSyslog) runner.getProcessor()).getListeningPort();
sendMessages(protocol, listeningPort, LineEnding.NONE, messages);
runner.run(1, STOP_ON_FINISH_ENABLED, INITIALIZE_DISABLED);
final List<MockFlowFile> successFlowFiles = runner.getFlowFilesForRelationship(ListenSyslog.REL_SUCCESS);
@ -149,13 +147,14 @@ public class TestListenSyslog {
@Test
public void testRunUdpInvalid() throws Exception {
final int port = NetworkUtils.getAvailableUdpPort();
final TransportProtocol protocol = TransportProtocol.UDP;
runner.setProperty(ListenSyslog.PROTOCOL, protocol.toString());
runner.setProperty(ListenSyslog.PORT, Integer.toString(port));
runner.setProperty(ListenSyslog.PORT, "0");
runner.run(1, STOP_ON_FINISH_DISABLED);
sendMessages(protocol, port, LineEnding.NONE, TIMESTAMP);
final int listeningPort = ((ListenSyslog) runner.getProcessor()).getListeningPort();
sendMessages(protocol, listeningPort, LineEnding.NONE, TIMESTAMP);
runner.run(1, STOP_ON_FINISH_ENABLED, INITIALIZE_DISABLED);
final List<MockFlowFile> invalidFlowFiles = runner.getFlowFilesForRelationship(ListenSyslog.REL_INVALID);
@ -164,15 +163,17 @@ public class TestListenSyslog {
final MockFlowFile flowFile = invalidFlowFiles.iterator().next();
flowFile.assertAttributeEquals(SyslogAttributes.SYSLOG_SENDER.key(), LOCALHOST_ADDRESS);
flowFile.assertAttributeEquals(SyslogAttributes.SYSLOG_PROTOCOL.key(), protocol.toString());
flowFile.assertAttributeEquals(SyslogAttributes.SYSLOG_PORT.key(), Integer.toString(port));
flowFile.assertAttributeEquals(SyslogAttributes.SYSLOG_PORT.key(), Integer.toString(listeningPort));
final String content = flowFile.getContent();
assertEquals(TIMESTAMP, content, "FlowFile content not matched");
}
private void assertSendSuccess(final TransportProtocol protocol, final int port) throws Exception {
private void assertSendSuccess(final TransportProtocol protocol) throws Exception {
runner.run(1, STOP_ON_FINISH_DISABLED);
final int port = ((ListenSyslog) runner.getProcessor()).getListeningPort();
sendMessages(protocol, port, LineEnding.UNIX, VALID_MESSAGE);
runner.run(1, STOP_ON_FINISH_ENABLED, INITIALIZE_DISABLED);

View File

@ -23,7 +23,6 @@ import org.apache.nifi.event.transport.configuration.ShutdownTimeout;
import org.apache.nifi.event.transport.configuration.TransportProtocol;
import org.apache.nifi.event.transport.netty.ByteArrayNettyEventSenderFactory;
import org.apache.nifi.processor.util.listen.ListenerProperties;
import org.apache.nifi.remote.io.socket.NetworkUtils;
import org.apache.nifi.reporting.InitializationException;
import org.apache.nifi.security.util.ClientAuth;
import org.apache.nifi.security.util.TlsException;
@ -33,8 +32,8 @@ import org.apache.nifi.util.MockFlowFile;
import org.apache.nifi.util.TestRunner;
import org.apache.nifi.util.TestRunners;
import org.apache.nifi.web.util.ssl.SslContextUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
@ -169,14 +168,14 @@ public class TestListenTCP {
}
}
private void run(final List<String> messages, final int flowFiles, final SSLContext sslContext)
throws Exception {
final int port = NetworkUtils.availablePort();
runner.setProperty(ListenerProperties.PORT, Integer.toString(port));
private void run(final List<String> messages, final int flowFiles, final SSLContext sslContext) throws Exception {
runner.setProperty(ListenerProperties.PORT, "0");
final String message = StringUtils.join(messages, null);
final byte[] bytes = message.getBytes(StandardCharsets.UTF_8);
runner.run(1, false, true);
final int port = ((ListenTCP) runner.getProcessor()).getListeningPort();
sendMessages(port, bytes, sslContext);
runner.run(flowFiles, false, false);
}

View File

@ -17,7 +17,6 @@
package org.apache.nifi.processors.standard;
import org.apache.nifi.json.JsonTreeReader;
import org.apache.nifi.remote.io.socket.NetworkUtils;
import org.apache.nifi.reporting.InitializationException;
import org.apache.nifi.schema.access.SchemaAccessUtils;
import org.apache.nifi.security.util.ClientAuth;
@ -215,11 +214,11 @@ public class TestListenTCPRecord {
}
protected void run(final int expectedTransferred, final SSLContext sslContext) throws IOException, InterruptedException {
final int port = NetworkUtils.availablePort();
runner.setProperty(ListenTCPRecord.PORT, Integer.toString(port));
runner.setProperty(ListenTCPRecord.PORT, "0");
// Run Processor and start listener without shutting down
runner.run(1, false, true);
final int port = ((ListenTCPRecord) runner.getProcessor()).getListeningPort();
final Thread thread = new Thread(() -> {
try (final Socket socket = getSocket(port, sslContext)) {

View File

@ -25,7 +25,6 @@ import org.apache.nifi.processor.util.listen.event.StandardEvent;
import org.apache.nifi.processor.util.listen.response.ChannelResponder;
import org.apache.nifi.provenance.ProvenanceEventRecord;
import org.apache.nifi.provenance.ProvenanceEventType;
import org.apache.nifi.remote.io.socket.NetworkUtils;
import org.apache.nifi.util.MockFlowFile;
import org.apache.nifi.util.TestRunner;
import org.apache.nifi.util.TestRunners;
@ -54,7 +53,6 @@ public class TestListenUDP {
private static final String LOCALHOST = "localhost";
private int port = 0;
private TestRunner runner;
@ -64,8 +62,7 @@ public class TestListenUDP {
@BeforeEach
public void setUp() throws Exception {
runner = TestRunners.newTestRunner(ListenUDP.class);
port = NetworkUtils.getAvailableUdpPort();
runner.setProperty(ListenUDP.PORT, Integer.toString(port));
runner.setProperty(ListenUDP.PORT, "0");
}
@Test
@ -173,13 +170,13 @@ public class TestListenUDP {
@Test
public void testWithSendingHostAndPortSameAsSender() throws IOException, InterruptedException {
final Integer sendingPort = NetworkUtils.getAvailableUdpPort();
// bind to the same sending port that processor has for Sending Host Port
final DatagramSocket socket = new DatagramSocket();
final int sendingPort = socket.getLocalPort();
runner.setProperty(ListenUDP.SENDING_HOST, LOCALHOST);
runner.setProperty(ListenUDP.SENDING_HOST_PORT, String.valueOf(sendingPort));
// bind to the same sending port that processor has for Sending Host Port
final DatagramSocket socket = new DatagramSocket(sendingPort);
final List<String> messages = getMessages(6);
final int expectedTransferred = messages.size();
@ -200,6 +197,8 @@ public class TestListenUDP {
}
private void verifyFlowFiles(List<MockFlowFile> mockFlowFiles) {
final int port = ((ListenUDP) runner.getProcessor()).getListeningPort();
for (int i = 0; i < mockFlowFiles.size(); i++) {
MockFlowFile flowFile = mockFlowFiles.get(i);
flowFile.assertContentEquals("This is message " + (i + 1));
@ -222,6 +221,7 @@ public class TestListenUDP {
throws IOException, InterruptedException {
// Run Processor and start Dispatcher without shutting down
runner.run(1, false, true);
final int port = ((ListenUDP) runner.getProcessor()).getListeningPort();
try {
final InetSocketAddress destination = new InetSocketAddress(LOCALHOST, port);

View File

@ -23,11 +23,12 @@ import org.apache.nifi.event.transport.configuration.TransportProtocol;
import org.apache.nifi.event.transport.message.ByteArrayMessage;
import org.apache.nifi.event.transport.netty.ByteArrayMessageNettyEventServerFactory;
import org.apache.nifi.event.transport.netty.NettyEventServerFactory;
import org.apache.nifi.mock.MockComponentLogger;
import org.apache.nifi.provenance.ProvenanceEventRecord;
import org.apache.nifi.provenance.ProvenanceEventType;
import org.apache.nifi.remote.io.socket.NetworkUtils;
import org.apache.nifi.util.TestRunner;
import org.apache.nifi.util.TestRunners;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@ -81,11 +82,21 @@ public class TestPutSyslog {
private InetAddress address;
private int port;
private EventServer eventServer;
private BlockingQueue<ByteArrayMessage> messages;
@BeforeEach
public void setRunner() throws UnknownHostException {
final byte[] delimiter = DELIMITER.getBytes(CHARSET);
messages = new LinkedBlockingQueue<>();
address = InetAddress.getByName(ADDRESS);
port = NetworkUtils.getAvailableUdpPort();
final NettyEventServerFactory serverFactory = new ByteArrayMessageNettyEventServerFactory(new MockComponentLogger(), address, 0, protocol, delimiter, MAX_FRAME_LENGTH, messages);
serverFactory.setShutdownQuietPeriod(ShutdownQuietPeriod.QUICK.getDuration());
serverFactory.setShutdownTimeout(ShutdownTimeout.QUICK.getDuration());
this.eventServer = serverFactory.getEventServer();
this.port = eventServer.getListeningPort();
runner = TestRunners.newTestRunner(PutSyslog.class);
runner.setProperty(PutSyslog.HOSTNAME, ADDRESS);
runner.setProperty(PutSyslog.PROTOCOL, protocol.toString());
@ -97,6 +108,13 @@ public class TestPutSyslog {
runner.assertValid();
}
@AfterEach
public void shutdownEventServer() {
if (eventServer != null) {
eventServer.shutdown();
}
}
@Test
public void testRunNoFlowFiles() {
runner.run();
@ -128,20 +146,14 @@ public class TestPutSyslog {
@Test
public void testRunFailure() {
runner.setProperty(PutSyslog.PROTOCOL, PutSyslog.TCP_VALUE);
runner.setProperty(PutSyslog.PORT, Integer.toString(NetworkUtils.getAvailableTcpPort()));
runner.setProperty(PutSyslog.HOSTNAME, "this-host-does-not-exist");
runner.setProperty(PutSyslog.PORT, "44");
runner.enqueue(new byte[]{});
runner.run();
runner.assertAllFlowFilesTransferred(PutSyslog.REL_FAILURE);
}
private void assertSyslogMessageSuccess(final String expectedSyslogMessage, final Map<String, String> attributes) throws InterruptedException {
final BlockingQueue<ByteArrayMessage> messages = new LinkedBlockingQueue<>();
final byte[] delimiter = DELIMITER.getBytes(CHARSET);
final NettyEventServerFactory serverFactory = new ByteArrayMessageNettyEventServerFactory(runner.getLogger(), address, port, protocol, delimiter, MAX_FRAME_LENGTH, messages);
serverFactory.setShutdownQuietPeriod(ShutdownQuietPeriod.QUICK.getDuration());
serverFactory.setShutdownTimeout(ShutdownTimeout.QUICK.getDuration());
final EventServer eventServer = serverFactory.getEventServer();
try {
runner.enqueue(expectedSyslogMessage, attributes);
runner.run();

View File

@ -24,7 +24,6 @@ import org.apache.nifi.event.transport.configuration.TransportProtocol;
import org.apache.nifi.event.transport.message.ByteArrayMessage;
import org.apache.nifi.event.transport.netty.ByteArrayMessageNettyEventServerFactory;
import org.apache.nifi.event.transport.netty.NettyEventServerFactory;
import org.apache.nifi.remote.io.socket.NetworkUtils;
import org.apache.nifi.security.util.TemporaryKeyStoreBuilder;
import org.apache.nifi.security.util.TlsConfiguration;
import org.apache.nifi.ssl.SSLContextService;
@ -39,6 +38,7 @@ import org.mockito.Mockito;
import javax.net.ssl.SSLContext;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
@ -69,7 +69,6 @@ public class TestPutTCP {
private final static String[] VALID_FILES = { "abcdefghijklmnopqrstuvwxyz", "zyxwvutsrqponmlkjihgfedcba", "12345678", "343424222", "!@£$%^&*()_+:|{}[];\\" };
private EventServer eventServer;
private int port;
private TestRunner runner;
private BlockingQueue<ByteArrayMessage> messages;
@ -77,7 +76,6 @@ public class TestPutTCP {
public void setup() throws Exception {
runner = TestRunners.newTestRunner(PutTCP.class);
runner.setVariable(SERVER_VARIABLE, TCP_SERVER_ADDRESS);
port = NetworkUtils.getAvailableTcpPort();
}
@AfterEach
@ -86,25 +84,10 @@ public class TestPutTCP {
shutdownServer();
}
@Test
public void testPortProperty() {
runner.setProperty(PutTCP.PORT, Integer.toString(MIN_INVALID_PORT));
runner.assertNotValid();
runner.setProperty(PutTCP.PORT, Integer.toString(MIN_VALID_PORT));
runner.assertValid();
runner.setProperty(PutTCP.PORT, Integer.toString(MAX_VALID_PORT));
runner.assertValid();
runner.setProperty(PutTCP.PORT, Integer.toString(MAX_INVALID_PORT));
runner.assertNotValid();
}
@Test
public void testRunSuccess() throws Exception {
createTestServer(OUTGOING_MESSAGE_DELIMITER);
configureProperties(TCP_SERVER_ADDRESS, OUTGOING_MESSAGE_DELIMITER, false);
createTestServer(port);
sendTestData(VALID_FILES);
assertMessagesReceived(VALID_FILES);
}
@ -122,24 +105,24 @@ public class TestPutTCP {
runner.addControllerService(identifier, sslContextService);
runner.enableControllerService(sslContextService);
runner.setProperty(PutTCP.SSL_CONTEXT_SERVICE, identifier);
createTestServer(sslContext, OUTGOING_MESSAGE_DELIMITER);
configureProperties(TCP_SERVER_ADDRESS, OUTGOING_MESSAGE_DELIMITER, false);
createTestServer(port, sslContext);
sendTestData(VALID_FILES);
assertMessagesReceived(VALID_FILES);
}
@Test
public void testRunSuccessServerVariableExpression() throws Exception {
createTestServer(OUTGOING_MESSAGE_DELIMITER);
configureProperties(TCP_SERVER_ADDRESS_EL, OUTGOING_MESSAGE_DELIMITER, false);
createTestServer(port);
sendTestData(VALID_FILES);
assertMessagesReceived(VALID_FILES);
}
@Test
public void testRunSuccessPruneSenders() throws Exception {
createTestServer(OUTGOING_MESSAGE_DELIMITER);
configureProperties(TCP_SERVER_ADDRESS, OUTGOING_MESSAGE_DELIMITER, false);
createTestServer(port);
sendTestData(VALID_FILES);
assertTransfers(VALID_FILES.length);
assertMessagesReceived(VALID_FILES);
@ -153,24 +136,24 @@ public class TestPutTCP {
@Test
public void testRunSuccessMultiCharDelimiter() throws Exception {
createTestServer(OUTGOING_MESSAGE_DELIMITER_MULTI_CHAR);
configureProperties(TCP_SERVER_ADDRESS, OUTGOING_MESSAGE_DELIMITER_MULTI_CHAR, false);
createTestServer(port);
sendTestData(VALID_FILES);
assertMessagesReceived(VALID_FILES);
}
@Test
public void testRunSuccessConnectionPerFlowFile() throws Exception {
createTestServer(OUTGOING_MESSAGE_DELIMITER);
configureProperties(TCP_SERVER_ADDRESS, OUTGOING_MESSAGE_DELIMITER, true);
createTestServer(port);
sendTestData(VALID_FILES);
assertMessagesReceived(VALID_FILES);
}
@Test
public void testRunSuccessConnectionFailure() throws Exception {
createTestServer(OUTGOING_MESSAGE_DELIMITER);
configureProperties(TCP_SERVER_ADDRESS, OUTGOING_MESSAGE_DELIMITER, false);
createTestServer(port);
sendTestData(VALID_FILES);
assertMessagesReceived(VALID_FILES);
@ -178,16 +161,16 @@ public class TestPutTCP {
sendTestData(VALID_FILES);
runner.assertQueueEmpty();
createTestServer(OUTGOING_MESSAGE_DELIMITER);
configureProperties(TCP_SERVER_ADDRESS, OUTGOING_MESSAGE_DELIMITER, false);
createTestServer(port);
sendTestData(VALID_FILES);
assertMessagesReceived(VALID_FILES);
}
@Test
public void testRunSuccessEmptyFile() throws Exception {
createTestServer(OUTGOING_MESSAGE_DELIMITER);
configureProperties(TCP_SERVER_ADDRESS, OUTGOING_MESSAGE_DELIMITER, false);
createTestServer(port);
sendTestData(EMPTY_FILE);
assertTransfers(1);
runner.assertQueueEmpty();
@ -195,8 +178,8 @@ public class TestPutTCP {
@Test
public void testRunSuccessLargeValidFile() throws Exception {
createTestServer(OUTGOING_MESSAGE_DELIMITER);
configureProperties(TCP_SERVER_ADDRESS, OUTGOING_MESSAGE_DELIMITER, true);
createTestServer(port);
final String[] testData = createContent(VALID_LARGE_FILE_SIZE);
sendTestData(testData);
assertMessagesReceived(testData);
@ -204,23 +187,22 @@ public class TestPutTCP {
@Test
public void testRunSuccessFiveHundredMessages() throws Exception {
createTestServer(OUTGOING_MESSAGE_DELIMITER);
configureProperties(TCP_SERVER_ADDRESS, OUTGOING_MESSAGE_DELIMITER, false);
createTestServer(port);
final String[] testData = createContent(VALID_SMALL_FILE_SIZE);
sendTestData(testData, LOAD_TEST_ITERATIONS, LOAD_TEST_THREAD_COUNT);
assertMessagesReceived(testData, LOAD_TEST_ITERATIONS);
}
private void createTestServer(final int port) throws Exception {
createTestServer(port, null);
private void createTestServer(final String delimiter) throws UnknownHostException {
createTestServer(null, delimiter);
}
private void createTestServer(final int port, final SSLContext sslContext) throws Exception {
private void createTestServer(final SSLContext sslContext, final String delimiter) throws UnknownHostException {
messages = new LinkedBlockingQueue<>();
final byte[] delimiter = getDelimiter();
final InetAddress listenAddress = InetAddress.getByName(TCP_SERVER_ADDRESS);
NettyEventServerFactory serverFactory = new ByteArrayMessageNettyEventServerFactory(runner.getLogger(),
listenAddress, port, TransportProtocol.TCP, delimiter, VALID_LARGE_FILE_SIZE, messages);
listenAddress, 0, TransportProtocol.TCP, delimiter.getBytes(), VALID_LARGE_FILE_SIZE, messages);
if (sslContext != null) {
serverFactory.setSslContext(sslContext);
}
@ -235,9 +217,9 @@ public class TestPutTCP {
}
}
private void configureProperties(String host, String outgoingMessageDelimiter, boolean connectionPerFlowFile) {
private void configureProperties(final String host, final String outgoingMessageDelimiter, final boolean connectionPerFlowFile) {
runner.setProperty(PutTCP.HOSTNAME, host);
runner.setProperty(PutTCP.PORT, Integer.toString(port));
runner.setProperty(PutTCP.PORT, String.valueOf(eventServer.getListeningPort()));
if (outgoingMessageDelimiter != null) {
runner.setProperty(PutTCP.OUTGOING_MESSAGE_DELIMITER, outgoingMessageDelimiter);
@ -296,12 +278,4 @@ public class TestPutTCP {
return new String[] { new String(content) };
}
private byte[] getDelimiter() {
String delimiter = runner.getProcessContext().getProperty(PutTCP.OUTGOING_MESSAGE_DELIMITER).getValue();
if (delimiter != null) {
return delimiter.getBytes();
} else {
return null;
}
}
}

View File

@ -23,7 +23,6 @@ import org.apache.nifi.event.transport.configuration.TransportProtocol;
import org.apache.nifi.event.transport.message.ByteArrayMessage;
import org.apache.nifi.event.transport.netty.ByteArrayMessageNettyEventServerFactory;
import org.apache.nifi.event.transport.netty.NettyEventServerFactory;
import org.apache.nifi.remote.io.socket.NetworkUtils;
import org.apache.nifi.util.TestRunner;
import org.apache.nifi.util.TestRunners;
import org.junit.jupiter.api.AfterEach;
@ -65,8 +64,7 @@ public class TestPutUDP {
public void setup() throws Exception {
runner = TestRunners.newTestRunner(PutUDP.class);
runner.setVariable(SERVER_VARIABLE, UDP_SERVER_ADDRESS);
port = NetworkUtils.getAvailableUdpPort();
createTestServer(port, VALID_LARGE_FILE_SIZE);
createTestServer(VALID_LARGE_FILE_SIZE);
}
@AfterEach
@ -116,12 +114,12 @@ public class TestPutUDP {
configureProperties();
sendMessages(VALID_FILES);
assertMessagesReceived(VALID_FILES);
reset(port);
reset();
configureProperties();
sendMessages(VALID_FILES);
assertMessagesReceived(VALID_FILES);
reset(port);
reset();
configureProperties();
sendMessages(VALID_FILES);
@ -129,10 +127,10 @@ public class TestPutUDP {
runner.assertQueueEmpty();
}
private void reset(final int port) throws Exception {
private void reset() throws Exception {
runner.clearTransferState();
removeTestServer();
createTestServer(port, MAX_FRAME_LENGTH);
createTestServer(MAX_FRAME_LENGTH);
}
private void configureProperties() {
@ -181,7 +179,7 @@ public class TestPutUDP {
return new String[] { new String(content).concat("\n") };
}
private void createTestServer(final int port, final int frameSize) throws Exception {
private void createTestServer(final int frameSize) throws Exception {
messages = new LinkedBlockingQueue<>();
final byte[] delimiter = DELIMITER.getBytes(CHARSET);
final InetAddress listenAddress = InetAddress.getByName(UDP_SERVER_ADDRESS);
@ -191,6 +189,7 @@ public class TestPutUDP {
serverFactory.setShutdownQuietPeriod(ShutdownQuietPeriod.QUICK.getDuration());
serverFactory.setShutdownTimeout(ShutdownTimeout.QUICK.getDuration());
eventServer = serverFactory.getEventServer();
this.port = eventServer.getListeningPort();
}
private void removeTestServer() {

View File

@ -16,13 +16,12 @@
*/
package org.apache.nifi.processors.standard.ftp;
import org.apache.nifi.remote.io.socket.NetworkUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import javax.net.SocketFactory;
@ -54,13 +53,10 @@ public class ProxyFTPClientTest {
private static final String WELCOME_REPLY = "220 Welcome";
private int port;
private ProxyFTPClient client;
@BeforeEach
public void setClient() {
port = NetworkUtils.getAvailableTcpPort();
client = new ProxyFTPClient(socketFactory);
}
@ -70,13 +66,13 @@ public class ProxyFTPClientTest {
when(socket.getInputStream()).thenReturn(new ByteArrayInputStream(WELCOME_REPLY.getBytes(StandardCharsets.US_ASCII)));
when(socket.getOutputStream()).thenReturn(new ByteArrayOutputStream());
client.connect(HOST, port);
client.connect(HOST, 0);
verify(socket).connect(socketAddressCaptor.capture(), anyInt());
final InetSocketAddress socketAddress = socketAddressCaptor.getValue();
assertNotNull(socketAddress);
assertEquals(HOST, socketAddress.getHostString());
assertEquals(port, socketAddress.getPort());
assertEquals(0, socketAddress.getPort());
}
}

View File

@ -1,101 +0,0 @@
/*
* 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.processors.standard.ftp;
import org.apache.nifi.components.PropertyValue;
import org.apache.nifi.context.PropertyContext;
import org.apache.nifi.processors.standard.socket.ClientConnectException;
import org.apache.nifi.remote.io.socket.NetworkUtils;
import org.apache.nifi.util.MockPropertyValue;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.Collections;
import static org.apache.nifi.processors.standard.util.FTPTransfer.BUFFER_SIZE;
import static org.apache.nifi.processors.standard.util.FTPTransfer.HTTP_PROXY_PASSWORD;
import static org.apache.nifi.processors.standard.util.FTPTransfer.HTTP_PROXY_USERNAME;
import static org.apache.nifi.processors.standard.util.FTPTransfer.PORT;
import static org.apache.nifi.processors.standard.util.FTPTransfer.PROXY_HOST;
import static org.apache.nifi.processors.standard.util.FTPTransfer.PROXY_PORT;
import static org.apache.nifi.processors.standard.util.FTPTransfer.PROXY_TYPE;
import static org.apache.nifi.processors.standard.util.FTPTransfer.PROXY_TYPE_DIRECT;
import static org.apache.nifi.processors.standard.util.FTPTransfer.PROXY_CONFIGURATION_SERVICE;
import static org.apache.nifi.processors.standard.util.FTPTransfer.UTF8_ENCODING;
import static org.apache.nifi.processors.standard.util.FileTransfer.CONNECTION_TIMEOUT;
import static org.apache.nifi.processors.standard.util.FileTransfer.DATA_TIMEOUT;
import static org.apache.nifi.processors.standard.util.FileTransfer.HOSTNAME;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
public class StandardFTPClientProviderTest {
private static final PropertyValue NULL_PROPERTY_VALUE = new MockPropertyValue(null);
private static final PropertyValue BOOLEAN_TRUE_PROPERTY_VALUE = new MockPropertyValue(Boolean.TRUE.toString());
private static final PropertyValue BUFFER_SIZE_PROPERTY_VALUE = new MockPropertyValue("1 KB");
private static final PropertyValue TIMEOUT_PROPERTY_VALUE = new MockPropertyValue("2 s");
private static final String LOCALHOST = "localhost";
private static final PropertyValue HOSTNAME_PROPERTY = new MockPropertyValue(LOCALHOST);
@Mock
private PropertyContext context;
private StandardFTPClientProvider provider;
private int port;
@BeforeEach
public void setProvider() {
when(context.getProperty(any())).thenReturn(BOOLEAN_TRUE_PROPERTY_VALUE);
when(context.getProperty(UTF8_ENCODING)).thenReturn(BOOLEAN_TRUE_PROPERTY_VALUE);
when(context.getProperty(BUFFER_SIZE)).thenReturn(BUFFER_SIZE_PROPERTY_VALUE);
when(context.getProperty(CONNECTION_TIMEOUT)).thenReturn(TIMEOUT_PROPERTY_VALUE);
when(context.getProperty(DATA_TIMEOUT)).thenReturn(TIMEOUT_PROPERTY_VALUE);
when(context.getProperty(PROXY_CONFIGURATION_SERVICE)).thenReturn(NULL_PROPERTY_VALUE);
when(context.getProperty(PROXY_TYPE)).thenReturn(new MockPropertyValue(PROXY_TYPE_DIRECT));
when(context.getProperty(PROXY_HOST)).thenReturn(NULL_PROPERTY_VALUE);
when(context.getProperty(PROXY_PORT)).thenReturn(NULL_PROPERTY_VALUE);
when(context.getProperty(HTTP_PROXY_USERNAME)).thenReturn(NULL_PROPERTY_VALUE);
when(context.getProperty(HTTP_PROXY_PASSWORD)).thenReturn(NULL_PROPERTY_VALUE);
when(context.getProperty(HOSTNAME)).thenReturn(HOSTNAME_PROPERTY);
port = NetworkUtils.getAvailableTcpPort();
when(context.getProperty(PORT)).thenReturn(new MockPropertyValue(Integer.toString(port)));
provider = new StandardFTPClientProvider();
}
@Test
public void testGetClientConnectException() {
final ClientConnectException exception = assertThrows(ClientConnectException.class, () -> provider.getClient(context, Collections.emptyMap()));
assertTrue(exception.getMessage().contains(LOCALHOST));
assertTrue(exception.getMessage().contains(Integer.toString(port)));
}
}

View File

@ -1,119 +0,0 @@
/*
* 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.processors.standard.ssh;
import org.apache.nifi.components.PropertyValue;
import org.apache.nifi.context.PropertyContext;
import org.apache.nifi.processors.standard.socket.ClientConfigurationException;
import org.apache.nifi.processors.standard.socket.ClientConnectException;
import org.apache.nifi.remote.io.socket.NetworkUtils;
import org.apache.nifi.util.MockPropertyValue;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.Collections;
import static org.apache.nifi.processors.standard.util.FTPTransfer.PORT;
import static org.apache.nifi.processors.standard.util.FTPTransfer.PROXY_TYPE;
import static org.apache.nifi.processors.standard.util.FTPTransfer.PROXY_HOST;
import static org.apache.nifi.processors.standard.util.FTPTransfer.PROXY_PORT;
import static org.apache.nifi.processors.standard.util.FTPTransfer.HTTP_PROXY_USERNAME;
import static org.apache.nifi.processors.standard.util.FTPTransfer.HTTP_PROXY_PASSWORD;
import static org.apache.nifi.processors.standard.util.FTPTransfer.PROXY_TYPE_DIRECT;
import static org.apache.nifi.processors.standard.util.FileTransfer.CONNECTION_TIMEOUT;
import static org.apache.nifi.processors.standard.util.FileTransfer.DATA_TIMEOUT;
import static org.apache.nifi.processors.standard.util.FileTransfer.HOSTNAME;
import static org.apache.nifi.processors.standard.util.FileTransfer.USE_COMPRESSION;
import static org.apache.nifi.processors.standard.util.SFTPTransfer.CIPHERS_ALLOWED;
import static org.apache.nifi.processors.standard.util.SFTPTransfer.HOST_KEY_FILE;
import static org.apache.nifi.processors.standard.util.SFTPTransfer.KEY_ALGORITHMS_ALLOWED;
import static org.apache.nifi.processors.standard.util.SFTPTransfer.KEY_EXCHANGE_ALGORITHMS_ALLOWED;
import static org.apache.nifi.processors.standard.util.SFTPTransfer.MESSAGE_AUTHENTICATION_CODES_ALLOWED;
import static org.apache.nifi.processors.standard.util.SFTPTransfer.PROXY_CONFIGURATION_SERVICE;
import static org.apache.nifi.processors.standard.util.SFTPTransfer.STRICT_HOST_KEY_CHECKING;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
public class StandardSSHClientProviderTest {
private static final PropertyValue NULL_PROPERTY_VALUE = new MockPropertyValue(null);
private static final PropertyValue BOOLEAN_TRUE_PROPERTY_VALUE = new MockPropertyValue(Boolean.TRUE.toString());
private static final PropertyValue BOOLEAN_FALSE_PROPERTY_VALUE = new MockPropertyValue(Boolean.FALSE.toString());
private static final PropertyValue TIMEOUT_PROPERTY_VALUE = new MockPropertyValue("2 s");
private static final String LOCALHOST = "localhost";
private static final PropertyValue HOSTNAME_PROPERTY = new MockPropertyValue(LOCALHOST);
@Mock
private PropertyContext context;
private StandardSSHClientProvider provider;
private int port;
@BeforeEach
public void setProvider() {
when(context.getProperty(any())).thenReturn(BOOLEAN_TRUE_PROPERTY_VALUE);
when(context.getProperty(CIPHERS_ALLOWED)).thenReturn(NULL_PROPERTY_VALUE);
when(context.getProperty(KEY_ALGORITHMS_ALLOWED)).thenReturn(NULL_PROPERTY_VALUE);
when(context.getProperty(KEY_EXCHANGE_ALGORITHMS_ALLOWED)).thenReturn(NULL_PROPERTY_VALUE);
when(context.getProperty(MESSAGE_AUTHENTICATION_CODES_ALLOWED)).thenReturn(NULL_PROPERTY_VALUE);
when(context.getProperty(CONNECTION_TIMEOUT)).thenReturn(TIMEOUT_PROPERTY_VALUE);
when(context.getProperty(DATA_TIMEOUT)).thenReturn(TIMEOUT_PROPERTY_VALUE);
when(context.getProperty(STRICT_HOST_KEY_CHECKING)).thenReturn(BOOLEAN_FALSE_PROPERTY_VALUE);
when(context.getProperty(HOST_KEY_FILE)).thenReturn(NULL_PROPERTY_VALUE);
when(context.getProperty(USE_COMPRESSION)).thenReturn(BOOLEAN_FALSE_PROPERTY_VALUE);
when(context.getProperty(PROXY_CONFIGURATION_SERVICE)).thenReturn(NULL_PROPERTY_VALUE);
when(context.getProperty(PROXY_TYPE)).thenReturn(new MockPropertyValue(PROXY_TYPE_DIRECT));
when(context.getProperty(PROXY_HOST)).thenReturn(NULL_PROPERTY_VALUE);
when(context.getProperty(PROXY_PORT)).thenReturn(NULL_PROPERTY_VALUE);
when(context.getProperty(HTTP_PROXY_USERNAME)).thenReturn(NULL_PROPERTY_VALUE);
when(context.getProperty(HTTP_PROXY_PASSWORD)).thenReturn(NULL_PROPERTY_VALUE);
when(context.getProperty(HOSTNAME)).thenReturn(HOSTNAME_PROPERTY);
port = NetworkUtils.getAvailableTcpPort();
when(context.getProperty(PORT)).thenReturn(new MockPropertyValue(Integer.toString(port)));
provider = new StandardSSHClientProvider();
}
@Test
public void testGetClientConfigurationException() {
final ClientConfigurationException exception = assertThrows(ClientConfigurationException.class, () -> provider.getClient(context, Collections.emptyMap()));
assertTrue(exception.getMessage().contains(LOCALHOST));
assertTrue(exception.getMessage().contains(Integer.toString(port)));
}
@Test
public void testGetClientConnectException() {
final ClientConnectException exception = assertThrows(ClientConnectException.class, () -> provider.getClient(context, Collections.emptyMap()));
assertTrue(exception.getMessage().contains(LOCALHOST));
assertTrue(exception.getMessage().contains(Integer.toString(port)));
}
}

View File

@ -36,7 +36,7 @@ public abstract class EventCacheServer implements CacheServer {
private final ComponentLog log;
private final int port;
private volatile int port;
private EventServer eventServer;
@ -55,6 +55,7 @@ public abstract class EventCacheServer implements CacheServer {
@Override
public void start() {
eventServer = createEventServer();
port = eventServer.getListeningPort();
log.info("Started Cache Server Port [{}]", port);
}

View File

@ -19,11 +19,10 @@ package org.apache.nifi.distributed.cache.server.map;
import org.apache.commons.lang3.SerializationException;
import org.apache.nifi.distributed.cache.client.AtomicCacheEntry;
import org.apache.nifi.distributed.cache.client.Deserializer;
import org.apache.nifi.distributed.cache.client.DistributedMapCacheClientService;
import org.apache.nifi.distributed.cache.client.Serializer;
import org.apache.nifi.distributed.cache.client.exception.DeserializationException;
import org.apache.nifi.distributed.cache.client.DistributedMapCacheClientService;
import org.apache.nifi.processor.Processor;
import org.apache.nifi.remote.io.socket.NetworkUtils;
import org.apache.nifi.util.TestRunner;
import org.apache.nifi.util.TestRunners;
import org.junit.jupiter.api.AfterAll;
@ -56,18 +55,18 @@ public class DistributedMapCacheTest {
@BeforeAll
public static void startServices() throws Exception {
final String port = Integer.toString(NetworkUtils.getAvailableTcpPort());
runner = TestRunners.newTestRunner(Mockito.mock(Processor.class));
server = new DistributedMapCacheServer();
runner.addControllerService(server.getClass().getName(), server);
runner.setProperty(server, DistributedMapCacheServer.PORT, port);
runner.setProperty(server, DistributedMapCacheServer.PORT, "0");
runner.enableControllerService(server);
final int port = server.getPort();
client = new DistributedMapCacheClientService();
runner.addControllerService(client.getClass().getName(), client);
runner.setProperty(client, DistributedMapCacheClientService.HOSTNAME, "localhost");
runner.setProperty(client, DistributedMapCacheClientService.PORT, port);
runner.setProperty(client, DistributedMapCacheClientService.PORT, String.valueOf(port));
runner.enableControllerService(client);
}

View File

@ -22,7 +22,6 @@ import org.apache.nifi.distributed.cache.client.DistributedMapCacheClientService
import org.apache.nifi.distributed.cache.client.Serializer;
import org.apache.nifi.distributed.cache.client.exception.DeserializationException;
import org.apache.nifi.processor.Processor;
import org.apache.nifi.remote.io.socket.NetworkUtils;
import org.apache.nifi.security.util.SslContextFactory;
import org.apache.nifi.security.util.TemporaryKeyStoreBuilder;
import org.apache.nifi.security.util.TlsConfiguration;
@ -55,7 +54,6 @@ public class DistributedMapCacheTlsTest {
@BeforeAll
public static void setServices() throws Exception {
final String port = Integer.toString(NetworkUtils.getAvailableTcpPort());
runner = TestRunners.newTestRunner(Mockito.mock(Processor.class));
sslContextService = createSslContextService();
runner.addControllerService(sslContextService.getIdentifier(), sslContextService);
@ -63,14 +61,15 @@ public class DistributedMapCacheTlsTest {
server = new DistributedMapCacheServer();
runner.addControllerService(server.getClass().getName(), server);
runner.setProperty(server, DistributedMapCacheServer.PORT, port);
runner.setProperty(server, DistributedMapCacheServer.PORT, "0");
runner.setProperty(server, DistributedMapCacheServer.SSL_CONTEXT_SERVICE, sslContextService.getIdentifier());
runner.enableControllerService(server);
final int listeningPort = server.getPort();
client = new DistributedMapCacheClientService();
runner.addControllerService(client.getClass().getName(), client);
runner.setProperty(client, DistributedMapCacheClientService.HOSTNAME, "localhost");
runner.setProperty(client, DistributedMapCacheClientService.PORT, port);
runner.setProperty(client, DistributedMapCacheClientService.PORT, String.valueOf(listeningPort));
runner.setProperty(client, DistributedMapCacheClientService.SSL_CONTEXT_SERVICE, sslContextService.getIdentifier());
runner.enableControllerService(client);
}

View File

@ -21,7 +21,6 @@ import org.apache.nifi.distributed.cache.protocol.ProtocolHandshake;
import org.apache.nifi.distributed.cache.protocol.ProtocolVersion;
import org.apache.nifi.distributed.cache.server.EvictionPolicy;
import org.apache.nifi.logging.ComponentLog;
import org.apache.nifi.remote.io.socket.NetworkUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@ -77,12 +76,11 @@ class StandardMapCacheServerTest {
@BeforeEach
void setServer() throws IOException {
final int port = NetworkUtils.getAvailableTcpPort();
server = new StandardMapCacheServer(
log,
IDENTIFIER,
SSL_CONTEXT_DISABLED,
port,
0,
MAX_CACHE_ENTRIES,
EVICTION_POLICY,
PERSISTENCE_PATH_DISABLED,

View File

@ -30,7 +30,6 @@ import org.apache.nifi.event.transport.netty.NettyEventServerFactory;
import org.apache.nifi.logging.ComponentLog;
import org.apache.nifi.remote.StandardVersionNegotiator;
import org.apache.nifi.remote.VersionNegotiator;
import org.apache.nifi.remote.io.socket.NetworkUtils;
import org.apache.nifi.reporting.InitializationException;
import org.apache.nifi.util.NoOpProcessor;
import org.apache.nifi.util.TestRunner;
@ -65,7 +64,6 @@ public class TestDistributedMapCacheClientService {
@BeforeEach
public void setRunner() throws UnknownHostException {
runner = TestRunners.newTestRunner(NoOpProcessor.class);
port = NetworkUtils.getAvailableTcpPort();
final InetAddress serverAddress = InetAddress.getByName(LOCALHOST);
final NettyEventServerFactory serverFactory = new NettyEventServerFactory(serverAddress, port, TransportProtocol.TCP);
@ -80,6 +78,7 @@ public class TestDistributedMapCacheClientService {
new CacheVersionRequestHandler(log, versionNegotiator)
));
server = serverFactory.getEventServer();
port = server.getListeningPort();
}
@AfterEach

View File

@ -40,7 +40,6 @@ import org.apache.nifi.logging.ComponentLog;
import org.apache.nifi.processor.DataUnit;
import org.apache.nifi.processor.Processor;
import org.apache.nifi.remote.StandardVersionNegotiator;
import org.apache.nifi.remote.io.socket.NetworkUtils;
import org.apache.nifi.reporting.InitializationException;
import org.apache.nifi.util.MockConfigurationContext;
import org.apache.nifi.util.MockControllerServiceInitializationContext;
@ -52,13 +51,12 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.net.ssl.SSLContext;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
@ -91,8 +89,7 @@ public class TestDistributedMapServerAndClient {
server = new DistributedMapCacheServer();
runner.addControllerService("server", server);
final int port = NetworkUtils.getAvailableTcpPort();
runner.setProperty(server, DistributedMapCacheServer.PORT, Integer.toString(port));
runner.setProperty(server, DistributedMapCacheServer.PORT, "0");
}
@AfterEach
@ -249,8 +246,7 @@ public class TestDistributedMapServerAndClient {
}
};
runner.addControllerService("server", server);
final int port = NetworkUtils.getAvailableTcpPort();
runner.setProperty(server, DistributedMapCacheServer.PORT, Integer.toString(port));
runner.setProperty(server, DistributedMapCacheServer.PORT, "0");
runner.enableControllerService(server);
DistributedMapCacheClientService client = new DistributedMapCacheClientService();
@ -313,19 +309,16 @@ public class TestDistributedMapServerAndClient {
@Test
public void testIncompleteHandshakeScenario() throws InitializationException, IOException {
// Default port used by Distributed Server and Client
final int port = NetworkUtils.getAvailableTcpPort();
// This is used to simulate a DistributedCacheServer that does not complete the handshake response
final BlockingQueue<ByteArrayMessage> messages = new LinkedBlockingQueue<>();
final NettyEventServerFactory serverFactory = getEventServerFactory(port, messages);
final NettyEventServerFactory serverFactory = getEventServerFactory(0, messages);
final EventServer eventServer = serverFactory.getEventServer();
DistributedMapCacheClientService client = new DistributedMapCacheClientService();
runner.addControllerService("client", client);
runner.setProperty(client, DistributedMapCacheClientService.HOSTNAME, "localhost");
runner.setProperty(client, DistributedMapCacheClientService.PORT, String.valueOf(port));
runner.setProperty(client, DistributedMapCacheClientService.PORT, String.valueOf(eventServer.getListeningPort()));
runner.setProperty(client, DistributedMapCacheClientService.COMMUNICATIONS_TIMEOUT, "250 ms");
runner.enableControllerService(client);

View File

@ -17,11 +17,10 @@
package org.apache.nifi.distributed.cache.server.set;
import org.apache.commons.lang3.SerializationException;
import org.apache.nifi.distributed.cache.client.Serializer;
import org.apache.nifi.distributed.cache.client.DistributedSetCacheClientService;
import org.apache.nifi.distributed.cache.client.Serializer;
import org.apache.nifi.distributed.cache.server.DistributedSetCacheServer;
import org.apache.nifi.processor.Processor;
import org.apache.nifi.remote.io.socket.NetworkUtils;
import org.apache.nifi.util.TestRunner;
import org.apache.nifi.util.TestRunners;
import org.junit.jupiter.api.AfterAll;
@ -45,18 +44,18 @@ public class DistributedSetCacheTest {
@BeforeAll
public static void setRunner() throws Exception {
final String port = Integer.toString(NetworkUtils.getAvailableTcpPort());
runner = TestRunners.newTestRunner(Mockito.mock(Processor.class));
server = new DistributedSetCacheServer();
runner.addControllerService(server.getClass().getName(), server);
runner.setProperty(server, DistributedSetCacheServer.PORT, port);
runner.setProperty(server, DistributedSetCacheServer.PORT, "0");
runner.enableControllerService(server);
final int port = server.getPort();
client = new DistributedSetCacheClientService();
runner.addControllerService(client.getClass().getName(), client);
runner.setProperty(client, DistributedSetCacheClientService.HOSTNAME, "localhost");
runner.setProperty(client, DistributedSetCacheClientService.PORT, port);
runner.setProperty(client, DistributedSetCacheClientService.PORT, String.valueOf(port));
runner.enableControllerService(client);
}

View File

@ -16,18 +16,6 @@
*/
package org.apache.nifi.distributed.cache.server.set;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.SerializationException;
import org.apache.commons.lang3.StringUtils;
@ -38,7 +26,6 @@ import org.apache.nifi.distributed.cache.server.DistributedCacheServer;
import org.apache.nifi.distributed.cache.server.DistributedSetCacheServer;
import org.apache.nifi.processor.DataUnit;
import org.apache.nifi.processor.Processor;
import org.apache.nifi.remote.io.socket.NetworkUtils;
import org.apache.nifi.reporting.InitializationException;
import org.apache.nifi.util.MockConfigurationContext;
import org.apache.nifi.util.MockControllerServiceInitializationContext;
@ -46,10 +33,22 @@ import org.apache.nifi.util.MockPropertyValue;
import org.apache.nifi.util.TestRunner;
import org.apache.nifi.util.TestRunners;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class TestDistributedSetServerAndClient {
private final File dataFile = new File("target/cache-data");
@ -69,8 +68,7 @@ public class TestDistributedSetServerAndClient {
server = new DistributedSetCacheServer();
runner.addControllerService("server", server);
final int port = NetworkUtils.getAvailableTcpPort();
runner.setProperty(server, DistributedSetCacheServer.PORT, Integer.toString(port));
runner.setProperty(server, DistributedSetCacheServer.PORT, "0");
}
@AfterEach

View File

@ -21,7 +21,6 @@ import org.apache.nifi.event.transport.configuration.TransportProtocol;
import org.apache.nifi.event.transport.message.ByteArrayMessage;
import org.apache.nifi.event.transport.netty.ByteArrayMessageNettyEventServerFactory;
import org.apache.nifi.event.transport.netty.NettyEventServerFactory;
import org.apache.nifi.remote.io.socket.NetworkUtils;
import org.apache.nifi.serialization.SimpleRecordSchema;
import org.apache.nifi.serialization.WriteResult;
import org.apache.nifi.serialization.record.MapRecord;
@ -96,8 +95,8 @@ class TestUDPEventRecordSink {
runner.addControllerService(WRITER_IDENTIFIER, recordWriter);
runner.enableControllerService(recordWriter);
final int port = NetworkUtils.getAvailableUdpPort();
eventServer = createServer(runner, port);
eventServer = createServer(runner, 0);
final int port = eventServer.getListeningPort();
sink = new UDPEventRecordSink();
runner.addControllerService(IDENTIFIER, sink);

View File

@ -110,8 +110,7 @@ public class ListenWebSocket extends AbstractWebSocketGatewayProcessor {
@Override
protected WebSocketService getWebSocketService(final ProcessContext context) {
return context.getProperty(PROP_WEBSOCKET_SERVER_SERVICE)
.asControllerService(WebSocketService.class);
return context.getProperty(PROP_WEBSOCKET_SERVER_SERVICE).asControllerService(WebSocketService.class);
}
@Override

View File

@ -20,7 +20,6 @@ import org.apache.nifi.processor.ProcessSessionFactory;
import org.apache.nifi.processor.Relationship;
import org.apache.nifi.provenance.ProvenanceEventRecord;
import org.apache.nifi.provenance.ProvenanceEventType;
import org.apache.nifi.remote.io.socket.NetworkUtils;
import org.apache.nifi.reporting.InitializationException;
import org.apache.nifi.util.MockFlowFile;
import org.apache.nifi.util.MockProcessSession;
@ -127,26 +126,36 @@ class TestConnectWebSocket extends TestListenWebSocket {
@Test
void testDynamicUrlsParsedFromFlowFileAndAbleToConnect() throws InitializationException {
// Start websocket server
final int port = NetworkUtils.availablePort();
TestRunner webSocketListener = getListenWebSocket(port);
final TestRunner webSocketListener = TestRunners.newTestRunner(ListenWebSocket.class);
final String serverId = "ws-server-service";
JettyWebSocketServer server = new JettyWebSocketServer();
webSocketListener.addControllerService(serverId, server);
webSocketListener.setProperty(server, JettyWebSocketServer.LISTEN_PORT, "0");
webSocketListener.enableControllerService(server);
webSocketListener.setProperty(ListenWebSocket.PROP_WEBSOCKET_SERVER_SERVICE, serverId);
webSocketListener.setProperty(ListenWebSocket.PROP_SERVER_URL_PATH, "/test");
webSocketListener.run(1, false);
final int listeningPort = server.getListeningPort();
final TestRunner runner = TestRunners.newTestRunner(ConnectWebSocket.class);
final String serviceId = "ws-service";
final String clientId = "ws-service";
final String endpointId = "client-1";
MockFlowFile flowFile = getFlowFile();
runner.enqueue(flowFile);
JettyWebSocketClient service = new JettyWebSocketClient();
JettyWebSocketClient client = new JettyWebSocketClient();
runner.addControllerService(serviceId, service);
runner.setProperty(service, JettyWebSocketClient.WS_URI, String.format("ws://localhost:%s/${dynamicUrlPart}", port));
runner.enableControllerService(service);
runner.addControllerService(clientId, client);
runner.setProperty(client, JettyWebSocketClient.WS_URI, String.format("ws://localhost:%s/${dynamicUrlPart}", listeningPort));
runner.enableControllerService(client);
runner.setProperty(ConnectWebSocket.PROP_WEBSOCKET_CLIENT_SERVICE, serviceId);
runner.setProperty(ConnectWebSocket.PROP_WEBSOCKET_CLIENT_SERVICE, clientId);
runner.setProperty(ConnectWebSocket.PROP_WEBSOCKET_CLIENT_ID, endpointId);
runner.run(1, false);
@ -192,20 +201,6 @@ class TestConnectWebSocket extends TestListenWebSocket {
runner.stop();
}
private TestRunner getListenWebSocket(final int port) throws InitializationException {
final TestRunner runner = TestRunners.newTestRunner(ListenWebSocket.class);
final String serviceId = "ws-server-service";
JettyWebSocketServer service = new JettyWebSocketServer();
runner.addControllerService(serviceId, service);
runner.setProperty(service, JettyWebSocketServer.LISTEN_PORT, String.valueOf(port));
runner.enableControllerService(service);
runner.setProperty(ListenWebSocket.PROP_WEBSOCKET_SERVER_SERVICE, serviceId);
runner.setProperty(ListenWebSocket.PROP_SERVER_URL_PATH, "/test");
return runner;
}
private MockFlowFile getFlowFile() {
Map<String, String> attributes = new HashMap<>();

View File

@ -315,17 +315,21 @@ public class JettyWebSocketServer extends AbstractJettyWebSocketService implemen
final SslContextFactory sslContextFactory = createSslFactory(context);
final ServerConnector serverConnector = createConnector(sslContextFactory, listenPort);
server.setConnectors(new Connector[] {serverConnector});
servletHandler.addServletWithMapping(JettyWebSocketServlet.class, "/*");
getLogger().info("Starting JettyWebSocketServer on port {}.", new Object[]{listenPort});
server.start();
listenPort = serverConnector.getLocalPort();
portToControllerService.put(listenPort, this);
}
public int getListeningPort() {
return listenPort;
}
private ServerConnector createConnector(final SslContextFactory sslContextFactory, final Integer listenPort) {
final ServerConnector serverConnector;

View File

@ -16,7 +16,6 @@
*/
package org.apache.nifi.websocket.jetty;
import org.apache.nifi.remote.io.socket.NetworkUtils;
import org.apache.nifi.websocket.WebSocketClientService;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.client.ClientUpgradeRequest;
@ -59,7 +58,7 @@ class ITJettyWebsocketReconnect {
clientService = new JettyWebSocketTestClient();
clientServiceContext = new ControllerServiceTestContext(clientService, "JettyWebSocketClient1");
clientServiceContext.setCustomValue(JettyWebSocketClient.WS_URI, "ws://localhost:" + NetworkUtils.getAvailableTcpPort() + "/test");
clientServiceContext.setCustomValue(JettyWebSocketClient.WS_URI, "ws://localhost:0/test");
clientServiceContext.setCustomValue(JettyWebSocketClient.USER_NAME, "user2");
clientServiceContext.setCustomValue(JettyWebSocketClient.USER_PASSWORD, "password2");

View File

@ -17,7 +17,6 @@
package org.apache.nifi.websocket.jetty;
import org.apache.nifi.processor.Processor;
import org.apache.nifi.remote.io.socket.NetworkUtils;
import org.apache.nifi.util.TestRunner;
import org.apache.nifi.util.TestRunners;
import org.eclipse.jetty.websocket.api.Session;
@ -77,12 +76,11 @@ public class TestJettyWebSocketServer {
@Test
public void testWebSocketConnect() throws Exception {
final int port = NetworkUtils.availablePort();
final String identifier = JettyWebSocketServer.class.getSimpleName();
final JettyWebSocketServer server = new JettyWebSocketServer();
runner.addControllerService(identifier, server);
runner.setProperty(server, JettyWebSocketServer.LISTEN_PORT, Integer.toString(port));
runner.setProperty(server, JettyWebSocketServer.LISTEN_PORT, "0");
runner.enableControllerService(server);
server.registerProcessor(ROOT_ENDPOINT_ID, runner.getProcessor());
@ -103,10 +101,12 @@ public class TestJettyWebSocketServer {
}
};
try {
client.start();
final URI uri = getWebSocketUri(port);
final URI uri = getWebSocketUri(server.getListeningPort());
final Future<Session> connectSession = client.connect(adapter, uri);
final Session session = connectSession.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
session.getRemote().sendString(command);

View File

@ -27,7 +27,6 @@ import org.apache.nifi.registry.security.identity.IdentityMapper;
import org.apache.nifi.registry.security.ldap.LdapAuthenticationStrategy;
import org.apache.nifi.registry.security.ldap.ReferralStrategy;
import org.apache.nifi.registry.util.StandardPropertyValue;
import org.apache.nifi.remote.io.socket.NetworkUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@ -85,9 +84,9 @@ public class LdapUserGroupProviderTest {
public void setup() {
server = new UnboundIdContainer("o=nifi", "classpath:nifi-example.ldif");
server.setApplicationContext(new GenericApplicationContext());
serverPort = NetworkUtils.availablePort();
server.setPort(serverPort);
server.setPort(0);
server.afterPropertiesSet();
serverPort = server.getPort();
final UserGroupProviderInitializationContext initializationContext = mock(UserGroupProviderInitializationContext.class);
when(initializationContext.getIdentifier()).thenReturn("identifier");

View File

@ -17,7 +17,6 @@
package org.apache.nifi.registry.jetty.connector;
import org.apache.nifi.registry.properties.NiFiRegistryProperties;
import org.apache.nifi.remote.io.socket.NetworkUtils;
import org.apache.nifi.security.util.TemporaryKeyStoreBuilder;
import org.apache.nifi.security.util.TlsConfiguration;
import org.apache.nifi.util.StringUtils;
@ -65,9 +64,8 @@ class ApplicationServerConnectorFactoryTest {
@Test
void testGetServerConnectorRequiredProperties() {
final int port = NetworkUtils.getAvailableTcpPort();
final Properties configuredProperties = new Properties();
configuredProperties.put(NiFiRegistryProperties.WEB_HTTP_PORT, Integer.toString(port));
configuredProperties.put(NiFiRegistryProperties.WEB_HTTP_PORT, "0");
final NiFiRegistryProperties properties = getProperties(configuredProperties);
final ApplicationServerConnectorFactory factory = new ApplicationServerConnectorFactory(server, properties);
@ -80,9 +78,8 @@ class ApplicationServerConnectorFactoryTest {
@Test
void testGetServerConnectorHostProperty() {
final int port = NetworkUtils.getAvailableTcpPort();
final Properties configuredProperties = new Properties();
configuredProperties.put(NiFiRegistryProperties.WEB_HTTP_PORT, Integer.toString(port));
configuredProperties.put(NiFiRegistryProperties.WEB_HTTP_PORT, "0");
configuredProperties.put(NiFiRegistryProperties.WEB_HTTP_HOST, LOCALHOST);
final NiFiRegistryProperties properties = getProperties(configuredProperties);
@ -96,9 +93,8 @@ class ApplicationServerConnectorFactoryTest {
@Test
void testGetServerConnectorHostPropertyEmpty() {
final int port = NetworkUtils.getAvailableTcpPort();
final Properties configuredProperties = new Properties();
configuredProperties.put(NiFiRegistryProperties.WEB_HTTP_PORT, Integer.toString(port));
configuredProperties.put(NiFiRegistryProperties.WEB_HTTP_PORT, "0");
configuredProperties.put(NiFiRegistryProperties.WEB_HTTP_HOST, StringUtils.EMPTY);
final NiFiRegistryProperties properties = getProperties(configuredProperties);
@ -112,9 +108,8 @@ class ApplicationServerConnectorFactoryTest {
@Test
void testGetServerConnectorSslProperties() {
final int port = NetworkUtils.getAvailableTcpPort();
final Properties configuredProperties = getSecurityProperties();
configuredProperties.put(NiFiRegistryProperties.WEB_HTTPS_PORT, Integer.toString(port));
configuredProperties.put(NiFiRegistryProperties.WEB_HTTPS_PORT, "0");
final NiFiRegistryProperties properties = getProperties(configuredProperties);
final ApplicationServerConnectorFactory factory = new ApplicationServerConnectorFactory(server, properties);
@ -128,9 +123,8 @@ class ApplicationServerConnectorFactoryTest {
@Test
void testGetServerConnectorHttp2Properties() {
final int port = NetworkUtils.getAvailableTcpPort();
final Properties configuredProperties = getSecurityProperties();
configuredProperties.put(NiFiRegistryProperties.WEB_HTTPS_PORT, Integer.toString(port));
configuredProperties.put(NiFiRegistryProperties.WEB_HTTPS_PORT, "0");
configuredProperties.put(NiFiRegistryProperties.WEB_HTTPS_APPLICATION_PROTOCOLS, H2_PROTOCOL);
configuredProperties.put(NiFiRegistryProperties.WEB_HTTPS_CIPHERSUITES_INCLUDE, INCLUDE_CIPHERS);
configuredProperties.put(NiFiRegistryProperties.WEB_HTTPS_CIPHERSUITES_EXCLUDE, EXCLUDE_CIPHERS);

View File

@ -18,9 +18,8 @@
package org.apache.nifi.toolkit.tls.service;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.nifi.remote.io.socket.NetworkUtils;
import org.apache.nifi.security.util.KeystoreType;
import org.apache.nifi.security.util.KeyStoreUtils;
import org.apache.nifi.security.util.KeystoreType;
import org.apache.nifi.toolkit.tls.configuration.TlsClientConfig;
import org.apache.nifi.toolkit.tls.configuration.TlsConfig;
import org.apache.nifi.toolkit.tls.service.client.TlsCertificateAuthorityClient;
@ -96,13 +95,12 @@ public class TlsCertificateAuthorityTest {
subjectAlternativeName = "nifi.apache.org";
String myTestTokenUseSomethingStronger = "myTestTokenUseSomethingStronger";
int port = NetworkUtils.getAvailableTcpPort();
serverConfig = new TlsConfig();
serverConfig.setCaHostname("localhost");
serverConfig.setToken(myTestTokenUseSomethingStronger);
serverConfig.setKeyStore(serverKeyStore);
serverConfig.setPort(port);
serverConfig.setPort(0);
serverConfig.setDays(5);
serverConfig.setKeySize(2048);
serverConfig.initDefaults();
@ -114,7 +112,7 @@ public class TlsCertificateAuthorityTest {
clientConfig.setTrustStore(clientTrustStore);
clientConfig.setToken(myTestTokenUseSomethingStronger);
clientConfig.setDomainAlternativeNames(Collections.singletonList(subjectAlternativeName));
clientConfig.setPort(port);
clientConfig.setPort(0);
clientConfig.setKeySize(2048);
clientConfig.initDefaults();