ramackri commented on code in PR #1000: URL: https://github.com/apache/ranger/pull/1000#discussion_r3377983877
########## security-admin/src/main/java/org/apache/ranger/common/RangerSuperUserConfig.java: ########## @@ -0,0 +1,182 @@ +/* + * 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.ranger.common; + +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang3.StringUtils; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; + +/** + * Configuration-based Ranger Admin super users and super groups. + * When {@code ranger.admin.super.users} or + * {@code ranger.admin.super.groups} are set, matching authenticated users + * receive full Ranger administrative privileges (system admin and key admin + * capabilities) without requiring corresponding roles in the Ranger database. + */ +public final class RangerSuperUserConfig { + private RangerSuperUserConfig() { + } + + /** + * @return true when {@code ranger.admin.super.users} or + * {@code ranger.admin.super.groups} has at least one non-blank + * entry + */ + public static boolean isEnabled() { + return hasNonEmptyEntry( Review Comment: Done — `RangerSuperUserConfig` is now a lazy singleton that caches `enabled`, `superUsersConfigured`, and `superGroupsConfigured` at first use (commit 6839362f0). ########## security-admin/src/main/java/org/apache/ranger/biz/XUserMgr.java: ########## @@ -1887,6 +1897,51 @@ public Set<String> getGroupsForUser(String userName) { return ret; } + /** + * Returns synced group names from Ranger DB (no API permission checks). + * For config super-user / super-group matching at login only. + * + * @param userName user login id + * @return group names from {@code x_group_users} + */ + public Set<String> getSyncedGroupsForUser(final String userName) { + Set<String> ret = new HashSet<>(); + + try { + XXUser xUser = daoManager.getXXUser().findByUserName(userName); Review Comment: Done — added `XXGroupUserDao.findGroupNamesByUserName()` and `XUserMgr.getGroupsForUser()` uses a single query returning group names directly, avoiding separate `XXUser` and `XXGroupUser` lookups (commit 6839362f0). ########## security-admin/src/main/java/org/apache/ranger/biz/XUserMgr.java: ########## @@ -1887,6 +1897,51 @@ public Set<String> getGroupsForUser(String userName) { return ret; } + /** + * Returns synced group names from Ranger DB (no API permission checks). + * For config super-user / super-group matching at login only. + * + * @param userName user login id + * @return group names from {@code x_group_users} + */ + public Set<String> getSyncedGroupsForUser(final String userName) { Review Comment: Done — renamed `getSyncedGroupsForUser()` to `getGroupsForUser()` (commit 6839362f0). ########## security-admin/src/main/java/org/apache/ranger/biz/RangerBizUtil.java: ########## @@ -1117,13 +1129,48 @@ public boolean isUserAllowedForGrantRevoke(RangerService rangerService, String u return isUserInConfigParameter(rangerService, ServiceREST.Allowed_User_List_For_Grant_Revoke, userName); } + /** + * True when {@code username} is a full Ranger admin: DB sys/admin role, + * config super-user ({@code ranger.admin.super.users/groups}), or the + * current session is a Ranger admin ({@link UserSessionBase#isUserAdmin()}). + */ public boolean isUserRangerAdmin(String username) { + if (StringUtils.isBlank(username)) { + return false; + } + + UserSessionBase userSession = ContextUtil.getCurrentUserSession(); + + // Ranger admin on current session (DB sys admin or config super-user). + if (userSession != null + && username.equalsIgnoreCase(userSession.getLoginId()) + && userSession.isUserAdmin()) { + return true; + } + + // Config super-user when no session context (e.g. grantor checks). + if (RangerSuperUserConfig.isEnabled() && xUserMgr != null) { Review Comment: Done — `isUserRangerAdmin()` now uses the short-circuit pattern you suggested: check `isSuperUser(username)` first, then group lookup only when super-groups are configured (commit 6839362f0). ########## security-admin/src/main/java/org/apache/ranger/biz/RangerBizUtil.java: ########## @@ -1117,13 +1129,48 @@ public boolean isUserAllowedForGrantRevoke(RangerService rangerService, String u return isUserInConfigParameter(rangerService, ServiceREST.Allowed_User_List_For_Grant_Revoke, userName); } + /** + * True when {@code username} is a full Ranger admin: DB sys/admin role, + * config super-user ({@code ranger.admin.super.users/groups}), or the + * current session is a Ranger admin ({@link UserSessionBase#isUserAdmin()}). + */ public boolean isUserRangerAdmin(String username) { + if (StringUtils.isBlank(username)) { + return false; + } + + UserSessionBase userSession = ContextUtil.getCurrentUserSession(); + + // Ranger admin on current session (DB sys admin or config super-user). + if (userSession != null + && username.equalsIgnoreCase(userSession.getLoginId()) + && userSession.isUserAdmin()) { + return true; + } + + // Config super-user when no session context (e.g. grantor checks). + if (RangerSuperUserConfig.isEnabled() && xUserMgr != null) { + final boolean configSuperUser; + + if (RangerSuperUserConfig.isSuperGroupsConfigured()) { + configSuperUser = RangerSuperUserConfig.isSuperUser(username, + xUserMgr.getSyncedGroupsForUser(username)); + } else { + configSuperUser = RangerSuperUserConfig.isSuperUser(username); + } + + if (configSuperUser) { + return true; + } + } + boolean isAdmin = false; try { VXUser vxUser = xUserService.getXUserByUserName(username); - if (vxUser != null && (vxUser.getUserRoleList().contains(RangerConstants.ROLE_ADMIN) || vxUser.getUserRoleList().contains(RangerConstants.ROLE_SYS_ADMIN))) { + if (vxUser != null && (vxUser.getUserRoleList().contains(RangerConstants.ROLE_ADMIN) + || vxUser.getUserRoleList().contains(RangerConstants.ROLE_SYS_ADMIN))) { Review Comment: Acknowledged — reverted unrelated formatting changes in `RangerBizUtil` so the diff stays focused on this enhancement. -- 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]
