handavid commented on code in PR #1552:
URL: https://github.com/apache/knox/pull/1552#discussion_r4740767180
##########
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:
I see that the javadoc correctly states:
> @return the user's attributes (the first value of each, keyed by the
requested name)
I think we can hold off on a follow-up JIRA until we have a use-case for
returning all values.
--
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]