Polish "Add Docker Compose service connection support for OpenLDAP"

See gh-39258
This commit is contained in:
Scott Frederick 2024-01-22 16:33:59 -06:00
parent eb940c3907
commit bee6fe899c
13 changed files with 56 additions and 60 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 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.

View File

@ -19,7 +19,7 @@ package org.springframework.boot.autoconfigure.ldap;
import org.springframework.boot.autoconfigure.service.connection.ConnectionDetails;
/**
* Details required to establish a connection to a Ldap service.
* Details required to establish a connection to an LDAP service.
*
* @author Philipp Kessler
* @since 3.3.0
@ -28,13 +28,13 @@ public interface LdapConnectionDetails extends ConnectionDetails {
/**
* LDAP URLs of the server.
* @return list of the LDAP urls to use
* @return the LDAP URLs to use
*/
String[] getUrls();
/**
* Base suffix from which all operations should originate.
* @return base suffix from which all operations should originate or null.
* @return base suffix
*/
default String getBase() {
return null;
@ -42,7 +42,7 @@ public interface LdapConnectionDetails extends ConnectionDetails {
/**
* Login username of the server.
* @return login username of the server or null.
* @return login username
*/
default String getUsername() {
return null;
@ -50,7 +50,7 @@ public interface LdapConnectionDetails extends ConnectionDetails {
/**
* Login password of the server.
* @return login password of the server or null.
* @return login password
*/
default String getPassword() {
return null;

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 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.
@ -119,7 +119,7 @@ class LdapAutoConfigurationTests {
}
@Test
void shouldUseCustomConnectionDetailsWhenDefined() {
void usesCustomConnectionDetailsWhenDefined() {
this.contextRunner.withUserConfiguration(ConnectionDetailsConfiguration.class).run((context) -> {
assertThat(context).hasSingleBean(LdapContextSource.class)
.hasSingleBean(LdapConnectionDetails.class)

View File

@ -31,21 +31,22 @@ import org.springframework.boot.docker.compose.service.connection.DockerComposeC
*
* @author Philipp Kessler
*/
class LdapDockerComposeConnectionDetailsFactory extends DockerComposeConnectionDetailsFactory<LdapConnectionDetails> {
class OpenLdapDockerComposeConnectionDetailsFactory
extends DockerComposeConnectionDetailsFactory<LdapConnectionDetails> {
protected LdapDockerComposeConnectionDetailsFactory() {
protected OpenLdapDockerComposeConnectionDetailsFactory() {
super("osixia/openldap");
}
@Override
protected LdapConnectionDetails getDockerComposeConnectionDetails(DockerComposeConnectionSource source) {
return new LdapDockerComposeConnectionDetails(source.getRunningService());
return new OpenLdapDockerComposeConnectionDetails(source.getRunningService());
}
/**
* {@link LdapConnectionDetails} backed by an {@code openldap} {@link RunningService}.
*/
static class LdapDockerComposeConnectionDetails extends DockerComposeConnectionDetails
static class OpenLdapDockerComposeConnectionDetails extends DockerComposeConnectionDetails
implements LdapConnectionDetails {
private final String[] urls;
@ -56,20 +57,21 @@ class LdapDockerComposeConnectionDetailsFactory extends DockerComposeConnectionD
private final String password;
LdapDockerComposeConnectionDetails(RunningService service) {
OpenLdapDockerComposeConnectionDetails(RunningService service) {
super(service);
Map<String, String> env = service.env();
boolean usesTls = Boolean.parseBoolean(env.getOrDefault("LDAP_TLS", "true"));
String ldapPort = usesTls ? env.getOrDefault("LDAPS_PORT", "636") : env.getOrDefault("LDAP_PORT", "389");
this.urls = new String[] { "%s://%s:%d".formatted(usesTls ? "ldaps" : "ldap", service.host(),
service.ports().get(Integer.parseInt(ldapPort))) };
String baseDn = env.getOrDefault("LDAP_BASE_DN", null);
if (baseDn == null) {
baseDn = Arrays.stream(env.getOrDefault("LDAP_DOMAIN", "example.org").split("\\."))
if (env.containsKey("LDAP_BASE_DN")) {
this.base = env.get("LDAP_BASE_DN");
}
else {
this.base = Arrays.stream(env.getOrDefault("LDAP_DOMAIN", "example.org").split("\\."))
.map("dc=%s"::formatted)
.collect(Collectors.joining(","));
}
this.base = baseDn;
this.password = env.getOrDefault("LDAP_ADMIN_PASSWORD", "admin");
this.username = "cn=admin,%s".formatted(this.base);
}

View File

@ -15,6 +15,6 @@
*/
/**
* Auto-configuration for docker compose Ldap service connections.
* Auto-configuration for Docker Compose LDAP service connections.
*/
package org.springframework.boot.docker.compose.service.connection.ldap;

View File

@ -9,7 +9,7 @@ org.springframework.boot.docker.compose.service.connection.activemq.ActiveMQDock
org.springframework.boot.docker.compose.service.connection.cassandra.CassandraDockerComposeConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.elasticsearch.ElasticsearchDockerComposeConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.flyway.JdbcAdaptingFlywayConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.ldap.LdapDockerComposeConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.ldap.OpenLdapDockerComposeConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.liquibase.JdbcAdaptingLiquibaseConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.mariadb.MariaDbJdbcDockerComposeConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.mariadb.MariaDbR2dbcDockerComposeConnectionDetailsFactory,\

View File

@ -25,14 +25,14 @@ import org.springframework.boot.testsupport.testcontainers.DockerImageNames;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration tests for {@link LdapDockerComposeConnectionDetailsFactory}.
* Integration tests for {@link OpenLdapDockerComposeConnectionDetailsFactory}.
*
* @author Philipp Kessler
*/
class LdapDockerComposeConnectionDetailsFactoryIntegrationTests extends AbstractDockerComposeIntegrationTests {
class OpenLdapDockerComposeConnectionDetailsFactoryIntegrationTests extends AbstractDockerComposeIntegrationTests {
LdapDockerComposeConnectionDetailsFactoryIntegrationTests() {
super("ldap-compose.yaml", DockerImageNames.ldap());
OpenLdapDockerComposeConnectionDetailsFactoryIntegrationTests() {
super("ldap-compose.yaml", DockerImageNames.openLdap());
}
@Test

View File

@ -81,6 +81,9 @@ The following service connections are currently supported:
| `JdbcConnectionDetails`
| Containers named "gvenzl/oracle-free", "gvenzl/oracle-xe", "mariadb", "mssql/server", "mysql", or "postgres"
| `LdapConnectionDetails`
| Containers named "osixia/openldap"
| `MongoConnectionDetails`
| Containers named "mongo"

View File

@ -35,22 +35,22 @@ import org.springframework.boot.testcontainers.service.connection.ServiceConnect
*
* @author Philipp Kessler
*/
class LdapContainerConnectionDetailsFactory
class OpenLdapContainerConnectionDetailsFactory
extends ContainerConnectionDetailsFactory<Container<?>, LdapConnectionDetails> {
LdapContainerConnectionDetailsFactory() {
OpenLdapContainerConnectionDetailsFactory() {
super("osixia/openldap");
}
@Override
protected LdapConnectionDetails getContainerConnectionDetails(ContainerConnectionSource<Container<?>> source) {
return new LdapContainerConnectionDetailsFactory.LdapContainerConnectionDetails(source);
return new OpenLdapContainerConnectionDetails(source);
}
private static final class LdapContainerConnectionDetails extends ContainerConnectionDetails<Container<?>>
private static final class OpenLdapContainerConnectionDetails extends ContainerConnectionDetails<Container<?>>
implements LdapConnectionDetails {
private LdapContainerConnectionDetails(ContainerConnectionSource<Container<?>> source) {
private OpenLdapContainerConnectionDetails(ContainerConnectionSource<Container<?>> source) {
super(source);
}
@ -65,14 +65,13 @@ class LdapContainerConnectionDetailsFactory
@Override
public String getBase() {
String baseDn = getContainer().getEnvMap().getOrDefault("LDAP_BASE_DN", null);
if (baseDn == null) {
baseDn = Arrays
.stream(getContainer().getEnvMap().getOrDefault("LDAP_DOMAIN", "example.org").split("\\."))
.map("dc=%s"::formatted)
.collect(Collectors.joining(","));
Map<String, String> env = getContainer().getEnvMap();
if (env.containsKey("LDAP_BASE_DN")) {
return env.get("LDAP_BASE_DN");
}
return baseDn;
return Arrays.stream(env.getOrDefault("LDAP_DOMAIN", "example.org").split("\\."))
.map("dc=%s"::formatted)
.collect(Collectors.joining(","));
}
@Override

View File

@ -16,7 +16,7 @@ org.springframework.boot.testcontainers.service.connection.flyway.FlywayContaine
org.springframework.boot.testcontainers.service.connection.elasticsearch.ElasticsearchContainerConnectionDetailsFactory,\
org.springframework.boot.testcontainers.service.connection.jdbc.JdbcContainerConnectionDetailsFactory,\
org.springframework.boot.testcontainers.service.connection.kafka.KafkaContainerConnectionDetailsFactory,\
org.springframework.boot.testcontainers.service.connection.ldap.LdapContainerConnectionDetailsFactory,\
org.springframework.boot.testcontainers.service.connection.ldap.OpenLdapContainerConnectionDetailsFactory,\
org.springframework.boot.testcontainers.service.connection.liquibase.LiquibaseContainerConnectionDetailsFactory,\
org.springframework.boot.testcontainers.service.connection.mongo.MongoContainerConnectionDetailsFactory,\
org.springframework.boot.testcontainers.service.connection.neo4j.Neo4jContainerConnectionDetailsFactory,\

View File

@ -18,9 +18,6 @@ package org.springframework.boot.testcontainers.service.connection.ldap;
import java.util.List;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import org.junit.jupiter.api.Test;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
@ -29,7 +26,7 @@ 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.testcontainers.LdapContainer;
import org.springframework.boot.testsupport.testcontainers.OpenLdapContainer;
import org.springframework.context.annotation.Configuration;
import org.springframework.ldap.core.AttributesMapper;
import org.springframework.ldap.core.LdapTemplate;
@ -39,17 +36,17 @@ import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link LdapContainerConnectionDetailsFactory}.
* Tests for {@link OpenLdapContainerConnectionDetailsFactory}.
*
* @author Philipp Kessler
*/
@SpringJUnitConfig
@Testcontainers(disabledWithoutDocker = true)
class LdapContainerConnectionDetailsFactoryIntegrationTests {
class OpenLdapContainerConnectionDetailsFactoryIntegrationTests {
@Container
@ServiceConnection
static final LdapContainer openLdap = new LdapContainer().withEnv("LDAP_TLS", "false");
static final OpenLdapContainer openLdap = new OpenLdapContainer().withEnv("LDAP_TLS", "false");
@Autowired
private LdapTemplate ldapTemplate;
@ -57,12 +54,7 @@ class LdapContainerConnectionDetailsFactoryIntegrationTests {
@Test
void connectionCanBeMadeToLdapContainer() {
List<String> cn = this.ldapTemplate.search(LdapQueryBuilder.query().where("objectclass").is("dcObject"),
new AttributesMapper<String>() {
@Override
public String mapFromAttributes(Attributes attributes) throws NamingException {
return attributes.get("dc").get().toString();
}
});
(AttributesMapper<String>) (attributes) -> attributes.get("dc").get().toString());
assertThat(cn).hasSize(1);
assertThat(cn.get(0)).isEqualTo("example");
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 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.
@ -40,8 +40,6 @@ public final class DockerImageNames {
private static final String KAFKA_VERSION = "7.4.0";
private static final String LDAP_VERSION = "1.5.0";
private static final String MARIADB_VERSION = "10.10";
private static final String MONGO_VERSION = "5.0.17";
@ -50,6 +48,8 @@ public final class DockerImageNames {
private static final String NEO4J_VERSION = "4.4.11";
private static final String OPEN_LDAP_VERSION = "1.5.0";
private static final String ORACLE_FREE_VERSION = "23.3-slim";
private static final String ORACLE_XE_VERSION = "18.4.0-slim";
@ -122,11 +122,11 @@ public final class DockerImageNames {
}
/**
* Return a {@link DockerImageName} suitable for running OpenLdap.
* @return a docker image name for running OpenLdap
* Return a {@link DockerImageName} suitable for running OpenLDAP.
* @return a docker image name for running OpenLDAP
*/
public static DockerImageName ldap() {
return DockerImageName.parse("osixia/openldap").withTag(LDAP_VERSION);
public static DockerImageName openLdap() {
return DockerImageName.parse("osixia/openldap").withTag(OPEN_LDAP_VERSION);
}
/**

View File

@ -19,16 +19,16 @@ package org.springframework.boot.testsupport.testcontainers;
import org.testcontainers.containers.GenericContainer;
/**
* A {@link GenericContainer} for OpenLdap.
* A {@link GenericContainer} for OpenLDAP.
*
* @author Philipp Kessler
*/
public class LdapContainer extends GenericContainer<LdapContainer> {
public class OpenLdapContainer extends GenericContainer<OpenLdapContainer> {
private static final int DEFAULT_LDAP_PORT = 389;
public LdapContainer() {
super(DockerImageNames.ldap());
public OpenLdapContainer() {
super(DockerImageNames.openLdap());
addExposedPorts(DEFAULT_LDAP_PORT);
}