Search in sources :

Example 1 with HttpProxy

use of org.apache.nifi.remote.protocol.http.HttpProxy in project nifi by apache.

the class SiteToSiteCliMain method parseCli.

/**
 * Parses command line options into a CliParse object
 *
 * @param options an empty options object (so callers can print usage if the parse fails
 * @param args    the string array of arguments
 * @return a CliParse object containing the constructed SiteToSiteClient.Builder and a TransferDirection
 * @throws ParseException if there is an error parsing the command line
 */
public static CliParse parseCli(Options options, String[] args) throws ParseException {
    options.addOption("u", URL_OPTION, true, "NiFI URL to connect to (default: " + URL_OPTION_DEFAULT + ")");
    options.addOption("d", DIRECTION_OPTION, true, "Direction (valid directions: " + Arrays.stream(TransferDirection.values()).map(Object::toString).collect(Collectors.joining(", ")) + ") (default: " + DIRECTION_OPTION_DEFAULT + ")");
    options.addOption("n", PORT_NAME_OPTION, true, "Port name");
    options.addOption("i", PORT_IDENTIFIER_OPTION, true, "Port id");
    options.addOption(null, TIMEOUT_OPTION, true, "Timeout");
    options.addOption(null, PENALIZATION_OPTION, true, "Penalization period");
    options.addOption(null, KEYSTORE_OPTION, true, "Keystore");
    options.addOption(null, KEY_STORE_TYPE_OPTION, true, "Keystore type (default: " + KEYSTORE_TYPE_OPTION_DEFAULT + ")");
    options.addOption(null, KEY_STORE_PASSWORD_OPTION, true, "Keystore password");
    options.addOption(null, TRUST_STORE_OPTION, true, "Truststore");
    options.addOption(null, TRUST_STORE_TYPE_OPTION, true, "Truststore type (default: " + KEYSTORE_TYPE_OPTION_DEFAULT + ")");
    options.addOption(null, TRUST_STORE_PASSWORD_OPTION, true, "Truststore password");
    options.addOption(null, NEED_CLIENT_AUTH_OPTION, false, "Need client auth");
    options.addOption("c", COMPRESSION_OPTION, false, "Use compression");
    options.addOption(null, PEER_PERSISTENCE_FILE_OPTION, true, "File to write peer information to so it can be recovered on restart");
    options.addOption("p", TRANSPORT_PROTOCOL_OPTION, true, "Site to site transport protocol (default: " + TRANSPORT_PROTOCOL_OPTION_DEFAULT + ")");
    options.addOption(null, BATCH_COUNT_OPTION, true, "Number of flow files in a batch");
    options.addOption(null, BATCH_SIZE_OPTION, true, "Size of flow files in a batch");
    options.addOption(null, BATCH_DURATION_OPTION, true, "Duration of a batch");
    options.addOption(null, PROXY_HOST_OPTION, true, "Proxy hostname");
    options.addOption(null, PROXY_PORT_OPTION, true, "Proxy port");
    options.addOption(null, PROXY_USERNAME_OPTION, true, "Proxy username");
    options.addOption(null, PROXY_PASSWORD_OPTION, true, "Proxy password");
    options.addOption("h", HELP_OPTION, false, "Show help message and exit");
    CommandLineParser parser = new DefaultParser();
    CommandLine commandLine;
    commandLine = parser.parse(options, args);
    if (commandLine.hasOption(HELP_OPTION)) {
        printUsage(null, options);
        System.exit(1);
    }
    SiteToSiteClient.Builder builder = new SiteToSiteClient.Builder();
    builder.url(commandLine.getOptionValue(URL_OPTION, URL_OPTION_DEFAULT));
    if (commandLine.hasOption(PORT_NAME_OPTION)) {
        builder.portName(commandLine.getOptionValue(PORT_NAME_OPTION));
    }
    if (commandLine.hasOption(PORT_IDENTIFIER_OPTION)) {
        builder.portIdentifier(commandLine.getOptionValue(PORT_IDENTIFIER_OPTION));
    }
    if (commandLine.hasOption(TIMEOUT_OPTION)) {
        builder.timeout(FormatUtils.getTimeDuration(commandLine.getOptionValue(TIMEOUT_OPTION), TimeUnit.NANOSECONDS), TimeUnit.NANOSECONDS);
    }
    if (commandLine.hasOption(PENALIZATION_OPTION)) {
        builder.nodePenalizationPeriod(FormatUtils.getTimeDuration(commandLine.getOptionValue(PENALIZATION_OPTION), TimeUnit.NANOSECONDS), TimeUnit.NANOSECONDS);
    }
    if (commandLine.hasOption(KEYSTORE_OPTION)) {
        builder.keystoreFilename(commandLine.getOptionValue(KEYSTORE_OPTION));
        builder.keystoreType(KeystoreType.valueOf(commandLine.getOptionValue(KEY_STORE_TYPE_OPTION, KEYSTORE_TYPE_OPTION_DEFAULT).toUpperCase()));
        if (commandLine.hasOption(KEY_STORE_PASSWORD_OPTION)) {
            builder.keystorePass(commandLine.getOptionValue(KEY_STORE_PASSWORD_OPTION));
        } else {
            throw new ParseException("Must specify keystore password");
        }
    }
    if (commandLine.hasOption(TRUST_STORE_OPTION)) {
        builder.truststoreFilename(commandLine.getOptionValue(TRUST_STORE_OPTION));
        builder.truststoreType(KeystoreType.valueOf(commandLine.getOptionValue(TRUST_STORE_TYPE_OPTION, KEYSTORE_TYPE_OPTION_DEFAULT).toUpperCase()));
        if (commandLine.hasOption(TRUST_STORE_PASSWORD_OPTION)) {
            builder.truststorePass(commandLine.getOptionValue(TRUST_STORE_PASSWORD_OPTION));
        } else {
            throw new ParseException("Must specify truststore password");
        }
    }
    if (commandLine.hasOption(COMPRESSION_OPTION)) {
        builder.useCompression(true);
    } else {
        builder.useCompression(false);
    }
    if (commandLine.hasOption(PEER_PERSISTENCE_FILE_OPTION)) {
        builder.peerPersistenceFile(new File(commandLine.getOptionValue(PEER_PERSISTENCE_FILE_OPTION)));
    }
    if (commandLine.hasOption(BATCH_COUNT_OPTION)) {
        builder.requestBatchCount(Integer.parseInt(commandLine.getOptionValue(BATCH_COUNT_OPTION)));
    }
    if (commandLine.hasOption(BATCH_SIZE_OPTION)) {
        builder.requestBatchSize(Long.parseLong(commandLine.getOptionValue(BATCH_SIZE_OPTION)));
    }
    if (commandLine.hasOption(BATCH_DURATION_OPTION)) {
        builder.requestBatchDuration(FormatUtils.getTimeDuration(commandLine.getOptionValue(BATCH_DURATION_OPTION), TimeUnit.NANOSECONDS), TimeUnit.NANOSECONDS);
    }
    if (commandLine.hasOption(PROXY_HOST_OPTION)) {
        builder.httpProxy(new HttpProxy(commandLine.getOptionValue(PROXY_HOST_OPTION), Integer.parseInt(commandLine.getOptionValue(PROXY_PORT_OPTION, PROXY_PORT_OPTION_DEFAULT)), commandLine.getOptionValue(PROXY_USERNAME_OPTION), commandLine.getOptionValue(PROXY_PASSWORD_OPTION)));
    }
    builder.transportProtocol(SiteToSiteTransportProtocol.valueOf(commandLine.getOptionValue(TRANSPORT_PROTOCOL_OPTION, TRANSPORT_PROTOCOL_OPTION_DEFAULT).toUpperCase()));
    TransferDirection transferDirection = TransferDirection.valueOf(commandLine.getOptionValue(DIRECTION_OPTION, DIRECTION_OPTION_DEFAULT));
    return new CliParse() {

        @Override
        public SiteToSiteClient.Builder getBuilder() {
            return builder;
        }

        @Override
        public TransferDirection getTransferDirection() {
            return transferDirection;
        }
    };
}
Also used : SiteToSiteClient(org.apache.nifi.remote.client.SiteToSiteClient) HttpProxy(org.apache.nifi.remote.protocol.http.HttpProxy) CommandLine(org.apache.commons.cli.CommandLine) TransferDirection(org.apache.nifi.remote.TransferDirection) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) File(java.io.File) DefaultParser(org.apache.commons.cli.DefaultParser)

