Skip to content

GH-8581: Do not overwrite configuration of external provided SshClient #8584

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 2 commits into from

Conversation

azaaiman
Copy link
Contributor

Fixes #8581

Moved configuring an internal provided SshClient to a separate method, which only will execute if isInnerClient is true.
Added a test proving an external configured SshClient can connect to a SshServer.

@@ -35,10 +38,12 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.awaitility.Awaitility.await;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
Copy link
Member

Choose a reason for hiding this comment

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

Error: eckstyle] [ERROR] /home/runner/work/spring-integration/spring-integration/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/session/SftpSessionFactoryTests.java:41:47: Using a static member import should be avoided - org.junit.jupiter.api.Assertions.assertDoesNotThrow. [AvoidStaticImport]

We prefer to use assertion from AsssertJ library. See assertThatNoException() instead of JUnit's one.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, I replaced it.

sftpSessionFactory.setPort(server.getPort());
sftpSessionFactory.setUser("user");

assertDoesNotThrow(() -> sftpSessionFactory.getSession().connect());
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure that this test really reflects what we would expect from its name.
I don't even think that we need to test against the real server and make connections.
There is probably just enough to verify against some mock that its method calls don't happen or so.

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 have thought about using some mock.
But the init code does start a connection and tries to authenticate.

What you would want is something like this:

@Test
void externallyProvidedSshClientShouldNotHaveItsConfigurationOverwritten() throws IOException {
	SshClient externalClient = spy(SshClient.setUpDefaultClient());

	DefaultSftpSessionFactory sftpSessionFactory = new DefaultSftpSessionFactory(externalClient, false);
	sftpSessionFactory.setHost("localhost");
	sftpSessionFactory.setUser("user");

	assertThatNoException().isThrownBy(() -> sftpSessionFactory.getSession());

	verify(externalClient, never()).setServerKeyVerifier(any());
	verify(externalClient, never()).setPasswordIdentityProvider(any());
	verify(externalClient, never()).setKeyIdentityProvider(any());
	verify(externalClient, never()).setUserInteraction(any());
}

But that would raise an exception:

java.lang.IllegalStateException: failed to create SFTP Session
	at org.springframework.integration.sftp.session.DefaultSftpSessionFactory.getSession(DefaultSftpSessionFactory.java:292)
...
Caused by: org.apache.sshd.common.SshException: No more authentication methods available
...
	at org.springframework.integration.sftp.session.DefaultSftpSessionFactory.initClientSession(DefaultSftpSessionFactory.java:319)
	at org.springframework.integration.sftp.session.DefaultSftpSessionFactory.getSession(DefaultSftpSessionFactory.java:282)
	... 89 more
Caused by: org.apache.sshd.common.SshException: No more authentication methods available
	at org.apache.sshd.client.session.ClientUserAuthService.tryNext(ClientUserAuthService.java:379)

I doubt it would be wise to mock the better part of the calls which happen in the init towards the SshClient.

Any suggestions?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah... I see: the integration with that SshClient API is very heavy, therefore it is probably better to exchange a performance of the test in favor of clear code and direct calls instead of our error-prone mocks.
So, never mind then and thank you for your thoughts!
Please, consider to fix an assert in favor of assertThatNoException() - and we are good to merge this.

…ovided SshClient

replace JUnit assertDoesNotThrow by AssertJ assertThatNoException in test
@azaaiman azaaiman requested a review from artembilan March 28, 2023 05:25
@artembilan
Copy link
Member

Merged as 775e6fd and cherry-picked to 6.0.x.

@azaaiman ,

Thank you for contribution; looking forward for more!

@artembilan artembilan closed this Mar 28, 2023
@azaaiman azaaiman deleted the GH-8581 branch March 29, 2023 08:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

External configured SshClient has it's configuration overridden by DefaultSftpSessionFactory
2 participants