NIFI-2937 - Adding configJsonIn option to tls-toolkit client and server

This closes #1158

Signed-off-by: Yolanda M. Davis <ymdavis@apache.org>
This commit is contained in:
Bryan Rosander 2016-10-24 16:27:59 -04:00 committed by Yolanda M. Davis
parent 1d74b5d3ce
commit 8c09bef4f8
5 changed files with 97 additions and 20 deletions

View File

@ -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() {

View File

@ -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;

View File

@ -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;

View File

@ -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

View File

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