Skip to content

Commit dcd67f1

Browse files
committed
1 parent 847ae15 commit dcd67f1

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

config/findbugs-exclude.xml

+9
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@
2525

2626

2727
<!-- these specific issues are deliberate design decisions -->
28+
29+
<!-- Deliberately ignoring this, as the check for a null SSLParameters is actually necessary.
30+
See https://jira.mongodb.org/browse/JAVA-2876 for details. -->
31+
<Match>
32+
<Class name="com.mongodb.client.internal.KeyManagementService"/>
33+
<Method name="enableHostNameVerification" params="javax.net.ssl.SSLSocket"/>
34+
<Bug pattern="RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE"/>
35+
</Match>
36+
2837
<!-- Deliberately ignoring this, as many BSONObject subclasses don't do it -->
2938
<Match>
3039
<Package name="com.mongodb"/>

driver-sync/src/main/com/mongodb/client/internal/KeyManagementService.java

+15-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@
2020
import com.mongodb.MongoSocketReadException;
2121
import com.mongodb.MongoSocketWriteException;
2222
import com.mongodb.ServerAddress;
23+
import com.mongodb.internal.connection.SslHelper;
2324

2425
import javax.net.ssl.SSLContext;
26+
import javax.net.ssl.SSLParameters;
27+
import javax.net.ssl.SSLSocket;
2528
import java.io.IOException;
2629
import java.io.InputStream;
2730
import java.io.OutputStream;
@@ -42,14 +45,15 @@ class KeyManagementService {
4245
}
4346

4447
public InputStream stream(final String host, final ByteBuffer message) {
45-
Socket socket;
48+
SSLSocket socket;
4649
try {
47-
socket = sslContext.getSocketFactory().createSocket();
50+
socket = (SSLSocket) sslContext.getSocketFactory().createSocket();
4851
} catch (IOException e) {
4952
throw new MongoSocketOpenException("Exception opening connection to Key Management Service", new ServerAddress(host, port), e);
5053
}
5154

5255
try {
56+
enableHostNameVerification(socket);
5357
socket.setSoTimeout(timeoutMillis);
5458
socket.connect(new InetSocketAddress(InetAddress.getByName(host), port), timeoutMillis);
5559
} catch (IOException e) {
@@ -79,6 +83,15 @@ public InputStream stream(final String host, final ByteBuffer message) {
7983
}
8084
}
8185

86+
private void enableHostNameVerification(final SSLSocket socket) {
87+
SSLParameters sslParameters = socket.getSSLParameters();
88+
if (sslParameters == null) {
89+
sslParameters = new SSLParameters();
90+
}
91+
SslHelper.enableHostNameVerification(sslParameters);
92+
socket.setSSLParameters(sslParameters);
93+
}
94+
8295
public int getPort() {
8396
return port;
8497
}

0 commit comments

Comments
 (0)