Skip to content

fix(audit-log): Correct sentry_app key in internal integration events - #125182

Merged
armenzg merged 2 commits into
masterfrom
seer/fix/audit-log-sentry-app-keyerror-bP6JYg
Sep 22, 2026
Merged

armenzg merged 2 commits into
masterfrom
seer/fix/audit-log-sentry-app-keyerror-bP6JYg

Conversation

@sentry

@sentry sentry Bot commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

This PR addresses the KeyError: 'sentry_app' occurring when rendering audit log entries for internal integration token events.

Problem:
The audit log template for INTERNAL_INTEGRATION_ADD_TOKEN and INTERNAL_INTEGRATION_REMOVE_TOKEN events expected a {sentry_app} placeholder. However, the create_audit_entry calls in sentry_internal_app_tokens.py and sentry_internal_app_token_details.py were supplying "sentry_app_slug": sentry_app.slug in their data dictionaries, leading to a KeyError when str.format() was called.

Initial Attempt & Iteration:
An initial attempt included hardening AuditLogEvent.render() in src/sentry/audit_log/manager.py to use str.format_map(collections.defaultdict(str, ...)) to prevent KeyError for missing keys. While this provided defense-in-depth, it was found to conflict with an existing test (test_invalid_template in tests/sentry/api/serializers/test_auditlogentry.py) which specifically asserts that a KeyError is caught by the AuditLogEntrySerializer and results in an empty note. The format_map change bypassed this KeyError catch, causing the test to fail.

Final Solution:
The manager.py change was reverted to its original str.format(**audit_log_entry.data) behavior. The core fix, which is sufficient, is to correct the key name at the call sites:

  • src/sentry/sentry_apps/api/endpoints/sentry_internal_app_tokens.py: Changed "sentry_app_slug": sentry_app.slug to "sentry_app": sentry_app.name for INTERNAL_INTEGRATION_ADD_TOKEN events.
  • src/sentry/sentry_apps/api/endpoints/sentry_internal_app_token_details.py: Changed "sentry_app_slug": sentry_app.slug to "sentry_app": sentry_app.name for INTERNAL_INTEGRATION_REMOVE_TOKEN events.

This aligns the data provided with the template's expectation, resolving the KeyError. The AuditLogEntrySerializer's existing KeyError handling remains as a robust fallback for any future template/data mismatches.

Legal Boilerplate

Look, I get it. The entity doing business as "Sentry" was incorporated in the State of Delaware in 2015 as Functional Software, Inc. and is gonna need some rights from me in order to utilize my contributions in this here PR. So here's the deal: I retain all rights, title and interest in and to my contributions, and by keeping this boilerplate intact I confirm that Sentry can use, modify, copy, and redistribute my contributions, under Sentry's choice of terms.

Fixes SENTRY-42CT

@sentry <feedback>: Autofix iterates on these changes
@sentry stop iterating: Autofix stops iterating on this run

This PR was automatically generated by Sentry. You can adjust this setting at any time.

@sentry
sentry Bot requested review from a team as code owners September 22, 2026 10:49
@github-actions github-actions Bot added the Scope: Backend Automatically applied to PRs that change backend components label Sep 22, 2026
@sentry sentry Bot changed the title fix(audit-log): Prevent KeyError for sentry_app in audit log rendering fix(audit-log): Correct sentry_app key in internal integration events Sep 22, 2026
@armenzg armenzg added the Trigger: getsentry tests Once code is reviewed: apply label to PR to trigger getsentry tests label Sep 22, 2026
@armenzg
armenzg merged commit 35eead5 into master Sep 22, 2026
80 of 81 checks passed
@armenzg
armenzg deleted the seer/fix/audit-log-sentry-app-keyerror-bP6JYg branch September 22, 2026 14:13
scttcper added a commit that referenced this pull request Nov 16, 2026
…1042)

Token created/revoked audit log entries written before #125182 stored
the app under `sentry_app_slug` instead of `sentry_app`, so they raise a
KeyError and show a blank note whenever an org's audit log is loaded.
Those old rows now render with the slug.

Also drops a stray `.format()` on the disabled internal integration note
that would raise on a name with braces in it.

fixes SENTRY-42CT

Co-authored-by: Claude Fable 5.2 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Scope: Backend Automatically applied to PRs that change backend components Trigger: getsentry tests Once code is reviewed: apply label to PR to trigger getsentry tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant