Search in sources :

Example 1 with CommandLineParseException

use of org.apache.nifi.toolkit.tls.commandLine.CommandLineParseException in project nifi by apache.

the class TlsToolkitStandaloneCommandLine method doParse.

@Override
protected CommandLine doParse(String... args) throws CommandLineParseException {
    CommandLine commandLine = super.doParse(args);
    String outputDirectory = commandLine.getOptionValue(OUTPUT_DIRECTORY_ARG, DEFAULT_OUTPUT_DIRECTORY);
    baseDir = new File(outputDirectory);
    dnPrefix = commandLine.getOptionValue(NIFI_DN_PREFIX_ARG, TlsConfig.DEFAULT_DN_PREFIX);
    dnSuffix = commandLine.getOptionValue(NIFI_DN_SUFFIX_ARG, TlsConfig.DEFAULT_DN_SUFFIX);
    domainAlternativeNames = commandLine.getOptionValue(SUBJECT_ALTERNATIVE_NAMES);
    Stream<String> globalOrderExpressions = null;
    if (commandLine.hasOption(GLOBAL_PORT_SEQUENCE_ARG)) {
        globalOrderExpressions = Arrays.stream(commandLine.getOptionValues(GLOBAL_PORT_SEQUENCE_ARG)).flatMap(s -> Arrays.stream(s.split(","))).map(String::trim);
    }
    if (commandLine.hasOption(HOSTNAMES_ARG)) {
        instanceDefinitions = Collections.unmodifiableList(InstanceDefinition.createDefinitions(globalOrderExpressions, Arrays.stream(commandLine.getOptionValues(HOSTNAMES_ARG)).flatMap(s -> Arrays.stream(s.split(",")).map(String::trim)), parsePasswordSupplier(commandLine, KEY_STORE_PASSWORD_ARG, passwordUtil.passwordSupplier()), parsePasswordSupplier(commandLine, KEY_PASSWORD_ARG, commandLine.hasOption(DIFFERENT_KEY_AND_KEYSTORE_PASSWORDS_ARG) ? passwordUtil.passwordSupplier() : null), parsePasswordSupplier(commandLine, TRUST_STORE_PASSWORD_ARG, passwordUtil.passwordSupplier())));
    } else {
        instanceDefinitions = Collections.emptyList();
    }
    String[] clientDnValues = commandLine.getOptionValues(CLIENT_CERT_DN_ARG);
    if (clientDnValues != null) {
        clientDns = Collections.unmodifiableList(Arrays.stream(clientDnValues).collect(Collectors.toList()));
    } else {
        clientDns = Collections.emptyList();
    }
    clientPasswords = Collections.unmodifiableList(getPasswords(CLIENT_CERT_PASSWORD_ARG, commandLine, clientDns.size(), CLIENT_CERT_DN_ARG));
    clientPasswordsGenerated = commandLine.getOptionValues(CLIENT_CERT_PASSWORD_ARG) == null;
    overwrite = commandLine.hasOption(OVERWRITE_ARG);
    String nifiPropertiesFile = commandLine.getOptionValue(NIFI_PROPERTIES_FILE_ARG, "");
    try {
        if (StringUtils.isEmpty(nifiPropertiesFile)) {
            logger.info("No " + NIFI_PROPERTIES_FILE_ARG + " specified, using embedded one.");
            niFiPropertiesWriterFactory = new NiFiPropertiesWriterFactory();
        } else {
            logger.info("Using " + nifiPropertiesFile + " as template.");
            niFiPropertiesWriterFactory = new NiFiPropertiesWriterFactory(new FileInputStream(nifiPropertiesFile));
        }
    } catch (IOException e) {
        printUsageAndThrow("Unable to read nifi.properties from " + (StringUtils.isEmpty(nifiPropertiesFile) ? "classpath" : nifiPropertiesFile), ExitCode.ERROR_READING_NIFI_PROPERTIES);
    }
    return commandLine;
}
Also used : IntStream(java.util.stream.IntStream) ExitCode(org.apache.nifi.toolkit.tls.commandLine.ExitCode) Arrays(java.util.Arrays) Logger(org.slf4j.Logger) BaseTlsToolkitCommandLine(org.apache.nifi.toolkit.tls.commandLine.BaseTlsToolkitCommandLine) TlsConfig(org.apache.nifi.toolkit.tls.configuration.TlsConfig) PasswordUtil(org.apache.nifi.toolkit.tls.util.PasswordUtil) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) StringUtils(org.apache.nifi.util.StringUtils) Supplier(java.util.function.Supplier) Collectors(java.util.stream.Collectors) File(java.io.File) CommandLineParseException(org.apache.nifi.toolkit.tls.commandLine.CommandLineParseException) NiFiPropertiesWriterFactory(org.apache.nifi.toolkit.tls.properties.NiFiPropertiesWriterFactory) List(java.util.List) Stream(java.util.stream.Stream) InstanceDefinition(org.apache.nifi.toolkit.tls.configuration.InstanceDefinition) Paths(java.nio.file.Paths) CommandLine(org.apache.commons.cli.CommandLine) Path(java.nio.file.Path) Collections(java.util.Collections) StandaloneConfig(org.apache.nifi.toolkit.tls.configuration.StandaloneConfig) BaseTlsToolkitCommandLine(org.apache.nifi.toolkit.tls.commandLine.BaseTlsToolkitCommandLine) CommandLine(org.apache.commons.cli.CommandLine) IOException(java.io.IOException) File(java.io.File) NiFiPropertiesWriterFactory(org.apache.nifi.toolkit.tls.properties.NiFiPropertiesWriterFactory) FileInputStream(java.io.FileInputStream)

