Commit eab0b84a authored by Phillip Webb's avatar Phillip Webb

Polish 'Add support for multi baseDn;

Update multi baseDn support to use the recently introduced
`@Delimter` annotation

Closes gh-11764
parent 270dc2cd
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.
......@@ -20,7 +20,6 @@ import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import com.unboundid.ldap.listener.InMemoryDirectoryServer;
......@@ -86,10 +85,6 @@ public class EmbeddedLdapAutoConfiguration {
this.properties = properties;
this.applicationContext = applicationContext;
this.environment = environment;
}
@PostConstruct
public void validateBaseDns() {
Assert.notEmpty(this.embeddedProperties.getBaseDn(), "No baseDn found.");
}
......@@ -108,8 +103,8 @@ public class EmbeddedLdapAutoConfiguration {
@Bean
public InMemoryDirectoryServer directoryServer() throws LDAPException {
InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig(
this.embeddedProperties.getBaseDn());
String[] baseDn = this.embeddedProperties.getBaseDn().toArray(new String[0]);
InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig(baseDn);
if (hasCredentials(this.embeddedProperties.getCredential())) {
config.addAdditionalBindCredentials(
this.embeddedProperties.getCredential().getUsername(),
......
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.
......@@ -16,7 +16,11 @@
package org.springframework.boot.autoconfigure.ldap.embedded;
import java.util.ArrayList;
import java.util.List;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.bind.convert.Delimiter;
import org.springframework.core.io.Resource;
/**
......@@ -42,7 +46,8 @@ public class EmbeddedLdapProperties {
/**
* List of base DN.
*/
private String[] baseDn = new String[0];
@Delimiter(Delimiter.NONE)
private List<String> baseDn = new ArrayList<>();
/**
* Schema (LDIF) script resource reference.
......@@ -70,11 +75,11 @@ public class EmbeddedLdapProperties {
this.credential = credential;
}
public String[] getBaseDn() {
public List<String> getBaseDn() {
return this.baseDn;
}
public void setBaseDn(String[] baseDn) {
public void setBaseDn(List<String> baseDn) {
this.baseDn = baseDn;
}
......
......@@ -50,10 +50,8 @@ public class EmbeddedLdapAutoConfigurationTests {
@Test
public void testSetDefaultPort() {
this.contextRunner
.withPropertyValues("spring.ldap.embedded.port:1234",
"spring.ldap.embedded.base-dn[0]:dc=spring,dc=org")
.run(context -> {
this.contextRunner.withPropertyValues("spring.ldap.embedded.port:1234",
"spring.ldap.embedded.base-dn:dc=spring,dc=org").run(context -> {
InMemoryDirectoryServer server = context
.getBean(InMemoryDirectoryServer.class);
assertThat(server.getListenPort()).isEqualTo(1234);
......@@ -63,7 +61,7 @@ public class EmbeddedLdapAutoConfigurationTests {
@Test
public void testRandomPortWithEnvironment() {
this.contextRunner
.withPropertyValues("spring.ldap.embedded.base-dn[0]:dc=spring,dc=org")
.withPropertyValues("spring.ldap.embedded.base-dn:dc=spring,dc=org")
.run(context -> {
InMemoryDirectoryServer server = context
.getBean(InMemoryDirectoryServer.class);
......@@ -75,7 +73,7 @@ public class EmbeddedLdapAutoConfigurationTests {
@Test
public void testRandomPortWithValueAnnotation() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
TestPropertyValues.of("spring.ldap.embedded.base-dn[0]:dc=spring,dc=org")
TestPropertyValues.of("spring.ldap.embedded.base-dn:dc=spring,dc=org")
.applyTo(context);
context.register(EmbeddedLdapAutoConfiguration.class,
LdapClientConfiguration.class,
......@@ -89,7 +87,7 @@ public class EmbeddedLdapAutoConfigurationTests {
@Test
public void testSetCredentials() {
this.contextRunner
.withPropertyValues("spring.ldap.embedded.base-dn[0]:dc=spring,dc=org",
.withPropertyValues("spring.ldap.embedded.base-dn:dc=spring,dc=org",
"spring.ldap.embedded.credential.username:uid=root",
"spring.ldap.embedded.credential.password:boot")
.run(context -> {
......@@ -103,7 +101,7 @@ public class EmbeddedLdapAutoConfigurationTests {
@Test
public void testSetPartitionSuffix() {
this.contextRunner
.withPropertyValues("spring.ldap.embedded.base-dn[0]:dc=spring,dc=org")
.withPropertyValues("spring.ldap.embedded.base-dn:dc=spring,dc=org")
.run(context -> {
InMemoryDirectoryServer server = context
.getBean(InMemoryDirectoryServer.class);
......@@ -115,7 +113,7 @@ public class EmbeddedLdapAutoConfigurationTests {
@Test
public void testSetLdifFile() {
this.contextRunner
.withPropertyValues("spring.ldap.embedded.base-dn[0]:dc=spring,dc=org")
.withPropertyValues("spring.ldap.embedded.base-dn:dc=spring,dc=org")
.run(context -> {
InMemoryDirectoryServer server = context
.getBean(InMemoryDirectoryServer.class);
......@@ -128,7 +126,7 @@ public class EmbeddedLdapAutoConfigurationTests {
@Test
public void testQueryEmbeddedLdap() {
this.contextRunner
.withPropertyValues("spring.ldap.embedded.base-dn[0]:dc=spring,dc=org")
.withPropertyValues("spring.ldap.embedded.base-dn:dc=spring,dc=org")
.withConfiguration(AutoConfigurations.of(LdapAutoConfiguration.class,
LdapDataAutoConfiguration.class))
.run(context -> {
......@@ -144,7 +142,7 @@ public class EmbeddedLdapAutoConfigurationTests {
public void testDisableSchemaValidation() {
this.contextRunner
.withPropertyValues("spring.ldap.embedded.validation.enabled:false",
"spring.ldap.embedded.base-dn[0]:dc=spring,dc=org")
"spring.ldap.embedded.base-dn:dc=spring,dc=org")
.run(context -> {
InMemoryDirectoryServer server = context
.getBean(InMemoryDirectoryServer.class);
......@@ -154,12 +152,10 @@ public class EmbeddedLdapAutoConfigurationTests {
@Test
public void testCustomSchemaValidation() {
this.contextRunner
.withPropertyValues(
this.contextRunner.withPropertyValues(
"spring.ldap.embedded.validation.schema:classpath:custom-schema.ldif",
"spring.ldap.embedded.ldif:classpath:custom-schema-sample.ldif",
"spring.ldap.embedded.base-dn[0]:dc=spring,dc=org")
.run(context -> {
"spring.ldap.embedded.base-dn:dc=spring,dc=org").run(context -> {
InMemoryDirectoryServer server = context
.getBean(InMemoryDirectoryServer.class);
......
......@@ -4417,6 +4417,30 @@ follows:
spring.ldap.embedded.base-dn=dc=spring,dc=io
----
[NOTE]
====
It is possible to define multiple base-dn values, however, since distinguished names
usually contain commas, they must be defined using the correct notation.
In yaml files, you can use the yaml list notation:
[source,yaml,indent=0]
----
spring.ldap.embedded.base-dn:
- dc=spring,dc=io
- dc=pivotal,dc=io
----
in properties files, you must include the index as part of the property name:
[source,properties,indent=0]
----
spring.ldap.embedded.base-dn[0]=dc=spring,dc=io
spring.ldap.embedded.base-dn[1]=dc=pivotal,dc=io
----
====
WARNING: `spring.ldap.embedded.base-dn` supports multi base DN, so it must define as follows `spring.ldap.embedded.base-dn[0]=dc=spring,dc=io`
By default, the server starts on a random port and triggers the regular LDAP support.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment