Skip to content

Commit 9554b67

Browse files
committed
Add support for new TLS-related connection string options
* tls=true|false * tlsInsecure=true|false * tlsAllowInvalidHostnames=true|false JAVA-3066
1 parent 0eb7ee4 commit 9554b67

File tree

3 files changed

+143
-9
lines changed

3 files changed

+143
-9
lines changed

driver-core/src/main/com/mongodb/ConnectionString.java

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,22 @@
105105
* all members of the set.</li>
106106
* </ul>
107107
* <p>Connection Configuration:</p>
108-
* <p>Connection Configuration:</p>
109108
* <ul>
110-
* <li>{@code streamType=nio2|netty}: The stream type to use for connections. If unspecified, nio2 will be used for asynchronous
111-
* clients. Note that this query parameter has been deprecated and applications should use
112-
* {@link MongoClientSettings.Builder#streamFactoryFactory(StreamFactoryFactory)} instead.</li>
113-
* <li>{@code ssl=true|false}: Whether to connect using SSL.</li>
114-
* <li>{@code sslInvalidHostNameAllowed=true|false}: Whether to allow invalid host names for SSL connections.</li>
109+
* <li>{@code ssl=true|false}: Whether to connect using TLS.</li>
110+
* <li>{@code tls=true|false}: Whether to connect using TLS. Supersedes the ssl option</li>
111+
* <li>{@code tlsInsecure=true|false}: If connecting with TLS, this option enables insecure TLS connections. Currently this has the
112+
* same effect of setting tlsAllowInvalidHostnames to true. Other mechanism for relaxing TLS security constraints must be handled in
113+
* the application by customizing the {@link javax.net.ssl.SSLContext}</li>
114+
* <li>{@code sslInvalidHostNameAllowed=true|false}: Whether to allow invalid host names for TLS connections.</li>
115+
* <li>{@code tlsAllowInvalidHostnames=true|false}: Whether to allow invalid host names for TLS connections. Supersedes the
116+
* sslInvalidHostNameAllowed option</li>
115117
* <li>{@code connectTimeoutMS=ms}: How long a connection can take to be opened before timing out.</li>
116118
* <li>{@code socketTimeoutMS=ms}: How long a send or receive on a socket can take before timing out.</li>
117119
* <li>{@code maxIdleTimeMS=ms}: Maximum idle time of a pooled connection. A connection that exceeds this limit will be closed</li>
118120
* <li>{@code maxLifeTimeMS=ms}: Maximum life time of a pooled connection. A connection that exceeds this limit will be closed</li>
121+
* <li>{@code streamType=nio2|netty}: The stream type to use for connections. If unspecified, nio2 will be used for asynchronous
122+
* clients. Note that this query parameter has been deprecated and applications should use
123+
* {@link MongoClientSettings.Builder#streamFactoryFactory(StreamFactoryFactory)} instead.</li>
119124
* </ul>
120125
* <p>Connection pool configuration:</p>
121126
* <ul>
@@ -396,11 +401,23 @@ public ConnectionString(final String connectionString) {
396401
GENERAL_OPTIONS_KEYS.add("maxidletimems");
397402
GENERAL_OPTIONS_KEYS.add("maxlifetimems");
398403
GENERAL_OPTIONS_KEYS.add("sockettimeoutms");
404+
405+
// Order matters here: Having tls after ssl means than the tls option will supersede the ssl option when both are set
399406
GENERAL_OPTIONS_KEYS.add("ssl");
400-
GENERAL_OPTIONS_KEYS.add("streamtype");
407+
GENERAL_OPTIONS_KEYS.add("tls");
408+
409+
// Order matters here: Having tlsinsecure before sslinvalidhostnameallowed and tlsallowinvalidhostnames means that those options
410+
// will supersede this one when both are set.
411+
GENERAL_OPTIONS_KEYS.add("tlsinsecure");
412+
413+
// Order matters here: Having tlsallowinvalidhostnames after sslinvalidhostnameallowed means than the tlsallowinvalidhostnames
414+
// option will supersede the sslinvalidhostnameallowed option when both are set
401415
GENERAL_OPTIONS_KEYS.add("sslinvalidhostnameallowed");
416+
GENERAL_OPTIONS_KEYS.add("tlsallowinvalidhostnames");
417+
402418
GENERAL_OPTIONS_KEYS.add("replicaset");
403419
GENERAL_OPTIONS_KEYS.add("readconcernlevel");
420+
GENERAL_OPTIONS_KEYS.add("streamtype");
404421

405422
GENERAL_OPTIONS_KEYS.add("serverselectiontimeoutms");
406423
GENERAL_OPTIONS_KEYS.add("localthresholdms");
@@ -479,10 +496,16 @@ private void translateOptions(final Map<String, List<String>> optionsMap) {
479496
connectTimeout = parseInteger(value, "connecttimeoutms");
480497
} else if (key.equals("sockettimeoutms")) {
481498
socketTimeout = parseInteger(value, "sockettimeoutms");
499+
} else if (key.equals("tlsallowinvalidhostnames")) {
500+
sslInvalidHostnameAllowed = parseBoolean(value, "tlsAllowInvalidHostnames");
482501
} else if (key.equals("sslinvalidhostnameallowed")) {
483502
sslInvalidHostnameAllowed = parseBoolean(value, "sslinvalidhostnameallowed");
503+
} else if (key.equals("tlsinsecure")) {
504+
sslInvalidHostnameAllowed = parseBoolean(value, "tlsinsecure");
484505
} else if (key.equals("ssl")) {
485506
sslEnabled = parseBoolean(value, "ssl");
507+
} else if (key.equals("tls")) {
508+
sslEnabled = parseBoolean(value, "tls");
486509
} else if (key.equals("streamtype")) {
487510
streamType = value;
488511
LOGGER.warn("The streamType query parameter is deprecated and support for it will be removed"

driver-core/src/test/unit/com/mongodb/ConnectionStringSpecification.groovy

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,110 @@ class ConnectionStringSpecification extends Specification {
195195
type << ['amp', 'semi', 'mixed']
196196
}
197197

198+
def 'should parse options to enable TLS'() {
199+
when:
200+
def connectionString = new ConnectionString('mongodb://localhost/?ssl=false')
201+
202+
then:
203+
connectionString.getSslEnabled() == false
204+
205+
when:
206+
connectionString = new ConnectionString('mongodb://localhost/?ssl=true')
207+
208+
then:
209+
connectionString.getSslEnabled()
210+
211+
when:
212+
connectionString = new ConnectionString('mongodb://localhost/?tls=false')
213+
214+
then:
215+
connectionString.getSslEnabled() == false
216+
217+
when:
218+
connectionString = new ConnectionString('mongodb://localhost/?tls=true')
219+
220+
then:
221+
connectionString.getSslEnabled()
222+
223+
when:
224+
connectionString = new ConnectionString('mongodb://localhost/?tls=true&ssl=false')
225+
226+
then:
227+
connectionString.getSslEnabled()
228+
229+
when:
230+
connectionString = new ConnectionString('mongodb://localhost/?tls=false&ssl=true')
231+
232+
then:
233+
connectionString.getSslEnabled() == false
234+
}
235+
236+
def 'should parse options to enable TLS invalid host names'() {
237+
when:
238+
def connectionString = new ConnectionString('mongodb://localhost/?ssl=true&sslInvalidHostNameAllowed=false')
239+
240+
then:
241+
connectionString.getSslInvalidHostnameAllowed() == false
242+
243+
when:
244+
connectionString = new ConnectionString('mongodb://localhost/?ssl=true&sslInvalidHostNameAllowed=true')
245+
246+
then:
247+
connectionString.getSslInvalidHostnameAllowed()
248+
249+
when:
250+
connectionString = new ConnectionString('mongodb://localhost/?tls=true&tlsAllowInvalidHostnames=false')
251+
252+
then:
253+
connectionString.getSslInvalidHostnameAllowed() == false
254+
255+
when:
256+
connectionString = new ConnectionString('mongodb://localhost/?tls=true&tlsAllowInvalidHostnames=true')
257+
258+
then:
259+
connectionString.getSslInvalidHostnameAllowed()
260+
261+
when:
262+
connectionString = new ConnectionString(
263+
'mongodb://localhost/?tls=true&tlsAllowInvalidHostnames=false&sslInvalidHostNameAllowed=true')
264+
265+
then:
266+
connectionString.getSslInvalidHostnameAllowed() == false
267+
268+
when:
269+
connectionString = new ConnectionString(
270+
'mongodb://localhost/?tls=true&tlsAllowInvalidHostnames=true&sslInvalidHostNameAllowed=false')
271+
272+
then:
273+
connectionString.getSslInvalidHostnameAllowed()
274+
}
275+
276+
def 'should parse options to enable unsecured TLS'() {
277+
when:
278+
def connectionString = new ConnectionString('mongodb://localhost/?tls=true&tlsInsecure=true')
279+
280+
then:
281+
connectionString.getSslInvalidHostnameAllowed()
282+
283+
when:
284+
connectionString = new ConnectionString('mongodb://localhost/?tls=true&tlsInsecure=false')
285+
286+
then:
287+
connectionString.getSslInvalidHostnameAllowed() == false
288+
289+
when:
290+
connectionString = new ConnectionString('mongodb://localhost/?tls=true&tlsInsecure=true&tlsAllowInvalidHostnames=false')
291+
292+
then:
293+
connectionString.getSslInvalidHostnameAllowed() == false
294+
295+
when:
296+
connectionString = new ConnectionString('mongodb://localhost/?tls=true&tlsInsecure=false&tlsAllowInvalidHostnames=true')
297+
298+
then:
299+
connectionString.getSslInvalidHostnameAllowed()
300+
}
301+
198302
@Unroll
199303
def 'should throw IllegalArgumentException when the string #cause'() {
200304
when:
@@ -247,6 +351,7 @@ class ConnectionStringSpecification extends Specification {
247351
connectionString.getReadPreference() == null;
248352
connectionString.getRequiredReplicaSetName() == null
249353
connectionString.getSslEnabled() == null
354+
connectionString.getSslInvalidHostnameAllowed() == null
250355
connectionString.getStreamType() == null
251356
connectionString.getApplicationName() == null
252357
connectionString.getCompressorList() == []

driver-legacy/src/main/com/mongodb/MongoClientURI.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,14 @@
8989
*
9090
* <p>Connection Configuration:</p>
9191
* <ul>
92-
* <li>{@code ssl=true|false}: Whether to connect using SSL.</li>
93-
* <li>{@code sslInvalidHostNameAllowed=true|false}: Whether to allow invalid host names for SSL connections.</li>
92+
* <li>{@code ssl=true|false}: Whether to connect using TLS.</li>
93+
* <li>{@code tls=true|false}: Whether to connect using TLS. Supersedes the ssl option</li>
94+
* <li>{@code tlsInsecure=true|false}: If connecting with TLS, this option enables insecure TLS connections. Currently this has the
95+
* same effect of setting tlsAllowInvalidHostnames to true. Other mechanism for relaxing TLS security constraints must be handled in
96+
* the application by customizing the {@link javax.net.ssl.SSLContext}</li>
97+
* <li>{@code sslInvalidHostNameAllowed=true|false}: Whether to allow invalid host names for TLS connections.</li>
98+
* <li>{@code tlsAllowInvalidHostnames=true|false}: Whether to allow invalid host names for TLS connections. Supersedes the
99+
* sslInvalidHostNameAllowed option</li>
94100
* <li>{@code connectTimeoutMS=ms}: How long a connection can take to be opened before timing out.</li>
95101
* <li>{@code socketTimeoutMS=ms}: How long a send or receive on a socket can take before timing out.</li>
96102
* <li>{@code maxIdleTimeMS=ms}: Maximum idle time of a pooled connection. A connection that exceeds this limit will be closed</li>

0 commit comments

Comments
 (0)