Skip to content

Commit

Permalink
LDAP Security : upgrade to 1.3.2, fix busy port issue
Browse files Browse the repository at this point in the history
  • Loading branch information
NielsCharlier committed Mar 11, 2015
1 parent 005e337 commit ede695e
Show file tree
Hide file tree
Showing 8 changed files with 281 additions and 126 deletions.
2 changes: 1 addition & 1 deletion src/pom.xml
Expand Up @@ -789,7 +789,7 @@
<dependency>
<groupId>org.springframework.ldap</groupId>
<artifactId>spring-ldap-test</artifactId>
<version>1.3.1.RELEASE</version>
<version>1.3.2.RELEASE</version>
</dependency>
<!--
<dependency>
Expand Down
@@ -0,0 +1,77 @@
/* (c) 2014 Open Source Geospatial Foundation - all rights reserved
* (c) 2001 - 2013 OpenPlans
* This code is licensed under the GPL 2.0 license, available at the root
* application directory.
*/
package org.geoserver.security.ldap;

import org.apache.directory.server.core.DefaultDirectoryService;
import org.apache.directory.server.core.DirectoryService;
import org.apache.directory.server.core.entry.ServerEntry;
import org.apache.directory.server.core.partition.impl.btree.jdbm.JdbmPartition;
import org.apache.directory.server.ldap.LdapServer;
import org.apache.directory.server.protocol.shared.transport.TcpTransport;
import org.apache.directory.shared.ldap.name.LdapDN;

import java.io.File;

/**
* Helper class for embedded Apache Directory Server.
*
* copied and modified from org.springframework.ldap.test.EmbeddedLdapServer
* to allow anonymous access (there was no alternative way)
*
* @author Mattias Hellborg Arthursson
* @author Niels Charlier
*/
public class EmbeddedLdapServer {
private final DirectoryService directoryService;
private final LdapServer ldapServer;

private EmbeddedLdapServer(DirectoryService directoryService,
LdapServer ldapServer) {
this.directoryService = directoryService;
this.ldapServer = ldapServer;
}

public static EmbeddedLdapServer newEmbeddedServer(String defaultPartitionName, String defaultPartitionSuffix, int port,
boolean allowAnonymousAccess)
throws Exception{

DefaultDirectoryService directoryService = new DefaultDirectoryService();
directoryService.setShutdownHookEnabled(true);
directoryService.setAllowAnonymousAccess(allowAnonymousAccess);
directoryService.setWorkingDirectory(new File(System.getProperty("java.io.tmpdir") + "/apacheds-test"));
directoryService.getChangeLog().setEnabled( false );

JdbmPartition partition = new JdbmPartition();
partition.setId(defaultPartitionName);
partition.setSuffix(defaultPartitionSuffix);
directoryService.addPartition(partition);

directoryService.startup();

// Inject the apache root entry if it does not already exist
if ( !directoryService.getAdminSession().exists( partition.getSuffixDn() ) )
{
ServerEntry entry = directoryService.newEntry(new LdapDN(defaultPartitionSuffix));
entry.add("objectClass", "top", "domain", "extensibleObject");
entry.add("dc", defaultPartitionName);
directoryService.getAdminSession().add( entry );
}

LdapServer ldapServer = new LdapServer();
ldapServer.setDirectoryService(directoryService);

TcpTransport ldapTransport = new TcpTransport(port);
ldapServer.setTransports( ldapTransport );
ldapServer.start();

return new EmbeddedLdapServer(directoryService, ldapServer);
}

public void shutdown() throws Exception {
ldapServer.stop();
directoryService.shutdown();
}
}
Expand Up @@ -18,7 +18,7 @@
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;

import edu.emory.mathcs.backport.java.util.Collections;
import java.util.Collections;

/**
*
Expand Down
Expand Up @@ -12,7 +12,6 @@
import org.geoserver.security.GeoServerSecurityManager;
import org.junit.After;
import org.junit.Before;
import org.springframework.ldap.test.LdapTestUtils;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
Expand Down Expand Up @@ -64,9 +63,8 @@ public void setUp() throws Exception {
public void tearDown() throws Exception {
tempFolder.delete();

LdapTestUtils
.destroyApacheDirectoryServer(LdapTestUtils.DEFAULT_PRINCIPAL,
LdapTestUtils.DEFAULT_PASSWORD);
LDAPTestUtils.shutdownEmbeddedServer();

if(SecurityContextHolder.getContext() != null) {
SecurityContextHolder.getContext().setAuthentication(null);
}
Expand Down

0 comments on commit ede695e

Please sign in to comment.