zhoujinsong commented on code in PR #4118: URL: https://github.com/apache/amoro/pull/4118#discussion_r2993236244
########## amoro-ams/src/main/java/org/apache/amoro/server/authorization/LdapGroupRoleResolver.java: ########## @@ -0,0 +1,239 @@ +/* + * 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.apache.amoro.server.utils.PreconditionUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.naming.Context; +import javax.naming.NamingEnumeration; +import javax.naming.NamingException; +import javax.naming.directory.Attribute; +import javax.naming.directory.Attributes; +import javax.naming.directory.InitialDirContext; + +import java.text.MessageFormat; +import java.util.Collections; +import java.util.HashSet; +import java.util.Hashtable; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +class LdapGroupRoleResolver { + private static final Logger LOG = LoggerFactory.getLogger(LdapGroupRoleResolver.class); + + interface GroupMemberLoader { + Set<String> loadMembers(String groupDn, String memberAttribute) throws NamingException; + } + + private final boolean enabled; + private final String memberAttribute; + private final MessageFormat userDnFormatter; + private final Map<String, String> groupRoleMappings; + private final GroupMemberLoader groupMemberLoader; + + static LdapGroupRoleResolver disabled() { + return new LdapGroupRoleResolver(); + } + + LdapGroupRoleResolver(Configurations conf) { + this(conf, new JndiGroupMemberLoader(conf)); + } + + private LdapGroupRoleResolver() { + this.enabled = false; + this.memberAttribute = ""; + this.userDnFormatter = new MessageFormat("{0}"); + this.groupRoleMappings = Collections.emptyMap(); + this.groupMemberLoader = (groupDn, attribute) -> Collections.emptySet(); + } + + LdapGroupRoleResolver(Configurations conf, GroupMemberLoader groupMemberLoader) { + this.enabled = conf.get(AmoroManagementConf.AUTHORIZATION_LDAP_ROLE_MAPPING_ENABLED); + this.groupMemberLoader = groupMemberLoader; + if (!enabled) { + this.memberAttribute = ""; + this.userDnFormatter = new MessageFormat("{0}"); + this.groupRoleMappings = Collections.emptyMap(); + return; + } + + String ldapUrl = conf.get(AmoroManagementConf.HTTP_SERVER_LOGIN_AUTH_LDAP_URL); + PreconditionUtils.checkNotNullOrEmpty( + ldapUrl, AmoroManagementConf.HTTP_SERVER_LOGIN_AUTH_LDAP_URL.key()); + + this.memberAttribute = + conf.get(AmoroManagementConf.AUTHORIZATION_LDAP_ROLE_MAPPING_GROUP_MEMBER_ATTRIBUTE); + PreconditionUtils.checkNotNullOrEmpty( + memberAttribute, + AmoroManagementConf.AUTHORIZATION_LDAP_ROLE_MAPPING_GROUP_MEMBER_ATTRIBUTE.key()); + + String userDnPattern = + conf.get(AmoroManagementConf.AUTHORIZATION_LDAP_ROLE_MAPPING_USER_DN_PATTERN); + PreconditionUtils.checkNotNullOrEmpty( + userDnPattern, AmoroManagementConf.AUTHORIZATION_LDAP_ROLE_MAPPING_USER_DN_PATTERN.key()); + this.userDnFormatter = new MessageFormat(userDnPattern); + this.groupRoleMappings = loadGroupRoleMappings(conf); + } + + Set<String> resolve(String username) { + if (!enabled) { + return Collections.emptySet(); + } + + String userDn = userDnFormatter.format(new String[] {username}); + Set<String> resolvedRoles = new HashSet<>(); + for (Map.Entry<String, String> entry : groupRoleMappings.entrySet()) { + String groupDn = entry.getKey(); + try { + Set<String> members = groupMemberLoader.loadMembers(groupDn, memberAttribute); + if (members.stream().anyMatch(member -> matchesMember(username, userDn, member))) { + resolvedRoles.add(entry.getValue()); + } + } catch (NamingException e) { + LOG.error("Failed to query LDAP group {} for user {}", groupDn, username, e); + throw new RuntimeException( + "LDAP role resolution failed while querying group '" + groupDn + "'", e); + } + } + return resolvedRoles; + } + + private static Map<String, String> loadGroupRoleMappings(Configurations conf) { + List<Map<String, String>> groups = + conf.getOptional(AmoroManagementConf.AUTHORIZATION_LDAP_ROLE_MAPPING_GROUPS) + .orElse(Collections.emptyList()); + return groups.stream() + .filter(LdapGroupRoleResolver::hasRequiredGroupFields) + .collect( + Collectors.toMap( + group -> String.valueOf(group.get("group-dn")).trim(), + group -> + parseRoleName( + String.valueOf(group.get("group-dn")), String.valueOf(group.get("role"))), + (existing, replacement) -> replacement, + LinkedHashMap::new)); + } + + private static boolean hasRequiredGroupFields(Map<String, String> group) { + if (group.get("group-dn") == null || group.get("role") == null) { + LOG.warn( + "Ignore invalid http-server.authorization.ldap-role-mapping.groups entry: {}", group); + return false; + } Review Comment: **[Bug] LDAP group query fails hard on every login when one group is unreachable** If any single group DN throws a `NamingException`, the resolver re-throws a `RuntimeException`, which propagates all the way to `LoginController` and causes the login to fail — even though authentication already succeeded and the user might belong to other groups. Consider: (1) catching the exception per-group, logging a warning, and continuing to query the remaining groups; (2) or at least documenting that a partial LDAP outage will break all logins. The current test `testThrowsWhenLookupFails` validates the hard-fail behaviour, but that policy seems too strict for a background enrichment step. -- 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]
