Class: Mongo::Options::Redacted

Inherits:
BSON::Document
  • Object
show all
Includes:
RedactedRepresentation
Defined in:
lib/mongo/options/redacted.rb

Overview

Class for wrapping options that could be sensitive.

Sensitive values are redacted in inspect, to_s, pretty printed, JSON, YAML and Marshal output. The stored values are unchanged. Deserializing a YAML or Marshal representation that contained sensitive values raises ArgumentError instead of creating an object with marker credentials.

Since:

  • 2.1.0

Constant Summary collapse

SENSITIVE_OPTIONS =

The options whose values will be redacted. The username is included because it is part of the userinfo segment, whose components the connection string specification treats as sensitive values.

The arguments of the sensitive URI options are redacted as a whole: authMechanismProperties may carry a credential in any of its properties (for example an AWS session token), and the driver cannot know which properties a future mechanism will treat as secret.

Since:

  • 2.1.0

%i[user
password
pwd
proxy_password
proxy_username
ssl_key_pass_phrase
auth_mech_properties].freeze
STRING_REPLACEMENT =

The replacement string used in place of the value for sensitive keys.

Since:

  • 2.1.0

'<REDACTED>'

Constants included from RedactedRepresentation

Mongo::Options::RedactedRepresentation::LEGACY_SERIALIZATION_ERROR, Mongo::Options::RedactedRepresentation::REDACTED_SERIALIZATION_ERROR, Mongo::Options::RedactedRepresentation::SERIALIZATION_OPTIONS_KEY, Mongo::Options::RedactedRepresentation::SERIALIZATION_REDACTED_KEY

Instance Method Summary collapse

Methods included from RedactedRepresentation

#as_extended_json, #as_json, #encode_with, #init_with, #inspect, #marshal_dump, #marshal_load, #pretty_print, #to_extended_json, #to_json, #to_s

Instance Method Details

#has_key?(key) ⇒ true, false Also known as: key?

Whether these options contain a given key.

Examples:

Determine if the options contain a given key.

options.has_key?(:name)

Parameters:

  • key (String, Symbol) —

    The key to check for existence.

Returns:

  • (true, false) —

    If the options contain the given key.

Since:

  • 2.1.0



262
263
264
# File 'lib/mongo/options/redacted.rb', line 262

def has_key?(key)
  super(convert_key(key))
end

#reject {|The| ... } ⇒ Options::Redacted

Returns a new options object consisting of pairs for which the block returns false.

Examples:

Get a new options object with pairs for which the block returns false.

new_options = options.reject { |k, v| k == 'database' }

Yield Parameters:

  • The (String, Object) —

    key as a string and its value.

Returns:

Since:

  • 2.1.0



277
278
279
280
# File 'lib/mongo/options/redacted.rb', line 277

def reject(&block)
  new_options = dup
  new_options.reject!(&block) || new_options
end

#reject! {|The| ... } ⇒ Options::Redacted?

Only keeps pairs for which the block returns false.

Examples:

Remove pairs from this object for which the block returns true.

options.reject! { |k, v| k == 'database' }

Yield Parameters:

  • The (String, Object) —

    key as a string and its value.

Returns:

Since:

  • 2.1.0



292
293
294
295
296
297
298
299
300
301
302
# File 'lib/mongo/options/redacted.rb', line 292

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

#select {|The| ... } ⇒ Options::Redacted

Returns a new options object consisting of pairs for which the block returns true.

Examples:

Get a new options object with pairs for which the block returns true.

ssl_options = options.select { |k, v| k =~ /ssl/ }

Yield Parameters:

  • The (String, Object) —

    key as a string and its value.

Returns:

Since:

  • 2.1.0



314
315
316
317
# File 'lib/mongo/options/redacted.rb', line 314

def select(&block)
  new_options = dup
  new_options.select!(&block) || new_options
end

#select! {|The| ... } ⇒ Options::Redacted?

Only keeps pairs for which the block returns true.

Examples:

Remove pairs from this object for which the block does not return true.

options.select! { |k, v| k =~ /ssl/ }

Yield Parameters:

  • The (String, Object) —

    key as a string and its value.

Returns:

Since:

  • 2.1.0



329
330
331
332
333
334
335
336
337
338
339
# File 'lib/mongo/options/redacted.rb', line 329

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