From eab0b84a80e4e70bbf84289e1318e99c70ff899e Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Sun, 4 Feb 2018 10:39:29 -0800 Subject: [PATCH] Polish 'Add support for multi baseDn; Update multi baseDn support to use the recently introduced `@Delimter` annotation Closes gh-11764 --- .../EmbeddedLdapAutoConfiguration.java | 11 ++----- .../ldap/embedded/EmbeddedLdapProperties.java | 13 +++++--- .../EmbeddedLdapAutoConfigurationTests.java | 30 ++++++++----------- .../main/asciidoc/spring-boot-features.adoc | 24 +++++++++++++++ 4 files changed, 49 insertions(+), 29 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ldap/embedded/EmbeddedLdapAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ldap/embedded/EmbeddedLdapAutoConfiguration.java index 74d5807659d..12c6f80f2ac 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ldap/embedded/EmbeddedLdapAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ldap/embedded/EmbeddedLdapAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * 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(), diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ldap/embedded/EmbeddedLdapProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ldap/embedded/EmbeddedLdapProperties.java index a42bd120691..0076d9364b1 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ldap/embedded/EmbeddedLdapProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ldap/embedded/EmbeddedLdapProperties.java @@ -1,5 +1,5 @@ /* - * 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 baseDn = new ArrayList<>(); /** * Schema (LDIF) script resource reference. @@ -70,11 +75,11 @@ public class EmbeddedLdapProperties { this.credential = credential; } - public String[] getBaseDn() { + public List getBaseDn() { return this.baseDn; } - public void setBaseDn(String[] baseDn) { + public void setBaseDn(List baseDn) { this.baseDn = baseDn; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/ldap/embedded/EmbeddedLdapAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/ldap/embedded/EmbeddedLdapAutoConfigurationTests.java index b07364fbd1a..ddfeb7d95f8 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/ldap/embedded/EmbeddedLdapAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/ldap/embedded/EmbeddedLdapAutoConfigurationTests.java @@ -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( - "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 -> { + 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:dc=spring,dc=org").run(context -> { InMemoryDirectoryServer server = context .getBean(InMemoryDirectoryServer.class); diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc index 95b5532f76d..6ce1cac6a49 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -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.