Repository: nifi Updated Branches: refs/heads/master 1d74b5d3c -> 8c09bef4f
NIFI-2937 - Adding configJsonIn option to tls-toolkit client and server This closes #1158 Signed-off-by: Yolanda M. Davis <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/nifi/repo Commit: http://git-wip-us.apache.org/repos/asf/nifi/commit/8c09bef4 Tree: http://git-wip-us.apache.org/repos/asf/nifi/tree/8c09bef4 Diff: http://git-wip-us.apache.org/repos/asf/nifi/diff/8c09bef4 Branch: refs/heads/master Commit: 8c09bef4f8d06b4ee5b0b9e5d5ec88cdcd4a372c Parents: 1d74b5d Author: Bryan Rosander <[email protected]> Authored: Mon Oct 24 16:27:59 2016 -0400 Committer: Yolanda M. Davis <[email protected]> Committed: Tue Oct 25 14:30:04 2016 -0400 ---------------------------------------------------------------------- .../BaseCertificateAuthorityCommandLine.java | 32 +++++++++++------ ...lsCertificateAuthorityClientCommandLine.java | 8 +++-- ...sCertificateAuthorityServiceCommandLine.java | 7 ++-- ...rtificateAuthorityClientCommandLineTest.java | 34 ++++++++++++++++-- ...tificateAuthorityServiceCommandLineTest.java | 36 ++++++++++++++++++++ 5 files changed, 97 insertions(+), 20 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/nifi/blob/8c09bef4/nifi-toolkit/nifi-toolkit-tls/src/main/java/org/apache/nifi/toolkit/tls/service/BaseCertificateAuthorityCommandLine.java ---------------------------------------------------------------------- diff --git a/nifi-toolkit/nifi-toolkit-tls/src/main/java/org/apache/nifi/toolkit/tls/service/BaseCertificateAuthorityCommandLine.java b/nifi-toolkit/nifi-toolkit-tls/src/main/java/org/apache/nifi/toolkit/tls/service/BaseCertificateAuthorityCommandLine.java index 47ebd17..3d4a4fb 100644 --- a/nifi-toolkit/nifi-toolkit-tls/src/main/java/org/apache/nifi/toolkit/tls/service/BaseCertificateAuthorityCommandLine.java +++ b/nifi-toolkit/nifi-toolkit-tls/src/main/java/org/apache/nifi/toolkit/tls/service/BaseCertificateAuthorityCommandLine.java @@ -32,14 +32,15 @@ import java.io.File; public abstract class BaseCertificateAuthorityCommandLine extends BaseCommandLine { public static final String TOKEN_ARG = "token"; public static final String CONFIG_JSON_ARG = "configJson"; + public static final String READ_CONFIG_JSON_ARG = "configJsonIn"; public static final String USE_CONFIG_JSON_ARG = "useConfigJson"; - public static final String PORT_ARG = "PORT"; + public static final String PORT_ARG = "PORT"; public static final String DEFAULT_CONFIG_JSON = new File("config.json").getPath(); private String token; - private String configJson; - private boolean onlyUseConfigJson; + private String configJsonOut; + private String configJsonIn; private int port; private String dn; @@ -47,6 +48,8 @@ public abstract class BaseCertificateAuthorityCommandLine extends BaseCommandLin super(header); addOptionWithArg("t", TOKEN_ARG, getTokenDescription()); addOptionWithArg("f", CONFIG_JSON_ARG, "The place to write configuration info", DEFAULT_CONFIG_JSON); + addOptionWithArg(null, READ_CONFIG_JSON_ARG, "The place to read configuration info from (defaults to the value of " + CONFIG_JSON_ARG + "), implies " + + USE_CONFIG_JSON_ARG + " if set.", CONFIG_JSON_ARG + " value"); addOptionNoArg("F", USE_CONFIG_JSON_ARG, "Flag specifying that all configuration is read from " + CONFIG_JSON_ARG + " to facilitate automated use (otherwise " + CONFIG_JSON_ARG + " will only be written to."); addOptionWithArg("p", PORT_ARG, getPortDescription(), TlsConfig.DEFAULT_PORT); @@ -66,11 +69,18 @@ public abstract class BaseCertificateAuthorityCommandLine extends BaseCommandLin CommandLine commandLine = super.doParse(args); token = commandLine.getOptionValue(TOKEN_ARG); - onlyUseConfigJson = commandLine.hasOption(USE_CONFIG_JSON_ARG); - if (StringUtils.isEmpty(token) && !onlyUseConfigJson) { - printUsageAndThrow(TOKEN_ARG + " argument must not be empty unless " + USE_CONFIG_JSON_ARG + " set", ExitCode.ERROR_TOKEN_ARG_EMPTY); + + boolean useConfigJson = commandLine.hasOption(USE_CONFIG_JSON_ARG); + + configJsonOut = commandLine.getOptionValue(CONFIG_JSON_ARG, DEFAULT_CONFIG_JSON); + configJsonIn = commandLine.getOptionValue(READ_CONFIG_JSON_ARG); + if (StringUtils.isEmpty(configJsonIn) && useConfigJson) { + configJsonIn = configJsonOut; + } + + if (StringUtils.isEmpty(token) && StringUtils.isEmpty(configJsonIn)) { + printUsageAndThrow(TOKEN_ARG + " argument must not be empty unless " + USE_CONFIG_JSON_ARG + " or " + READ_CONFIG_JSON_ARG+ " set", ExitCode.ERROR_TOKEN_ARG_EMPTY); } - configJson = commandLine.getOptionValue(CONFIG_JSON_ARG, DEFAULT_CONFIG_JSON); port = getIntValue(commandLine, PORT_ARG, TlsConfig.DEFAULT_PORT); dn = commandLine.getOptionValue(DN_ARG, TlsConfig.calcDefaultDn(getDnHostname())); return commandLine; @@ -80,12 +90,12 @@ public abstract class BaseCertificateAuthorityCommandLine extends BaseCommandLin return token; } - public String getConfigJson() { - return configJson; + public String getConfigJsonOut() { + return configJsonOut; } - public boolean onlyUseConfigJson() { - return onlyUseConfigJson; + public String getConfigJsonIn() { + return configJsonIn; } public int getPort() { http://git-wip-us.apache.org/repos/asf/nifi/blob/8c09bef4/nifi-toolkit/nifi-toolkit-tls/src/main/java/org/apache/nifi/toolkit/tls/service/client/TlsCertificateAuthorityClientCommandLine.java ---------------------------------------------------------------------- diff --git a/nifi-toolkit/nifi-toolkit-tls/src/main/java/org/apache/nifi/toolkit/tls/service/client/TlsCertificateAuthorityClientCommandLine.java b/nifi-toolkit/nifi-toolkit-tls/src/main/java/org/apache/nifi/toolkit/tls/service/client/TlsCertificateAuthorityClientCommandLine.java index 83b28be..16725ad 100644 --- a/nifi-toolkit/nifi-toolkit-tls/src/main/java/org/apache/nifi/toolkit/tls/service/client/TlsCertificateAuthorityClientCommandLine.java +++ b/nifi-toolkit/nifi-toolkit-tls/src/main/java/org/apache/nifi/toolkit/tls/service/client/TlsCertificateAuthorityClientCommandLine.java @@ -25,6 +25,7 @@ import org.apache.nifi.toolkit.tls.configuration.TlsClientConfig; import org.apache.nifi.toolkit.tls.service.BaseCertificateAuthorityCommandLine; import org.apache.nifi.toolkit.tls.util.InputStreamFactory; import org.apache.nifi.toolkit.tls.util.TlsHelper; +import org.apache.nifi.util.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -67,7 +68,7 @@ public class TlsCertificateAuthorityClientCommandLine extends BaseCertificateAut System.exit(e.getExitCode().ordinal()); } new TlsCertificateAuthorityClient().generateCertificateAndGetItSigned(tlsCertificateAuthorityClientCommandLine.createClientConfig(), - tlsCertificateAuthorityClientCommandLine.getCertificateDirectory(), tlsCertificateAuthorityClientCommandLine.getConfigJson(), + tlsCertificateAuthorityClientCommandLine.getCertificateDirectory(), tlsCertificateAuthorityClientCommandLine.getConfigJsonOut(), tlsCertificateAuthorityClientCommandLine.differentPasswordForKeyAndKeystore()); System.exit(ExitCode.SUCCESS.ordinal()); } @@ -119,8 +120,9 @@ public class TlsCertificateAuthorityClientCommandLine extends BaseCertificateAut } public TlsClientConfig createClientConfig() throws IOException { - if (onlyUseConfigJson()) { - try (InputStream inputStream = inputStreamFactory.create(new File(getConfigJson()))) { + String configJsonIn = getConfigJsonIn(); + if (!StringUtils.isEmpty(configJsonIn)) { + try (InputStream inputStream = inputStreamFactory.create(new File(configJsonIn))) { TlsClientConfig tlsClientConfig = new ObjectMapper().readValue(inputStream, TlsClientConfig.class); tlsClientConfig.initDefaults(); return tlsClientConfig; http://git-wip-us.apache.org/repos/asf/nifi/blob/8c09bef4/nifi-toolkit/nifi-toolkit-tls/src/main/java/org/apache/nifi/toolkit/tls/service/server/TlsCertificateAuthorityServiceCommandLine.java ---------------------------------------------------------------------- diff --git a/nifi-toolkit/nifi-toolkit-tls/src/main/java/org/apache/nifi/toolkit/tls/service/server/TlsCertificateAuthorityServiceCommandLine.java b/nifi-toolkit/nifi-toolkit-tls/src/main/java/org/apache/nifi/toolkit/tls/service/server/TlsCertificateAuthorityServiceCommandLine.java index 948409a..d02074c 100644 --- a/nifi-toolkit/nifi-toolkit-tls/src/main/java/org/apache/nifi/toolkit/tls/service/server/TlsCertificateAuthorityServiceCommandLine.java +++ b/nifi-toolkit/nifi-toolkit-tls/src/main/java/org/apache/nifi/toolkit/tls/service/server/TlsCertificateAuthorityServiceCommandLine.java @@ -57,15 +57,16 @@ public class TlsCertificateAuthorityServiceCommandLine extends BaseCertificateAu System.exit(e.getExitCode().ordinal()); } TlsCertificateAuthorityService tlsCertificateAuthorityService = new TlsCertificateAuthorityService(); - tlsCertificateAuthorityService.start(tlsCertificateAuthorityServiceCommandLine.createConfig(), tlsCertificateAuthorityServiceCommandLine.getConfigJson(), + tlsCertificateAuthorityService.start(tlsCertificateAuthorityServiceCommandLine.createConfig(), tlsCertificateAuthorityServiceCommandLine.getConfigJsonOut(), tlsCertificateAuthorityServiceCommandLine.differentPasswordForKeyAndKeystore()); System.out.println("Server Started"); System.out.flush(); } public TlsConfig createConfig() throws IOException { - if (onlyUseConfigJson()) { - try (InputStream inputStream = inputStreamFactory.create(new File(getConfigJson()))) { + String configJsonIn = getConfigJsonIn(); + if (!StringUtils.isEmpty(configJsonIn)) { + try (InputStream inputStream = inputStreamFactory.create(new File(configJsonIn))) { TlsConfig tlsConfig = new ObjectMapper().readValue(inputStream, TlsConfig.class); tlsConfig.initDefaults(); return tlsConfig; http://git-wip-us.apache.org/repos/asf/nifi/blob/8c09bef4/nifi-toolkit/nifi-toolkit-tls/src/test/java/org/apache/nifi/toolkit/tls/service/client/TlsCertificateAuthorityClientCommandLineTest.java ---------------------------------------------------------------------- diff --git a/nifi-toolkit/nifi-toolkit-tls/src/test/java/org/apache/nifi/toolkit/tls/service/client/TlsCertificateAuthorityClientCommandLineTest.java b/nifi-toolkit/nifi-toolkit-tls/src/test/java/org/apache/nifi/toolkit/tls/service/client/TlsCertificateAuthorityClientCommandLineTest.java index 3770cb7..b4e8748 100644 --- a/nifi-toolkit/nifi-toolkit-tls/src/test/java/org/apache/nifi/toolkit/tls/service/client/TlsCertificateAuthorityClientCommandLineTest.java +++ b/nifi-toolkit/nifi-toolkit-tls/src/test/java/org/apache/nifi/toolkit/tls/service/client/TlsCertificateAuthorityClientCommandLineTest.java @@ -21,6 +21,7 @@ import org.apache.nifi.toolkit.tls.commandLine.CommandLineParseException; import org.apache.nifi.toolkit.tls.commandLine.ExitCode; import org.apache.nifi.toolkit.tls.configuration.TlsClientConfig; import org.apache.nifi.toolkit.tls.configuration.TlsConfig; +import org.apache.nifi.toolkit.tls.service.BaseCertificateAuthorityCommandLine; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -71,7 +72,8 @@ public class TlsCertificateAuthorityClientCommandLineTest { assertEquals(TlsConfig.DEFAULT_KEY_PAIR_ALGORITHM, clientConfig.getKeyPairAlgorithm()); assertEquals(testToken, clientConfig.getToken()); assertEquals(TlsConfig.DEFAULT_PORT, clientConfig.getPort()); - assertEquals(TlsCertificateAuthorityClientCommandLine.DEFAULT_CONFIG_JSON, tlsCertificateAuthorityClientCommandLine.getConfigJson()); + assertEquals(TlsCertificateAuthorityClientCommandLine.DEFAULT_CONFIG_JSON, tlsCertificateAuthorityClientCommandLine.getConfigJsonOut()); + assertNull(tlsCertificateAuthorityClientCommandLine.getConfigJsonIn()); assertEquals(TlsCertificateAuthorityClientCommandLine.DEFAULT_CERTIFICATE_DIRECTORY, tlsCertificateAuthorityClientCommandLine.getCertificateDirectory()); } @@ -133,10 +135,36 @@ public class TlsCertificateAuthorityClientCommandLineTest { } @Test - public void testConfigFile() throws CommandLineParseException { + public void testConfigJsonOut() throws CommandLineParseException { String testPath = "/1/2/3/4"; tlsCertificateAuthorityClientCommandLine.parse("-t", testToken, "-f", testPath); - assertEquals(testPath, tlsCertificateAuthorityClientCommandLine.getConfigJson()); + assertEquals(testPath, tlsCertificateAuthorityClientCommandLine.getConfigJsonOut()); + assertNull(tlsCertificateAuthorityClientCommandLine.getConfigJsonIn()); + } + + @Test + public void testConfigJsonOutAndUseForBoth() throws CommandLineParseException { + String testPath = "/1/2/3/4"; + tlsCertificateAuthorityClientCommandLine.parse("-t", testToken, "-f", testPath, "-F"); + assertEquals(testPath, tlsCertificateAuthorityClientCommandLine.getConfigJsonOut()); + assertEquals(testPath, tlsCertificateAuthorityClientCommandLine.getConfigJsonIn()); + } + + @Test + public void testConfigJsonIn() throws CommandLineParseException { + String testPath = "/1/2/3/4"; + tlsCertificateAuthorityClientCommandLine.parse("-t", testToken, "--" + BaseCertificateAuthorityCommandLine.READ_CONFIG_JSON_ARG, testPath); + assertEquals(BaseCertificateAuthorityCommandLine.DEFAULT_CONFIG_JSON, tlsCertificateAuthorityClientCommandLine.getConfigJsonOut()); + assertEquals(testPath, tlsCertificateAuthorityClientCommandLine.getConfigJsonIn()); + } + + @Test + public void testConfigJsonInAndOut() throws CommandLineParseException { + String testPath = "/1/2/3/4"; + String testIn = "/2/3/4/5"; + tlsCertificateAuthorityClientCommandLine.parse("-t", testToken, "-f", testPath, "--" + BaseCertificateAuthorityCommandLine.READ_CONFIG_JSON_ARG, testIn); + assertEquals(testPath, tlsCertificateAuthorityClientCommandLine.getConfigJsonOut()); + assertEquals(testIn, tlsCertificateAuthorityClientCommandLine.getConfigJsonIn()); } @Test http://git-wip-us.apache.org/repos/asf/nifi/blob/8c09bef4/nifi-toolkit/nifi-toolkit-tls/src/test/java/org/apache/nifi/toolkit/tls/service/server/TlsCertificateAuthorityServiceCommandLineTest.java ---------------------------------------------------------------------- diff --git a/nifi-toolkit/nifi-toolkit-tls/src/test/java/org/apache/nifi/toolkit/tls/service/server/TlsCertificateAuthorityServiceCommandLineTest.java b/nifi-toolkit/nifi-toolkit-tls/src/test/java/org/apache/nifi/toolkit/tls/service/server/TlsCertificateAuthorityServiceCommandLineTest.java index 9654186..eaf56f1 100644 --- a/nifi-toolkit/nifi-toolkit-tls/src/test/java/org/apache/nifi/toolkit/tls/service/server/TlsCertificateAuthorityServiceCommandLineTest.java +++ b/nifi-toolkit/nifi-toolkit-tls/src/test/java/org/apache/nifi/toolkit/tls/service/server/TlsCertificateAuthorityServiceCommandLineTest.java @@ -19,6 +19,7 @@ package org.apache.nifi.toolkit.tls.service.server; import org.apache.nifi.toolkit.tls.commandLine.CommandLineParseException; import org.apache.nifi.toolkit.tls.configuration.TlsConfig; +import org.apache.nifi.toolkit.tls.service.BaseCertificateAuthorityCommandLine; import org.apache.nifi.toolkit.tls.util.InputStreamFactory; import org.junit.Before; import org.junit.Test; @@ -49,6 +50,8 @@ public class TlsCertificateAuthorityServiceCommandLineTest { @Test public void testDefaults() throws CommandLineParseException, IOException { tlsCertificateAuthorityServiceCommandLine.parse("-t", testToken); + assertEquals(BaseCertificateAuthorityCommandLine.DEFAULT_CONFIG_JSON, tlsCertificateAuthorityServiceCommandLine.getConfigJsonOut()); + assertNull(tlsCertificateAuthorityServiceCommandLine.getConfigJsonIn()); TlsConfig tlsConfig = tlsCertificateAuthorityServiceCommandLine.createConfig(); assertEquals(TlsConfig.DEFAULT_HOSTNAME, tlsConfig.getCaHostname()); assertEquals(testToken, tlsConfig.getToken()); @@ -113,4 +116,37 @@ public class TlsCertificateAuthorityServiceCommandLineTest { tlsCertificateAuthorityServiceCommandLine.parse("-t", testToken, "-d", Integer.toString(days)); assertEquals(days, tlsCertificateAuthorityServiceCommandLine.createConfig().getDays()); } + + @Test + public void testConfigJsonOut() throws CommandLineParseException { + String out = "testJson.out"; + tlsCertificateAuthorityServiceCommandLine.parse("-t", testToken, "-f", out); + assertEquals(out, tlsCertificateAuthorityServiceCommandLine.getConfigJsonOut()); + assertNull(tlsCertificateAuthorityServiceCommandLine.getConfigJsonIn()); + } + + @Test + public void testConfigJsonOutAndUseForBoth() throws CommandLineParseException { + String out = "testJson.out"; + tlsCertificateAuthorityServiceCommandLine.parse("-t", testToken, "-f", out, "-F"); + assertEquals(out, tlsCertificateAuthorityServiceCommandLine.getConfigJsonOut()); + assertEquals(out, tlsCertificateAuthorityServiceCommandLine.getConfigJsonIn()); + } + + @Test + public void testConfigJsonIn() throws CommandLineParseException { + String in = "testJson.in"; + tlsCertificateAuthorityServiceCommandLine.parse("-t", testToken, "--" + BaseCertificateAuthorityCommandLine.READ_CONFIG_JSON_ARG, in); + assertEquals(BaseCertificateAuthorityCommandLine.DEFAULT_CONFIG_JSON, tlsCertificateAuthorityServiceCommandLine.getConfigJsonOut()); + assertEquals(in, tlsCertificateAuthorityServiceCommandLine.getConfigJsonIn()); + } + + @Test + public void testConfigJsonInAndOut() throws CommandLineParseException { + String out = "testJson.out"; + String in = "testJson.in"; + tlsCertificateAuthorityServiceCommandLine.parse("-t", testToken, "-f", out, "--" + BaseCertificateAuthorityCommandLine.READ_CONFIG_JSON_ARG, in); + assertEquals(out, tlsCertificateAuthorityServiceCommandLine.getConfigJsonOut()); + assertEquals(in, tlsCertificateAuthorityServiceCommandLine.getConfigJsonIn()); + } }