Example 2 with CommandLineParseException

use of org.apache.nifi.toolkit.tls.commandLine.CommandLineParseException in project nifi by apache.

the class TlsToolkitGetStatusCommandLine method main.

public static void main(String[] args) {
    TlsToolkitGetStatusCommandLine commandLine = new TlsToolkitGetStatusCommandLine();
    try {
        commandLine.parse(args);
    } catch (CommandLineParseException e) {
        System.exit(e.getExitCode().ordinal());
    }
    final GetStatusConfig config = commandLine.createConfig();
    try {
        final TlsToolkitGetStatus tlsToolkitGetStatus = new TlsToolkitGetStatus();
        tlsToolkitGetStatus.get(config);
    } catch (Exception e) {
        commandLine.printUsage("Error communicating with " + config.getUrl().toString() + " (" + e.getMessage() + ")");
        System.exit(ExitCode.SERVICE_ERROR.ordinal());
    }
    System.exit(ExitCode.SUCCESS.ordinal());
}
Also used : CommandLineParseException(org.apache.nifi.toolkit.tls.commandLine.CommandLineParseException) GetStatusConfig(org.apache.nifi.toolkit.tls.configuration.GetStatusConfig) URISyntaxException(java.net.URISyntaxException) CommandLineParseException(org.apache.nifi.toolkit.tls.commandLine.CommandLineParseException)

Example 3 with CommandLineParseException

use of org.apache.nifi.toolkit.tls.commandLine.CommandLineParseException in project nifi by apache.

the class TlsToolkitGetStatusCommandLineTest method testSuccess.

@Test
public void testSuccess() {
    try {
        final String urlStr = "https://localhost:8443/test";
        commandLine.parse("-u", urlStr, "-ts", "src/test/resources/localhost/truststore.jks", "-tst", "JKS", "-tsp", "t7rmn1fg8np2ck1sduqdd85opv");
        final GetStatusConfig config = commandLine.createConfig();
        Assert.assertNotNull(config);
        final URI url = config.getUrl();
        Assert.assertNotNull(url);
        Assert.assertEquals(urlStr, url.toString());
        final SSLContext sslContext = config.getSslContext();
        Assert.assertNotNull(sslContext);
    } catch (CommandLineParseException e) {
        fail("Expected success");
    }
}
Also used : CommandLineParseException(org.apache.nifi.toolkit.tls.commandLine.CommandLineParseException) SSLContext(javax.net.ssl.SSLContext) URI(java.net.URI) GetStatusConfig(org.apache.nifi.toolkit.tls.configuration.GetStatusConfig) Test(org.junit.Test)

