Redact Secrets Before Logging

Medium ⏱ 12 min 55% acceptance ★★★★☆ 4.4
Write redact(data, secret_keys) walking a parsed JSON dict recursively (dicts nested in dicts) and replacing the value of any key in secret_keys with '***' — payloads must be loggable without leaking passwords or tokens. Return a new structure.

Examples

Example 1
Input
redact({'user': 'ana', 'auth': {'password': 'x1'}}, {'password'})
Output
{'user': 'ana', 'auth': {'password': '***'}}
Explanation

The nested secret got masked.

Constraints

  • Recurse into nested dicts.
  • Original input unmodified.

Topics

JSONrecursive transform

Companies

OktaAuth0CrowdStrike

Hints

Hint 1

Rebuild each dict in a comprehension or loop.

Hint 2

Replace when key in secret_keys, recurse when value is a dict.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.