Repository: nifi
Updated Branches:
  refs/heads/master 1a91ecc37 -> 17cb2e284


NIFI-3319 Made TLS toolkit default output directory calculation more robust.

This closes #1502.

Signed-off-by: Andy LoPresto <[email protected]>


Project: http://git-wip-us.apache.org/repos/asf/nifi/repo
Commit: http://git-wip-us.apache.org/repos/asf/nifi/commit/17cb2e28
Tree: http://git-wip-us.apache.org/repos/asf/nifi/tree/17cb2e28
Diff: http://git-wip-us.apache.org/repos/asf/nifi/diff/17cb2e28

Branch: refs/heads/master
Commit: 17cb2e284daf22a4b87864a5462ebc24c23e3f31
Parents: 1a91ecc
Author: Bryan Rosander <[email protected]>
Authored: Mon Feb 13 16:44:50 2017 -0500
Committer: Andy LoPresto <[email protected]>
Committed: Tue Feb 14 01:00:50 2017 -0800

----------------------------------------------------------------------
 .../TlsToolkitStandaloneCommandLine.java        | 45 ++++++++++++--------
 .../TlsToolkitStandaloneCommandLineTest.java    | 16 +++++++
 2 files changed, 44 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/nifi/blob/17cb2e28/nifi-toolkit/nifi-toolkit-tls/src/main/java/org/apache/nifi/toolkit/tls/standalone/TlsToolkitStandaloneCommandLine.java
----------------------------------------------------------------------
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 5db8046..fbfe782 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();
         }

http://git-wip-us.apache.org/repos/asf/nifi/blob/17cb2e28/nifi-toolkit/nifi-toolkit-tls/src/test/java/org/apache/nifi/toolkit/tls/standalone/TlsToolkitStandaloneCommandLineTest.java
----------------------------------------------------------------------
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 c025e5d..7437b84 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();

Reply via email to