Skip to content

SPEC-319: Redact sensitive events #686

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/mongo/monitoring/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

require 'mongo/monitoring/event/secure'
require 'mongo/monitoring/event/command_started'
require 'mongo/monitoring/event/command_succeeded'
require 'mongo/monitoring/event/command_failed'
3 changes: 2 additions & 1 deletion lib/mongo/monitoring/event/command_started.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module Event
#
# @since 2.1.0
class CommandStarted
include Secure

# @return [ Server::Address ] address The server address.
attr_reader :address
Expand Down Expand Up @@ -57,7 +58,7 @@ def initialize(command_name, database_name, address, request_id, operation_id, c
@address = address
@request_id = request_id
@operation_id = operation_id
@command = command
@command = redacted(command_name, command)
end

# Create the event from a wire protocol message payload.
Expand Down
3 changes: 2 additions & 1 deletion lib/mongo/monitoring/event/command_succeeded.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module Event
#
# @since 2.1.0
class CommandSucceeded
include Secure

# @return [ Server::Address ] address The server address.
attr_reader :address
Expand Down Expand Up @@ -61,7 +62,7 @@ def initialize(command_name, database_name, address, request_id, operation_id, r
@address = address
@request_id = request_id
@operation_id = operation_id
@reply = reply
@reply = redacted(command_name, reply)
@duration = duration
end

Expand Down
58 changes: 58 additions & 0 deletions lib/mongo/monitoring/event/secure.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Copyright (C) 2015 MongoDB, Inc.
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an 'AS IS' BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

module Mongo
class Monitoring
module Event

# Provides behaviour to redact sensitive information from commands and
# replies.
#
# @since 2.1.0
module Secure

# The list of commands that has the data redacted for security.
#
# @since 2.1.0
REDACTED_COMMANDS = [
'authenticate',
'saslStart',
'saslContinue',
'getnonce',
'createUser',
'updateUser',
'copydbgetnonce',
'copydbsaslstart',
'copydb'
].freeze

# Redact secure information from the document if it's command is in the
# list.
#
# @example Get the redacted document.
# secure.redacted(command_name, document)
#
# @param [ String, Symbol ] command_name The command name.
# @param [ BSON::Document ] document The document.
#
# @return [ BSON::Document ] The redacted document.
#
# @since 2.1.0
def redacted(command_name, document)
REDACTED_COMMANDS.include?(command_name.to_s) ? BSON::Document.new : document
end
end
end
end
end
26 changes: 26 additions & 0 deletions spec/mongo/monitoring/event/command_started_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'spec_helper'

describe Mongo::Monitoring::Event::CommandStarted do

describe '#initialize' do

let(:address) do
Mongo::Address.new('127.0.0.1:27017')
end

let(:command) do
BSON::Document.new(test: 'value')
end

context 'when the command should be redacted' do

let(:event) do
described_class.new('copydb', 'admin', address, 1, 2, command)
end

it 'sets the command to an empty document' do
expect(event.command).to be_empty
end
end
end
end
26 changes: 26 additions & 0 deletions spec/mongo/monitoring/event/command_succeeded_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'spec_helper'

describe Mongo::Monitoring::Event::CommandSucceeded do

describe '#initialize' do

let(:address) do
Mongo::Address.new('127.0.0.1:27017')
end

let(:reply) do
BSON::Document.new(test: 'value')
end

context 'when the reply should be redacted' do

let(:event) do
described_class.new('copydb', 'admin', address, 1, 2, reply, 0.5)
end

it 'sets the reply to an empty document' do
expect(event.reply).to be_empty
end
end
end
end
57 changes: 57 additions & 0 deletions spec/mongo/monitoring/event/secure_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
require 'spec_helper'

describe Mongo::Monitoring::Event::Secure do

let(:document) do
BSON::Document.new(test: 'value')
end

let(:klass) do
Class.new do
include Mongo::Monitoring::Event::Secure
end
end

describe '#redacted' do

let(:secure) do
klass.new
end

context 'when the command must be redacted' do

context 'when the command name is a string' do

let(:redacted) do
secure.redacted('saslStart', document)
end

it 'returns an empty document' do
expect(redacted).to be_empty
end
end

context 'when the command name is a symbol' do

let(:redacted) do
secure.redacted(:saslStart, document)
end

it 'returns an empty document' do
expect(redacted).to be_empty
end
end
end

context 'when the command is not in the redacted list' do

let(:redacted) do
secure.redacted(:find, document)
end

it 'returns the document' do
expect(redacted).to eq(document)
end
end
end
end