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

feat(scopes): Enforce scope hierarchy #54510

Merged
merged 14 commits into from
Sep 26, 2023

Conversation

schew2381
Copy link
Contributor

@schew2381 schew2381 commented Aug 9, 2023

Note

Implementation

To model the hierarchy I used a simple dict of scope -> all granted scopes. I chose this approach b/c it's very simple and easy to enforce in the code.

We use a pre-save signal on the ApiKey and ApiToken models to enforce scope hierarchy. The basic scope hierarchy for each resource is:

  • read grants nothing
  • write grants read
  • admin grants read+write

When updating one of these models, there is no way to know if the scopes are enforced without fetching it from the DB. To avoid this trip, we always iterate through the hierarchy mapping.

@github-actions github-actions bot added the Scope: Backend Automatically applied to PRs that change backend components label Aug 9, 2023
@codecov
Copy link

codecov bot commented Aug 10, 2023

Codecov Report

Merging #54510 (142dc96) into master (4f44f1d) will increase coverage by 23.37%.
Report is 1 commits behind head on master.
The diff coverage is 100.00%.

❗ Current head 142dc96 differs from pull request most recent head b9bacb7. Consider uploading reports for the commit b9bacb7 to get more accurate results

@@             Coverage Diff             @@
##           master   #54510       +/-   ##
===========================================
+ Coverage   55.22%   78.60%   +23.37%     
===========================================
  Files        5069     5080       +11     
  Lines      218768   218661      -107     
  Branches    37052    37020       -32     
===========================================
+ Hits       120825   171881    +51056     
+ Misses      94455    41212    -53243     
- Partials     3488     5568     +2080     
Files Changed Coverage
src/sentry/conf/server.py 100.00%
src/sentry/models/apiscopes.py 100.00%
src/sentry/receivers/__init__.py 100.00%
src/sentry/receivers/tokens.py 100.00%

Comment on lines +30 to +33
"scopes": [
"event:read",
"project:read",
],
Copy link
Contributor Author

@schew2381 schew2381 Aug 10, 2023

Choose a reason for hiding this comment

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

fix: refactor test to work with alphabetical order

@@ -64,7 +64,7 @@ def setUp(self):
super().setUp()

self.sentry_app = self.create_sentry_app(
name="external_app", organization=self.org, scopes=("org:write", "team:admin")
name="external_app", organization=self.org, scopes=("org:read", "team:read")
Copy link
Contributor Author

@schew2381 schew2381 Aug 10, 2023

Choose a reason for hiding this comment

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

fix: refactor test to avoid implicit hierarchy

@@ -79,4 +79,4 @@ def test_create_token(self):

# verify token was created properly
assert api_token.expires_at == today
assert api_token.scope_list == ["org:write", "team:admin"]
assert api_token.scope_list == ["org:read", "team:read"]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

fix: refactor test to avoid implicit hierarchy

@@ -325,7 +325,7 @@ def test_valid_params_id_token_additional_scopes(self):
data = json.loads(resp.content)
token = ApiToken.objects.get(token=data["access_token"])

assert token.get_scopes() == ["openid", "profile", "email"]
assert token.get_scopes() == ["email", "openid", "profile"]
Copy link
Contributor Author

@schew2381 schew2381 Sep 11, 2023

Choose a reason for hiding this comment

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

nit: change ordering b/c response scopes will be in alphabetical order

@schew2381 schew2381 requested a review from a team September 11, 2023 23:39
@schew2381 schew2381 marked this pull request as ready for review September 11, 2023 23:40
@schew2381 schew2381 requested a review from a team as a code owner September 11, 2023 23:40
@schew2381 schew2381 changed the title [WIP] Add token hierarchy feat(scopes): Add token hierarchy Sep 11, 2023
@schew2381 schew2381 marked this pull request as draft September 15, 2023 16:59
src/sentry/conf/server.py Show resolved Hide resolved
src/sentry/models/apiscopes.py Show resolved Hide resolved
src/sentry/receivers/tokens.py Show resolved Hide resolved
@schew2381 schew2381 requested a review from a team September 25, 2023 17:23
@schew2381 schew2381 changed the title feat(scopes): Add token hierarchy feat(scopes): Enforce scope hierarchy Sep 25, 2023
@schew2381 schew2381 merged commit 647704b into master Sep 26, 2023
49 checks passed
@schew2381 schew2381 deleted the seiji/feat/implement-scope-hierarchy branch September 26, 2023 18:44
@github-actions github-actions bot locked and limited conversation to collaborators Oct 12, 2023
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.

3 participants