|
| 1 | +/* |
| 2 | + * Copyright 2012-2025 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.Map; |
| 20 | + |
| 21 | +import org.springframework.boot.autoconfigure.ldap.LdapConnectionDetails; |
| 22 | +import org.springframework.boot.docker.compose.core.RunningService; |
| 23 | +import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionDetailsFactory; |
| 24 | +import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionSource; |
| 25 | + |
| 26 | +/** |
| 27 | + * {@link DockerComposeConnectionDetailsFactory} to create {@link LdapConnectionDetails} |
| 28 | + * for an {@code ldap} service. |
| 29 | + * |
| 30 | + * @author Eddú Meléndez |
| 31 | + */ |
| 32 | +class LLdapDockerComposeConnectionDetailsFactory extends DockerComposeConnectionDetailsFactory<LdapConnectionDetails> { |
| 33 | + |
| 34 | + protected LLdapDockerComposeConnectionDetailsFactory() { |
| 35 | + super("lldap/lldap"); |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + protected LdapConnectionDetails getDockerComposeConnectionDetails(DockerComposeConnectionSource source) { |
| 40 | + return new LLdapDockerComposeConnectionDetails(source.getRunningService()); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * {@link LdapConnectionDetails} backed by an {@code lldap} {@link RunningService}. |
| 45 | + */ |
| 46 | + static class LLdapDockerComposeConnectionDetails extends DockerComposeConnectionDetails |
| 47 | + implements LdapConnectionDetails { |
| 48 | + |
| 49 | + private final String[] urls; |
| 50 | + |
| 51 | + private final String base; |
| 52 | + |
| 53 | + private final String username; |
| 54 | + |
| 55 | + private final String password; |
| 56 | + |
| 57 | + LLdapDockerComposeConnectionDetails(RunningService service) { |
| 58 | + super(service); |
| 59 | + Map<String, String> env = service.env(); |
| 60 | + boolean usesTls = Boolean.parseBoolean(env.getOrDefault("LLDAP_LDAPS_OPTIONS__ENABLED", "false")); |
| 61 | + String ldapPort = usesTls ? env.getOrDefault("LLDAP_LDAPS_OPTIONS__PORT", "6360") |
| 62 | + : env.getOrDefault("LLDAP_LDAP_PORT", "3890"); |
| 63 | + this.urls = new String[] { "%s://%s:%d".formatted(usesTls ? "ldaps" : "ldap", service.host(), |
| 64 | + service.ports().get(Integer.parseInt(ldapPort))) }; |
| 65 | + this.base = env.getOrDefault("LLDAP_LDAP_BASE_DN", "dc=example,dc=com"); |
| 66 | + this.password = env.getOrDefault("LLDAP_LDAP_USER_PASS", "password"); |
| 67 | + this.username = "cn=admin,ou=people,%s".formatted(this.base); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public String[] getUrls() { |
| 72 | + return this.urls; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public String getBase() { |
| 77 | + return this.base; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public String getUsername() { |
| 82 | + return this.username; |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public String getPassword() { |
| 87 | + return this.password; |
| 88 | + } |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | +} |
0 commit comments