Repository: hadoop Updated Branches: refs/heads/branch-2.6 9195e81fd -> 4bc2ece80
HADOOP-13434. Add bash quoting to Shell class. (Owen O'Malley) (cherry picked from commit 49a95718754cb6d302371b2421e86da7bce50297) Project: http://git-wip-us.apache.org/repos/asf/hadoop/repo Commit: http://git-wip-us.apache.org/repos/asf/hadoop/commit/4bc2ece8 Tree: http://git-wip-us.apache.org/repos/asf/hadoop/tree/4bc2ece8 Diff: http://git-wip-us.apache.org/repos/asf/hadoop/diff/4bc2ece8 Branch: refs/heads/branch-2.6 Commit: 4bc2ece80bbb1b48fc7593ec0ac83596005b1d0d Parents: 9195e81 Author: Arpit Agarwal <[email protected]> Authored: Wed Aug 3 14:28:44 2016 -0700 Committer: Sangjin Lee <[email protected]> Committed: Wed Sep 14 22:08:17 2016 -0700 ---------------------------------------------------------------------- hadoop-common-project/hadoop-common/CHANGES.txt | 2 + .../main/java/org/apache/hadoop/util/Shell.java | 50 +++++++++++++++----- .../java/org/apache/hadoop/util/TestShell.java | 7 +++ 3 files changed, 47 insertions(+), 12 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/hadoop/blob/4bc2ece8/hadoop-common-project/hadoop-common/CHANGES.txt ---------------------------------------------------------------------- diff --git a/hadoop-common-project/hadoop-common/CHANGES.txt b/hadoop-common-project/hadoop-common/CHANGES.txt index f06ca46..7ca53c3 100644 --- a/hadoop-common-project/hadoop-common/CHANGES.txt +++ b/hadoop-common-project/hadoop-common/CHANGES.txt @@ -65,6 +65,8 @@ Release 2.6.5 - UNRELEASED HADOOP-11361. Fix a race condition in MetricsSourceAdapter.updateJmxCache. (Vinayakumar B, Yongjun Zhang, and Brahma Reddy Battula via ozawa) + HADOOP-13434. Add quoting to Shell class. (Owen O'Malley) + Release 2.6.4 - 2016-02-11 INCOMPATIBLE CHANGES http://git-wip-us.apache.org/repos/asf/hadoop/blob/4bc2ece8/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/Shell.java ---------------------------------------------------------------------- diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/Shell.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/Shell.java index 3740d51..1e25517 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/Shell.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/Shell.java @@ -82,8 +82,23 @@ abstract public class Shell { } } - /** a Unix command to get the current user's name */ - public final static String USER_NAME_COMMAND = "whoami"; + /** + * Quote the given arg so that bash will interpret it as a single value. + * Note that this quotes it for one level of bash, if you are passing it + * into a badly written shell script, you need to fix your shell script. + * @param arg the argument to quote + * @return the quoted string + */ + static String bashQuote(String arg) { + StringBuilder buffer = new StringBuilder(arg.length() + 2); + buffer.append('\''); + buffer.append(arg.replace("'", "'\\''")); + buffer.append('\''); + return buffer.toString(); + } + + /** a Unix command to get the current user's name: {@value}. */ + public static final String USER_NAME_COMMAND = "whoami"; /** Windows CreateProcess synchronization object */ public static final Object WindowsProcessLaunchLock = new Object(); @@ -133,7 +148,7 @@ abstract public class Shell { /** a Unix command to get the current user's groups list */ public static String[] getGroupsCommand() { return (WINDOWS)? new String[]{"cmd", "/c", "groups"} - : new String[]{"bash", "-c", "groups"}; + : new String[]{"groups"}; } /** @@ -143,17 +158,21 @@ abstract public class Shell { * i.e. the user's primary group will be included twice. */ public static String[] getGroupsForUserCommand(final String user) { - //'groups username' command return is non-consistent across different unixes - return (WINDOWS)? new String[] { WINUTILS, "groups", "-F", "\"" + user + "\""} - : new String [] {"bash", "-c", "id -gn " + user - + "&& id -Gn " + user}; + //'groups username' command return is inconsistent across different unixes + if (WINDOWS) { + return new String[] + {getWinUtilsPath(), "groups", "-F", "\"" + user + "\""}; + } else { + String quotedUser = bashQuote(user); + return new String[] {"bash", "-c", "id -gn " + quotedUser + + "; id -Gn " + quotedUser}; + } } /** a Unix command to get a given netgroup's user list */ public static String[] getUsersForNetgroupCommand(final String netgroup) { //'groups username' command return is non-consistent across different unixes - return (WINDOWS)? new String [] {"cmd", "/c", "getent netgroup " + netgroup} - : new String [] {"bash", "-c", "getent netgroup " + netgroup}; + return new String[] {"getent", "netgroup", netgroup}; } /** Return a command to get permission information. */ @@ -259,8 +278,9 @@ abstract public class Shell { */ public static String[] getRunScriptCommand(File script) { String absolutePath = script.getAbsolutePath(); - return WINDOWS ? new String[] { "cmd", "/c", absolutePath } : - new String[] { "/bin/bash", absolutePath }; + return WINDOWS ? + new String[] {"cmd", "/c", absolutePath } + : new String[] {"/bin/bash", bashQuote(absolutePath) }; } /** a Unix command to set permission */ @@ -737,7 +757,13 @@ abstract public class Shell { /** Execute the shell command. */ public void execute() throws IOException { - this.run(); + for (String s : command) { + if (s == null) { + throw new IOException("(null) entry in command string: " + + StringUtils.join(" ", command)); + } + } + this.run(); } @Override http://git-wip-us.apache.org/repos/asf/hadoop/blob/4bc2ece8/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestShell.java ---------------------------------------------------------------------- diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestShell.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestShell.java index accb469..9511c91 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestShell.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestShell.java @@ -209,4 +209,11 @@ public class TestShell extends TestCase { assertEquals(2, command.getRunCount()); } } + + @Test + public void testBashQuote() { + assertEquals("'foobar'", Shell.bashQuote("foobar")); + assertEquals("'foo'\\''bar'", Shell.bashQuote("foo'bar")); + assertEquals("''\\''foo'\\''bar'\\'''", Shell.bashQuote("'foo'bar'")); + } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
