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;
}
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());
}
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");
}
}
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);
}
}
Aggregations