Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/ldap search query #229

Merged
merged 3 commits into from May 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -17,6 +17,7 @@
package org.elasticsearch.plugin.readonlyrest.acl.definitions.ldaps.unboundid;

import com.google.common.collect.Lists;
import com.unboundid.ldap.sdk.Filter;
import com.unboundid.ldap.sdk.LDAPException;
import com.unboundid.ldap.sdk.SearchRequest;
import com.unboundid.ldap.sdk.SearchResultEntry;
Expand Down Expand Up @@ -59,7 +60,7 @@ public CompletableFuture<Optional<LdapUser>> userById(String userId) {
new UnboundidSearchResultListener(searchUser),
userSearchFilterConfig.getSearchUserBaseDN(),
SearchScope.SUB,
String.format("(%s=%s)", userSearchFilterConfig.getUidAttribute(), userId)
String.format("(%s=%s)", userSearchFilterConfig.getUidAttribute(), Filter.encodeValue(userId))
)),
requestTimeout.toMillis()
);
Expand Down
Expand Up @@ -18,6 +18,7 @@

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.unboundid.ldap.sdk.Filter;
import com.unboundid.ldap.sdk.LDAPException;
import com.unboundid.ldap.sdk.SearchRequest;
import com.unboundid.ldap.sdk.SearchResultEntry;
Expand Down Expand Up @@ -72,7 +73,10 @@ public CompletableFuture<Set<LdapGroup>> userGroups(LdapUser user) {
new UnboundidSearchResultListener(searchGroups),
userGroupsSearchFilterConfig.getSearchGroupBaseDN(),
SearchScope.SUB,
String.format("(&(cn=*)(%s=%s))", userGroupsSearchFilterConfig.getUniqueMemberAttribute(), user.getDN())
String.format(
"(&(cn=*)(%s=%s))", userGroupsSearchFilterConfig.getUniqueMemberAttribute(),
Filter.encodeValue(user.getDN())
)
)),
requestTimeout.toMillis()
);
Expand Down
Expand Up @@ -17,8 +17,10 @@

package org.elasticsearch.plugin.readonlyrest.acl.definitions.ldaps;

import com.google.common.collect.Sets;
import org.elasticsearch.plugin.readonlyrest.acl.definitions.ldaps.unboundid.ConnectionConfig;
import org.elasticsearch.plugin.readonlyrest.acl.definitions.ldaps.unboundid.UnboundidAuthenticationLdapClient;
import org.elasticsearch.plugin.readonlyrest.acl.definitions.ldaps.unboundid.UnboundidGroupsProviderLdapClient;
import org.elasticsearch.plugin.readonlyrest.acl.definitions.ldaps.unboundid.UserGroupsSearchFilterConfig;
import org.elasticsearch.plugin.readonlyrest.acl.definitions.ldaps.unboundid.UserSearchFilterConfig;
import org.elasticsearch.plugin.readonlyrest.utils.containers.LdapContainer;
import org.elasticsearch.plugin.readonlyrest.utils.esdependent.MockedESContext;
Expand All @@ -28,14 +30,15 @@

import java.time.Duration;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;

public class UnboundidAuthenticationLdapClientTests {

@ClassRule
public static LdapContainer ldapContainer = LdapContainer.create("/test_example.ldif");

private UnboundidAuthenticationLdapClient client = new UnboundidAuthenticationLdapClient(
private UnboundidGroupsProviderLdapClient client = new UnboundidGroupsProviderLdapClient(
new ConnectionConfig.Builder(ldapContainer.getLdapHost())
.setPort(ldapContainer.getLdapPort())
.setPoolSize(10)
Expand All @@ -45,6 +48,7 @@ public class UnboundidAuthenticationLdapClientTests {
.setTrustAllCerts(false)
.build(),
new UserSearchFilterConfig.Builder("ou=People,dc=example,dc=com").build(),
new UserGroupsSearchFilterConfig.Builder("ou=Groups,dc=example,dc=com").build(),
Optional.of(ldapContainer.getSearchingUserConfig()),
MockedESContext.INSTANCE
);
Expand All @@ -55,7 +59,7 @@ public void testAuthenticationSuccess() throws Exception {
new LdapCredentials("cartman", "user2")
);
Optional<LdapUser> user = userF.get();
Assert.assertEquals(user.isPresent(), true);
Assert.assertEquals(true, user.isPresent());
}

@Test
Expand All @@ -64,7 +68,7 @@ public void testAuthenticationErrorDueToInvalidPassword() throws Exception {
new LdapCredentials("cartman", "wrongpassword")
);
Optional<LdapUser> user = userF.get();
Assert.assertEquals(user.isPresent(), false);
Assert.assertEquals(false, user.isPresent());
}

@Test
Expand All @@ -73,6 +77,16 @@ public void testAuthenticationErrorDueToUnknownUser() throws Exception {
new LdapCredentials("nonexistent", "whatever")
);
Optional<LdapUser> user = userF.get();
Assert.assertEquals(user.isPresent(), false);
Assert.assertEquals(false, user.isPresent());
}

@Test
public void testAuthenticationSuccessAndCNIsProperlyEscaped() throws Exception {
CompletableFuture<Set<LdapGroup>> groupsF = client.authenticate(new LdapCredentials("viktor", "user2"))
.thenCompose(ldapUser ->
ldapUser.map(user -> client.userGroups(user))
.orElse(CompletableFuture.completedFuture(Sets.newHashSet()))
);
Assert.assertEquals(false, groupsF.get().isEmpty());
}
}
11 changes: 11 additions & 0 deletions src/test/resources/test_example.ldif
Expand Up @@ -35,6 +35,16 @@ sn: Bong
uid: bong
userPassword:: e1NIQX1zOXFuZTB3RXFWVWJoNEhRTVpIK0NZOHlYbWM9

dn: cn=Viktor Navorski (Tom Hanks),ou=People,dc=example,dc=com
objectClass: top
objectClass: person
objectClass: organizationalPerson
objectClass: inetOrgPerson
cn: Viktor Navorski (Tom Hanks)
sn: Navorski
uid: viktor
userPassword:: e1NNRDV9czdnM0NVekVCMGQxMm5CM0N3VGFrQmp3K0VGMTE3cFg=

dn: ou=Groups,dc=example,dc=com
objectClass: top
objectClass: organizationalUnit
Expand All @@ -60,3 +70,4 @@ cn: group3
uniqueMember: cn=Chanandler Bong,ou=People,dc=example,dc=com
uniqueMember: cn=Eric Cartman,ou=People,dc=example,dc=com
uniqueMember: cn=Morgan Freeman,ou=People,dc=example,dc=com
uniqueMember: cn=Viktor Navorski (Tom Hanks),ou=People,dc=example,dc=com