|
| 1 | +/* |
| 2 | + * Copyright 2012-2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.docker.compose.service.connection.ldap; |
| 18 | + |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.stream.Collectors; |
| 22 | + |
| 23 | +import org.springframework.boot.autoconfigure.ldap.LdapConnectionDetails; |
| 24 | +import org.springframework.boot.docker.compose.core.RunningService; |
| 25 | +import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionDetailsFactory; |
| 26 | +import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionSource; |
| 27 | + |
| 28 | +/** |
| 29 | + * {@link DockerComposeConnectionDetailsFactory} to create {@link LdapConnectionDetails} |
| 30 | + * for an {@code ldap} service. |
| 31 | + * |
| 32 | + * @author Philipp Kessler |
| 33 | + */ |
| 34 | +class OpenLdapDockerComposeConnectionDetailsFactory |
| 35 | + extends DockerComposeConnectionDetailsFactory<LdapConnectionDetails> { |
| 36 | + |
| 37 | + protected OpenLdapDockerComposeConnectionDetailsFactory() { |
| 38 | + super("osixia/openldap"); |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + protected LdapConnectionDetails getDockerComposeConnectionDetails(DockerComposeConnectionSource source) { |
| 43 | + return new OpenLdapDockerComposeConnectionDetails(source.getRunningService()); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * {@link LdapConnectionDetails} backed by an {@code openldap} {@link RunningService}. |
| 48 | + */ |
| 49 | + static class OpenLdapDockerComposeConnectionDetails extends DockerComposeConnectionDetails |
| 50 | + implements LdapConnectionDetails { |
| 51 | + |
| 52 | + private final String[] urls; |
| 53 | + |
| 54 | + private final String base; |
| 55 | + |
| 56 | + private final String username; |
| 57 | + |
| 58 | + private final String password; |
| 59 | + |
| 60 | + OpenLdapDockerComposeConnectionDetails(RunningService service) { |
| 61 | + super(service); |
| 62 | + Map<String, String> env = service.env(); |
| 63 | + boolean usesTls = Boolean.parseBoolean(env.getOrDefault("LDAP_TLS", "true")); |
| 64 | + String ldapPort = usesTls ? env.getOrDefault("LDAPS_PORT", "636") : env.getOrDefault("LDAP_PORT", "389"); |
| 65 | + this.urls = new String[] { "%s://%s:%d".formatted(usesTls ? "ldaps" : "ldap", service.host(), |
| 66 | + service.ports().get(Integer.parseInt(ldapPort))) }; |
| 67 | + if (env.containsKey("LDAP_BASE_DN")) { |
| 68 | + this.base = env.get("LDAP_BASE_DN"); |
| 69 | + } |
| 70 | + else { |
| 71 | + this.base = Arrays.stream(env.getOrDefault("LDAP_DOMAIN", "example.org").split("\\.")) |
| 72 | + .map("dc=%s"::formatted) |
| 73 | + .collect(Collectors.joining(",")); |
| 74 | + } |
| 75 | + this.password = env.getOrDefault("LDAP_ADMIN_PASSWORD", "admin"); |
| 76 | + this.username = "cn=admin,%s".formatted(this.base); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public String[] getUrls() { |
| 81 | + return this.urls; |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public String getBase() { |
| 86 | + return this.base; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public String getUsername() { |
| 91 | + return this.username; |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public String getPassword() { |
| 96 | + return this.password; |
| 97 | + } |
| 98 | + |
| 99 | + } |
| 100 | + |
| 101 | +} |
0 commit comments