Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add secrets sanitization helpers #6107

Merged
merged 3 commits into from
Mar 24, 2020
Merged

Conversation

florimondmanca
Copy link
Contributor

@florimondmanca florimondmanca commented Mar 20, 2020

What does this PR do?

Add helpers to sanitize any known secrets (i.e. those manipulated by an integration) from logs, service check messages, and exception tracebacks.

Motivation

Make it easier to ensure passwords aren't leaked in logs and UI messages.

Of limited use for credentials contained in URLs (as I believe the Agent already scrubs those?), but most useful for general logs that might leak secrets, whether those leaks are known:

password = instance.get('password')
if password is not None:
message = message.replace(password, "*" * 6)
self.service_check(self.SERVICE_CHECK_NAME, AgentCheck.CRITICAL, tags=service_check_tags, message=message)

or unknown/possible/unproven: #5715 (comment)

In general we should register any password/secret managed by an integration.

Additional Notes

Review checklist (to be filled by reviewers)

  • Feature or bugfix MUST have appropriate tests (unit, integration, e2e)
  • PR title must be written as a CHANGELOG entry (see why)
  • Files changes must correspond to the primary purpose of the PR as described in the title (small unrelated changes should have their own PR)
  • PR must have changelog/ and integration/ labels attached

@codecov
Copy link

codecov bot commented Mar 20, 2020

Codecov Report

Merging #6107 into master will decrease coverage by 0.96%.
The diff coverage is 89.18%.

Impacted Files Coverage Δ
datadog_checks_base/datadog_checks/base/log.py 84.05% <70.00%> (-6.14%) ⬇️
datadog_checks_base/tests/test_agent_check.py 98.34% <88.37%> (-1.14%) ⬇️
...dog_checks_base/datadog_checks/base/checks/base.py 84.93% <93.75%> (+0.36%) ⬆️
...g_checks_base/datadog_checks/base/utils/secrets.py 100.00% <100.00%> (ø)
datadog_checks_base/tests/test_utils.py 100.00% <100.00%> (ø)
couch/tests/test_couchv2.py 96.75% <0.00%> (-1.18%) ⬇️
pgbouncer/datadog_checks/pgbouncer/pgbouncer.py 76.22% <0.00%> (-0.39%) ⬇️
statsd/datadog_checks/statsd/statsd.py 81.81% <0.00%> (-0.33%) ⬇️
directory/datadog_checks/directory/directory.py 88.57% <0.00%> (-0.32%) ⬇️
php_fpm/datadog_checks/php_fpm/php_fpm.py 80.34% <0.00%> (-0.17%) ⬇️
... and 299 more

@florimondmanca florimondmanca changed the title Add password redaction helpers Add secrets redaction helpers Mar 20, 2020
@florimondmanca florimondmanca mentioned this pull request Mar 20, 2020
18 tasks
Copy link
Contributor

@ofek ofek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still would not protect against exposure in chained tracebacks, maybe worth doing as a more general filtering function, used by logger and in run()?

datadog_checks_base/datadog_checks/base/log.py Outdated Show resolved Hide resolved
Copy link
Member

@mgarabed mgarabed left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great - will be very helpful!

There are some other escape vectors though, a good use case can be seen in #6003. That case requires sanitizing the message prior to service_check and an exception message.

One approach would be to have the sanitize filter as a method on the AgentCheck class, then checks could call it directly.

@florimondmanca florimondmanca changed the title Add secrets redaction helpers Add secrets sanitization helpers Mar 23, 2020
@florimondmanca florimondmanca force-pushed the florimondmanca/redact-secrets branch from 0cbc9fa to 64a9840 Compare March 23, 2020 09:47
@florimondmanca florimondmanca requested review from ofek and mgarabed March 23, 2020 09:47
@florimondmanca
Copy link
Contributor Author

florimondmanca commented Mar 23, 2020

@ofek @mgarabed Thanks, updated the PR to:

  • Minimize the impact in the common case (down to an attribute lookup)
  • Add registration/sanitization methods on AgentCheck directly.
  • Apply scrubbing to exception messages/tracebacks too.

LMK what you think!

@hithwen hithwen requested a review from therve March 23, 2020 10:08
ofek
ofek previously requested changes Mar 23, 2020
Copy link
Contributor

@ofek ofek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great

datadog_checks_base/datadog_checks/base/log.py Outdated Show resolved Hide resolved
@florimondmanca florimondmanca requested a review from ofek March 24, 2020 14:55
Copy link
Member

@mgarabed mgarabed left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

Copy link
Contributor

@ofek ofek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Diff is reasonably small, no need to break this up

Great job!

@florimondmanca florimondmanca mentioned this pull request Mar 24, 2020
4 tasks
@florimondmanca
Copy link
Contributor Author

Thanks all!

CI failures are unrelated:

Merging…

@florimondmanca florimondmanca merged commit 42a24d2 into master Mar 24, 2020
@florimondmanca florimondmanca deleted the florimondmanca/redact-secrets branch March 24, 2020 15:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants