Skip to content

fix(api): Properly fetch org from token - #65403

Merged
schew2381 merged 20 commits into
masterfrom
seiji/fix/properly-fetch-org-from-token
Mar 19, 2024
Merged

schew2381 merged 20 commits into
masterfrom
seiji/fix/properly-fetch-org-from-token

Conversation

@schew2381

@schew2381 schew2381 commented Feb 20, 2024 •

Copy link
Copy Markdown
Contributor

Requires #67159

Uses the new param in sentry app to fetch the relevant SentryAppInstallation from a token id by also checking SentryAppInstallationToken, which will link to the relevant SentryAppInstallation.

Moved test for get_rate_limit_key from test_ratelimit_middleware.py to test_get_ratelimit_key.py.
Also added individual tests for ApiTokenReplica and OrgAuthToken models

Closes https://github.com/getsentry/team-core-product-foundations/issues/159

@schew2381 schew2381 self-assigned this Feb 20, 2024
@github-actions github-actions Bot added the Scope: Backend Automatically applied to PRs that change backend components label Feb 20, 2024
@codecov

codecov Bot commented Feb 20, 2024 •

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 84.31%. Comparing base (397e8c7) to head (bcb041b).
Report is 8 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #65403      +/-   ##
==========================================
- Coverage   84.31%   84.31%   -0.01%     
==========================================
  Files        5306     5306              
  Lines      237091   237093       +2     
  Branches    41002    41003       +1     
==========================================
- Hits       199915   199913       -2     
- Misses      36958    36962       +4     
  Partials      218      218              
Files Coverage Δ
src/sentry/ratelimits/utils.py 90.47% <100.00%> (-3.65%) ⬇️

... and 6 files with indirect coverage changes

@getsantry

getsantry Bot commented Mar 12, 2024

Copy link
Copy Markdown
Contributor

This issue 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 remove the label Waiting for: Community, I will leave it alone ... forever!


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

@getsantry getsantry Bot added the Stale label Mar 12, 2024
@schew2381 schew2381 removed the Stale label Mar 12, 2024
@github-actions github-actions Bot added the Scope: Frontend Automatically applied to PRs that change frontend components label Mar 14, 2024
@github-actions

Copy link
Copy Markdown
Contributor

🚨 Warning: This pull request contains Frontend and Backend changes!

It's discouraged to make changes to Sentry's Frontend and Backend in a single pull request. The Frontend and Backend are not atomically deployed. If the changes are interdependent of each other, they must be separated into two pull requests and be made forward or backwards compatible, such that the Backend or Frontend can be safely deployed independently.

Have questions? Please ask in the #discuss-dev-infra channel.

@schew2381 schew2381 removed the Scope: Frontend Automatically applied to PRs that change frontend components label Mar 15, 2024
Comment on lines 89 to +93
token_id = request_auth.id
elif isinstance(request_auth, AuthenticatedToken) and request_auth.entity_id is not None:
token_id = request_auth.entity_id
elif isinstance(request_auth, ApiTokenReplica) and request_auth.apitoken_id is not None:
token_id = request_auth.apitoken_id

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Seems kinda funny/dangerous that we have to do this. Are we concerned about the token falling into the wrong condition?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Also, I think it might make sense for this entire if condition to be a method on the request_auth class? Seems like it would be useful and consistent logic we'd want across the code base

@schew2381 schew2381 Mar 15, 2024 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

These checks are done because from what I understand for hybrid cloud, the AuthenticatedToken class is like an interface for all our tokens that we pass around in the control region.

is_api_token_auth ensures the token is either an ApiToken or AuthenticatedToken that was created from an ApiToken so it's guaranteed that one of the conditions will pass.

I think the rate limiter can run in all regions, so that's why we can't simply do request_auth.organization when trying to fetch the org, and instead have to:

  1. Fetch the token
  2. Use a hybrid cloud service to find the installation belonging to the token
  3. Return the org on the installation

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

These checks are done because from what I understand for hybrid cloud, the AuthenticatedToken class is like an interface for all our tokens that we pass around in the control region.

Correct, AuthenticatedToken is used in all silo modes though. The isinstance() checks are likely to appease type checking.

@schew2381
schew2381 marked this pull request as ready for review March 15, 2024 19:30
@schew2381
schew2381 requested review from a team as code owners March 15, 2024 19:30
@schew2381
schew2381 removed the request for review from a team March 15, 2024 19:30

@volokluev volokluev left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This looks good to me but I cannot comment on the hybrid cloud stuff, it's out of my wheelhouse

Comment on lines 89 to +93
token_id = request_auth.id
elif isinstance(request_auth, AuthenticatedToken) and request_auth.entity_id is not None:
token_id = request_auth.entity_id
elif isinstance(request_auth, ApiTokenReplica) and request_auth.apitoken_id is not None:
token_id = request_auth.apitoken_id

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

These checks are done because from what I understand for hybrid cloud, the AuthenticatedToken class is like an interface for all our tokens that we pass around in the control region.

Correct, AuthenticatedToken is used in all silo modes though. The isinstance() checks are likely to appease type checking.

Comment thread src/sentry/ratelimits/utils.py Resolved
filter={
"status": SentryAppInstallationStatus.INSTALLED,
"api_token_id": token_id,
"api_installation_token_id": token_id,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We can't start using new RPC methods and parameters in the same pull request that the method or parameter is defined in. The reason for this is deploys don't go out to all regions and control at the same time. Regions deploy first, and then control. With the current changes you would be sending an unsupported filter condition to control for the duration of the deploy (20 min or so).

Instead we need to deploy the new parameter support first, and then deploy changes that use the new filter option.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Just merged #67159, waiting for it to deploy

@schew2381 schew2381 Mar 18, 2024 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

deploy went out, just have to deploy this one now

@schew2381
schew2381 merged commit 873de12 into master Mar 19, 2024
@schew2381
schew2381 deleted the seiji/fix/properly-fetch-org-from-token branch March 19, 2024 17:20
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 4, 2024

This branch was successfully deployed

1 active deployment
Preview — bcb041bf Deployed Mar 18, 2024 by vercel[bot]
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

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