Add Docker Compose service connection support for OpenLDAP

See gh-39258
This commit is contained in:
PhilKes
2024-01-21 15:07:37 +01:00
committed by Scott Frederick
parent a0a804cfdf
commit eb940c3907
16 changed files with 592 additions and 5 deletions

View File

@@ -0,0 +1,99 @@
/*
* 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.
* 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.Arrays;
import java.util.Map;
import java.util.stream.Collectors;
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 Philipp Kessler
*/
class LdapDockerComposeConnectionDetailsFactory extends DockerComposeConnectionDetailsFactory<LdapConnectionDetails> {
protected LdapDockerComposeConnectionDetailsFactory() {
super("osixia/openldap");
}
@Override
protected LdapConnectionDetails getDockerComposeConnectionDetails(DockerComposeConnectionSource source) {
return new LdapDockerComposeConnectionDetails(source.getRunningService());
}
/**
* {@link LdapConnectionDetails} backed by an {@code openldap} {@link RunningService}.
*/
static class LdapDockerComposeConnectionDetails extends DockerComposeConnectionDetails
implements LdapConnectionDetails {
private final String[] urls;
private final String base;
private final String username;
private final String password;
LdapDockerComposeConnectionDetails(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("\\."))
.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);
}
@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;
}
}
}

View File

@@ -0,0 +1,20 @@
/*
* 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.
* 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.
*/
/**
* Auto-configuration for docker compose Ldap service connections.
*/
package org.springframework.boot.docker.compose.service.connection.ldap;

View File

@@ -9,6 +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.liquibase.JdbcAdaptingLiquibaseConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.mariadb.MariaDbJdbcDockerComposeConnectionDetailsFactory,\
org.springframework.boot.docker.compose.service.connection.mariadb.MariaDbR2dbcDockerComposeConnectionDetailsFactory,\

View File

@@ -0,0 +1,48 @@
/*
* 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.
* 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.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.ldap.LdapConnectionDetails;
import org.springframework.boot.docker.compose.service.connection.test.AbstractDockerComposeIntegrationTests;
import org.springframework.boot.testsupport.testcontainers.DockerImageNames;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration tests for {@link LdapDockerComposeConnectionDetailsFactory}.
*
* @author Philipp Kessler
*/
class LdapDockerComposeConnectionDetailsFactoryIntegrationTests extends AbstractDockerComposeIntegrationTests {
LdapDockerComposeConnectionDetailsFactoryIntegrationTests() {
super("ldap-compose.yaml", DockerImageNames.ldap());
}
@Test
void runCreatesConnectionDetails() {
LdapConnectionDetails connectionDetails = run(LdapConnectionDetails.class);
assertThat(connectionDetails.getUsername()).isEqualTo("cn=admin,dc=ldap,dc=example,dc=org");
assertThat(connectionDetails.getPassword()).isEqualTo("somepassword");
assertThat(connectionDetails.getBase()).isEqualTo("dc=ldap,dc=example,dc=org");
assertThat(connectionDetails.getUrls()).hasSize(1);
assertThat(connectionDetails.getUrls()[0]).startsWith("ldaps://");
}
}

View File

@@ -0,0 +1,11 @@
services:
ldap:
image: '{imageName}'
environment:
- 'LDAP_DOMAIN=ldap.example.org'
- 'LDAP_ADMIN_PASSWORD=somepassword'
- 'LDAP_TLS=true'
hostname: ldap
ports:
- "389"
- "636"