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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -20,7 +20,6 @@ import java.io.InputStream; ...@@ -20,7 +20,6 @@ import java.io.InputStream;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy; import javax.annotation.PreDestroy;
import com.unboundid.ldap.listener.InMemoryDirectoryServer; import com.unboundid.ldap.listener.InMemoryDirectoryServer;
...@@ -86,10 +85,6 @@ public class EmbeddedLdapAutoConfiguration { ...@@ -86,10 +85,6 @@ public class EmbeddedLdapAutoConfiguration {
this.properties = properties; this.properties = properties;
this.applicationContext = applicationContext; this.applicationContext = applicationContext;
this.environment = environment; this.environment = environment;
}
@PostConstruct
public void validateBaseDns() {
Assert.notEmpty(this.embeddedProperties.getBaseDn(), "No baseDn found."); Assert.notEmpty(this.embeddedProperties.getBaseDn(), "No baseDn found.");
} }
...@@ -108,8 +103,8 @@ public class EmbeddedLdapAutoConfiguration { ...@@ -108,8 +103,8 @@ public class EmbeddedLdapAutoConfiguration {
@Bean @Bean
public InMemoryDirectoryServer directoryServer() throws LDAPException { public InMemoryDirectoryServer directoryServer() throws LDAPException {
InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig( String[] baseDn = this.embeddedProperties.getBaseDn().toArray(new String[0]);
this.embeddedProperties.getBaseDn()); InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig(baseDn);
if (hasCredentials(this.embeddedProperties.getCredential())) { if (hasCredentials(this.embeddedProperties.getCredential())) {
config.addAdditionalBindCredentials( config.addAdditionalBindCredentials(
this.embeddedProperties.getCredential().getUsername(), 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -16,7 +16,11 @@ ...@@ -16,7 +16,11 @@
package org.springframework.boot.autoconfigure.ldap.embedded; 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.ConfigurationProperties;
import org.springframework.boot.context.properties.bind.convert.Delimiter;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
/** /**
...@@ -42,7 +46,8 @@ public class EmbeddedLdapProperties { ...@@ -42,7 +46,8 @@ public class EmbeddedLdapProperties {
/** /**
* List of base DN. * List of base DN.
*/ */
private String[] baseDn = new String[0]; @Delimiter(Delimiter.NONE)
private List<String> baseDn = new ArrayList<>();
/** /**
* Schema (LDIF) script resource reference. * Schema (LDIF) script resource reference.
...@@ -70,11 +75,11 @@ public class EmbeddedLdapProperties { ...@@ -70,11 +75,11 @@ public class EmbeddedLdapProperties {
this.credential = credential; this.credential = credential;
} }
public String[] getBaseDn() { public List<String> getBaseDn() {
return this.baseDn; return this.baseDn;
} }
public void setBaseDn(String[] baseDn) { public void setBaseDn(List<String> baseDn) {
this.baseDn = baseDn; this.baseDn = baseDn;
} }
......
...@@ -50,10 +50,8 @@ public class EmbeddedLdapAutoConfigurationTests { ...@@ -50,10 +50,8 @@ public class EmbeddedLdapAutoConfigurationTests {
@Test @Test
public void testSetDefaultPort() { public void testSetDefaultPort() {
this.contextRunner this.contextRunner.withPropertyValues("spring.ldap.embedded.port:1234",
.withPropertyValues("spring.ldap.embedded.port:1234", "spring.ldap.embedded.base-dn:dc=spring,dc=org").run(context -> {
"spring.ldap.embedded.base-dn[0]:dc=spring,dc=org")
.run(context -> {
InMemoryDirectoryServer server = context InMemoryDirectoryServer server = context
.getBean(InMemoryDirectoryServer.class); .getBean(InMemoryDirectoryServer.class);
assertThat(server.getListenPort()).isEqualTo(1234); assertThat(server.getListenPort()).isEqualTo(1234);
...@@ -63,7 +61,7 @@ public class EmbeddedLdapAutoConfigurationTests { ...@@ -63,7 +61,7 @@ public class EmbeddedLdapAutoConfigurationTests {
@Test @Test
public void testRandomPortWithEnvironment() { public void testRandomPortWithEnvironment() {
this.contextRunner 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 -> { .run(context -> {
InMemoryDirectoryServer server = context InMemoryDirectoryServer server = context
.getBean(InMemoryDirectoryServer.class); .getBean(InMemoryDirectoryServer.class);
...@@ -75,7 +73,7 @@ public class EmbeddedLdapAutoConfigurationTests { ...@@ -75,7 +73,7 @@ public class EmbeddedLdapAutoConfigurationTests {
@Test @Test
public void testRandomPortWithValueAnnotation() { public void testRandomPortWithValueAnnotation() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); 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); .applyTo(context);
context.register(EmbeddedLdapAutoConfiguration.class, context.register(EmbeddedLdapAutoConfiguration.class,
LdapClientConfiguration.class, LdapClientConfiguration.class,
...@@ -89,7 +87,7 @@ public class EmbeddedLdapAutoConfigurationTests { ...@@ -89,7 +87,7 @@ public class EmbeddedLdapAutoConfigurationTests {
@Test @Test
public void testSetCredentials() { public void testSetCredentials() {
this.contextRunner 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.username:uid=root",
"spring.ldap.embedded.credential.password:boot") "spring.ldap.embedded.credential.password:boot")
.run(context -> { .run(context -> {
...@@ -103,7 +101,7 @@ public class EmbeddedLdapAutoConfigurationTests { ...@@ -103,7 +101,7 @@ public class EmbeddedLdapAutoConfigurationTests {
@Test @Test
public void testSetPartitionSuffix() { public void testSetPartitionSuffix() {
this.contextRunner 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 -> { .run(context -> {
InMemoryDirectoryServer server = context InMemoryDirectoryServer server = context
.getBean(InMemoryDirectoryServer.class); .getBean(InMemoryDirectoryServer.class);
...@@ -115,7 +113,7 @@ public class EmbeddedLdapAutoConfigurationTests { ...@@ -115,7 +113,7 @@ public class EmbeddedLdapAutoConfigurationTests {
@Test @Test
public void testSetLdifFile() { public void testSetLdifFile() {
this.contextRunner 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 -> { .run(context -> {
InMemoryDirectoryServer server = context InMemoryDirectoryServer server = context
.getBean(InMemoryDirectoryServer.class); .getBean(InMemoryDirectoryServer.class);
...@@ -128,7 +126,7 @@ public class EmbeddedLdapAutoConfigurationTests { ...@@ -128,7 +126,7 @@ public class EmbeddedLdapAutoConfigurationTests {
@Test @Test
public void testQueryEmbeddedLdap() { public void testQueryEmbeddedLdap() {
this.contextRunner 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, .withConfiguration(AutoConfigurations.of(LdapAutoConfiguration.class,
LdapDataAutoConfiguration.class)) LdapDataAutoConfiguration.class))
.run(context -> { .run(context -> {
...@@ -144,7 +142,7 @@ public class EmbeddedLdapAutoConfigurationTests { ...@@ -144,7 +142,7 @@ public class EmbeddedLdapAutoConfigurationTests {
public void testDisableSchemaValidation() { public void testDisableSchemaValidation() {
this.contextRunner this.contextRunner
.withPropertyValues("spring.ldap.embedded.validation.enabled:false", .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 -> { .run(context -> {
InMemoryDirectoryServer server = context InMemoryDirectoryServer server = context
.getBean(InMemoryDirectoryServer.class); .getBean(InMemoryDirectoryServer.class);
...@@ -154,12 +152,10 @@ public class EmbeddedLdapAutoConfigurationTests { ...@@ -154,12 +152,10 @@ public class EmbeddedLdapAutoConfigurationTests {
@Test @Test
public void testCustomSchemaValidation() { public void testCustomSchemaValidation() {
this.contextRunner this.contextRunner.withPropertyValues(
.withPropertyValues( "spring.ldap.embedded.validation.schema:classpath:custom-schema.ldif",
"spring.ldap.embedded.validation.schema:classpath:custom-schema.ldif", "spring.ldap.embedded.ldif:classpath:custom-schema-sample.ldif",
"spring.ldap.embedded.ldif:classpath:custom-schema-sample.ldif", "spring.ldap.embedded.base-dn:dc=spring,dc=org").run(context -> {
"spring.ldap.embedded.base-dn[0]:dc=spring,dc=org")
.run(context -> {
InMemoryDirectoryServer server = context InMemoryDirectoryServer server = context
.getBean(InMemoryDirectoryServer.class); .getBean(InMemoryDirectoryServer.class);
......
...@@ -4417,6 +4417,30 @@ follows: ...@@ -4417,6 +4417,30 @@ follows:
spring.ldap.embedded.base-dn=dc=spring,dc=io 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` 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. 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