Module: Mongo::Options::RedactedRepresentation Private

Included in:
Redacted, RedactedHash
Defined in:
lib/mongo/options/redacted.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

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

Sensitive values are replaced by Mongo::Options::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 collapse

SERIALIZATION_REDACTED_KEY =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

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

'redacted'
SERIALIZATION_OPTIONS_KEY =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

'options'
REDACTED_SERIALIZATION_ERROR =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

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

'Cannot deserialize options whose sensitive values were redacted: ' \
'recreate the options from configuration instead.'
LEGACY_SERIALIZATION_ERROR =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

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.

'Cannot deserialize options without a redaction envelope: ' \
'recreate the options from configuration instead.'

Instance Method Summary collapse

Instance Method Details

#as_extended_json(**options) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Parameters:

  • options (Hash) —

    BSON Extended JSON options.

Returns:

  • (Hash) —

    The redacted Hash.



126
127
128
# 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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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.



135
136
137
138
139
140
141
142
# 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) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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.



94
95
96
97
98
99
# 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) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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.



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

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

#inspect ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get a string representation of the options.

Returns:

  • (String) —

    The string representation of the options.



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

def inspect
  redacted_string(:inspect)
end

#marshal_dump ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Dump the options for Marshal, redacting sensitive values.

Returns:

  • (Hash) —

    The envelope with redacted options.



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

def marshal_dump
  serialization_envelope
end

#marshal_load(hash) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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.



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

def marshal_load(hash)
  load_serialization_envelope(hash)
end

#pretty_print(q) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Pretty print the options, redacting sensitive values.

Parameters:

  • q (PP) —

    The pretty printer.



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

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

#to_extended_json(**options) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Serialize the options to Extended JSON, redacting sensitive values.

Parameters:

  • options (Hash) —

    BSON Extended JSON options.

Returns:

  • (String) —

    The Extended JSON representation.



117
118
119
# 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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Serialize the options to JSON, redacting sensitive values.

Parameters:

  • args (Array) —

    JSON generator arguments and state.

Returns:

  • (String) —

    The JSON representation.



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

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

#to_s ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get a string representation of the options.

Returns:

  • (String) —

    The string representation of the options.



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

def to_s
  redacted_string(:to_s)
end