Skip to content

Commit 7715e53

Browse files
authored
fix(shell-api): relax db.auth() input validation MONGOSH-1051 (#1159)
Authenticating without username/password can be valid for some authentication mechanisms, so skip that if a mechanism was passed in explicitly.
1 parent 9fe24c6 commit 7715e53

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

packages/cli-repl/test/e2e-tls.spec.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,13 @@ describe('e2e TLS', () => {
214214
});
215215
const prompt = await shell.waitForPromptOrExit();
216216
expect(prompt.state).to.equal('prompt');
217-
await shell.executeLine('db.runCommand({ connectionStatus: 1 })');
218-
shell.assertContainsOutput(`user: '${certUser}'`);
217+
expect(await shell.executeLine('db.runCommand({ connectionStatus: 1 })'))
218+
.to.include(`user: '${certUser}'`);
219+
220+
expect(await shell.executeLine('db.getSiblingDB("$external").auth({mechanism: "MONGODB-X509"})'))
221+
.to.include('ok: 1');
222+
expect(await shell.executeLine('db.runCommand({ connectionStatus: 1 })'))
223+
.to.include(`user: '${certUser}'`);
219224
});
220225

221226
it('works with valid cert (connection string)', async() => {
@@ -228,8 +233,13 @@ describe('e2e TLS', () => {
228233
});
229234
const prompt = await shell.waitForPromptOrExit();
230235
expect(prompt.state).to.equal('prompt');
231-
await shell.executeLine('db.runCommand({ connectionStatus: 1 })');
232-
shell.assertContainsOutput(`user: '${certUser}'`);
236+
expect(await shell.executeLine('db.runCommand({ connectionStatus: 1 })'))
237+
.to.include(`user: '${certUser}'`);
238+
239+
expect(await shell.executeLine('db.getSiblingDB("$external").auth({mechanism: "MONGODB-X509"})'))
240+
.to.include('ok: 1');
241+
expect(await shell.executeLine('db.runCommand({ connectionStatus: 1 })'))
242+
.to.include(`user: '${certUser}'`);
233243
});
234244

235245
it('fails with invalid cert (args)', async() => {

packages/shell-api/src/database.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -521,9 +521,9 @@ export default class Database extends ShellApiWithMongoClass {
521521
CommonErrors.InvalidArgument
522522
);
523523
}
524-
if (!authDoc.user || !authDoc.pwd) {
524+
if ((!authDoc.user || !authDoc.pwd) && !authDoc.mechanism) {
525525
throw new MongoshInvalidInputError(
526-
'auth expects user document with \'user\' and \'pwd\' fields',
526+
'auth expects user document with at least \'user\' and \'pwd\' or \'mechanism\' fields',
527527
CommonErrors.InvalidArgument
528528
);
529529
}

0 commit comments

Comments
 (0)