handavid commented on code in PR #1247: URL: https://github.com/apache/knox/pull/1247#discussion_r3370650395
########## gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/LDAPRolesLookupInterceptor.java: ########## @@ -0,0 +1,147 @@ +/* + * 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.knox.gateway.services.ldap; + +import org.apache.directory.api.ldap.model.cursor.ListCursor; +import org.apache.directory.api.ldap.model.entry.Attribute; +import org.apache.directory.api.ldap.model.entry.Entry; +import org.apache.directory.api.ldap.model.entry.Value; +import org.apache.directory.api.ldap.model.exception.LdapException; +import org.apache.directory.api.ldap.model.exception.LdapInvalidDnException; +import org.apache.directory.api.ldap.model.name.Dn; +import org.apache.directory.api.ldap.model.name.Rdn; +import org.apache.directory.server.core.api.filtering.EntryFilteringCursor; +import org.apache.directory.server.core.api.filtering.EntryFilteringCursorImpl; +import org.apache.directory.server.core.api.interceptor.BaseInterceptor; +import org.apache.directory.server.core.api.interceptor.context.SearchOperationContext; +import org.apache.knox.gateway.i18n.messages.MessagesFactory; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +/** + * Interceptor that replaces group names in memberOf attributes with role names + * if LDAP roles lookup is enabled. + */ +public class LDAPRolesLookupInterceptor extends BaseInterceptor { + private static final LdapMessages LOG = MessagesFactory.get(LdapMessages.class); + private final LDAPRolesLookupService rolesLookupService; + + public LDAPRolesLookupInterceptor(LDAPRolesLookupService rolesLookupService) { + this.rolesLookupService = rolesLookupService; + } + + @Override + public EntryFilteringCursor search(SearchOperationContext ctx) throws LdapException { + final List<Entry> entries = new ArrayList<>(); + try (EntryFilteringCursor cursor = next(ctx)) { + while (cursor.next()) { + entries.add(modifyEntry(cursor.get())); Review Comment: I don't think it makes sense to do this entry-by-entry. this will be very inefficient if we have an entry set where many users contain the same groups. Instead, I think this should be done in 3 steps. 1. collect all the users and groups 2. map the users and groups to roles. you can re-use the `RoleMapping` class 3. replace the `memberOf` groups with roles and add any roles for the user. -- 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]
