From c8abf5f20a7ca802e3e7c93c8c5d260902cb4052 Mon Sep 17 00:00:00 2001 From: Andrew Wang Date: Tue, 5 Aug 2014 00:20:30 +0000 Subject: [PATCH] HADOOP-10927. Fix CredentialShell help behavior and error codes. Contributed by Josh Elser. git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1615827 13f79535-47bb-0310-9956-ffa450edef68 --- .../hadoop-common/CHANGES.txt | 3 ++ .../hadoop-common/src/main/bin/hadoop | 1 + .../security/alias/CredentialShell.java | 29 ++++++++++++------- .../hadoop/security/alias/TestCredShell.java | 29 +++++++++++++++---- 4 files changed, 46 insertions(+), 16 deletions(-) diff --git a/hadoop-common-project/hadoop-common/CHANGES.txt b/hadoop-common-project/hadoop-common/CHANGES.txt index 6542cb7f84..c9726dff07 100644 --- a/hadoop-common-project/hadoop-common/CHANGES.txt +++ b/hadoop-common-project/hadoop-common/CHANGES.txt @@ -519,6 +519,9 @@ Release 2.6.0 - UNRELEASED HADOOP-10928. Incorrect usage on `hadoop credential list`. (Josh Elser via wang) + HADOOP-10927. Fix CredentialShell help behavior and error codes. + (Josh Elser via wang) + Release 2.5.0 - UNRELEASED INCOMPATIBLE CHANGES diff --git a/hadoop-common-project/hadoop-common/src/main/bin/hadoop b/hadoop-common-project/hadoop-common/src/main/bin/hadoop index 55c843ff77..b1e2018f61 100755 --- a/hadoop-common-project/hadoop-common/src/main/bin/hadoop +++ b/hadoop-common-project/hadoop-common/src/main/bin/hadoop @@ -35,6 +35,7 @@ function print_usage(){ echo " distcp copy file or directories recursively" echo " archive -archiveName NAME -p * create a hadoop archive" echo " classpath prints the class path needed to get the" + echo " credential interact with credential providers" echo " Hadoop jar and the required libraries" echo " daemonlog get/set the log level for each daemon" echo " or" diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/security/alias/CredentialShell.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/security/alias/CredentialShell.java index 0ac3994317..6d9c6af263 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/security/alias/CredentialShell.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/security/alias/CredentialShell.java @@ -67,11 +67,11 @@ public int run(String[] args) throws Exception { if (command.validate()) { command.execute(); } else { - exitCode = -1; + exitCode = 1; } } catch (Exception e) { e.printStackTrace(err); - return -1; + return 1; } return exitCode; } @@ -79,29 +79,36 @@ public int run(String[] args) throws Exception { /** * Parse the command line arguments and initialize the data *
-   * % hadoop alias create alias [-provider providerPath]
-   * % hadoop alias list [-provider providerPath]
-   * % hadoop alias delete alias [-provider providerPath] [-i]
+   * % hadoop credential create alias [-provider providerPath]
+   * % hadoop credential list [-provider providerPath]
+   * % hadoop credential delete alias [-provider providerPath] [-i]
    * 
* @param args - * @return + * @return 0 if the argument(s) were recognized, 1 otherwise * @throws IOException */ - private int init(String[] args) throws IOException { + protected int init(String[] args) throws IOException { + // no args should print the help message + if (0 == args.length) { + printCredShellUsage(); + ToolRunner.printGenericCommandUsage(System.err); + return 1; + } + for (int i = 0; i < args.length; i++) { // parse command line if (args[i].equals("create")) { String alias = args[++i]; command = new CreateCommand(alias); if (alias.equals("-help")) { printCredShellUsage(); - return -1; + return 0; } } else if (args[i].equals("delete")) { String alias = args[++i]; command = new DeleteCommand(alias); if (alias.equals("-help")) { printCredShellUsage(); - return -1; + return 0; } } else if (args[i].equals("list")) { command = new ListCommand(); @@ -115,11 +122,11 @@ private int init(String[] args) throws IOException { value = args[++i]; } else if (args[i].equals("-help")) { printCredShellUsage(); - return -1; + return 0; } else { printCredShellUsage(); ToolRunner.printGenericCommandUsage(System.err); - return -1; + return 1; } } return 0; diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/security/alias/TestCredShell.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/security/alias/TestCredShell.java index 05eb7b8c2a..b9f0dc937c 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/security/alias/TestCredShell.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/security/alias/TestCredShell.java @@ -17,16 +17,18 @@ */ package org.apache.hadoop.security.alias; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.PrintStream; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import org.apache.hadoop.conf.Configuration; -import org.apache.hadoop.security.alias.CredentialShell.PasswordReader; import org.junit.Before; import org.junit.Test; @@ -87,7 +89,7 @@ public void testInvalidProvider() throws Exception { CredentialShell cs = new CredentialShell(); cs.setConf(new Configuration()); rc = cs.run(args1); - assertEquals(-1, rc); + assertEquals(1, rc); assertTrue(outContent.toString().contains("There are no valid " + "CredentialProviders configured.")); } @@ -122,7 +124,7 @@ public void testTransientProviderOnlyConfig() throws Exception { config.set(CredentialProviderFactory.CREDENTIAL_PROVIDER_PATH, "user:///"); cs.setConf(config); rc = cs.run(args1); - assertEquals(-1, rc); + assertEquals(1, rc); assertTrue(outContent.toString().contains("There are no valid " + "CredentialProviders configured.")); } @@ -139,7 +141,7 @@ public void testPromptForCredentialWithEmptyPasswd() throws Exception { shell.setConf(new Configuration()); shell.setPasswordReader(new MockPasswordReader(passwords)); rc = shell.run(args1); - assertEquals(outContent.toString(), -1, rc); + assertEquals(outContent.toString(), 1, rc); assertTrue(outContent.toString().contains("Passwords don't match")); } @@ -186,4 +188,21 @@ public void format(String message) { System.out.println(message); } } + + @Test + public void testEmptyArgList() throws Exception { + CredentialShell shell = new CredentialShell(); + shell.setConf(new Configuration()); + assertEquals(1, shell.init(new String[0])); + } + + @Test + public void testCommandHelpExitsNormally() throws Exception { + for (String cmd : Arrays.asList("create", "list", "delete")) { + CredentialShell shell = new CredentialShell(); + shell.setConf(new Configuration()); + assertEquals("Expected help argument on " + cmd + " to return 0", + 0, shell.init(new String[] {cmd, "-help"})); + } + } }