|
| 1 | +# Unless explicitly stated otherwise all files in this repository are licensed |
| 2 | +# under the Apache License Version 2.0. |
| 3 | +# This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 4 | +# Copyright 2021 Datadog, Inc. |
| 5 | + |
| 6 | +import json |
| 7 | +import logging |
| 8 | + |
| 9 | +redactable_keys = ["authorization", "x-authorization", "password", "token"] |
| 10 | +max_depth = 10 |
| 11 | +logger = logging.getLogger(__name__) |
| 12 | + |
| 13 | + |
| 14 | +def tag_object(span, key, obj, depth=0): |
| 15 | + if depth >= max_depth: |
| 16 | + return |
| 17 | + else: |
| 18 | + depth += 1 |
| 19 | + if obj is None: |
| 20 | + return span.set_tag(key, obj) |
| 21 | + if _should_try_string(obj): |
| 22 | + parsed = None |
| 23 | + try: |
| 24 | + parsed = json.loads(obj) |
| 25 | + return tag_object(span, key, parsed, depth) |
| 26 | + except ValueError: |
| 27 | + redacted = _redact_val(key, obj[0:5000]) |
| 28 | + return span.set_tag(key, redacted) |
| 29 | + if isinstance(obj, int) or isinstance(obj, float): |
| 30 | + return span.set_tag(key, obj) |
| 31 | + if isinstance(obj, list): |
| 32 | + for k, v in enumerate(obj): |
| 33 | + formatted_key = "{}.{}".format(key, k) |
| 34 | + tag_object(span, formatted_key, v, depth) |
| 35 | + return |
| 36 | + if isinstance(obj, object): |
| 37 | + for k in obj: |
| 38 | + v = obj.get(k) |
| 39 | + formatted_key = "{}.{}".format(key, k) |
| 40 | + tag_object(span, formatted_key, v, depth) |
| 41 | + return |
| 42 | + |
| 43 | + |
| 44 | +def _should_try_string(obj): |
| 45 | + try: |
| 46 | + if isinstance(obj, str) or isinstance(obj, unicode): |
| 47 | + return True |
| 48 | + except NameError: |
| 49 | + if isinstance(obj, bytes): |
| 50 | + return True |
| 51 | + |
| 52 | + return False |
| 53 | + |
| 54 | + |
| 55 | +def _redact_val(k, v): |
| 56 | + split_key = k.split(".").pop() or k |
| 57 | + if split_key in redactable_keys: |
| 58 | + return "redacted" |
| 59 | + return v |
0 commit comments