From 20fb03a6c4d45fd49653cf6c69d3d6c55d527ccd Mon Sep 17 00:00:00 2001 From: Jing Zhao Date: Mon, 31 Mar 2014 20:55:14 +0000 Subject: [PATCH] HADOOP-10345. Merge change r1583454 from trunk. git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/branch-2@1583457 13f79535-47bb-0310-9956-ffa450edef68 --- .../hadoop-common/CHANGES.txt | 3 ++ .../hadoop/security/authorize/ProxyUsers.java | 4 +- .../org/apache/hadoop/util/StringUtils.java | 7 +++- .../security/authorize/TestProxyUsers.java | 37 +++++++++++++++++++ .../apache/hadoop/util/TestStringUtils.java | 11 ++++++ 5 files changed, 59 insertions(+), 3 deletions(-) diff --git a/hadoop-common-project/hadoop-common/CHANGES.txt b/hadoop-common-project/hadoop-common/CHANGES.txt index 6d013517dec..0c11d1e4501 100644 --- a/hadoop-common-project/hadoop-common/CHANGES.txt +++ b/hadoop-common-project/hadoop-common/CHANGES.txt @@ -11,6 +11,9 @@ Release 2.5.0 - UNRELEASED HADOOP-10451. Remove unused field and imports from SaslRpcServer. (Benoy Antony via jing9) + HADOOP-10345. Sanitize the the inputs (groups and hosts) for the proxyuser + configuration. (Benoy Antony via jing9) + OPTIMIZATIONS BUG FIXES diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/security/authorize/ProxyUsers.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/security/authorize/ProxyUsers.java index 52952588739..94721e96f37 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/security/authorize/ProxyUsers.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/security/authorize/ProxyUsers.java @@ -69,7 +69,7 @@ public class ProxyUsers { Map allMatchKeys = conf.getValByRegex(regex); for(Entry entry : allMatchKeys.entrySet()) { proxyGroups.put(entry.getKey(), - StringUtils.getStringCollection(entry.getValue())); + StringUtils.getTrimmedStringCollection(entry.getValue())); } // now hosts @@ -77,7 +77,7 @@ public class ProxyUsers { allMatchKeys = conf.getValByRegex(regex); for(Entry entry : allMatchKeys.entrySet()) { proxyHosts.put(entry.getKey(), - StringUtils.getStringCollection(entry.getValue())); + StringUtils.getTrimmedStringCollection(entry.getValue())); } init = true; diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/StringUtils.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/StringUtils.java index 9ffbe63457b..e7f983ac668 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/StringUtils.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/StringUtils.java @@ -28,9 +28,11 @@ import java.util.Arrays; import java.util.Collection; import java.util.Date; import java.util.Iterator; +import java.util.LinkedHashSet; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Set; import java.util.StringTokenizer; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -351,12 +353,15 @@ public class StringUtils { /** * Splits a comma separated value String, trimming leading and trailing whitespace on each value. + * Duplicate and empty values are removed. * @param str a comma separated with values * @return a Collection of String values */ public static Collection getTrimmedStringCollection(String str){ - return new ArrayList( + Set set = new LinkedHashSet( Arrays.asList(getTrimmedStrings(str))); + set.remove(""); + return set; } /** diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/security/authorize/TestProxyUsers.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/security/authorize/TestProxyUsers.java index 6f346a117c1..9bb2c3bf65c 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/security/authorize/TestProxyUsers.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/security/authorize/TestProxyUsers.java @@ -18,6 +18,8 @@ package org.apache.hadoop.security.authorize; import java.util.Arrays; +import java.util.Collection; + import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.util.StringUtils; import org.apache.hadoop.security.UserGroupInformation; @@ -133,6 +135,41 @@ public class TestProxyUsers { assertNotAuthorized(proxyUserUgi, "1.2.3.5"); } + @Test + public void testWithDuplicateProxyGroups() throws Exception { + Configuration conf = new Configuration(); + conf.set( + ProxyUsers.getProxySuperuserGroupConfKey(REAL_USER_NAME), + StringUtils.join(",", Arrays.asList(GROUP_NAMES,GROUP_NAMES))); + conf.set( + ProxyUsers.getProxySuperuserIpConfKey(REAL_USER_NAME), + PROXY_IP); + ProxyUsers.refreshSuperUserGroupsConfiguration(conf); + + Collection groupsToBeProxied = ProxyUsers.getProxyGroups().get( + ProxyUsers.getProxySuperuserGroupConfKey(REAL_USER_NAME)); + + assertEquals (1,groupsToBeProxied.size()); + } + + @Test + public void testWithDuplicateProxyHosts() throws Exception { + Configuration conf = new Configuration(); + conf.set( + ProxyUsers.getProxySuperuserGroupConfKey(REAL_USER_NAME), + StringUtils.join(",", Arrays.asList(GROUP_NAMES))); + conf.set( + ProxyUsers.getProxySuperuserIpConfKey(REAL_USER_NAME), + StringUtils.join(",", Arrays.asList(PROXY_IP,PROXY_IP))); + ProxyUsers.refreshSuperUserGroupsConfiguration(conf); + + Collection hosts = ProxyUsers.getProxyHosts().get( + ProxyUsers.getProxySuperuserIpConfKey(REAL_USER_NAME)); + + assertEquals (1,hosts.size()); + } + + private void assertNotAuthorized(UserGroupInformation proxyUgi, String host) { try { ProxyUsers.authorize(proxyUgi, host, null); diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestStringUtils.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestStringUtils.java index 4f06a31649e..0c930d43923 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestStringUtils.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestStringUtils.java @@ -22,9 +22,12 @@ import static org.apache.hadoop.util.StringUtils.TraditionalBinaryPrefix.long2St import static org.apache.hadoop.util.StringUtils.TraditionalBinaryPrefix.string2long; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -401,6 +404,14 @@ public class TestStringUtils extends UnitTestcaseTimeLimit { "begin %foo%_%bar%_%baz% end", pattern, replacements)); } + @Test + public void testGetUniqueNonEmptyTrimmedStrings (){ + final String TO_SPLIT = ",foo, bar,baz,,blah,blah,bar,"; + Collection col = StringUtils.getTrimmedStringCollection(TO_SPLIT); + assertEquals(4, col.size()); + assertTrue(col.containsAll(Arrays.asList(new String[]{"foo","bar","baz","blah"}))); + } + // Benchmark for StringUtils split public static void main(String []args) { final String TO_SPLIT = "foo,bar,baz,blah,blah";