Skip to content

Commit 9740b59

Browse files
eleftheriasrwinch
authored andcommitted
Load LDIF file from classpath in unboundId mode
Fixes: gh-7833
1 parent 57fba90 commit 9740b59

File tree

3 files changed

+156
-3
lines changed

3 files changed

+156
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/*
2+
* Copyright 2002-2020 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+
package org.springframework.security.ldap.server;
17+
18+
import org.junit.After;
19+
import org.junit.Test;
20+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
21+
import org.springframework.context.annotation.Bean;
22+
import org.springframework.context.annotation.Configuration;
23+
import org.springframework.ldap.core.ContextSource;
24+
import org.springframework.security.ldap.DefaultSpringSecurityContextSource;
25+
import org.springframework.security.ldap.SpringSecurityLdapTemplate;
26+
27+
import javax.annotation.PreDestroy;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
import static org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown;
31+
32+
/**
33+
* Tests for {@link UnboundIdContainer}, specifically relating to LDIF file detection.
34+
*
35+
* @author Eleftheria Stein
36+
*/
37+
public class UnboundIdContainerLdifTests {
38+
39+
AnnotationConfigApplicationContext appCtx;
40+
41+
@After
42+
public void closeAppContext() {
43+
if (appCtx != null) {
44+
appCtx.close();
45+
appCtx = null;
46+
}
47+
}
48+
49+
@Test
50+
public void unboundIdContainerWhenCustomLdifNameThenLdifLoaded() {
51+
appCtx = new AnnotationConfigApplicationContext(CustomLdifConfig.class);
52+
53+
DefaultSpringSecurityContextSource contextSource = (DefaultSpringSecurityContextSource) appCtx
54+
.getBean(ContextSource.class);
55+
56+
SpringSecurityLdapTemplate template = new SpringSecurityLdapTemplate(contextSource);
57+
assertThat(template.compare("uid=bob,ou=people", "uid", "bob")).isTrue();
58+
}
59+
60+
@Configuration
61+
static class CustomLdifConfig {
62+
private UnboundIdContainer container = new UnboundIdContainer("dc=springframework,dc=org",
63+
"classpath:test-server.ldif");
64+
65+
@Bean
66+
UnboundIdContainer ldapContainer() {
67+
this.container.setPort(0);
68+
return this.container;
69+
}
70+
71+
@Bean
72+
ContextSource contextSource(UnboundIdContainer container) {
73+
return new DefaultSpringSecurityContextSource("ldap://127.0.0.1:"
74+
+ container.getPort() + "/dc=springframework,dc=org");
75+
}
76+
77+
@PreDestroy
78+
void shutdown() {
79+
this.container.stop();
80+
}
81+
}
82+
83+
@Test
84+
public void unboundIdContainerWhenWildcardLdifNameThenLdifLoaded() {
85+
appCtx = new AnnotationConfigApplicationContext(WildcardLdifConfig.class);
86+
87+
DefaultSpringSecurityContextSource contextSource = (DefaultSpringSecurityContextSource) appCtx
88+
.getBean(ContextSource.class);
89+
90+
SpringSecurityLdapTemplate template = new SpringSecurityLdapTemplate(contextSource);
91+
assertThat(template.compare("uid=bob,ou=people", "uid", "bob")).isTrue();
92+
}
93+
94+
@Configuration
95+
static class WildcardLdifConfig {
96+
private UnboundIdContainer container = new UnboundIdContainer("dc=springframework,dc=org",
97+
"classpath*:test-server.ldif");
98+
99+
@Bean
100+
UnboundIdContainer ldapContainer() {
101+
this.container.setPort(0);
102+
return this.container;
103+
}
104+
105+
@Bean
106+
ContextSource contextSource(UnboundIdContainer container) {
107+
return new DefaultSpringSecurityContextSource("ldap://127.0.0.1:"
108+
+ container.getPort() + "/dc=springframework,dc=org");
109+
}
110+
111+
@PreDestroy
112+
void shutdown() {
113+
this.container.stop();
114+
}
115+
}
116+
117+
@Test
118+
public void unboundIdContainerWhenMalformedLdifThenException() {
119+
try {
120+
appCtx = new AnnotationConfigApplicationContext(MalformedLdifConfig.class);
121+
failBecauseExceptionWasNotThrown(IllegalStateException.class);
122+
} catch (Exception e) {
123+
assertThat(e.getCause()).isInstanceOf(IllegalStateException.class);
124+
assertThat(e.getMessage()).contains("Unable to load LDIF classpath:test-server-malformed.txt");
125+
}
126+
}
127+
128+
@Configuration
129+
static class MalformedLdifConfig {
130+
private UnboundIdContainer container = new UnboundIdContainer("dc=springframework,dc=org",
131+
"classpath:test-server-malformed.txt");
132+
133+
@Bean
134+
UnboundIdContainer ldapContainer() {
135+
this.container.setPort(0);
136+
return this.container;
137+
}
138+
139+
@PreDestroy
140+
void shutdown() {
141+
this.container.stop();
142+
}
143+
}
144+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
dn: ou=groups,dc=springframework,dc=org
2+
objectclass: top
3+
objectclass: organizationalUnit
4+
ou: groups
5+
6+
dn ou=subgroups,ou=groups,dc=springframework,dc=org
7+
objectclass: top
8+
objectclass: organizationalUnit
9+
ou: subgroups

ldap/src/main/java/org/springframework/security/ldap/server/UnboundIdContainer.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,10 @@ public void start() {
114114

115115
private void importLdif(InMemoryDirectoryServer directoryServer) {
116116
if (StringUtils.hasText(this.ldif)) {
117-
Resource resource = this.context.getResource(this.ldif);
118117
try {
119-
if (resource.exists()) {
120-
try (InputStream inputStream = resource.getInputStream()) {
118+
Resource[] resources = this.context.getResources(this.ldif);
119+
if (resources.length > 0 && resources[0].exists()) {
120+
try (InputStream inputStream = resources[0].getInputStream()) {
121121
directoryServer.importFromLDIF(false, new LDIFReader(inputStream));
122122
}
123123
}

0 commit comments

Comments
 (0)