Question

I have written some unit tests for LDAP authentication using spring security and the embedded ApacheDS server. The server is started up automagically via configuration of the element in the spring application context. I specify an LDIF file for the tests. All is wonderful. The test ran on our Jenkins instance for a week until one day when the test could not shutdown properly and delete the apacheds-spring-security directory. Subsequent test runs will crash due to the presence of this directory.

This issue was previously described in

ApacheDS Embedded with Spring

and the answer was to provide a command line argument specifying a working directory location. If I could do this, I could specify the target directory of my maven build, and maven would take care of the problem for me when it does a clean prior to build and test.

All good. Problem is, how to specify this working directory? -DapacheDSWorkDir="foo" is ignored when I run a maven build, and attempts to set this value programmatically also do not work. Anybody got any ideas? As you can see, the configuration does not include any mention of the underlying LDAP provider.

    <ldap-server id="ldapServer" 
        url="ldap://localhost:33389/dc=aws,dc=com" 
        manager-dn="uid=admin,ou=system" 
        manager-password="secret"/>

<ldap-server ldif="classpath:/ldap/test-server.ldif"
    root="dc=aws,dc=com"/>

<bean:bean id="federatedLdapUserContextSource" class="org.springframework.security.ldap.DefaultSpringSecurityContextSource">
    <bean:constructor-arg value="ldap://localhost:33389/dc=aws,dc=com"/>        
</bean:bean>

Any help would be much appreciated.

Was it helpful?

Solution

Try this (mind the semi-pseudo-code):

package spring;

import org.springframework.security.ldap.server.ApacheDSContainer;

public final class EmbeddedApacheDsConfigurer implements BeanPostProcessor {

    private String workingDirectory;

    public void setWorkingDirectory(String workingDirectory) {
        this.workingDirectory = workingDirectory;
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof ApacheDSContainer) {
             ((ApacheDSContainer)bean).setWorkingDirectory(new File(workingDirectory));
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
         return bean;
    }
}

Then in your test bean config:

<bean class="spring.EmbeddedApacheDsConfigurer" p:workingDirectory="target/apacheds" />

This assumes Maven's working dir gets set to the root of the project upon running tests.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top