Skip to content

Commit 4e88488

Browse files
committed
CDRIVER-2258 Import authentication spec tests
1 parent f1006e0 commit 4e88488

File tree

4 files changed

+330
-17
lines changed

4 files changed

+330
-17
lines changed

src/mongoc/mongoc-uri.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,6 @@ bool
632632
mongoc_uri_option_is_utf8 (const char *key)
633633
{
634634
return !strcasecmp (key, MONGOC_URI_APPNAME) ||
635-
!strcasecmp (key, MONGOC_URI_GSSAPISERVICENAME) ||
636635
!strcasecmp (key, MONGOC_URI_REPLICASET) ||
637636
!strcasecmp (key, MONGOC_URI_READPREFERENCE) ||
638637
!strcasecmp (key, MONGOC_URI_SSLCLIENTCERTIFICATEKEYFILE) ||
@@ -751,6 +750,18 @@ mongoc_uri_parse_option (mongoc_uri_t *uri, const char *str)
751750
MONGOC_WARNING ("Overwriting previously provided value for '%s'", key);
752751
}
753752
mongoc_read_concern_set_level (uri->read_concern, value);
753+
} else if (!strcmp (lkey, MONGOC_URI_GSSAPISERVICENAME)) {
754+
char *tmp = bson_strdup_printf ("SERVICE_NAME:%s", value);
755+
if (bson_has_field (&uri->credentials,
756+
MONGOC_URI_AUTHMECHANISMPROPERTIES)) {
757+
MONGOC_WARNING (
758+
"authMechanismProperties SERVICE_NAME already set, ignoring '%s'",
759+
lkey);
760+
} else if (!mongoc_uri_parse_auth_mechanism_properties (uri, tmp)) {
761+
bson_free (tmp);
762+
goto UNSUPPORTED_VALUE;
763+
}
764+
bson_free (tmp);
754765
} else if (!strcmp (lkey, MONGOC_URI_AUTHMECHANISMPROPERTIES)) {
755766
if (bson_has_field (&uri->credentials, lkey)) {
756767
MONGOC_WARNING ("Overwriting previously provided value for '%s'", key);
@@ -853,6 +864,15 @@ mongoc_uri_finalize_auth (mongoc_uri_t *uri, bson_error_t *error)
853864
&uri->credentials, MONGOC_URI_AUTHSOURCE, -1, "$external", -1);
854865
}
855866
}
867+
/* MONGODB-X509 is the only mechanism that doesn't require username */
868+
if (strcasecmp (mechanism, "MONGODB-X509")) {
869+
if (!mongoc_uri_get_username (uri)) {
870+
MONGOC_URI_ERROR (error,
871+
"'%s' authentication mechanism requires username",
872+
mechanism);
873+
return false;
874+
}
875+
}
856876
}
857877
return true;
858878
}
Lines changed: 291 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,291 @@
1+
{
2+
"tests": [
3+
{
4+
"description": "should use the default source and mechanism",
5+
"uri": "mongodb://user:password@localhost",
6+
"hosts": [],
7+
"valid": true,
8+
"warning": false,
9+
"auth": {
10+
"db": "admin"
11+
},
12+
"options": null
13+
},
14+
{
15+
"description": "should use the database when no authSource is specified",
16+
"uri": "mongodb://user:password@localhost/foo",
17+
"hosts": [],
18+
"valid": true,
19+
"warning": false,
20+
"auth": {
21+
"db": "foo"
22+
},
23+
"options": null
24+
},
25+
{
26+
"description": "should use the authSource when specified",
27+
"uri": "mongodb://user:password@localhost/foo?authSource=bar",
28+
"hosts": [],
29+
"valid": true,
30+
"warning": false,
31+
"auth": {
32+
"db": "bar"
33+
},
34+
"options": null
35+
},
36+
{
37+
"description": "should recognize an empty password",
38+
"uri": "mongodb://user:@localhost",
39+
"hosts": [],
40+
"valid": true,
41+
"warning": false,
42+
"auth": {
43+
"username": "user",
44+
"password": ""
45+
},
46+
"options": null
47+
},
48+
{
49+
"description": "should recognize no password",
50+
"uri": "mongodb://user@localhost",
51+
"hosts": [],
52+
"valid": true,
53+
"warning": false,
54+
"auth": {
55+
"username": "user",
56+
"password": null
57+
},
58+
"options": null
59+
},
60+
{
61+
"description": "should recognize no password",
62+
"uri": "mongodb://user@localhost",
63+
"hosts": [],
64+
"valid": true,
65+
"warning": false,
66+
"auth": {
67+
"username": "user",
68+
"password": null
69+
},
70+
"options": null
71+
},
72+
{
73+
"description": "should recognize a url escaped character in the username",
74+
"uri": "mongodb://user%40DOMAIN.COM:password@localhost",
75+
"hosts": [],
76+
"valid": true,
77+
"warning": false,
78+
"auth": {
79+
"username": "user@DOMAIN.COM"
80+
},
81+
"options": null
82+
},
83+
{
84+
"description": "should recognize the mechanism",
85+
"uri": "mongodb://user@localhost/?authMechanism=GSSAPI",
86+
"hosts": [],
87+
"valid": true,
88+
"warning": false,
89+
"auth": null,
90+
"options": {
91+
"authmechanism": "GSSAPI"
92+
}
93+
},
94+
{
95+
"description": "should use $external as the source",
96+
"uri": "mongodb://user%40DOMAIN.COM:password@localhost/?authMechanism=GSSAPI",
97+
"hosts": [],
98+
"valid": true,
99+
"warning": false,
100+
"auth": {
101+
"db": "$external"
102+
},
103+
"options": null
104+
},
105+
{
106+
"description": "should use $external as the source when a database is specified",
107+
"uri": "mongodb://user%40DOMAIN.COM:password@localhost/foo?authMechanism=GSSAPI",
108+
"hosts": [],
109+
"valid": true,
110+
"warning": false,
111+
"auth": {
112+
"db": "$external"
113+
},
114+
"options": null
115+
},
116+
{
117+
"description": "should throw an exception when an authSource is specified other than $external",
118+
"uri": "mongodb://user%40DOMAIN.COM:password@localhost/foo?authMechanism=GSSAPI&authSource=bar",
119+
"hosts": [],
120+
"valid": false,
121+
"warning": true,
122+
"auth": null,
123+
"options": null
124+
125+
},
126+
{
127+
"description": "should throw an exception when an authMechanism is specified with no username",
128+
"uri": "mongodb://localhost/?authMechanism=GSSAPI",
129+
"hosts": [],
130+
"valid": false,
131+
"warning": true,
132+
"auth": null,
133+
"options": null
134+
},
135+
{
136+
"description": "should accept generic mechanism property",
137+
"uri": "mongodb://user%40DOMAIN.COM:password@localhost/?authMechanism=GSSAPI&authMechanismProperties=SERVICE_NAME:other,CANONICALIZE_HOST_NAME:true",
138+
"hosts": [],
139+
"valid": true,
140+
"warning": false,
141+
"auth": null,
142+
"options": {
143+
"authmechanism": "GSSAPI",
144+
"authmechanismproperties": {
145+
"SERVICE_NAME": "other",
146+
"CANONICALIZE_HOST_NAME": true
147+
}
148+
}
149+
},
150+
{
151+
"description": "should accept legacy gssapiServiceName",
152+
"uri": "mongodb://user%40DOMAIN.COM:password@localhost/?authMechanism=GSSAPI&gssapiServiceName=other",
153+
"hosts": [],
154+
"valid": true,
155+
"warning": false,
156+
"auth": null,
157+
"options": {
158+
"authmechanism": "GSSAPI",
159+
"authmechanismproperties": {
160+
"SERVICE_NAME": "other"
161+
}
162+
}
163+
},
164+
{
165+
"description": "should recognize the mechanism",
166+
"uri": "mongodb://user:pass@localhost/?authMechanism=MONGODB-CR",
167+
"hosts": [],
168+
"valid": true,
169+
"warning": false,
170+
"auth": null,
171+
"options": {
172+
"authmechanism": "MONGODB-CR"
173+
}
174+
},
175+
{
176+
"description": "should throw an exception when an authMechanism is specified with no username",
177+
"uri": "mongodb://localhost/?authMechanism=MONGODB-CR",
178+
"hosts": [],
179+
"valid": false,
180+
"warning": true,
181+
"auth": null,
182+
"options": null
183+
},
184+
{
185+
"description": "should use $external as the source",
186+
"uri": "mongodb://CN%3DmyName%2COU%3DmyOrgUnit%2CO%3DmyOrg%2CL%3DmyLocality%2CST%3DmyState%2CC%3DmyCountry@localhost/?authMechanism=MONGODB-X509",
187+
"hosts": [],
188+
"valid": true,
189+
"warning": false,
190+
"auth": {
191+
"db": "$external"
192+
},
193+
"options": null
194+
},
195+
{
196+
"description": "should use $external as the source when a database is specified",
197+
"uri": "mongodb://CN%3DmyName%2COU%3DmyOrgUnit%2CO%3DmyOrg%2CL%3DmyLocality%2CST%3DmyState%2CC%3DmyCountry@localhost/foo?authMechanism=MONGODB-X509",
198+
"hosts": [],
199+
"valid": true,
200+
"warning": false,
201+
"auth": {
202+
"db": "$external"
203+
},
204+
"options": null
205+
},
206+
{
207+
"description": "should throw an exception when an authSource is specified other than $external",
208+
"uri": "mongodb://CN%3DmyName%2COU%3DmyOrgUnit%2CO%3DmyOrg%2CL%3DmyLocality%2CST%3DmyState%2CC%3DmyCountry@localhost/foo?authMechanism=MONGODB-X509&authSource=bar",
209+
"hosts": [],
210+
"valid": false,
211+
"warning": true,
212+
"auth": null,
213+
"options": null
214+
},
215+
{
216+
"description": "should recognize the mechanism",
217+
"uri": "mongodb://CN%3DmyName%2COU%3DmyOrgUnit%2CO%3DmyOrg%2CL%3DmyLocality%2CST%3DmyState%2CC%3DmyCountry@localhost/?authMechanism=MONGODB-X509",
218+
"hosts": [],
219+
"valid": true,
220+
"warning": false,
221+
"auth": null,
222+
"options": {
223+
"authmechanism": "MONGODB-X509"
224+
}
225+
},
226+
{
227+
"description": "should recognize the mechanism with no username",
228+
"uri": "mongodb://localhost/?authMechanism=MONGODB-X509",
229+
"hosts": [],
230+
"valid": true,
231+
"warning": false,
232+
"auth": {
233+
"username": null
234+
},
235+
"options": {
236+
"authmechanism": "MONGODB-X509"
237+
}
238+
},
239+
{
240+
"description": "should recognize the encoded username",
241+
"uri": "mongodb://CN%3DmyName%2COU%3DmyOrgUnit%2CO%3DmyOrg%2CL%3DmyLocality%2CST%3DmyState%2CC%3DmyCountry@localhost/?authMechanism=MONGODB-X509",
242+
"hosts": [],
243+
"valid": true,
244+
"warning": false,
245+
"auth": {
246+
"username": "CN=myName,OU=myOrgUnit,O=myOrg,L=myLocality,ST=myState,C=myCountry"
247+
},
248+
"options": null
249+
},
250+
{
251+
"description": "should recognize the mechanism",
252+
"uri": "mongodb://user:password@localhost/?authMechanism=PLAIN",
253+
"hosts": [],
254+
"valid": true,
255+
"warning": false,
256+
"auth": null,
257+
"options": {
258+
"authmechanism": "PLAIN"
259+
}
260+
},
261+
{
262+
"description": "should throw an exception when an authMechanism is specified with no username",
263+
"uri": "mongodb://localhost/?authMechanism=PLAIN",
264+
"hosts": [],
265+
"valid": false,
266+
"warning": true,
267+
"auth": null,
268+
"options": null
269+
},
270+
{
271+
"description": "should recognize the mechanism",
272+
"uri": "mongodb://user:password@localhost/?authMechanism=SCRAM-SHA-1",
273+
"hosts": [],
274+
"valid": true,
275+
"warning": false,
276+
"auth": null,
277+
"options": {
278+
"authmechanism": "SCRAM-SHA-1"
279+
}
280+
},
281+
{
282+
"description": "should throw an exception when an authMechanism is specified with no username",
283+
"uri": "mongodb://localhost/?authMechanism=SCRAM-SHA-1",
284+
"hosts": [],
285+
"valid": false,
286+
"warning": true,
287+
"auth": null,
288+
"options": null
289+
}
290+
]
291+
}

