Skip to content

fix(NODE-3356): update redaction logic for command monitoring events #2849

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/cmap/command_monitoring_events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class CommandStartedEvent {
this.requestId = command.requestId;
this.databaseName = databaseName(command);
this.commandName = commandName;
this.command = cmd;
this.command = maybeRedact(commandName, cmd, cmd);
}
}

Expand Down Expand Up @@ -82,7 +82,7 @@ export class CommandSucceededEvent {
this.requestId = command.requestId;
this.commandName = commandName;
this.duration = calculateDurationInMs(started);
this.reply = maybeRedact(commandName, extractReply(command, reply));
this.reply = maybeRedact(commandName, cmd, extractReply(command, reply));
}
}

Expand Down Expand Up @@ -123,7 +123,7 @@ export class CommandFailedEvent {
this.requestId = command.requestId;
this.commandName = commandName;
this.duration = calculateDurationInMs(started);
this.failure = maybeRedact(commandName, error) as Error;
this.failure = maybeRedact(commandName, cmd, error) as Error;
}
}

Expand All @@ -140,13 +140,18 @@ const SENSITIVE_COMMANDS = new Set([
'copydb'
]);

const HELLO_COMMANDS = new Set(['hello', 'ismaster', 'isMaster']);

// helper methods
const extractCommandName = (commandDoc: Document) => Object.keys(commandDoc)[0];
const namespace = (command: WriteProtocolMessageType) => command.ns;
const databaseName = (command: WriteProtocolMessageType) => command.ns.split('.')[0];
const collectionName = (command: WriteProtocolMessageType) => command.ns.split('.')[1];
const maybeRedact = (commandName: string, result?: Error | Document) =>
SENSITIVE_COMMANDS.has(commandName) ? {} : result;
const maybeRedact = (commandName: string, commandDoc: Document, result: Error | Document) =>
SENSITIVE_COMMANDS.has(commandName) ||
(HELLO_COMMANDS.has(commandName) && commandDoc.speculativeAuthenticate)
? {}
: result;

const LEGACY_FIND_QUERY_MAP: { [key: string]: string } = {
$query: 'filter',
Expand Down
14 changes: 2 additions & 12 deletions test/functional/apm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ describe('APM', function () {
expect(started).to.have.length(1);
expect(succeeded).to.have.length(1);
expect(failed).to.have.length(0);
expect(started[0].commandObj).to.eql({ getnonce: true });
expect(started[0].command).to.eql({});
expect(succeeded[0].reply).to.eql({});
return client.close();
});
Expand Down Expand Up @@ -969,22 +969,12 @@ describe('APM', function () {
describe('command monitoring unified spec tests', () => {
for (const loadedSpec of loadSpecTests('command-monitoring/unified')) {
expect(loadedSpec).to.include.all.keys(['description', 'tests']);
// TODO: NODE-3356 unskip redaction tests
const testsToSkip =
loadedSpec.description === 'redacted-commands'
? loadedSpec.tests
.map(test => test.description)
.filter(
description =>
description !== 'hello without speculative authenticate is not redacted'
)
: [];
context(String(loadedSpec.description), function () {
for (const test of loadedSpec.tests) {
it(String(test.description), {
metadata: { sessions: { skipLeakTests: true } },
test: async function () {
await runUnifiedTest(this, loadedSpec, test, testsToSkip);
await runUnifiedTest(this, loadedSpec, test);
}
});
}
Expand Down