[java-identity-provider] branch main updated: Use new shared InMemoryDirectory.

Daniel Fisher dfisher at vt.edu
Fri Nov 11 01:21:58 UTC 2022


This is an automated email from the git hooks/post-receive script.

dfisher pushed a commit to branch main
in repository java-identity-provider.

View the commit online:
http://git.shibboleth.net/view/?p=java-identity-provider.git;a=commit;h=c2081f68ff0f075c70977b026e82024b4f85eed7

The following commit(s) were added to refs/heads/main by this push:
     new c2081f68f Use new shared InMemoryDirectory.
c2081f68f is described below

commit c2081f68ff0f075c70977b026e82024b4f85eed7
Author: Daniel Fisher <dfisher at vt.edu>
AuthorDate: Thu Nov 10 20:18:39 2022 -0500

    Use new shared InMemoryDirectory.
    
    Add dependency on shib-testing artifact where necessary.
---
 idp-authn-impl/pom.xml                             |   6 --
 .../authn/impl/JAASCredentialValidatorTest.java    |  30 +++---
 .../authn/impl/LDAPCredentialValidatorTest.java    |  36 +++----
 .../idp/authn/impl/ValidateCredentialsTest.java    |  36 +++----
 idp-conf/pom.xml                                   |  12 +--
 .../net/shibboleth/idp/test/InMemoryDirectory.java | 108 ---------------------
 .../idp/test/flows/AbstractFlowTest.java           |  21 ++--
 7 files changed, 62 insertions(+), 187 deletions(-)

diff --git a/idp-authn-impl/pom.xml b/idp-authn-impl/pom.xml
index 4b68ffed5..6d4a88287 100644
--- a/idp-authn-impl/pom.xml
+++ b/idp-authn-impl/pom.xml
@@ -270,12 +270,6 @@
             <scope>test</scope>
         </dependency>
 
-        <dependency>
-            <groupId>com.unboundid</groupId>
-            <artifactId>unboundid-ldapsdk</artifactId>
-            <scope>test</scope>
-        </dependency>
-
         <dependency>
             <groupId>org.mockito</groupId>
             <artifactId>mockito-core</artifactId>
diff --git a/idp-authn-impl/src/test/java/net/shibboleth/idp/authn/impl/JAASCredentialValidatorTest.java b/idp-authn-impl/src/test/java/net/shibboleth/idp/authn/impl/JAASCredentialValidatorTest.java
index eb7c9bebe..fc3383114 100644
--- a/idp-authn-impl/src/test/java/net/shibboleth/idp/authn/impl/JAASCredentialValidatorTest.java
+++ b/idp-authn-impl/src/test/java/net/shibboleth/idp/authn/impl/JAASCredentialValidatorTest.java
@@ -40,11 +40,6 @@ import org.testng.annotations.BeforeClass;
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
-import com.unboundid.ldap.listener.InMemoryDirectoryServer;
-import com.unboundid.ldap.listener.InMemoryDirectoryServerConfig;
-import com.unboundid.ldap.listener.InMemoryListenerConfig;
-import com.unboundid.ldap.sdk.LDAPException;
-
 import jakarta.servlet.http.HttpServletRequest;
 import net.shibboleth.idp.authn.AuthnEventIds;
 import net.shibboleth.idp.authn.context.AuthenticationContext;
@@ -59,6 +54,9 @@ import net.shibboleth.idp.profile.testing.ActionTestingSupport;
 import net.shibboleth.shared.collection.Pair;
 import net.shibboleth.shared.component.ComponentInitializationException;
 import net.shibboleth.shared.net.URISupport;