tests/test-mongoc-connection-uri.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ test_all_spec_tests (TestSuite *suite)
189189

190190
ASSERT (realpath (JSON_DIR "/connection_uri", resolved));
191191
install_json_test_suite (suite, resolved, &test_connection_uri_cb);
192+
193+
ASSERT (realpath (JSON_DIR "/auth", resolved));
194+
install_json_test_suite (suite, resolved, &test_connection_uri_cb);
192195
}
193196

194197

tests/test-mongoc-uri.c

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -376,22 +376,6 @@ test_mongoc_uri_new (void)
376376
ASSERT (uri);
377377
mongoc_uri_destroy (uri);
378378

379-
/* deprecated gssapiServiceName option */
380-
uri = mongoc_uri_new ("mongodb://christian%40realm.cc@localhost:27017/"
381-
"?" MONGOC_URI_AUTHMECHANISM
382-
"=GSSAPI&" MONGOC_URI_GSSAPISERVICENAME "=blah");
383-
ASSERT (uri);
384-
options = mongoc_uri_get_options (uri);
385-
ASSERT (options);
386-
BSON_ASSERT (0 == strcmp (mongoc_uri_get_auth_mechanism (uri), "GSSAPI"));
387-
BSON_ASSERT (0 ==
388-
strcmp (mongoc_uri_get_username (uri), "christian@realm.cc"));
389-
BSON_ASSERT (
390-
bson_iter_init_find_case (&iter, options, MONGOC_URI_GSSAPISERVICENAME) &&
391-
BSON_ITER_HOLDS_UTF8 (&iter) &&
392-
(0 == strcmp (bson_iter_utf8 (&iter, NULL), "blah")));
393-
mongoc_uri_destroy (uri);
394-
395379
/* MONGODB-CR */
396380

