Skip to content

Commit a53af6e

Browse files
CSHARP-2913: Lift restriction on authSource without credentials
1 parent 2c15f36 commit a53af6e

File tree

4 files changed

+79
-28
lines changed

4 files changed

+79
-28
lines changed

src/MongoDB.Driver/MongoUrl.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,7 @@ public bool HasAuthenticationSettings
271271
return
272272
_username != null ||
273273
_password != null ||
274-
_authenticationMechanism != null ||
275-
_authenticationSource != null;
274+
_authenticationMechanism != null;
276275
}
277276
}
278277

tests/MongoDB.Driver.Tests/Specifications/auth/AuthTestRunner.cs

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ private void AssertValid(MongoCredential mongoCredential, BsonDocument definitio
6565
}
6666

6767
var expectedCredential = definition["credential"] as BsonDocument;
68-
if (expectedCredential != null)
68+
if (expectedCredential == null)
69+
{
70+
mongoCredential.Should().BeNull();
71+
}
72+
else
6973
{
7074
JsonDrivenHelper.EnsureAllFieldsAreValid(expectedCredential, "username", "password", "source", "mechanism", "mechanism_properties");
7175
mongoCredential.Username.Should().Be(ValueToString(expectedCredential["username"]));
@@ -75,10 +79,10 @@ private void AssertValid(MongoCredential mongoCredential, BsonDocument definitio
7579
mongoCredential.Source.Should().Be(ValueToString(expectedCredential["source"]));
7680
mongoCredential.Mechanism.Should().Be(ValueToString(expectedCredential["mechanism"]));
7781

78-
var authenticator = mongoCredential.ToAuthenticator();
79-
if (authenticator is GssapiAuthenticator gssapiAuthenticator)
82+
var expectedMechanismProperties = expectedCredential["mechanism_properties"];
83+
if (mongoCredential.Mechanism == GssapiAuthenticator.MechanismName)
8084
{
81-
expectedCredential.TryGetValue("mechanism_properties", out var expectedMechanismProperties);
85+
var gssapiAuthenticator = (GssapiAuthenticator)mongoCredential.ToAuthenticator();
8286
if (expectedMechanismProperties.IsBsonNull)
8387
{
8488
var serviceName = gssapiAuthenticator._mechanism_serviceName();
@@ -109,7 +113,16 @@ private void AssertValid(MongoCredential mongoCredential, BsonDocument definitio
109113
}
110114
else
111115
{
112-
// Other authenticators do not contain mechanism properties
116+
var actualMechanismProperties = mongoCredential._mechanismProperties();
117+
if (expectedMechanismProperties.IsBsonNull)
118+
{
119+
actualMechanismProperties.Should().BeEmpty();
120+
}
121+
else
122+
{
123+
var authMechanismProperties = new BsonDocument(actualMechanismProperties.Select(kv => new BsonElement(kv.Key, BsonValue.Create(kv.Value))));
124+
authMechanismProperties.Should().BeEquivalentTo(expectedMechanismProperties.AsBsonDocument);
125+
}
113126
}
114127
}
115128
}
@@ -130,22 +143,7 @@ private string ValueToString(BsonValue value)
130143
// nested types
131144
private class TestCaseFactory : JsonDrivenTestCaseFactory
132145
{
133-
#region static
134-
private static readonly string[] __ignoredTestNames =
135-
{
136-
// Auth tests create auth mechanism which this test does not expect. Altering this behavior would break GSSAPI tests.
137-
"should recognise the mechanism (MONGODB-AWS)"
138-
};
139-
#endregion
140-
141-
// protected properties
142146
protected override string PathPrefix => "MongoDB.Driver.Tests.Specifications.auth.tests.";
143-
144-
// protected methods
145-
protected override IEnumerable<JsonDrivenTestCase> CreateTestCases(BsonDocument document)
146-
{
147-
return base.CreateTestCases(document).Where(test => !__ignoredTestNames.Any(ignoredName => test.Name.EndsWith(ignoredName)));
148-
}
149147
}
150148
}
151149

@@ -168,4 +166,12 @@ private static object _mechanism(GssapiAuthenticator obj)
168166
return Reflector.GetFieldValue(obj, nameof(_mechanism));
169167
}
170168
}
169+
170+
internal static class MongoCredentialReflector
171+
{
172+
public static Dictionary<string, object> _mechanismProperties(this MongoCredential obj)
173+
{
174+
return (Dictionary<string, object>)Reflector.GetFieldValue(obj, nameof(_mechanismProperties));
175+
}
176+
}
171177
}

tests/MongoDB.Driver.Tests/Specifications/auth/tests/connection-string.json

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,18 @@
216216
"mechanism_properties": null
217217
}
218218
},
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+
},
219231
{
220232
"description": "should throw an exception if supplied a password (MONGODB-X509)",
221233
"uri": "mongodb://user:password@localhost/?authMechanism=MONGODB-X509",
@@ -362,9 +374,10 @@
362374
"credential": null
363375
},
364376
{
365-
"description": "authSource without username is invalid (default mechanism)",
377+
"description": "authSource without username doesn't create credential (default mechanism)",
366378
"uri": "mongodb://localhost/?authSource=foo",
367-
"valid": false
379+
"valid": true,
380+
"credential": null
368381
},
369382
{
370383
"description": "should throw an exception if no username provided (userinfo implies default mechanism)",
@@ -388,6 +401,18 @@
388401
"mechanism_properties": null
389402
}
390403
},
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+
},
391416
{
392417
"description": "should throw an exception if username and no password (MONGODB-AWS)",
393418
"uri": "mongodb://user@localhost/?authMechanism=MONGODB-AWS",
@@ -421,4 +446,4 @@
421446
}
422447
}
423448
]
424-
}
449+
}

tests/MongoDB.Driver.Tests/Specifications/auth/tests/connection-string.yml

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,16 @@ tests:
175175
source: "$external"
176176
mechanism: "MONGODB-X509"
177177
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: ~
178188
-
179189
description: "should throw an exception if supplied a password (MONGODB-X509)"
180190
uri: "mongodb://user:password@localhost/?authMechanism=MONGODB-X509"
@@ -296,9 +306,10 @@ tests:
296306
valid: true
297307
credential: ~
298308
-
299-
description: "authSource without username is invalid (default mechanism)"
309+
description: "authSource without username doesn't create credential (default mechanism)"
300310
uri: "mongodb://localhost/?authSource=foo"
301-
valid: false
311+
valid: true
312+
credential: ~
302313
-
303314
description: "should throw an exception if no username provided (userinfo implies default mechanism)"
304315
uri: "mongodb://@localhost.com/"
@@ -317,6 +328,16 @@ tests:
317328
source: "$external"
318329
mechanism: "MONGODB-AWS"
319330
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: ~
320341
-
321342
description: "should throw an exception if username and no password (MONGODB-AWS)"
322343
uri: "mongodb://user@localhost/?authMechanism=MONGODB-AWS"
@@ -342,4 +363,4 @@ tests:
342363
source: "$external"
343364
mechanism: "MONGODB-AWS"
344365
mechanism_properties:
345-
AWS_SESSION_TOKEN: "token!@#$%^&*()_+"
366+
AWS_SESSION_TOKEN: "token!@#$%^&*()_+"

0 commit comments

Comments
 (0)