Example 2 with HttpProxy

use of org.apache.nifi.remote.protocol.http.HttpProxy in project nifi by apache.

the class TestHttpClient method testSendProxyAuthFailed.

@Test
public void testSendProxyAuthFailed() throws Exception {
    try (final SiteToSiteClient client = getDefaultBuilder().portName("input-running").httpProxy(new HttpProxy("localhost", proxyServerWithAuth.getListenAddress().getPort(), null, null)).build()) {
        final Transaction transaction = client.createTransaction(TransferDirection.SEND);
        assertNull("createTransaction should fail at peer selection and return null.", transaction);
    }
}
Also used : SiteToSiteClient(org.apache.nifi.remote.client.SiteToSiteClient) HttpProxy(org.apache.nifi.remote.protocol.http.HttpProxy) Transaction(org.apache.nifi.remote.Transaction) Test(org.junit.Test)

Example 3 with HttpProxy

use of org.apache.nifi.remote.protocol.http.HttpProxy in project nifi by apache.

the class StandardRemoteProcessGroup method getSiteToSiteRestApiClient.

private SiteToSiteRestApiClient getSiteToSiteRestApiClient() {
    SiteToSiteRestApiClient apiClient = new SiteToSiteRestApiClient(sslContext, new HttpProxy(proxyHost, proxyPort, proxyUser, proxyPassword), getEventReporter());
    apiClient.setConnectTimeoutMillis(getCommunicationsTimeout(TimeUnit.MILLISECONDS));
    apiClient.setReadTimeoutMillis(getCommunicationsTimeout(TimeUnit.MILLISECONDS));
    apiClient.setLocalAddress(getLocalAddress());
    apiClient.setCacheExpirationMillis(remoteContentsCacheExpiration);
    return apiClient;
}
Also used : HttpProxy(org.apache.nifi.remote.protocol.http.HttpProxy) SiteToSiteRestApiClient(org.apache.nifi.remote.util.SiteToSiteRestApiClient)