397381
/* should recognize this mechanism */
@@ -463,6 +447,7 @@ test_mongoc_uri_authmechanismproperties (void)
463447
{
464448
mongoc_uri_t *uri;
465449
bson_t props;
450+
const bson_t *options;
466451

467452
uri = mongoc_uri_new ("mongodb://user@localhost/?" MONGOC_URI_AUTHMECHANISM
468453
"=SCRAM-SHA1"
@@ -491,6 +476,20 @@ test_mongoc_uri_authmechanismproperties (void)
491476
ASSERT_MATCH (&props, "{'a': 'four', 'b': {'$exists': false}}");
492477

493478
mongoc_uri_destroy (uri);
479+
480+
/* deprecated gssapiServiceName option */
481+
uri = mongoc_uri_new ("mongodb://christian%40realm.cc@localhost:27017/"
482+
"?" MONGOC_URI_AUTHMECHANISM
483+
"=GSSAPI&" MONGOC_URI_GSSAPISERVICENAME "=blah");
484+
ASSERT (uri);
485+
options = mongoc_uri_get_options (uri);
486+
ASSERT (options);
487+
BSON_ASSERT (0 == strcmp (mongoc_uri_get_auth_mechanism (uri), "GSSAPI"));
488+
BSON_ASSERT (0 ==
489+
strcmp (mongoc_uri_get_username (uri), "christian@realm.cc"));
490+
ASSERT (mongoc_uri_get_mechanism_properties (uri, &props));
491+
ASSERT_MATCH (&props, "{'SERVICE_NAME': 'blah'}");
492+
mongoc_uri_destroy (uri);
494493
}
495494

496495

0 commit comments

Comments
 (0)