123456789_123456789_123456789_123456789_123456789_

Module: Mongo::Options::RedactedRepresentation Private

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
Defined in: lib/mongo/options/redacted.rb

Overview

Shared string, debug and serialization representations for option objects that may carry sensitive values.

Sensitive values are replaced by Redacted::STRING_REPLACEMENT in inspect, to_s, pretty printing, JSON, YAML, Extended JSON and Marshal output. The stored values are not changed.

BSON encoding is intentionally not overridden: these objects are BSON documents, and BSON is the wire representation used by the driver. The native BSON encoder writes nested hashes without dispatching to Ruby methods on them, so a Ruby-level to_bson override would be both incomplete and would change what is written to the wire.

Constant Summary

  • LEGACY_SERIALIZATION_ERROR =

    ::Mongo::Error raised for a payload that reaches the new object-tag YAML loader or the user-defined Marshal loader but does not contain the redaction envelope. Legacy Hash formats (for example YAML with a !ruby/hash tag, or standard Marshal data for a Hash subclass) can bypass those hooks and are not covered by this error.

    # File 'lib/mongo/options/redacted.rb', line 53
    'Cannot deserialize options without a redaction envelope: ' \
    'recreate the options from configuration instead.'
  • REDACTED_SERIALIZATION_ERROR =

    ::Mongo::Error raised when deserializing a representation whose sensitive values were redacted.

    # File 'lib/mongo/options/redacted.rb', line 44
    'Cannot deserialize options whose sensitive values were redacted: ' \
    'recreate the options from configuration instead.'
  • SERIALIZATION_OPTIONS_KEY =
    # File 'lib/mongo/options/redacted.rb', line 40
    'options'
  • SERIALIZATION_REDACTED_KEY =

    Envelope keys used by YAML and Marshal. They surround, rather than live inside, the user's options hash.

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

Instance Attribute Summary

Instance Method Summary

Instance Attribute Details

#has_sensitive_keys? ⇒ Boolean (readonly, private)

[ GitHub ]

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

def has_sensitive_keys?
  keys.any? { |key| sensitive_key?(key) }
end

Instance Method Details

#as_extended_json(**options) ⇒ Hash

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

Parameters:

  • options (Hash) —

    BSON Extended JSON options.

Returns:

  • (Hash) —

    The redacted Hash.

[ GitHub ]

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

def as_extended_json(**options)
  redacted_serialization_hash.as_extended_json(**options)
end

#as_json(options = nil) ⇒ Hash

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

Parameters:

  • options (Hash | nil) (defaults to: nil) —

    The ActiveSupport options.

Returns:

  • (Hash) —

    The redacted Hash.

[ GitHub ]

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

def as_json(options = nil)
  redacted = redacted_serialization_hash
  if redacted.respond_to?(:as_json)
    redacted.as_json(options)
  else
    redacted
  end
end

#encode_with(coder)

Serialize the options to YAML, redacting sensitive values.

The user's options are nested under an envelope so that metadata cannot collide with option names. Deserialization raises when sensitive values were redacted.

Parameters:

  • coder (Psych::Coder) —

    The YAML coder.

[ GitHub ]

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

def encode_with(coder)
  coder.tag = "!ruby/object:#{self.class.name}"
  serialization_envelope.each do |key, value|
    coder[key] = value
  end
end

#init_with(coder)

Initialize options from YAML.

Raises if the serialized form contains redacted sensitive values or does not use the redaction envelope. It never creates an object with marker strings standing in for credentials.

Parameters:

  • coder (Psych::Coder) —

    The YAML coder.

[ GitHub ]

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

def init_with(coder)
  load_serialization_envelope(coder.map)
end

#inspect ⇒ String

Get a string representation of the options.

Returns:

  • (String) —

    The string representation of the options.

[ GitHub ]

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

def inspect
  redacted_string(:inspect)
end

#load_serialization_envelope(envelope) (private)

[ GitHub ]

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

def load_serialization_envelope(envelope)
  unless envelope.is_a?(Hash) &&
         envelope.key?(SERIALIZATION_REDACTED_KEY) &&
         envelope.key?(SERIALIZATION_OPTIONS_KEY)
    raise ArgumentError, LEGACY_SERIALIZATION_ERROR
  end

  options = envelope[SERIALIZATION_OPTIONS_KEY] || {}
  if envelope[SERIALIZATION_REDACTED_KEY] ||
     options.keys.any? { |key| sensitive_key?(key) }
    raise ArgumentError, REDACTED_SERIALIZATION_ERROR
  end

  options.each do |key, value|
    self[key] = value
  end
end

#marshal_dump ⇒ Hash

Dump the options for Marshal, redacting sensitive values.

Returns:

  • (Hash) —

    The envelope with redacted options.

[ GitHub ]

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

def marshal_dump
  serialization_envelope
end

#marshal_load(hash)

Load the options from Marshal.

Raises if the serialized form contains redacted sensitive values or does not use the redaction envelope. It never creates an object with marker strings standing in for credentials.

Parameters:

  • hash (Hash) —

    The dumped envelope.

[ GitHub ]

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

def marshal_load(hash)
  load_serialization_envelope(hash)
end

#pretty_print(q)

Pretty print the options, redacting sensitive values.

Parameters:

  • q (PP) —

    The pretty printer.

[ GitHub ]

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

def pretty_print(q)
  q.pp(redacted_serialization_hash)
end

#redacted_serialization_hash (private)

[ GitHub ]

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

def redacted_serialization_hash
  each_with_object({}) do |(key, value), copy|
    copy[key] = sensitive_key?(key) ? Redacted::STRING_REPLACEMENT : value
  end
end

#redacted_string(method) (private)

[ GitHub ]

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

def redacted_string(method)
  '{' + map do |key, value|
    "#{key.public_send(method)}=>#{redacted_value(key, value, method)}"
  end.join(', ') + '}'
end

#redacted_value(key, value, method) (private)

[ GitHub ]

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

def redacted_value(key, value, method)
  return Redacted::STRING_REPLACEMENT if sensitive_key?(key)

  value.public_send(method)
end

#sensitive_key?(key) ⇒ Boolean (private)

[ GitHub ]

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

def sensitive_key?(key)
  return false unless key.respond_to?(:to_sym)

  Redacted::SENSITIVE_OPTIONS.include?(key.to_sym)
end

#serialization_envelope (private)

[ GitHub ]

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

def serialization_envelope
  {
    SERIALIZATION_REDACTED_KEY => has_sensitive_keys?,
    SERIALIZATION_OPTIONS_KEY => redacted_serialization_hash,
  }
end

#to_extended_json(**options) ⇒ String

Serialize the options to Extended JSON, redacting sensitive values.

Parameters:

  • options (Hash) —

    BSON Extended JSON options.

Returns:

  • (String) —

    The Extended JSON representation.

[ GitHub ]

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

def to_extended_json(**options)
  redacted_serialization_hash.to_extended_json(**options)
end

#to_json(*args) ⇒ String

Serialize the options to JSON, redacting sensitive values.

Parameters:

  • args (Array) —

    JSON generator arguments and state.

Returns:

  • (String) —

    The JSON representation.

[ GitHub ]

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

def to_json(*args)
  redacted_serialization_hash.to_json(*args)
end

#to_s ⇒ String

Get a string representation of the options.

Returns:

  • (String) —

    The string representation of the options.

[ GitHub ]

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

def to_s
  redacted_string(:to_s)
end