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

Conversation

dratler
Copy link
Contributor

@dratler dratler commented Apr 25, 2020

Hi @eleftherias

Thanks for all the aid here is a PR I have opened with tests that I understand is missing and once they will throw exceptions this PR will be completed.

Copy link
Contributor

@eleftherias eleftherias left a comment

Choose a reason for hiding this comment

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

Thanks for the PR @dratler!This is a great starting point.
I have left some comments inline.
I suggest adding the test to the UnboundIdContainerTests file, rather than creating a new one.

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

@eleftherias eleftherias changed the title PR 7791 Throw exception when specified ldif file does not exist Apr 30, 2020
1. move the tests into UnboundIdContainerTests
2. Starting phase one of the commit
@dratler
Copy link
Contributor Author

dratler commented May 2, 2020

Hi @eleftherias ,
I have created a first draft of how to do this PR .
can you check if I'm on the right path ?

Copy link
Contributor

@eleftherias eleftherias left a comment

Choose a reason for hiding this comment

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

Thanks for the updates @dratler.
I have left feedback inline.

Copy link
Contributor

@eleftherias eleftherias left a comment

Choose a reason for hiding this comment

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

Thanks for the updates @dratler!
I have requested some changes inline.

if (!StringUtils.hasText(ldif)) {
throw new IllegalArgumentException("Unable to load LDIF " + ldif);
}
if (!ldif.endsWith(".ldif")) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please remove this check. We do not require the file to have a ".ldif" extension to be processed.

