Revert "HADOOP-15836. Review of AccessControlList. Contributed by BELUGA BEHR."
This reverts commit 00254d7b8c
.
This commit is contained in:
parent
296818a2c8
commit
dd268a64d3
@ -20,13 +20,11 @@
|
||||
import java.io.DataInput;
|
||||
import java.io.DataOutput;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.hadoop.classification.InterfaceAudience;
|
||||
import org.apache.hadoop.classification.InterfaceStability;
|
||||
import org.apache.hadoop.conf.Configuration;
|
||||
@ -36,6 +34,7 @@
|
||||
import org.apache.hadoop.io.WritableFactory;
|
||||
import org.apache.hadoop.security.Groups;
|
||||
import org.apache.hadoop.security.UserGroupInformation;
|
||||
import org.apache.hadoop.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Class representing a configured access control list.
|
||||
@ -58,9 +57,9 @@ public class AccessControlList implements Writable {
|
||||
private static final int INITIAL_CAPACITY = 256;
|
||||
|
||||
// Set of users who are granted access.
|
||||
private final Set<String> users = new TreeSet<>();
|
||||
private Collection<String> users;
|
||||
// Set of groups which are granted access
|
||||
private final Set<String> groups = new TreeSet<>();
|
||||
private Collection<String> groups;
|
||||
// Whether all users are granted access.
|
||||
private boolean allAllowed;
|
||||
|
||||
@ -86,7 +85,7 @@ public AccessControlList(String aclString) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a new ACL from String representation of users and groups.
|
||||
* Construct a new ACL from String representation of users and groups
|
||||
*
|
||||
* The arguments are comma separated lists
|
||||
*
|
||||
@ -104,39 +103,38 @@ public AccessControlList(String users, String groups) {
|
||||
* @param aclString build ACL from array of Strings
|
||||
*/
|
||||
private void buildACL(String[] userGroupStrings) {
|
||||
users = new HashSet<String>();
|
||||
groups = new HashSet<String>();
|
||||
for (String aclPart : userGroupStrings) {
|
||||
if (aclPart != null && isWildCardACLValue(aclPart)) {
|
||||
allAllowed = true;
|
||||
return;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!allAllowed) {
|
||||
if (userGroupStrings.length >= 1 && userGroupStrings[0] != null) {
|
||||
String[] userList = userGroupStrings[0].split(",");
|
||||
for (String user : userList) {
|
||||
if (StringUtils.isNotBlank(user)) {
|
||||
users.add(user.trim());
|
||||
}
|
||||
}
|
||||
users = StringUtils.getTrimmedStringCollection(userGroupStrings[0]);
|
||||
}
|
||||
|
||||
if (userGroupStrings.length == 2 && userGroupStrings[1] != null) {
|
||||
String[] groupList = userGroupStrings[1].split(",");
|
||||
for (String group : groupList) {
|
||||
if (StringUtils.isNotBlank(group)) {
|
||||
groups.add(group.trim());
|
||||
groups = StringUtils.getTrimmedStringCollection(userGroupStrings[1]);
|
||||
groupsMapping.cacheGroupsAdd(new LinkedList<String>(groups));
|
||||
}
|
||||
}
|
||||
groupsMapping.cacheGroupsAdd(new ArrayList<>(groups));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether ACL string contains wildcard.
|
||||
* Checks whether ACL string contains wildcard
|
||||
*
|
||||
* @param aclString check this ACL string for wildcard
|
||||
* @return true if ACL string contains wildcard false otherwise
|
||||
*/
|
||||
private boolean isWildCardACLValue(String aclString) {
|
||||
return WILDCARD_ACL_VALUE.equals(aclString.trim());
|
||||
if (aclString.contains(WILDCARD_ACL_VALUE) &&
|
||||
aclString.trim().equals(WILDCARD_ACL_VALUE)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isAllAllowed() {
|
||||
@ -146,7 +144,8 @@ public boolean isAllAllowed() {
|
||||
/**
|
||||
* Add user to the names of users allowed for this service.
|
||||
*
|
||||
* @param user The user name
|
||||
* @param user
|
||||
* The user name
|
||||
*/
|
||||
public void addUser(String user) {
|
||||
if (isWildCardACLValue(user)) {
|
||||
@ -160,15 +159,17 @@ public void addUser(String user) {
|
||||
/**
|
||||
* Add group to the names of groups allowed for this service.
|
||||
*
|
||||
* @param group The group name
|
||||
* @param group
|
||||
* The group name
|
||||
*/
|
||||
public void addGroup(String group) {
|
||||
if (isWildCardACLValue(group)) {
|
||||
throw new IllegalArgumentException(
|
||||
"Group " + group + " can not be added");
|
||||
throw new IllegalArgumentException("Group " + group + " can not be added");
|
||||
}
|
||||
if (!isAllAllowed()) {
|
||||
groupsMapping.cacheGroupsAdd(Collections.singletonList(group));
|
||||
List<String> groupsList = new LinkedList<String>();
|
||||
groupsList.add(group);
|
||||
groupsMapping.cacheGroupsAdd(groupsList);
|
||||
groups.add(group);
|
||||
}
|
||||
}
|
||||
@ -176,7 +177,8 @@ public void addGroup(String group) {
|
||||
/**
|
||||
* Remove user from the names of users allowed for this service.
|
||||
*
|
||||
* @param user The user name
|
||||
* @param user
|
||||
* The user name
|
||||
*/
|
||||
public void removeUser(String user) {
|
||||
if (isWildCardACLValue(user)) {
|
||||
@ -190,12 +192,13 @@ public void removeUser(String user) {
|
||||
/**
|
||||
* Remove group from the names of groups allowed for this service.
|
||||
*
|
||||
* @param group The group name
|
||||
* @param group
|
||||
* The group name
|
||||
*/
|
||||
public void removeGroup(String group) {
|
||||
if (isWildCardACLValue(group)) {
|
||||
throw new IllegalArgumentException(
|
||||
"Group " + group + " can not be removed");
|
||||
throw new IllegalArgumentException("Group " + group
|
||||
+ " can not be removed");
|
||||
}
|
||||
if (!isAllAllowed()) {
|
||||
groups.remove(group);
|
||||
@ -204,20 +207,18 @@ public void removeGroup(String group) {
|
||||
|
||||
/**
|
||||
* Get the names of users allowed for this service.
|
||||
*
|
||||
* @return an unmodifiable set of user names in alphabetic order.
|
||||
* @return the set of user names. the set must not be modified.
|
||||
*/
|
||||
public Collection<String> getUsers() {
|
||||
return Collections.unmodifiableSet(users);
|
||||
return users;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the names of user groups allowed for this service.
|
||||
*
|
||||
* @return an unmodifiable set of group names in alphabetic order.
|
||||
* @return the set of group names. the set must not be modified.
|
||||
*/
|
||||
public Collection<String> getGroups() {
|
||||
return Collections.unmodifiableSet(groups);
|
||||
return groups;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -229,8 +230,7 @@ public Collection<String> getGroups() {
|
||||
public final boolean isUserInList(UserGroupInformation ugi) {
|
||||
if (allAllowed || users.contains(ugi.getShortUserName())) {
|
||||
return true;
|
||||
}
|
||||
if (!groups.isEmpty()) {
|
||||
} else if (!groups.isEmpty()) {
|
||||
for (String group : ugi.getGroups()) {
|
||||
if (groups.contains(group)) {
|
||||
return true;
|
||||
@ -326,7 +326,7 @@ public void readFields(DataInput in) throws IOException {
|
||||
* @return comma separated list of users
|
||||
*/
|
||||
private String getUsersString() {
|
||||
return String.join(",", users);
|
||||
return getString(users);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -335,6 +335,26 @@ private String getUsersString() {
|
||||
* @return comma separated list of groups
|
||||
*/
|
||||
private String getGroupsString() {
|
||||
return String.join(",", groups);
|
||||
return getString(groups);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns comma-separated concatenated single String of all strings of
|
||||
* the given set
|
||||
*
|
||||
* @param strings set of strings to concatenate
|
||||
*/
|
||||
private String getString(Collection<String> strings) {
|
||||
StringBuilder sb = new StringBuilder(INITIAL_CAPACITY);
|
||||
boolean first = true;
|
||||
for(String str: strings) {
|
||||
if (!first) {
|
||||
sb.append(",");
|
||||
} else {
|
||||
first = false;
|
||||
}
|
||||
sb.append(str);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
@ -21,11 +21,9 @@
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.hadoop.classification.InterfaceAudience;
|
||||
@ -39,7 +37,9 @@
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.google.common.collect.Iterables;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
@InterfaceAudience.LimitedPrivate({"HDFS", "MapReduce"})
|
||||
@InterfaceStability.Evolving
|
||||
@ -202,14 +202,14 @@ public void testAclString() {
|
||||
validateGetAclString(acl);
|
||||
|
||||
acl = new AccessControlList(" group1,group2");
|
||||
assertEquals("Members of the groups [group1, group2] are allowed",
|
||||
acl.toString());
|
||||
assertTrue(acl.toString().equals(
|
||||
"Members of the groups [group1, group2] are allowed"));
|
||||
validateGetAclString(acl);
|
||||
|
||||
acl = new AccessControlList("user1,user2 group1,group2");
|
||||
assertEquals("Users [user1, user2] and members of the groups "
|
||||
+ "[group1, group2] are allowed", acl.toString());
|
||||
|
||||
assertTrue(acl.toString().equals(
|
||||
"Users [user1, user2] and " +
|
||||
"members of the groups [group1, group2] are allowed"));
|
||||
validateGetAclString(acl);
|
||||
}
|
||||
|
||||
@ -228,42 +228,45 @@ public void testAccessControlList() throws Exception {
|
||||
|
||||
acl = new AccessControlList("drwho tardis");
|
||||
users = acl.getUsers();
|
||||
assertEquals(1, users.size());
|
||||
assertEquals("drwho", Iterables.getOnlyElement(users));
|
||||
assertEquals(users.size(), 1);
|
||||
assertEquals(users.iterator().next(), "drwho");
|
||||
groups = acl.getGroups();
|
||||
assertEquals(1, groups.size());
|
||||
assertEquals("tardis", Iterables.getOnlyElement(groups));
|
||||
assertEquals(groups.size(), 1);
|
||||
assertEquals(groups.iterator().next(), "tardis");
|
||||
|
||||
acl = new AccessControlList("drwho");
|
||||
users = acl.getUsers();
|
||||
assertEquals(1, users.size());
|
||||
assertEquals("drwho", Iterables.getOnlyElement(users));
|
||||
assertEquals(users.size(), 1);
|
||||
assertEquals(users.iterator().next(), "drwho");
|
||||
groups = acl.getGroups();
|
||||
assertEquals(0, groups.size());
|
||||
assertEquals(groups.size(), 0);
|
||||
|
||||
acl = new AccessControlList("drwho ");
|
||||
users = acl.getUsers();
|
||||
assertEquals(1, users.size());
|
||||
assertEquals("drwho", Iterables.getOnlyElement(users));
|
||||
assertEquals(users.size(), 1);
|
||||
assertEquals(users.iterator().next(), "drwho");
|
||||
groups = acl.getGroups();
|
||||
assertEquals(0, groups.size());
|
||||
assertEquals(groups.size(), 0);
|
||||
|
||||
acl = new AccessControlList(" tardis");
|
||||
users = acl.getUsers();
|
||||
assertEquals(0, users.size());
|
||||
assertEquals(users.size(), 0);
|
||||
groups = acl.getGroups();
|
||||
assertEquals(1, groups.size());
|
||||
assertEquals("tardis", Iterables.getOnlyElement(groups));
|
||||
assertEquals(groups.size(), 1);
|
||||
assertEquals(groups.iterator().next(), "tardis");
|
||||
|
||||
Iterator<String> iter;
|
||||
acl = new AccessControlList("drwho,joe tardis, users");
|
||||
users = acl.getUsers();
|
||||
assertEquals(2, users.size());
|
||||
assertTrue(users.contains("drwho"));
|
||||
assertTrue(users.contains("joe"));
|
||||
assertEquals(users.size(), 2);
|
||||
iter = users.iterator();
|
||||
assertEquals(iter.next(), "drwho");
|
||||
assertEquals(iter.next(), "joe");
|
||||
groups = acl.getGroups();
|
||||
assertEquals(2, groups.size());
|
||||
assertTrue(groups.contains("tardis"));
|
||||
assertTrue(groups.contains("users"));
|
||||
assertEquals(groups.size(), 2);
|
||||
iter = groups.iterator();
|
||||
assertEquals(iter.next(), "tardis");
|
||||
assertEquals(iter.next(), "users");
|
||||
}
|
||||
|
||||
/**
|
||||
@ -281,45 +284,49 @@ public void testAddRemoveAPI() {
|
||||
|
||||
acl.addUser("drwho");
|
||||
users = acl.getUsers();
|
||||
assertEquals(1, users.size());
|
||||
assertEquals("drwho", Iterables.getOnlyElement(users));
|
||||
assertEquals(users.size(), 1);
|
||||
assertEquals(users.iterator().next(), "drwho");
|
||||
assertEquals("drwho ", acl.getAclString());
|
||||
|
||||
acl.addGroup("tardis");
|
||||
groups = acl.getGroups();
|
||||
assertEquals(1, groups.size());
|
||||
assertEquals("tardis", Iterables.getOnlyElement(groups));
|
||||
assertEquals(groups.size(), 1);
|
||||
assertEquals(groups.iterator().next(), "tardis");
|
||||
assertEquals("drwho tardis", acl.getAclString());
|
||||
|
||||
acl.addUser("joe");
|
||||
acl.addGroup("users");
|
||||
users = acl.getUsers();
|
||||
assertEquals(2, users.size());
|
||||
assertTrue(users.contains("drwho"));
|
||||
assertTrue(users.contains("joe"));
|
||||
|
||||
assertEquals(users.size(), 2);
|
||||
Iterator<String> iter = users.iterator();
|
||||
assertEquals(iter.next(), "drwho");
|
||||
assertEquals(iter.next(), "joe");
|
||||
groups = acl.getGroups();
|
||||
assertEquals(2, groups.size());
|
||||
assertTrue(groups.contains("tardis"));
|
||||
assertTrue(groups.contains("users"));
|
||||
assertEquals(groups.size(), 2);
|
||||
iter = groups.iterator();
|
||||
assertEquals(iter.next(), "tardis");
|
||||
assertEquals(iter.next(), "users");
|
||||
assertEquals("drwho,joe tardis,users", acl.getAclString());
|
||||
|
||||
acl.removeUser("joe");
|
||||
acl.removeGroup("users");
|
||||
users = acl.getUsers();
|
||||
assertEquals(1, users.size());
|
||||
assertEquals("drwho", Iterables.getOnlyElement(users));
|
||||
assertEquals(users.size(), 1);
|
||||
assertFalse(users.contains("joe"));
|
||||
groups = acl.getGroups();
|
||||
assertEquals(1, groups.size());
|
||||
assertEquals("tardis", Iterables.getOnlyElement(groups));
|
||||
assertEquals(groups.size(), 1);
|
||||
assertFalse(groups.contains("users"));
|
||||
assertEquals("drwho tardis", acl.getAclString());
|
||||
|
||||
acl.removeGroup("tardis");
|
||||
groups = acl.getGroups();
|
||||
assertEquals(0, groups.size());
|
||||
assertFalse(groups.contains("tardis"));
|
||||
assertEquals("drwho ", acl.getAclString());
|
||||
|
||||
acl.removeUser("drwho");
|
||||
assertEquals(0, users.size());
|
||||
assertFalse(users.contains("drwho"));
|
||||
assertEquals(0, acl.getGroups().size());
|
||||
assertEquals(0, acl.getUsers().size());
|
||||
assertEquals(" ", acl.getAclString());
|
||||
|
Loading…
Reference in New Issue
Block a user