Interview Prep/Python for DevOps/Exceptions, context managers, files & logging

How do you avoid leaking secrets into logs?

AdvancedConceptExceptions, context managers, files & logging
Answer
  • Never log whole request objects, headers, environment dumps or config dicts.
  • Wrap secrets in a type whose __repr__ and __str__ return "****" (pydantic's SecretStr does this).
  • Add a logging.Filter that redacts patterns (AKIA[0-9A-Z]{16}, Bearer \S+, password=\S+) as a safety net.
  • Keep urllib3/botocore at WARNING in production; DEBUG for botocore logs full request bodies.
  • Mask in CI (::add-mask:: in GitHub Actions).

Redaction filters are a backstop, not the control. The control is not logging the value at all.

All Python for DevOps questions Roadmap: Python for automation

More on Exceptions, context managers, files & logging