From 1251490b749a4113812ef3137f3b77282b8e0f82 Mon Sep 17 00:00:00 2001 From: Chad Wilson Date: Fri, 27 Oct 2023 14:17:37 +0800 Subject: [PATCH] Partial migration to Apache DS 2 To make this work requires migration to the LDAP Client API v2 which is probably out of scope. --- build.gradle | 7 ++--- .../cd/go/apacheds/LdapIntegrationTest.java | 27 +++++++++++++++---- .../ldap/BaseIntegrationTest.java | 15 +++-------- .../LdapAuthenticatorIntegrationTest.java | 9 ++++++- .../ldap/LdapPluginIntegrationTest.java | 13 ++++++--- .../IsValidUserExecutorIntegrationTest.java | 13 ++++++--- .../SearchUserExecutorIntegrationTest.java | 13 ++++++--- .../framework/ldap/LdapIntegrationTest.java | 27 +++++++++++++++---- 8 files changed, 90 insertions(+), 34 deletions(-) diff --git a/build.gradle b/build.gradle index 9805324..22945f1 100644 --- a/build.gradle +++ b/build.gradle @@ -100,14 +100,15 @@ dependencies { testRuntimeOnly group: 'org.junit.jupiter', name: 'junit-jupiter-engine' testRuntimeOnly group: 'org.junit.platform', name: 'junit-platform-launcher' - integrationTestImplementation group: 'com.github.stefanbirkner', name: 'system-rules', version: '1.19.0' + integrationTestImplementation 'uk.org.webcompere:system-stubs-jupiter:2.1.3' integrationTestImplementation group: 'org.slf4j', name: 'slf4j-simple', version: '2.0.5' integrationTestImplementation project.deps.apacheDs integrationTestImplementation group: 'org.apache.directory.server', name: 'apacheds-core-annotations', version: project.versions.apacheDs - integrationTestImplementation group: 'org.apache.directory.server', name: 'apacheds-core-integ', version: '2.0.0-M24' + integrationTestImplementation group: 'org.apache.directory.server', name: 'apacheds-core-integ', version: project.versions.apacheDs integrationTestImplementation group: 'org.apache.directory.server', name: 'apacheds-core-jndi', version: project.versions.apacheDs integrationTestImplementation group: 'org.apache.directory.server', name: 'apacheds-core-constants', version: project.versions.apacheDs - integrationTestRuntimeOnly group: 'org.junit.vintage', name: 'junit-vintage-engine' + integrationTestRuntimeOnly group: 'org.junit.jupiter', name: 'junit-jupiter-engine' + integrationTestRuntimeOnly group: 'org.junit.platform', name: 'junit-platform-launcher' } test { diff --git a/src/integration/cd/go/apacheds/LdapIntegrationTest.java b/src/integration/cd/go/apacheds/LdapIntegrationTest.java index 94fa04a..91a68f1 100644 --- a/src/integration/cd/go/apacheds/LdapIntegrationTest.java +++ b/src/integration/cd/go/apacheds/LdapIntegrationTest.java @@ -24,9 +24,15 @@ import cd.go.authentication.ldap.model.LdapConfiguration; import cd.go.authentication.ldap.model.User; import org.apache.directory.ldap.client.template.exception.LdapRuntimeException; -import org.junit.Rule; -import org.junit.Test; -import org.junit.contrib.java.lang.system.ProvideSystemProperty; +import org.apache.directory.server.annotations.CreateLdapServer; +import org.apache.directory.server.annotations.CreateTransport; +import org.apache.directory.server.core.annotations.ApplyLdifFiles; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import uk.org.webcompere.systemstubs.jupiter.SystemStub; +import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension; +import uk.org.webcompere.systemstubs.properties.SystemProperties; import java.util.List; @@ -34,11 +40,22 @@ import static java.text.MessageFormat.format; import static org.assertj.core.api.Assertions.*; +@ApplyLdifFiles(value = "users.ldif", clazz = BaseIntegrationTest.class) +@CreateLdapServer(transports = { + @CreateTransport(protocol = "LDAP") +}) +@ExtendWith(SystemStubsExtension.class) public class LdapIntegrationTest extends BaseIntegrationTest { - @Rule - public final ProvideSystemProperty systemProperty = new ProvideSystemProperty(USE_JNDI_LDAP_CLIENT, "false"); + + @SystemStub + private SystemProperties systemProperties; private ApacheDsLdapClient ldap; + @BeforeEach + public void setUp() { + systemProperties.set(USE_JNDI_LDAP_CLIENT, "true"); + } + @Test public void authenticate_shouldAuthenticateUser() { LdapConfiguration ldapConfiguration = ldapConfiguration(new String[]{"ou=system"}); diff --git a/src/integration/cd/go/authentication/ldap/BaseIntegrationTest.java b/src/integration/cd/go/authentication/ldap/BaseIntegrationTest.java index 4260fe3..b1eedc7 100644 --- a/src/integration/cd/go/authentication/ldap/BaseIntegrationTest.java +++ b/src/integration/cd/go/authentication/ldap/BaseIntegrationTest.java @@ -18,21 +18,14 @@ import cd.go.authentication.ldap.model.LdapConfiguration; import com.google.gson.Gson; -import org.apache.directory.server.annotations.CreateLdapServer; -import org.apache.directory.server.annotations.CreateTransport; -import org.apache.directory.server.core.annotations.ApplyLdifFiles; import org.apache.directory.server.core.integ.AbstractLdapTestUnit; -import org.apache.directory.server.core.integ.FrameworkRunner; -import org.junit.runner.RunWith; +import org.apache.directory.server.core.integ.ApacheDSTestExtension; +import org.junit.jupiter.api.extension.ExtendWith; import java.util.HashMap; import java.util.Map; -@RunWith(FrameworkRunner.class) -@ApplyLdifFiles(value = "users.ldif", clazz = BaseIntegrationTest.class) -@CreateLdapServer(transports = { - @CreateTransport(protocol = "LDAP") -}) +@ExtendWith(ApacheDSTestExtension.class) public abstract class BaseIntegrationTest extends AbstractLdapTestUnit { protected LdapConfiguration ldapConfiguration(String[] searchBases) { Map configuration = configAsMap("uid=admin,ou=system", "secret", "(uid={0})", searchBases @@ -59,7 +52,7 @@ protected LdapConfiguration ldapConfiguration(String username, String password, private Map configAsMap(String managerDN, String password, String userLoginFilter, String[] searchBases) { Map configuration = new HashMap<>(); - configuration.put("Url", String.format("ldap://localhost:%s", ldapServer.getPort())); + configuration.put("Url", String.format("ldap://localhost:%s", classLdapServer.getPort())); configuration.put("SearchBases", String.join("\n", searchBases)); configuration.put("ManagerDN", managerDN); configuration.put("Password", password); diff --git a/src/integration/cd/go/authentication/ldap/LdapAuthenticatorIntegrationTest.java b/src/integration/cd/go/authentication/ldap/LdapAuthenticatorIntegrationTest.java index 8763883..0bf90cd 100644 --- a/src/integration/cd/go/authentication/ldap/LdapAuthenticatorIntegrationTest.java +++ b/src/integration/cd/go/authentication/ldap/LdapAuthenticatorIntegrationTest.java @@ -17,13 +17,20 @@ package cd.go.authentication.ldap; import cd.go.authentication.ldap.model.*; -import org.junit.Test; +import org.apache.directory.server.annotations.CreateLdapServer; +import org.apache.directory.server.annotations.CreateTransport; +import org.apache.directory.server.core.annotations.ApplyLdifFiles; +import org.junit.jupiter.api.Test; import java.util.Arrays; import java.util.Collections; import static org.assertj.core.api.Assertions.assertThat; +@ApplyLdifFiles(value = "users.ldif", clazz = BaseIntegrationTest.class) +@CreateLdapServer(transports = { + @CreateTransport(protocol = "LDAP") +}) public class LdapAuthenticatorIntegrationTest extends BaseIntegrationTest { @Test diff --git a/src/integration/cd/go/authentication/ldap/LdapPluginIntegrationTest.java b/src/integration/cd/go/authentication/ldap/LdapPluginIntegrationTest.java index 8ec7b96..6a52e7d 100644 --- a/src/integration/cd/go/authentication/ldap/LdapPluginIntegrationTest.java +++ b/src/integration/cd/go/authentication/ldap/LdapPluginIntegrationTest.java @@ -21,8 +21,11 @@ import com.thoughtworks.go.plugin.api.request.DefaultGoPluginApiRequest; import com.thoughtworks.go.plugin.api.response.GoPluginApiResponse; import org.apache.commons.codec.binary.Base64; -import org.junit.Before; -import org.junit.Test; +import org.apache.directory.server.annotations.CreateLdapServer; +import org.apache.directory.server.annotations.CreateTransport; +import org.apache.directory.server.core.annotations.ApplyLdifFiles; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import static cd.go.authentication.ldap.executor.RequestFromServer.*; import static cd.go.plugin.base.ResourceReader.readResource; @@ -32,10 +35,14 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.skyscreamer.jsonassert.JSONAssert.assertEquals; +@ApplyLdifFiles(value = "users.ldif", clazz = BaseIntegrationTest.class) +@CreateLdapServer(transports = { + @CreateTransport(protocol = "LDAP") +}) public class LdapPluginIntegrationTest extends BaseIntegrationTest { private LdapPlugin ldapPlugin; - @Before + @BeforeEach public void setUp() { ldapPlugin = new LdapPlugin(); ldapPlugin.initializeGoApplicationAccessor(null); diff --git a/src/integration/cd/go/authentication/ldap/executor/IsValidUserExecutorIntegrationTest.java b/src/integration/cd/go/authentication/ldap/executor/IsValidUserExecutorIntegrationTest.java index eb6055f..2d7accd 100644 --- a/src/integration/cd/go/authentication/ldap/executor/IsValidUserExecutorIntegrationTest.java +++ b/src/integration/cd/go/authentication/ldap/executor/IsValidUserExecutorIntegrationTest.java @@ -22,8 +22,11 @@ import cd.go.authentication.ldap.model.IsValidUserRequest; import cd.go.authentication.ldap.model.LdapConfiguration; import com.google.gson.Gson; -import org.junit.Before; -import org.junit.Test; +import org.apache.directory.server.annotations.CreateLdapServer; +import org.apache.directory.server.annotations.CreateTransport; +import org.apache.directory.server.core.annotations.ApplyLdifFiles; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import java.util.HashMap; import java.util.Map; @@ -31,10 +34,14 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.*; +@ApplyLdifFiles(value = "users.ldif", clazz = BaseIntegrationTest.class) +@CreateLdapServer(transports = { + @CreateTransport(protocol = "LDAP") +}) public class IsValidUserExecutorIntegrationTest extends BaseIntegrationTest { private LdapFactory ldapFactory; - @Before + @BeforeEach public void setUp() { ldapFactory = spy(new LdapFactory()); } diff --git a/src/integration/cd/go/authentication/ldap/executor/SearchUserExecutorIntegrationTest.java b/src/integration/cd/go/authentication/ldap/executor/SearchUserExecutorIntegrationTest.java index 4b7798a..0b4577e 100644 --- a/src/integration/cd/go/authentication/ldap/executor/SearchUserExecutorIntegrationTest.java +++ b/src/integration/cd/go/authentication/ldap/executor/SearchUserExecutorIntegrationTest.java @@ -25,8 +25,11 @@ import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import com.thoughtworks.go.plugin.api.response.GoPluginApiResponse; -import org.junit.Before; -import org.junit.Test; +import org.apache.directory.server.annotations.CreateLdapServer; +import org.apache.directory.server.annotations.CreateTransport; +import org.apache.directory.server.core.annotations.ApplyLdifFiles; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import java.util.*; @@ -34,11 +37,15 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.*; +@ApplyLdifFiles(value = "users.ldif", clazz = BaseIntegrationTest.class) +@CreateLdapServer(transports = { + @CreateTransport(protocol = "LDAP") +}) public class SearchUserExecutorIntegrationTest extends BaseIntegrationTest { private LdapFactory ldapFactory; private SearchUserExecutor searchUserExecutor; - @Before + @BeforeEach public void setUp() { ldapFactory = spy(new LdapFactory()); searchUserExecutor = new SearchUserExecutor(ldapFactory); diff --git a/src/integration/cd/go/framework/ldap/LdapIntegrationTest.java b/src/integration/cd/go/framework/ldap/LdapIntegrationTest.java index 3ae5b00..a1e52f9 100644 --- a/src/integration/cd/go/framework/ldap/LdapIntegrationTest.java +++ b/src/integration/cd/go/framework/ldap/LdapIntegrationTest.java @@ -22,9 +22,15 @@ import cd.go.authentication.ldap.mapper.UsernameResolver; import cd.go.authentication.ldap.model.LdapConfiguration; import cd.go.authentication.ldap.model.User; -import org.junit.Rule; -import org.junit.Test; -import org.junit.contrib.java.lang.system.ProvideSystemProperty; +import org.apache.directory.server.annotations.CreateLdapServer; +import org.apache.directory.server.annotations.CreateTransport; +import org.apache.directory.server.core.annotations.ApplyLdifFiles; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import uk.org.webcompere.systemstubs.jupiter.SystemStub; +import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension; +import uk.org.webcompere.systemstubs.properties.SystemProperties; import javax.naming.directory.DirContext; import java.util.List; @@ -35,11 +41,22 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.*; +@ApplyLdifFiles(value = "users.ldif", clazz = BaseIntegrationTest.class) +@CreateLdapServer(transports = { + @CreateTransport(protocol = "LDAP") +}) +@ExtendWith(SystemStubsExtension.class) public class LdapIntegrationTest extends BaseIntegrationTest { - @Rule - public final ProvideSystemProperty systemProperty = new ProvideSystemProperty(USE_JNDI_LDAP_CLIENT, "true"); + + @SystemStub + private SystemProperties systemProperties; private JNDILdapClient jndiLdapClient; + @BeforeEach + public void setUp() { + systemProperties.set(USE_JNDI_LDAP_CLIENT, "true"); + } + @Test public void authenticate_shouldAuthenticateUser() { LdapConfiguration ldapConfiguration = ldapConfiguration(new String[]{"ou=system"});