Skip to content

Commit f68d8ef

Browse files
committed
feat: throw if credentials set
1 parent c6545f3 commit f68d8ef

File tree

5 files changed

+33
-7
lines changed

5 files changed

+33
-7
lines changed

src/cmap/auth/mongo_credentials.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export interface AuthMechanismProperties extends Document {
5858
SERVICE_NAME?: string;
5959
SERVICE_REALM?: string;
6060
CANONICALIZE_HOST_NAME?: GSSAPICanonicalizationValue;
61+
/** @internal */
6162
AWS_SESSION_TOKEN?: string;
6263
/** A user provided OIDC machine callback function. */
6364
OIDC_CALLBACK?: OIDCCallbackFunction;

src/cmap/auth/mongodb_aws.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,10 @@ export class MongoDBAWS extends AuthProvider {
5656
);
5757
}
5858

59-
if (!authContext.credentials.username) {
60-
authContext.credentials = await makeTempCredentials(
61-
authContext.credentials,
62-
this.credentialFetcher
63-
);
64-
}
59+
authContext.credentials = await makeTempCredentials(
60+
authContext.credentials,
61+
this.credentialFetcher
62+
);
6563

6664
const { credentials } = authContext;
6765

src/connection_string.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,18 @@ export function parseOptions(
423423
);
424424
}
425425

426+
if (isAws) {
427+
const { username, password } = mongoOptions.credentials;
428+
if (username || password) {
429+
throw new MongoParseError(
430+
'username and password cannot be provided when using MONGODB-AWS'
431+
);
432+
}
433+
if (mongoOptions.credentials.mechanismProperties.AWS_SESSION_TOKEN) {
434+
throw new MongoParseError('AWS_SESSION_TOKEN cannot be provided when using MONGODB-AWS');
435+
}
436+
}
437+
426438
mongoOptions.credentials.validate();
427439

428440
// Check if the only auth related option provided was authSource, if so we can remove credentials

test/spec/auth/legacy/connection-string.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,21 @@
440440
}
441441
}
442442
},
443+
{
444+
"description": "should throw an exception if username provided (MONGODB-AWS) implies default mechanism)",
445+
"uri": "mongodb://user:localhost.com/",
446+
"valid": false
447+
},
448+
{
449+
"description": "should throw an exception if username and password provided (MONGODB-AWS) implies default mechanism)",
450+
"uri": "mongodb://user@pass:localhost.com/",
451+
"valid": false
452+
},
453+
{
454+
"description": "should throw an exception if AWS_SESSION_TOKEN provided (MONGODB-AWS) implies default mechanism)",
455+
"uri": "mongodb://localhost/?authMechanism=MONGODB-AWS&authMechanismProperties=AWS_SESSION_TOKEN:token",
456+
"valid": false
457+
},
443458
{
444459
"description": "should recognise the mechanism with test environment (MONGODB-OIDC)",
445460
"uri": "mongodb://localhost/?authMechanism=MONGODB-OIDC&authMechanismProperties=ENVIRONMENT:test",

test/spec/auth/legacy/connection-string.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,4 +480,4 @@ tests:
480480
(MONGODB-OIDC)
481481
uri: mongodb://user:pass@localhost/?authMechanism=MONGODB-OIDC&authMechanismProperties=ENVIRONMENT:k8s
482482
valid: false
483-
credential: null
483+
credential: null

0 commit comments

Comments
 (0)