-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Merge changes from tls-channel to prevent accidentally calling SSLEng… #1726
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
Open
vbabanin
wants to merge
5
commits into
mongodb:main
Choose a base branch
from
vbabanin:JAVA-5797-fix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
952d834
Merge changes from tls-channel to prevent accidentally calling SSLEng…
vbabanin a0808f8
Perform handshake after marking handshake started.
vbabanin 745ca96
Add integration test.
vbabanin a7b8855
Add comments.
vbabanin 92188dd
Remove unused import.
vbabanin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,36 +16,52 @@ | |
|
||
package com.mongodb.internal.connection; | ||
|
||
import com.mongodb.ClusterFixture; | ||
import com.mongodb.MongoSocketOpenException; | ||
import com.mongodb.ServerAddress; | ||
import com.mongodb.connection.SocketSettings; | ||
import com.mongodb.connection.SslSettings; | ||
import com.mongodb.internal.TimeoutContext; | ||
import com.mongodb.internal.TimeoutSettings; | ||
import org.bson.ByteBuf; | ||
import org.bson.ByteBufNIO; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
import org.mockito.MockedStatic; | ||
import org.mockito.Mockito; | ||
import org.mockito.invocation.InvocationOnMock; | ||
import org.mockito.stubbing.Answer; | ||
|
||
import javax.net.ssl.SSLContext; | ||
import javax.net.ssl.SSLEngine; | ||
import java.io.IOException; | ||
import java.net.ServerSocket; | ||
import java.nio.ByteBuffer; | ||
import java.nio.channels.InterruptedByTimeoutException; | ||
import java.nio.channels.SocketChannel; | ||
import java.util.Collections; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import static com.mongodb.ClusterFixture.getPrimaryServerDescription; | ||
import static com.mongodb.internal.connection.OperationContext.simpleOperationContext; | ||
import static java.lang.String.format; | ||
import static java.util.concurrent.TimeUnit.MILLISECONDS; | ||
import static java.util.concurrent.TimeUnit.SECONDS; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertInstanceOf; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
import static org.junit.jupiter.api.Assumptions.assumeTrue; | ||
import static org.mockito.ArgumentMatchers.anyInt; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.Mockito.atLeast; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
class TlsChannelStreamFunctionalTest { | ||
private static final SslSettings SSL_SETTINGS = SslSettings.builder().enabled(true).build(); | ||
|
@@ -98,6 +114,7 @@ void shouldEstablishConnection(final int connectTimeoutMs) throws IOException, I | |
try (StreamFactoryFactory streamFactoryFactory = new TlsChannelStreamFactoryFactory(new DefaultInetAddressResolver()); | ||
MockedStatic<SocketChannel> socketChannelMockedStatic = Mockito.mockStatic(SocketChannel.class); | ||
ServerSocket serverSocket = new ServerSocket(0, 1)) { | ||
|
||
SingleResultSpyCaptor<SocketChannel> singleResultSpyCaptor = new SingleResultSpyCaptor<>(); | ||
socketChannelMockedStatic.when(SocketChannel::open).thenAnswer(singleResultSpyCaptor); | ||
|
||
|
@@ -147,4 +164,35 @@ public T answer(final InvocationOnMock invocationOnMock) throws Throwable { | |
private static OperationContext createOperationContext(final int connectTimeoutMs) { | ||
return simpleOperationContext(new TimeoutContext(TimeoutSettings.DEFAULT.withConnectTimeoutMS(connectTimeoutMs))); | ||
} | ||
|
||
@Test | ||
@DisplayName("should not call beginHandshake more than once during TLS session establishment") | ||
void shouldNotCallBeginHandshakeMoreThenOnceDuringTlsSessionEstablishment() throws Exception { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a test case, as upstream didn't include one to cover this change. |
||
assumeTrue(ClusterFixture.getSslSettings().isEnabled()); | ||
|
||
//given | ||
try (StreamFactoryFactory streamFactoryFactory = new TlsChannelStreamFactoryFactory(new DefaultInetAddressResolver())) { | ||
|
||
SSLContext sslContext = Mockito.spy(SSLContext.getDefault()); | ||
SingleResultSpyCaptor<SSLEngine> singleResultSpyCaptor = new SingleResultSpyCaptor<>(); | ||
when(sslContext.createSSLEngine(anyString(), anyInt())).thenAnswer(singleResultSpyCaptor); | ||
|
||
StreamFactory streamFactory = streamFactoryFactory.create( | ||
SocketSettings.builder().build(), | ||
SslSettings.builder(ClusterFixture.getSslSettings()) | ||
.context(sslContext) | ||
.build()); | ||
|
||
Stream stream = streamFactory.create(getPrimaryServerDescription().getAddress()); | ||
stream.open(ClusterFixture.OPERATION_CONTEXT); | ||
ByteBuf wrap = new ByteBufNIO(ByteBuffer.wrap(new byte[]{1, 3, 4})); | ||
|
||
//when | ||
stream.write(Collections.singletonList(wrap), ClusterFixture.OPERATION_CONTEXT); | ||
|
||
//then | ||
SECONDS.sleep(5); | ||
verify(singleResultSpyCaptor.getResult(), times(1)).beginHandshake(); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Setting handshakeCompleted to true immediately may lead to an inconsistent handshake state if the subsequent session initialization callback fails. Consider moving this assignment to after a successful execution of the callback or resetting it on failure.
Copilot uses AI. Check for mistakes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps a bad recommendation? However, best to confirm if
initSessionCallback
could be called twice and if that has any ramifications.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think in this case the Copilot suggestion does not apply.
Based on the
TLSChannelImpl
javadoc, the callback is only invoked when the TLS session is established or re-established:"Register a callback function to be executed when the TLS session is established (or re-established)..."
mongo-java-driver/driver-core/src/main/com/mongodb/internal/connection/tlschannel/TlsChannelBuilder.java
Line 93 in 7cc4be2
If the callback fails, it doesn't indicate that the session was not established or in partial state. Internally, we still mark the handshake as completed to prevent re-establishing the session again on the next read or write. So the callback doesn’t control session validity - it's more of a post-handshake hook. Also, we rely on the default session callback, which is a no-op. Reference:
mongo-java-driver/driver-core/src/main/com/mongodb/internal/connection/tlschannel/TlsChannelBuilder.java
Line 34 in 7cc4be2
Given this, I believe we are okay and we can copy the logic from the upstream.