RedactEx.Redactor (RedactEx v0.1.8)
View SourceRedactEx
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)
7So you may consider it when evaluating for which kind of data you're planning to use byte-based helpers
Algorithms
Optimized algorithms for strings with expected length(s)
Those algorithms will match on the byte_size of the string
| Algorithm | Description |
|---|---|
:simple | keeps the first keep% of the string and sets the rest to redactor, according to redacted_size |
:center | splits keep% of the string between head and tail, and sets the middle to redactor, according to redacted_size |
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 casesCommon configuration
Configuration of a redactor is a keyword list containing
redactorstring character used to redact the hidden part of the. Defaults to*redacted_sizeinteger | :auto length of the resulting string that will be set as redacted. Defaults toauto,which will set it to `expected_string_length - keep`algorithmatom | module algorithm used to redact the string. Defaults tosimpleif 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`)keepinteger quote of the string that will be kept. Defaults to25%lengthinteger or:*length of the string to be considered.:*stands for the fallback function configuration. Is overridden by key:lengthsif presentlengths[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 generatedexceptlist(atom()) list of environments for which this configuration will have effectfallback_valuestring fixed value for redacting non-string values
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 lengthCustom algorithms
You can define custom algorithms to define your own implementations. Take a look at RedactEx.Algorithms.Algorithm behaviour.