Lightning.Scrubber (Lightning v2.19.0-pre)

View Source

Process used to scrub strings of sensitive information.

Can be started via start_link/1.

{:ok, scrubber} =
  Lightning.Scrubber.start_link(
    samples:
      Lightning.Credentials.sensitive_values_for(credential)
  )

Takes an optional :name key, in case you need to name the process.

Summary

Functions

Builds the state scrub_string/2 takes, without starting a process.

Returns a specification to start this module under a supervisor.

Prepare a list of sensitive samples (strings) into a potentially bigger list composed of variations a sample may appear.

This is used for scrubbing logs (is_list) and JSON (is_binary)

Process-free counterpart to scrub/2 for a single string.

Recursively scrubs all values from a data structure, replacing them with type placeholders. Keeps the structure and the field names, and hides the values.

Functions

add_samples(agent, new_samples, basic_auth)

build_state(sample_pairs)

@spec build_state([{[String.t()], [String.t()]}]) :: Lightning.Scrubber.State.t()

Builds the state scrub_string/2 takes, without starting a process.

scrub/2 needs an agent because a run accumulates samples as it goes. A caller that already knows every sample would otherwise start - and stay linked to - an agent per string it scrubs.

Takes {samples, basic_auth} pairs, the same shape add_samples/3 takes.

child_spec(arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

encode_samples(samples, basic_auth \\ [])

@spec encode_samples(samples :: [String.t()], basic_auth :: [String.t()]) :: [
  String.t()
]

Prepare a list of sensitive samples (strings) into a potentially bigger list composed of variations a sample may appear.

samples(agent)

scrub(agent, lines)

This is used for scrubbing logs (is_list) and JSON (is_binary)

scrub_string(state, data)

@spec scrub_string(Lightning.Scrubber.State.t(), String.t() | nil) :: String.t() | nil

Process-free counterpart to scrub/2 for a single string.

scrub_values(value, array_limit \\ 2)

@spec scrub_values(any(), non_neg_integer()) :: any()

Recursively scrubs all values from a data structure, replacing them with type placeholders. Keeps the structure and the field names, and hides the values.

Field names are not values and survive as they are, so a body keyed by record identifier would carry those identifiers out. Long lists keep two samples and keys are capped across the whole structure, and a map that was truncated says how many it dropped under a "..." key. Arrays are sampled up to array_limit elements with a "...N more" indicator if truncated.

Examples

iex> scrub_values(%{"name" => "John", "age" => 30})
%{"name" => "string", "age" => "number"}

iex> scrub_values([1, 2, 3])
["number", "number", "...1 more"]

start_link(opts)

@spec start_link(
  opts :: [
    samples: [String.t()],
    basic_auth: [String.t()],
    name: nil | GenServer.name()
  ]
) :: Agent.on_start()