View Source RedactEx.Redactor (RedactEx v0.1.6)

RedactEx

Utility that defines a set of functions to fast-redact data where possible, e.g. when you already know the length of the string you're gonna have as validated input Redacting is unidirectional and is meant to help the safety of logs and exported data

Note that right now this function matches on binary data, so you probably want to consider it if UTF-stuff is heavily involved when defining lengths. For example, in extreme cases, considering that a UTF-8 string may have a string length of 5 (e.g. héllo) but a byte size of 6 (as by documentation)

iex> string = "héllo"
"héllo"
iex> String.length(string)
5
iex> byte_size(string)
7

So you may consider it when evaluating for which kind of data you're planning to use byte-based helpers

algorithms

Algorithms

optimized-algorithms-for-strings-with-expected-length-s

Optimized algorithms for strings with expected length(s)

Those algorithms will match on the byte_size of the string

AlgorithmDescription
:simplekeeps the first keep% of the string and sets the rest to redactor, according to redacted_size
:centersplits keep% of the string between head and tail, and sets the middle to redactor, according to redacted_size

configuration

Configuration

Options: redactors a list of redacting rules. Each element can be:

          - a tuple in the form {redactor_function_name, configuration}
          - a tuple in the form {[redactor_function_names], configuration}
          - a single redactor_function_name
          `redactor_function_name` can be an atom or string value in all cases

common-configuration

Common configuration

Configuration of a redactor is a keyword list containing

  • redactor string character used to redact the hidden part of the. Defaults to *
  • redacted_size integer | :auto length of the resulting string that will be set as redacted. Defaults to auto,

                                                      which will set it to `expected_string_length - keep`
  • algorithm atom | module algorithm used to redact the string. Defaults to simple

                                                      if a module is given, it must implement the `RedactEx.Algorithms.Algorithm` behaviour
                                                      Supported atoms are
                                                      - `:simple` (alias for `RedactEx.Algorithms.Simple`)
                                                      - `:center` (alias for `RedactEx.Algorithms.Center`)
  • keep integer quote of the string that will be kept. Defaults to 25%
  • length integer or :* length of the string to be considered. :* stands for the fallback function configuration. Is overridden by key :lengths if present
  • lengths [integer or :*] | min..max lengths of the strings to be considered. :* stands for the fallback function configuration. A function for each specific length will be generated

  • except list(atom()) list of environments for which this configuration will have effect
  • fallback_value string fixed value for redacting non-string values

example

Example

iex> defmodule MyApp.Redacting do
...>    @moduledoc false
...>    use RedactEx.Redactor, redactors: [
...>      {"redact_three", length: 3, algorithm: :simple},
...>      {"redact", lengths: 1..3, algorithm: :simple}
...>    ]
...> end
iex> MyApp.Redacting.redact("test")
...> "t***"

This will expose the following functions:
     * `MyApp.Redacting.redact` with fast matches on input of length 1, 2, 3
     * `MyApp.Redacting.redact_three` with fast matches on input of length 3
     * both `redact/1` and `redact_three/1` will have a slower fallback function for input of different length

custom-algorithms

Custom algorithms

You can define custom algorithms to define your own implementations. Take a look at RedactEx.Algorithms.Algorithm behaviour.