xiaoyuyao commented on a change in pull request #1701: URL: https://github.com/apache/ozone/pull/1701#discussion_r562380251
########## File path: hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/endpoint/S3Acl.java ########## @@ -0,0 +1,388 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.hadoop.ozone.s3.endpoint; + +import org.apache.hadoop.ozone.OzoneAcl; +import org.apache.hadoop.ozone.s3.endpoint.S3BucketAcl.Grant; +import org.apache.hadoop.ozone.s3.endpoint.S3BucketAcl.Grantee; +import org.apache.hadoop.ozone.s3.exception.OS3Exception; +import org.apache.hadoop.ozone.s3.exception.S3ErrorTable; +import org.apache.hadoop.ozone.security.acl.IAccessAuthorizer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.BitSet; +import java.util.List; + +import static org.apache.hadoop.ozone.s3.exception.S3ErrorTable.INVALID_ARGUMENT; +import static org.apache.hadoop.ozone.s3.exception.S3ErrorTable.NOT_IMPLEMENTED; + +public final class S3Acl { + private static final Logger LOG = LoggerFactory.getLogger(S3Acl.class); + + // ACL put related headers + public static final String grantRead = "x-amz-grant-read"; + public static final String grantWrite = "x-amz-grant-write"; + public static final String grantReadACP = "x-amz-grant-read-acp"; + public static final String grantWriteACP = "x-amz-grant-write-acp"; + public static final String grantFullControl = "x-amz-grant-full-control"; + + // Not supported headers at current stage, may support it in future + public static final String cannedAclHeader = "x-amz-acl"; + + /** + * https://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html. + */ + enum ACLType { + // Allows grantee to list the objects in the bucket + READ("READ"), + // Allows grantee to create, overwrite, and delete any object in the bucket + WRITE("WRITE"), + // Allows grantee to write the bucket ACL + READ_ACP("READ_ACP"), + // Allows grantee to write the ACL for the applicable bucket + WRITE_ACP("WRITE_ACP"), + // Allows grantee above all permissions on the bucket + FULL_CONTROL("FULL_CONTROL"); + + public String getValue() { + return value; + } + /** + * String value for this Enum. + */ + private final String value; + + /** + * @param val String type for this enum. + */ + ACLType(String val) { + value = val; + } + + + public static ACLType getType(String typeStr) { + for(ACLType type: ACLType.values()) { + if (type.getValue().equals(typeStr)) { + return type; + } + } + return null; + } + } + + enum ACLIdentityType { + USER("CanonicalUser", true, "id"), + GROUP("Group", false, "url"), + USER_BY_EMAIL("AmazonCustomerByEmail", false, "emailAddress"); + + public String getGranteeType() { + return granteeType; + } + + public String getHeaderType() { + return granteeInHeader; + } + + /** + * Grantee type in body XML. + */ + private final String granteeType; + + /** + * Is this type supported or not. + */ + private final boolean supported; + + /** + * Grantee type in header. + */ + private final String granteeInHeader; + + /** + * Init OzoneACLtypes enum. + * + * @param val String type for this enum. + */ + ACLIdentityType(String val, boolean support, String headerType) { + granteeType = val; + supported = support; + granteeInHeader = headerType; + } + + boolean isSupported() { + return supported; + } + + public static ACLIdentityType getTypeFromGranteeType(String typeStr) { + for(ACLIdentityType type: ACLIdentityType.values()) { + if (type.getGranteeType().equals(typeStr)) { + return type; + } + } + return null; + } + + public static ACLIdentityType getTypeFromHeaderType(String typeStr) { + for(ACLIdentityType type: ACLIdentityType.values()) { + if (type.getHeaderType().equals(typeStr)) { + return type; + } + } + return null; + } + } + + public static boolean isGranteeTypeSupported(String typeStr) { + ACLIdentityType type = ACLIdentityType.getTypeFromGranteeType(typeStr); + return type == null ? false : type.isSupported(); + } + + public static boolean isHeaderTypeSupported(String typeStr) { + ACLIdentityType type = ACLIdentityType.getTypeFromHeaderType(typeStr); + return type == null ? false : type.isSupported(); + } + + public static List<Grant> ozoneNativeAclToS3Acl(OzoneAcl ozoneAcl) { + // Since currently only "CanonicalUser" is supported, which maps to Ozone + // "USER" + List<Grant> grantList = new ArrayList<>(); + if (ozoneAcl.getType() != IAccessAuthorizer.ACLIdentityType.USER) { + return grantList; + } + + Grantee grantee = new Grantee(); + grantee.setDisplayName(ozoneAcl.getName()); + grantee.setId(ozoneAcl.getName()); + + List<IAccessAuthorizer.ACLType> acls = ozoneAcl.getAclList(); + if (acls.contains(IAccessAuthorizer.ACLType.ALL)) { + Grant grant = new Grant(); + grant.setGrantee(grantee); + grant.setPermission(ACLType.FULL_CONTROL.toString()); + grantList.add(grant); + return grantList; + } else if (acls.contains(IAccessAuthorizer.ACLType.WRITE_ACL)) { Review comment: should we allow other combinations here? such as READ+WRITE? I feel if we remove "else" here, the 1-1 mapping between Ozone AclType to the S3 ACLType can be added. ########## File path: hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/endpoint/S3Acl.java ########## @@ -0,0 +1,388 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.hadoop.ozone.s3.endpoint; + +import org.apache.hadoop.ozone.OzoneAcl; +import org.apache.hadoop.ozone.s3.endpoint.S3BucketAcl.Grant; +import org.apache.hadoop.ozone.s3.endpoint.S3BucketAcl.Grantee; +import org.apache.hadoop.ozone.s3.exception.OS3Exception; +import org.apache.hadoop.ozone.s3.exception.S3ErrorTable; +import org.apache.hadoop.ozone.security.acl.IAccessAuthorizer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.BitSet; +import java.util.List; + +import static org.apache.hadoop.ozone.s3.exception.S3ErrorTable.INVALID_ARGUMENT; +import static org.apache.hadoop.ozone.s3.exception.S3ErrorTable.NOT_IMPLEMENTED; + +public final class S3Acl { + private static final Logger LOG = LoggerFactory.getLogger(S3Acl.class); + + // ACL put related headers + public static final String grantRead = "x-amz-grant-read"; + public static final String grantWrite = "x-amz-grant-write"; + public static final String grantReadACP = "x-amz-grant-read-acp"; + public static final String grantWriteACP = "x-amz-grant-write-acp"; + public static final String grantFullControl = "x-amz-grant-full-control"; + + // Not supported headers at current stage, may support it in future + public static final String cannedAclHeader = "x-amz-acl"; + + /** + * https://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html. + */ + enum ACLType { + // Allows grantee to list the objects in the bucket + READ("READ"), + // Allows grantee to create, overwrite, and delete any object in the bucket + WRITE("WRITE"), + // Allows grantee to write the bucket ACL + READ_ACP("READ_ACP"), + // Allows grantee to write the ACL for the applicable bucket + WRITE_ACP("WRITE_ACP"), + // Allows grantee above all permissions on the bucket + FULL_CONTROL("FULL_CONTROL"); + + public String getValue() { + return value; + } + /** + * String value for this Enum. + */ + private final String value; + + /** + * @param val String type for this enum. + */ + ACLType(String val) { + value = val; + } + + + public static ACLType getType(String typeStr) { + for(ACLType type: ACLType.values()) { + if (type.getValue().equals(typeStr)) { + return type; + } + } + return null; + } + } + + enum ACLIdentityType { + USER("CanonicalUser", true, "id"), + GROUP("Group", false, "url"), + USER_BY_EMAIL("AmazonCustomerByEmail", false, "emailAddress"); + + public String getGranteeType() { + return granteeType; + } + + public String getHeaderType() { + return granteeInHeader; + } + + /** + * Grantee type in body XML. + */ + private final String granteeType; + + /** + * Is this type supported or not. + */ + private final boolean supported; + + /** + * Grantee type in header. + */ + private final String granteeInHeader; + + /** + * Init OzoneACLtypes enum. + * + * @param val String type for this enum. + */ + ACLIdentityType(String val, boolean support, String headerType) { + granteeType = val; + supported = support; + granteeInHeader = headerType; + } + + boolean isSupported() { + return supported; + } + + public static ACLIdentityType getTypeFromGranteeType(String typeStr) { + for(ACLIdentityType type: ACLIdentityType.values()) { + if (type.getGranteeType().equals(typeStr)) { + return type; + } + } + return null; + } + + public static ACLIdentityType getTypeFromHeaderType(String typeStr) { + for(ACLIdentityType type: ACLIdentityType.values()) { + if (type.getHeaderType().equals(typeStr)) { + return type; + } + } + return null; + } + } + + public static boolean isGranteeTypeSupported(String typeStr) { + ACLIdentityType type = ACLIdentityType.getTypeFromGranteeType(typeStr); + return type == null ? false : type.isSupported(); + } + + public static boolean isHeaderTypeSupported(String typeStr) { + ACLIdentityType type = ACLIdentityType.getTypeFromHeaderType(typeStr); + return type == null ? false : type.isSupported(); + } + + public static List<Grant> ozoneNativeAclToS3Acl(OzoneAcl ozoneAcl) { + // Since currently only "CanonicalUser" is supported, which maps to Ozone + // "USER" + List<Grant> grantList = new ArrayList<>(); + if (ozoneAcl.getType() != IAccessAuthorizer.ACLIdentityType.USER) { + return grantList; + } + + Grantee grantee = new Grantee(); + grantee.setDisplayName(ozoneAcl.getName()); + grantee.setId(ozoneAcl.getName()); + + List<IAccessAuthorizer.ACLType> acls = ozoneAcl.getAclList(); + if (acls.contains(IAccessAuthorizer.ACLType.ALL)) { + Grant grant = new Grant(); + grant.setGrantee(grantee); + grant.setPermission(ACLType.FULL_CONTROL.toString()); + grantList.add(grant); + return grantList; + } else if (acls.contains(IAccessAuthorizer.ACLType.WRITE_ACL)) { + Grant grant = new Grant(); + grant.setGrantee(grantee); + grant.setPermission(ACLType.WRITE_ACP.toString()); + grantList.add(grant); + } else if (acls.contains(IAccessAuthorizer.ACLType.READ_ACL)) { + Grant grant = new Grant(); + grant.setGrantee(grantee); + grant.setPermission(ACLType.READ_ACP.toString()); + grantList.add(grant); + } else if (acls.contains(IAccessAuthorizer.ACLType.WRITE) && + acls.contains(IAccessAuthorizer.ACLType.DELETE) && + acls.contains(IAccessAuthorizer.ACLType.CREATE)) { + Grant grant = new Grant(); + grant.setGrantee(grantee); + grant.setPermission(ACLType.WRITE.toString()); + grantList.add(grant); + } else if (acls.contains(IAccessAuthorizer.ACLType.READ) && + acls.contains(IAccessAuthorizer.ACLType.LIST)) { + Grant grant = new Grant(); + grant.setGrantee(grantee); + grant.setPermission(ACLType.READ.toString()); + grantList.add(grant); + } else { + LOG.error("Cannot find a good mapping for Ozone ACL {} to S3", + ozoneAcl.toString()); + } + return grantList; + } + + public static List<OzoneAcl> s3AclToOzoneNativeAclOnBucket( + S3BucketAcl bucketAcl) throws OS3Exception { + List<OzoneAcl> ozoneAclList = new ArrayList<>(); + List<Grant> grantList = bucketAcl.getAclList().getGrantList(); + for (Grant grant : grantList) { + // Only "CanonicalUser" is supported, which maps to Ozone "USER" + ACLIdentityType identityType = ACLIdentityType.getTypeFromGranteeType( + grant.getGrantee().getXsiType()); + if (identityType != null && identityType.isSupported()) { + String permission = grant.getPermission(); + BitSet acls = getOzoneAclOnBucketFromS3Permission(permission); + OzoneAcl defaultOzoneAcl = new OzoneAcl( + IAccessAuthorizer.ACLIdentityType.USER, + grant.getGrantee().getId(), acls, + OzoneAcl.AclScope.DEFAULT); + OzoneAcl accessOzoneAcl = new OzoneAcl( + IAccessAuthorizer.ACLIdentityType.USER, + grant.getGrantee().getId(), acls, + OzoneAcl.AclScope.ACCESS); + ozoneAclList.add(defaultOzoneAcl); + ozoneAclList.add(accessOzoneAcl); + } else { + LOG.error("Grantee type {} is not supported", + grant.getGrantee().getXsiType()); + throw S3ErrorTable.newError(NOT_IMPLEMENTED, + grant.getGrantee().getXsiType()); + } + } + return ozoneAclList; + } + + public static BitSet getOzoneAclOnBucketFromS3Permission(String permission) + throws OS3Exception { + ACLType permissionType = ACLType.getType(permission); + if (permissionType == null) { + throw S3ErrorTable.newError(S3ErrorTable.INVALID_ARGUMENT, permission); + } + BitSet acls = new BitSet(IAccessAuthorizer.ACLType.getNoOfAcls()); + switch (permissionType) { + case FULL_CONTROL: + acls.set(IAccessAuthorizer.ACLType.ALL.ordinal()); + break; + case WRITE_ACP: + acls.set(IAccessAuthorizer.ACLType.WRITE_ACL.ordinal()); + break; + case READ_ACP: + acls.set(IAccessAuthorizer.ACLType.READ_ACL.ordinal()); + break; + case WRITE: + acls.set(IAccessAuthorizer.ACLType.WRITE.ordinal()); + acls.set(IAccessAuthorizer.ACLType.DELETE.ordinal()); + acls.set(IAccessAuthorizer.ACLType.CREATE.ordinal()); + break; + case READ: + acls.set(IAccessAuthorizer.ACLType.READ.ordinal()); + acls.set(IAccessAuthorizer.ACLType.LIST.ordinal()); + break; + default: + LOG.error("Failed to recognize S3 permission {}", permission); + throw S3ErrorTable.newError(INVALID_ARGUMENT, permission); + } + return acls; + } + + public static List<OzoneAcl> s3AclToOzoneNativeAclOnVolume( + S3BucketAcl bucketAcl) throws OS3Exception { + List<OzoneAcl> ozoneAclList = new ArrayList<>(); + List<Grant> grantList = bucketAcl.getAclList().getGrantList(); + for (Grant grant : grantList) { + // Only "CanonicalUser" is supported, which maps to Ozone "USER" + ACLIdentityType identityType = ACLIdentityType.getTypeFromGranteeType( + grant.getGrantee().getXsiType()); + if (identityType != null && identityType.isSupported()) { + String permission = grant.getPermission(); + BitSet acls = getOzoneAclOnVolumeFromS3Permission(permission); + OzoneAcl defaultOzoneAcl = new OzoneAcl( + IAccessAuthorizer.ACLIdentityType.USER, Review comment: s3 acl may have multiple grants for the same user. When it is converted to OzoneAcl, we should combine to take the advantage of BitSet. ########## File path: hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/endpoint/BucketEndpoint.java ########## @@ -353,6 +375,215 @@ public MultiDeleteResponse multiDelete(@PathParam("bucket") String bucketName, return result; } + /** + * Implement acl get. + * <p> + * see: https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetBucketAcl.html + */ + public S3BucketAcl getAcl(String bucketName) + throws OS3Exception, IOException { + S3BucketAcl result = new S3BucketAcl(); + try { + OzoneBucket bucket = getBucket(bucketName); + OzoneVolume volume = getVolume(); + S3Owner owner = new S3Owner(volume.getOwner(), volume.getOwner()); + result.setOwner(owner); + // Use set to remove ACLs with different scopes(ACCESS and DEFAULT) + Set<Grant> grantSet = new HashSet<>(); + // Return ACL list + for (OzoneAcl acl : bucket.getAcls()) { + List<Grant> grants = S3Acl.ozoneNativeAclToS3Acl(acl); + grantSet.addAll(grants); + } + ArrayList<Grant> grantList = new ArrayList<>(); + grantList.addAll(grantSet); + result.setAclList( + new S3BucketAcl.AccessControlList(grantList)); + return result; + } catch (OMException ex) { + if (ex.getResult() == ResultCodes.BUCKET_NOT_FOUND) { + throw S3ErrorTable.newError(S3ErrorTable + .NO_SUCH_BUCKET, bucketName); + } else if (ex.getResult() == ResultCodes.PERMISSION_DENIED) { + throw S3ErrorTable.newError(S3ErrorTable + .ACCESS_DENIED, bucketName); + } else { + LOG.error("Failed to get acl of Bucket " + bucketName, ex); + throw S3ErrorTable.newError(S3ErrorTable.INTERNAL_ERROR, bucketName); + } + } + } + + /** + * Implement acl put. + * <p> + * see: https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutBucketAcl.html + */ + public Response putAcl(String bucketName, HttpHeaders httpHeaders, + InputStream body) throws IOException, OS3Exception { + String grantReads = httpHeaders.getHeaderString(S3Acl.grantRead); + String grantWrites = httpHeaders.getHeaderString(S3Acl.grantWrite); + String grantReadACP = httpHeaders.getHeaderString(S3Acl.grantReadACP); + String grantWriteACP = httpHeaders.getHeaderString(S3Acl.grantWriteACP); + String grantFull = httpHeaders.getHeaderString(S3Acl.grantFullControl); + + try { + OzoneBucket bucket = getBucket(bucketName); + OzoneVolume volume = getVolume(); + + List<OzoneAcl> ozoneAclListOnBucket = new ArrayList<>(); + List<OzoneAcl> ozoneAclListOnVolume = new ArrayList<>(); + + if (grantReads == null && grantWrites == null && grantReadACP == null + && grantWriteACP == null && grantFull == null) { + S3BucketAcl putBucketAclRequest = + new PutBucketAclRequestUnmarshaller().readFrom( + null, null, null, null, null, body); + // Handle grants in body + ozoneAclListOnBucket.addAll( + S3Acl.s3AclToOzoneNativeAclOnBucket(putBucketAclRequest)); + ozoneAclListOnVolume.addAll( + S3Acl.s3AclToOzoneNativeAclOnVolume(putBucketAclRequest)); + } else { + + // Handle grants in headers + if (grantReads != null) { + ozoneAclListOnBucket.addAll(getAndConvertAclOnBucket(grantReads, + S3Acl.ACLType.READ.getValue())); + ozoneAclListOnVolume.addAll(getAndConvertAclOnVolume(grantReads, + S3Acl.ACLType.READ.getValue())); + } + if (grantWrites != null) { + ozoneAclListOnBucket.addAll(getAndConvertAclOnBucket(grantWrites, + S3Acl.ACLType.WRITE.getValue())); + ozoneAclListOnVolume.addAll(getAndConvertAclOnVolume(grantWrites, + S3Acl.ACLType.WRITE.getValue())); + } + if (grantReadACP != null) { + ozoneAclListOnBucket.addAll(getAndConvertAclOnBucket(grantReadACP, + S3Acl.ACLType.READ_ACP.getValue())); + ozoneAclListOnVolume.addAll(getAndConvertAclOnVolume(grantReadACP, + S3Acl.ACLType.READ_ACP.getValue())); + } + if (grantWriteACP != null) { + ozoneAclListOnBucket.addAll(getAndConvertAclOnBucket(grantWriteACP, + S3Acl.ACLType.WRITE_ACP.getValue())); + ozoneAclListOnVolume.addAll(getAndConvertAclOnVolume(grantWriteACP, + S3Acl.ACLType.WRITE_ACP.getValue())); + } + if (grantFull != null) { + ozoneAclListOnBucket.addAll(getAndConvertAclOnBucket(grantFull, + S3Acl.ACLType.FULL_CONTROL.getValue())); + ozoneAclListOnVolume.addAll(getAndConvertAclOnVolume(grantFull, + S3Acl.ACLType.FULL_CONTROL.getValue())); + } + } + + List<OzoneAcl> oldBucketAcls = bucket.getAcls(); + List<OzoneAcl> oldVolumeAcls = + S3Acl.getVolumeAclFromBucketAcl(oldBucketAcls); + + // Add new ACLs + for (OzoneAcl addAcl : ozoneAclListOnBucket) { + bucket.addAcl(addAcl); Review comment: why don't we call volume/bucket setAcl() directly instead of add new/remove old? ########## File path: hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/endpoint/BucketEndpoint.java ########## @@ -353,6 +375,215 @@ public MultiDeleteResponse multiDelete(@PathParam("bucket") String bucketName, return result; } + /** + * Implement acl get. + * <p> + * see: https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetBucketAcl.html + */ + public S3BucketAcl getAcl(String bucketName) + throws OS3Exception, IOException { + S3BucketAcl result = new S3BucketAcl(); + try { + OzoneBucket bucket = getBucket(bucketName); + OzoneVolume volume = getVolume(); + S3Owner owner = new S3Owner(volume.getOwner(), volume.getOwner()); + result.setOwner(owner); + // Use set to remove ACLs with different scopes(ACCESS and DEFAULT) + Set<Grant> grantSet = new HashSet<>(); + // Return ACL list + for (OzoneAcl acl : bucket.getAcls()) { + List<Grant> grants = S3Acl.ozoneNativeAclToS3Acl(acl); + grantSet.addAll(grants); + } + ArrayList<Grant> grantList = new ArrayList<>(); + grantList.addAll(grantSet); + result.setAclList( + new S3BucketAcl.AccessControlList(grantList)); + return result; + } catch (OMException ex) { + if (ex.getResult() == ResultCodes.BUCKET_NOT_FOUND) { + throw S3ErrorTable.newError(S3ErrorTable + .NO_SUCH_BUCKET, bucketName); + } else if (ex.getResult() == ResultCodes.PERMISSION_DENIED) { + throw S3ErrorTable.newError(S3ErrorTable + .ACCESS_DENIED, bucketName); + } else { + LOG.error("Failed to get acl of Bucket " + bucketName, ex); + throw S3ErrorTable.newError(S3ErrorTable.INTERNAL_ERROR, bucketName); + } + } + } + + /** + * Implement acl put. + * <p> + * see: https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutBucketAcl.html + */ + public Response putAcl(String bucketName, HttpHeaders httpHeaders, + InputStream body) throws IOException, OS3Exception { + String grantReads = httpHeaders.getHeaderString(S3Acl.grantRead); + String grantWrites = httpHeaders.getHeaderString(S3Acl.grantWrite); + String grantReadACP = httpHeaders.getHeaderString(S3Acl.grantReadACP); + String grantWriteACP = httpHeaders.getHeaderString(S3Acl.grantWriteACP); + String grantFull = httpHeaders.getHeaderString(S3Acl.grantFullControl); + + try { + OzoneBucket bucket = getBucket(bucketName); + OzoneVolume volume = getVolume(); + + List<OzoneAcl> ozoneAclListOnBucket = new ArrayList<>(); + List<OzoneAcl> ozoneAclListOnVolume = new ArrayList<>(); + + if (grantReads == null && grantWrites == null && grantReadACP == null + && grantWriteACP == null && grantFull == null) { + S3BucketAcl putBucketAclRequest = + new PutBucketAclRequestUnmarshaller().readFrom( + null, null, null, null, null, body); + // Handle grants in body + ozoneAclListOnBucket.addAll( + S3Acl.s3AclToOzoneNativeAclOnBucket(putBucketAclRequest)); + ozoneAclListOnVolume.addAll( + S3Acl.s3AclToOzoneNativeAclOnVolume(putBucketAclRequest)); + } else { + + // Handle grants in headers + if (grantReads != null) { + ozoneAclListOnBucket.addAll(getAndConvertAclOnBucket(grantReads, + S3Acl.ACLType.READ.getValue())); + ozoneAclListOnVolume.addAll(getAndConvertAclOnVolume(grantReads, + S3Acl.ACLType.READ.getValue())); + } + if (grantWrites != null) { + ozoneAclListOnBucket.addAll(getAndConvertAclOnBucket(grantWrites, + S3Acl.ACLType.WRITE.getValue())); + ozoneAclListOnVolume.addAll(getAndConvertAclOnVolume(grantWrites, + S3Acl.ACLType.WRITE.getValue())); + } + if (grantReadACP != null) { + ozoneAclListOnBucket.addAll(getAndConvertAclOnBucket(grantReadACP, + S3Acl.ACLType.READ_ACP.getValue())); + ozoneAclListOnVolume.addAll(getAndConvertAclOnVolume(grantReadACP, + S3Acl.ACLType.READ_ACP.getValue())); + } + if (grantWriteACP != null) { + ozoneAclListOnBucket.addAll(getAndConvertAclOnBucket(grantWriteACP, + S3Acl.ACLType.WRITE_ACP.getValue())); + ozoneAclListOnVolume.addAll(getAndConvertAclOnVolume(grantWriteACP, + S3Acl.ACLType.WRITE_ACP.getValue())); + } + if (grantFull != null) { + ozoneAclListOnBucket.addAll(getAndConvertAclOnBucket(grantFull, + S3Acl.ACLType.FULL_CONTROL.getValue())); + ozoneAclListOnVolume.addAll(getAndConvertAclOnVolume(grantFull, + S3Acl.ACLType.FULL_CONTROL.getValue())); + } + } + + List<OzoneAcl> oldBucketAcls = bucket.getAcls(); + List<OzoneAcl> oldVolumeAcls = + S3Acl.getVolumeAclFromBucketAcl(oldBucketAcls); + + // Add new ACLs + for (OzoneAcl addAcl : ozoneAclListOnBucket) { + bucket.addAcl(addAcl); + } + for (OzoneAcl addAcl : ozoneAclListOnVolume) { + volume.addAcl(addAcl); + } + + // A put request will reset all previous ACLs + for (OzoneAcl removeAcl : oldBucketAcls) { + bucket.removeAcl(removeAcl); + } + for (OzoneAcl removeAcl : oldVolumeAcls) { + volume.removeAcl(removeAcl); + } + } catch (OMException exception) { + LOG.error("Error in set ACL Request for bucket: {}", bucketName, + exception); + if (exception.getResult() == ResultCodes.BUCKET_NOT_FOUND) { + throw S3ErrorTable.newError(S3ErrorTable.NO_SUCH_BUCKET, + bucketName); + } else if (exception.getResult() == ResultCodes.PERMISSION_DENIED) { + throw S3ErrorTable.newError(S3ErrorTable + .ACCESS_DENIED, bucketName); + } + throw exception; + } + return Response.status(HttpStatus.SC_OK).build(); + } + + /** + * Example: x-amz-grant-write: \ + * uri="http://acs.amazonaws.com/groups/s3/LogDelivery", id="111122223333", \ + * id="555566667777". + */ + private List<OzoneAcl> getAndConvertAclOnBucket(String value, + String permission) throws OS3Exception { + List<OzoneAcl> ozoneAclList = new ArrayList<>(); + if (StringUtils.isEmpty(value)) { + return ozoneAclList; + } + String[] subValues = value.split(","); + for (String acl: subValues) { + String[] part = acl.split("="); + if (part.length != 2) { + throw S3ErrorTable.newError(S3ErrorTable.INVALID_ARGUMENT, acl); + } + S3Acl.ACLIdentityType type = + S3Acl.ACLIdentityType.getTypeFromHeaderType(part[0]); + if (type == null || !type.isSupported()) { + LOG.warn("S3 grantee {} is null or not supported", part[0]); + throw S3ErrorTable.newError(NOT_IMPLEMENTED, part[0]); + } + // Build ACL on Bucket + BitSet aclsOnBucket = + S3Acl.getOzoneAclOnBucketFromS3Permission(permission); + OzoneAcl defaultOzoneAcl = new OzoneAcl( + IAccessAuthorizer.ACLIdentityType.USER, part[1], aclsOnBucket, + OzoneAcl.AclScope.DEFAULT); + OzoneAcl accessOzoneAcl = new OzoneAcl( + IAccessAuthorizer.ACLIdentityType.USER, part[1], aclsOnBucket, + OzoneAcl.AclScope.ACCESS); + ozoneAclList.add(defaultOzoneAcl); + ozoneAclList.add(accessOzoneAcl); + } + return ozoneAclList; + } + + private List<OzoneAcl> getAndConvertAclOnVolume(String value, + String permission) throws OS3Exception { + List<OzoneAcl> ozoneAclList = new ArrayList<>(); + if (StringUtils.isEmpty(value)) { + return ozoneAclList; + } + String[] subValues = value.split(","); + for (String acl: subValues) { + String[] part = acl.split("="); + if (part.length != 2) { + throw S3ErrorTable.newError(S3ErrorTable.INVALID_ARGUMENT, acl); + } + S3Acl.ACLIdentityType type = + S3Acl.ACLIdentityType.getTypeFromHeaderType(part[0]); + if (type == null || !type.isSupported()) { + LOG.warn("S3 grantee {} is null or not supported", part[0]); + throw S3ErrorTable.newError(NOT_IMPLEMENTED, part[0]); + } + // Build ACL on Bucket Review comment: NIT: you mean volume? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
