Skip to content

Commit 8c8b4c3

Browse files
authored
fix(NODE-3356): update redaction logic for command monitoring events (#2847)
1 parent 2c5d440 commit 8c8b4c3

File tree

2 files changed

+12
-22
lines changed

2 files changed

+12
-22
lines changed

lib/core/connection/apm.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,20 @@ const SENSITIVE_COMMANDS = new Set([
1717
'copydb'
1818
]);
1919

20+
const HELLO_COMMANDS = new Set(['hello', 'ismaster', 'isMaster']);
21+
2022
// helper methods
2123
const extractCommandName = commandDoc => Object.keys(commandDoc)[0];
2224
const namespace = command => command.ns;
2325
const databaseName = command => command.ns.split('.')[0];
2426
const collectionName = command => command.ns.split('.')[1];
2527
const generateConnectionId = pool =>
2628
pool.options ? `${pool.options.host}:${pool.options.port}` : pool.address;
27-
const maybeRedact = (commandName, result) => (SENSITIVE_COMMANDS.has(commandName) ? {} : result);
29+
const maybeRedact = (commandName, cmd, result) =>
30+
SENSITIVE_COMMANDS.has(commandName) ||
31+
(HELLO_COMMANDS.has(commandName) && cmd.speculativeAuthenticate)
32+
? {}
33+
: result;
2834
const isLegacyPool = pool => pool.s && pool.queue;
2935

3036
const LEGACY_FIND_QUERY_MAP = {
@@ -181,17 +187,11 @@ class CommandStartedEvent {
181187
const commandName = extractCommandName(cmd);
182188
const connectionDetails = extractConnectionDetails(pool);
183189

184-
// NOTE: remove in major revision, this is not spec behavior
185-
if (SENSITIVE_COMMANDS.has(commandName)) {
186-
this.commandObj = {};
187-
this.commandObj[commandName] = true;
188-
}
189-
190190
Object.assign(this, connectionDetails, {
191191
requestId: command.requestId,
192192
databaseName: databaseName(command),
193193
commandName,
194-
command: cmd
194+
command: maybeRedact(commandName, cmd, cmd)
195195
});
196196
}
197197
}
@@ -215,7 +215,7 @@ class CommandSucceededEvent {
215215
requestId: command.requestId,
216216
commandName,
217217
duration: calculateDurationInMs(started),
218-
reply: maybeRedact(commandName, extractReply(command, reply))
218+
reply: maybeRedact(commandName, cmd, extractReply(command, reply))
219219
});
220220
}
221221
}
@@ -239,7 +239,7 @@ class CommandFailedEvent {
239239
requestId: command.requestId,
240240
commandName,
241241
duration: calculateDurationInMs(started),
242-
failure: maybeRedact(commandName, error)
242+
failure: maybeRedact(commandName, cmd, error)
243243
});
244244
}
245245
}

test/functional/apm.test.js

+2-12
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ describe('APM', function() {
622622
expect(started).to.have.length(1);
623623
expect(succeeded).to.have.length(1);
624624
expect(failed).to.have.length(0);
625-
expect(started[0].commandObj).to.eql({ getnonce: true });
625+
expect(started[0].command).to.eql({});
626626
expect(succeeded[0].reply).to.eql({});
627627
return client.close();
628628
});
@@ -1129,22 +1129,12 @@ describe('APM', function() {
11291129
describe('command monitoring unified spec tests', () => {
11301130
for (const loadedSpec of loadSpecTests('command-monitoring/unified')) {
11311131
expect(loadedSpec).to.include.all.keys(['description', 'tests']);
1132-
// TODO: NODE-3356 unskip redaction tests
1133-
const testsToSkip =
1134-
loadedSpec.description === 'redacted-commands'
1135-
? loadedSpec.tests
1136-
.map(test => test.description)
1137-
.filter(
1138-
description =>
1139-
description !== 'hello without speculative authenticate is not redacted'
1140-
)
1141-
: [];
11421132
context(String(loadedSpec.description), function() {
11431133
for (const test of loadedSpec.tests) {
11441134
it(String(test.description), {
11451135
metadata: { sessions: { skipLeakTests: true } },
11461136
test() {
1147-
return runUnifiedTest(this, loadedSpec, test, testsToSkip);
1137+
return runUnifiedTest(this, loadedSpec, test);
11481138
}
11491139
});
11501140
}

0 commit comments

Comments
 (0)