Skip to content

Redaction

rn_forge.commons.logging.redaction

Secret redaction for structured log values.

:func:redact returns a copy of a value with secrets replaced by :data:REDACTED. A :class:RedactionPolicy selects what counts as a secret:

  • Keys. A value stored under a key matching field_pattern is replaced wholesale, whatever its type, except None and "", which are kept. :meth:RedactionPolicy.default matches a key whose final segment is authorization, cookie, password, secret or token (plural allowed), case-insensitively. Segments split on _, -, . and a camelCase boundary, so access_token and accessToken match while token_count does not.
  • Values. Any string, under any key, has the credential in Bearer <token> and Basic <credential> replaced when scrub_credentials is set, and the password in scheme://user:password@host replaced when scrub_url_passwords is set. A credential must contain a digit, a symbol or an inner capital; a plain word after the scheme is kept.

Dicts, lists and tuples are rebuilt, at any depth; the input is never mutated. Exception tracebacks are not inspected.

REDACTED = '[redacted]' module-attribute

The replacement for a redacted value.

RedactionPolicy(field_pattern, scrub_credentials=True, scrub_url_passwords=True) dataclass

What :func:redact treats as a secret.

Attributes:

Name Type Description
field_pattern Pattern[str]

Searched against a key; a match redacts the key's value.

scrub_credentials bool

Replace the credential in Bearer <token> and Basic <credential> inside string values.

scrub_url_passwords bool

Replace the password in scheme://user:password@host inside string values.

default() classmethod

Return the policy described in the module docstring.

scrub(text)

Return text with credentials and URL passwords replaced.

redact(value, *, policy=None, key='')

Return a copy of value with secrets replaced by :data:REDACTED.

Parameters:

Name Type Description Default
value Any

A scalar, or a dict, list or tuple nested to any depth. Other types are returned unchanged.

required
policy RedactionPolicy | None

What to redact. Defaults to :meth:RedactionPolicy.default.

None
key str

The key value is stored under, if any.

''

Returns:

Type Description
Any

value unchanged when key does not match, otherwise a redacted copy.

Any

A value under a matching key becomes :data:REDACTED unless it is

Any

None or "".

Example::

redact({"user": "ann", "password": "hunter2"})
# {'user': 'ann', 'password': '[redacted]'}