morningman commented on code in PR #17153:
URL: https://github.com/apache/doris/pull/17153#discussion_r1118306757


##########
fe/fe-core/src/main/java/org/apache/doris/catalog/authorizer/RangerHiveAccessControllerFactory.java:
##########
@@ -0,0 +1,46 @@
+// 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.doris.catalog.authorizer;
+
+import org.apache.doris.mysql.privilege.AccessControllerFactory;
+import org.apache.doris.mysql.privilege.CatalogAccessController;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class RangerHiveAccessControllerFactory implements 
AccessControllerFactory {
+
+    private static volatile Map<String, RangerHiveAccessController> 
controllerMap = new HashMap<>();

Review Comment:
   There is a AccessControllerManager to save all AccessControllers, why do we 
need this `controllerMap` here?



##########
fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/CatalogAccessController.java:
##########
@@ -25,39 +25,38 @@
 public interface CatalogAccessController {
     // ==== Catalog ====
     default boolean checkCtlPriv(boolean hasGlobal, UserIdentity currentUser, 
String ctl, PrivPredicate wanted) {
-        boolean res = checkCtlPriv(currentUser, ctl, wanted);
-        return hasGlobal || res;
+        return hasGlobal || checkCtlPriv(currentUser, ctl, wanted);

Review Comment:
   I wrote the origin code on purpose, because we need to make sure that 
`checkCtlPriv/checkDbPriv/....` are called everytime, or the plugin can not 
audit these requests.
   
   If using `hasGlobal || checkCtlPriv(currentUser, ctl, wanted);`, and when 
`hasGlobal` is true, the `checkCtlPriv()` will not be called



##########
fe/fe-core/src/main/java/org/apache/doris/catalog/authorizer/RangerHiveAccessController.java:
##########
@@ -0,0 +1,188 @@
+// 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.doris.catalog.authorizer;
+
+import org.apache.doris.analysis.UserIdentity;
+import org.apache.doris.common.AuthorizationException;
+import org.apache.doris.mysql.privilege.CatalogAccessController;
+import org.apache.doris.mysql.privilege.PrivPredicate;
+
+import 
org.apache.hadoop.hive.ql.security.authorization.plugin.HiveAccessControlException;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.apache.ranger.plugin.policyengine.RangerAccessRequest;
+import org.apache.ranger.plugin.policyengine.RangerAccessRequestImpl;
+import org.apache.ranger.plugin.policyengine.RangerAccessResult;
+import org.apache.ranger.plugin.policyengine.RangerPolicyEngine;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+public class RangerHiveAccessController implements CatalogAccessController {
+    public static final String CLIENT_TYPE_DORIS = "doris";
+    private static final Logger LOG = 
LogManager.getLogger(RangerHiveAccessController.class);
+    private static volatile RangerHivePlugin hivePlugin = null;
+
+    public RangerHiveAccessController(Map<String, String> properties) {
+        String serviceName = properties.get("ranger.service.name");
+        hivePlugin = new RangerHivePlugin(serviceName);
+    }
+
+    private RangerAccessRequestImpl createRequest(UserIdentity currentUser, 
HiveAccessType accessType) {
+        RangerAccessRequestImpl request = new RangerAccessRequestImpl();
+        request.setUser(currentUser.getQualifiedUser());
+        request.setUserRoles(currentUser.getRoles());
+        request.setAction(accessType.name());
+        if (accessType == HiveAccessType.USE) {
+            request.setAccessType(RangerPolicyEngine.ANY_ACCESS);
+        } else {
+            request.setAccessType(accessType.name().toLowerCase());
+        }
+        request.setClientIPAddress(currentUser.getHost());
+        request.setClientType(CLIENT_TYPE_DORIS);
+        request.setAccessTime(new Date());
+
+        return request;
+    }
+
+    public void checkPrivileges(UserIdentity currentUser, HiveAccessType 
accessType,
+                                List<RangerHiveResource> hiveResources) throws 
AuthorizationException {
+        List<RangerAccessRequest> requests = new ArrayList<>();
+        for (RangerHiveResource resource : hiveResources) {
+            RangerAccessRequestImpl request = createRequest(currentUser, 
accessType);
+            request.setResource(resource);
+
+            requests.add(request);
+        }
+
+        RangerHiveAuditHandler auditHandler = new 
RangerHiveAuditHandler(hivePlugin.getConfig());

Review Comment:
   It is strange to create `RangerHiveAuditHandler` for each 
`checkPrivileges()` call.



##########
fe/fe-core/src/main/java/org/apache/doris/catalog/authorizer/RangerHiveAccessController.java:
##########
@@ -0,0 +1,188 @@
+// 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.doris.catalog.authorizer;
+
+import org.apache.doris.analysis.UserIdentity;
+import org.apache.doris.common.AuthorizationException;
+import org.apache.doris.mysql.privilege.CatalogAccessController;
+import org.apache.doris.mysql.privilege.PrivPredicate;
+
+import 
org.apache.hadoop.hive.ql.security.authorization.plugin.HiveAccessControlException;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.apache.ranger.plugin.policyengine.RangerAccessRequest;
+import org.apache.ranger.plugin.policyengine.RangerAccessRequestImpl;
+import org.apache.ranger.plugin.policyengine.RangerAccessResult;
+import org.apache.ranger.plugin.policyengine.RangerPolicyEngine;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+public class RangerHiveAccessController implements CatalogAccessController {
+    public static final String CLIENT_TYPE_DORIS = "doris";
+    private static final Logger LOG = 
LogManager.getLogger(RangerHiveAccessController.class);
+    private static volatile RangerHivePlugin hivePlugin = null;
+
+    public RangerHiveAccessController(Map<String, String> properties) {
+        String serviceName = properties.get("ranger.service.name");
+        hivePlugin = new RangerHivePlugin(serviceName);
+    }
+
+    private RangerAccessRequestImpl createRequest(UserIdentity currentUser, 
HiveAccessType accessType) {
+        RangerAccessRequestImpl request = new RangerAccessRequestImpl();
+        request.setUser(currentUser.getQualifiedUser());
+        request.setUserRoles(currentUser.getRoles());
+        request.setAction(accessType.name());
+        if (accessType == HiveAccessType.USE) {
+            request.setAccessType(RangerPolicyEngine.ANY_ACCESS);
+        } else {
+            request.setAccessType(accessType.name().toLowerCase());
+        }
+        request.setClientIPAddress(currentUser.getHost());
+        request.setClientType(CLIENT_TYPE_DORIS);
+        request.setAccessTime(new Date());
+
+        return request;
+    }
+
+    public void checkPrivileges(UserIdentity currentUser, HiveAccessType 
accessType,
+                                List<RangerHiveResource> hiveResources) throws 
AuthorizationException {
+        List<RangerAccessRequest> requests = new ArrayList<>();
+        for (RangerHiveResource resource : hiveResources) {
+            RangerAccessRequestImpl request = createRequest(currentUser, 
accessType);
+            request.setResource(resource);
+
+            requests.add(request);
+        }
+
+        RangerHiveAuditHandler auditHandler = new 
RangerHiveAuditHandler(hivePlugin.getConfig());
+        Collection<RangerAccessResult> results = 
hivePlugin.isAccessAllowed(requests, auditHandler);
+        for (RangerAccessResult result : results) {
+            LOG.debug("match policy:" + result.getPolicyId());
+            LOG.debug("will audit: " + result.toString());
+            auditHandler.processResult(result);

Review Comment:
   I saw in `isAccessAllowed()`, it already call the 
`auditHandler.processResult()`, why do we need to call it again here?



##########
fe/fe-core/src/main/java/org/apache/doris/catalog/authorizer/RangerHiveAccessController.java:
##########
@@ -0,0 +1,188 @@
+// 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.doris.catalog.authorizer;
+
+import org.apache.doris.analysis.UserIdentity;
+import org.apache.doris.common.AuthorizationException;
+import org.apache.doris.mysql.privilege.CatalogAccessController;
+import org.apache.doris.mysql.privilege.PrivPredicate;
+
+import 
org.apache.hadoop.hive.ql.security.authorization.plugin.HiveAccessControlException;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.apache.ranger.plugin.policyengine.RangerAccessRequest;
+import org.apache.ranger.plugin.policyengine.RangerAccessRequestImpl;
+import org.apache.ranger.plugin.policyengine.RangerAccessResult;
+import org.apache.ranger.plugin.policyengine.RangerPolicyEngine;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+public class RangerHiveAccessController implements CatalogAccessController {
+    public static final String CLIENT_TYPE_DORIS = "doris";
+    private static final Logger LOG = 
LogManager.getLogger(RangerHiveAccessController.class);
+    private static volatile RangerHivePlugin hivePlugin = null;
+
+    public RangerHiveAccessController(Map<String, String> properties) {
+        String serviceName = properties.get("ranger.service.name");
+        hivePlugin = new RangerHivePlugin(serviceName);
+    }
+
+    private RangerAccessRequestImpl createRequest(UserIdentity currentUser, 
HiveAccessType accessType) {
+        RangerAccessRequestImpl request = new RangerAccessRequestImpl();
+        request.setUser(currentUser.getQualifiedUser());
+        request.setUserRoles(currentUser.getRoles());
+        request.setAction(accessType.name());
+        if (accessType == HiveAccessType.USE) {
+            request.setAccessType(RangerPolicyEngine.ANY_ACCESS);
+        } else {
+            request.setAccessType(accessType.name().toLowerCase());
+        }
+        request.setClientIPAddress(currentUser.getHost());
+        request.setClientType(CLIENT_TYPE_DORIS);
+        request.setAccessTime(new Date());
+
+        return request;
+    }
+
+    public void checkPrivileges(UserIdentity currentUser, HiveAccessType 
accessType,
+                                List<RangerHiveResource> hiveResources) throws 
AuthorizationException {
+        List<RangerAccessRequest> requests = new ArrayList<>();
+        for (RangerHiveResource resource : hiveResources) {
+            RangerAccessRequestImpl request = createRequest(currentUser, 
accessType);
+            request.setResource(resource);
+
+            requests.add(request);
+        }
+
+        RangerHiveAuditHandler auditHandler = new 
RangerHiveAuditHandler(hivePlugin.getConfig());
+        Collection<RangerAccessResult> results = 
hivePlugin.isAccessAllowed(requests, auditHandler);
+        for (RangerAccessResult result : results) {
+            LOG.debug("match policy:" + result.getPolicyId());
+            LOG.debug("will audit: " + result.toString());
+            auditHandler.processResult(result);
+            if (!result.getIsAllowed()) {
+                LOG.warn(result.getReason());
+                throw new AuthorizationException(String.format(
+                    "Permission denied: user [%s] does not have privilege for 
[%s] command on [%s]",
+                    currentUser.getQualifiedUser(), accessType.name(),
+                    result.getAccessRequest().getResource().getAsString()));
+            }
+        }
+    }
+
+    public boolean checkPrivilege(UserIdentity currentUser, HiveAccessType 
accessType,

Review Comment:
   ```suggestion
       private boolean checkPrivilege(UserIdentity currentUser, HiveAccessType 
accessType,
   ```



##########
fe/fe-core/src/main/java/org/apache/doris/catalog/authorizer/RangerHiveAccessController.java:
##########
@@ -0,0 +1,188 @@
+// 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.doris.catalog.authorizer;
+
+import org.apache.doris.analysis.UserIdentity;
+import org.apache.doris.common.AuthorizationException;
+import org.apache.doris.mysql.privilege.CatalogAccessController;
+import org.apache.doris.mysql.privilege.PrivPredicate;
+
+import 
org.apache.hadoop.hive.ql.security.authorization.plugin.HiveAccessControlException;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.apache.ranger.plugin.policyengine.RangerAccessRequest;
+import org.apache.ranger.plugin.policyengine.RangerAccessRequestImpl;
+import org.apache.ranger.plugin.policyengine.RangerAccessResult;
+import org.apache.ranger.plugin.policyengine.RangerPolicyEngine;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+public class RangerHiveAccessController implements CatalogAccessController {
+    public static final String CLIENT_TYPE_DORIS = "doris";
+    private static final Logger LOG = 
LogManager.getLogger(RangerHiveAccessController.class);
+    private static volatile RangerHivePlugin hivePlugin = null;
+
+    public RangerHiveAccessController(Map<String, String> properties) {
+        String serviceName = properties.get("ranger.service.name");
+        hivePlugin = new RangerHivePlugin(serviceName);
+    }
+
+    private RangerAccessRequestImpl createRequest(UserIdentity currentUser, 
HiveAccessType accessType) {
+        RangerAccessRequestImpl request = new RangerAccessRequestImpl();
+        request.setUser(currentUser.getQualifiedUser());
+        request.setUserRoles(currentUser.getRoles());
+        request.setAction(accessType.name());
+        if (accessType == HiveAccessType.USE) {
+            request.setAccessType(RangerPolicyEngine.ANY_ACCESS);
+        } else {
+            request.setAccessType(accessType.name().toLowerCase());
+        }
+        request.setClientIPAddress(currentUser.getHost());
+        request.setClientType(CLIENT_TYPE_DORIS);
+        request.setAccessTime(new Date());
+
+        return request;
+    }
+
+    public void checkPrivileges(UserIdentity currentUser, HiveAccessType 
accessType,
+                                List<RangerHiveResource> hiveResources) throws 
AuthorizationException {
+        List<RangerAccessRequest> requests = new ArrayList<>();
+        for (RangerHiveResource resource : hiveResources) {
+            RangerAccessRequestImpl request = createRequest(currentUser, 
accessType);
+            request.setResource(resource);
+
+            requests.add(request);
+        }
+
+        RangerHiveAuditHandler auditHandler = new 
RangerHiveAuditHandler(hivePlugin.getConfig());
+        Collection<RangerAccessResult> results = 
hivePlugin.isAccessAllowed(requests, auditHandler);
+        for (RangerAccessResult result : results) {
+            LOG.debug("match policy:" + result.getPolicyId());
+            LOG.debug("will audit: " + result.toString());
+            auditHandler.processResult(result);
+            if (!result.getIsAllowed()) {
+                LOG.warn(result.getReason());
+                throw new AuthorizationException(String.format(
+                    "Permission denied: user [%s] does not have privilege for 
[%s] command on [%s]",
+                    currentUser.getQualifiedUser(), accessType.name(),
+                    result.getAccessRequest().getResource().getAsString()));
+            }
+        }
+    }
+
+    public boolean checkPrivilege(UserIdentity currentUser, HiveAccessType 
accessType,
+                                  RangerHiveResource resource) {
+        RangerHiveAuditHandler auditHandler = new 
RangerHiveAuditHandler(hivePlugin.getConfig());
+        RangerAccessRequestImpl request = createRequest(currentUser, 
accessType);
+        request.setResource(resource);
+
+        RangerAccessResult result = hivePlugin.isAccessAllowed(request, 
auditHandler);
+        auditHandler.processResult(result);
+        auditHandler.flushAudit();
+
+        if (result == null) {
+            LOG.warn("Error getting authorizer result, please check your 
ranger config");
+            return false;
+        }
+
+        if (result.getIsAllowed()) {
+            LOG.debug("pass");

Review Comment:
   remove these temporary debug log



##########
fe/fe-core/src/main/java/org/apache/doris/catalog/authorizer/RangerHiveAccessController.java:
##########
@@ -0,0 +1,188 @@
+// 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.doris.catalog.authorizer;
+
+import org.apache.doris.analysis.UserIdentity;
+import org.apache.doris.common.AuthorizationException;
+import org.apache.doris.mysql.privilege.CatalogAccessController;
+import org.apache.doris.mysql.privilege.PrivPredicate;
+
+import 
org.apache.hadoop.hive.ql.security.authorization.plugin.HiveAccessControlException;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.apache.ranger.plugin.policyengine.RangerAccessRequest;
+import org.apache.ranger.plugin.policyengine.RangerAccessRequestImpl;
+import org.apache.ranger.plugin.policyengine.RangerAccessResult;
+import org.apache.ranger.plugin.policyengine.RangerPolicyEngine;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+public class RangerHiveAccessController implements CatalogAccessController {
+    public static final String CLIENT_TYPE_DORIS = "doris";
+    private static final Logger LOG = 
LogManager.getLogger(RangerHiveAccessController.class);
+    private static volatile RangerHivePlugin hivePlugin = null;
+
+    public RangerHiveAccessController(Map<String, String> properties) {
+        String serviceName = properties.get("ranger.service.name");
+        hivePlugin = new RangerHivePlugin(serviceName);
+    }
+
+    private RangerAccessRequestImpl createRequest(UserIdentity currentUser, 
HiveAccessType accessType) {
+        RangerAccessRequestImpl request = new RangerAccessRequestImpl();
+        request.setUser(currentUser.getQualifiedUser());
+        request.setUserRoles(currentUser.getRoles());
+        request.setAction(accessType.name());
+        if (accessType == HiveAccessType.USE) {
+            request.setAccessType(RangerPolicyEngine.ANY_ACCESS);
+        } else {
+            request.setAccessType(accessType.name().toLowerCase());
+        }
+        request.setClientIPAddress(currentUser.getHost());
+        request.setClientType(CLIENT_TYPE_DORIS);
+        request.setAccessTime(new Date());
+
+        return request;
+    }
+
+    public void checkPrivileges(UserIdentity currentUser, HiveAccessType 
accessType,
+                                List<RangerHiveResource> hiveResources) throws 
AuthorizationException {
+        List<RangerAccessRequest> requests = new ArrayList<>();
+        for (RangerHiveResource resource : hiveResources) {
+            RangerAccessRequestImpl request = createRequest(currentUser, 
accessType);
+            request.setResource(resource);
+
+            requests.add(request);
+        }
+
+        RangerHiveAuditHandler auditHandler = new 
RangerHiveAuditHandler(hivePlugin.getConfig());
+        Collection<RangerAccessResult> results = 
hivePlugin.isAccessAllowed(requests, auditHandler);
+        for (RangerAccessResult result : results) {
+            LOG.debug("match policy:" + result.getPolicyId());
+            LOG.debug("will audit: " + result.toString());
+            auditHandler.processResult(result);
+            if (!result.getIsAllowed()) {
+                LOG.warn(result.getReason());
+                throw new AuthorizationException(String.format(
+                    "Permission denied: user [%s] does not have privilege for 
[%s] command on [%s]",
+                    currentUser.getQualifiedUser(), accessType.name(),
+                    result.getAccessRequest().getResource().getAsString()));
+            }
+        }
+    }
+
+    public boolean checkPrivilege(UserIdentity currentUser, HiveAccessType 
accessType,
+                                  RangerHiveResource resource) {
+        RangerHiveAuditHandler auditHandler = new 
RangerHiveAuditHandler(hivePlugin.getConfig());
+        RangerAccessRequestImpl request = createRequest(currentUser, 
accessType);
+        request.setResource(resource);
+
+        RangerAccessResult result = hivePlugin.isAccessAllowed(request, 
auditHandler);
+        auditHandler.processResult(result);
+        auditHandler.flushAudit();

Review Comment:
   What is the flush policy? eg, where and when should we call this 
`flushAudit()` mannually?



##########
fe/fe-core/src/main/java/org/apache/doris/catalog/authorizer/RangerHiveAccessController.java:
##########
@@ -0,0 +1,188 @@
+// 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.doris.catalog.authorizer;
+
+import org.apache.doris.analysis.UserIdentity;
+import org.apache.doris.common.AuthorizationException;
+import org.apache.doris.mysql.privilege.CatalogAccessController;
+import org.apache.doris.mysql.privilege.PrivPredicate;
+
+import 
org.apache.hadoop.hive.ql.security.authorization.plugin.HiveAccessControlException;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.apache.ranger.plugin.policyengine.RangerAccessRequest;
+import org.apache.ranger.plugin.policyengine.RangerAccessRequestImpl;
+import org.apache.ranger.plugin.policyengine.RangerAccessResult;
+import org.apache.ranger.plugin.policyengine.RangerPolicyEngine;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+public class RangerHiveAccessController implements CatalogAccessController {
+    public static final String CLIENT_TYPE_DORIS = "doris";
+    private static final Logger LOG = 
LogManager.getLogger(RangerHiveAccessController.class);
+    private static volatile RangerHivePlugin hivePlugin = null;
+
+    public RangerHiveAccessController(Map<String, String> properties) {
+        String serviceName = properties.get("ranger.service.name");
+        hivePlugin = new RangerHivePlugin(serviceName);
+    }
+
+    private RangerAccessRequestImpl createRequest(UserIdentity currentUser, 
HiveAccessType accessType) {
+        RangerAccessRequestImpl request = new RangerAccessRequestImpl();
+        request.setUser(currentUser.getQualifiedUser());
+        request.setUserRoles(currentUser.getRoles());
+        request.setAction(accessType.name());
+        if (accessType == HiveAccessType.USE) {
+            request.setAccessType(RangerPolicyEngine.ANY_ACCESS);
+        } else {
+            request.setAccessType(accessType.name().toLowerCase());
+        }
+        request.setClientIPAddress(currentUser.getHost());
+        request.setClientType(CLIENT_TYPE_DORIS);
+        request.setAccessTime(new Date());
+
+        return request;
+    }
+
+    public void checkPrivileges(UserIdentity currentUser, HiveAccessType 
accessType,
+                                List<RangerHiveResource> hiveResources) throws 
AuthorizationException {
+        List<RangerAccessRequest> requests = new ArrayList<>();
+        for (RangerHiveResource resource : hiveResources) {
+            RangerAccessRequestImpl request = createRequest(currentUser, 
accessType);
+            request.setResource(resource);
+
+            requests.add(request);
+        }
+
+        RangerHiveAuditHandler auditHandler = new 
RangerHiveAuditHandler(hivePlugin.getConfig());
+        Collection<RangerAccessResult> results = 
hivePlugin.isAccessAllowed(requests, auditHandler);
+        for (RangerAccessResult result : results) {
+            LOG.debug("match policy:" + result.getPolicyId());
+            LOG.debug("will audit: " + result.toString());
+            auditHandler.processResult(result);
+            if (!result.getIsAllowed()) {
+                LOG.warn(result.getReason());
+                throw new AuthorizationException(String.format(
+                    "Permission denied: user [%s] does not have privilege for 
[%s] command on [%s]",
+                    currentUser.getQualifiedUser(), accessType.name(),
+                    result.getAccessRequest().getResource().getAsString()));
+            }
+        }
+    }
+
+    public boolean checkPrivilege(UserIdentity currentUser, HiveAccessType 
accessType,
+                                  RangerHiveResource resource) {
+        RangerHiveAuditHandler auditHandler = new 
RangerHiveAuditHandler(hivePlugin.getConfig());
+        RangerAccessRequestImpl request = createRequest(currentUser, 
accessType);
+        request.setResource(resource);
+
+        RangerAccessResult result = hivePlugin.isAccessAllowed(request, 
auditHandler);
+        auditHandler.processResult(result);
+        auditHandler.flushAudit();
+
+        if (result == null) {
+            LOG.warn("Error getting authorizer result, please check your 
ranger config");
+            return false;
+        }
+
+        if (result.getIsAllowed()) {
+            LOG.debug("pass");
+            return true;
+        } else {
+            LOG.warn(String.format(
+                    "Permission denied: user [%s] does not have privilege for 
[%s] command on [%s]",
+                    currentUser.getQualifiedUser(), accessType.name(),
+                    result.getAccessRequest().getResource().getAsString()));
+            return false;
+        }
+    }
+
+    public void getFilterExpr(UserIdentity currentUser, HiveAccessType 
accessType,
+                              RangerHiveResource resource) throws 
HiveAccessControlException {
+        RangerAccessRequestImpl request = createRequest(currentUser, 
accessType);
+        request.setResource(resource);
+        RangerHiveAuditHandler auditHandler = new 
RangerHiveAuditHandler(hivePlugin.getConfig());
+        RangerAccessResult result = hivePlugin.isAccessAllowed(request, 
auditHandler);
+        LOG.debug("getFilterExpr: " + result.getFilterExpr());
+    }
+
+    public void getColumnMask(UserIdentity currentUser, HiveAccessType 
accessType,
+                              RangerHiveResource resource) {
+        RangerAccessRequestImpl request = createRequest(currentUser, 
accessType);
+        request.setResource(resource);
+        RangerHiveAuditHandler auditHandler = new 
RangerHiveAuditHandler(hivePlugin.getConfig());
+        RangerAccessResult result = hivePlugin.isAccessAllowed(request, 
auditHandler);
+        LOG.debug("getColumnMask:" + result.getMaskType());

Review Comment:
   merge into one LOG line



##########
fe/fe-core/src/main/java/org/apache/doris/catalog/authorizer/RangerHiveAccessController.java:
##########
@@ -0,0 +1,188 @@
+// 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.doris.catalog.authorizer;
+
+import org.apache.doris.analysis.UserIdentity;
+import org.apache.doris.common.AuthorizationException;
+import org.apache.doris.mysql.privilege.CatalogAccessController;
+import org.apache.doris.mysql.privilege.PrivPredicate;
+
+import 
org.apache.hadoop.hive.ql.security.authorization.plugin.HiveAccessControlException;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.apache.ranger.plugin.policyengine.RangerAccessRequest;
+import org.apache.ranger.plugin.policyengine.RangerAccessRequestImpl;
+import org.apache.ranger.plugin.policyengine.RangerAccessResult;
+import org.apache.ranger.plugin.policyengine.RangerPolicyEngine;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+public class RangerHiveAccessController implements CatalogAccessController {
+    public static final String CLIENT_TYPE_DORIS = "doris";
+    private static final Logger LOG = 
LogManager.getLogger(RangerHiveAccessController.class);
+    private static volatile RangerHivePlugin hivePlugin = null;
+
+    public RangerHiveAccessController(Map<String, String> properties) {
+        String serviceName = properties.get("ranger.service.name");
+        hivePlugin = new RangerHivePlugin(serviceName);
+    }
+
+    private RangerAccessRequestImpl createRequest(UserIdentity currentUser, 
HiveAccessType accessType) {
+        RangerAccessRequestImpl request = new RangerAccessRequestImpl();
+        request.setUser(currentUser.getQualifiedUser());
+        request.setUserRoles(currentUser.getRoles());
+        request.setAction(accessType.name());
+        if (accessType == HiveAccessType.USE) {
+            request.setAccessType(RangerPolicyEngine.ANY_ACCESS);
+        } else {
+            request.setAccessType(accessType.name().toLowerCase());
+        }
+        request.setClientIPAddress(currentUser.getHost());
+        request.setClientType(CLIENT_TYPE_DORIS);
+        request.setAccessTime(new Date());
+
+        return request;
+    }
+
+    public void checkPrivileges(UserIdentity currentUser, HiveAccessType 
accessType,

Review Comment:
   ```suggestion
       private void checkPrivileges(UserIdentity currentUser, HiveAccessType 
accessType,
   ```



##########
fe/fe-core/src/main/java/org/apache/doris/catalog/authorizer/RangerHiveResource.java:
##########
@@ -0,0 +1,95 @@
+// 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.doris.catalog.authorizer;
+
+import org.apache.ranger.plugin.policyengine.RangerAccessResourceImpl;
+
+public class RangerHiveResource extends RangerAccessResourceImpl {
+    public static final String KEY_DATABASE = "database";
+    public static final String KEY_TABLE = "table";
+    public static final String KEY_UDF = "udf";
+    public static final String KEY_COLUMN = "column";
+    private HiveObjectType objectType = null;
+
+    //FirstLevelResource => Database
+    //SecondLevelResource => Table or UDF
+    //ThirdLevelResource => column
+    public RangerHiveResource(HiveObjectType objectType, String 
firstLevelResource) {
+        this(objectType, firstLevelResource, null, null);
+    }
+
+    public RangerHiveResource(HiveObjectType objectType, String 
firstLevelResource, String secondLevelResource) {
+        this(objectType, firstLevelResource, secondLevelResource, null);
+    }
+
+    public RangerHiveResource(HiveObjectType objectType, String 
firstLevelResource, String secondLevelResource,
+                              String thirdLevelResource) {
+        this.objectType = objectType;
+
+        switch (objectType) {
+            case DATABASE:
+                setValue(KEY_DATABASE, firstLevelResource);
+                break;
+
+            case FUNCTION:
+                if (firstLevelResource == null) {

Review Comment:
   Why only change `firstLevelResource`? What about second and third?
   And I saw in `setValue`, if `value` is null, it will be removed from the 
`elements`.
   
   Please add some comments in code to explain.
   



##########
fe/fe-core/src/main/java/org/apache/doris/catalog/authorizer/RangerHiveAccessController.java:
##########
@@ -0,0 +1,188 @@
+// 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.doris.catalog.authorizer;
+
+import org.apache.doris.analysis.UserIdentity;
+import org.apache.doris.common.AuthorizationException;
+import org.apache.doris.mysql.privilege.CatalogAccessController;
+import org.apache.doris.mysql.privilege.PrivPredicate;
+
+import 
org.apache.hadoop.hive.ql.security.authorization.plugin.HiveAccessControlException;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.apache.ranger.plugin.policyengine.RangerAccessRequest;
+import org.apache.ranger.plugin.policyengine.RangerAccessRequestImpl;
+import org.apache.ranger.plugin.policyengine.RangerAccessResult;
+import org.apache.ranger.plugin.policyengine.RangerPolicyEngine;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+public class RangerHiveAccessController implements CatalogAccessController {
+    public static final String CLIENT_TYPE_DORIS = "doris";
+    private static final Logger LOG = 
LogManager.getLogger(RangerHiveAccessController.class);
+    private static volatile RangerHivePlugin hivePlugin = null;
+
+    public RangerHiveAccessController(Map<String, String> properties) {
+        String serviceName = properties.get("ranger.service.name");
+        hivePlugin = new RangerHivePlugin(serviceName);
+    }
+
+    private RangerAccessRequestImpl createRequest(UserIdentity currentUser, 
HiveAccessType accessType) {
+        RangerAccessRequestImpl request = new RangerAccessRequestImpl();
+        request.setUser(currentUser.getQualifiedUser());
+        request.setUserRoles(currentUser.getRoles());
+        request.setAction(accessType.name());
+        if (accessType == HiveAccessType.USE) {
+            request.setAccessType(RangerPolicyEngine.ANY_ACCESS);
+        } else {
+            request.setAccessType(accessType.name().toLowerCase());
+        }
+        request.setClientIPAddress(currentUser.getHost());
+        request.setClientType(CLIENT_TYPE_DORIS);
+        request.setAccessTime(new Date());
+
+        return request;
+    }
+
+    public void checkPrivileges(UserIdentity currentUser, HiveAccessType 
accessType,
+                                List<RangerHiveResource> hiveResources) throws 
AuthorizationException {
+        List<RangerAccessRequest> requests = new ArrayList<>();
+        for (RangerHiveResource resource : hiveResources) {
+            RangerAccessRequestImpl request = createRequest(currentUser, 
accessType);
+            request.setResource(resource);
+
+            requests.add(request);
+        }
+
+        RangerHiveAuditHandler auditHandler = new 
RangerHiveAuditHandler(hivePlugin.getConfig());
+        Collection<RangerAccessResult> results = 
hivePlugin.isAccessAllowed(requests, auditHandler);
+        for (RangerAccessResult result : results) {
+            LOG.debug("match policy:" + result.getPolicyId());
+            LOG.debug("will audit: " + result.toString());
+            auditHandler.processResult(result);
+            if (!result.getIsAllowed()) {
+                LOG.warn(result.getReason());

Review Comment:
   ```suggestion
                   LOG.debug(result.getReason());
   ```



##########
fe/fe-core/src/main/java/org/apache/doris/catalog/authorizer/RangerHiveAccessController.java:
##########
@@ -0,0 +1,188 @@
+// 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.doris.catalog.authorizer;
+
+import org.apache.doris.analysis.UserIdentity;
+import org.apache.doris.common.AuthorizationException;
+import org.apache.doris.mysql.privilege.CatalogAccessController;
+import org.apache.doris.mysql.privilege.PrivPredicate;
+
+import 
org.apache.hadoop.hive.ql.security.authorization.plugin.HiveAccessControlException;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.apache.ranger.plugin.policyengine.RangerAccessRequest;
+import org.apache.ranger.plugin.policyengine.RangerAccessRequestImpl;
+import org.apache.ranger.plugin.policyengine.RangerAccessResult;
+import org.apache.ranger.plugin.policyengine.RangerPolicyEngine;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+public class RangerHiveAccessController implements CatalogAccessController {
+    public static final String CLIENT_TYPE_DORIS = "doris";
+    private static final Logger LOG = 
LogManager.getLogger(RangerHiveAccessController.class);
+    private static volatile RangerHivePlugin hivePlugin = null;

Review Comment:
   Why using static? 



##########
fe/fe-core/src/main/java/org/apache/doris/catalog/authorizer/RangerHiveAccessController.java:
##########
@@ -0,0 +1,188 @@
+// 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.doris.catalog.authorizer;
+
+import org.apache.doris.analysis.UserIdentity;
+import org.apache.doris.common.AuthorizationException;
+import org.apache.doris.mysql.privilege.CatalogAccessController;
+import org.apache.doris.mysql.privilege.PrivPredicate;
+
+import 
org.apache.hadoop.hive.ql.security.authorization.plugin.HiveAccessControlException;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.apache.ranger.plugin.policyengine.RangerAccessRequest;
+import org.apache.ranger.plugin.policyengine.RangerAccessRequestImpl;
+import org.apache.ranger.plugin.policyengine.RangerAccessResult;
+import org.apache.ranger.plugin.policyengine.RangerPolicyEngine;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+public class RangerHiveAccessController implements CatalogAccessController {
+    public static final String CLIENT_TYPE_DORIS = "doris";
+    private static final Logger LOG = 
LogManager.getLogger(RangerHiveAccessController.class);
+    private static volatile RangerHivePlugin hivePlugin = null;
+
+    public RangerHiveAccessController(Map<String, String> properties) {
+        String serviceName = properties.get("ranger.service.name");
+        hivePlugin = new RangerHivePlugin(serviceName);
+    }
+
+    private RangerAccessRequestImpl createRequest(UserIdentity currentUser, 
HiveAccessType accessType) {
+        RangerAccessRequestImpl request = new RangerAccessRequestImpl();
+        request.setUser(currentUser.getQualifiedUser());
+        request.setUserRoles(currentUser.getRoles());
+        request.setAction(accessType.name());
+        if (accessType == HiveAccessType.USE) {
+            request.setAccessType(RangerPolicyEngine.ANY_ACCESS);
+        } else {
+            request.setAccessType(accessType.name().toLowerCase());
+        }
+        request.setClientIPAddress(currentUser.getHost());
+        request.setClientType(CLIENT_TYPE_DORIS);
+        request.setAccessTime(new Date());
+
+        return request;
+    }
+
+    public void checkPrivileges(UserIdentity currentUser, HiveAccessType 
accessType,
+                                List<RangerHiveResource> hiveResources) throws 
AuthorizationException {
+        List<RangerAccessRequest> requests = new ArrayList<>();
+        for (RangerHiveResource resource : hiveResources) {
+            RangerAccessRequestImpl request = createRequest(currentUser, 
accessType);
+            request.setResource(resource);
+
+            requests.add(request);
+        }
+
+        RangerHiveAuditHandler auditHandler = new 
RangerHiveAuditHandler(hivePlugin.getConfig());
+        Collection<RangerAccessResult> results = 
hivePlugin.isAccessAllowed(requests, auditHandler);
+        for (RangerAccessResult result : results) {
+            LOG.debug("match policy:" + result.getPolicyId());
+            LOG.debug("will audit: " + result.toString());
+            auditHandler.processResult(result);
+            if (!result.getIsAllowed()) {
+                LOG.warn(result.getReason());
+                throw new AuthorizationException(String.format(
+                    "Permission denied: user [%s] does not have privilege for 
[%s] command on [%s]",
+                    currentUser.getQualifiedUser(), accessType.name(),
+                    result.getAccessRequest().getResource().getAsString()));
+            }
+        }
+    }
+
+    public boolean checkPrivilege(UserIdentity currentUser, HiveAccessType 
accessType,
+                                  RangerHiveResource resource) {
+        RangerHiveAuditHandler auditHandler = new 
RangerHiveAuditHandler(hivePlugin.getConfig());

Review Comment:
   ditto



##########
fe/fe-core/src/main/java/org/apache/doris/catalog/authorizer/RangerHiveAccessController.java:
##########
@@ -0,0 +1,188 @@
+// 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.doris.catalog.authorizer;
+
+import org.apache.doris.analysis.UserIdentity;
+import org.apache.doris.common.AuthorizationException;
+import org.apache.doris.mysql.privilege.CatalogAccessController;
+import org.apache.doris.mysql.privilege.PrivPredicate;
+
+import 
org.apache.hadoop.hive.ql.security.authorization.plugin.HiveAccessControlException;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.apache.ranger.plugin.policyengine.RangerAccessRequest;
+import org.apache.ranger.plugin.policyengine.RangerAccessRequestImpl;
+import org.apache.ranger.plugin.policyengine.RangerAccessResult;
+import org.apache.ranger.plugin.policyengine.RangerPolicyEngine;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+public class RangerHiveAccessController implements CatalogAccessController {
+    public static final String CLIENT_TYPE_DORIS = "doris";
+    private static final Logger LOG = 
LogManager.getLogger(RangerHiveAccessController.class);
+    private static volatile RangerHivePlugin hivePlugin = null;
+
+    public RangerHiveAccessController(Map<String, String> properties) {
+        String serviceName = properties.get("ranger.service.name");
+        hivePlugin = new RangerHivePlugin(serviceName);
+    }
+
+    private RangerAccessRequestImpl createRequest(UserIdentity currentUser, 
HiveAccessType accessType) {
+        RangerAccessRequestImpl request = new RangerAccessRequestImpl();
+        request.setUser(currentUser.getQualifiedUser());
+        request.setUserRoles(currentUser.getRoles());
+        request.setAction(accessType.name());
+        if (accessType == HiveAccessType.USE) {
+            request.setAccessType(RangerPolicyEngine.ANY_ACCESS);
+        } else {
+            request.setAccessType(accessType.name().toLowerCase());
+        }
+        request.setClientIPAddress(currentUser.getHost());
+        request.setClientType(CLIENT_TYPE_DORIS);
+        request.setAccessTime(new Date());
+
+        return request;
+    }
+
+    public void checkPrivileges(UserIdentity currentUser, HiveAccessType 
accessType,
+                                List<RangerHiveResource> hiveResources) throws 
AuthorizationException {
+        List<RangerAccessRequest> requests = new ArrayList<>();
+        for (RangerHiveResource resource : hiveResources) {
+            RangerAccessRequestImpl request = createRequest(currentUser, 
accessType);
+            request.setResource(resource);
+
+            requests.add(request);
+        }
+
+        RangerHiveAuditHandler auditHandler = new 
RangerHiveAuditHandler(hivePlugin.getConfig());
+        Collection<RangerAccessResult> results = 
hivePlugin.isAccessAllowed(requests, auditHandler);
+        for (RangerAccessResult result : results) {
+            LOG.debug("match policy:" + result.getPolicyId());
+            LOG.debug("will audit: " + result.toString());
+            auditHandler.processResult(result);
+            if (!result.getIsAllowed()) {
+                LOG.warn(result.getReason());
+                throw new AuthorizationException(String.format(
+                    "Permission denied: user [%s] does not have privilege for 
[%s] command on [%s]",
+                    currentUser.getQualifiedUser(), accessType.name(),
+                    result.getAccessRequest().getResource().getAsString()));
+            }
+        }
+    }
+
+    public boolean checkPrivilege(UserIdentity currentUser, HiveAccessType 
accessType,
+                                  RangerHiveResource resource) {
+        RangerHiveAuditHandler auditHandler = new 
RangerHiveAuditHandler(hivePlugin.getConfig());
+        RangerAccessRequestImpl request = createRequest(currentUser, 
accessType);
+        request.setResource(resource);
+
+        RangerAccessResult result = hivePlugin.isAccessAllowed(request, 
auditHandler);
+        auditHandler.processResult(result);
+        auditHandler.flushAudit();
+
+        if (result == null) {
+            LOG.warn("Error getting authorizer result, please check your 
ranger config");

Review Comment:
   Could we print more detail error info here?



##########
fe/fe-core/src/main/java/org/apache/doris/catalog/authorizer/RangerHiveAccessController.java:
##########
@@ -0,0 +1,188 @@
+// 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.doris.catalog.authorizer;
+
+import org.apache.doris.analysis.UserIdentity;
+import org.apache.doris.common.AuthorizationException;
+import org.apache.doris.mysql.privilege.CatalogAccessController;
+import org.apache.doris.mysql.privilege.PrivPredicate;
+
+import 
org.apache.hadoop.hive.ql.security.authorization.plugin.HiveAccessControlException;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.apache.ranger.plugin.policyengine.RangerAccessRequest;
+import org.apache.ranger.plugin.policyengine.RangerAccessRequestImpl;
+import org.apache.ranger.plugin.policyengine.RangerAccessResult;
+import org.apache.ranger.plugin.policyengine.RangerPolicyEngine;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+public class RangerHiveAccessController implements CatalogAccessController {
+    public static final String CLIENT_TYPE_DORIS = "doris";
+    private static final Logger LOG = 
LogManager.getLogger(RangerHiveAccessController.class);
+    private static volatile RangerHivePlugin hivePlugin = null;
+
+    public RangerHiveAccessController(Map<String, String> properties) {
+        String serviceName = properties.get("ranger.service.name");
+        hivePlugin = new RangerHivePlugin(serviceName);
+    }
+
+    private RangerAccessRequestImpl createRequest(UserIdentity currentUser, 
HiveAccessType accessType) {
+        RangerAccessRequestImpl request = new RangerAccessRequestImpl();
+        request.setUser(currentUser.getQualifiedUser());
+        request.setUserRoles(currentUser.getRoles());
+        request.setAction(accessType.name());
+        if (accessType == HiveAccessType.USE) {
+            request.setAccessType(RangerPolicyEngine.ANY_ACCESS);
+        } else {
+            request.setAccessType(accessType.name().toLowerCase());
+        }
+        request.setClientIPAddress(currentUser.getHost());
+        request.setClientType(CLIENT_TYPE_DORIS);
+        request.setAccessTime(new Date());
+
+        return request;
+    }
+
+    public void checkPrivileges(UserIdentity currentUser, HiveAccessType 
accessType,
+                                List<RangerHiveResource> hiveResources) throws 
AuthorizationException {
+        List<RangerAccessRequest> requests = new ArrayList<>();
+        for (RangerHiveResource resource : hiveResources) {
+            RangerAccessRequestImpl request = createRequest(currentUser, 
accessType);
+            request.setResource(resource);
+
+            requests.add(request);
+        }
+
+        RangerHiveAuditHandler auditHandler = new 
RangerHiveAuditHandler(hivePlugin.getConfig());
+        Collection<RangerAccessResult> results = 
hivePlugin.isAccessAllowed(requests, auditHandler);
+        for (RangerAccessResult result : results) {
+            LOG.debug("match policy:" + result.getPolicyId());
+            LOG.debug("will audit: " + result.toString());
+            auditHandler.processResult(result);
+            if (!result.getIsAllowed()) {
+                LOG.warn(result.getReason());
+                throw new AuthorizationException(String.format(
+                    "Permission denied: user [%s] does not have privilege for 
[%s] command on [%s]",
+                    currentUser.getQualifiedUser(), accessType.name(),
+                    result.getAccessRequest().getResource().getAsString()));
+            }
+        }
+    }
+
+    public boolean checkPrivilege(UserIdentity currentUser, HiveAccessType 
accessType,
+                                  RangerHiveResource resource) {
+        RangerHiveAuditHandler auditHandler = new 
RangerHiveAuditHandler(hivePlugin.getConfig());
+        RangerAccessRequestImpl request = createRequest(currentUser, 
accessType);
+        request.setResource(resource);
+
+        RangerAccessResult result = hivePlugin.isAccessAllowed(request, 
auditHandler);
+        auditHandler.processResult(result);
+        auditHandler.flushAudit();
+
+        if (result == null) {
+            LOG.warn("Error getting authorizer result, please check your 
ranger config");
+            return false;
+        }
+
+        if (result.getIsAllowed()) {
+            LOG.debug("pass");
+            return true;
+        } else {
+            LOG.warn(String.format(

Review Comment:
   ```suggestion
               LOG.debug(String.format(
   ```



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