Skip to content

Commit a10066a

Browse files
committed
RUBY-1021 Use SensitiveOptions class to redact option fields when printing
1 parent c6abea6 commit a10066a

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

lib/mongo/client.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,14 +292,14 @@ def list_databases
292292
private
293293

294294
def create_from_addresses(addresses, opts = {})
295-
@options = Database::DEFAULT_OPTIONS.merge(opts).freeze
295+
@options = Options::SensitiveOptions.new.merge(Database::DEFAULT_OPTIONS.merge(opts)).freeze
296296
@cluster = Cluster.new(addresses, @monitoring, options)
297297
@database = Database.new(self, options[:database], options)
298298
end
299299

300300
def create_from_uri(connection_string, opts = {})
301301
uri = URI.new(connection_string, opts)
302-
@options = Database::DEFAULT_OPTIONS.merge(uri.client_options.merge(opts)).freeze
302+
@options = Options::SensitiveOptions.new.merge(Database::DEFAULT_OPTIONS.merge(uri.client_options.merge(opts))).freeze
303303
@cluster = Cluster.new(uri.servers, @monitoring, options)
304304
@database = Database.new(self, options[:database], options)
305305
end

lib/mongo/options.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@
1313
# limitations under the License.
1414

1515
require 'mongo/options/mapper'
16+
require 'mongo/options/sensitive_options'
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
module Options
17+
18+
SENSITIVE_OPTIONS = [:password, :pwd]
19+
REDACTED_STRING = '<REDACTED>'
20+
21+
class SensitiveOptions < BSON::Document
22+
23+
def inspect
24+
'{' + reduce('') do |string, (k, v)|
25+
string << "#{k}=>#{redact(k,v)}"
26+
end + '}'
27+
end
28+
29+
private
30+
31+
def redact(k, v)
32+
return REDACTED_STRING if SENSITIVE_OPTIONS.include?(k.to_sym)
33+
v.inspect
34+
end
35+
end
36+
end
37+
end

lib/mongo/server/connection.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ def deliver(messages)
183183
def setup_authentication!
184184
if options[:user]
185185
default_mechanism = @server.features.scram_sha_1_enabled? ? :scram : :mongodb_cr
186-
user = Auth::User.new({ :auth_mech => default_mechanism }.merge(options))
186+
user = Auth::User.new(Options::SensitiveOptions.new(:auth_mech => default_mechanism).merge(options))
187187
@authenticator = Auth.get(user)
188188
end
189189
end

0 commit comments

Comments
 (0)