File: redacted.rb

package info (click to toggle)
ruby-mongo 2.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,332 kB
  • sloc: ruby: 45,579; makefile: 5
file content (156 lines) | stat: -rw-r--r-- 4,765 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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