Skip to main content

philiprehberger-masked-print

Tests PyPI version Last updated

philiprehberger-masked-print

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

Target nested keys with path globs

mask_dict() accepts dotted path globs to mask specific nested fields without touching the default key heuristics.

config = {
    "database": {
        "primary":   {"host": "db1", "password": "p1"},
        "replica":   {"host": "db2", "password": "p2"},
    },
    "auth": {"public_key": "pk", "token": "tk"},
}

safe = mask_dict(
    config,
    paths=["database.*.password", "auth.token"],
)
# database.primary.password and database.replica.password are masked
# auth.token is masked; auth.public_key is left alone

A * in a path glob matches a single segment. Path matching runs in addition to the default sensitive_keys matching, so both rule sets compose.

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, paths=None, show_first=4, show_last=3) Recursively mask sensitive key values; paths targets nested keys with dotted globs like "database.*.password"
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

Release files for philiprehberger-masked-print 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for philiprehberger-masked-print 0.3.0
File Size Uploaded
philiprehberger_masked_print-0.3.0.tar.gz 196.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for philiprehberger-masked-print 0.3.0
File Interpreter ABI Platform
philiprehberger_masked_print-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 202.6 kB

Release files / philiprehberger_masked_print-0.3.0.tar.gz

Download URL philiprehberger_masked_print-0.3.0.tar.gz
Size 196.0 kB
Tags Source
SHA-256 checksum
How to use checksums
f82b412948bd6e431caecf874dde27ec761ac8ceb6ef021e3ebcaf1b65c2877f
BLAKE2b-256 checksum
How to use checksums
f2b664d35eb35e827ac766276d1263129affa760d1325ae7b08d88a9f67ffaa5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.13

Release files / philiprehberger_masked_print-0.3.0-py3-none-any.whl

Download URL philiprehberger_masked_print-0.3.0-py3-none-any.whl
Size 6.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e950e7b94fc6f0f9fc49643726375a1c9fe90f4ca02eb7bfa7daa663edd73a8e
BLAKE2b-256 checksum
How to use checksums
29e86c6f44fc4c1d0528984bd502a4ddf291562abd3d8372d9bc03ef95b56b4c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.13

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files

0.2.0

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page