Skip to main content

Automatically mask sensitive values (API keys, passwords, tokens) in logs and print output

Project description

philiprehberger-masked-print

Tests PyPI version License

Automatically mask sensitive values (API keys, passwords, tokens) in logs and print output.

Install

pip install philiprehberger-masked-print

Usage

from philiprehberger_masked_print import mask, mask_dict, MaskedFormatter

# Mask a single string
masked = mask("sk-abc123secret456xyz")
# "sk-a*************xyz"

Mask dictionaries

config = {
    "host": "localhost",
    "password": "super-secret-value",
    "database": {
        "connection_string": "postgres://admin:pass@localhost/db",
    },
}

safe = mask_dict(config)
# {
#     "host": "localhost",
#     "password": "supe***********lue",
#     "database": {
#         "connection_string": "post*****************/db",
#     },
# }

Auto-mask log output

import logging
from philiprehberger_masked_print import MaskedFormatter

handler = logging.StreamHandler()
handler.setFormatter(MaskedFormatter("%(levelname)s: %(message)s"))

logger = logging.getLogger("app")
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)

logger.info("Using key sk-proj-abc123def456ghi789jkl012mno")
# INFO: Using key sk-p*************************mno

API

Function / Class Description
mask(value, *, show_first=4, show_last=3, mask_char="*") Mask a string, keeping the first and last N characters visible
mask_dict(data, *, sensitive_keys=None, show_first=4, show_last=3) Recursively mask sensitive key values in a dictionary
MaskedFormatter(fmt) Logging formatter that auto-redacts secret patterns (sk-..., eyJ..., AKIA..., URL credentials)

Development

pip install -e .
python -m pytest tests/ -v

License

MIT

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

philiprehberger_masked_print-0.1.4.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

philiprehberger_masked_print-0.1.4-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file philiprehberger_masked_print-0.1.4.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_masked_print-0.1.4.tar.gz
Algorithm Hash digest
SHA256 77cb4a02de9b1a0de1e74d112fe5fed22fd829f64b5bea5e8e24080840877985
MD5 a9695c18ff855bcc84131e2cb8e81583
BLAKE2b-256 7e6a342c6a8a045d85e86ae44e505b06da5fbca8ab2ceedee26ee1aa07f2641a

See more details on using hashes here.

File details

Details for the file philiprehberger_masked_print-0.1.4-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_masked_print-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 302f3bb603ac7c0d3e3b1805f819aa9b85c8fe621850e38f1a5a31ca00d81ef2
MD5 4ac0d2865285a82b6679f4956b15980e
BLAKE2b-256 7d63205af372707bcf18ead17caf6cffeda8d9570703433ee801ffac59058c4f

See more details on using hashes here.

Supported by

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