-
Notifications
You must be signed in to change notification settings - Fork 533
RUBY-1021 Use Redacted options class to redact option fields when printing #687
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
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
8a6d068
RUBY-1021 Use SensitiveOptions class to redact option fields when pri…
estolfo ea03c84
RUBY-1021 to_s method
estolfo 4c6de4c
Get specs passing and change name to Options::Redacted
estolfo fec72a9
RUBY-1021 Test and documentation for Redacted Options
estolfo 1338f16
RUBY-1021 Ensure options merged with client options are Options::Reda…
estolfo afbe202
RUBY-1021 Use helper method to build string
estolfo 63b4e31
RUBY-1021 Initialize options to Redacted and update uri parser
estolfo 26c1939
RUBY-1021 Tests for inspect methods
estolfo ca239e5
RUBY-1021 initialize read preference with redacted options in FSBucket
estolfo 2748bf5
RUBY-1021 use more unique string for password in tests
estolfo 866d024
RUBY-1021 implement #select and #reject for Options::Redacted
estolfo 42b6283
RUBY-1021 more unique password string
estolfo 0a33b88
RUBY-1021 Take out unnecessary self refs
estolfo 25a87b9
RUBY-1021 Document #reject! and #select! returning nil if unchanged
estolfo 01c9718
RUBY-1021 fix spec typos
estolfo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ | |
# limitations under the License. | ||
|
||
require 'mongo/options/mapper' | ||
require 'mongo/options/redacted' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
# 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 | ||
module Options | ||
|
||
# Class for wrapping options that could be sensitive. | ||
# When printed, the sensitive values will be redacted. | ||
# | ||
# @since 2.1.0 | ||
class Redacted < BSON::Document | ||
|
||
# The options whose values will be redacted. | ||
# | ||
# @since 2.1.0 | ||
SENSITIVE_OPTIONS = [ :password, | ||
:pwd ].freeze | ||
|
||
# The replacement string used in place of the value for sensitive keys. | ||
# | ||
# @since 2.1.0 | ||
STRING_REPLACEMENT = '<REDACTED>'.freeze | ||
|
||
# Get a string representation of the options. | ||
# | ||
# @return [ String ] The string representation of the options. | ||
# | ||
# @since 2.1.0 | ||
def inspect | ||
redacted_string(:inspect) | ||
end | ||
|
||
# Get a string representation of the options. | ||
# | ||
# @return [ String ] The string representation of the options. | ||
# | ||
# @since 2.1.0 | ||
def to_s | ||
redacted_string(:to_s) | ||
end | ||
|
||
# Whether these options contain a given key. | ||
# | ||
# @example Determine if the options contain a given key. | ||
# options.has_key?(:name) | ||
# | ||
# @param [ String, Symbol ] key The key to check for existence. | ||
# | ||
# @return [ true, false ] If the options contain the given key. | ||
# | ||
# @since 2.1.0 | ||
def has_key?(key) | ||
super(convert_key(key)) | ||
end | ||
alias_method :key?, :has_key? | ||
|
||
# Returns a new options object consisting of pairs for which the block returns false. | ||
# | ||
# @example Get a new options object with pairs for which the block returns false. | ||
# new_options = options.reject { |k, v| k == 'database' } | ||
# | ||
# @yieldparam [ String, Object ] The key as a string and its value. | ||
# | ||
# @return [ Options::Redacted ] A new options object. | ||
# | ||
# @since 2.1.0 | ||
def reject(&block) | ||
new_options = dup | ||
new_options.reject!(&block) || new_options | ||
end | ||
|
||
# Only keeps pairs for which the block returns false. | ||
# | ||
# @example Remove pairs from this object for which the block returns true. | ||
# options.reject! { |k, v| k == 'database' } | ||
# | ||
# @yieldparam [ String, Object ] The key as a string and its value. | ||
# | ||
# @return [ Options::Redacted, nil ] This object or nil if no changes were made. | ||
# | ||
# @since 2.1.0 | ||
def reject! | ||
if block_given? | ||
n_keys = keys.size | ||
keys.each do |key| | ||
delete(key) if yield(key, self[key]) | ||
end | ||
n_keys == keys.size ? nil : self | ||
else | ||
to_enum | ||
end | ||
end | ||
|
||
# Returns a new options object consisting of pairs for which the block returns true. | ||
# | ||
# @example Get a new options object with pairs for which the block returns true. | ||
# ssl_options = options.select { |k, v| k =~ /ssl/ } | ||
# | ||
# @yieldparam [ String, Object ] The key as a string and its value. | ||
# | ||
# @return [ Options::Redacted ] A new options object. | ||
# | ||
# @since 2.1.0 | ||
def select(&block) | ||
new_options = dup | ||
new_options.select!(&block) || new_options | ||
end | ||
|
||
# Only keeps pairs for which the block returns true. | ||
# | ||
# @example Remove pairs from this object for which the block does not return true. | ||
# options.select! { |k, v| k =~ /ssl/ } | ||
# | ||
# @yieldparam [ String, Object ] The key as a string and its value. | ||
# | ||
# @return [ Options::Redacted, nil ] This object or nil if no changes were made. | ||
# | ||
# @since 2.1.0 | ||
def select! | ||
if block_given? | ||
n_keys = keys.size | ||
keys.each do |key| | ||
delete(key) unless yield(key, self[key]) | ||
end | ||
n_keys == keys.size ? nil : self | ||
else | ||
to_enum | ||
end | ||
end | ||
|
||
private | ||
|
||
def redacted_string(method) | ||
'{' + reduce([]) do |list, (k, v)| | ||
list << "#{k.send(method)}=>#{redact(k, v, method)}" | ||
end.join(', ') + '}' | ||
end | ||
|
||
def redact(k, v, method) | ||
return STRING_REPLACEMENT if SENSITIVE_OPTIONS.include?(k.to_sym) | ||
v.send(method) | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would
freeze
this.