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_patternis replaced wholesale, whatever its type, exceptNoneand"", which are kept. :meth:RedactionPolicy.defaultmatches a key whose final segment isauthorization,cookie,password,secretortoken(plural allowed), case-insensitively. Segments split on_,-,.and a camelCase boundary, soaccess_tokenandaccessTokenmatch whiletoken_countdoes not. - Values. Any string, under any key, has the credential in
Bearer <token>andBasic <credential>replaced whenscrub_credentialsis set, and the password inscheme://user:password@hostreplaced whenscrub_url_passwordsis 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 |
scrub_url_passwords |
bool
|
Replace the password in
|
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: |
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: |
Any
|
|
Example::
redact({"user": "ann", "password": "hunter2"})
# {'user': 'ann', 'password': '[redacted]'}