1
1
'use strict' ;
2
- const Msg = require ( '../connection/msg' ) . Msg ;
3
2
const KillCursor = require ( '../connection/commands' ) . KillCursor ;
4
3
const GetMore = require ( '../connection/commands' ) . GetMore ;
5
4
const 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 ;
21
6
22
7
// helper methods
23
- const extractCommandName = commandDoc => Object . keys ( commandDoc ) [ 0 ] ;
24
8
const namespace = command => command . ns ;
25
9
const databaseName = command => command . ns . split ( '.' ) [ 0 ] ;
26
- const collectionName = command => command . ns . split ( '.' ) [ 1 ] ;
27
10
const generateConnectionId = pool =>
28
11
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 ;
34
12
const isLegacyPool = pool => pool . s && pool . queue ;
35
13
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
-
127
14
const extractReply = ( command , reply ) => {
128
15
if ( command instanceof GetMore ) {
129
16
return {
@@ -183,15 +70,15 @@ class CommandStartedEvent {
183
70
* @param {Object } command the command
184
71
*/
185
72
constructor ( pool , command ) {
186
- const cmd = extractCommand ( command ) ;
187
- const commandName = extractCommandName ( cmd ) ;
73
+ const extractedCommand = extractCommand ( command ) ;
74
+ const commandName = extractedCommand . name ;
188
75
const connectionDetails = extractConnectionDetails ( pool ) ;
189
76
190
77
Object . assign ( this , connectionDetails , {
191
78
requestId : command . requestId ,
192
79
databaseName : databaseName ( command ) ,
193
80
commandName,
194
- command : maybeRedact ( commandName , cmd , cmd )
81
+ command : extractedCommand . shouldRedact ? { } : extractedCommand . cmd
195
82
} ) ;
196
83
}
197
84
}
@@ -207,15 +94,15 @@ class CommandSucceededEvent {
207
94
* @param {Array } started a high resolution tuple timestamp of when the command was first sent, to calculate duration
208
95
*/
209
96
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 ;
212
99
const connectionDetails = extractConnectionDetails ( pool ) ;
213
100
214
101
Object . assign ( this , connectionDetails , {
215
102
requestId : command . requestId ,
216
103
commandName,
217
104
duration : calculateDurationInMs ( started ) ,
218
- reply : maybeRedact ( commandName , cmd , extractReply ( command , reply ) )
105
+ reply : extractedCommand . shouldRedact ? { } : extractReply ( command , reply )
219
106
} ) ;
220
107
}
221
108
}
@@ -231,15 +118,15 @@ class CommandFailedEvent {
231
118
* @param {Array } started a high resolution tuple timestamp of when the command was first sent, to calculate duration
232
119
*/
233
120
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 ;
236
123
const connectionDetails = extractConnectionDetails ( pool ) ;
237
124
238
125
Object . assign ( this , connectionDetails , {
239
126
requestId : command . requestId ,
240
127
commandName,
241
128
duration : calculateDurationInMs ( started ) ,
242
- failure : maybeRedact ( commandName , cmd , error )
129
+ failure : extractedCommand . shouldRedact ? { } : error
243
130
} ) ;
244
131
}
245
132
}
0 commit comments