Class: SemanticLogger::Redactor

Inherits:
Object
  • Object
show all
Defined in:
lib/semantic_logger/redactor.rb

Overview

Replaces the values of an application's sensitive keys, such as passwords and access tokens, in the keyed data of a log entry: its payload, named tags, context, and metric dimensions (see Log#redact and SemanticLogger.sensitive_keys=).

Keys are matched by name, the name of a Symbol key or a String key, both as written and in snake_case, which is lower case, so that "X-Api-Key" and "apiKey" are also asked about as "x_api_key" and "api_key". Keys of any other kind are never sensitive.

Internal: built by SemanticLogger.sensitive_keys=, and asked by Log#redact to redact each of an entry's keyed fields, on the thread that writes the entry to the appenders.

Constant Summary collapse

MASK =

The value that replaces a sensitive one, unless the sensitive keys supply their own (see #initialize). The same as Rails uses for its filtered parameters.

"[FILTERED]".freeze
CIRCULAR =

Replaces a structure found again inside itself, which cannot be copied whole.

"[FILTERED: circular]".freeze
NESTED_TOO_DEEPLY =

Replaces a structure nested deeper than the formatters write (see Utils.json_safe), so that walking it can never overflow the stack.

"[FILTERED: nested too deeply]".freeze
MAX_DEPTH =
Utils::MAX_JSON_SAFE_DEPTH
SNAKE_CASE =

A key name in snake_case already, which is not converted (see #snake_case_forms).

/\A[a-z\d_]*\z/
MAX_REMEMBERED_KEYS =

How many keys the answer is remembered for (see #remember). Log entries use the same few keys over and over, but the names of request parameters, for example, are chosen by whoever sends the request, so neither how many nor how long they are is left to them.

1_000
MAX_REMEMBERED_KEY_LENGTH =

The longest key name, in characters, that the answer is remembered for (see #remember).

128

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sensitive_keys) ⇒ Redactor

Parameters sensitive_keys: [Array|Regexp|Object] Which keys are sensitive, one of: Array: Symbols and Strings, each matching any key name that contains it, ignoring case, as the Symbols in Rails' config.filter_parameters do, and Regexps. Regexp: Matches the sensitive key names. An object that responds to #filter_param(key, value), such as ActiveSupport::ParameterFilter: given a key name and nil, it returns nil when the key is not sensitive, and otherwise the value that replaces the key's value. An object that responds to #call(name), such as a Proc: given a key name, it returns whether the key is sensitive.

The answer for each key is remembered (see #replacement_for), so the same key name must always get the same answer.

Raises ArgumentError for any other value.



69
70
71
72
73
# File 'lib/semantic_logger/redactor.rb', line 69

def initialize(sensitive_keys)
  @sensitive_keys = sensitive_keys
  @matcher        = matcher_for(sensitive_keys)
  @replacements   = Concurrent::Map.new
end

Instance Attribute Details

#sensitive_keys ⇒ Object (readonly)

Returns the sensitive keys, as supplied.



51
52
53
# File 'lib/semantic_logger/redactor.rb', line 51

def sensitive_keys
  @sensitive_keys
end

Instance Method Details

#redact(value) ⇒ Object

Returns a copy of the value in which the value of every sensitive key is replaced, at any depth, or the value itself when it has no sensitive key, so that what the formatters write only changes when something is redacted. The exceptions are a structure that holds itself, cut off with CIRCULAR, and one nested deeper than MAX_DEPTH, replaced with NESTED_TOO_DEEPLY.

Looks inside the keys and values of the structures that the formatters write as JSON objects and arrays (see Utils.structure?), such as a Hash, Array, Set, Struct, or Data, and inside an object that responds to #to_unsafe_h, such as ActionController::Parameters. A copy is a Hash or an Array, as they write it. Any other value is returned as it is. The value and the structures in it are never changed, since they can be the application's own, or be shared by other log entries, as named tags are.

Never raises a StandardError: when the value cannot be redacted, such as when the sensitive keys raise, it is replaced whole, and the error is reported to the internal logger.



89
90
91
92
93
94
95
96
# File 'lib/semantic_logger/redactor.rb', line 89

def redact(value)
  return value unless structure?(value)

  walk(value, {}.compare_by_identity, 0)
rescue StandardError => e
  report(e)
  {redaction_failed: Utils.exception_name(e)}
end

#replacement_for(key) ⇒ Object

Returns the value that replaces the value of a sensitive key, or nil when the key is not sensitive, or is neither a Symbol nor a String.

The answer is remembered (see #remember), since asking the sensitive keys costs far more than looking it up.



103
104
105
106
107
108
109
110
111
112
# File 'lib/semantic_logger/redactor.rb', line 103

def replacement_for(key)
  return unless key.is_a?(Symbol) || key.is_a?(String)

  replacement = @replacements.fetch(key, UNKNOWN)
  return replacement unless UNKNOWN.equal?(replacement)

  replacement = ask(key)
  remember(key, replacement)
  replacement
end