Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

misprint-0.1.1.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

misprint-0.1.1-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file misprint-0.1.1.tar.gz.

File metadata

  • Download URL: misprint-0.1.1.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

Hashes for misprint-0.1.1.tar.gz
Algorithm Hash digest
SHA256 f4e6f68e967f52330d335fb1745ca506c9fc62df4a448767575ebcb7b8d8ab75
MD5 ccebe871f922c4e4ec30cdbcd60e1e10
BLAKE2b-256 7f87274be601370fd3b000c00ce9e7acad4f53532ff5aa436e93964305d96d93

See more details on using hashes here.

File details

Details for the file misprint-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: misprint-0.1.1-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

Hashes for misprint-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d68efd35e2a545875e5f52f721cead3e2df5d0797b165040d9d02a6cbc69aee6
MD5 3f3eba405f1d6f66209afbcee1601806
BLAKE2b-256 5accebe3693ab97c9ae64579b694c989f0a9a80c40cdb5852402bb9ac2f36450

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page