From 17cb2e284daf22a4b87864a5462ebc24c23e3f31 Mon Sep 17 00:00:00 2001 From: Bryan Rosander Date: Mon, 13 Feb 2017 16:44:50 -0500 Subject: [PATCH] NIFI-3319 Made TLS toolkit default output directory calculation more robust. This closes #1502. Signed-off-by: Andy LoPresto --- .../TlsToolkitStandaloneCommandLine.java | 45 ++++++++++++------- .../TlsToolkitStandaloneCommandLineTest.java | 16 +++++++ 2 files changed, 44 insertions(+), 17 deletions(-) diff --git a/nifi-toolkit/nifi-toolkit-tls/src/main/java/org/apache/nifi/toolkit/tls/standalone/TlsToolkitStandaloneCommandLine.java b/nifi-toolkit/nifi-toolkit-tls/src/main/java/org/apache/nifi/toolkit/tls/standalone/TlsToolkitStandaloneCommandLine.java index 5db8046e65..fbfe782c7c 100644 --- a/nifi-toolkit/nifi-toolkit-tls/src/main/java/org/apache/nifi/toolkit/tls/standalone/TlsToolkitStandaloneCommandLine.java +++ b/nifi-toolkit/nifi-toolkit-tls/src/main/java/org/apache/nifi/toolkit/tls/standalone/TlsToolkitStandaloneCommandLine.java @@ -17,6 +17,18 @@ package org.apache.nifi.toolkit.tls.standalone; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.function.Supplier; +import java.util.stream.Collectors; +import java.util.stream.IntStream; +import java.util.stream.Stream; import org.apache.commons.cli.CommandLine; import org.apache.nifi.toolkit.tls.commandLine.BaseCommandLine; import org.apache.nifi.toolkit.tls.commandLine.CommandLineParseException; @@ -30,18 +42,6 @@ import org.apache.nifi.util.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.nio.file.Paths; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.function.Supplier; -import java.util.stream.Collectors; -import java.util.stream.IntStream; -import java.util.stream.Stream; - /** * Command line parser for a StandaloneConfig object and a main entry point to invoke the parser and run the standalone generator */ @@ -59,7 +59,18 @@ public class TlsToolkitStandaloneCommandLine extends BaseCommandLine { public static final String NIFI_DN_PREFIX_ARG = "nifiDnPrefix"; public static final String NIFI_DN_SUFFIX_ARG = "nifiDnSuffix"; - public static final String DEFAULT_OUTPUT_DIRECTORY = "../" + Paths.get(".").toAbsolutePath().normalize().getFileName().toString(); + public static final String DEFAULT_OUTPUT_DIRECTORY = calculateDefaultOutputDirectory(Paths.get(".")); + + protected static String calculateDefaultOutputDirectory(Path currentPath) { + Path currentAbsolutePath = currentPath.toAbsolutePath(); + Path parent = currentAbsolutePath.getParent(); + if (parent == currentAbsolutePath.getRoot()) { + return parent.toString(); + } else { + Path currentNormalizedPath = currentAbsolutePath.normalize(); + return "../" + currentNormalizedPath.getFileName().toString(); + } + } public static final String DESCRIPTION = "Creates certificates and config files for nifi cluster."; @@ -131,10 +142,10 @@ public class TlsToolkitStandaloneCommandLine extends BaseCommandLine { 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()))); + 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(); } diff --git a/nifi-toolkit/nifi-toolkit-tls/src/test/java/org/apache/nifi/toolkit/tls/standalone/TlsToolkitStandaloneCommandLineTest.java b/nifi-toolkit/nifi-toolkit-tls/src/test/java/org/apache/nifi/toolkit/tls/standalone/TlsToolkitStandaloneCommandLineTest.java index c025e5d037..7437b841d4 100644 --- a/nifi-toolkit/nifi-toolkit-tls/src/test/java/org/apache/nifi/toolkit/tls/standalone/TlsToolkitStandaloneCommandLineTest.java +++ b/nifi-toolkit/nifi-toolkit-tls/src/test/java/org/apache/nifi/toolkit/tls/standalone/TlsToolkitStandaloneCommandLineTest.java @@ -34,6 +34,8 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; import java.security.SecureRandom; import java.util.Collections; import java.util.List; @@ -414,6 +416,20 @@ public class TlsToolkitStandaloneCommandLineTest { tlsToolkitStandaloneCommandLine.parse("-n", "notInGlobalOrder", "-G", "nifi[1-3]"); } + @Test + public void testDefaultOutputPathRoot() { + Path root = Paths.get(".").toAbsolutePath().getRoot().resolve("."); + String calculateDefaultOutputDirectory = TlsToolkitStandaloneCommandLine.calculateDefaultOutputDirectory(root); + assertEquals(root.toAbsolutePath().getRoot().toString(), calculateDefaultOutputDirectory); + } + + @Test + public void testDefaultOutputPath() { + Path path = Paths.get("."); + String calculateDefaultOutputDirectory = TlsToolkitStandaloneCommandLine.calculateDefaultOutputDirectory(path); + assertEquals("../" + path.toAbsolutePath().normalize().getFileName().toString(), calculateDefaultOutputDirectory); + } + private Properties getProperties() throws IOException { NiFiPropertiesWriter niFiPropertiesWriter = tlsToolkitStandaloneCommandLine.createConfig().getNiFiPropertiesWriterFactory().create(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();