zhoujinsong commented on code in PR #4118:
URL: https://github.com/apache/amoro/pull/4118#discussion_r2993236134


##########
amoro-ams/src/main/java/org/apache/amoro/server/authorization/CasbinAuthorizationManager.java:
##########
@@ -0,0 +1,148 @@
+/*
+ * 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.amoro.server.authorization;
+
+import org.apache.amoro.config.Configurations;
+import org.apache.amoro.server.AmoroManagementConf;
+import org.casbin.jcasbin.main.Enforcer;
+import org.casbin.jcasbin.model.Model;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.nio.charset.StandardCharsets;
+import java.util.Collections;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+public class CasbinAuthorizationManager {
+  private static final String MODEL_RESOURCE = 
"authorization/amoro_rbac_model.conf";
+  private static final String POLICY_RESOURCE = 
"authorization/amoro_rbac_policy.csv";
+
+  private final boolean authorizationEnabled;
+  private final Enforcer enforcer;
+  private final Map<String, Set<String>> rolePrivileges;
+
+  public CasbinAuthorizationManager(Configurations serviceConfig) {
+    this.authorizationEnabled = 
serviceConfig.get(AmoroManagementConf.AUTHORIZATION_ENABLED);
+    this.enforcer = authorizationEnabled ? createEnforcer() : null;
+    this.rolePrivileges =
+        authorizationEnabled ? loadRolePrivileges(POLICY_RESOURCE) : 
Collections.emptyMap();
+  }
+
+  public boolean isAuthorizationEnabled() {
+    return authorizationEnabled;
+  }
+
+  public boolean authorize(Set<String> roles, AuthorizationRequest request) {
+    if (!authorizationEnabled) {
+      return true;
+    }
+
+    if (roles == null || roles.isEmpty()) {
+      return false;
+    }
+
+    for (String role : roles) {
+      if (Boolean.TRUE.equals(
+          enforcer.enforce(
+              role,

Review Comment:
   **[Bug] `resolvePrivileges` and `authorize` use inconsistent privilege 
sources**
   
   `resolvePrivileges` reads from the pre-built `rolePrivileges` map (loaded 
from the CSV), while `authorize` uses the live Casbin enforcer. If a policy row 
carries `eft=deny`, `authorize` will correctly block the request, but 
`resolvePrivileges` will still include that privilege in the list returned to 
the frontend (because `loadRolePrivileges` only filters rows whose index-5 
token is literally `"deny"`).
   
   Consider deriving privileges from the enforcer directly, e.g. 
`enforcer.getPermissionsForUser(role)`, so both paths stay in sync.



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

Reply via email to