-
Notifications
You must be signed in to change notification settings - Fork 488
Added config interceptor for in-memory server #1000
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
Merged
jzheaux
merged 4 commits into
spring-projects:main
from
etrandafir93:dev/938-embedded_test_servers
Apr 3, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,13 @@ | |
|
||
package org.springframework.ldap.test.unboundid; | ||
|
||
import java.util.List; | ||
import java.util.function.Consumer; | ||
|
||
import javax.naming.InvalidNameException; | ||
import javax.naming.ldap.LdapName; | ||
import javax.naming.ldap.Rdn; | ||
|
||
import com.unboundid.ldap.listener.InMemoryDirectoryServer; | ||
import com.unboundid.ldap.listener.InMemoryDirectoryServerConfig; | ||
import com.unboundid.ldap.listener.InMemoryListenerConfig; | ||
|
@@ -24,6 +31,7 @@ | |
import com.unboundid.ldap.sdk.LDAPException; | ||
|
||
import org.springframework.util.Assert; | ||
import org.springframework.util.CollectionUtils; | ||
|
||
/** | ||
* Helper class for embedded Unboundid ldap server. | ||
|
@@ -45,27 +53,30 @@ public EmbeddedLdapServer(InMemoryDirectoryServer directoryServer) { | |
this.directoryServer = directoryServer; | ||
} | ||
|
||
/** | ||
* Creates a new {@link Builder} with a given partition suffix. | ||
* | ||
* @since 3.3 | ||
*/ | ||
public static Builder withPartitionSuffix(String partitionSuffix) { | ||
return new Builder(partitionSuffix); | ||
} | ||
|
||
/** | ||
* Creates and starts new embedded LDAP server. | ||
* @deprecated Use the builder pattern exposed via | ||
* {@link #withPartitionSuffix(String)} instead. | ||
*/ | ||
@Deprecated(since = "3.3") | ||
public static EmbeddedLdapServer newEmbeddedServer(String defaultPartitionName, String defaultPartitionSuffix, | ||
jzheaux marked this conversation as resolved.
Show resolved
Hide resolved
|
||
int port) throws Exception { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry that I didn't catch this earlier. We can't remove checked exceptions from signatures as this can break some forms of compatibility. Please see https://wiki.eclipse.org/Evolving_Java-based_APIs_2 and look specifically for "Delete checked exceptions thrown". |
||
InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig(defaultPartitionSuffix); | ||
jzheaux marked this conversation as resolved.
Show resolved
Hide resolved
|
||
config.addAdditionalBindCredentials("uid=admin,ou=system", "secret"); | ||
|
||
config.setListenerConfigs(InMemoryListenerConfig.createLDAPConfig("LDAP", port)); | ||
|
||
config.setEnforceSingleStructuralObjectClass(false); | ||
config.setEnforceAttributeSyntaxCompliance(true); | ||
EmbeddedLdapServer server = EmbeddedLdapServer.withPartitionSuffix(defaultPartitionSuffix) | ||
.partitionName(defaultPartitionName) | ||
.port(port) | ||
.build(); | ||
|
||
Entry entry = new Entry(new DN(defaultPartitionSuffix)); | ||
entry.addAttribute("objectClass", "top", "domain", "extensibleObject"); | ||
entry.addAttribute("dc", defaultPartitionName); | ||
|
||
InMemoryDirectoryServer directoryServer = new InMemoryDirectoryServer(config); | ||
directoryServer.add(entry); | ||
directoryServer.startListening(); | ||
return new EmbeddedLdapServer(directoryServer); | ||
server.start(); | ||
return server; | ||
} | ||
|
||
/** | ||
|
@@ -102,4 +113,119 @@ public void shutdown() { | |
this.directoryServer.shutDown(true); | ||
} | ||
|
||
/** | ||
* Helper class for embedded Unboundid ldap server. | ||
* | ||
* @author Emanuel Trandafir | ||
* @since 3.3 | ||
*/ | ||
public static final class Builder { | ||
|
||
private final String partitionSuffix; | ||
|
||
private String partitionName; | ||
|
||
private int port = 0; | ||
|
||
private Consumer<InMemoryDirectoryServerConfig> configurationCustomizer = (__) -> { | ||
}; | ||
|
||
private Builder(String partitionSuffix) { | ||
this.partitionSuffix = partitionSuffix; | ||
this.partitionName = leftMostElement(partitionSuffix); | ||
} | ||
|
||
/** | ||
* Sets the port for the embedded LDAP server. | ||
* @param port the port for the embedded LDAP server. Defaults to 0 in which case | ||
* the server should automatically choose an available port. | ||
* @return this {@link Builder} instance. | ||
*/ | ||
public Builder port(int port) { | ||
jzheaux marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.port = port; | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets a customizer for the {@link InMemoryDirectoryServerConfig}. | ||
* @param configurationCustomizer a {@link Consumer} function that will be applied | ||
* to the {@link InMemoryDirectoryServerConfig} before creating the | ||
* {@link InMemoryDirectoryServer}. The default values, it a Consumer function | ||
* that does nothing: (config) -> {} | ||
* @return this {@link Builder} instance. | ||
*/ | ||
public Builder configurationCustomizer(Consumer<InMemoryDirectoryServerConfig> configurationCustomizer) { | ||
this.configurationCustomizer = configurationCustomizer; | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets the partition name for the embedded LDAP server. | ||
* @param partitionName the partition name for the embedded LDAP server. Defaults | ||
* to the left most element of the partition suffix. | ||
* @return this {@link Builder} instance. | ||
*/ | ||
public Builder partitionName(String partitionName) { | ||
this.partitionName = partitionName; | ||
return this; | ||
} | ||
|
||
/** | ||
* Builds and returns a {@link EmbeddedLdapServer}. | ||
* <p> | ||
* In order to start the server, you should call | ||
* {@link EmbeddedLdapServer#start()}. | ||
* @return a new {@link EmbeddedLdapServer}. | ||
*/ | ||
public EmbeddedLdapServer build() { | ||
try { | ||
InMemoryDirectoryServerConfig config = inMemoryDirectoryServerConfig(this.partitionSuffix, this.port); | ||
this.configurationCustomizer.accept(config); | ||
|
||
Entry entry = ldapEntry(this.partitionName, this.partitionSuffix); | ||
InMemoryDirectoryServer directoryServer = inMemoryDirectoryServer(config, entry); | ||
return new EmbeddedLdapServer(directoryServer); | ||
} | ||
catch (Exception ex) { | ||
throw new RuntimeException(ex); | ||
} | ||
} | ||
|
||
static String leftMostElement(String partitionSuffix) { | ||
try { | ||
List<Rdn> rdns = new LdapName(partitionSuffix).getRdns(); | ||
return CollectionUtils.lastElement(rdns).getValue().toString(); | ||
} | ||
catch (InvalidNameException ex) { | ||
throw new RuntimeException(ex); | ||
} | ||
} | ||
|
||
private static InMemoryDirectoryServerConfig inMemoryDirectoryServerConfig(String partitionSuffix, int port) | ||
throws LDAPException { | ||
InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig(partitionSuffix); | ||
config.addAdditionalBindCredentials("uid=admin,ou=system", "secret"); | ||
config.setListenerConfigs(InMemoryListenerConfig.createLDAPConfig("LDAP", port)); | ||
config.setEnforceSingleStructuralObjectClass(false); | ||
config.setEnforceAttributeSyntaxCompliance(true); | ||
return config; | ||
} | ||
|
||
private static Entry ldapEntry(String defaultPartitionName, String defaultPartitionSuffix) | ||
throws LDAPException { | ||
Entry entry = new Entry(new DN(defaultPartitionSuffix)); | ||
entry.addAttribute("objectClass", "top", "domain", "extensibleObject"); | ||
entry.addAttribute("dc", defaultPartitionName); | ||
return entry; | ||
} | ||
|
||
private static InMemoryDirectoryServer inMemoryDirectoryServer(InMemoryDirectoryServerConfig config, | ||
Entry entry) throws LDAPException { | ||
InMemoryDirectoryServer directoryServer = new InMemoryDirectoryServer(config); | ||
directoryServer.add(entry); | ||
return directoryServer; | ||
} | ||
|
||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.