11'use strict' ;
2- const Msg = require ( '../connection/msg' ) . Msg ;
32const KillCursor = require ( '../connection/commands' ) . KillCursor ;
43const GetMore = require ( '../connection/commands' ) . GetMore ;
54const calculateDurationInMs = require ( '../../utils' ) . calculateDurationInMs ;
6-
7- /** Commands that we want to redact because of the sensitive nature of their contents */
8- const SENSITIVE_COMMANDS = new Set ( [
9- 'authenticate' ,
10- 'saslStart' ,
11- 'saslContinue' ,
12- 'getnonce' ,
13- 'createUser' ,
14- 'updateUser' ,
15- 'copydbgetnonce' ,
16- 'copydbsaslstart' ,
17- 'copydb'
18- ] ) ;
19-
20- const HELLO_COMMANDS = new Set ( [ 'hello' , 'ismaster' , 'isMaster' ] ) ;
5+ const extractCommand = require ( '../../command_utils' ) . extractCommand ;
216
227// helper methods
23- const extractCommandName = commandDoc => Object . keys ( commandDoc ) [ 0 ] ;
248const namespace = command => command . ns ;
259const databaseName = command => command . ns . split ( '.' ) [ 0 ] ;
26- const collectionName = command => command . ns . split ( '.' ) [ 1 ] ;
2710const generateConnectionId = pool =>
2811 pool . options ? `${ pool . options . host } :${ pool . options . port } ` : pool . address ;
29- const maybeRedact = ( commandName , cmd , result ) =>
30- SENSITIVE_COMMANDS . has ( commandName ) ||
31- ( HELLO_COMMANDS . has ( commandName ) && cmd . speculativeAuthenticate )
32- ? { }
33- : result ;
3412const isLegacyPool = pool => pool . s && pool . queue ;
3513
36- const LEGACY_FIND_QUERY_MAP = {
37- $query : 'filter' ,
38- $orderby : 'sort' ,
39- $hint : 'hint' ,
40- $comment : 'comment' ,
41- $maxScan : 'maxScan' ,
42- $max : 'max' ,
43- $min : 'min' ,
44- $returnKey : 'returnKey' ,
45- $showDiskLoc : 'showRecordId' ,
46- $maxTimeMS : 'maxTimeMS' ,
47- $snapshot : 'snapshot'
48- } ;
49-
50- const LEGACY_FIND_OPTIONS_MAP = {
51- numberToSkip : 'skip' ,
52- numberToReturn : 'batchSize' ,
53- returnFieldsSelector : 'projection'
54- } ;
55-
56- const OP_QUERY_KEYS = [
57- 'tailable' ,
58- 'oplogReplay' ,
59- 'noCursorTimeout' ,
60- 'awaitData' ,
61- 'partial' ,
62- 'exhaust'
63- ] ;
64-
65- /**
66- * Extract the actual command from the query, possibly upconverting if it's a legacy
67- * format
68- *
69- * @param {Object } command the command
70- */
71- const extractCommand = command => {
72- if ( command instanceof GetMore ) {
73- return {
74- getMore : command . cursorId ,
75- collection : collectionName ( command ) ,
76- batchSize : command . numberToReturn
77- } ;
78- }
79-
80- if ( command instanceof KillCursor ) {
81- return {
82- killCursors : collectionName ( command ) ,
83- cursors : command . cursorIds
84- } ;
85- }
86-
87- if ( command instanceof Msg ) {
88- return command . command ;
89- }
90-
91- if ( command . query && command . query . $query ) {
92- let result ;
93- if ( command . ns === 'admin.$cmd' ) {
94- // upconvert legacy command
95- result = Object . assign ( { } , command . query . $query ) ;
96- } else {
97- // upconvert legacy find command
98- result = { find : collectionName ( command ) } ;
99- Object . keys ( LEGACY_FIND_QUERY_MAP ) . forEach ( key => {
100- if ( typeof command . query [ key ] !== 'undefined' )
101- result [ LEGACY_FIND_QUERY_MAP [ key ] ] = command . query [ key ] ;
102- } ) ;
103- }
104-
105- Object . keys ( LEGACY_FIND_OPTIONS_MAP ) . forEach ( key => {
106- if ( typeof command [ key ] !== 'undefined' ) result [ LEGACY_FIND_OPTIONS_MAP [ key ] ] = command [ key ] ;
107- } ) ;
108-
109- OP_QUERY_KEYS . forEach ( key => {
110- if ( command [ key ] ) result [ key ] = command [ key ] ;
111- } ) ;
112-
113- if ( typeof command . pre32Limit !== 'undefined' ) {
114- result . limit = command . pre32Limit ;
115- }
116-
117- if ( command . query . $explain ) {
118- return { explain : result } ;
119- }
120-
121- return result ;
122- }
123-
124- return command . query ? command . query : command ;
125- } ;
126-
12714const extractReply = ( command , reply ) => {
12815 if ( command instanceof GetMore ) {
12916 return {
@@ -183,15 +70,15 @@ class CommandStartedEvent {
18370 * @param {Object } command the command
18471 */
18572 constructor ( pool , command ) {
186- const cmd = extractCommand ( command ) ;
187- const commandName = extractCommandName ( cmd ) ;
73+ const extractedCommand = extractCommand ( command ) ;
74+ const commandName = extractedCommand . name ;
18875 const connectionDetails = extractConnectionDetails ( pool ) ;
18976
19077 Object . assign ( this , connectionDetails , {
19178 requestId : command . requestId ,
19279 databaseName : databaseName ( command ) ,
19380 commandName,
194- command : maybeRedact ( commandName , cmd , cmd )
81+ command : extractedCommand . shouldRedact ? { } : extractedCommand . cmd
19582 } ) ;
19683 }
19784}
@@ -207,15 +94,15 @@ class CommandSucceededEvent {
20794 * @param {Array } started a high resolution tuple timestamp of when the command was first sent, to calculate duration
20895 */
20996 constructor ( pool , command , reply , started ) {
210- const cmd = extractCommand ( command ) ;
211- const commandName = extractCommandName ( cmd ) ;
97+ const extractedCommand = extractCommand ( command ) ;
98+ const commandName = extractedCommand . name ;
21299 const connectionDetails = extractConnectionDetails ( pool ) ;
213100
214101 Object . assign ( this , connectionDetails , {
215102 requestId : command . requestId ,
216103 commandName,
217104 duration : calculateDurationInMs ( started ) ,
218- reply : maybeRedact ( commandName , cmd , extractReply ( command , reply ) )
105+ reply : extractedCommand . shouldRedact ? { } : extractReply ( command , reply )
219106 } ) ;
220107 }
221108}
@@ -231,15 +118,15 @@ class CommandFailedEvent {
231118 * @param {Array } started a high resolution tuple timestamp of when the command was first sent, to calculate duration
232119 */
233120 constructor ( pool , command , error , started ) {
234- const cmd = extractCommand ( command ) ;
235- const commandName = extractCommandName ( cmd ) ;
121+ const extractedCommand = extractCommand ( command ) ;
122+ const commandName = extractedCommand . name ;
236123 const connectionDetails = extractConnectionDetails ( pool ) ;
237124
238125 Object . assign ( this , connectionDetails , {
239126 requestId : command . requestId ,
240127 commandName,
241128 duration : calculateDurationInMs ( started ) ,
242- failure : maybeRedact ( commandName , cmd , error )
129+ failure : extractedCommand . shouldRedact ? { } : error
243130 } ) ;
244131 }
245132}
0 commit comments