[CSHARP-3228] Percent-encoded authSource in mongodb+srv connection string fails for MONGODB-AWS and GSSAPI Created: 20/Oct/20  Updated: 28/Oct/23  Resolved: 01/Dec/20

Status: Closed
Project: C# Driver
Component/s: Connectivity
Affects Version/s: 2.11.2
Fix Version/s: 2.11.5

Type: Bug Priority: Major - P3
Reporter: James Kovacs Assignee: James Kovacs
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Problem/Incident
Related
related to CSHARP-3239 Cannot authenticate with an AWS sessi... Closed
Backwards Compatibility: Fully Compatible

 Description   

Consider the following code snippet:

var uri = "mongodb+srv://AWS_KEY:AWS_PRIVATE_KEY@cluster441.ebdql.mongodb.net/<dbname>?authMechanism=MONGODB-AWS&authSource=%24external&retryWrites=true&w=majority";
var mongoURL = new MongoUrl(uri);
var client = new MongoClient(mongoURL);
Console.WriteLine("Finished!");

Construction of the MongoClient instance fails with the following stack trace:

Unhandled Exception: System.ArgumentException: A MONGODB-AWS source must be $external.
Parameter name: source
   at MongoDB.Driver.MongoCredential.EnsureNullOrExternalSource(String mechanism, String source)
   at MongoDB.Driver.MongoCredential.FromComponents(String mechanism, String source, String databaseName, String username, MongoIdentityEvidence evidence)
   at MongoDB.Driver.MongoClientSettings.FromUrl(MongoUrl url)
   at MongoDB.Driver.MongoClient..ctor(MongoUrl url)
   at tour.Program.Main(String[] args) in /shared/csharp/Program.cs:line 14

If we change authSource=%24external to authSource=$external, the code works as expected. If we change the scheme from mongodb+srv:// to mongodb:// (and switched to the correct hostnames), it also works as expected.



 Comments   
Comment by Githook User [ 01/Dec/20 ]

Author:

{'name': 'James Kovacs', 'email': 'jkovacs@post.harvard.edu', 'username': 'JamesKovacs'}

Message: CSHARP-3228 / CSHARP-3239: Avoid double-escaping connection string options when merging in options from a TXT record.
Branch: v2.11.x
https://github.com/mongodb/mongo-csharp-driver/commit/8a453e09b78f4df9d66b9a64f48caa38165ae3d0

Comment by Githook User [ 01/Dec/20 ]

Author:

{'name': 'James Kovacs', 'email': 'jkovacs@post.harvard.edu', 'username': 'JamesKovacs'}

Message: CSHARP-3228 / CSHARP-3239: Avoid double-escaping connection string options when merging in options from a TXT record.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/c52e4d5459922b212b61f5d6539f56b959e9cb8d

Comment by James Kovacs [ 20/Oct/20 ]

Easiest workaround is to use authSource=$external without percent-encoding it. Literal dollar signs are allowed in the fragment portion of a URI and the .NET Driver handles this case properly.

Generated at Wed Feb 07 21:44:44 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.