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

HRQB 39 - Remove sensitive data from Sentry events #102

Merged
merged 2 commits into from
Jul 17, 2024

Conversation

ghukill
Copy link
Collaborator

@ghukill ghukill commented Jul 12, 2024

Purpose and background context

HRQBClient deals with sensitive data that comes from the data warehouse. Sentry is useful for alerting us when there are issues, and it provides a lot of context about the incident (e.g. a python Exception). In this context, by default, all variables in scope at the moment are also included. This could result in a Sentry event that has PII information because variables are in scope, even if they have nothing do to directly with the exception.

While we have filtering on Sentry's side after the data has been recieved, this PR removes sensitive data from events before data is sent to Sentry. This is achieved by using the before_send callback in `sentry_sdk.init().

A couple things of note:

There is both a sentry_before_send_callback() function (registered as the callback) and a _remove_sensitive_scope_variables() function, which is called by the former. This allows easier testing of _remove_sensitive_scope_variables(), where it is not directly registered by the Sentry SDK and therefore harder to mock.

Second, the test test_sentry_scope_variables_removed_from_sent_event is worth looking at. This utilizes the library pytest-mock, which was new to me, as this provides a convenience "spy" functionality on traditional mocks. This allows looking at the returned value of a mocked object, without modifying the object's behavior at all. I anticipate using this in future tests as well, in this project and beyond.

Includes new or updated dependencies?

YES

Changes expectations for external applications?

YES: data will be filtered from events before sending to Sentry

What are the relevant tickets?

Developer

  • All new ENV is documented in README
  • All new ENV has been added to staging and production environments
  • All related Jira tickets are linked in commit message(s)
  • Stakeholder approval has been confirmed (or is not needed)

Code Reviewer(s)

  • The commit message is clear and follows our guidelines (not just this PR message)
  • There are appropriate tests covering any new functionality
  • The provided documentation is sufficient for understanding any new functionality introduced
  • Any manual tests have been performed or provided examples verified
  • New dependencies are appropriate or there were no changes

Why these changes are being introduced:

Though data scrubbing is configured in Sentry for this project
removing sensitive data before sending provides another layer
of protection from exposing sensitive data in Sentry issues.

How this addresses that need:
* Utilizes Sentry's before_send() method to remove all variables
and scope data from Sentry events before sending

Side effects of this change:
* Sentry events do not have variables from stack trace,
but still contain helpful debug information

Relevant ticket(s):
* https://mitlibraries.atlassian.net/browse/HRQB-39
Copy link

@ehanson8 ehanson8 left a comment

Choose a reason for hiding this comment

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

A very educational PR for me!

hrqb/config.py Show resolved Hide resolved
tests/test_config.py Show resolved Hide resolved


def test_sentry_scope_variables_removed_from_sent_event(
mocker,

Choose a reason for hiding this comment

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

[Non-blocking] Where does this fixture come from? I looked for it in conftest and by searching for "mocker" in the repo but couldn't find its definition. 🤔

Copy link
Collaborator Author

@ghukill ghukill Jul 17, 2024

Choose a reason for hiding this comment

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

Good question! It looks as though pytest-mock adds this as a default fixture that can be used by tests (without explicit import anywhere).

@ghukill ghukill merged commit 9173ceb into main Jul 17, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants