Skip to content

Throw exception when specified ldif file does not exist #8434

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.security.ldap;

import com.unboundid.ldap.listener.InMemoryDirectoryServer;
import com.unboundid.ldap.sdk.LDAPException;
import org.junit.Test;


/**
* @Auther - dratler
*/
public class SpringLdapFalseAuthTest {


@Test(expected = IllegalArgumentException.class)
public void testInMemoryDirectoryServerInvalidLdifFile() {
//TODO - get exception here in case of invalid root base
try {
new InMemoryDirectoryServer("dc=springframework,dc=org",
"classpath:missing-file.ldif");
} catch (LDAPException e) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An IllegalArgumentException is more suitable in this case.
LDAPException should only be used when a problem occurs while performing LDAP-related processing.
In this case, we haven't started the LDAP-related processing yet, so we should use IllegalArgumentException.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The InMemoryDirectoryServer throw LDAPException I can wrap it but I think it's not a good practice.
public InMemoryDirectoryServer(final String... baseDNs) throws LDAPException

throw new IllegalArgumentException(e);
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@
*/
public class UnboundIdContainerTests {

private final String validLdifClassPath = "classpath:test-server.ldif";
private final String validRootDn = "dc=springframework,dc=org";

@Test
public void startLdapServer() throws Exception {
UnboundIdContainer server = new UnboundIdContainer("dc=springframework,dc=org",
"classpath:test-server.ldif");
UnboundIdContainer server = new UnboundIdContainer(validLdifClassPath,
validRootDn);
server.setApplicationContext(new GenericApplicationContext());
List<Integer> ports = getDefaultPorts(1);
server.setPort(ports.get(0));
Expand All @@ -47,9 +50,10 @@ public void startLdapServer() throws Exception {
}
}


@Test
public void afterPropertiesSetWhenPortIsZeroThenRandomPortIsSelected() throws Exception {
UnboundIdContainer server = new UnboundIdContainer("dc=springframework,dc=org", null);
UnboundIdContainer server = new UnboundIdContainer(validLdifClassPath, validRootDn);
server.setPort(0);

try {
Expand Down Expand Up @@ -77,4 +81,38 @@ private List<Integer> getDefaultPorts(int count) throws IOException {
}
}

@Test(expected = IllegalArgumentException.class)
public void testInvalidLdapFile(){
UnboundIdContainer server = new UnboundIdContainer(validRootDn, "classpath:missing-file.ldif");
server.setPort(0);
try {
server.start();

}catch (Exception e){} finally {
server.destroy();
}
}

@Test(expected = IllegalArgumentException.class)
public void testInvalidNullLdapFile(){
new UnboundIdContainer(validRootDn, null);
}

@Test(expected = IllegalArgumentException.class)
public void testInvalidEmptyPathLdapFile(){
new UnboundIdContainer(validRootDn, "");
}

@Test(expected = IllegalArgumentException.class)
public void testInvalidRootDnLdapFile(){
UnboundIdContainer server = new UnboundIdContainer("dc=fake,dc=org", validLdifClassPath);
server.setPort(0);
try {
server.start();

}catch (Exception e){} finally {
server.destroy();
}
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test for the original issue.
The test should create an UnboundIdContainer with an LDIF that does not exist, and ensure that an exception is thrown.

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.springframework.security.ldap.server;

import java.io.File;
import java.io.InputStream;

import com.unboundid.ldap.listener.InMemoryDirectoryServer;
Expand All @@ -24,7 +25,6 @@
import com.unboundid.ldap.sdk.Entry;
import com.unboundid.ldap.sdk.LDAPException;
import com.unboundid.ldif.LDIFReader;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
Expand Down Expand Up @@ -54,9 +54,31 @@ public class UnboundIdContainer implements InitializingBean, DisposableBean, Lif

public UnboundIdContainer(String defaultPartitionSuffix, String ldif) {
this.defaultPartitionSuffix = defaultPartitionSuffix;
setLdif(ldif);
}

public String getLdif() {
return ldif;
}

public void setLdif(String ldif) {
if (!StringUtils.hasText(ldif)) {
throw new IllegalArgumentException("ldif file value is missing");
}
checkFilePath(ldif);
this.ldif = ldif;
}

private void checkFilePath(String ldif) {
File ldifFile = new File(ldif);
if (ldifFile.exists()) {
throw new IllegalArgumentException("the requested file not found within given path");
}
if (ldifFile.isFile()) {
throw new IllegalArgumentException("the given path is not a file");
}
}

public int getPort() {
return this.port;
}
Expand Down Expand Up @@ -113,9 +135,10 @@ public void start() {
}

private void importLdif(InMemoryDirectoryServer directoryServer) {
//TODO - would like to remove this check I think set and get are the best place to handle these*
if (StringUtils.hasText(this.ldif)) {
try {
Resource[] resources = this.context.getResources(this.ldif);
Resource[] resources = this.context.getResources(getLdif());
if (resources.length > 0 && resources[0].exists()) {
try (InputStream inputStream = resources[0].getInputStream()) {
directoryServer.importFromLDIF(false, new LDIFReader(inputStream));
Expand Down