Module: DockerSwarm::LogHelper

Defined in:
lib/docker_swarm/log_helper.rb

Overview

Helper module to centralize logging logic and formatting

Constant Summary collapse

SENSITIVE_KEYS =

data se matchea con \b para que Data (Secret/Config) se filtre pero metadata u otras claves no caigan en falso positivo. auth cubre headers de autenticación (X-Registry-Auth, Authorization), case-insensitive.

/password|pass|passwd|secret|token|api_key|auth|\bdata\b/i.freeze
FILTERED =
"[FILTERED]"

Class Method Summary collapse

Class Method Details

.format_kv(payload) ⇒ String

Formats a hash into a KV structured string with sensitive data masking

Parameters:

  • payload (Hash)

    The data to format

Returns:

  • (String)

    KV formatted string



37
38
39
40
41
42
43
# File 'lib/docker_swarm/log_helper.rb', line 37

def self.format_kv(payload)
  sanitize(payload).map do |k, v|
    "#{k}=#{v}"
  end.join(" ")
rescue
  "event=logging_error"
end

.sanitize(value) ⇒ Object

Redacta recursivamente los valores cuya CLAVE es sensible, a cualquier profundidad (hashes y arrays anidados). No muta la entrada: devuelve copias.

Un header sensible puede viajar anidado (headers: { "X-Registry-Auth" => "<cred>" }) y el match por clave de primer nivel no lo alcanzaba — el hash interno se interpolaba entero.

Parameters:

  • value (Object)

    hash, array o escalar

Returns:

  • (Object)

    copia con los valores sensibles reemplazados por [FILTERED]



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/docker_swarm/log_helper.rb', line 21

def self.sanitize(value)
  case value
  when Hash
    value.each_with_object({}) do |(k, v), acc|
      acc[k] = k.to_s.match?(SENSITIVE_KEYS) ? FILTERED : sanitize(v)
    end
  when Array
    value.map { |v| sanitize(v) }
  else
    value
  end
end