Skip to content

Commit bc196cf

Browse files
author
Divjot Arora
authored
Resync auth tests and error for empty authSource in a URI (#449)
This commit also syncs spec tests for a spec change that made empty usernames invalid. GODRIVER-1479 GODRIVER-1577
1 parent 745a7b8 commit bc196cf

File tree

3 files changed

+92
-1
lines changed

3 files changed

+92
-1
lines changed

data/auth/connection-string.json

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,16 @@
107107
}
108108
}
109109
},
110+
{
111+
"description": "must raise an error when the authSource is empty",
112+
"uri": "mongodb://user:password@localhost/foo?authSource=",
113+
"valid": false
114+
},
115+
{
116+
"description": "must raise an error when the authSource is empty without credentials",
117+
"uri": "mongodb://localhost/admin?authSource=",
118+
"valid": false
119+
},
110120
{
111121
"description": "should throw an exception if authSource is invalid (GSSAPI)",
112122
"uri": "mongodb://user%40DOMAIN.COM@localhost/?authMechanism=GSSAPI&authSource=foo",
@@ -206,6 +216,18 @@
206216
"mechanism_properties": null
207217
}
208218
},
219+
{
220+
"description": "should recognize the mechanism with no username when auth source is explicitly specified (MONGODB-X509)",
221+
"uri": "mongodb://localhost/?authMechanism=MONGODB-X509&authSource=$external",
222+
"valid": true,
223+
"credential": {
224+
"username": null,
225+
"password": null,
226+
"source": "$external",
227+
"mechanism": "MONGODB-X509",
228+
"mechanism_properties": null
229+
}
230+
},
209231
{
210232
"description": "should throw an exception if supplied a password (MONGODB-X509)",
211233
"uri": "mongodb://user:password@localhost/?authMechanism=MONGODB-X509",
@@ -357,6 +379,16 @@
357379
"valid": true,
358380
"credential": null
359381
},
382+
{
383+
"description": "should throw an exception if no username provided (userinfo implies default mechanism)",
384+
"uri": "mongodb://@localhost.com/",
385+
"valid": false
386+
},
387+
{
388+
"description": "should throw an exception if no username/password provided (userinfo implies default mechanism)",
389+
"uri": "mongodb://:@localhost.com/",
390+
"valid": false
391+
},
360392
{
361393
"description": "should recognise the mechanism (MONGODB-AWS)",
362394
"uri": "mongodb://localhost/?authMechanism=MONGODB-AWS",
@@ -369,6 +401,18 @@
369401
"mechanism_properties": null
370402
}
371403
},
404+
{
405+
"description": "should recognise the mechanism when auth source is explicitly specified (MONGODB-AWS)",
406+
"uri": "mongodb://localhost/?authMechanism=MONGODB-AWS&authSource=$external",
407+
"valid": true,
408+
"credential": {
409+
"username": null,
410+
"password": null,
411+
"source": "$external",
412+
"mechanism": "MONGODB-AWS",
413+
"mechanism_properties": null
414+
}
415+
},
372416
{
373417
"description": "should throw an exception if username and no password (MONGODB-AWS)",
374418
"uri": "mongodb://user@localhost/?authMechanism=MONGODB-AWS",

data/auth/connection-string.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ tests:
8585
mechanism: "GSSAPI"
8686
mechanism_properties:
8787
SERVICE_NAME: "mongodb"
88+
-
89+
description: "must raise an error when the authSource is empty"
90+
uri: "mongodb://user:password@localhost/foo?authSource="
91+
valid: false
92+
-
93+
description: "must raise an error when the authSource is empty without credentials"
94+
uri: "mongodb://localhost/admin?authSource="
95+
valid: false
8896
-
8997
description: "should throw an exception if authSource is invalid (GSSAPI)"
9098
uri: "mongodb://user%40DOMAIN.COM@localhost/?authMechanism=GSSAPI&authSource=foo"
@@ -167,6 +175,16 @@ tests:
167175
source: "$external"
168176
mechanism: "MONGODB-X509"
169177
mechanism_properties: ~
178+
-
179+
description: "should recognize the mechanism with no username when auth source is explicitly specified (MONGODB-X509)"
180+
uri: "mongodb://localhost/?authMechanism=MONGODB-X509&authSource=$external"
181+
valid: true
182+
credential:
183+
username: ~
184+
password: ~
185+
source: "$external"
186+
mechanism: "MONGODB-X509"
187+
mechanism_properties: ~
170188
-
171189
description: "should throw an exception if supplied a password (MONGODB-X509)"
172190
uri: "mongodb://user:password@localhost/?authMechanism=MONGODB-X509"
@@ -292,6 +310,14 @@ tests:
292310
uri: "mongodb://localhost/?authSource=foo"
293311
valid: true
294312
credential: ~
313+
-
314+
description: "should throw an exception if no username provided (userinfo implies default mechanism)"
315+
uri: "mongodb://@localhost.com/"
316+
valid: false
317+
-
318+
description: "should throw an exception if no username/password provided (userinfo implies default mechanism)"
319+
uri: "mongodb://:@localhost.com/"
320+
valid: false
295321
-
296322
description: "should recognise the mechanism (MONGODB-AWS)"
297323
uri: "mongodb://localhost/?authMechanism=MONGODB-AWS"
@@ -302,6 +328,16 @@ tests:
302328
source: "$external"
303329
mechanism: "MONGODB-AWS"
304330
mechanism_properties: ~
331+
-
332+
description: "should recognise the mechanism when auth source is explicitly specified (MONGODB-AWS)"
333+
uri: "mongodb://localhost/?authMechanism=MONGODB-AWS&authSource=$external"
334+
valid: true
335+
credential:
336+
username: ~
337+
password: ~
338+
source: "$external"
339+
mechanism: "MONGODB-AWS"
340+
mechanism_properties: ~
305341
-
306342
description: "should throw an exception if username and no password (MONGODB-AWS)"
307343
uri: "mongodb://user@localhost/?authMechanism=MONGODB-AWS"

x/mongo/driver/connstring/connstring.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ type ConnString struct {
115115
WNumber int
116116
WNumberSet bool
117117
Username string
118+
UsernameSet bool
118119
ZlibLevel int
119120
ZlibLevelSet bool
120121
ZstdLevel int
@@ -137,7 +138,7 @@ func (u *ConnString) String() string {
137138
func (u *ConnString) HasAuthParameters() bool {
138139
// Check all auth parameters except for AuthSource because an auth source without other credentials is semantically
139140
// valid and must not be interpreted as a request for authentication.
140-
return u.AuthMechanism != "" || u.AuthMechanismProperties != nil || u.Username != "" || u.PasswordSet
141+
return u.AuthMechanism != "" || u.AuthMechanismProperties != nil || u.UsernameSet || u.PasswordSet
141142
}
142143

143144
// Validate checks that the Auth and SSL parameters are valid values.
@@ -224,6 +225,7 @@ func (p *parser) parse(original string) error {
224225
if err != nil {
225226
return internal.WrapErrorf(err, "invalid username")
226227
}
228+
p.UsernameSet = true
227229

228230
// Validate and process the password.
229231
if strings.Contains(password, ":") {
@@ -342,6 +344,12 @@ func (p *parser) validate() error {
342344
}
343345

344346
func (p *parser) setDefaultAuthParams(dbName string) error {
347+
// We do this check here rather than in validateAuth because this function is called as part of parsing and sets
348+
// the value of AuthSource if authentication is enabled.
349+
if p.AuthSourceSet && p.AuthSource == "" {
350+
return errors.New("authSource must be non-empty when supplied in a URI")
351+
}
352+
345353
switch strings.ToLower(p.AuthMechanism) {
346354
case "plain":
347355
if p.AuthSource == "" {
@@ -466,6 +474,9 @@ func (p *parser) validateAuth() error {
466474
return fmt.Errorf("SCRAM-SHA-256 cannot have mechanism properties")
467475
}
468476
case "":
477+
if p.UsernameSet && p.Username == "" {
478+
return fmt.Errorf("username required if URI contains user info")
479+
}
469480
default:
470481
return fmt.Errorf("invalid auth mechanism")
471482
}

0 commit comments

Comments
 (0)