From 7c4a70686512dd7a2a172cbc633500d30b165dc5 Mon Sep 17 00:00:00 2001 From: Eleftheria Stein-Kousathana Date: Wed, 22 Jul 2020 18:43:49 +0200 Subject: [PATCH] Throw exception if specified ldif does not exist Closes gh-7791 Co-Authored-By: Shay Dratler --- .../server/UnboundIdContainerLdifTests.java | 50 +++++++++++++++++++ .../ldap/server/UnboundIdContainer.java | 5 +- 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/ldap/src/integration-test/java/org/springframework/security/ldap/server/UnboundIdContainerLdifTests.java b/ldap/src/integration-test/java/org/springframework/security/ldap/server/UnboundIdContainerLdifTests.java index a407a244e8..e5346d56f5 100644 --- a/ldap/src/integration-test/java/org/springframework/security/ldap/server/UnboundIdContainerLdifTests.java +++ b/ldap/src/integration-test/java/org/springframework/security/ldap/server/UnboundIdContainerLdifTests.java @@ -141,4 +141,54 @@ public class UnboundIdContainerLdifTests { this.container.stop(); } } + + @Test + public void unboundIdContainerWhenMissingLdifThenException() { + try { + appCtx = new AnnotationConfigApplicationContext(MissingLdifConfig.class); + failBecauseExceptionWasNotThrown(IllegalStateException.class); + } catch (Exception e) { + assertThat(e.getCause()).isInstanceOf(IllegalStateException.class); + assertThat(e.getMessage()).contains("Unable to load LDIF classpath:does-not-exist.ldif"); + } + } + + @Configuration + static class MissingLdifConfig { + private UnboundIdContainer container = new UnboundIdContainer("dc=springframework,dc=org", + "classpath:does-not-exist.ldif"); + + @Bean + UnboundIdContainer ldapContainer() { + this.container.setPort(0); + return this.container; + } + + @PreDestroy + void shutdown() { + this.container.stop(); + } + } + + @Test + public void unboundIdContainerWhenWildcardLdifNotFoundThenProceeds() { + new AnnotationConfigApplicationContext(WildcardNoLdifConfig.class); + } + + @Configuration + static class WildcardNoLdifConfig { + private UnboundIdContainer container = new UnboundIdContainer("dc=springframework,dc=org", + "classpath*:*.test.ldif"); + + @Bean + UnboundIdContainer ldapContainer() { + this.container.setPort(0); + return this.container; + } + + @PreDestroy + void shutdown() { + this.container.stop(); + } + } } diff --git a/ldap/src/main/java/org/springframework/security/ldap/server/UnboundIdContainer.java b/ldap/src/main/java/org/springframework/security/ldap/server/UnboundIdContainer.java index aaa54ee413..9d5794be27 100644 --- a/ldap/src/main/java/org/springframework/security/ldap/server/UnboundIdContainer.java +++ b/ldap/src/main/java/org/springframework/security/ldap/server/UnboundIdContainer.java @@ -116,7 +116,10 @@ public class UnboundIdContainer implements InitializingBean, DisposableBean, Lif if (StringUtils.hasText(this.ldif)) { try { Resource[] resources = this.context.getResources(this.ldif); - if (resources.length > 0 && resources[0].exists()) { + if (resources.length > 0) { + if (!resources[0].exists()) { + throw new IllegalArgumentException("Unable to find LDIF resource " + this.ldif); + } try (InputStream inputStream = resources[0].getInputStream()) { directoryServer.importFromLDIF(false, new LDIFReader(inputStream)); }