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

fix: use locks to handle apigrants safely #82052

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

mdtro
Copy link
Member

@mdtro mdtro commented Dec 12, 2024

  • Use locks to ensure an ApiGrant cannot be used twice during a race condition that would result in multiple access/refresh token pairs.
  • When an ApiAuthorization is deleted, remove all ApiGrants as well that may not have been exchanged yet.
  • Update the cleanup script to remove ApiGrants that are past their expiration time, 10 minutes, rather than 30 days.
    • Applicable expired ApiTokens will still be deleted after 30 days, aligned with the current behavior.

@github-actions github-actions bot added the Scope: Backend Automatically applied to PRs that change backend components label Dec 12, 2024
@mdtro mdtro force-pushed the mdtro/handle-api-grant-auth-correctly branch from cb93a9d to edd9b69 Compare December 12, 2024 22:03
@mdtro mdtro marked this pull request as ready for review December 12, 2024 22:35
@mdtro mdtro requested review from a team as code owners December 12, 2024 22:35
Copy link
Member

@sentaur-athena sentaur-athena left a comment

Choose a reason for hiding this comment

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

Mostly questions that can be addressed in another PR. Thanks for fixing this.

@@ -86,6 +87,8 @@ def post(self, request: Request, installation) -> Response:
},
)
return Response({"error": e.msg or "Unauthorized"}, status=403)
except UnableToAcquireLock:
Copy link
Member

Choose a reason for hiding this comment

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

Would this happen frequently? I wonder if we need to do a frontend change to improve user experience if double requests to this endpoint can easily happen.

Copy link
Member Author

Choose a reason for hiding this comment

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

This should never happen in practice.

From the user's perspective, they will not see this as it is the request from the backend system of the app to exchange the authorization code for an access token.

We do make some assumptions here though, if they are receiving a 409, they may be sending two requests too quickly. This is a bug in their implementation, but at least one of their request flows will receive the access token. If not, they can retry the request after the lock has expired (we only hold a lock for 10 seconds).

@getsantry
Copy link
Contributor

getsantry bot commented Jan 8, 2025

This pull request has gone three weeks without activity. In another week, I will close it.

But! If you comment or otherwise update it, I will reset the clock, and if you add the label WIP, I will leave it alone unless WIP is removed ... forever!


"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀

@getsantry getsantry bot added the Stale label Jan 8, 2025
Copy link

codecov bot commented Jan 8, 2025

Codecov Report

Attention: Patch coverage is 91.80328% with 5 lines in your changes missing coverage. Please review.

✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
src/sentry/models/apitoken.py 83.33% 2 Missing ⚠️
...ry_apps/api/endpoints/sentry_app_authorizations.py 33.33% 2 Missing ⚠️
src/sentry/api/endpoints/api_authorizations.py 85.71% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master   #82052   +/-   ##
=======================================
  Coverage   87.89%   87.89%           
=======================================
  Files        9657     9657           
  Lines      546420   546466   +46     
  Branches    21267    21267           
=======================================
+ Hits       480272   480314   +42     
- Misses      65842    65846    +4     
  Partials      306      306           

@mdtro mdtro added WIP and removed Stale labels Jan 8, 2025
@mdtro mdtro marked this pull request as draft January 8, 2025 17:18
@mdtro mdtro force-pushed the mdtro/handle-api-grant-auth-correctly branch from edd9b69 to c9445b0 Compare February 19, 2025 23:50
@mdtro mdtro force-pushed the mdtro/handle-api-grant-auth-correctly branch from af1e62a to 2280c66 Compare February 20, 2025 17:46
mdtro added a commit that referenced this pull request Feb 26, 2025
Pulled from #82052 to break out
these changes into smaller PRs.

- Delete associated grant when revoking API authorizations
- Use proper transaction management with outbox_context
ameliahsu pushed a commit that referenced this pull request Mar 5, 2025
Pulled from #82052 to break out
these changes into smaller PRs.

- Delete associated grant when revoking API authorizations
- Use proper transaction management with outbox_context
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 WIP
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants