diff --git a/hadoop-common-project/hadoop-common/CHANGES.txt b/hadoop-common-project/hadoop-common/CHANGES.txt index ab3adbde703..3b858ff5bd0 100644 --- a/hadoop-common-project/hadoop-common/CHANGES.txt +++ b/hadoop-common-project/hadoop-common/CHANGES.txt @@ -218,6 +218,8 @@ Release 2.6.0 - UNRELEASED HADOOP-10583. bin/hadoop key throws NPE with no args and assorted other fixups. (clamb via tucu) + HADOOP-10586. KeyShell doesn't allow setting Options via CLI. (clamb via tucu) + Release 2.5.0 - 2014-08-11 INCOMPATIBLE CHANGES diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/crypto/key/KeyShell.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/crypto/key/KeyShell.java index e31f1e1021a..72cd13d1275 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/crypto/key/KeyShell.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/crypto/key/KeyShell.java @@ -89,6 +89,8 @@ public class KeyShell extends Configured implements Tool { * @throws IOException */ private int init(String[] args) throws IOException { + final Options options = KeyProvider.options(getConf()); + for (int i = 0; i < args.length; i++) { // parse command line boolean moreTokens = (i < args.length - 1); if (args[i].equals("create")) { @@ -97,7 +99,7 @@ public class KeyShell extends Configured implements Tool { keyName = args[++i]; } - command = new CreateCommand(keyName); + command = new CreateCommand(keyName, options); if ("--help".equals(keyName)) { printKeyShellUsage(); return -1; @@ -127,9 +129,11 @@ public class KeyShell extends Configured implements Tool { } else if ("list".equals(args[i])) { command = new ListCommand(); } else if ("--size".equals(args[i]) && moreTokens) { - getConf().set(KeyProvider.DEFAULT_BITLENGTH_NAME, args[++i]); + options.setBitLength(Integer.parseInt(args[++i])); } else if ("--cipher".equals(args[i]) && moreTokens) { - getConf().set(KeyProvider.DEFAULT_CIPHER_NAME, args[++i]); + options.setCipher(args[++i]); + } else if ("--description".equals(args[i]) && moreTokens) { + options.setDescription(args[++i]); } else if ("--provider".equals(args[i]) && moreTokens) { userSuppliedProvider = true; getConf().set(KeyProviderFactory.KEY_PROVIDER_PATH, args[++i]); @@ -399,6 +403,7 @@ public class KeyShell extends Configured implements Tool { private class CreateCommand extends Command { public static final String USAGE = "create [--cipher ] [--size ]\n" + + " [--description ]\n" + " [--provider ] [--help]"; public static final String DESC = "The create subcommand creates a new key for the name specified\n" + @@ -408,10 +413,12 @@ public class KeyShell extends Configured implements Tool { "The default keysize is 256. You may specify the requested key\n" + "length using the --size argument.\n"; - String keyName = null; + final String keyName; + final Options options; - public CreateCommand(String keyName) { + public CreateCommand(String keyName, Options options) { this.keyName = keyName; + this.options = options; } public boolean validate() { @@ -434,7 +441,6 @@ public class KeyShell extends Configured implements Tool { public void execute() throws IOException, NoSuchAlgorithmException { warnIfTransientProvider(); try { - Options options = KeyProvider.options(getConf()); provider.createKey(keyName, options); out.println(keyName + " has been successfully created."); provider.flush(); diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/crypto/key/TestKeyShell.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/crypto/key/TestKeyShell.java index 7faa1359e75..121b87d8cd9 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/crypto/key/TestKeyShell.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/crypto/key/TestKeyShell.java @@ -111,6 +111,30 @@ public class TestKeyShell { assertFalse(outContent.toString(), outContent.toString().contains("key1")); } + /* HADOOP-10586 KeyShell didn't allow -description. */ + @Test + public void testKeySuccessfulCreationWithDescription() throws Exception { + outContent.reset(); + String[] args1 = {"create", "key1", "--provider", + "jceks://file" + tmpDir + "/keystore.jceks", + "--description", "someDescription"}; + int rc = 0; + KeyShell ks = new KeyShell(); + ks.setConf(new Configuration()); + rc = ks.run(args1); + assertEquals(0, rc); + assertTrue(outContent.toString().contains("key1 has been successfully " + + "created.")); + + outContent.reset(); + String[] args2a = {"list", "--metadata", "--provider", + "jceks://file" + tmpDir + "/keystore.jceks"}; + rc = ks.run(args2a); + assertEquals(0, rc); + assertTrue(outContent.toString().contains("description")); + assertTrue(outContent.toString().contains("someDescription")); + } + @Test public void testInvalidKeySize() throws Exception { String[] args1 = {"create", "key1", "--size", "56", "--provider",