15 package com.cloudera.impala.util;
17 import java.io.IOException;
18 import java.util.Arrays;
19 import java.util.HashSet;
22 import java.util.List;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 import org.apache.hadoop.fs.permission.AclEntry;
28 import org.apache.hadoop.fs.permission.AclEntryType;
29 import org.apache.hadoop.fs.permission.AclStatus;
30 import org.apache.hadoop.fs.permission.AclStatus;
31 import org.apache.hadoop.fs.permission.AclEntryScope;
32 import org.apache.hadoop.fs.FileStatus;
33 import org.apache.hadoop.fs.FileSystem;
34 import org.apache.hadoop.fs.Path;
35 import org.apache.hadoop.fs.permission.FsAction;
36 import org.apache.hadoop.fs.permission.FsPermission;
37 import org.apache.hadoop.security.UserGroupInformation;
38 import org.apache.hadoop.hdfs.protocol.AclException;
39 import org.apache.hadoop.ipc.RemoteException;
41 import com.google.common.base.Preconditions;
42 import com.google.common.collect.ImmutableList;
43 import com.google.common.collect.Maps;
44 import com.google.common.collect.Lists;
51 private final static Logger
LOG = LoggerFactory.getLogger(FsPermissionChecker.class);
54 private final Set<String>
groups_ =
new HashSet<String>();
59 }
catch (IOException e) {
60 throw new RuntimeException(
61 "Error initializing FsPermissionChecker: " + e.getMessage(), e);
66 UserGroupInformation ugi = UserGroupInformation.getCurrentUser();
67 groups_.addAll(Arrays.asList(ugi.getGroupNames()));
69 user_ = ugi.getShortUserName();
74 ImmutableList.of(AclEntryType.USER, AclEntryType.GROUP, AclEntryType.OTHER);
90 protected Permissions(FileStatus fileStatus, AclStatus aclStatus) {
91 Preconditions.checkNotNull(fileStatus);
95 if (aclStatus_ == null)
return;
101 entriesByTypes_.put(t, Lists.<AclEntry>newArrayList());
104 List<AclEntry> fullAclList =
106 for (AclEntry e: fullAclList) {
107 if (e.getType() == AclEntryType.MASK && e.getScope() != AclEntryScope.DEFAULT) {
110 entriesByTypes_.get(e.getType()).add(e);
120 if (
mask_ == null)
return false;
122 switch (acl.getType()) {
124 return acl.getName() != null;
137 if (e.getScope() == AclEntryScope.DEFAULT)
return false;
139 switch (e.getType()) {
141 String aclUser = e.getName() == null ?
aclStatus_.getOwner() : e.getName();
142 return FsPermissionChecker.this.user_.equals(aclUser);
144 String aclGroup = e.getName() == null ?
aclStatus_.getGroup() : e.getName();
145 return FsPermissionChecker.this.groups_.contains(aclGroup);
151 LOG.warn(
"Unknown Acl type: " + e.getType());
169 boolean foundMatch =
false;
172 if (t == AclEntryType.OTHER) {
176 return foundMatch ?
false : e.getPermission().implies(action);
180 if (e.getPermission().implies(action)) {
182 if (
mask_.getPermission().implies(action))
return true;
188 if (t == AclEntryType.USER)
return false;
202 if (aclPerms != null)
return aclPerms;
210 return permissions_.getUserAction().implies(action);
214 return permissions_.getGroupAction().implies(action);
216 return permissions_.getOtherAction().implies(action);
228 List<AclEntry> entries) {
230 List<AclEntry> aclEntries = Lists.newArrayListWithCapacity(entries.size() + 3);
233 aclEntries.add(
new AclEntry.Builder()
234 .setScope(AclEntryScope.ACCESS)
235 .setType(AclEntryType.USER)
236 .setPermission(perm.getUserAction())
241 boolean hasAccessAcl =
false;
242 for (AclEntry entry: entries) {
245 if (entry.getScope() == AclEntryScope.DEFAULT)
break;
247 aclEntries.add(entry);
252 aclEntries.add(
new AclEntry.Builder()
253 .setScope(AclEntryScope.ACCESS)
254 .setType(hasAccessAcl ? AclEntryType.MASK : AclEntryType.GROUP)
255 .setPermission(perm.getGroupAction())
259 aclEntries.add(
new AclEntry.Builder()
260 .setScope(AclEntryScope.ACCESS)
261 .setType(AclEntryType.OTHER)
262 .setPermission(perm.getOtherAction())
274 Preconditions.checkNotNull(fs);
275 Preconditions.checkNotNull(
path);
276 AclStatus aclStatus = null;
278 aclStatus = fs.getAclStatus(
path);
279 }
catch (AclException ex) {
280 LOG.trace(
"No ACLs retrieved, skipping ACLs check (HDFS will enforce ACLs)", ex);
281 }
catch (UnsupportedOperationException ex) {
282 LOG.trace(
"No ACLs retrieved, unsupported", ex);
string path("/usr/lib/sasl2:/usr/lib64/sasl2:/usr/local/lib/sasl2:/usr/lib/x86_64-linux-gnu/sasl2")
final AclStatus aclStatus_
static final FsPermissionChecker instance_
final Set< String > groups_
Permissions(FileStatus fileStatus, AclStatus aclStatus)
final FsPermission permissions_
boolean checkPermissions(FsAction action)
static List< AclEntryType > ACL_TYPE_PRIORITY
Permissions getPermissions(FileSystem fs, Path path)
boolean shouldApplyMask(AclEntry acl)
Boolean checkAcls(FsAction action)
List< AclEntry > getAclFromPermAndEntries(FsPermission perm, List< AclEntry > entries)
static FsPermissionChecker getInstance()
Map< AclEntryType, List< AclEntry > > entriesByTypes_
boolean canReadAndWrite()
boolean isApplicableAcl(AclEntry e)
final FileStatus fileStatus_