|
| 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 | + # Class for wrapping options that could be sensitive. |
| 19 | + # When printed, the sensitive values will be redacted. |
| 20 | + # |
| 21 | + # @since 2.1.0 |
| 22 | + class Redacted < BSON::Document |
| 23 | + |
| 24 | + # The options whose values will be redacted. |
| 25 | + # |
| 26 | + # @since 2.1.0 |
| 27 | + SENSITIVE_OPTIONS = [ :password, |
| 28 | + :pwd ].freeze |
| 29 | + |
| 30 | + # The replacement string used in place of the value for sensitive keys. |
| 31 | + # |
| 32 | + # @since 2.1.0 |
| 33 | + STRING_REPLACEMENT = '<REDACTED>'.freeze |
| 34 | + |
| 35 | + # Get a string representation of the options. |
| 36 | + # |
| 37 | + # @return [ String ] The string representation of the options. |
| 38 | + # |
| 39 | + # @since 2.1.0 |
| 40 | + def inspect |
| 41 | + redacted_string(:inspect) |
| 42 | + end |
| 43 | + |
| 44 | + # Get a string representation of the options. |
| 45 | + # |
| 46 | + # @return [ String ] The string representation of the options. |
| 47 | + # |
| 48 | + # @since 2.1.0 |
| 49 | + def to_s |
| 50 | + redacted_string(:to_s) |
| 51 | + end |
| 52 | + |
| 53 | + # Whether these options contain a given key. |
| 54 | + # |
| 55 | + # @example Determine if the options contain a given key. |
| 56 | + # options.has_key?(:name) |
| 57 | + # |
| 58 | + # @param [ String, Symbol ] key The key to check for existence. |
| 59 | + # |
| 60 | + # @return [ true, false ] If the options contain the given key. |
| 61 | + # |
| 62 | + # @since 2.1.0 |
| 63 | + def has_key?(key) |
| 64 | + super(convert_key(key)) |
| 65 | + end |
| 66 | + alias_method :key?, :has_key? |
| 67 | + |
| 68 | + # Returns a new options object consisting of pairs for which the block returns false. |
| 69 | + # |
| 70 | + # @example Get a new options object with pairs for which the block returns false. |
| 71 | + # new_options = options.reject { |k, v| k == 'database' } |
| 72 | + # |
| 73 | + # @yieldparam [ String, Object ] The key as a string and its value. |
| 74 | + # |
| 75 | + # @return [ Options::Redacted ] A new options object. |
| 76 | + # |
| 77 | + # @since 2.1.0 |
| 78 | + def reject(&block) |
| 79 | + new_options = dup |
| 80 | + new_options.reject!(&block) || new_options |
| 81 | + end |
| 82 | + |
| 83 | + # Only keeps pairs for which the block returns false. |
| 84 | + # |
| 85 | + # @example Remove pairs from this object for which the block returns true. |
| 86 | + # options.reject! { |k, v| k == 'database' } |
| 87 | + # |
| 88 | + # @yieldparam [ String, Object ] The key as a string and its value. |
| 89 | + # |
| 90 | + # @return [ Options::Redacted, nil ] This object or nil if no changes were made. |
| 91 | + # |
| 92 | + # @since 2.1.0 |
| 93 | + def reject! |
| 94 | + if block_given? |
| 95 | + n_keys = keys.size |
| 96 | + keys.each do |key| |
| 97 | + delete(key) if yield(key, self[key]) |
| 98 | + end |
| 99 | + n_keys == keys.size ? nil : self |
| 100 | + else |
| 101 | + to_enum |
| 102 | + end |
| 103 | + end |
| 104 | + |
| 105 | + # Returns a new options object consisting of pairs for which the block returns true. |
| 106 | + # |
| 107 | + # @example Get a new options object with pairs for which the block returns true. |
| 108 | + # ssl_options = options.select { |k, v| k =~ /ssl/ } |
| 109 | + # |
| 110 | + # @yieldparam [ String, Object ] The key as a string and its value. |
| 111 | + # |
| 112 | + # @return [ Options::Redacted ] A new options object. |
| 113 | + # |
| 114 | + # @since 2.1.0 |
| 115 | + def select(&block) |
| 116 | + new_options = dup |
| 117 | + new_options.select!(&block) || new_options |
| 118 | + end |
| 119 | + |
| 120 | + # Only keeps pairs for which the block returns true. |
| 121 | + # |
| 122 | + # @example Remove pairs from this object for which the block does not return true. |
| 123 | + # options.select! { |k, v| k =~ /ssl/ } |
| 124 | + # |
| 125 | + # @yieldparam [ String, Object ] The key as a string and its value. |
| 126 | + # |
| 127 | + # @return [ Options::Redacted, nil ] This object or nil if no changes were made. |
| 128 | + # |
| 129 | + # @since 2.1.0 |
| 130 | + def select! |
| 131 | + if block_given? |
| 132 | + n_keys = keys.size |
| 133 | + keys.each do |key| |
| 134 | + delete(key) unless yield(key, self[key]) |
| 135 | + end |
| 136 | + n_keys == keys.size ? nil : self |
| 137 | + else |
| 138 | + to_enum |
| 139 | + end |
| 140 | + end |
| 141 | + |
| 142 | + private |
| 143 | + |
| 144 | + def redacted_string(method) |
| 145 | + '{' + reduce([]) do |list, (k, v)| |
| 146 | + list << "#{k.send(method)}=>#{redact(k, v, method)}" |
| 147 | + end.join(', ') + '}' |
| 148 | + end |
| 149 | + |
| 150 | + def redact(k, v, method) |
| 151 | + return STRING_REPLACEMENT if SENSITIVE_OPTIONS.include?(k.to_sym) |
| 152 | + v.send(method) |
| 153 | + end |
| 154 | + end |
| 155 | + end |
| 156 | +end |
0 commit comments