Example 4 with HttpProxy

use of org.apache.nifi.remote.protocol.http.HttpProxy in project nifi by apache.

the class AbstractSiteToSiteReportingTask method setup.

@OnScheduled
public void setup(final ConfigurationContext context) throws IOException {
    final SSLContextService sslContextService = context.getProperty(SSL_CONTEXT).asControllerService(SSLContextService.class);
    final SSLContext sslContext = sslContextService == null ? null : sslContextService.createSSLContext(SSLContextService.ClientAuth.REQUIRED);
    final ComponentLog logger = getLogger();
    final EventReporter eventReporter = new EventReporter() {

        @Override
        public void reportEvent(final Severity severity, final String category, final String message) {
            switch(severity) {
                case WARNING:
                    logger.warn(message);
                    break;
                case ERROR:
                    logger.error(message);
                    break;
                default:
                    break;
            }
        }
    };
    final String destinationUrl = context.getProperty(DESTINATION_URL).evaluateAttributeExpressions().getValue();
    final SiteToSiteTransportProtocol mode = SiteToSiteTransportProtocol.valueOf(context.getProperty(TRANSPORT_PROTOCOL).getValue());
    final HttpProxy httpProxy = mode.equals(SiteToSiteTransportProtocol.RAW) || StringUtils.isEmpty(context.getProperty(HTTP_PROXY_HOSTNAME).getValue()) ? null : new HttpProxy(context.getProperty(HTTP_PROXY_HOSTNAME).getValue(), context.getProperty(HTTP_PROXY_PORT).asInteger(), context.getProperty(HTTP_PROXY_USERNAME).getValue(), context.getProperty(HTTP_PROXY_PASSWORD).getValue());
    siteToSiteClient = new SiteToSiteClient.Builder().urls(SiteToSiteRestApiClient.parseClusterUrls(destinationUrl)).portName(context.getProperty(PORT_NAME).getValue()).useCompression(context.getProperty(COMPRESS).asBoolean()).eventReporter(eventReporter).sslContext(sslContext).timeout(context.getProperty(TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS), TimeUnit.MILLISECONDS).transportProtocol(mode).httpProxy(httpProxy).build();
}
Also used : HttpProxy(org.apache.nifi.remote.protocol.http.HttpProxy) SSLContextService(org.apache.nifi.ssl.SSLContextService) RestrictedSSLContextService(org.apache.nifi.ssl.RestrictedSSLContextService) SSLContext(javax.net.ssl.SSLContext) SiteToSiteTransportProtocol(org.apache.nifi.remote.protocol.SiteToSiteTransportProtocol) ComponentLog(org.apache.nifi.logging.ComponentLog) EventReporter(org.apache.nifi.events.EventReporter) OnScheduled(org.apache.nifi.annotation.lifecycle.OnScheduled)

