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
8
const namespace = command => command . ns ;
24
9
const databaseName = command => command . ns . split ( '.' ) [ 0 ] ;
25
- const collectionName = command => command . ns . split ( '.' ) [ 1 ] ;
26
10
const generateConnectionId = pool =>
27
11
pool . options ? `${ pool . options . host } :${ pool . options . port } ` : pool . address ;
28
- const shouldRedactCommand = ( commandName , cmd ) =>
29
- SENSITIVE_COMMANDS . has ( commandName ) ||
30
- ( HELLO_COMMANDS . has ( commandName ) && ! ! cmd . speculativeAuthenticate ) ;
31
12
const isLegacyPool = pool => pool . s && pool . queue ;
32
13
33
- const LEGACY_FIND_QUERY_MAP = {
34
- $query : 'filter' ,
35
- $orderby : 'sort' ,
36
- $hint : 'hint' ,
37
- $comment : 'comment' ,
38
- $maxScan : 'maxScan' ,
39
- $max : 'max' ,
40
- $min : 'min' ,
41
- $returnKey : 'returnKey' ,
42
- $showDiskLoc : 'showRecordId' ,
43
- $maxTimeMS : 'maxTimeMS' ,
44
- $snapshot : 'snapshot'
45
- } ;
46
-
47
- const LEGACY_FIND_OPTIONS_MAP = {
48
- numberToSkip : 'skip' ,
49
- numberToReturn : 'batchSize' ,
50
- returnFieldsSelector : 'projection'
51
- } ;
52
-
53
- const OP_QUERY_KEYS = [
54
- 'tailable' ,
55
- 'oplogReplay' ,
56
- 'noCursorTimeout' ,
57
- 'awaitData' ,
58
- 'partial' ,
59
- 'exhaust'
60
- ] ;
61
-
62
- /**
63
- * Extract the actual command from the query, possibly upconverting if it's a legacy
64
- * format
65
- *
66
- * @param {Object } command the command
67
- */
68
- const extractCommand = command => {
69
- let extractedCommand ;
70
- if ( command instanceof GetMore ) {
71
- extractedCommand = {
72
- getMore : command . cursorId ,
73
- collection : collectionName ( command ) ,
74
- batchSize : command . numberToReturn
75
- } ;
76
- } else if ( command instanceof KillCursor ) {
77
- extractedCommand = {
78
- killCursors : collectionName ( command ) ,
79
- cursors : command . cursorIds
80
- } ;
81
- } else if ( command instanceof Msg ) {
82
- extractedCommand = command . command ;
83
- } else if ( command . query && command . query . $query ) {
84
- let result ;
85
- if ( command . ns === 'admin.$cmd' ) {
86
- // upconvert legacy command
87
- result = Object . assign ( { } , command . query . $query ) ;
88
- } else {
89
- // upconvert legacy find command
90
- result = { find : collectionName ( command ) } ;
91
- Object . keys ( LEGACY_FIND_QUERY_MAP ) . forEach ( key => {
92
- if ( typeof command . query [ key ] !== 'undefined' )
93
- result [ LEGACY_FIND_QUERY_MAP [ key ] ] = command . query [ key ] ;
94
- } ) ;
95
- }
96
-
97
- Object . keys ( LEGACY_FIND_OPTIONS_MAP ) . forEach ( key => {
98
- if ( typeof command [ key ] !== 'undefined' ) result [ LEGACY_FIND_OPTIONS_MAP [ key ] ] = command [ key ] ;
99
- } ) ;
100
-
101
- OP_QUERY_KEYS . forEach ( key => {
102
- if ( command [ key ] ) result [ key ] = command [ key ] ;
103
- } ) ;
104
-
105
- if ( typeof command . pre32Limit !== 'undefined' ) {
106
- result . limit = command . pre32Limit ;
107
- }
108
-
109
- if ( command . query . $explain ) {
110
- extractedCommand = { explain : result } ;
111
- } else {
112
- extractedCommand = result ;
113
- }
114
- } else {
115
- extractedCommand = command . query || command ;
116
- }
117
-
118
- return { cmd : extractedCommand , name : Object . keys ( extractedCommand ) [ 0 ] } ;
119
- } ;
120
-
121
14
const extractReply = ( command , reply ) => {
122
15
if ( command instanceof GetMore ) {
123
16
return {
@@ -178,15 +71,14 @@ class CommandStartedEvent {
178
71
*/
179
72
constructor ( pool , command ) {
180
73
const extractedCommand = extractCommand ( command ) ;
181
- const cmd = extractedCommand . cmd ;
182
74
const commandName = extractedCommand . name ;
183
75
const connectionDetails = extractConnectionDetails ( pool ) ;
184
76
185
77
Object . assign ( this , connectionDetails , {
186
78
requestId : command . requestId ,
187
79
databaseName : databaseName ( command ) ,
188
80
commandName,
189
- command : shouldRedactCommand ( commandName , cmd ) ? { } : cmd
81
+ command : extractedCommand . shouldRedact ? { } : extractedCommand . cmd
190
82
} ) ;
191
83
}
192
84
}
@@ -203,15 +95,14 @@ class CommandSucceededEvent {
203
95
*/
204
96
constructor ( pool , command , reply , started ) {
205
97
const extractedCommand = extractCommand ( command ) ;
206
- const cmd = extractedCommand . cmd ;
207
98
const commandName = extractedCommand . name ;
208
99
const connectionDetails = extractConnectionDetails ( pool ) ;
209
100
210
101
Object . assign ( this , connectionDetails , {
211
102
requestId : command . requestId ,
212
103
commandName,
213
104
duration : calculateDurationInMs ( started ) ,
214
- reply : shouldRedactCommand ( commandName , cmd ) ? { } : extractReply ( command , reply )
105
+ reply : extractedCommand . shouldRedact ? { } : extractReply ( command , reply )
215
106
} ) ;
216
107
}
217
108
}
@@ -228,15 +119,14 @@ class CommandFailedEvent {
228
119
*/
229
120
constructor ( pool , command , error , started ) {
230
121
const extractedCommand = extractCommand ( command ) ;
231
- const cmd = extractedCommand . cmd ;
232
122
const commandName = extractedCommand . name ;
233
123
const connectionDetails = extractConnectionDetails ( pool ) ;
234
124
235
125
Object . assign ( this , connectionDetails , {
236
126
requestId : command . requestId ,
237
127
commandName,
238
128
duration : calculateDurationInMs ( started ) ,
239
- failure : shouldRedactCommand ( commandName , cmd ) ? { } : error
129
+ failure : extractedCommand . shouldRedact ? { } : error
240
130
} ) ;
241
131
}
242
132
}
0 commit comments