[ 
https://issues.apache.org/jira/browse/KNOX-3622?focusedWorklogId=1057644&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-1057644
 ]

ASF GitHub Bot logged work on KNOX-3622:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 30/Nov/26 14:28
            Start Date: 30/Nov/26 14:28
    Worklog Time Spent: 10m 
      Work Description: handavid commented on code in PR #1552:
URL: https://github.com/apache/knox/pull/1552#discussion_r4740526113


##########
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:
   what about multi-valued attributes? `getString()` only returns the first 
value.





Issue Time Tracking
-------------------

    Worklog Id:     (was: 1057644)
    Time Spent: 40m  (was: 0.5h)

> KnoxIDF: allow the LDAP user params lookup to use the Knox LDAP service
> -----------------------------------------------------------------------
>
>                 Key: KNOX-3622
>                 URL: https://issues.apache.org/jira/browse/KNOX-3622
>             Project: Apache Knox
>          Issue Type: Improvement
>          Components: KnoxIDF, LDAP
>            Reporter: Sandor Molnar
>            Assignee: Sandor Molnar
>            Priority: Major
>             Fix For: 3.1.0
>
>          Time Spent: 40m
>  Remaining Estimate: 0h
>
> Today the KnoxIDF LDAP user params provider ({{LdapUserParamsProvider}}) 
> always opens its own JNDI connection. It needs separate configuration: 
> {{user.params.provider.ldap.url}}, {{systemUser}}, {{systemPasswordAlias}}, 
> {{userSearchBase}}, {{groupSearchBase}}, and so on. In production this 
> usually points back at Knox's own embedded LDAP server. The same backend is 
> then configured twice, and every lookup does a network bind and search 
> against our own process.
> {{HadoopGroupProvider}} already avoids this with {{use.ldap.service=true}}, 
> which resolves groups in-process through {{KnoxLDAPService}}. KnoxIDF should 
> offer the same option.
> *Proposed changes*
> * Add a {{lookupUser(username, attributes...)}} method to {{KnoxLDAPService}} 
> / {{KnoxLDAPServerManager}}, next to {{lookupUserGroups}}. It returns the 
> requested attributes and the user's groups. The search runs through the 
> embedded server's admin session, so it uses the full interceptor chain: the 
> proxy backend, attribute mapping and the roles lookup interceptor. 
> {{lookupUserGroups}} is reimplemented on top of it.
> * Escape the username in the {{(uid=...)}} search filter. It is currently 
> concatenated unescaped, so a username such as {{*}} matches some other user. 
> The fix also covers the existing {{lookupUserGroups}} callers (the Hadoop 
> group provider and the delegation policy).
> * Add {{KnoxLdapServiceUserParamsProvider}}. It maps cn/sn/givenName/mail to 
> name/family_name/given_name/email, plus sub, email_verified and roles. Both 
> providers share the claim mapping, so their output can't drift apart.
> * Add the topology parameter {{user.params.provider.ldap.use.ldap.service}} 
> (default {{false}}). When it is {{true}} and {{gateway.ldap.enabled}} is on, 
> the new provider is used. It takes precedence over 
> {{user.params.provider.ldap.url}}, and no URL, bind user or password alias is 
> needed. If the embedded LDAP is disabled, a warning is logged and the 
> existing behavior applies ({{url}} gives the JNDI provider; otherwise no user 
> params). {{knoxidf.token.user.params.enabled}} still applies.
> * Roles semantics: the new provider returns every group the LDAP service 
> resolves (the same set {{HadoopGroupProvider}} sees). The JNDI provider keeps 
> only the groups directly under {{groupSearchBase}}. With the roles lookup 
> interceptor enabled, both return the same roles.
> * Document the new parameter in {{knox-site/docs/knoxidf/configuration.md}}.
> *Testing*
> * Unit tests for {{lookupUser}} (attributes, groups, user not found, filter 
> escaping), for the new provider, and for the provider factory's selection and 
> fallback logic.
> * E2E:
> ** A new {{knoxidf-token-ldap-service}} topology, which is {{knoxidf-token}} 
> with only the new flag in place of the LDAP connection settings.
> ** A new {{test_user_params_claims}} test checks the profile and email claims 
> in both the access token and /userinfo.
> ** The KnoxIDF suite is re-run through the new provider for guest and sam, 
> with a parity check against the JNDI provider.
> ** REST roles lookup variants are added.
> ** The KnoxIDF E2E tests now revoke the tokens and auth codes they obtain, so 
> they no longer use up the gateway-wide {{knox.token.limit.per.user}} that 
> other suites share.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to