Skip to content

Commit 57c73e4

Browse files
committed
Merge pull request #3134 from getsentry/perms
Fixed API keys not correctly reflecting scopes
1 parent 5c9ec80 commit 57c73e4

File tree

4 files changed

+85
-4
lines changed

4 files changed

+85
-4
lines changed

src/sentry/models/apikey.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def get_audit_log_data(self):
9595
}
9696

9797
def get_scopes(self):
98-
return self.scopes.keys()
98+
return [k for k, v in self.scopes.iteritems() if v]
9999

100100

101101
class SystemKey(object):
@@ -117,5 +117,8 @@ def get_scopes(self):
117117
# All scopes!
118118
return ApiKey.scopes
119119

120+
def has_scope(self, scope):
121+
return True
122+
120123

121124
ROOT_KEY = SystemKey()

tests/sentry/api/bases/test_organization.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ def has_object_perm(self, method, obj, auth=None, user=None, is_superuser=None):
1919
request.user = user
2020
request.method = method
2121
request.is_superuser = lambda: is_superuser if is_superuser is not None else user.is_superuser
22-
return perm.has_object_permission(request, None, obj)
22+
return (
23+
perm.has_permission(request, None) and
24+
perm.has_object_permission(request, None, obj)
25+
)
2326

2427

2528
class OrganizationPermissionTest(OrganizationPermissionBase):
@@ -44,11 +47,34 @@ def test_org_member(self):
4447
def test_api_key_with_org_access(self):
4548
key = ApiKey.objects.create(
4649
organization=self.org,
50+
scopes=getattr(ApiKey.scopes, 'org:read'),
4751
)
4852
assert self.has_object_perm('GET', self.org, auth=key)
4953

5054
def test_api_key_without_org_access(self):
5155
key = ApiKey.objects.create(
5256
organization=self.create_organization(),
57+
scopes=getattr(ApiKey.scopes, 'org:read')
58+
)
59+
assert not self.has_object_perm('GET', self.org, auth=key)
60+
61+
def test_api_key_without_access(self):
62+
key = ApiKey.objects.create(
63+
organization=self.org,
64+
scopes=0,
5365
)
5466
assert not self.has_object_perm('GET', self.org, auth=key)
67+
68+
def test_api_key_with_wrong_access(self):
69+
key = ApiKey.objects.create(
70+
organization=self.org,
71+
scopes=getattr(ApiKey.scopes, 'team:read'),
72+
)
73+
assert not self.has_object_perm('GET', self.org, auth=key)
74+
75+
def test_api_key_with_wrong_access_for_method(self):
76+
key = ApiKey.objects.create(
77+
organization=self.org,
78+
scopes=getattr(ApiKey.scopes, 'org:read'),
79+
)
80+
assert not self.has_object_perm('PUT', self.org, auth=key)

tests/sentry/api/bases/test_project.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ def has_object_perm(self, method, obj, auth=None, user=None, is_superuser=None):
2121
request.user = user
2222
request.method = method
2323
request.is_superuser = lambda: is_superuser if is_superuser is not None else user.is_superuser
24-
return perm.has_object_permission(request, None, obj)
24+
return (
25+
perm.has_permission(request, None) and
26+
perm.has_object_permission(request, None, obj)
27+
)
2528

2629

2730
class ProjectPermissionTest(ProjectPermissionBase):
@@ -76,11 +79,34 @@ def test_member_with_team_access(self):
7679
def test_api_key_with_org_access(self):
7780
key = ApiKey.objects.create(
7881
organization=self.org,
82+
scopes=getattr(ApiKey.scopes, 'project:read'),
7983
)
8084
assert self.has_object_perm('GET', self.project, auth=key)
8185

8286
def test_api_key_without_org_access(self):
8387
key = ApiKey.objects.create(
8488
organization=self.create_organization(),
89+
scopes=getattr(ApiKey.scopes, 'project:read'),
90+
)
91+
assert not self.has_object_perm('GET', self.project, auth=key)
92+
93+
def test_api_key_without_access(self):
94+
key = ApiKey.objects.create(
95+
organization=self.org,
96+
scopes=0,
8597
)
8698
assert not self.has_object_perm('GET', self.project, auth=key)
99+
100+
def test_api_key_with_wrong_access(self):
101+
key = ApiKey.objects.create(
102+
organization=self.org,
103+
scopes=getattr(ApiKey.scopes, 'team:read'),
104+
)
105+
assert not self.has_object_perm('GET', self.project, auth=key)
106+
107+
def test_api_key_with_wrong_access_for_method(self):
108+
key = ApiKey.objects.create(
109+
organization=self.org,
110+
scopes=getattr(ApiKey.scopes, 'project:read'),
111+
)
112+
assert not self.has_object_perm('PUT', self.project, auth=key)

tests/sentry/api/bases/test_team.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ def has_object_perm(self, method, obj, auth=None, user=None, is_superuser=None):
2020
request.user = user
2121
request.method = method
2222
request.is_superuser = lambda: is_superuser if is_superuser is not None else user.is_superuser
23-
return perm.has_object_permission(request, None, obj)
23+
return (
24+
perm.has_permission(request, None) and
25+
perm.has_object_permission(request, None, obj)
26+
)
2427

2528

2629
class TeamPermissionTest(TeamPermissionBase):
@@ -55,11 +58,34 @@ def test_get_with_team_membership(self):
5558
def test_get_api_key_with_org_access(self):
5659
key = ApiKey.objects.create(
5760
organization=self.org,
61+
scopes=getattr(ApiKey.scopes, 'team:read'),
5862
)
5963
assert self.has_object_perm('GET', self.team, auth=key)
6064

6165
def test_get_api_key_without_org_access(self):
6266
key = ApiKey.objects.create(
6367
organization=self.create_organization(),
68+
scopes=getattr(ApiKey.scopes, 'team:read'),
6469
)
6570
assert not self.has_object_perm('GET', self.team, auth=key)
71+
72+
def test_api_key_without_access(self):
73+
key = ApiKey.objects.create(
74+
organization=self.org,
75+
scopes=0,
76+
)
77+
assert not self.has_object_perm('GET', self.org, auth=key)
78+
79+
def test_api_key_with_wrong_access(self):
80+
key = ApiKey.objects.create(
81+
organization=self.org,
82+
scopes=getattr(ApiKey.scopes, 'project:read'),
83+
)
84+
assert not self.has_object_perm('GET', self.org, auth=key)
85+
86+
def test_api_key_with_wrong_access_for_method(self):
87+
key = ApiKey.objects.create(
88+
organization=self.org,
89+
scopes=getattr(ApiKey.scopes, 'team:read'),
90+
)
91+
assert not self.has_object_perm('PUT', self.project, auth=key)

0 commit comments

Comments
 (0)