Module: FlyIO::Redactor

Defined in:
lib/fly_io/redactor.rb

Constant Summary collapse

FILTERED =
"[FILTERED]"
SENSITIVE_KEYS =
/authorization|token|secret|password|credential|private.?key|certificate/i
AUTHORIZATION_VALUE =
%r{\b(?:Bearer|FlyV1)\s+[A-Za-z0-9._~+/=:-]+}i

Class Method Summary collapse

Class Method Details

.redact(value) ⇒ Object



11
12
13
# File 'lib/fly_io/redactor.rb', line 11

def redact(value)
  value.to_s.gsub(AUTHORIZATION_VALUE) { |match| "#{match.split.first} #{FILTERED}" }
end

.redact_object(value, key = nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/fly_io/redactor.rb', line 15

def redact_object(value, key = nil)
  return FILTERED if key&.match?(SENSITIVE_KEYS)

  case value
  when Model
    redact_object(value.to_h, key)
  when Hash
    value.each_with_object({}) do |(child_key, child), result|
      result[child_key] = redact_object(child, child_key.to_s)
    end
  when Array
    value.map { |child| redact_object(child) }
  when String
    redact(value)
  else
    value
  end
end