Skip to content

Commit 039c8ec

Browse files
committed
Merge pull request #686 from mongodb/SPEC-319
SPEC-319: Redact sensitive events
2 parents dacf2e0 + 9efe358 commit 039c8ec

File tree

7 files changed

+172
-2
lines changed

7 files changed

+172
-2
lines changed

lib/mongo/monitoring/event.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
require 'mongo/monitoring/event/secure'
1516
require 'mongo/monitoring/event/command_started'
1617
require 'mongo/monitoring/event/command_succeeded'
1718
require 'mongo/monitoring/event/command_failed'

lib/mongo/monitoring/event/command_started.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ module Event
2020
#
2121
# @since 2.1.0
2222
class CommandStarted
23+
include Secure
2324

2425
# @return [ Server::Address ] address The server address.
2526
attr_reader :address
@@ -57,7 +58,7 @@ def initialize(command_name, database_name, address, request_id, operation_id, c
5758
@address = address
5859
@request_id = request_id
5960
@operation_id = operation_id
60-
@command = command
61+
@command = redacted(command_name, command)
6162
end
6263

6364
# Create the event from a wire protocol message payload.

lib/mongo/monitoring/event/command_succeeded.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ module Event
2020
#
2121
# @since 2.1.0
2222
class CommandSucceeded
23+
include Secure
2324

2425
# @return [ Server::Address ] address The server address.
2526
attr_reader :address
@@ -61,7 +62,7 @@ def initialize(command_name, database_name, address, request_id, operation_id, r
6162
@address = address
6263
@request_id = request_id
6364
@operation_id = operation_id
64-
@reply = reply
65+
@reply = redacted(command_name, reply)
6566
@duration = duration
6667
end
6768

lib/mongo/monitoring/event/secure.rb

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Copyright (C) 2015 MongoDB, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the 'License');
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an 'AS IS' BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
module Mongo
16+
class Monitoring
17+
module Event
18+
19+
# Provides behaviour to redact sensitive information from commands and
20+
# replies.
21+
#
22+
# @since 2.1.0
23+
module Secure
24+
25+
# The list of commands that has the data redacted for security.
26+
#
27+
# @since 2.1.0
28+
REDACTED_COMMANDS = [
29+
'authenticate',
30+
'saslStart',
31+
'saslContinue',
32+
'getnonce',
33+
'createUser',
34+
'updateUser',
35+
'copydbgetnonce',
36+
'copydbsaslstart',
37+
'copydb'
38+
].freeze
39+
40+
# Redact secure information from the document if it's command is in the
41+
# list.
42+
#
43+
# @example Get the redacted document.
44+
# secure.redacted(command_name, document)
45+
#
46+
# @param [ String, Symbol ] command_name The command name.
47+
# @param [ BSON::Document ] document The document.
48+
#
49+
# @return [ BSON::Document ] The redacted document.
50+
#
51+
# @since 2.1.0
52+
def redacted(command_name, document)
53+
REDACTED_COMMANDS.include?(command_name.to_s) ? BSON::Document.new : document
54+
end
55+
end
56+
end
57+
end
58+
end
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
require 'spec_helper'
2+
3+
describe Mongo::Monitoring::Event::CommandStarted do
4+
5+
describe '#initialize' do
6+
7+
let(:address) do
8+
Mongo::Address.new('127.0.0.1:27017')
9+
end
10+
11+
let(:command) do
12+
BSON::Document.new(test: 'value')
13+
end
14+
15+
context 'when the command should be redacted' do
16+
17+
let(:event) do
18+
described_class.new('copydb', 'admin', address, 1, 2, command)
19+
end
20+
21+
it 'sets the command to an empty document' do
22+
expect(event.command).to be_empty
23+
end
24+
end
25+
end
26+
end
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
require 'spec_helper'
2+
3+
describe Mongo::Monitoring::Event::CommandSucceeded do
4+
5+
describe '#initialize' do
6+
7+
let(:address) do
8+
Mongo::Address.new('127.0.0.1:27017')
9+
end
10+
11+
let(:reply) do
12+
BSON::Document.new(test: 'value')
13+
end
14+
15+
context 'when the reply should be redacted' do
16+
17+
let(:event) do
18+
described_class.new('copydb', 'admin', address, 1, 2, reply, 0.5)
19+
end
20+
21+
it 'sets the reply to an empty document' do
22+
expect(event.reply).to be_empty
23+
end
24+
end
25+
end
26+
end
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
require 'spec_helper'
2+
3+
describe Mongo::Monitoring::Event::Secure do
4+
5+
let(:document) do
6+
BSON::Document.new(test: 'value')
7+
end
8+
9+
let(:klass) do
10+
Class.new do
11+
include Mongo::Monitoring::Event::Secure
12+
end
13+
end
14+
15+
describe '#redacted' do
16+
17+
let(:secure) do
18+
klass.new
19+
end
20+
21+
context 'when the command must be redacted' do
22+
23+
context 'when the command name is a string' do
24+
25+
let(:redacted) do
26+
secure.redacted('saslStart', document)
27+
end
28+
29+
it 'returns an empty document' do
30+
expect(redacted).to be_empty
31+
end
32+
end
33+
34+
context 'when the command name is a symbol' do
35+
36+
let(:redacted) do
37+
secure.redacted(:saslStart, document)
38+
end
39+
40+
it 'returns an empty document' do
41+
expect(redacted).to be_empty
42+
end
43+
end
44+
end
45+
46+
context 'when the command is not in the redacted list' do
47+
48+
let(:redacted) do
49+
secure.redacted(:find, document)
50+
end
51+
52+
it 'returns the document' do
53+
expect(redacted).to eq(document)
54+
end
55+
end
56+
end
57+
end

0 commit comments

Comments
 (0)