123456789_123456789_123456789_123456789_123456789_

Class: Mongo::Options::Redacted

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, BSON::Document
Instance Chain:
self, RedactedRepresentation, BSON::Document
Inherits: BSON::Document
  • Object
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

  • 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 ::Mongo::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

    # File 'lib/mongo/options/redacted.rb', line 239
    %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

    # File 'lib/mongo/options/redacted.rb', line 250
    '<REDACTED>'

RedactedRepresentation - Included

LEGACY_SERIALIZATION_ERROR, REDACTED_SERIALIZATION_ERROR, SERIALIZATION_OPTIONS_KEY, SERIALIZATION_REDACTED_KEY

Instance Attribute Summary

Instance Method Summary

RedactedRepresentation - Included

#as_extended_json

Convert the options to a Hash for BSON Extended JSON encoding.

#as_json

Convert the options to a Hash for ActiveSupport's JSON encoding.

#encode_with

Serialize the options to YAML, redacting sensitive values.

#init_with

Initialize options from YAML.

#inspect

Get a string representation of the options.

#marshal_dump

Dump the options for Marshal, redacting sensitive values.

#marshal_load

Load the options from Marshal.

#pretty_print

Pretty print the options, redacting sensitive values.

#to_extended_json

Serialize the options to Extended JSON, redacting sensitive values.

#to_json

Serialize the options to JSON, redacting sensitive values.

#to_s

Get a string representation of the options.

#load_serialization_envelope, #redacted_serialization_hash, #redacted_string, #redacted_value, #sensitive_key?, #serialization_envelope

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

[ GitHub ]

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

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

#key?(key)

Alias for #has_key?.

[ GitHub ]

  
# File 'lib/mongo/options/redacted.rb', line 265

alias key? has_key?

#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

[ GitHub ]

  
# 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

[ GitHub ]

  
# 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

[ GitHub ]

  
# 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

[ GitHub ]

  
# 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