A logging filter that 'misprints' your secrets
Project description
A logging filter that hides your secrets. Integrates with Python’s logging module.
Installation
pip install misprint
Usage
misprint.Misprinter
You can use the Misprinter class to redact exact string matches or regular expressions within a string:
misprinter = Misprinter(token=["my_secret_token", re.compile(r"^ghp_\w+")])
assert misprinter.mask("this is my_secret_token") == "this is ****"
assert misprinter.make("github tokens: ghp_abc123 ghp_def456") == "github tokens: **** ****"
If you need to add a mask for new data to an existing instance then you can use the add_mask_for method:
misprinter = Misprinter()
assert misprinter.mask("a secret1234") == "a secret1234"
misprinter.add_mask_for("secret1234")
assert misprinter.mask("a secret1234") == "a ****"
You can also initialise your Misprinter instance with use_named_masks=True if you would like to be able to identify what data has been masked more easily:
misprinter = Misprinter(use_named_masks=True)
misprinter.add_mask_for("another_secret", name="database password")
assert (
misprinter.mask("printing another_secret")
== "printing <'database password' (value removed)>"
)
misprint.MisprintFilter
misprint also provides a logging.Filter subclass, which integrates with the Python standard library’s logging module to enable redaction of log messages.
MisprintFilter is a subclass of Misprinter, so inherits all of the methods detailed above. This is useful, as the filter can be connected to the loggers that are configured for a program at startup, and new secrets can be conveniently added to the filter to be redacted in the logs:
import logging
import sys
import misprint
logging.basicConfig(
datefmt="[%X]",
handlers=[logging.StreamHandler(sys.stderr)], # plus others
)
misprinter = misprint.MisprintFilter()
for handler in logging.getLogger().handlers:
handler.addFilter(misprinter)
A_TOKEN = "asdf1234"
misprinter.add_mask_for(A_TOKEN)
log.error("Bad token: %s", A_TOKEN)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file misprint-0.1.0.tar.gz
.
File metadata
- Download URL: misprint-0.1.0.tar.gz
- Upload date:
- Size: 4.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.8.3 requests/2.28.2 setuptools/65.5.1 requests-toolbelt/0.9.1 tqdm/4.64.0 CPython/3.8.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 15bfab0d0aefb4ba8c91c5701b17cd062078b8c0697d8eae9276e462bbd9745f |
|
MD5 | 10b8e0ca03ca5c1273de9a973f187bd8 |
|
BLAKE2b-256 | ddb0f8e0bf795ce6d1f159edeec2d5f40c8ccc23cf90617eb8a3ee49e03fbf0e |
File details
Details for the file misprint-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: misprint-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.8.3 requests/2.28.2 setuptools/65.5.1 requests-toolbelt/0.9.1 tqdm/4.64.0 CPython/3.8.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 57bf265949888d32d8a2c7d1efa21cc44b9a38f5fba3a7773ecdfff035224148 |
|
MD5 | 00d80488e65c8a49736a49be76b4b9bf |
|
BLAKE2b-256 | 1b6e6fc696acc8f22e22969a799b68458b449346cee1b3f829544322b0a4fb7c |