-
Notifications
You must be signed in to change notification settings - Fork 41k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44389 from eddumelendez
* pr/44389: Add ServiceConnection support for LLdapContainer (Testcontainers) Add ServiceConnection support for lldap/lldap (Compose) Closes gh-44389
- Loading branch information
Showing
12 changed files
with
292 additions
and
1 deletion.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
...e/service/connection/ldap/LLdapDockerComposeConnectionDetailsFactoryIntegrationTests.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright 2012-2025 the original author or authors. | ||
* | ||
* Licensed 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 | ||
* | ||
* https://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 org.springframework.boot.docker.compose.service.connection.ldap; | ||
|
||
import org.springframework.boot.autoconfigure.ldap.LdapConnectionDetails; | ||
import org.springframework.boot.docker.compose.service.connection.test.DockerComposeTest; | ||
import org.springframework.boot.testsupport.container.TestImage; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Integration tests for {@link LLdapDockerComposeConnectionDetailsFactory}. | ||
* | ||
* @author Eddú Meléndez | ||
*/ | ||
class LLdapDockerComposeConnectionDetailsFactoryIntegrationTests { | ||
|
||
@DockerComposeTest(composeFile = "lldap-compose.yaml", image = TestImage.LLDAP) | ||
void runCreatesConnectionDetails(LdapConnectionDetails connectionDetails) { | ||
assertThat(connectionDetails.getUsername()).isEqualTo("cn=admin,ou=people,dc=springframework,dc=org"); | ||
assertThat(connectionDetails.getPassword()).isEqualTo("somepassword"); | ||
assertThat(connectionDetails.getBase()).isEqualTo("dc=springframework,dc=org"); | ||
assertThat(connectionDetails.getUrls()).hasSize(1); | ||
assertThat(connectionDetails.getUrls()[0]).startsWith("ldap://"); | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
...ources/org/springframework/boot/docker/compose/service/connection/ldap/lldap-compose.yaml
This file contains 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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
services: | ||
ldap: | ||
image: '{imageName}' | ||
environment: | ||
- 'LLDAP_LDAP_BASE_DN=dc=springframework,dc=org' | ||
- 'LLDAP_LDAP_USER_PASS=somepassword' | ||
ports: | ||
- "3890" |
92 changes: 92 additions & 0 deletions
92
...ot/docker/compose/service/connection/ldap/LLdapDockerComposeConnectionDetailsFactory.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright 2012-2025 the original author or authors. | ||
* | ||
* Licensed 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 | ||
* | ||
* https://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 org.springframework.boot.docker.compose.service.connection.ldap; | ||
|
||
import java.util.Map; | ||
|
||
import org.springframework.boot.autoconfigure.ldap.LdapConnectionDetails; | ||
import org.springframework.boot.docker.compose.core.RunningService; | ||
import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionDetailsFactory; | ||
import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionSource; | ||
|
||
/** | ||
* {@link DockerComposeConnectionDetailsFactory} to create {@link LdapConnectionDetails} | ||
* for an {@code ldap} service. | ||
* | ||
* @author Eddú Meléndez | ||
*/ | ||
class LLdapDockerComposeConnectionDetailsFactory extends DockerComposeConnectionDetailsFactory<LdapConnectionDetails> { | ||
|
||
protected LLdapDockerComposeConnectionDetailsFactory() { | ||
super("lldap/lldap"); | ||
} | ||
|
||
@Override | ||
protected LdapConnectionDetails getDockerComposeConnectionDetails(DockerComposeConnectionSource source) { | ||
return new LLdapDockerComposeConnectionDetails(source.getRunningService()); | ||
} | ||
|
||
/** | ||
* {@link LdapConnectionDetails} backed by an {@code lldap} {@link RunningService}. | ||
*/ | ||
static class LLdapDockerComposeConnectionDetails extends DockerComposeConnectionDetails | ||
implements LdapConnectionDetails { | ||
|
||
private final String[] urls; | ||
|
||
private final String base; | ||
|
||
private final String username; | ||
|
||
private final String password; | ||
|
||
LLdapDockerComposeConnectionDetails(RunningService service) { | ||
super(service); | ||
Map<String, String> env = service.env(); | ||
boolean usesTls = Boolean.parseBoolean(env.getOrDefault("LLDAP_LDAPS_OPTIONS__ENABLED", "false")); | ||
String ldapPort = usesTls ? env.getOrDefault("LLDAP_LDAPS_OPTIONS__PORT", "6360") | ||
: env.getOrDefault("LLDAP_LDAP_PORT", "3890"); | ||
this.urls = new String[] { "%s://%s:%d".formatted(usesTls ? "ldaps" : "ldap", service.host(), | ||
service.ports().get(Integer.parseInt(ldapPort))) }; | ||
this.base = env.getOrDefault("LLDAP_LDAP_BASE_DN", "dc=example,dc=com"); | ||
this.password = env.getOrDefault("LLDAP_LDAP_USER_PASS", "password"); | ||
this.username = "cn=admin,ou=people,%s".formatted(this.base); | ||
} | ||
|
||
@Override | ||
public String[] getUrls() { | ||
return this.urls; | ||
} | ||
|
||
@Override | ||
public String getBase() { | ||
return this.base; | ||
} | ||
|
||
@Override | ||
public String getUsername() { | ||
return this.username; | ||
} | ||
|
||
@Override | ||
public String getPassword() { | ||
return this.password; | ||
} | ||
|
||
} | ||
|
||
} |
This file contains 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 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 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 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
68 changes: 68 additions & 0 deletions
68
...iners/service/connection/ldap/LLdapContainerConnectionDetailsFactoryIntegrationTests.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright 2012-2025 the original author or authors. | ||
* | ||
* Licensed 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 | ||
* | ||
* https://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 org.springframework.boot.testcontainers.service.connection.ldap; | ||
|
||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.testcontainers.junit.jupiter.Container; | ||
import org.testcontainers.junit.jupiter.Testcontainers; | ||
import org.testcontainers.ldap.LLdapContainer; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration; | ||
import org.springframework.boot.autoconfigure.ldap.LdapAutoConfiguration; | ||
import org.springframework.boot.testcontainers.service.connection.ServiceConnection; | ||
import org.springframework.boot.testsupport.container.TestImage; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.ldap.core.AttributesMapper; | ||
import org.springframework.ldap.core.LdapTemplate; | ||
import org.springframework.ldap.query.LdapQueryBuilder; | ||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Tests for {@link LLdapContainerConnectionDetailsFactory}. | ||
* | ||
* @author Eddú Meléndez | ||
*/ | ||
@SpringJUnitConfig | ||
@Testcontainers(disabledWithoutDocker = true) | ||
class LLdapContainerConnectionDetailsFactoryIntegrationTests { | ||
|
||
@Container | ||
@ServiceConnection | ||
static final LLdapContainer lldap = TestImage.container(LLdapContainer.class); | ||
|
||
@Autowired | ||
private LdapTemplate ldapTemplate; | ||
|
||
@Test | ||
void connectionCanBeMadeToLdapContainer() { | ||
List<String> cn = this.ldapTemplate.search(LdapQueryBuilder.query().where("objectClass").is("inetOrgPerson"), | ||
(AttributesMapper<String>) (attributes) -> attributes.get("cn").get().toString()); | ||
assertThat(cn).singleElement().isEqualTo("Administrator"); | ||
} | ||
|
||
@Configuration(proxyBeanMethods = false) | ||
@ImportAutoConfiguration({ LdapAutoConfiguration.class }) | ||
static class TestConfiguration { | ||
|
||
} | ||
|
||
} |
69 changes: 69 additions & 0 deletions
69
...k/boot/testcontainers/service/connection/ldap/LLdapContainerConnectionDetailsFactory.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright 2012-2025 the original author or authors. | ||
* | ||
* Licensed 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 | ||
* | ||
* https://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 org.springframework.boot.testcontainers.service.connection.ldap; | ||
|
||
import org.testcontainers.ldap.LLdapContainer; | ||
|
||
import org.springframework.boot.autoconfigure.ldap.LdapConnectionDetails; | ||
import org.springframework.boot.testcontainers.service.connection.ContainerConnectionDetailsFactory; | ||
import org.springframework.boot.testcontainers.service.connection.ContainerConnectionSource; | ||
import org.springframework.boot.testcontainers.service.connection.ServiceConnection; | ||
|
||
/** | ||
* {@link ContainerConnectionDetailsFactory} to create {@link LdapConnectionDetails} from | ||
* a {@link ServiceConnection @ServiceConnection}-annotated {@link LLdapContainer}. | ||
* | ||
* @author Eddú Meléndez | ||
*/ | ||
class LLdapContainerConnectionDetailsFactory | ||
extends ContainerConnectionDetailsFactory<LLdapContainer, LdapConnectionDetails> { | ||
|
||
@Override | ||
protected LdapConnectionDetails getContainerConnectionDetails(ContainerConnectionSource<LLdapContainer> source) { | ||
return new LLdapContainerConnectionDetails(source); | ||
} | ||
|
||
private static final class LLdapContainerConnectionDetails extends ContainerConnectionDetails<LLdapContainer> | ||
implements LdapConnectionDetails { | ||
|
||
private LLdapContainerConnectionDetails(ContainerConnectionSource<LLdapContainer> source) { | ||
super(source); | ||
} | ||
|
||
@Override | ||
public String[] getUrls() { | ||
return new String[] { getContainer().getLdapUrl() }; | ||
} | ||
|
||
@Override | ||
public String getBase() { | ||
return getContainer().getBaseDn(); | ||
} | ||
|
||
@Override | ||
public String getUsername() { | ||
return getContainer().getUser(); | ||
} | ||
|
||
@Override | ||
public String getPassword() { | ||
return getContainer().getUserPass(); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains 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 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 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