Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
jyemin committed Feb 18, 2021
1 parent 32052a1 commit ae5b1c0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
9 changes: 9 additions & 0 deletions config/findbugs-exclude.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@


<!-- these specific issues are deliberate design decisions -->

<!-- Deliberately ignoring this, as the check for a null SSLParameters is actually necessary.
See https://jira.mongodb.org/browse/JAVA-2876 for details. -->
<Match>
<Class name="com.mongodb.client.internal.KeyManagementService"/>
<Method name="enableHostNameVerification" params="javax.net.ssl.SSLSocket"/>
<Bug pattern="RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE"/>
</Match>

<!-- Deliberately ignoring this, as many BSONObject subclasses don't do it -->
<Match>
<Package name="com.mongodb"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
package com.mongodb.client.internal;

import com.mongodb.ServerAddress;
import com.mongodb.internal.connection.SslHelper;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLParameters;
import javax.net.ssl.SSLSocket;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand All @@ -38,9 +41,10 @@ class KeyManagementService {

public InputStream stream(final String host, final ByteBuffer message) throws IOException {
ServerAddress serverAddress = host.contains(":") ? new ServerAddress(host) : new ServerAddress(host, defaultPort);
Socket socket = sslContext.getSocketFactory().createSocket();
SSLSocket socket = (SSLSocket) sslContext.getSocketFactory().createSocket();

try {
enableHostNameVerification(socket);
socket.setSoTimeout(timeoutMillis);
socket.connect(serverAddress.getSocketAddress(), timeoutMillis);
} catch (IOException e) {
Expand Down Expand Up @@ -68,6 +72,15 @@ public InputStream stream(final String host, final ByteBuffer message) throws IO
}
}

private void enableHostNameVerification(final SSLSocket socket) {
SSLParameters sslParameters = socket.getSSLParameters();
if (sslParameters == null) {
sslParameters = new SSLParameters();
}
SslHelper.enableHostNameVerification(sslParameters);
socket.setSSLParameters(sslParameters);
}

public int getDefaultPort() {
return defaultPort;
}
Expand Down

0 comments on commit ae5b1c0

Please sign in to comment.