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 Last updated

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

Installation

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",
#     },
# }

Extend defaults at runtime

from philiprehberger_masked_print import register_pattern, register_sensitive_key

# Add a domain-specific secret pattern picked up by MaskedFormatter
register_pattern(r"PINPIN-\d{4,}")

# Add a custom key that mask_dict should treat as sensitive
register_sensitive_key("session_id")

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)
register_pattern(pattern) Register an extra regex pattern for MaskedFormatter to redact
register_sensitive_key(key) Add a key substring to the default sensitive-key set used by mask_dict

Development

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

Support

If you find this project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

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.2.0.tar.gz (7.4 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.2.0-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for philiprehberger_masked_print-0.2.0.tar.gz
Algorithm Hash digest
SHA256 470abb7da4d4d426a8926c64694562cf2aaf437cf06dcbcad2d66ae26e3d651d
MD5 df4f2dcf16d1fae148d052aaeb5832b1
BLAKE2b-256 2246df25b99e9f839ace167c6e5a9d57f12c245856f9c7a858a2e5a5a6d07a78

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_masked_print-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6aad030c05a4ffac12f61992a7d0ff03406b68facd6b4e13649fc0ace26f2407
MD5 74b0d4a7732e596fb581b34eda33f6af
BLAKE2b-256 781822e56311e1a7324912c074083007a3639c03bc4f0f8c70ad5a8a200780d8

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