Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,26 @@
"platform:level3",
}

# The members a list search populates on each role entry, as the RDN value (uid) of each member
# DN. A role's members are the users directly assigned to it plus the user members of its assigned
# groups. Direct users and group members are resolved to user entries against the backend, so:
# - platform:admin is assigned to user admin and group admin ({admin}), so admin appears once;
# - platform:deployer is assigned to the id recursiveuser, resolved by a uid search to the
# directory's recursiveUser (uid matching is case-insensitive);
# - platform:level1/level2/level3 all resolve to recursiveUser: level1 contains the user directly,
# and level2/level3 nest the level below, which the LDAP proxy backend expands to the nested
# users (gateway.ldap.recursive.group.resolution).
EXPECTED_ROLE_MEMBERS = {
"platform:analyst": {"sam", "tom"},
"platform:scientist": {"sam"},
"platform:admin": {"admin"},
"platform:deployer": {"recursiveUser"},
"platform:viewer": {"guest"},
"platform:level1": {"recursiveUser"},
"platform:level2": {"recursiveUser"},
"platform:level3": {"recursiveUser"},
}

# (people base, group base) pairs: the proxy DNs and the backend DNs are both accepted.
BASE_PAIRS = (
(PROXY_PEOPLE_BASE, PROXY_GROUPS_BASE),
Expand Down Expand Up @@ -191,6 +211,54 @@ def test_list_all_roles_bypass_role_lookup(self) -> None:
self.assertTrue({"analyst", "scientist", "admin", "level1", "level2", "level3"} <= groups)
self.assertFalse(any(group.startswith("platform:") for group in groups))

# ---- List searches populate each role entry's member attribute ----

def role_members_by_role(self, entries) -> dict[str, set[str]]:
"""Map each role entry's cn (its RDN value) to the set of member uids, where each member
uid is the RDN value of a member DN. A role with no members maps to an empty set."""
members_by_role = {}
for entry in entries:
role = entry.entry_dn.split(",", 1)[0].split("=", 1)[1]
attributes = {name.lower(): values
for name, values in entry.entry_attributes_as_dict.items()}
member_dns = attributes.get("member", []) or []
members_by_role[role] = {member_dn.split(",", 1)[0].split("=", 1)[1]
for member_dn in member_dns}
return members_by_role

def assert_all_roles_members(self, attributes) -> None:
"""List all roles from the proxy ou=groups base and assert each role's resolved members."""
entries = self.search(
PROXY_GROUPS_BASE, "(objectClass=groupOfNames)", attributes=attributes)
self.assertEqual(EXPECTED_ROLE_MEMBERS, self.role_members_by_role(entries))

def test_list_roles_populates_members_for_all_attributes(self) -> None:
"""A '*' list search carries each role's members: the users directly assigned to the role
plus the user members of the role's groups. Regression: the members used to be dropped when
all attributes ('*') were requested, while an explicit 'member' request worked."""
self.assert_all_roles_members(ldap3.ALL_ATTRIBUTES)

def test_list_roles_populates_members_for_member_attribute(self) -> None:
"""A list search that requests only the member attribute carries each role's members."""
self.assert_all_roles_members(["member"])

def test_list_roles_omits_member_when_not_requested(self) -> None:
"""When the member attribute is not requested the role entries do not carry it (and no
member-resolution search is made)."""
entries = self.search(PROXY_GROUPS_BASE, "(objectClass=groupOfNames)", attributes=["cn"])
self.assertEqual(ALL_ROLES, set(self.rdn_values(entries)))
for entry in entries:
self.assertNotIn("member", self.attribute_names(entry))

def test_list_roles_members_bypass_role_lookup_returns_backend_group_members(self) -> None:
"""With role lookup bypassed the backend groups are listed with their own member DNs,
rather than roles with resolved members."""
entries = self.search(
PROXY_GROUPS_BASE, "(objectClass=groupOfNames)", [ROLE_LOOKUP_BYPASS_CONTROL])
members = self.role_members_by_role(entries)
self.assertEqual({"sam", "tom"}, members["analyst"])
self.assertNotIn("platform:analyst", members)

# ---- User role lookup: a user's memberOf is replaced with the user's roles ----

def search_user(self, uid_filter_value: str) -> tuple[str, set[str]]:
Expand All @@ -216,7 +284,8 @@ def user_roles(self, uid_filter_value: str) -> set[str]:
def test_user_roles_from_groups(self) -> None:
"""A user gets the roles of its groups, including nested groups."""
self.assertTrue({"platform:analyst", "platform:scientist"} <= self.user_roles("sam"))
self.assertIn("platform:level1", self.user_roles("recursiveUser"))
self.assertTrue({"platform:level1", "platform:level2", "platform:level3"} <=
self.user_roles("recursiveUser"))

def test_user_role_lookup_keeps_user_id_case(self) -> None:
"""By default the user id is sent as the directory has it (recursiveUser), so a role
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.apache.directory.api.ldap.model.exception.LdapNoSuchObjectException;
import org.apache.directory.api.ldap.model.exception.LdapOperationException;
import org.apache.directory.api.ldap.model.filter.ExprNode;
import org.apache.directory.api.ldap.model.filter.FilterEncoder;
import org.apache.directory.api.ldap.model.filter.FilterParser;
import org.apache.directory.api.ldap.model.message.LdapResult;
import org.apache.directory.api.ldap.model.message.Response;
Expand All @@ -44,6 +45,7 @@
import org.apache.directory.api.ldap.model.message.controls.PagedResults;
import org.apache.directory.api.ldap.model.message.controls.PagedResultsImpl;
import org.apache.directory.api.ldap.model.name.Dn;
import org.apache.directory.api.ldap.model.name.Rdn;
import org.apache.directory.api.ldap.model.schema.AttributeType;
import org.apache.directory.api.ldap.model.schema.LdapSyntax;
import org.apache.directory.api.ldap.model.schema.MatchingRule;
Expand Down Expand Up @@ -72,9 +74,11 @@
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -761,6 +765,8 @@ public List<Entry> search(String searchBase, SearchScope searchScope, String fil
if (attributes == null || attributes.length == 0) {
attributes = ALL_ATTRIBUTES;
}
boolean memberRequested = isAttributeRequested(attributes, "member", schemaManager)
|| isAttributeRequested(attributes, "uniqueMember", schemaManager);
boolean memberOfRequested = isAttributeRequested(attributes, MEMBER_OF, schemaManager);
String[] remoteAttributes = toRemoteAttributes(attributes, memberOfRequested && useMemberOf);

Expand Down Expand Up @@ -791,10 +797,14 @@ public List<Entry> search(String searchBase, SearchScope searchScope, String fil
}
throw e;
}
Map<String, List<String>> nestedGroupMembersCache = new HashMap<>();
for (Entry entry : searchResults) {
if (memberOfRequested) {
addGroupMemberships(entry, connection, entryCache, resolvedParentsCache);
}
if (memberRequested && recursiveGroupResolution) {
addNestedGroupMembers(entry, connection, schemaManager, nestedGroupMembersCache);
}
results.add(remoteSchemaConverter.convertRemoteEntryToProxyEntry(entry, schemaManager));
}
return results;
Expand Down Expand Up @@ -940,6 +950,193 @@ private void addGroupMemberships(Entry entry, LdapConnection connection, Map<Str
}
}

/**
* When recursive group resolution is enabled, expands a group entry's member list to include the
* members of the nested groups it references, transitively. Unlike the memberOf path, retrieving a
* group returns its member attribute as the backend stores it (direct members only), so the nesting
* is resolved here: a member that is itself a group has its members merged in, up to
* {@code recursiveGroupResolutionMaxDepth} levels, guarding against cycles.
* <p>
* Which members are groups to recurse into is decided authoritatively by object class rather than by
* DN location: each level issues one batched search that matches the level's candidate DNs and
* restricts to the group object classes, so the server returns only those candidates that are
* actually groups. This stays correct when users and groups share a container (a group DN that is a
* descendant of the user search base would otherwise be mistaken for a user and not expanded) while
* only fetching the groups, not every member. The entry is still a raw remote entry, so its member
* values - and the DNs searched - are remote DNs that are rewritten to proxy DNs later during schema
* conversion. Entries without members (e.g. users) are unchanged.
*/
private void addNestedGroupMembers(Entry entry, LdapConnection connection, SchemaManager schemaManager,
Map<String, List<String>> nestedGroupMembersCache) throws LdapException, CursorException, IOException {
final List<String> directMembers = collectGroupMemberDns(entry);
if (directMembers.isEmpty()) {
return;
}
// Seed the cycle guards with the group's own DN so a nested group that references it back does not
// re-add it (which would make the group a member of itself) or cause a redundant re-expansion.
final String ownNormDn = normalizeDn(entry.getDn().getName(), schemaManager);
final Set<String> knownMembers = new HashSet<>();
knownMembers.add(ownNormDn);
final Set<String> visitedGroups = new HashSet<>();
visitedGroups.add(ownNormDn);
// Merge nested members into whichever membership attribute the entry already uses, so a
// groupOfUniqueNames keeps one uniqueMember list instead of also gaining a stray member attribute.
final String memberAttribute = entry.get("member") != null ? "member" : "uniqueMember";
// Candidate member DNs whose type is not yet known; the batched group search below classifies them.
List<String> currentCandidates = new ArrayList<>();
for (String memberDn : directMembers) {
if (knownMembers.add(normalizeDn(memberDn, schemaManager))) {
currentCandidates.add(memberDn);
}
}
for (int depth = 0; !currentCandidates.isEmpty() && depth < recursiveGroupResolutionMaxDepth; depth++) {
final List<String> toExpand = new ArrayList<>();
for (String memberDn : currentCandidates) {
if (visitedGroups.add(normalizeDn(memberDn, schemaManager))) {
toExpand.add(memberDn);
}
}
if (toExpand.isEmpty()) {
break;
}
final List<String> nextCandidates = new ArrayList<>();
for (List<String> members : expandGroups(connection, toExpand, schemaManager, nestedGroupMembersCache).values()) {
for (String memberDn : members) {
if (knownMembers.add(normalizeDn(memberDn, schemaManager))) {
entry.add(memberAttribute, memberDn);
nextCandidates.add(memberDn);
}
}
}
currentCandidates = nextCandidates;
}
}

/**
* Resolves the direct member/uniqueMember DNs of the given candidate DNs that are groups. A single
* batched search (split if needed to honor maxFilterLength) matches the candidates by RDN and
* restricts to the group object classes, so candidates that are not groups (e.g. users) are filtered
* out by the server and never fetched. Results are matched back to the requested DNs exactly to guard
* against an RDN collision elsewhere in the subtree, and cached per search (including an empty result
* for a candidate that is not a group) so a group reached by several paths is read only once.
*
* @return a map from each resolved group's normalized DN to its member DNs
*/
private Map<String, List<String>> expandGroups(LdapConnection connection, List<String> candidateDns, SchemaManager schemaManager,
Map<String, List<String>> cache) throws LdapException, CursorException, IOException {
final Map<String, List<String>> resolved = new LinkedHashMap<>();
final Map<String, String> toSearch = new LinkedHashMap<>(); // normalized DN -> original DN
for (String dn : candidateDns) {
final String norm = normalizeDn(dn, schemaManager);
final List<String> cached = cache.get(norm);
if (cached != null) {
resolved.put(norm, cached);
} else {
toSearch.putIfAbsent(norm, dn);
}
}
if (toSearch.isEmpty()) {
return resolved;
}
// Seed an empty result for every searched DN so a candidate that is not a group is not re-searched.
toSearch.keySet().forEach(norm -> cache.put(norm, List.of()));

final String filter = buildGroupByDnFilter(toSearch.values());
if (filter == null) {
return resolved;
}
final List<Entry> groups = performSplitSearch(connection, remoteGroupSearchBase, splitFilter(filter),
SearchScope.SUBTREE, "member", "uniqueMember");
for (Entry group : groups) {
final String norm = normalizeDn(group.getDn().getName(), schemaManager);
if (!toSearch.containsKey(norm)) {
continue; // an RDN match for an entry outside the requested set
}
final List<String> members = collectGroupMemberDns(group);
cache.put(norm, members);
resolved.put(norm, members);
}
return resolved;
}

/**
* Builds the filter that fetches the given candidate DNs that are groups, matching each candidate by
* its RDN and restricting to the group object classes so non-group candidates are excluded by the
* server. Returns {@code null} when none of the candidates yields a usable RDN clause.
*/
private String buildGroupByDnFilter(Collection<String> candidateDns) {
final StringBuilder clauses = new StringBuilder();
for (String dn : candidateDns) {
final String clause = rdnClause(dn);
if (clause != null) {
clauses.append(clause);
}
}
if (clauses.length() == 0) {
return null;
}
return "(&" + groupObjectClassFilter() + "(|" + clauses + "))";
}

/** A filter clause matching an entry by its RDN, or {@code null} when the DN cannot be parsed. */
private String rdnClause(String dn) {
try {
// Parse without a schema manager: the value is a remote DN whose attributes the local schema may not know.
final Rdn rdn = new Dn(dn).getRdn();
return "(" + rdn.getType() + "=" + FilterEncoder.encodeFilterValue(rdn.getValue()) + ")";
} catch (LdapException e) {
return null;
}
}

/**
* An object-class filter matching group entries: the configured group object class plus the standard
* groupOfNames and groupOfUniqueNames, de-duplicated case-insensitively.
*/
private String groupObjectClassFilter() {
final Set<String> seen = new LinkedHashSet<>();
final StringBuilder sb = new StringBuilder();
int count = 0;
for (String objectClass : new String[] {remoteGroupObjectClass, "groupOfNames", "groupOfUniqueNames"}) {
if (objectClass != null && seen.add(objectClass.toLowerCase(ROOT))) {
sb.append("(objectClass=").append(objectClass).append(")");
count++;
}
}
return count == 1 ? sb.toString() : "(|" + sb + ")";
}

/** Collects an entry's member and uniqueMember values; empty for an entry that is not a group. */
private List<String> collectGroupMemberDns(Entry entry) {
final List<String> members = new ArrayList<>();
for (String attributeName : new String[] {"member", "uniqueMember"}) {
final Attribute attribute = entry.get(attributeName);
if (attribute != null) {
for (Value value : attribute) {
members.add(value.getString());
}
}
}
return members;
}

/** A whitespace- and case-normalized key for a DN, for de-duplicating members and detecting cycles. */
private String normalizeDn(String dn, SchemaManager schemaManager) {
try {
return new Dn(schemaManager, dn).getNormName();
} catch (LdapException e) {
// The proxy schema may not know a remote RDN attribute (e.g. sAMAccountName in a DN). Fall
// back to a schema-less DN, which still normalizes structure (whitespace, RDN order, type
// case) so two spellings of the same DN compare equal; only if that also fails lowercase the
// raw string.
try {
return new Dn(dn).getNormName();
} catch (LdapException e2) {
return dn.toLowerCase(Locale.ROOT);
}
}
}

private List<Entry> getUserGroupsEntries(LdapConnection connection, Entry user, Map<String, Entry> entryCache, Map<String, Set<String>> resolvedParentsCache) throws LdapException, CursorException, IOException {
List<Entry> groups = new ArrayList<>();
if (useMemberOf) {
Expand Down
Loading
Loading