Commit 952e766e authored by Stephane Nicoll's avatar Stephane Nicoll

Merge pull request #11744 from eddumelendez:gh-11722

* pr/11744:
  Polish "Add support for anonymousReadOnly in LdapProperties"
  Add support for anonymousReadOnly in LdapProperties
  Move tests to use ApplicationContextRunner
parents 145d46e0 960989cf
/* /*
* 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.
...@@ -54,6 +54,7 @@ public class LdapAutoConfiguration { ...@@ -54,6 +54,7 @@ public class LdapAutoConfiguration {
LdapContextSource source = new LdapContextSource(); LdapContextSource source = new LdapContextSource();
source.setUserDn(this.properties.getUsername()); source.setUserDn(this.properties.getUsername());
source.setPassword(this.properties.getPassword()); source.setPassword(this.properties.getPassword());
source.setAnonymousReadOnly(this.properties.getAnonymousReadOnly());
source.setBase(this.properties.getBase()); source.setBase(this.properties.getBase());
source.setUrls(this.properties.determineUrls(this.environment)); source.setUrls(this.properties.determineUrls(this.environment));
source.setBaseEnvironmentProperties( source.setBaseEnvironmentProperties(
......
/* /*
* 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.
...@@ -55,10 +55,15 @@ public class LdapProperties { ...@@ -55,10 +55,15 @@ public class LdapProperties {
*/ */
private String password; private String password;
/**
* Whether read-only operations should use an anonymous environment.
*/
private boolean anonymousReadOnly;
/** /**
* LDAP specification settings. * LDAP specification settings.
*/ */
private Map<String, String> baseEnvironment = new HashMap<>(); private final Map<String, String> baseEnvironment = new HashMap<>();
public String[] getUrls() { public String[] getUrls() {
return this.urls; return this.urls;
...@@ -92,12 +97,16 @@ public class LdapProperties { ...@@ -92,12 +97,16 @@ public class LdapProperties {
this.password = password; this.password = password;
} }
public Map<String, String> getBaseEnvironment() { public boolean getAnonymousReadOnly() {
return this.baseEnvironment; return this.anonymousReadOnly;
} }
public void setBaseEnvironment(Map<String, String> baseEnvironment) { public void setAnonymousReadOnly(boolean anonymousReadOnly) {
this.baseEnvironment = baseEnvironment; this.anonymousReadOnly = anonymousReadOnly;
}
public Map<String, String> getBaseEnvironment() {
return this.baseEnvironment;
} }
public String[] determineUrls(Environment environment) { public String[] determineUrls(Environment environment) {
......
/* /*
* 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,13 +16,12 @@ ...@@ -16,13 +16,12 @@
package org.springframework.boot.autoconfigure.ldap; package org.springframework.boot.autoconfigure.ldap;
import org.junit.After;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.ldap.core.ContextSource; import org.springframework.ldap.core.ContextSource;
import org.springframework.ldap.core.support.LdapContextSource;
import org.springframework.test.util.ReflectionTestUtils; import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
...@@ -31,61 +30,73 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -31,61 +30,73 @@ import static org.assertj.core.api.Assertions.assertThat;
* Tests for {@link LdapAutoConfiguration}. * Tests for {@link LdapAutoConfiguration}.
* *
* @author Eddú Meléndez * @author Eddú Meléndez
* @author Stephane Nicoll
*/ */
public class LdapAutoConfigurationTests { public class LdapAutoConfigurationTests {
private AnnotationConfigApplicationContext context; private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(LdapAutoConfiguration.class));
@After
public void close() {
if (this.context != null) {
this.context.close();
}
}
@Test @Test
public void testDefaultUrl() { public void contextSourceWithDefaultUrl() {
load(); this.contextRunner.run(context -> {
ContextSource contextSource = this.context.getBean(ContextSource.class); LdapContextSource contextSource = context.getBean(LdapContextSource.class);
String[] urls = (String[]) ReflectionTestUtils.getField(contextSource, "urls"); String[] urls = (String[]) ReflectionTestUtils
assertThat(urls).containsExactly("ldap://localhost:389"); .getField(contextSource, "urls");
assertThat(urls).containsExactly("ldap://localhost:389");
assertThat(contextSource.isAnonymousReadOnly()).isFalse();
});
} }
@Test @Test
public void testContextSourceSetOneUrl() { public void contextSourceWithSingleUrl() {
load("spring.ldap.urls:ldap://localhost:123"); this.contextRunner.withPropertyValues("spring.ldap.urls:ldap://localhost:123")
ContextSource contextSource = this.context.getBean(ContextSource.class); .run(context -> {
String[] urls = (String[]) ReflectionTestUtils.getField(contextSource, "urls"); ContextSource contextSource = context.getBean(ContextSource.class);
assertThat(urls).containsExactly("ldap://localhost:123"); String[] urls = (String[]) ReflectionTestUtils.getField(
contextSource, "urls");
assertThat(urls).containsExactly("ldap://localhost:123");
});
} }
@Test @Test
public void testContextSourceSetTwoUrls() { public void contextSourceWithSeveralUrls() {
load("spring.ldap.urls:ldap://localhost:123,ldap://mycompany:123"); this.contextRunner
ContextSource contextSource = this.context.getBean(ContextSource.class); .withPropertyValues(
LdapProperties ldapProperties = this.context.getBean(LdapProperties.class); "spring.ldap.urls:ldap://localhost:123,ldap://mycompany:123")
String[] urls = (String[]) ReflectionTestUtils.getField(contextSource, "urls"); .run(context -> {
assertThat(urls).containsExactly("ldap://localhost:123", "ldap://mycompany:123"); ContextSource contextSource = context.getBean(ContextSource.class);
assertThat(ldapProperties.getUrls()).hasSize(2); LdapProperties ldapProperties = context.getBean(LdapProperties.class);
String[] urls = (String[]) ReflectionTestUtils.getField(
contextSource, "urls");
assertThat(urls).containsExactly("ldap://localhost:123",
"ldap://mycompany:123");
assertThat(ldapProperties.getUrls()).hasSize(2);
});
} }
@Test @Test
public void testContextSourceWithMoreProperties() { public void contextSourceWithExtraCustomization() {
load("spring.ldap.urls:ldap://localhost:123", "spring.ldap.username:root", this.contextRunner
"spring.ldap.password:root", "spring.ldap.base:cn=SpringDevelopers", .withPropertyValues(
"spring.ldap.baseEnvironment.java.naming.security" "spring.ldap.urls:ldap://localhost:123",
+ ".authentication:DIGEST-MD5"); "spring.ldap.username:root",
LdapProperties ldapProperties = this.context.getBean(LdapProperties.class); "spring.ldap.password:secret",
assertThat(ldapProperties.getBaseEnvironment()) "spring.ldap.anonymous-read-only:true",
.containsEntry("java.naming.security.authentication", "DIGEST-MD5"); "spring.ldap.base:cn=SpringDevelopers",
} "spring.ldap.baseEnvironment.java.naming.security.authentication:DIGEST-MD5")
.run(context -> {
private void load(String... properties) { LdapContextSource contextSource = context.getBean(
this.context = new AnnotationConfigApplicationContext(); LdapContextSource.class);
TestPropertyValues.of(properties).applyTo(this.context); assertThat(contextSource.getUserDn()).isEqualTo("root");
this.context.register(LdapAutoConfiguration.class, assertThat(contextSource.getPassword()).isEqualTo("secret");
PropertyPlaceholderAutoConfiguration.class); assertThat(contextSource.isAnonymousReadOnly()).isTrue();
this.context.refresh(); assertThat(contextSource.getBaseLdapPathAsString()).isEqualTo(
"cn=SpringDevelopers");
LdapProperties ldapProperties = context.getBean(LdapProperties.class);
assertThat(ldapProperties.getBaseEnvironment()).containsEntry(
"java.naming.security.authentication", "DIGEST-MD5");
});
} }
} }
...@@ -362,11 +362,12 @@ content into your application. Rather, pick only the properties that you need. ...@@ -362,11 +362,12 @@ content into your application. Rather, pick only the properties that you need.
spring.jersey.type=servlet # Jersey integration type. spring.jersey.type=servlet # Jersey integration type.
# SPRING LDAP ({sc-spring-boot-autoconfigure}/ldap/LdapProperties.{sc-ext}[LdapProperties]) # SPRING LDAP ({sc-spring-boot-autoconfigure}/ldap/LdapProperties.{sc-ext}[LdapProperties])
spring.ldap.urls= # LDAP URLs of the server. spring.ldap.anonymous-read-only=false # Whether read-only operations should use an anonymous environment.
spring.ldap.base= # Base suffix from which all operations should originate. spring.ldap.base= # Base suffix from which all operations should originate.
spring.ldap.username= # Login username of the server.
spring.ldap.password= # Login password of the server.
spring.ldap.base-environment.*= # LDAP specification settings. spring.ldap.base-environment.*= # LDAP specification settings.
spring.ldap.password= # Login password of the server.
spring.ldap.urls= # LDAP URLs of the server.
spring.ldap.username= # Login username of the server.
# EMBEDDED LDAP ({sc-spring-boot-autoconfigure}/ldap/embedded/EmbeddedLdapProperties.{sc-ext}[EmbeddedLdapProperties]) # EMBEDDED LDAP ({sc-spring-boot-autoconfigure}/ldap/embedded/EmbeddedLdapProperties.{sc-ext}[EmbeddedLdapProperties])
spring.ldap.embedded.base-dn= # The base DN spring.ldap.embedded.base-dn= # The base DN
......
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