xiaoyuyao commented on a change in pull request #1701:
URL: https://github.com/apache/ozone/pull/1701#discussion_r612649256
##########
File path:
hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/endpoint/BucketEndpoint.java
##########
@@ -353,6 +373,204 @@ 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();
+ // TODO: use bucket owner instead of volume owner here once bucket owner
+ // TODO: is supported.
+ S3Owner owner = new S3Owner(volume.getOwner(), volume.getOwner());
+ result.setOwner(owner);
+
+ // TODO: remove this duplication avoid logic when ACCESS and DEFAULT
scope
+ // TODO: are merged.
+ // 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()));
+ }
+ }
+
+ // A put request will reset all previous ACLs
+ bucket.setAcl(ozoneAclListOnBucket);
+ volume.setAcl(ozoneAclListOnVolume);
Review comment:
Should we read the existing volume acl, add the new ones necessary and
set it back instead of overwrite the existing ACL on the s3 volume?
##########
File path:
hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/endpoint/S3Acl.java
##########
@@ -0,0 +1,390 @@
+/*
+ * 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 GRANT_READ = "x-amz-grant-read";
+ public static final String GRANT_WRITE = "x-amz-grant-write";
+ public static final String GRANT_READ_CAP = "x-amz-grant-read-acp";
+ public static final String GRANT_WRITE_CAP = "x-amz-grant-write-acp";
+ public static final String GRANT_FULL_CONTROL = "x-amz-grant-full-control";
+
+ // Not supported headers at current stage, may support it in future
+ public static final String CANNED_ACL_HEADER = "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;
+ }
+ }
+
+ private S3Acl() {
+ }
+
+ 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);
+ } 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,
+ 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 getOzoneAclOnVolumeFromS3Permission(String permission)
+ throws OS3Exception {
+ BitSet acls = new BitSet(IAccessAuthorizer.ACLType.getNoOfAcls());
+ ACLType permissionType = ACLType.getType(permission);
+ if (permissionType == null) {
+ throw S3ErrorTable.newError(S3ErrorTable.INVALID_ARGUMENT, permission);
+ }
+ switch (permissionType) {
+ case FULL_CONTROL:
+ acls.set(IAccessAuthorizer.ACLType.ALL.ordinal());
+ break;
+ case WRITE_ACP:
+ acls.set(IAccessAuthorizer.ACLType.WRITE.ordinal());
+ break;
+ case READ_ACP:
+ acls.set(IAccessAuthorizer.ACLType.READ.ordinal());
+ break;
+ case WRITE:
+ acls.set(IAccessAuthorizer.ACLType.WRITE.ordinal());
+ break;
+ case READ:
Review comment:
In the design doc, the bucket read acl is mapped to read/list on both
volume and bucket. The code mapped to read only on the volume. Can you confirm
if the doc needs to be changed?
https://docs.google.com/document/d/18loKS6qbMKh8kKkpPvma9X-fNIcxBVAcHulAQxZpI1c/edit#
--
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]