Skip to content

Commit

Permalink
Remove secrets from logs
Browse files Browse the repository at this point in the history
(closes DataDog#274)
  • Loading branch information
jirikuncar committed Jan 23, 2020
1 parent 657bca9 commit dd269ac
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
8 changes: 8 additions & 0 deletions datadog/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from datadog.threadstats import ThreadStats, datadog_lambda_wrapper, lambda_metric # noqa
from datadog.util.compat import iteritems, NullHandler, text
from datadog.util.config import get_version
from datadog.util.format import SecretFormatter
from datadog.util.hostname import get_hostname


Expand Down Expand Up @@ -98,6 +99,13 @@ def initialize(api_key=None, app_key=None, host_name=None, api_host=None,
api._host_name = host_name or api._host_name or get_hostname(hostname_from_config)
api._api_host = api_host or api._api_host or os.environ.get('DATADOG_HOST', 'https://api.datadoghq.com')

# Remove secrets from all root loggers
secrets=[api._api_key, api._application_key]
for h in logging.root.handlers:
if isinstance(h.formatter, SecretFormatter) and h.formatter.secrets == secrets:
continue
h.setFormatter(SecretFormatter(h.formatter, secrets=secrets))

# Statsd configuration
# ...overrides the default `statsd` instance attributes
if statsd_socket_path:
Expand Down
18 changes: 18 additions & 0 deletions datadog/util/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,21 @@ def force_to_epoch_seconds(epoch_sec_or_dt):

def normalize_tags(tag_list):
return [tag.replace(',', '_') for tag in tag_list]


class SecretFormatter(object):
"""Remove secrets from logs."""

def __init__(self, formatter, secrets, replace="[SECRET]"):
self.formatter = formatter
self.secrets = secrets
self.replace = replace

def format(self, record):
msg = self.formatter.format(record)
for secret in self.secrets:
msg = msg.replace(secret, self.replace)
return msg

def __getattr__(self, attr):
return getattr(self.formatter, attr)

0 comments on commit dd269ac

Please sign in to comment.