Skip to main content

A logging filter that 'misprints' your secrets

Project description

misprint

PyPI - Version PyPI - Python Version

An integration with the Python standard library's logging module that masks secrets in log messages before they are emitted. This project was heavily inspired by an excellent blog post which has been updated/adapted for modern Python versions.

Misprint is fully type-annotated for easier integration with mypy.


Table of Contents

Installation

pip install misprint

License

misprint is distributed under the terms of the MIT license.

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.mask("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.2.0.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

misprint-0.2.0-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: misprint-0.2.0.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for misprint-0.2.0.tar.gz
Algorithm Hash digest
SHA256 c76df3a7f4f2e01e63169f8dfc78e73a4ab7ec56c8d86852fea2280bdb91ada8
MD5 fabf4a51432ffe82d1f6a525e7333247
BLAKE2b-256 34079a2bfb6855f27b08886114370a7bb2d9d20c22f657406f90338949af9b25

See more details on using hashes here.

File details

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

File metadata

  • Download URL: misprint-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for misprint-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 68e465f7033cec6d21f4a12df3a1e22dbe7ee7fb29205594c94565121f12aa6f
MD5 5878fdb7fe76fa4ee1ee52082b46ff3d
BLAKE2b-256 80fb9bfeffd1961b467912712f5b86f5af9ab363549c7b7a638aa4ac94e5ae4c

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