Example 4 with CommandLineParseException

use of org.apache.nifi.toolkit.tls.commandLine.CommandLineParseException in project nifi by apache.

the class TlsToolkitGetStatusCommandLine method postParse.

@Override
protected void postParse(CommandLine commandLine) throws CommandLineParseException {
    super.postParse(commandLine);
    final String urlValue = commandLine.getOptionValue(URL_ARG);
    if (StringUtils.isBlank(urlValue)) {
        printUsageAndThrow("Url was missing or blank", ExitCode.INVALID_ARGS);
    }
    try {
        this.url = new URI(urlValue);
    } catch (URISyntaxException e) {
        printUsageAndThrow("Invalid Url", ExitCode.INVALID_ARGS);
    }
    final String keystoreFilename = commandLine.getOptionValue(KEYSTORE_ARG);
    final String keystoreTypeStr = commandLine.getOptionValue(KEYSTORE_TYPE_ARG, DEFAULT_KEYSTORE_TYPE);
    final String keystorePassword = commandLine.getOptionValue(KEYSTORE_PASSWORD_ARG);
    final String keyPassword = commandLine.getOptionValue(KEY_PASSWORD_ARG);
    final String truststoreFilename = commandLine.getOptionValue(TRUSTSTORE_ARG);
    final String truststoreTypeStr = commandLine.getOptionValue(TRUSTSTORE_TYPE_ARG, DEFAULT_KEYSTORE_TYPE);
    final String truststorePassword = commandLine.getOptionValue(TRUSTSTORE_PASSWORD_ARG);
    final String protocol = commandLine.getOptionValue(PROTOCOL_ARG, DEFAULT_PROTOCOL);
    final boolean keystoreProvided = !StringUtils.isBlank(keystoreFilename);
    final boolean truststoreProvided = !StringUtils.isBlank(truststoreFilename);
    try {
        final char[] keystorePass = keystorePassword == null ? null : keystorePassword.toCharArray();
        final char[] keyPass = keyPassword == null ? null : keyPassword.toCharArray();
        final char[] trustPass = truststorePassword == null ? null : truststorePassword.toCharArray();
        if (keystoreProvided && truststoreProvided) {
            this.sslContext = SslContextFactory.createSslContext(keystoreFilename, keystorePass, keyPass, keystoreTypeStr, truststoreFilename, trustPass, truststoreTypeStr, SslContextFactory.ClientAuth.NONE, protocol);
        } else if (truststoreProvided) {
            this.sslContext = SslContextFactory.createTrustSslContext(truststoreFilename, trustPass, truststoreTypeStr, protocol);
        } else {
            printUsageAndThrow("No keystore or truststore was provided", ExitCode.INVALID_ARGS);
        }
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        printUsageAndThrow("Failed to create SSL Context: " + e.getMessage(), ExitCode.INVALID_ARGS);
    }
}
Also used : URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) CommandLineParseException(org.apache.nifi.toolkit.tls.commandLine.CommandLineParseException)

Aggregations

CommandLineParseException (org.apache.nifi.toolkit.tls.commandLine.CommandLineParseException)4 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 GetStatusConfig (org.apache.nifi.toolkit.tls.configuration.GetStatusConfig)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1 Supplier (java.util.function.Supplier)1 Collectors (java.util.stream.Collectors)1 IntStream (java.util.stream.IntStream)1 Stream (java.util.stream.Stream)1 SSLContext (javax.net.ssl.SSLContext)1 CommandLine (org.apache.commons.cli.CommandLine)1 BaseTlsToolkitCommandLine (org.apache.nifi.toolkit.tls.commandLine.BaseTlsToolkitCommandLine)1 ExitCode (org.apache.nifi.toolkit.tls.commandLine.ExitCode)1