Skip to content

fix(sentry app authorizations): Return token if already made for sentry #91507

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

Merged
merged 8 commits into from
May 20, 2025

Conversation

Christinarlong
Copy link
Contributor

@Christinarlong Christinarlong commented May 12, 2025

Made from this issue (and thread) where if our outbox requests took too long or failed in some way, the authorizations endpoint would 500 and not return an ApiToken leaving users without a working ApiToken and no way to get a new one

@github-actions github-actions bot added the Scope: Backend Automatically applied to PRs that change backend components label May 12, 2025
Comment on lines -40 to -46
except (SentryAppIntegratorError, SentryAppSentryError):
logger.info(
"refresher.context",
extra={
"application_id": self.application.id,
"refresh_token": self.refresh_token[-4:],
},
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Taking these logs out as they were to debug a prior customer issue a while back

@@ -106,7 +119,7 @@ def application(self) -> ApiApplication:
try:
return ApiApplication.objects.get(client_id=self.client_id)
except ApiApplication.DoesNotExist:
raise SentryAppIntegratorError(
raise SentryAppSentryError(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Making this consistent with _validator, this step could fail because either the user entered the wrong client id or our db actually borked. I leaned to the latter to be a bit more defensive but we can see later how common this issue is.

Copy link

codecov bot commented May 13, 2025

Codecov Report

Attention: Patch coverage is 98.21429% with 1 line in your changes missing coverage. Please review.

⚠️ Parser warning

The parser emitted a warning. Please review your JUnit XML file:

Warning while parsing testcase attributes: Limit of string is 1000 chars, for name, we got 2083 at 1:157236 in /home/runner/work/sentry/sentry/.artifacts/pytest.junit.xml
Files with missing lines Patch % Lines
src/sentry/sentry_apps/token_exchange/refresher.py 92.30% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##           master   #91507    +/-   ##
========================================
  Coverage   87.63%   87.63%            
========================================
  Files       10356    10349     -7     
  Lines      587133   587670   +537     
  Branches    22585    22653    +68     
========================================
+ Hits       514506   515033   +527     
- Misses      72199    72218    +19     
+ Partials      428      419     -9     

@Christinarlong Christinarlong marked this pull request as ready for review May 13, 2025 16:27
@Christinarlong Christinarlong requested review from a team as code owners May 13, 2025 16:27
},
token = self._create_new_token()
return token
except OperationalError as e:
Copy link
Member

@GabeVillalobos GabeVillalobos May 13, 2025

Choose a reason for hiding this comment

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

Instead of catching an OperationalError here, I wonder if we should instead catch and raise this in the outbox_context decorator as a distinct error. Handling DB level concerns at the API layer seems like something we want to avoid if possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried to do this via adding OutboxFlushError at the on_commit process since that lambda is responsible for all actions after the database has committed.

I was having trouble understanding what the test_outbox tests do & how to write one for the error handling changes. (I also didn't want to spend too much more time digging so I only made a test for refresher.py :bufo-hide:)

Lmk thoughts /

Copy link
Member

@markstory markstory left a comment

Choose a reason for hiding this comment

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

Makes sense to me. We need to not 500 when outbox processing fails, as we'll eventually complete the deferred work.

Comment on lines 198 to 206
try:
transaction.on_commit(
lambda: self.drain_shard(), using=router.db_for_write(type(self))
)
except DatabaseError as e:
raise OutboxFlushError(
f"Failed to process Outbox, {OutboxCategory(self.category).name} due to database error",
self,
) from e
Copy link
Member

Choose a reason for hiding this comment

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

Should this try catch be inside drain_shard()? Looking at the other call sites of drain_shard(), all locations are already catching OutboxFlushError or they don't have a try/except block.

Copy link
Member

Choose a reason for hiding this comment

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

+1, we discussed this via slack and came to the same conclusion. @markstory what do you think about using a distinct exception type for this. OutboxFlushError can be nebulous, but often indicates bad receiver/RPC logic, but maybe it's still the appropriate exception type to raise?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah yeah, sorry I didn't make the changes yesterday but yes we should be excepting within drain_shard since on_commit is only queueing up drain_shard. +1 to the question of what the purpose of OutboxFlushError is though, from when @GabeVillalobos and I talked yesterday it sounded like OutboxFlushError is used to indicate some implementer error. If so, I think it's appropriate if we make raise something else like OutboxDatabaseError.

@Christinarlong Christinarlong merged commit 60dc89a into master May 20, 2025
60 checks passed
@Christinarlong Christinarlong deleted the crl/best-try-refresh-token branch May 20, 2025 20:38
Copy link

sentry-io bot commented May 20, 2025

Suspect Issues

This pull request was deployed and Sentry observed the following issues:

  • ‼️ OutboxDatabaseError: Failed to process Outbox, ORG_AUTH_TOKEN_UPDATE due to database error sentry.tasks.drain_outbox_shards_control View Issue
  • ‼️ OutboxDatabaseError: Failed to process Outbox, SUBSCRIPTION_UPDATE due to database error sentry.tasks.drain_outbox_shards View Issue
  • ‼️ OutboxDatabaseError: Failed to process Outbox, AUTH_IDENTITY_UPDATE due to database error sentry.tasks.check_auth_identities View Issue
  • ‼️ OutboxDatabaseError: Failed to process Outbox, USER_UPDATE due to database error /api/0/users/{user_id}/ View Issue
  • ‼️ OutboxDatabaseError: Failed to process Outbox, ORGAUTHTOKEN_UPDATE_USED due to database error sentry.tasks.drain_outbox_shards View Issue

Did you find this useful? React with a 👍 or 👎

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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants