Skip to content

Commit 365c53f

Browse files
committed
CDRIVER-737: Improve SCRAM-SHA-1 failure message
1 parent f7cb8c4 commit 365c53f

File tree

5 files changed

+60
-8
lines changed

5 files changed

+60
-8
lines changed

build/autotools/CheckSSL.m4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
AC_ARG_ENABLE([ssl],
22
[AS_HELP_STRING([--enable-ssl=@<:@auto/yes/no@:>@],
3-
[Use OpenSSL for TLS connections.])],
3+
[Use OpenSSL for TLS connections and SCRAM-SHA-1 authentication. NOTE: OpenSSL is required for authenticating to MongoDB 3.0 and later])],
44
[],
55
[enable_ssl=auto])
66

doc/installing.page

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,15 @@
6666

6767
<p>Minimal dependencies are needed to build the MongoDB C driver. Optionally, if you want Kerberos (GSSAPI) or SSL support, you need to install <code>libsasl2</code> and <code>OpenSSL</code> libraries and development headers respectively.</p>
6868

69+
<note>
70+
<p>
71+
In MongoDB 3.0 and later the default authentication mechanism is SCRAM-SHA-1.
72+
The MongoDB C Driver must be built with OpenSSL to use SCRAM-SHA-1 authentication,
73+
since the driver uses hash algorithms from the OpenSSL library to implement SCRAM-SHA-1,
74+
even if it connects to MongoDB over a non-SSL connection.
75+
</p>
76+
</note>
77+
6978
<p>Make sure you have access to a <link xref="installing#supported-platforms">supported toolchain</link> such as GCC, Clang, SolarisStudio, or MinGW. Optionally, <code>pkg-config</code> can be used if your system supports it to simplify locating proper compiler and linker arguments when compiling your program.</p>
7079

7180
<p>The following will configure for a typical 64-bit Linux system such as RedHat Enterprise Linux 6 or CentOS 6. Note that not all systems place 64-bit libraries in <code>/usr/lib64</code>. Check your system to see what the convention is if you are building 64-bit versions of the library.</p>

src/mongoc/mongoc-cluster.c

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -998,23 +998,43 @@ _mongoc_cluster_auth_node (mongoc_cluster_t *cluster,
998998

999999
if (0 == strcasecmp (mechanism, "MONGODB-CR")) {
10001000
ret = _mongoc_cluster_auth_node_cr (cluster, stream, error);
1001-
#ifdef MONGOC_ENABLE_SSL
10021001
} else if (0 == strcasecmp (mechanism, "MONGODB-X509")) {
1002+
#ifdef MONGOC_ENABLE_SSL
10031003
ret = _mongoc_cluster_auth_node_x509 (cluster, stream, error);
1004+
#else
1005+
bson_set_error (error,
1006+
MONGOC_ERROR_CLIENT,
1007+
MONGOC_ERROR_CLIENT_AUTHENTICATE,
1008+
"The \"%s\" authentication mechanism requires libmongoc built with --enable-ssl",
1009+
mechanism);
1010+
#endif
10041011
} else if (0 == strcasecmp (mechanism, "SCRAM-SHA-1")) {
1012+
#ifdef MONGOC_ENABLE_SSL
10051013
ret = _mongoc_cluster_auth_node_scram (cluster, stream, error);
1014+
#else
1015+
bson_set_error (error,
1016+
MONGOC_ERROR_CLIENT,
1017+
MONGOC_ERROR_CLIENT_AUTHENTICATE,
1018+
"The \"%s\" authentication mechanism requires libmongoc built with --enable-ssl",
1019+
mechanism);
10061020
#endif
1007-
#ifdef MONGOC_ENABLE_SASL
10081021
} else if (0 == strcasecmp (mechanism, "GSSAPI")) {
1022+
#ifdef MONGOC_ENABLE_SASL
10091023
ret = _mongoc_cluster_auth_node_sasl (cluster, stream, hostname, error);
1024+
#else
1025+
bson_set_error (error,
1026+
MONGOC_ERROR_CLIENT,
1027+
MONGOC_ERROR_CLIENT_AUTHENTICATE,
1028+
"The \"%s\" authentication mechanism requires libmongoc built with --enable-sasl",
1029+
mechanism);
10101030
#endif
10111031
} else if (0 == strcasecmp (mechanism, "PLAIN")) {
10121032
ret = _mongoc_cluster_auth_node_plain (cluster, stream, error);
10131033
} else {
10141034
bson_set_error (error,
10151035
MONGOC_ERROR_CLIENT,
10161036
MONGOC_ERROR_CLIENT_AUTHENTICATE,
1017-
"The authentication mechanism \"%s\" is not supported.",
1037+
"Unknown authentication mechanism \"%s\".",
10181038
mechanism);
10191039
}
10201040

tests/test-libmongoc.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,14 @@ test_framework_get_uri_str (const char *uri_str)
390390
abort ();
391391
}
392392