Example 5 with HttpProxy

use of org.apache.nifi.remote.protocol.http.HttpProxy in project nifi by apache.

the class TestSiteInfoProvider method testSecure.

@Test
public void testSecure() throws Exception {
    final Set<String> expectedClusterUrl = new LinkedHashSet<>(Arrays.asList(new String[] { "https://node1:8443", "https://node2:8443" }));
    final String expectedActiveClusterUrl = "https://node2:8443/nifi-api";
    final SSLContext expectedSslConText = mock(SSLContext.class);
    final HttpProxy expectedHttpProxy = mock(HttpProxy.class);
    final SiteInfoProvider siteInfoProvider = spy(new SiteInfoProvider());
    siteInfoProvider.setClusterUrls(expectedClusterUrl);
    siteInfoProvider.setSslContext(expectedSslConText);
    siteInfoProvider.setProxy(expectedHttpProxy);
    final ControllerDTO controllerDTO = new ControllerDTO();
    final PortDTO inputPort1 = new PortDTO();
    inputPort1.setName("input-one");
    inputPort1.setId("input-0001");
    final PortDTO inputPort2 = new PortDTO();
    inputPort2.setName("input-two");
    inputPort2.setId("input-0002");
    final PortDTO outputPort1 = new PortDTO();
    outputPort1.setName("output-one");
    outputPort1.setId("output-0001");
    final PortDTO outputPort2 = new PortDTO();
    outputPort2.setName("output-two");
    outputPort2.setId("output-0002");
    final Set<PortDTO> inputPorts = new HashSet<>();
    inputPorts.add(inputPort1);
    inputPorts.add(inputPort2);
    final Set<PortDTO> outputPorts = new HashSet<>();
    outputPorts.add(outputPort1);
    outputPorts.add(outputPort2);
    controllerDTO.setInputPorts(inputPorts);
    controllerDTO.setOutputPorts(outputPorts);
    controllerDTO.setRemoteSiteListeningPort(8081);
    controllerDTO.setRemoteSiteHttpListeningPort(8443);
    controllerDTO.setSiteToSiteSecure(true);
    // SiteInfoProvider uses SiteToSIteRestApiClient to get ControllerDTO.
    doAnswer(invocation -> {
        final SSLContext sslContext = invocation.getArgumentAt(0, SSLContext.class);
        final HttpProxy httpProxy = invocation.getArgumentAt(1, HttpProxy.class);
        assertEquals(expectedSslConText, sslContext);
        assertEquals(expectedHttpProxy, httpProxy);
        final SiteToSiteRestApiClient apiClient = mock(SiteToSiteRestApiClient.class);
        when(apiClient.getController(eq(expectedClusterUrl))).thenReturn(controllerDTO);
        when(apiClient.getBaseUrl()).thenReturn(expectedActiveClusterUrl);
        return apiClient;
    }).when(siteInfoProvider).createSiteToSiteRestApiClient(any(), any());
    // siteInfoProvider should expose correct information of the remote NiFi cluster.
    assertEquals(controllerDTO.getRemoteSiteListeningPort(), siteInfoProvider.getSiteToSitePort());
    assertEquals(controllerDTO.getRemoteSiteHttpListeningPort(), siteInfoProvider.getSiteToSiteHttpPort());
    assertEquals(controllerDTO.isSiteToSiteSecure(), siteInfoProvider.isSecure());
    assertTrue(siteInfoProvider.isWebInterfaceSecure());
    assertEquals(inputPort1.getId(), siteInfoProvider.getInputPortIdentifier(inputPort1.getName()));
    assertEquals(inputPort2.getId(), siteInfoProvider.getInputPortIdentifier(inputPort2.getName()));
    assertEquals(outputPort1.getId(), siteInfoProvider.getOutputPortIdentifier(outputPort1.getName()));
    assertEquals(outputPort2.getId(), siteInfoProvider.getOutputPortIdentifier(outputPort2.getName()));
    assertNull(siteInfoProvider.getInputPortIdentifier("not-exist"));
    assertNull(siteInfoProvider.getOutputPortIdentifier("not-exist"));
    assertEquals(inputPort1.getId(), siteInfoProvider.getPortIdentifier(inputPort1.getName(), TransferDirection.SEND));
    assertEquals(outputPort1.getId(), siteInfoProvider.getPortIdentifier(outputPort1.getName(), TransferDirection.RECEIVE));
    assertEquals(expectedActiveClusterUrl, siteInfoProvider.getActiveClusterUrl().toString());
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HttpProxy(org.apache.nifi.remote.protocol.http.HttpProxy) PortDTO(org.apache.nifi.web.api.dto.PortDTO) SiteToSiteRestApiClient(org.apache.nifi.remote.util.SiteToSiteRestApiClient) ControllerDTO(org.apache.nifi.web.api.dto.ControllerDTO) SSLContext(javax.net.ssl.SSLContext) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Test(org.junit.Test)

Aggregations

HttpProxy (org.apache.nifi.remote.protocol.http.HttpProxy)7 SiteToSiteClient (org.apache.nifi.remote.client.SiteToSiteClient)3 Test (org.junit.Test)3 SSLContext (javax.net.ssl.SSLContext)2 SiteToSiteRestApiClient (org.apache.nifi.remote.util.SiteToSiteRestApiClient)2 File (java.io.File)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 CommandLine (org.apache.commons.cli.CommandLine)1 CommandLineParser (org.apache.commons.cli.CommandLineParser)1 DefaultParser (org.apache.commons.cli.DefaultParser)1 Options (org.apache.commons.cli.Options)1 ParseException (org.apache.commons.cli.ParseException)1 OnScheduled (org.apache.nifi.annotation.lifecycle.OnScheduled)1 EventReporter (org.apache.nifi.events.EventReporter)1 ComponentLog (org.apache.nifi.logging.ComponentLog)1 Transaction (org.apache.nifi.remote.Transaction)1 TransferDirection (org.apache.nifi.remote.TransferDirection)1 SiteToSiteClientConfig (org.apache.nifi.remote.client.SiteToSiteClientConfig)1 SiteToSiteTransportProtocol (org.apache.nifi.remote.protocol.SiteToSiteTransportProtocol)1