Skip to content

Commit

Permalink
replace method setAcl with modifyAclEntries while preserving acls
Browse files Browse the repository at this point in the history
  • Loading branch information
Meeth Gala committed Sep 1, 2023
1 parent 8f0938f commit 9486b5a
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.permission.AclEntry;
import org.apache.hadoop.fs.permission.AclEntryScope;
import org.apache.hadoop.fs.permission.AclEntryType;
import org.apache.hadoop.fs.permission.AclStatus;
import org.apache.hadoop.fs.permission.FsAction;
import org.apache.hadoop.fs.permission.FsPermission;

import com.google.common.base.Optional;
Expand Down Expand Up @@ -444,33 +441,7 @@ public static Map<String, OwnerAndPermission> resolveReplicatedAncestorOwnerAndP

private static List<AclEntry> getAclEntries(FileSystem srcFs, Path path) throws IOException {
AclStatus aclStatus = srcFs.getAclStatus(path);
return addOthersEntryTypeToAclEntriesIfMissing(aclStatus.getEntries());
}
/*
* When we get AclEntry from org.apache.hadoop.fs.permission.AclStatus, it's missing AclEntryType.OTHER.
* This causes AclTransformation validation to fail on the list of AclEntries. As a result, adding this helper
* method to provide DEFAULT scope in cases where AclEntryType.OTHER is absent
*/
private static List<AclEntry> addOthersEntryTypeToAclEntriesIfMissing(List<AclEntry> aclEntries) {
// Check if "others" entry is missing
boolean othersAclEntryTypeMissing = true;

for (AclEntry aclEntry : aclEntries) {
if (aclEntry.getType() == AclEntryType.OTHER) {
othersAclEntryTypeMissing = false;
break;
}
}
// If "others" entry is missing, add it
if (othersAclEntryTypeMissing) {
AclEntry othersEntry = new AclEntry.Builder().setType(AclEntryType.OTHER).setScope(AclEntryScope.DEFAULT)
.setPermission(FsAction.READ_EXECUTE).build();

// Modify the ACL entries to include the new "others" entry
aclEntries.add(othersEntry);
}
return aclEntries;

return aclStatus.getEntries();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,9 @@ public static void safeSetPathPermission(FileSystem fs, FileStatus file, OwnerAn
fs.setPermission(path, targetOwnerAndPermission.getFsPermission());
}
if (!targetOwnerAndPermission.getAclEntries().isEmpty()) {
fs.setAcl(path, targetOwnerAndPermission.getAclEntries());
// use modify acls instead of setAcl since latter requires all three acl entry types: user, group and others
// while overwriting the acls for a given path. If anyone is absent it fails acl transformation validation.
fs.modifyAclEntries(path, targetOwnerAndPermission.getAclEntries());
}
} catch (IOException ioe) {
log.warn("Failed to set permission for directory " + path, ioe);
Expand Down Expand Up @@ -519,7 +521,9 @@ private void ensureDirectoryExists(FileSystem fs, Path path, Iterator<OwnerAndPe
log.warn("Failed to set owner and/or group for path " + path + " to " + owner + ":" + group, ioe);
}
if (!aclEntries.isEmpty()) {
fs.setAcl(path, aclEntries);
// use modify acls instead of setAcl since latter requires all three acl entry types: user, group and others
// while overwriting the acls for a given path. If anyone is absent it fails acl transformation validation.
fs.modifyAclEntries(path, aclEntries);
}
} else {
fs.mkdirs(path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ public void cleanup() {
protected class TestLocalFileSystem extends LocalFileSystem {
private final ConcurrentHashMap<Path, List<AclEntry>> pathToAclEntries = new ConcurrentHashMap<>();
@Override
public void setAcl(Path path, List<AclEntry> aclEntries) {
public void modifyAclEntries(Path path, List<AclEntry> aclEntries) {
pathToAclEntries.put(path, aclEntries);
}
public ImmutableMap<Path, List<AclEntry>> getPathToAclEntries() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ public AclStatus getAclStatus(Path path) throws IOException {
public void setAcl(Path path, List<AclEntry> aclSpec) throws IOException {
this.underlyingFs.setAcl(path, aclSpec);
}
public void modifyAclEntries(Path path, List<AclEntry> aclSpec) throws IOException {
this.underlyingFs.modifyAclEntries(path, aclSpec);
}

public FsStatus getStatus() throws java.io.IOException {
return this.underlyingFs.getStatus();
Expand Down

0 comments on commit 9486b5a

Please sign in to comment.