Skip to content

Allow port=0 for ApacheDSContainer #8416

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 1 commit into from

Conversation

evgeniycheban
Copy link
Contributor

Fixes gh-8144

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Apr 18, 2020
Copy link
Member

@rwinch rwinch 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. I think there might be some confusion as to what we are looking for. Please see my comment inline.

@@ -143,6 +148,9 @@ public void afterPropertiesSet() throws Exception {
server.setDirectoryService(service);
// AbstractLdapIntegrationTests assume IPv4, so we specify the same here

if (this.port == 0) {
this.port = getRandomPort();
}
Copy link
Member

Choose a reason for hiding this comment

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

Sorry for the confusion. We are already finding a random port in this way within our configuration support.

The problem is sometimes the port gets resolved to an open port and assigned, the port is then taken up by another process or thread, and then this code tries to use the now taken port.

To fix this we want to be able to pass in 0 to TcpTransport to avoid a race condition.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the review. Do I understand correctly that we should also add an accessor to ApacheDSContainer for configured port value i.e 0?

Copy link
Member

Choose a reason for hiding this comment

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

It's not for the configured value, but for the actual port that is resolved by TcpTransport.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it should be getLocalPort for the actual port.

Copy link
Member

Choose a reason for hiding this comment

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

Yes we want the name of the method to be getLocalPort

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Member

@rwinch rwinch 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 quick turnaround. I have provided some feedback inline.


SocketAcceptor socketAcceptor = server.getSocketAcceptor(transport);
InetSocketAddress localAddress = socketAcceptor.getLocalAddress();
this.localPort = localAddress.getPort();
Copy link
Member

Choose a reason for hiding this comment

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

I think this logic should probably be moved to the start method since a user could invoke start directly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should we also move the server initialization to the start method?

Copy link
Member

Choose a reason for hiding this comment

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

Let's try and make as little changes in this PR as possible. If we see additional room for improvement, let's create separate tickets.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Member

@rwinch rwinch left a comment

Choose a reason for hiding this comment

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

Thanks. This looks good. The last changes are to have the code take advantage of these changes. For example, LdapAuthenticationProviderConfigurer can now be updated to use the fact that 0 works.

I'm also wondering if it is actually better to use getPort() to return the used port if 0 is passed in since if the DLS or XML config passes 0, then the port returns the randomly selected port.

@evgeniycheban
Copy link
Contributor Author

@rwinch I made changes to LdapAuthenticationProviderConfigurer.

@evgeniycheban evgeniycheban requested a review from rwinch May 3, 2020 02:04
@evgeniycheban evgeniycheban force-pushed the gh-8144 branch 4 times, most recently from 5c060c6 to ec8cf95 Compare May 3, 2020 14:28
Copy link
Member

@rwinch rwinch 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. We still need changes in LdapServerBeanDefinitionParser. We should also make sure our integration tests are always using port 0 and not use new ServerSocket(0).getLocalPort() anymore.

@evgeniycheban evgeniycheban force-pushed the gh-8144 branch 3 times, most recently from a79cbd8 to 56d154f Compare May 6, 2020 18:09
@evgeniycheban
Copy link
Contributor Author

evgeniycheban commented May 6, 2020

Thanks for the updates. We still need changes in LdapServerBeanDefinitionParser. We should also make sure our integration tests are always using port 0 and not use new ServerSocket(0).getLocalPort() anymore.

@rwinch I made changes to LdapServerBeanDefinitionParser as well.
What should we do with tests that use ServerSocket(0).getLocalPort() to get a list of available ports? For example ApacheDSContainerTests#multipleInstancesSimultanciously.

@evgeniycheban evgeniycheban force-pushed the gh-8144 branch 4 times, most recently from 62586fe to 358251b Compare May 8, 2020 23:54
@evgeniycheban evgeniycheban requested a review from rwinch May 11, 2020 18:59
@evgeniycheban
Copy link
Contributor Author

@rwinch Could you please take a look at the latest changes when you have a moment?

Copy link
Member

@rwinch rwinch 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. I've provided feedback inline

* @param container the embedded DS container
* @return an instance which will connect to the embedded LDAP server
*/
public static DefaultSpringSecurityContextSource createEmbeddedContextSource(EmbeddedDsContainer container) {
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we should need to add this method. I'd prefer to keep as many things private scope as possible.

Copy link
Contributor Author

@evgeniycheban evgeniycheban May 18, 2020

Choose a reason for hiding this comment

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

I added this factory method for XML config because we need to create and post process a bean of the embedded container first and then use the actual port value to build a url.

Copy link
Member

Choose a reason for hiding this comment

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

This can likely be done with a package private method within the config itself.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could you please provide some example of how this can be done in BeanDefinitionParser?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it. Added EmbeddedLdapServerConfigBean with factory method.

* @see ApacheDSContainer
* @see UnboundIdContainer
*/
public interface EmbeddedDsContainer {
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we should need to add a new public interface.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed.

@evgeniycheban evgeniycheban requested a review from rwinch May 19, 2020 09:47
@rwinch rwinch 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 21, 2020
@rwinch rwinch self-assigned this May 21, 2020
@rwinch rwinch added this to the 5.4.0-M2 milestone May 21, 2020
@rwinch
Copy link
Member

rwinch commented May 21, 2020

Thanks for the Pull Request and your patience as we worked on finding the best solution! This is now merged into master via 0fa339f 😄

@rwinch rwinch closed this May 21, 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 type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ApacheDSContainer should allow a zero port
3 participants