+import net.shibboleth.shared.testing.InMemoryDirectory;
+
+import static org.testng.Assert.assertEquals;
 
 /** Unit test for JAAS validation. */
 public class JAASCredentialValidatorTest extends BaseAuthenticationContextTest {
@@ -71,28 +69,26 @@ public class JAASCredentialValidatorTest extends BaseAuthenticationContextTest {
     
     private ValidateCredentials action;
 
-    private InMemoryDirectoryServer directoryServer;
+    private InMemoryDirectory directoryServer;
 
     /**
      * Creates an UnboundID in-memory directory server. Leverages LDIF found in test resources.
-     * 
-     * @throws LDAPException if the in-memory directory server cannot be created
      */
-    @BeforeClass public void setupDirectoryServer() throws LDAPException {
-
-        final InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig("dc=shibboleth,dc=net");
-        config.setListenerConfigs(InMemoryListenerConfig.createLDAPConfig("default", 10389));
-        config.addAdditionalBindCredentials("cn=Directory Manager", "password");
-        directoryServer = new InMemoryDirectoryServer(config);
-        directoryServer.importFromLDIF(true, DATA_PATH + "loginLDAPTest.ldif");
-        directoryServer.startListening();
+    @BeforeClass public void setupDirectoryServer() {
+        directoryServer =
+            new InMemoryDirectory(
+                new String[] {"dc=shibboleth,dc=net"},
+                new ClassPathResource(DATA_CLASSPATH + "loginLDAPTest.ldif"),
+                10389);
+        directoryServer.start();
     }
 
     /**
      * Shutdown the in-memory directory server.
      */
     @AfterClass public void teardownDirectoryServer() {
-        directoryServer.shutDown(true);
+        assertEquals(directoryServer.openConnectionCount(), 0);
+        directoryServer.stop(true);
     }
 
     @BeforeMethod public void setUp() throws ComponentInitializationException {
diff --git a/idp-authn-impl/src/test/java/net/shibboleth/idp/authn/impl/LDAPCredentialValidatorTest.java b/idp-authn-impl/src/test/java/net/shibboleth/idp/authn/impl/LDAPCredentialValidatorTest.java
index 332bb5046..96a07ff66 100644
--- a/idp-authn-impl/src/test/java/net/shibboleth/idp/authn/impl/LDAPCredentialValidatorTest.java
+++ b/idp-authn-impl/src/test/java/net/shibboleth/idp/authn/impl/LDAPCredentialValidatorTest.java
@@ -28,10 +28,7 @@ import java.util.function.Supplier;
 import java.util.regex.Pattern;
 
 import org.ldaptive.DefaultConnectionFactory;
-import org.ldaptive.LdapException;
 import org.ldaptive.auth.AccountState;
-import org.ldaptive.auth.AuthenticationResponse;
-import org.ldaptive.auth.AuthenticationResponseHandler;
 import org.ldaptive.auth.AuthenticationResultCode;
 import org.ldaptive.auth.Authenticator;
 import org.ldaptive.auth.SimpleBindAuthenticationHandler;
@@ -40,6 +37,7 @@ import org.ldaptive.auth.ext.PasswordPolicyAccountState;
 import org.ldaptive.control.PasswordPolicyControl;
 import org.ldaptive.jaas.LdapPrincipal;
 import org.opensaml.profile.context.ProfileRequestContext;
+import org.springframework.core.io.ClassPathResource;
 import org.springframework.mock.web.MockHttpServletRequest;
 import org.springframework.webflow.execution.Event;
 import org.testng.Assert;
@@ -48,11 +46,6 @@ import org.testng.annotations.BeforeClass;
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
-import com.unboundid.ldap.listener.InMemoryDirectoryServer;
-import com.unboundid.ldap.listener.InMemoryDirectoryServerConfig;
-import com.unboundid.ldap.listener.InMemoryListenerConfig;
-import com.unboundid.ldap.sdk.LDAPException;
-
 import jakarta.servlet.http.HttpServletRequest;
 import net.shibboleth.idp.authn.AuthenticationResult;
 import net.shibboleth.idp.authn.AuthnEventIds;
@@ -66,18 +59,21 @@ import net.shibboleth.idp.authn.impl.testing.BaseAuthenticationContextTest;
 import net.shibboleth.idp.authn.principal.UsernamePrincipal;
 import net.shibboleth.idp.profile.testing.ActionTestingSupport;
 import net.shibboleth.shared.component.ComponentInitializationException;
+import net.shibboleth.shared.testing.InMemoryDirectory;
 import net.shibboleth.shared.testing.VelocityEngine;
 
+import static org.testng.Assert.assertEquals;
+
 /** Unit test for LDAP credential validation. */
 public class LDAPCredentialValidatorTest extends BaseAuthenticationContextTest {
 
-    private static final String DATA_PATH = "src/test/resources/net/shibboleth/idp/authn/impl/";
+    private static final String DATA_PATH = "/net/shibboleth/idp/authn/impl/";
 
     private LDAPCredentialValidator validator;
     
     private ValidateCredentials action;
 
-    private InMemoryDirectoryServer directoryServer;
+    private InMemoryDirectory directoryServer;
 
     private TemplateSearchDnResolver dnResolver;
 
@@ -87,17 +83,14 @@ public class LDAPCredentialValidatorTest extends BaseAuthenticationContextTest {
 
     /**
      * Creates an UnboundID in-memory directory server. Leverages LDIF found in test resources.
-     * 
-     * @throws LDAPException if the in-memory directory server cannot be created
      */
-    @BeforeClass public void setupDirectoryServer() throws LDAPException {
-
-        InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig("dc=shibboleth,dc=net");
-        config.setListenerConfigs(InMemoryListenerConfig.createLDAPConfig("default", 10389));
-        config.addAdditionalBindCredentials("cn=Directory Manager", "password");
-        directoryServer = new InMemoryDirectoryServer(config);
-        directoryServer.importFromLDIF(true, DATA_PATH + "loginLDAPTest.ldif");
-        directoryServer.startListening();
+    @BeforeClass public void setupDirectoryServer() {
+        directoryServer =
+            new InMemoryDirectory(
+                new String[] {"dc=shibboleth,dc=net"},
+                new ClassPathResource(DATA_PATH + "loginLDAPTest.ldif"),
+                10389);
+        directoryServer.start();
     }
 
     /**
@@ -118,7 +111,8 @@ public class LDAPCredentialValidatorTest extends BaseAuthenticationContextTest {
      * Shutdown the in-memory directory server.
      */
     @AfterClass public void teardownDirectoryServer() {
-        directoryServer.shutDown(true);
+        assertEquals(directoryServer.openConnectionCount(), 0);
+        directoryServer.stop(true);
     }
 
     @BeforeMethod public void setUp() throws ComponentInitializationException {
diff --git a/idp-authn-impl/src/test/java/net/shibboleth/idp/authn/impl/ValidateCredentialsTest.java b/idp-authn-impl/src/test/java/net/shibboleth/idp/authn/impl/ValidateCredentialsTest.java
index 7a83949e9..db67d3469 100644
--- a/idp-authn-impl/src/test/java/net/shibboleth/idp/authn/impl/ValidateCredentialsTest.java
+++ b/idp-authn-impl/src/test/java/net/shibboleth/idp/authn/impl/ValidateCredentialsTest.java
@@ -30,7 +30,7 @@ import org.ldaptive.auth.AuthenticationResultCode;
 import org.ldaptive.auth.Authenticator;
 import org.ldaptive.auth.SimpleBindAuthenticationHandler;
 import org.ldaptive.jaas.LdapPrincipal;
-import org.springframework.core.io.FileSystemResource;
+import org.springframework.core.io.ClassPathResource;
 import org.springframework.mock.web.MockHttpServletRequest;
 import org.springframework.webflow.execution.Event;
 import org.testng.Assert;
@@ -39,11 +39,6 @@ import org.testng.annotations.BeforeClass;
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
-import com.unboundid.ldap.listener.InMemoryDirectoryServer;
-import com.unboundid.ldap.listener.InMemoryDirectoryServerConfig;
-import com.unboundid.ldap.listener.InMemoryListenerConfig;
-import com.unboundid.ldap.sdk.LDAPException;
-
 import jakarta.servlet.http.HttpServletRequest;
 import net.shibboleth.idp.authn.AuthenticationResult;
 import net.shibboleth.idp.authn.AuthnEventIds;
@@ -56,16 +51,19 @@ import net.shibboleth.idp.authn.impl.testing.BaseAuthenticationContextTest;
 import net.shibboleth.idp.authn.principal.UsernamePrincipal;
 import net.shibboleth.idp.profile.testing.ActionTestingSupport;
 import net.shibboleth.shared.component.ComponentInitializationException;
+import net.shibboleth.shared.testing.InMemoryDirectory;
 import net.shibboleth.shared.testing.VelocityEngine;
 
+import static org.testng.Assert.assertEquals;
+
 /** Unit test for multiple credential validation. */
 public class ValidateCredentialsTest extends BaseAuthenticationContextTest {
 
-    private static final String DATA_PATH = "src/test/resources/net/shibboleth/idp/authn/impl/";
+    private static final String DATA_PATH = "/net/shibboleth/idp/authn/impl/";
 
     private ValidateCredentials action;
 
-    private InMemoryDirectoryServer directoryServer;
+    private InMemoryDirectory directoryServer;
 
     private TemplateSearchDnResolver dnResolver;
 
@@ -75,17 +73,14 @@ public class ValidateCredentialsTest extends BaseAuthenticationContextTest {
 
     /**
      * Creates an UnboundID in-memory directory server. Leverages LDIF found in test resources.
-     * 
-     * @throws LDAPException if the in-memory directory server cannot be created
      */
-    @BeforeClass public void setupDirectoryServer() throws LDAPException {
-
-        InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig("dc=shibboleth,dc=net");
-        config.setListenerConfigs(InMemoryListenerConfig.createLDAPConfig("default", 10389));
-        config.addAdditionalBindCredentials("cn=Directory Manager", "password");
-        directoryServer = new InMemoryDirectoryServer(config);
-        directoryServer.importFromLDIF(true, DATA_PATH + "loginLDAPTest.ldif");
-        directoryServer.startListening();
+    @BeforeClass public void setupDirectoryServer() {
+        directoryServer =
+            new InMemoryDirectory(
+                new String[] {"dc=shibboleth,dc=net"},
+                new ClassPathResource(DATA_PATH + "loginLDAPTest.ldif"),
+                10389);
+        directoryServer.start();
     }
 
     /**
@@ -106,7 +101,8 @@ public class ValidateCredentialsTest extends BaseAuthenticationContextTest {
      * Shutdown the in-memory directory server.
      */
     @AfterClass public void teardownDirectoryServer() {
-        directoryServer.shutDown(true);
+        assertEquals(directoryServer.openConnectionCount(), 0);
+        directoryServer.stop(true);
     }
 
     @BeforeMethod public void setUp() throws ComponentInitializationException {
@@ -119,7 +115,7 @@ public class ValidateCredentialsTest extends BaseAuthenticationContextTest {
 
         final HTPasswdCredentialValidator htpasswd = new HTPasswdCredentialValidator();
         htpasswd.setId("htpasswd");
-        htpasswd.setResource(new FileSystemResource(DATA_PATH + "/htpasswd.txt"));
+        htpasswd.setResource(new ClassPathResource(DATA_PATH + "htpasswd.txt"));
         htpasswd.initialize();
         
         action = new ValidateCredentials();
diff --git a/idp-conf/pom.xml b/idp-conf/pom.xml
index 81b5b4af6..5320ed3a0 100644
--- a/idp-conf/pom.xml
+++ b/idp-conf/pom.xml
@@ -227,7 +227,12 @@
             <artifactId>shib-support</artifactId>
             <scope>test</scope>
         </dependency>
-        
+        <dependency>
+            <groupId>${shib-shared.groupId}</groupId>
+            <artifactId>shib-testing</artifactId>
+            <scope>test</scope>
+        </dependency>
+
         <dependency>
             <groupId>com.google.guava</groupId>
             <artifactId>guava</artifactId>
@@ -267,11 +272,6 @@
             <artifactId>ldaptive</artifactId>
             <scope>test</scope>
         </dependency>
-        <dependency>
-            <groupId>com.unboundid</groupId>
-            <artifactId>unboundid-ldapsdk</artifactId>
-            <scope>test</scope>
-        </dependency>
 
         <dependency>
             <groupId>org.apache.httpcomponents</groupId>
diff --git a/idp-conf/src/test/java/net/shibboleth/idp/test/InMemoryDirectory.java b/idp-conf/src/test/java/net/shibboleth/idp/test/InMemoryDirectory.java
deleted file mode 100644
index 80f2b0e55..000000000
--- a/idp-conf/src/test/java/net/shibboleth/idp/test/InMemoryDirectory.java
+++ /dev/null
@@ -1,108 +0,0 @@
-/*
- * Licensed to the University Corporation for Advanced Internet Development,
- * Inc. (UCAID) under one or more contributor license agreements.  See the
- * NOTICE file distributed with this work for additional information regarding
- * copyright ownership. The UCAID 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 net.shibboleth.idp.test;
-
-import java.io.IOException;
-import java.net.InetAddress;
-import java.security.GeneralSecurityException;
-import java.security.KeyStore;
-
-import javax.annotation.Nonnull;
-
-import com.unboundid.util.ssl.SSLUtil;
-import org.ldaptive.ssl.CredentialConfigFactory;
-import org.ldaptive.ssl.SSLContextInitializer;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.core.io.Resource;
-
-import com.unboundid.ldap.listener.InMemoryDirectoryServer;
-import com.unboundid.ldap.listener.InMemoryDirectoryServerConfig;
-import com.unboundid.ldap.listener.InMemoryListenerConfig;
-import com.unboundid.ldap.sdk.LDAPException;
-import com.unboundid.ldif.LDIFReader;
-
-import net.shibboleth.shared.annotation.ParameterName;
-import net.shibboleth.shared.annotation.constraint.Positive;
-import net.shibboleth.shared.logic.Constraint;
-
-/**
- * Manages an instance of the in-memory directory server.
- */
-public class InMemoryDirectory {
-
-    /** Class logger. */
-    @Nonnull private final Logger log = LoggerFactory.getLogger(InMemoryDirectory.class);
-
-    /** Directory server. */
-    @Nonnull private final InMemoryDirectoryServer directoryServer;
-
-    /**
-     * Constructor with STARTTLS support.
-     * 
-     * @param ldif the LDIF resource to be imported
-     * @param port port to listen on
-     * @param keystore to use for startTLS
-     * 
-     * @throws LDAPException if the in-memory directory server cannot be created
-     * @throws IOException if the LDIF resource cannot be imported
-     */
-    public InMemoryDirectory(@ParameterName(name="ldif") @Nonnull final Resource ldif,
-            @ParameterName(name="port") @Positive final int port,
-            @ParameterName(name="keystore") @Nonnull final Resource keystore) throws LDAPException,
-            IOException {
-        Constraint.isNotNull(ldif, "LDIF resource cannot be null");
-        final InMemoryDirectoryServerConfig config =
-                new InMemoryDirectoryServerConfig("dc=example,dc=org", "ou=system");
-        try {
-            final KeyStore ks = KeyStore.getInstance("JKS");
-            final String ksPass = "changeit";
-            ks.load(keystore.getInputStream(), ksPass.toCharArray());
-            final SSLContextInitializer sslInit =
-                CredentialConfigFactory.createKeyStoreCredentialConfig(ks, ksPass).createSSLContextInitializer();
-            final SSLUtil sslUtil = new SSLUtil(sslInit.getKeyManagers(), sslInit.getTrustManagers());
-            config.setListenerConfigs(
-                InMemoryListenerConfig.createLDAPConfig(
-                    "default", InetAddress.getByName("localhost"), port, sslUtil.createSSLSocketFactory()));
-        } catch (final GeneralSecurityException e) {
-            throw new IOException("Error reading keystore", e);
-        }
-        config.addAdditionalBindCredentials("cn=Directory Manager", "password");
-        directoryServer = new InMemoryDirectoryServer(config);
-        directoryServer.importFromLDIF(true, new LDIFReader(ldif.getInputStream()));
-    }
-
-    /**
-     * Starts the directory server.
-     * 
-     * @throws LDAPException if the in-memory directory server cannot be started
-     */
-    public void start() throws LDAPException {
-        directoryServer.startListening();
-        log.info("In-memory directory server started");
-    }
-
-    /**
-     * Stops the directory server without closing existing connections. Resources should be configured so that LDAP
-     * connections are closed when the spring application context shuts down.
-     */
-    public void stop() {
-        directoryServer.shutDown(false);
-        log.info("In-memory directory server stopped");
-    }
-}
diff --git a/idp-conf/src/test/java/net/shibboleth/idp/test/flows/AbstractFlowTest.java b/idp-conf/src/test/java/net/shibboleth/idp/test/flows/AbstractFlowTest.java
index f47985caa..859e8777b 100644
--- a/idp-conf/src/test/java/net/shibboleth/idp/test/flows/AbstractFlowTest.java
+++ b/idp-conf/src/test/java/net/shibboleth/idp/test/flows/AbstractFlowTest.java
@@ -17,7 +17,7 @@
 
 package net.shibboleth.idp.test.flows;
 
-import java.io.IOException;
+import java.util.Optional;
 
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
@@ -68,12 +68,10 @@ import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.BeforeSuite;
 
 import com.google.common.net.HttpHeaders;
-import com.unboundid.ldap.sdk.LDAPException;
 
 import jakarta.servlet.http.HttpServletRequest;
 import jakarta.servlet.http.HttpServletResponse;
 import net.shibboleth.idp.spring.IdPPropertiesApplicationContextInitializer;
-import net.shibboleth.idp.test.InMemoryDirectory;
 import net.shibboleth.idp.test.PreferFileSystemApplicationContextInitializer;
 import net.shibboleth.idp.test.PreferFileSystemContextLoader;
 import net.shibboleth.idp.test.TestEnvironmentApplicationContextInitializer;
@@ -83,8 +81,11 @@ import net.shibboleth.shared.security.IdentifierGenerationStrategy;
 import net.shibboleth.shared.security.IdentifierGenerationStrategy.ProviderType;
 import net.shibboleth.shared.servlet.impl.HttpServletRequestResponseContext;
 import net.shibboleth.shared.spring.security.factory.X509CertificateFactoryBean;
+import net.shibboleth.shared.testing.InMemoryDirectory;
 import net.shibboleth.shared.xml.ParserPool;
 
+import static org.testng.Assert.assertEquals;
+
 /**
  * Abstract flow test.
  */
@@ -233,13 +234,15 @@ public abstract class AbstractFlowTest extends AbstractTestNGSpringContextTests
 
     /**
      * Creates an UnboundID in-memory directory server. Leverages LDIF found at {@value #LDIF_FILE}.
-     * 
-     * @throws LDAPException if the in-memory directory server cannot be created
-     * @throws IOException if the LDIF resource cannot be imported
      */
-    @BeforeSuite public static void setupDirectoryServer() throws LDAPException, IOException {
+    @BeforeSuite public static void setupDirectoryServer() {
         directoryServer =
-            new InMemoryDirectory(new ClassPathResource(LDIF_FILE), 10389, new ClassPathResource(KEYSTORE_FILE));
+            new InMemoryDirectory(
+                new String[] {"dc=example,dc=org", "ou=system"},
+                new ClassPathResource(LDIF_FILE),
+                10389,
+                new ClassPathResource(KEYSTORE_FILE),
+                Optional.empty());
         directoryServer.start();
     }
 
@@ -250,7 +253,7 @@ public abstract class AbstractFlowTest extends AbstractTestNGSpringContextTests
      */
     @AfterSuite(alwaysRun = true) public static void teardownDirectoryServer() {
         if (directoryServer != null) {
-            directoryServer.stop();
+            directoryServer.stop(true);
         }
     }
 

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list