You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@knox.apache.org by sm...@apache.org on 2026/03/20 15:23:12 UTC
(knox) branch master updated: KNOX-3277 : LDAP Proxy improvements for working with AD backend (#1177)
This is an automated email from the ASF dual-hosted git repository.
smolnar pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/knox.git
The following commit(s) were added to refs/heads/master by this push:
new 18ab6c5ed KNOX-3277 : LDAP Proxy improvements for working with AD backend (#1177)
18ab6c5ed is described below
commit 18ab6c5edf309e36689373092b7408a8a6b7c8ba
Author: David Han <ha...@users.noreply.github.com>
AuthorDate: Fri Mar 20 10:23:06 2026 -0500
KNOX-3277 : LDAP Proxy improvements for working with AD backend (#1177)
The LdapProxyBackend and GroupLookupInterceptors are updated to work
with sAMAccountName in addition to uid and cn for user and group lookup.
This involved adding the memberOf and sAMAccountName attribute types
to the schema used by the proxy.
Group retrieval during user search is fixed to respect the useMemberOf flag.
Tests for the LdapProxyBackend were added.
---
gateway-server/pom.xml | 6 +
.../services/ldap/GroupLookupInterceptor.java | 13 +-
.../services/ldap/KnoxLDAPServerManager.java | 8 +-
.../knox/gateway/services/ldap/LdapMessages.java | 12 +
.../services/ldap/SchemaManagerFactory.java | 67 +++
.../services/ldap/backend/LdapProxyBackend.java | 63 ++-
.../ldap/backend/LdapProxyBackendTest.java | 455 +++++++++++++++++++++
.../test/resources/ldap-proxy-backend-test.ldif | 87 ++++
8 files changed, 682 insertions(+), 29 deletions(-)
diff --git a/gateway-server/pom.xml b/gateway-server/pom.xml
index e50154095..ab7ae5bb1 100644
--- a/gateway-server/pom.xml
+++ b/gateway-server/pom.xml
@@ -611,5 +611,11 @@
<artifactId>zookeeper-jute</artifactId>
<scope>test</scope>
</dependency>
+
+ <dependency>
+ <groupId>org.apache.mina</groupId>
+ <artifactId>mina-core</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
diff --git a/gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/GroupLookupInterceptor.java b/gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/GroupLookupInterceptor.java
index ea397fea7..12a1cd428 100644
--- a/gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/GroupLookupInterceptor.java
+++ b/gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/GroupLookupInterceptor.java
@@ -44,6 +44,7 @@ public class GroupLookupInterceptor extends BaseInterceptor {
private LdapBackend backend;
private static final Pattern UID_PATTERN = Pattern.compile(".*\\(uid=([^)]+)\\).*");
private static final Pattern CN_PATTERN = Pattern.compile(".*\\(cn=([^)]+)\\).*");
+ private static final Pattern SAMAACCOUNTNAME_PATTERN = Pattern.compile(".*\\(sAMAccountName=([^)]+)\\).*");
public GroupLookupInterceptor(DirectoryService directoryService, LdapBackend backend) {
this.directoryService = directoryService;
@@ -101,6 +102,9 @@ public class GroupLookupInterceptor extends BaseInterceptor {
if (backendEntry != null) {
// Return backend result directly without caching
entries.add(backendEntry);
+ LOG.ldapUserEntry(backendEntry.toString());
+ } else {
+ LOG.ldapUserNull(username);
}
}
} catch (Exception e) {
@@ -127,7 +131,9 @@ public class GroupLookupInterceptor extends BaseInterceptor {
}
private boolean isUserSearch(String filter) {
- return UID_PATTERN.matcher(filter).matches() || CN_PATTERN.matcher(filter).matches();
+ return UID_PATTERN.matcher(filter).matches()
+ || CN_PATTERN.matcher(filter).matches()
+ || SAMAACCOUNTNAME_PATTERN.matcher(filter).matches();
}
private String extractUser(String filter) {
@@ -141,6 +147,11 @@ public class GroupLookupInterceptor extends BaseInterceptor {
return cnMatcher.group(1);
}
+ Matcher samaaccountnameMatcher = SAMAACCOUNTNAME_PATTERN.matcher(filter);
+ if (samaaccountnameMatcher.matches()) {
+ return samaaccountnameMatcher.group(1);
+ }
+
return null;
}
}
diff --git a/gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/KnoxLDAPServerManager.java b/gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/KnoxLDAPServerManager.java
index 849f8e3a9..f0a87dc5b 100644
--- a/gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/KnoxLDAPServerManager.java
+++ b/gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/KnoxLDAPServerManager.java
@@ -21,8 +21,6 @@ import org.apache.directory.api.ldap.model.entry.DefaultEntry;
import org.apache.directory.api.ldap.model.entry.Entry;
import org.apache.directory.api.ldap.model.name.Dn;
import org.apache.directory.api.ldap.model.schema.SchemaManager;
-import org.apache.directory.api.ldap.schema.loader.JarLdifSchemaLoader;
-import org.apache.directory.api.ldap.schema.manager.impl.DefaultSchemaManager;
import org.apache.directory.server.core.DefaultDirectoryService;
import org.apache.directory.server.core.api.DirectoryService;
import org.apache.directory.server.core.api.InstanceLayout;
@@ -92,10 +90,8 @@ public class KnoxLDAPServerManager {
directoryService = new DefaultDirectoryService();
directoryService.setInstanceLayout(new InstanceLayout(workDir));
- // Create and load schema manager manually
- JarLdifSchemaLoader loader = new JarLdifSchemaLoader();
- SchemaManager schemaManager = new DefaultSchemaManager(loader);
- schemaManager.loadAllEnabled();
+ // Create SchemaManager
+ SchemaManager schemaManager = SchemaManagerFactory.createSchemaManager();
directoryService.setSchemaManager(schemaManager);
// Initialize schema partition
diff --git a/gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/LdapMessages.java b/gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/LdapMessages.java
index a03c0b689..a0b09c244 100644
--- a/gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/LdapMessages.java
+++ b/gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/LdapMessages.java
@@ -84,4 +84,16 @@ public interface LdapMessages {
@Message(level = MessageLevel.INFO,
text = "Cleaning up old lock file: {0}")
void ldapCleaningLockFile(String lockFile);
+
+ @Message(level = MessageLevel.DEBUG,
+ text = "Backend user found: {0}")
+ void ldapUserEntry(String user);
+
+ @Message(level = MessageLevel.DEBUG,
+ text = "Backend user not found: {0}")
+ void ldapUserNull(String username);
+
+ @Message(level = MessageLevel.ERROR,
+ text = "Failed to copy attribute: {0}")
+ void ldapAttributeCopyError(@StackTrace(level = MessageLevel.DEBUG) Exception e);
}
diff --git a/gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/SchemaManagerFactory.java b/gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/SchemaManagerFactory.java
new file mode 100644
index 000000000..611189389
--- /dev/null
+++ b/gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/SchemaManagerFactory.java
@@ -0,0 +1,67 @@
+/*
+ * 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.exception.LdapException;
+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;
+import org.apache.directory.api.ldap.model.schema.ObjectClass;
+import org.apache.directory.api.ldap.model.schema.SchemaManager;
+import org.apache.directory.api.ldap.schema.loader.JarLdifSchemaLoader;
+import org.apache.directory.api.ldap.schema.manager.impl.DefaultSchemaManager;
+
+import java.io.IOException;
+
+/**
+ * Factory class for creating SchemaManager instances.
+ */
+public class SchemaManagerFactory {
+
+ public static SchemaManager createSchemaManager() throws IOException, LdapException {
+ // Create and load schema manager manually
+ JarLdifSchemaLoader loader = new JarLdifSchemaLoader();
+ SchemaManager schemaManager = new DefaultSchemaManager(loader);
+ schemaManager.loadAllEnabled();
+
+ // Add Custom schemas
+ AttributeType memberOfAttrType = new AttributeType("1.2.840.113556.1.2.102");
+ memberOfAttrType.setNames(new String[]{"memberOf"});
+ memberOfAttrType.setSchemaName("other");
+ memberOfAttrType.setSyntax(new LdapSyntax("1.3.6.1.4.1.1466.115.121.1.12"));
+ memberOfAttrType.setDescription("attribute specifies the distinguished names of the groups to which this object belongs");
+ memberOfAttrType.setSingleValued(false);
+ schemaManager.add(memberOfAttrType);
+
+ AttributeType sAMAccountNameAttrType = new AttributeType("1.2.840.113556.1.4.221");
+ sAMAccountNameAttrType.setNames(new String[]{"sAMAccountName"});
+ sAMAccountNameAttrType.setSchemaName("other");
+ sAMAccountNameAttrType.setSyntax(new LdapSyntax("1.3.6.1.4.1.1466.115.121.1.15"));
+ sAMAccountNameAttrType.setDescription("Microsoft sAMAccountName attribute for compatibility");
+ sAMAccountNameAttrType.setEquality(new MatchingRule("caseignorematch"));
+ sAMAccountNameAttrType.setSubstring(new MatchingRule("caseignoresubstringsmatch"));
+ schemaManager.add(sAMAccountNameAttrType);
+
+ ObjectClass personObjectClass = schemaManager.lookupObjectClassRegistry("person");
+ personObjectClass.unlock();
+ personObjectClass.addMayAttributeTypes(memberOfAttrType, sAMAccountNameAttrType);
+ personObjectClass.lock();
+
+ return schemaManager;
+ }
+}
diff --git a/gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/backend/LdapProxyBackend.java b/gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/backend/LdapProxyBackend.java
index 2c37cb135..38328b571 100644
--- a/gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/backend/LdapProxyBackend.java
+++ b/gateway-server/src/main/java/org/apache/knox/gateway/services/ldap/backend/LdapProxyBackend.java
@@ -35,6 +35,7 @@ import org.apache.knox.gateway.services.ldap.LdapMessages;
import java.io.IOException;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
@@ -63,6 +64,14 @@ public class LdapProxyBackend implements LdapBackend {
private String groupMemberAttribute = "memberUid"; // member for AD, memberUid for POSIX
private boolean useMemberOf; // Use memberOf attribute for group lookup (efficient for AD)
+ private List<String> proxyEntryAttributeTypes = List.of(
+ // "uid" will always be filled
+ "cn",
+ "dn",
+ "mail",
+ "description");
+ private final String proxyEntryGroupMembershipAttributeType = "memberOf";
+
// Connection pool for efficient connection reuse
private LdapConnectionPool connectionPool;
@@ -120,7 +129,6 @@ public class LdapProxyBackend implements LdapBackend {
// Configure attribute mappings for AD/LDAP compatibility
userIdentifierAttribute = config.getOrDefault("userIdentifierAttribute", "uid");
- config.getOrDefault("userDnTemplate", "uid={username},ou=Users,{baseDn}");
groupMemberAttribute = config.getOrDefault("groupMemberAttribute", "memberUid");
useMemberOf = Boolean.parseBoolean(config.getOrDefault("useMemberOf", "false"));
@@ -260,7 +268,6 @@ public class LdapProxyBackend implements LdapBackend {
cursor.close();
return entry;
}
-
cursor.close();
return null;
} finally {
@@ -284,7 +291,7 @@ public class LdapProxyBackend implements LdapBackend {
if (cursor.next()) {
String userDn = cursor.get().getDn().toString();
cursor.close();
- return getUserGroupsInternal(connection, userDn, username);
+ return getCnsFromEntries(getUserGroupsInternal(connection, userDn, username));
}
cursor.close();
@@ -333,8 +340,8 @@ public class LdapProxyBackend implements LdapBackend {
return null;
}
- private List<String> getUserGroupsInternal(LdapConnection connection, String userDn, String username) throws LdapException, CursorException, IOException {
- List<String> groups = new ArrayList<>();
+ private List<Entry> getUserGroupsInternal(LdapConnection connection, String userDn, String username) throws LdapException, CursorException, IOException {
+ List<Entry> groups = new ArrayList<>();
// Search for groups where user is a member - build filter based on configuration
String filter;
@@ -356,17 +363,24 @@ public class LdapProxyBackend implements LdapBackend {
EntryCursor cursor = connection.search(groupSearchBase, filter, SearchScope.SUBTREE, "cn");
while (cursor.next()) {
- Entry groupEntry = cursor.get();
- Attribute cnAttr = groupEntry.get("cn");
- if (cnAttr != null) {
- groups.add(cnAttr.getString());
- }
+ groups.add(cursor.get());
}
cursor.close();
return groups;
}
+ private List<String> getCnsFromEntries(Collection<Entry> entries) throws LdapException {
+ List<String> cns = new ArrayList<>();
+ for (Entry entry : entries) {
+ Attribute cnAttr = entry.get("cn");
+ if (cnAttr != null) {
+ cns.add(cnAttr.getString());
+ }
+ }
+ return cns;
+ }
+
@Override
public List<Entry> searchUsers(String filter, SchemaManager schemaManager) throws Exception {
List<Entry> results = new ArrayList<>();
@@ -374,8 +388,7 @@ public class LdapProxyBackend implements LdapBackend {
try {
connection = getConnection();
- String searchValue = filter.contains("*") ? "*" : filter;
- String ldapFilter = "(" + userIdentifierAttribute + "=" + searchValue + ")";
+ String ldapFilter = "(" + userIdentifierAttribute + "=" + filter.trim() + ")";
EntryCursor cursor = connection.search(userSearchBase, ldapFilter, SearchScope.SUBTREE, "*");
while (cursor.next()) {
@@ -425,16 +438,17 @@ public class LdapProxyBackend implements LdapBackend {
}
}
- copyAttribute(sourceEntry, entry, "cn");
- copyAttribute(sourceEntry, entry, "sn");
- copyAttribute(sourceEntry, entry, "mail");
- copyAttribute(sourceEntry, entry, "description");
- copyAttribute(sourceEntry, entry, "memberOf"); // Preserve group memberships
+ for (String attributeType : proxyEntryAttributeTypes) {
+ copyAttribute(sourceEntry, entry, attributeType);
+ }
- // Get user's groups
- List<String> groups = getUserGroupsInternal(connection, sourceEntry.getDn().toString(), username);
- if (!groups.isEmpty()) {
- entry.add("description", "Groups: " + String.join(", ", groups));
+ if (useMemberOf) {
+ copyAttribute(sourceEntry, entry, proxyEntryGroupMembershipAttributeType);
+ } else {
+ List<Entry> groups = getUserGroupsInternal(connection, sourceEntry.getDn().toString(), username);
+ for (Entry groupEntry : groups) {
+ entry.add(proxyEntryGroupMembershipAttributeType, groupEntry.getDn().getName());
+ }
}
return entry;
@@ -445,7 +459,12 @@ public class LdapProxyBackend implements LdapBackend {
if (attr != null) {
// Copy all values of the attribute (important for multi-valued attributes like objectClass)
for (org.apache.directory.api.ldap.model.entry.Value value : attr) {
- target.add(attributeName, value.getString());
+ try {
+ target.add(attributeName, value.getString());
+ } catch (LdapException e) {
+ LOG.ldapAttributeCopyError(e);
+ throw e;
+ }
}
}
}
diff --git a/gateway-server/src/test/java/org/apache/knox/gateway/services/ldap/backend/LdapProxyBackendTest.java b/gateway-server/src/test/java/org/apache/knox/gateway/services/ldap/backend/LdapProxyBackendTest.java
new file mode 100644
index 000000000..a6b132b35
--- /dev/null
+++ b/gateway-server/src/test/java/org/apache/knox/gateway/services/ldap/backend/LdapProxyBackendTest.java
@@ -0,0 +1,455 @@
+/*
+ * 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.backend;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+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.schema.SchemaManager;
+import org.apache.directory.server.core.api.CoreSession;
+import org.apache.directory.server.core.api.DirectoryService;
+import org.apache.directory.server.core.api.InstanceLayout;
+import org.apache.directory.server.core.api.partition.Partition;
+import org.apache.directory.server.core.api.schema.SchemaPartition;
+import org.apache.directory.server.core.factory.JdbmPartitionFactory;
+import org.apache.directory.server.core.factory.PartitionFactory;
+import org.apache.directory.server.core.partition.ldif.LdifPartition;
+import org.apache.directory.server.ldap.LdapServer;
+import org.apache.directory.server.protocol.shared.store.LdifFileLoader;
+import org.apache.directory.server.protocol.shared.transport.TcpTransport;
+import org.apache.knox.gateway.security.ldap.SimpleDirectoryService;
+import org.apache.knox.gateway.services.ldap.SchemaManagerFactory;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import java.io.File;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
+
+public class LdapProxyBackendTest {
+ private static Map<String, String> ldapBackendConfig;
+
+ private static TcpTransport transport;
+ private static DirectoryService directoryService;
+ private static LdapServer ldapServer;
+ private static SchemaManager schemaManager;
+
+ private LdapProxyBackend ldapProxyBackend;
+
+ @BeforeClass
+ public static void setupBeforeClass() throws Exception {
+ transport = new TcpTransport(0);
+
+ directoryService = new SimpleDirectoryService();
+ directoryService.setShutdownHookEnabled(false);
+ // Use same schema for test that we use for code
+ schemaManager = SchemaManagerFactory.createSchemaManager();
+ directoryService.setSchemaManager(schemaManager);
+
+ String instanceDirectory = System.getProperty("java.io.tmpdir", "/tmp") + "/server-work-" + UUID.randomUUID().toString();
+ InstanceLayout instanceLayout = new InstanceLayout(instanceDirectory);
+ directoryService.setInstanceLayout(instanceLayout);
+
+ File workingDirectory = directoryService.getInstanceLayout().getPartitionsDirectory();
+ LdifPartition ldifPartition = new LdifPartition(schemaManager, directoryService.getDnFactory());
+ ldifPartition.setPartitionPath((new File(workingDirectory, "schema")).toURI());
+ SchemaPartition schemaPartition = new SchemaPartition(schemaManager);
+ schemaPartition.setWrappedPartition(ldifPartition);
+ directoryService.setSchemaPartition(schemaPartition);
+
+ PartitionFactory partitionFactory = new JdbmPartitionFactory();
+ Partition systemPartition = partitionFactory.createPartition(
+ schemaManager, directoryService.getDnFactory(),
+ "system", "ou=system", 500,
+ new File(directoryService.getInstanceLayout().getPartitionsDirectory(), "system"));
+ partitionFactory.addIndex(systemPartition, "objectClass", 100);
+ directoryService.setSystemPartition(systemPartition);
+ Partition partition = partitionFactory.createPartition(
+ schemaManager, directoryService.getDnFactory(),
+ "people", "dc=hadoop,dc=apache,dc=org", 500,
+ directoryService.getInstanceLayout().getInstanceDirectory());
+ directoryService.addPartition(partition);
+
+ // Start the DirectoryService
+ directoryService.startup();
+
+ // load test data from ldif file
+ File ldifFile = new File(
+ LdapProxyBackendTest.class.getResource( "/ldap-proxy-backend-test.ldif" )
+ .toURI());
+ CoreSession session = directoryService.getAdminSession();
+ LdifFileLoader lfl = new LdifFileLoader(session, ldifFile, null);
+ lfl.execute();
+
+ // Create and start the LDAP server
+ ldapServer = new LdapServer();
+ ldapServer.setTransports(transport);
+ ldapServer.setDirectoryService(directoryService);
+ ldapServer.start();
+
+ // Setup common backend config values for tests
+ ldapBackendConfig = Map.of(
+ "baseDn", "dc=hadoop,dc=apache,dc=org",
+ "remoteBaseDn", "dc=hadoop,dc=apache,dc=org",
+ "url", "ldap://localhost:" + transport.getAcceptor().getLocalAddress().getPort(),
+ "systemUsername", "uid=guest,ou=people,dc=hadoop,dc=apache,dc=org",
+ "systemPassword", "guest-password",
+ "userSearchBase", "ou=people,dc=hadoop,dc=apache,dc=org",
+ "groupSearchBase", "ou=groups,dc=hadoop,dc=apache,dc=org");
+ }
+
+ @AfterClass
+ public static void tearDownAfterClass() throws Exception {
+ if(ldapServer != null) {
+ ldapServer.stop();
+ }
+ if (directoryService != null) {
+ directoryService.shutdown();
+ }
+ }
+
+ @Before
+ public void setUp() throws Exception {
+ ldapProxyBackend = new LdapProxyBackend();
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ ldapProxyBackend.close();
+ }
+
+ @Test
+ public void testGetUserByDefaultUserSearchFilter() throws Exception {
+ // default searches by uid and uses group search for membership
+ ldapProxyBackend.initialize(ldapBackendConfig);
+
+ Entry entry = ldapProxyBackend.getUser("ldaptest1", schemaManager);
+ assertEquals("ldaptest1", entry.get("uid").getString());
+ assertEquals("TestCn1", entry.get("cn").getString());
+ assertEquals("ldaptest1@example.com", entry.get("mail").getString());
+ assertEquals("Test user ldaptest1", entry.get("description").getString());
+ assertNull(entry.get("sAMAccountName"));
+ assertEquals(2, entry.get("memberOf").size());
+ Set<String> expectedMemberOf = Set.of(
+ "cn=group1,ou=groups,dc=hadoop,dc=apache,dc=org",
+ "cn=group2,ou=groups,dc=hadoop,dc=apache,dc=org");
+ Set<String> foundMemberOf = new HashSet<>(2);
+ Iterator<Value> memberOfs = entry.get("memberOf").iterator();
+ while (memberOfs.hasNext()) {
+ foundMemberOf.add(memberOfs.next().getString());
+ }
+ assertEquals(expectedMemberOf, foundMemberOf);
+ }
+
+ @Test
+ public void testGetUserNotFound() throws Exception {
+ ldapProxyBackend.initialize(ldapBackendConfig);
+
+ Entry entry = ldapProxyBackend.getUser("nouser", schemaManager);
+ assertNull(entry);
+ }
+
+ @Test
+ public void testGetUserByUID() throws Exception {
+ Map<String, String> config = new HashMap<>(ldapBackendConfig);
+ config.put("userIdentifierAttribute", "uid");
+ ldapProxyBackend.initialize(config);
+
+ Entry entry = ldapProxyBackend.getUser("ldaptest1", schemaManager);
+ assertEquals("ldaptest1", entry.get("uid").getString());
+ assertEquals("TestCn1", entry.get("cn").getString());
+ assertEquals("ldaptest1@example.com", entry.get("mail").getString());
+ assertEquals("Test user ldaptest1", entry.get("description").getString());
+ assertNull(entry.get("sAMAccountName"));
+ assertEquals(2, entry.get("memberOf").size());
+ Set<String> expectedMemberOf = Set.of(
+ "cn=group1,ou=groups,dc=hadoop,dc=apache,dc=org",
+ "cn=group2,ou=groups,dc=hadoop,dc=apache,dc=org");
+ Set<String> foundMemberOf = new HashSet<>(2);
+ Iterator<Value> memberOfs = entry.get("memberOf").iterator();
+ while (memberOfs.hasNext()) {
+ foundMemberOf.add(memberOfs.next().getString());
+ }
+ assertEquals(expectedMemberOf, foundMemberOf);
+ }
+
+ @Test
+ public void testGetUserByCN() throws Exception {
+ Map<String, String> config = new HashMap<>(ldapBackendConfig);
+ config.put("userIdentifierAttribute", "cn");
+ ldapProxyBackend.initialize(config);
+
+ Entry entry = ldapProxyBackend.getUser("TestCn1", schemaManager);
+ assertEquals("TestCn1", entry.get("uid").getString());
+ assertEquals("TestCn1", entry.get("cn").getString());
+ assertEquals("ldaptest1@example.com", entry.get("mail").getString());
+ assertEquals("Test user ldaptest1", entry.get("description").getString());
+ assertNull(entry.get("sAMAccountName"));
+ assertEquals(2, entry.get("memberOf").size());
+ Set<String> expectedMemberOf = Set.of(
+ "cn=group1,ou=groups,dc=hadoop,dc=apache,dc=org",
+ "cn=group2,ou=groups,dc=hadoop,dc=apache,dc=org");
+ Set<String> foundMemberOf = new HashSet<>(2);
+ Iterator<Value> memberOfs = entry.get("memberOf").iterator();
+ while (memberOfs.hasNext()) {
+ foundMemberOf.add(memberOfs.next().getString());
+ }
+ assertEquals(expectedMemberOf, foundMemberOf);
+ }
+
+ @Test
+ public void testGetUserBySAMAccountName() throws Exception {
+ Map<String, String> config = new HashMap<>(ldapBackendConfig);
+ config.put("userIdentifierAttribute", "sAMAccountName");
+ ldapProxyBackend.initialize(config);
+
+ Entry entry = ldapProxyBackend.getUser("TestSam1", schemaManager);
+ assertEquals("TestSam1", entry.get("uid").getString());
+ assertEquals("TestCn1", entry.get("cn").getString());
+ assertEquals("ldaptest1@example.com", entry.get("mail").getString());
+ assertEquals("Test user ldaptest1", entry.get("description").getString());
+ assertEquals("TestSam1", entry.get("sAMAccountName").getString());
+ assertEquals(2, entry.get("memberOf").size());
+ Set<String> expectedMemberOf = Set.of(
+ "cn=group1,ou=groups,dc=hadoop,dc=apache,dc=org",
+ "cn=group2,ou=groups,dc=hadoop,dc=apache,dc=org");
+ Set<String> foundMemberOf = new HashSet<>(2);
+ Iterator<Value> memberOfs = entry.get("memberOf").iterator();
+ while (memberOfs.hasNext()) {
+ foundMemberOf.add(memberOfs.next().getString());
+ }
+ assertEquals(expectedMemberOf, foundMemberOf);
+ }
+
+ @Test
+ public void testGetUserUseMemberOf() throws Exception {
+ Map<String, String> config = new HashMap<>(ldapBackendConfig);
+ config.put("useMemberOf", "true");
+ ldapProxyBackend.initialize(config);
+
+ Entry entry = ldapProxyBackend.getUser("ldaptest2", schemaManager);
+ assertEquals("ldaptest2", entry.get("uid").getString());
+ assertEquals("TestCn2", entry.get("cn").getString());
+ assertEquals("ldaptest2@example.com", entry.get("mail").getString());
+ assertEquals("Test user ldaptest2", entry.get("description").getString());
+ assertNull(entry.get("sAMAccountName"));
+ assertEquals(2, entry.get("memberOf").size());
+ Set<String> expectedMemberOf = Set.of(
+ "cn=groupMemberOf1,ou=groups,dc=hadoop,dc=apache,dc=org",
+ "cn=groupMemberOf2,ou=groups,dc=hadoop,dc=apache,dc=org");
+ Set<String> foundMemberOf = new HashSet<>(2);
+ Iterator<Value> memberOfs = entry.get("memberOf").iterator();
+ while (memberOfs.hasNext()) {
+ foundMemberOf.add(memberOfs.next().getString());
+ }
+ assertEquals(expectedMemberOf, foundMemberOf);
+
+ }
+
+ @Test
+ public void testGetUserGroups() throws Exception {
+ ldapProxyBackend.initialize(ldapBackendConfig);
+
+ List<String> userGroups = ldapProxyBackend.getUserGroups("ldaptest1");
+ assertTrue(userGroups.contains("group1"));
+ assertTrue(userGroups.contains("group2"));
+ }
+
+ @Test
+ public void testGetUserGroupsNoGroups() throws Exception {
+ ldapProxyBackend.initialize(ldapBackendConfig);
+
+ List<String> userGroups = ldapProxyBackend.getUserGroups("ldaptest2");
+ assertTrue(userGroups.isEmpty());
+ }
+
+ @Test
+ public void testGetUserGroupsNoUser() throws Exception {
+ ldapProxyBackend.initialize(ldapBackendConfig);
+
+ List<String> userGroups = ldapProxyBackend.getUserGroups("nobody");
+ assertTrue(userGroups.isEmpty());
+ }
+
+ @Test
+ public void testGetUserGroupsUseMemberOf() throws Exception {
+ Map<String, String> config = new HashMap<>(ldapBackendConfig);
+ config.put("useMemberOf", "true");
+ ldapProxyBackend.initialize(config);
+
+ List<String> userGroups = ldapProxyBackend.getUserGroups("ldaptest2");
+ assertTrue(userGroups.contains("groupMemberOf1"));
+ assertTrue(userGroups.contains("groupMemberOf2"));
+ }
+
+ @Test
+ public void testGetUserGroupsUseMemberOfNoGroups() throws Exception {
+ Map<String, String> config = new HashMap<>(ldapBackendConfig);
+ config.put("useMemberOf", "true");
+ ldapProxyBackend.initialize(config);
+
+ List<String> userGroups = ldapProxyBackend.getUserGroups("ldaptest1");
+ assertTrue(userGroups.isEmpty());
+ }
+
+ @Test
+ public void testGetUserGroupsUseMemberOfNoUser() throws Exception {
+ Map<String, String> config = new HashMap<>(ldapBackendConfig);
+ config.put("useMemberOf", "true");
+ ldapProxyBackend.initialize(config);
+
+ List<String> userGroups = ldapProxyBackend.getUserGroups("nobody");
+ assertTrue(userGroups.isEmpty());
+ }
+
+ @Test
+ public void testSearchUsers() throws Exception {
+ // searches by uid by default
+ ldapProxyBackend.initialize(ldapBackendConfig);
+
+ List<Entry> entries = ldapProxyBackend.searchUsers("*", schemaManager);
+ Set<String> foundUids = new HashSet<>();
+ for (Entry entry : entries) {
+ foundUids.add(entry.get("uid").getString());
+ }
+ assertEquals(3, foundUids.size());
+ assertTrue(foundUids.contains("ldaptest1"));
+ assertTrue(foundUids.contains("ldaptest2"));
+ assertTrue(foundUids.contains("guest"));
+ }
+
+ @Test
+ public void testSearchUsersPartial() throws Exception {
+ // searches by uid by default
+ ldapProxyBackend.initialize(ldapBackendConfig);
+
+ List<Entry> entries = ldapProxyBackend.searchUsers("ldap*", schemaManager);
+ Set<String> foundUids = new HashSet<>();
+ for (Entry entry : entries) {
+ foundUids.add(entry.get("uid").getString());
+ }
+ assertEquals(2, foundUids.size());
+ assertTrue(foundUids.contains("ldaptest1"));
+ assertTrue(foundUids.contains("ldaptest2"));
+ }
+
+ @Test
+ public void testSearchUsersNoneFound() throws Exception {
+ // searches by uid by default
+ ldapProxyBackend.initialize(ldapBackendConfig);
+
+ List<Entry> entries = ldapProxyBackend.searchUsers("nobody*", schemaManager);
+ assertTrue(entries.isEmpty());
+ }
+
+ @Test
+ public void testSearchUsersByCn() throws Exception {
+ Map<String, String> config = new HashMap<>(ldapBackendConfig);
+ config.put("userIdentifierAttribute", "cn");
+ ldapProxyBackend.initialize(config);
+
+ List<Entry> entries = ldapProxyBackend.searchUsers("*", schemaManager);
+ Set<String> foundUids = new HashSet<>();
+ for (Entry entry : entries) {
+ foundUids.add(entry.get("uid").getString());
+ }
+ assertEquals(3, foundUids.size());
+ assertTrue(foundUids.contains("TestCn1"));
+ assertTrue(foundUids.contains("TestCn2"));
+ assertTrue(foundUids.contains("Guest"));
+ }
+
+ @Test
+ public void testSearchUsersPartialByCn() throws Exception {
+ Map<String, String> config = new HashMap<>(ldapBackendConfig);
+ config.put("userIdentifierAttribute", "cn");
+ ldapProxyBackend.initialize(config);
+
+ List<Entry> entries = ldapProxyBackend.searchUsers("TestCn*", schemaManager);
+ Set<String> foundUids = new HashSet<>();
+ for (Entry entry : entries) {
+ foundUids.add(entry.get("uid").getString());
+ }
+ assertEquals(2, foundUids.size());
+ assertTrue(foundUids.contains("TestCn1"));
+ assertTrue(foundUids.contains("TestCn2"));
+ }
+
+ @Test
+ public void testSearchUsersNoneFoundByCn() throws Exception {
+ Map<String, String> config = new HashMap<>(ldapBackendConfig);
+ config.put("userIdentifierAttribute", "cn");
+ ldapProxyBackend.initialize(config);
+
+ List<Entry> entries = ldapProxyBackend.searchUsers("nobody*", schemaManager);
+ assertTrue(entries.isEmpty());
+ }
+
+ @Test
+ public void testSearchUsersBySAMAccountName() throws Exception {
+ Map<String, String> config = new HashMap<>(ldapBackendConfig);
+ config.put("userIdentifierAttribute", "sAMAccountName");
+ ldapProxyBackend.initialize(config);
+
+ List<Entry> entries = ldapProxyBackend.searchUsers("*", schemaManager);
+ Set<String> foundUids = new HashSet<>();
+ for (Entry entry : entries) {
+ foundUids.add(entry.get("uid").getString());
+ }
+ assertEquals(2, foundUids.size());
+ assertTrue(foundUids.contains("TestSam1"));
+ assertTrue(foundUids.contains("TestSam2"));
+ }
+
+ @Test
+ public void testSearchUsersPartialBySAMAccountName() throws Exception {
+ Map<String, String> config = new HashMap<>(ldapBackendConfig);
+ config.put("userIdentifierAttribute", "sAMAccountName");
+ ldapProxyBackend.initialize(config);
+
+ List<Entry> entries = ldapProxyBackend.searchUsers("TestSam*", schemaManager);
+ Set<String> foundUids = new HashSet<>();
+ for (Entry entry : entries) {
+ foundUids.add(entry.get("uid").getString());
+ }
+ assertEquals(2, foundUids.size());
+ assertTrue(foundUids.contains("TestSam1"));
+ assertTrue(foundUids.contains("TestSam2"));
+ }
+
+ @Test
+ public void testSearchUsersNoneFoundBySAMAccountName() throws Exception {
+ Map<String, String> config = new HashMap<>(ldapBackendConfig);
+ config.put("userIdentifierAttribute", "sAMAccountName");
+ ldapProxyBackend.initialize(config);
+
+ List<Entry> entries = ldapProxyBackend.searchUsers("nobody*", schemaManager);
+ assertTrue(entries.isEmpty());
+ }
+}
\ No newline at end of file
diff --git a/gateway-server/src/test/resources/ldap-proxy-backend-test.ldif b/gateway-server/src/test/resources/ldap-proxy-backend-test.ldif
new file mode 100644
index 000000000..186342780
--- /dev/null
+++ b/gateway-server/src/test/resources/ldap-proxy-backend-test.ldif
@@ -0,0 +1,87 @@
+# 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.
+
+version: 1
+
+dn: dc=hadoop,dc=apache,dc=org
+objectclass: organization
+objectclass: dcObject
+o: Hadoop at Apache.org
+dc: hadoop
+description: Makers of Hadoop
+
+# entry for the people container
+dn: ou=people,dc=hadoop,dc=apache,dc=org
+objectclass:top
+objectclass:organizationalUnit
+ou: people
+
+# entry for group container
+dn: ou=groups,dc=hadoop,dc=apache,dc=org
+objectClass: top
+objectClass: organizationalUnit
+ou: groups
+
+# entry for the end user
+dn: uid=guest,ou=people,dc=hadoop,dc=apache,dc=org
+objectclass:top
+objectclass:person
+objectclass:organizationalPerson
+objectclass:inetOrgPerson
+cn: Guest
+sn: User
+uid: guest
+userPassword:guest-password
+
+dn: cn=group1,ou=groups,dc=hadoop,dc=apache,dc=org
+objectclass:top
+objectclass:groupOfNames
+cn: group1
+member: uid=ldaptest1,ou=people,dc=hadoop,dc=apache,dc=org
+
+dn: cn=group2,ou=groups,dc=hadoop,dc=apache,dc=org
+objectclass:top
+objectclass:groupOfNames
+cn: group2
+member: uid=ldaptest1,ou=people,dc=hadoop,dc=apache,dc=org
+
+dn: uid=ldaptest1,ou=people,dc=hadoop,dc=apache,dc=org
+objectclass:top
+objectclass:person
+objectclass:organizationalPerson
+objectclass:inetOrgPerson
+cn: TestCn1
+sn: Ldap
+uid: ldaptest1
+sAMAccountName: TestSam1
+userPassword: 12345
+mail: ldaptest1@example.com
+description: Test user ldaptest1
+
+dn: uid=ldaptest2,ou=people,dc=hadoop,dc=apache,dc=org
+objectclass:top
+objectclass:person
+objectclass:organizationalPerson
+objectclass:inetOrgPerson
+cn: TestCn2
+sn: Ldap
+uid: ldaptest2
+sAMAccountName: TestSam2
+userPassword: 12345
+mail: ldaptest2@example.com
+description: Test user ldaptest2
+memberOf: cn=groupMemberOf1,ou=groups,dc=hadoop,dc=apache,dc=org
+memberOf: cn=groupMemberOf2,ou=groups,dc=hadoop,dc=apache,dc=org