393+
#ifndef MONGOC_ENABLE_SSL
394+
if (user && password) {
395+
fprintf (stderr, "You need to configure with --enable-ssl"
396+
" when providing user+password (for SCRAM-SHA-1)\n");
397+
abort ();
398+
}
399+
#endif
400+
393401
/* add "ssl=true" if needed */
394402
if (test_framework_get_ssl () && !mongoc_uri_get_ssl (uri_parsed)) {
395403
test_uri_str = bson_strdup_printf (

tests/test-mongoc-client.c

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ gen_good_uri (const char *username,
5757

5858

5959
static void
60-
test_mongoc_client_authenticate (void)
60+
test_mongoc_client_authenticate (void *context)
6161
{
6262
mongoc_client_t *admin_client;
6363
char *username;
@@ -127,8 +127,22 @@ test_mongoc_client_authenticate (void)
127127
}
128128

129129

130+
int should_run_auth_tests (void)
131+
{
132+
#ifndef MONGOC_ENABLE_SSL
133+
mongoc_client_t *client = test_framework_client_new (NULL);
134+
uint32_t server_id = mongoc_cluster_preselect(&client->cluster, MONGOC_OPCODE_QUERY, NULL, NULL);
135+
136+
if (mongoc_cluster_node_max_wire_version (&client->cluster, server_id) > 2) {
137+
mongoc_client_destroy (client);
138+
return 0;
139+
}
140+
#endif
141+
142+
return 1;
143+
}
130144
static void
131-
test_mongoc_client_authenticate_failure (void)
145+
test_mongoc_client_authenticate_failure (void *context)
132146
{
133147
mongoc_collection_t *collection;
134148
mongoc_cursor_t *cursor;
@@ -151,6 +165,7 @@ test_mongoc_client_authenticate_failure (void)
151165
*/
152166
bson_init(&q);
153167
client = test_framework_client_new (bad_uri_str);
168+
154169
collection = mongoc_client_get_collection(client, "test", "test");
155170
suppress_one_message ();
156171
cursor = mongoc_collection_find(collection, MONGOC_QUERY_NONE, 0, 1, 0,
@@ -675,8 +690,8 @@ test_client_install (TestSuite *suite)
675690
}
676691

677692
TestSuite_Add (suite, "/Client/read_prefs", test_mongoc_client_read_prefs);
678-
TestSuite_Add (suite, "/Client/authenticate", test_mongoc_client_authenticate);
679-
TestSuite_Add (suite, "/Client/authenticate_failure", test_mongoc_client_authenticate_failure);
693+
TestSuite_AddFull (suite, "/Client/authenticate", test_mongoc_client_authenticate, NULL, NULL, should_run_auth_tests);
694+
TestSuite_AddFull (suite, "/Client/authenticate_failure", test_mongoc_client_authenticate_failure, NULL, NULL, should_run_auth_tests);
680695
TestSuite_Add (suite, "/Client/command", test_mongoc_client_command);
681696
TestSuite_Add (suite, "/Client/command_secondary", test_mongoc_client_command_secondary);
682697
TestSuite_Add (suite, "/Client/preselect", test_mongoc_client_preselect);

0 commit comments

Comments
 (0)