Module: Mongoid::Errors::ConfigRedactor

Extended by:
ConfigRedactor
Included in:
ConfigRedactor
Defined in:
lib/mongoid/errors/config_redactor.rb

Overview

Redacts credentials from a client configuration hash before it is interpolated into an exception message.

Constant Summary collapse

REDACTED =
'[REDACTED]'
SENSITIVE_KEYS =

Top-level keys whose values should be replaced wholesale.

%w[password auto_encryption_options].freeze
URI_USERINFO =

Match the userinfo portion of a MongoDB connection string.

%r{\A(mongodb(?:\+srv)?://)[^@/]+@}.freeze

Instance Method Summary collapse

Instance Method Details

#redact(config) ⇒ Object

Return a copy of the given config hash with sensitive values redacted. Recurses into nested hashes so that, e.g., :options => { :auto_encryption_options => ... } is also covered. Non-hash inputs are returned unchanged.



22
23
24
25
26
27
28
# File 'lib/mongoid/errors/config_redactor.rb', line 22

def redact(config)
  return config unless config.is_a?(Hash)

  config.each_with_object({}) do |(key, value), result|
    result[key] = redact_value(key, value)
  end
end

#redact_value(key, value) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/mongoid/errors/config_redactor.rb', line 30

def redact_value(key, value)
  if SENSITIVE_KEYS.include?(key.to_s)
    REDACTED
  elsif key.to_s == 'uri' && value.is_a?(String)
    value.sub(URI_USERINFO, "\\1#{REDACTED}@")
  elsif value.is_a?(Hash)
    redact(value)
  else
    value
  end
end