smolnar82 commented on code in PR #1552:
URL: https://github.com/apache/knox/pull/1552#discussion_r4740672449
##########
gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/KnoxLDAPServerManager.java:
##########
@@ -643,40 +645,70 @@ public List<String> getUserGroups(String username) throws
Exception {
* @return the group names and whether the roles lookup was applied
*/
public UserGroupsLookupResult lookupUserGroups(String username) throws
Exception {
+ final UserLookupResult result = lookupUser(username);
+ return new UserGroupsLookupResult(result.groups(),
result.rolesLookupApplied());
+ }
+
+ /**
+ * Get a user's entry from the configured backend: the requested
attributes, the groups and whether
+ * the roles lookup interceptor ran the roles lookup for that user
+ * @param username The username
+ * @param attributeNames The attributes to return, if the user entry has
them
+ * @return the user's attributes (the first value of each, keyed by the
requested name), groups and
+ * whether the roles lookup was applied
+ */
+ public UserLookupResult lookupUser(String username, String...
attributeNames) throws Exception {
SearchRequest searchRequest = new SearchRequestImpl();
searchRequest.setBase(new Dn(directoryService.getSchemaManager(),
baseDn));
searchRequest.setScope(SearchScope.SUBTREE);
- searchRequest.setFilter("(uid=" + username + ")");
+ // Escape the username: a value such as "*" would otherwise match any
user
+ searchRequest.setFilter("(uid=" +
FilterEncoder.encodeFilterValue(username) + ")");
searchRequest.addAttributes("*");
- List<String> groups = new ArrayList<>();
- boolean userFound = false;
try (Cursor<Entry> cursor =
directoryService.getAdminSession().search(searchRequest)) {
if (cursor.next()) {
- userFound = true;
Entry entry = cursor.get();
- Attribute memberOf = entry.get("memberOf");
- if (memberOf != null) {
- for (Value value : memberOf) {
- try {
- Dn groupDn = new Dn(value.getString());
- String groupName =
LdapUtils.extractGroupName(groupDn);
- // A full group DN (cn=role,ou=groups,...) is
always resolved. A bare
- // RDN (cn=role) is only produced by the roles
lookup interceptor when
- // no template group DN was available (see
LDAPRolesLookupInterceptor#addRoleAttribute),
- // so only accept it when that interceptor is
active.
- if (groupName != null && (groupDn.size() > 1 ||
hasRolesLookupInterceptor)) {
- groups.add(groupName);
- }
- } catch (LdapInvalidDnException e) {
- // Skip memberOf values that are not valid DNs
- }
+ // The roles lookup interceptor looks up roles for each user
entry the search returns
+ return new UserLookupResult(true, getAttributes(entry,
attributeNames), getGroups(entry), hasRolesLookupInterceptor);
+ }
+ }
+ // No entry, no roles lookup
+ return UserLookupResult.notFound();
+ }
+
+ private static Map<String, String> getAttributes(Entry entry, String...
attributeNames) throws LdapException {
+ final Map<String, String> attributes = new LinkedHashMap<>();
+ for (String attributeName : attributeNames) {
+ final Attribute attribute = entry.get(attributeName);
+ final String value = attribute != null && attribute.size() > 0 &&
attribute.isHumanReadable() ? attribute.getString() : null;
Review Comment:
Good catch, and it's intentional: the attributes we read (`cn`, `sn`,
`givenName`, `mail`) feed `name`/`family_name`/`given_name`/`email`, which OIDC
Core §5.1 defines as single string claims, so we have to pick one value anyway.
That matches the existing JNDI `LdapUserParamsProvider` (`attr.get()` → first
value), and it's documented on `KnoxLDAPServerManager.lookupUser` and
`UserLookupResult`. The genuinely multi-valued data, the groups/roles from
`memberOf`, is returned in full as a list. One caveat: LDAP doesn't order
values, so a user with several `cn`/`mail` values gets whichever the backend
returns first, the same as with the JNDI provider today. I'd like to keep this
PR scoped as is. If you think `lookupUser` should return all values (e.g.
`Map<String, List<String>>`) for future callers, could you file a follow-up
JIRA?
--
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]