Skip to content

Commit 9411374

Browse files
committed
CDRIVER-3428 enable TLS for any TLS options
tlsInsecure and tlsCertificateKeyFilePassword were missing.
1 parent f387ff0 commit 9411374

File tree

5 files changed

+48
-3
lines changed

5 files changed

+48
-3
lines changed

src/libmongoc/doc/mongoc_uri_get_ssl.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,5 @@ Fetches a boolean indicating if TLS was specified for use in the URI.
3434
Returns
3535
-------
3636

37-
Returns a boolean, true indicating that TLS should be used.
37+
Returns a boolean, true indicating that TLS should be used. This returns true if *any* :ref:`TLS option <tls_options>` is specified.
3838

src/libmongoc/doc/mongoc_uri_get_tls.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ Fetches a boolean indicating if TLS was specified for use in the URI.
2424
Returns
2525
-------
2626

27-
Returns a boolean, true indicating that TLS should be used.
27+
Returns a boolean, true indicating that TLS should be used. This returns true if *any* :ref:`TLS option <tls_options>` is specified.
2828

src/libmongoc/doc/mongoc_uri_t.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ MONGOC_URI_CANONICALIZEHOSTNAME canonicalizehostname Use
125125
MONGOC_URI_GSSAPISERVICENAME gssapiservicename Use alternative service name. The default is ``mongodb``.
126126
========================================== ================================= =========================================================================================================================================================================================================================
127127

128+
129+
.. _tls_options:
130+
128131
TLS Options
129132
-----------
130133

src/libmongoc/src/mongoc/mongoc-uri.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2242,7 +2242,10 @@ mongoc_uri_get_tls (const mongoc_uri_t *uri) /* IN */
22422242
if (bson_has_field (&uri->options, MONGOC_URI_TLSCERTIFICATEKEYFILE) ||
22432243
bson_has_field (&uri->options, MONGOC_URI_TLSCAFILE) ||
22442244
bson_has_field (&uri->options, MONGOC_URI_TLSALLOWINVALIDCERTIFICATES) ||
2245-
bson_has_field (&uri->options, MONGOC_URI_TLSALLOWINVALIDHOSTNAMES)) {
2245+
bson_has_field (&uri->options, MONGOC_URI_TLSALLOWINVALIDHOSTNAMES) ||
2246+
bson_has_field (&uri->options, MONGOC_URI_TLSINSECURE) ||
2247+
bson_has_field (&uri->options,
2248+
MONGOC_URI_TLSCERTIFICATEKEYFILEPASSWORD)) {
22462249
return true;
22472250
}
22482251

src/libmongoc/tests/test-mongoc-uri.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2452,6 +2452,42 @@ test_mongoc_uri_duplicates (void)
24522452
mongoc_uri_destroy (uri);
24532453
}
24542454

2455+
static void
2456+
test_one_tls_option_enables_tls ()
2457+
{
2458+
const char *opts[] = {MONGOC_URI_TLS "=true",
2459+
MONGOC_URI_TLSCERTIFICATEKEYFILE "=file.pem",
2460+
MONGOC_URI_TLSCERTIFICATEKEYFILEPASSWORD "=file.pem",
2461+
MONGOC_URI_TLSCAFILE "=file.pem",
2462+
MONGOC_URI_TLSALLOWINVALIDCERTIFICATES "=true",
2463+
MONGOC_URI_TLSALLOWINVALIDHOSTNAMES "=true",
2464+
MONGOC_URI_TLSINSECURE "=true",
2465+
MONGOC_URI_SSL "=true",
2466+
MONGOC_URI_SSLCLIENTCERTIFICATEKEYFILE "=file.pem",
2467+
MONGOC_URI_SSLCLIENTCERTIFICATEKEYPASSWORD "=file.pem",
2468+
MONGOC_URI_SSLCERTIFICATEAUTHORITYFILE "=file.pem",
2469+
MONGOC_URI_SSLALLOWINVALIDCERTIFICATES "=true",
2470+
MONGOC_URI_SSLALLOWINVALIDHOSTNAMES "=true"};
2471+
int i;
2472+
2473+
for (i = 0; i < sizeof (opts) / sizeof (opts[0]); i++) {
2474+
mongoc_uri_t *uri;
2475+
bson_error_t error;
2476+
char *uri_string;
2477+
2478+
uri_string =
2479+
bson_strdup_printf ("mongodb://localhost:27017/?%s", opts[i]);
2480+
uri = mongoc_uri_new_with_error (uri_string, &error);
2481+
bson_free (uri_string);
2482+
ASSERT_OR_PRINT (uri, error);
2483+
if (!mongoc_uri_get_tls (uri)) {
2484+
test_error (
2485+
"unexpected tls not enabled when following option set: %s\n",
2486+
opts[i]);
2487+
}
2488+
mongoc_uri_destroy (uri);
2489+
}
2490+
}
24552491

24562492
void
24572493
test_uri_install (TestSuite *suite)
@@ -2482,4 +2518,7 @@ test_uri_install (TestSuite *suite)
24822518
TestSuite_Add (suite, "/Uri/dns_options", test_mongoc_uri_dns_options);
24832519
TestSuite_Add (suite, "/Uri/utf8", test_mongoc_uri_utf8);
24842520
TestSuite_Add (suite, "/Uri/duplicates", test_mongoc_uri_duplicates);
2521+
TestSuite_Add (suite,
2522+
"/Uri/one_tls_option_enables_tls",
2523+
test_one_tls_option_enables_tls);
24852524
}

0 commit comments

Comments
 (0)