@@ -67,7 +76,9 @@ public void setPort(int port) {

@Override
public void destroy() {
stop();
if (null != directoryServer) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's keep this commit focused on throwing an exception with the LDIF file does not exist.
This doesn't seem like it is part of that.

Comment on lines 131 to 134
if (resources.length > 0
&& resources[0].exists()
&& resources[0].isFile()
&& resources[0].isReadable()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

With these changes, there is still no exception thrown when the file does not exist.
We need to check if the LDIF exists and throw an exception if it does not.
We can add the check for isFile() and isReadable() after we verify it exists.

Suggested change
if (resources.length > 0
&& resources[0].exists()
&& resources[0].isFile()
&& resources[0].isReadable()) {
if (resources.length > 0) {
if (!resources[0].exists()) {
throw new IllegalArgumentException("Could not find LDIF " + this.ldif);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will do

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.

Copy link
Contributor

@eleftherias eleftherias left a comment

Choose a reason for hiding this comment

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

Thanks for the updates @dratler.
I have left some more comments inline.

@eleftherias eleftherias added in: ldap An issue in spring-security-ldap type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels May 27, 2020
@dratler
Copy link
Contributor Author

dratler commented May 27, 2020

Hi @eleftherias ,
Sorry about the delay but I think this is the right one

@eleftherias
Copy link
Contributor

@dratler It looks like the tests are still failing. Were you able to run the build on your local machine?

@dratler
Copy link
Contributor Author

dratler commented May 28, 2020

Hi @eleftherias ,
I have run it on my local machine twice before pushing it.
I didn't have any issues.
Let me check the error and I will work on them.
Sorry about thta

@eleftherias
Copy link
Contributor

Thanks @dratler. You can also check out the Travis build that ran for this PR https://travis-ci.org/github/spring-projects/spring-security/builds/691902029

@dratler
Copy link
Contributor Author

dratler commented Jun 1, 2020

Hi @eleftherias ,
I have an issue I would like to advise with you.
First I would like to applogize about the previous commit after cleaning my machine I was able to reproduce it.
When performing ./gradlew build clean I'm getting execution regarding LdapServerBeanDefinitionParserTests. apacheDirectoryServerIsStartedByDefault related to Execution failed for task ':spring-security-itest-ldap-embedded-mode-unboundid:integrationTest'.
but the Stack trace indicates the following: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.unboundidServerContainer': Invocation of init method failed; nested exception is java.lang.IllegalStateException: Unable to load LDIF classpath:users.ldif at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1796) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:895) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85) at org.springframework.security.LdapServerBeanDefinitionParserTests.setup(LdapServerBeanDefinitionParserTests.java:38) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.runTestClass(JUnitTestClassExecutor.java:110) at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:58) at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:38) at org.gradle.api.internal.tasks.testing.junit.AbstractJUnitTestClassProcessor.processTestClass(AbstractJUnitTestClassProcessor.java:62) at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:51) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36) at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24) at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:33) at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:94) at com.sun.proxy.$Proxy2.processTestClass(Unknown Source) at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:118) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36) at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24) at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:182) at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:164) at org.gradle.internal.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:413) at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:64) at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:48) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:56) at java.lang.Thread.run(Thread.java:748) Caused by: java.lang.IllegalStateException: Unable to load LDIF classpath:users.ldif at org.springframework.security.ldap.server.UnboundIdContainer.importLdif(UnboundIdContainer.java:133) at org.springframework.security.ldap.server.UnboundIdContainer.start(UnboundIdContainer.java:111) at org.springframework.security.ldap.server.UnboundIdContainer.afterPropertiesSet(UnboundIdContainer.java:82) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1855) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1792) ... 60 more Caused by: java.lang.IllegalArgumentException: Unable to load LDIF :classpath:users.ldif at org.springframework.security.ldap.server.UnboundIdContainer.lambda$locateResource$1(UnboundIdContainer.java:150) at java.util.Optional.orElseThrow(Optional.java:290) at org.springframework.security.ldap.server.UnboundIdContainer.locateResource(UnboundIdContainer.java:150) at org.springframework.security.ldap.server.UnboundIdContainer.importLdif(UnboundIdContainer.java:128) ... 64 more
and there are 5 option can you help or advice how to debug that issue ?
I tried running these manually and they seem to be working and I want to resolve the issue

@eleftherias
Copy link
Contributor

Thanks for reaching out @dratler.
Here is why the test apacheDirectoryServerIsStartedByDefault is throwing an exception:

The updated code this.context.getResources(CLASSPATH_ALL_URL_PREFIX) is getting multiple resources from the classpath, not just LDIFs.
Then Objects.requireNonNull(resource.getFilename()).equalsIgnoreCase(this.ldif) is trying to match the resources to the provided LDIF pattern which is "classpath:users.ldif", but no file is named that, the file in this example is simply "users.ldif".

To mitigate this, we do not need to get all resources, just the LDIFs that the user has specified with their pattern.

Resource[] resources = this.context.getResources(this.ldif);

Then, if the resource they are asking for does not exist, we should throw an exception.

if (resources.length > 0) {
	if (!resources[0].exists()) {
			throw new IllegalArgumentException("Could not find LDIF " + this.ldif);
	}
	// ...

Let me know if this is unclear or if you have any questions.

@dratler
Copy link
Contributor Author

dratler commented Jun 8, 2020

Hi @eleftherias ,
I'm checking to see if a fix I have to build is working please ignore this commit.
thank you

@dratler
Copy link
Contributor Author

dratler commented Jun 21, 2020

Hi @eleftherias ,
I think I have completed this assignment I'm sorry it took a while, please review it and I will comply according to you comment.
thank you for the time and all the help.

Copy link
Contributor

@eleftherias eleftherias left a comment

Choose a reason for hiding this comment

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

Thanks for the updates @dratler.
I have left some more feedback inline.

@dratler
Copy link
Contributor Author

dratler commented Jul 18, 2020

Hi @eleftherias ,
I think I'm done.
please review this PR thank you for all the inputs.

@dratler dratler requested a review from eleftherias July 18, 2020 18:53
@eleftherias
Copy link
Contributor

Thanks @dratler! The changes have been merged to master via 7c4a706.

@eleftherias eleftherias added the status: duplicate A duplicate of another issue label Jul 22, 2020
@eleftherias eleftherias added this to the 5.4.0-RC1 milestone Jul 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: ldap An issue in spring-security-ldap status: duplicate A duplicate of another issue type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants