Skip to content

Commit 77867aa

Browse files
committed
implement connect
1 parent 5187a63 commit 77867aa

File tree

13 files changed

+177
-36
lines changed

13 files changed

+177
-36
lines changed

packages/cli-repl/src/cli-repl.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { LineByLineInput } from './line-by-line-input';
88
import { TELEMETRY, MONGOSH_WIKI } from './constants';
99
import isRecoverableError from 'is-recoverable-error';
1010
import { MongoshWarning } from '@mongosh/errors';
11-
import { changeHistory } from '@mongosh/history';
11+
import { changeHistory, retractPassword } from '@mongosh/history';
1212
import { REPLServer, Recoverable } from 'repl';
1313
import jsonParse from 'fast-json-parse';
1414
import CliOptions from './cli-options';
@@ -17,7 +17,6 @@ import i18n from '@mongosh/i18n';
1717
import { ObjectId } from 'bson';
1818
import repl from 'pretty-repl';
1919
import Nanobus from 'nanobus';
20-
import { redactPwd } from '.';
2120
import logger from './logger';
2221
import mkdirp from 'mkdirp';
2322
import clr from './clr';
@@ -95,7 +94,7 @@ class CliRepl {
9594
* @param {NodeOptions} driverOptions - The driver options.
9695
*/
9796
async connect(driverUri: string, driverOptions: NodeOptions): Promise<any> {
98-
console.log(i18n.__(CONNECTING), ' ', clr(redactPwd(driverUri), ['bold', 'green']));
97+
console.log(i18n.__(CONNECTING), ' ', clr(retractPassword(driverUri), ['bold', 'green']));
9998
return await CliServiceProvider.connect(driverUri, driverOptions);
10099
}
101100

packages/cli-repl/src/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import CliRepl from './cli-repl';
2-
import redactPwd from './redact-pwd';
32
import parseCliArgs from './arg-parser';
43
import mapCliToDriver from './arg-mapper';
54
import generateUri from './uri-generator';
@@ -15,7 +14,6 @@ export {
1514
TELEMETRY,
1615
MONGOSH_WIKI,
1716
CliRepl,
18-
redactPwd,
1917
completer,
2018
generateUri,
2119
parseCliArgs,

packages/cli-repl/src/logger.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import redactInfo from 'mongodb-redact';
44
import Analytics from 'analytics-node';
5-
import redactPwd from './redact-pwd';
5+
import { retractPassword } from '@mongosh/history';
66
import { ObjectId } from 'bson';
77
import pino from 'pino';
88
import path from 'path';
@@ -74,7 +74,7 @@ export default function logger(bus: any, logDir: string): void {
7474
}
7575

7676
bus.on('mongosh:connect', function(args: ConnectEvent) {
77-
const connectionUri = redactPwd(args.uri);
77+
const connectionUri = retractPassword(args.uri);
7878
delete args.uri;
7979
const params = { session_id, userId, connectionUri, ...args };
8080
log.info('mongosh:connect', params);

packages/history/src/history.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,21 @@ describe('changeHistory', () => {
1111
changeHistory(i);
1212
expect(i).to.deep.equal(history);
1313
});
14+
it('removes connect commands from history', () => {
15+
const i = ['db = connect(\'uri\', \'u\', \'p\')', 'db.shipwrecks.findOne()', 'use ships'];
16+
changeHistory(i);
17+
expect(i).to.deep.equal(history);
18+
});
19+
it('removes URI having Mongo from history', () => {
20+
const i = ['m = new Mongo(\'mongodb://anna:[email protected]:27017/test\')', 'db.shipwrecks.findOne()', 'use ships'];
21+
changeHistory(i);
22+
expect(i).to.deep.equal(['m = new Mongo(\'mongodb://<credentials>@127.0.0.1:27017/test\')'].concat(history));
23+
});
24+
it('removes URI having Mongo from history for srv', () => {
25+
const i = ['m = new Mongo(\'mongodb+srv://admin:[email protected]/admin\')', 'db.shipwrecks.findOne()', 'use ships'];
26+
changeHistory(i);
27+
expect(i).to.deep.equal(['m = new Mongo(\'mongodb+srv://admin:[email protected]/admin\')'].concat(history));
28+
});
1429

1530
it('leaves history as is if command is not sensitive', () => {
1631
const i = ['db.shipwrecks.find({quasou: "depth unknown"})', 'db.shipwrecks.findOne()', 'use ships'];

packages/history/src/history.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import redactInfo from 'mongodb-redact';
2+
import redactPwd from './redact-pwd';
23

34
/**
45
* Modifies command history array based on sensitive information.
@@ -11,12 +12,16 @@ import redactInfo from 'mongodb-redact';
1112
*/
1213
export function changeHistory(history: string[], redact = false): void {
1314
const hiddenCommands =
14-
RegExp('createUser|auth|updateUser|changeUserPassword', 'g');
15+
RegExp('createUser|auth|updateUser|changeUserPassword|connect', 'g');
1516

1617
if (hiddenCommands.test(history[0])) {
1718
history.shift();
1819
return;
1920
}
21+
if (/Mongo\(([^+)]+)\)/g.test(history[0])) {
22+
history[0] = history[0].replace(/Mongo\(([^+)]+)\)/g, (substr) => redactPwd(substr));
23+
return;
24+
}
2025

2126
if (redact) history[0] = redactInfo(history[0]);
2227
}

packages/history/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
export * from './history';
2+
import retractPassword from './redact-pwd';
3+
export { retractPassword };
File renamed without changes.
File renamed without changes.

packages/shell-api/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"dependencies": {
3636
"@mongosh/async-rewriter": "^0.0.5",
3737
"@mongosh/errors": "^0.0.5",
38+
"@mongosh/history": "^0.0.5",
3839
"@mongosh/i18n": "^0.0.5",
3940
"@mongosh/service-provider-core": "^0.0.5",
4041
"bson": "^4.0.4"

packages/shell-api/src/mongo.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import Database from './database';
1313
import ShellInternalState from './shell-internal-state';
1414
import { CommandResult } from './result';
1515
import { MongoshInternalError, MongoshInvalidInputError } from '@mongosh/errors';
16+
import { retractPassword } from '@mongosh/history';
1617

1718
@shellApiClassDefault
1819
@hasAsyncChild
@@ -39,7 +40,7 @@ export default class Mongo extends ShellApiClass {
3940
}
4041

4142
toReplString(): any {
42-
return this.uri;
43+
return retractPassword(this.uri);
4344
}
4445

4546
async connect(): Promise<void> {

0 commit comments

Comments
 (0)