ChenSammi commented on code in PR #9239:
URL: https://github.com/apache/ozone/pull/9239#discussion_r2532873550


##########
hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/security/acl/iam/IamSessionPolicyResolver.java:
##########
@@ -0,0 +1,329 @@
+/*
+ * 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.security.acl.iam;
+
+import static 
org.apache.hadoop.ozone.om.exceptions.OMException.ResultCodes.INVALID_REQUEST;
+import static 
org.apache.hadoop.ozone.om.exceptions.OMException.ResultCodes.NOT_SUPPORTED_OPERATION;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.LinkedHashSet;
+import java.util.Objects;
+import java.util.Set;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.hadoop.ozone.om.exceptions.OMException;
+import org.apache.hadoop.ozone.security.acl.AssumeRoleRequest;
+
+/**
+ * Resolves a limited subset of AWS IAM session policies into Ozone ACL grants,
+ * according to either the RangerOzoneAuthorizer or OzoneNativeAuthorizer 
constructs.
+ * <p>
+ * Here are some differences between the RangerOzoneAuthorizer and 
OzoneNativeAuthorizer:
+ *    - RangerOzoneAuthorizer doesn't currently use ResourceType.PREFIX, 
whereas OzoneNativeAuthorizer does.
+ *    - OzoneNativeAuthorizer doesn't allow wildcards in bucket names (ex. 
ResourceArn `arn:aws:s3:::*`,
+ *    `arn:aws:s3:::bucket*` or `*`), whereas RangerOzoneAuthorizer does.
+ *    - For OzoneNativeAuthorizer, certain object wildcards are accepted.   
For example, ResourceArn
+ *    `arn:aws:s3:::myBucket/*` and `arn:aws:s3:::myBucket/folder/logs/*` are 
accepted but not
+ *    `arn:aws:s3:::myBucket/file*.txt`.
+ * <p>
+ * The only supported ResourceArn has prefix arn:aws:s3::: - all others will 
throw
+ * OMException with NOT_SUPPORTED_OPERATION.
+ * <p>
+ * The only supported Condition operator is StringEquals - all others will 
throw
+ * OMException with NOT_SUPPORTED_OPERATION.  Furthermore, only one Condition 
is supported in a
+ * statement.
+ * <p>
+ * The only supported Condition attribute is s3:prefix - all others will throw
+ * OMException with NOT_SUPPORTED_OPERATION.
+ * <p>
+ * The only supported Effect is Allow - all others will throw OMException with 
NOT_SUPPORTED_OPERATION.
+ * <p>
+ * If a (currently) unsupported S3 action is requested, such as 
s3:GetAccelerateConfiguration,
+ * it will be silently ignored.
+ * <p>
+ * Supported wildcard expansions in Actions are: s3:*, s3:Get*, s3:Put*, 
s3:List*,
+ * s3:Create*, and s3:Delete*.
+ */
+public final class IamSessionPolicyResolver {
+
+  private static final ObjectMapper MAPPER = new ObjectMapper();
+
+  private static final int MAX_JSON_LENGTH = 2048;
+
+  private IamSessionPolicyResolver() {
+  }
+
+  /**
+   * Resolves an S3 IAM session policy in the form of a JSON String to a data 
structure comprising
+   * the IOzoneObjs and permissions that IAM policy grants (if any).
+   * <p>
+   * Each entry represents a path (such as /s3v/bucket1 or /s3v/bucket1/*) and 
a set of
+   * permissions (such as READ, LIST, CREATE).
+   * <p>
+   * The OzoneObj can be different depending on the AuthorizerType (see main 
Javadoc at top of file
+   * for examples).
+   * <p>
+   *
+   * @param policyJson     the IAM session policy
+   * @param volumeName     the volume under which the resource(s) live.  This 
may not be s3v in
+   *                       multi-tenant scenarios
+   * @param authorizerType whether the IOzoneObjs should be generated for use 
by the
+   *                       RangerOzoneAuthorizer or the OzoneNativeAuthorizer
+   * @return the data structure comprising the paths and permission pairings 
that
+   * the session policy grants (if any)
+   * @throws OMException if the policy JSON is invalid, malformed, or contains 
unsupported features
+   */
+  public static Set<AssumeRoleRequest.OzoneGrant> resolve(String policyJson, 
String volumeName,
+      AuthorizerType authorizerType) throws OMException {
+
+    validateInputParameters(policyJson, volumeName, authorizerType);
+
+    final Set<AssumeRoleRequest.OzoneGrant> result = new LinkedHashSet<>();
+
+    // Parse JSON into set of statements
+    final Set<JsonNode> statements = 
parseJsonAndRetrieveStatements(policyJson);
+
+    for (JsonNode stmt : statements) {
+      validateEffectInJsonStatement(stmt);

Review Comment:
   Do we support multiple actions like this example 
   
   ```
   {
     "Version":"2012-10-17",                             
     "Statement": [
       {
         "Sid": "FirstStatement",
         "Effect": "Allow",
         "Action": ["iam:ChangePassword"],
         "Resource": "*"
       },
       {
         "Sid": "SecondStatement",
         "Effect": "Allow",
         "Action": "s3:ListAllMyBuckets",
         "Resource": "*"
       },
       {
         "Sid": "ThirdStatement",
         "Effect": "Allow",
         "Action": [
           "s3:List*",
           "s3:Get*"
         ],
         "Resource": [
           "arn:aws:s3:::amzn-s3-demo-bucket-confidential-data",
           "arn:aws:s3:::amzn-s3-demo-bucket-confidential-data/*"
         ],
         "Condition": {"Bool": {"aws:MultiFactorAuthPresent": "true"}}
       }
     ]
   }
   ```
   quoted from 
https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies.html#policies_session
   



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to