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 =
# File 'lib/mongo/options/redacted.rb', line 53
::Mongo::Errorraised 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.'
-
REDACTED_SERIALIZATION_ERROR =
# File 'lib/mongo/options/redacted.rb', line 44
::Mongo::Errorraised when deserializing a representation whose sensitive values were redacted.'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 =
# File 'lib/mongo/options/redacted.rb', line 37
Envelope keys used by YAML and Marshal. They surround, rather than live inside, the user's options hash.
'redacted'
Instance Attribute Summary
- #has_sensitive_keys? ⇒ Boolean readonly private Internal use only
Instance Method Summary
-
#as_extended_json(**options) ⇒ Hash
Internal use only
Convert the options to a Hash for BSON Extended JSON encoding.
-
#as_json(options = nil) ⇒ Hash
Internal use only
Convert the options to a Hash for ActiveSupport's JSON encoding.
-
#encode_with(coder)
Internal use only
Serialize the options to YAML, redacting sensitive values.
-
#init_with(coder)
Internal use only
Initialize options from YAML.
-
#inspect ⇒ String
Internal use only
Get a string representation of the options.
-
#marshal_dump ⇒ Hash
Internal use only
Dump the options for Marshal, redacting sensitive values.
-
#marshal_load(hash)
Internal use only
Load the options from Marshal.
-
#pretty_print(q)
Internal use only
Pretty print the options, redacting sensitive values.
-
#to_extended_json(**options) ⇒ String
Internal use only
Serialize the options to Extended JSON, redacting sensitive values.
-
#to_json(*args) ⇒ String
Internal use only
Serialize the options to JSON, redacting sensitive values.
-
#to_s ⇒ String
Internal use only
Get a string representation of the options.
- #load_serialization_envelope(envelope) private Internal use only
- #redacted_serialization_hash private Internal use only
- #redacted_string(method) private Internal use only
- #redacted_value(key, value, method) private Internal use only
- #sensitive_key?(key) ⇒ Boolean private Internal use only
- #serialization_envelope private Internal use only
Instance Attribute Details
#has_sensitive_keys? ⇒ Boolean (readonly, private)
# 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.
# File 'lib/mongo/options/redacted.rb', line 126
def as_extended_json(**) redacted_serialization_hash.as_extended_json(**) end
#as_json(options = nil) ⇒ Hash
Convert the options to a Hash for ActiveSupport's JSON encoding.
# File 'lib/mongo/options/redacted.rb', line 135
def as_json( = nil) redacted = redacted_serialization_hash if redacted.respond_to?(:as_json) redacted.as_json() 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.
# 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.
# 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.
# 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 = envelope[SERIALIZATION_OPTIONS_KEY] || {} if envelope[SERIALIZATION_REDACTED_KEY] || .keys.any? { |key| sensitive_key?(key) } raise ArgumentError, REDACTED_SERIALIZATION_ERROR end .each do |key, value| self[key] = value end end
#marshal_dump ⇒ Hash
Dump the options for Marshal, redacting sensitive values.
# 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.
# 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.
# 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)
# 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.
# File 'lib/mongo/options/redacted.rb', line 117
def to_extended_json(**) redacted_serialization_hash.to_extended_json(**) end
#to_json(*args) ⇒ String
Serialize the options to JSON, redacting sensitive values.
# 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.
# File 'lib/mongo/options/redacted.rb', line 67
def to_s redacted_string(:to_s) end