4848from sentry .seer import agent_token
4949from sentry .seer .models .agent_write_grant import SeerAgentWriteGrant
5050from sentry .seer .signed_seer_api import _resolve_viewer_context
51- from sentry .service_accounts .models import GRANTABLE_SCOPES , ServiceAccountToken
52- from sentry .service_accounts .tokens import generate_token as generate_service_account_token
53- from sentry .service_accounts .tokens import stored_token_fields
51+ from sentry .service_accounts .models import GRANTABLE_SCOPES
5452from sentry .silo .base import SiloMode
5553from sentry .snuba .dataset import Dataset
5654from sentry .snuba .models import SnubaQuery , SnubaQueryEventType
5755from sentry .testutils .cases import APITestCase , OurLogTestCase
5856from sentry .testutils .helpers .datetime import freeze_time
59- from sentry .testutils .outbox import outbox_runner
6057from sentry .testutils .silo import assume_test_silo_mode
6158from sentry .testutils .skips import requires_snuba
6259from sentry .utils import json
@@ -346,15 +343,6 @@ class MatrixAuthentication(StrEnum):
346343 SCOPED_DOWN_AGENT_TOKEN = "scoped_down_agent_token"
347344 APPROVED_AGENT_TOKEN = "approved_agent_token"
348345 SERVICE_ACCOUNT_VIEWER_CONTEXT = "service_account_viewer_context"
349- SERVICE_ACCOUNT_TOKEN = "service_account_token"
350-
351-
352- # The same service account, authenticated through each path. Both must give every row the
353- # same outcome.
354- SERVICE_ACCOUNT_AUTHENTICATIONS = (
355- MatrixAuthentication .SERVICE_ACCOUNT_VIEWER_CONTEXT ,
356- MatrixAuthentication .SERVICE_ACCOUNT_TOKEN ,
357- )
358346
359347
360348@dataclass (frozen = True )
@@ -631,7 +619,7 @@ def _public_mutation_endpoints() -> tuple[PublicMutationEndpoint, ...]:
631619 {
632620 MatrixAuthentication .AGENT_TOKEN ,
633621 MatrixAuthentication .SCOPED_DOWN_AGENT_TOKEN ,
634- * SERVICE_ACCOUNT_AUTHENTICATIONS ,
622+ MatrixAuthentication . SERVICE_ACCOUNT_VIEWER_CONTEXT ,
635623 }
636624)
637625# Org auth tokens can only be managed without a token (a session, or a viewer context,
@@ -714,7 +702,7 @@ class ServiceAccountGap(StrEnum):
714702 MatrixAuthentication .VIEWER_CONTEXT ,
715703 MatrixAuthentication .AGENT_TOKEN ,
716704 MatrixAuthentication .SCOPED_DOWN_AGENT_TOKEN ,
717- * SERVICE_ACCOUNT_AUTHENTICATIONS ,
705+ MatrixAuthentication . SERVICE_ACCOUNT_VIEWER_CONTEXT ,
718706)
719707PUBLIC_MUTATION_AUTHENTICATIONS = tuple (MatrixAuthentication )
720708
@@ -2401,20 +2389,6 @@ def _create_matrix_service_account(self) -> None:
24012389 ),
24022390 key = SECRET ,
24032391 )
2404- self ._service_account_token = generate_service_account_token (
2405- self .org .slug , "https://test-region.sentry.io"
2406- )
2407- stored = stored_token_fields (self ._service_account_token )
2408- with assume_test_silo_mode (SiloMode .CONTROL ), outbox_runner ():
2409- ServiceAccountToken .objects .create (
2410- service_account = service_account ,
2411- organization_id = self .org .id ,
2412- name = "permission-matrix" ,
2413- token_hashed = stored .token_hashed ,
2414- token_last_characters = stored .token_last_characters ,
2415- scope_list = sorted (SERVICE_ACCOUNT_SCOPES ),
2416- created_by = self .owner ,
2417- )
24182392
24192393 def _assert_service_account_outcome (
24202394 self ,
@@ -2436,14 +2410,14 @@ def _assert_service_account_outcome(
24362410 response .content ,
24372411 )
24382412
2439- def _assert_service_account_user_global_get_denied (
2440- self , endpoint : PublicGetEndpoint , authentication : MatrixAuthentication
2441- ) -> None :
2413+ def _assert_service_account_user_global_get_denied (self , endpoint : PublicGetEndpoint ) -> None :
24422414 self ._create_matrix_service_account ()
24432415 assert endpoint .silo_mode is not None
24442416 with assume_test_silo_mode (endpoint .silo_mode ):
24452417 session_response = self .client .get (endpoint .path_template )
2446- response = self ._request (endpoint .path_template , authentication )
2418+ response = self ._request (
2419+ endpoint .path_template , MatrixAuthentication .SERVICE_ACCOUNT_VIEWER_CONTEXT
2420+ )
24472421
24482422 assert session_response .status_code == 200 , session_response .content
24492423 assert response .status_code in {401 , 403 }, response .content
@@ -2491,11 +2465,6 @@ def _request(
24912465 path , HTTP_X_VIEWER_CONTEXT = self ._service_account_context
24922466 )
24932467 return service_account_response
2494- if authentication is MatrixAuthentication .SERVICE_ACCOUNT_TOKEN :
2495- service_account_token_response : Response = client .get (
2496- path , HTTP_AUTHORIZATION = f"Bearer { self ._service_account_token } "
2497- )
2498- return service_account_token_response
24992468 if authentication in {
25002469 MatrixAuthentication .AGENT_TOKEN ,
25012470 MatrixAuthentication .SCOPED_DOWN_AGENT_TOKEN ,
@@ -2530,8 +2499,6 @@ def _mutation_request(
25302499 )
25312500 elif authentication is MatrixAuthentication .SERVICE_ACCOUNT_VIEWER_CONTEXT :
25322501 headers ["HTTP_X_VIEWER_CONTEXT" ] = self ._service_account_context
2533- elif authentication is MatrixAuthentication .SERVICE_ACCOUNT_TOKEN :
2534- headers ["HTTP_AUTHORIZATION" ] = f"Bearer { self ._service_account_token } "
25352502 elif authentication in {
25362503 MatrixAuthentication .AGENT_TOKEN ,
25372504 MatrixAuthentication .SCOPED_DOWN_AGENT_TOKEN ,
@@ -2650,7 +2617,7 @@ def _assert_public_get_authentication(
26502617 with self .feature (FLAG ):
26512618 agent_bearer = self ._mint_agent_token (requested_scopes = requested_scopes )
26522619 agent_scopes = frozenset (agent_token .decode_agent_token (agent_bearer )["scopes" ])
2653- if authentication in SERVICE_ACCOUNT_AUTHENTICATIONS :
2620+ if authentication is MatrixAuthentication . SERVICE_ACCOUNT_VIEWER_CONTEXT :
26542621 self ._create_matrix_service_account ()
26552622 if endpoint .endpoint_name == "SeerModelsEndpoint" :
26562623 cache .set (SEER_MODELS_CACHE_KEY , {"models" : ["matrix-model" ]})
@@ -2721,7 +2688,7 @@ def _assert_public_get_authentication(
27212688 MatrixAuthentication .USER_TOKEN ,
27222689 user_token_scopes = agent_scopes ,
27232690 )
2724- elif authentication in SERVICE_ACCOUNT_AUTHENTICATIONS :
2691+ elif authentication is MatrixAuthentication . SERVICE_ACCOUNT_VIEWER_CONTEXT :
27252692 authorization_oracle = self ._request (
27262693 path ,
27272694 MatrixAuthentication .USER_TOKEN ,
@@ -2749,7 +2716,7 @@ def _assert_public_get_authentication(
27492716 response .content ,
27502717 )
27512718 return
2752- if authentication in SERVICE_ACCOUNT_AUTHENTICATIONS :
2719+ if authentication is MatrixAuthentication . SERVICE_ACCOUNT_VIEWER_CONTEXT :
27532720 self ._assert_service_account_outcome (endpoint , authorization_oracle , response )
27542721 return
27552722
@@ -2813,7 +2780,7 @@ def _assert_public_mutation_authentication(
28132780 requested_scopes = requested_scopes ,
28142781 )
28152782 agent_scopes = frozenset (agent_token .decode_agent_token (agent_bearer )["scopes" ])
2816- if authentication in SERVICE_ACCOUNT_AUTHENTICATIONS :
2783+ if authentication is MatrixAuthentication . SERVICE_ACCOUNT_VIEWER_CONTEXT :
28172784 self ._create_matrix_service_account ()
28182785
28192786 silo_scope = (
@@ -2885,7 +2852,7 @@ def _assert_public_mutation_authentication(
28852852 MatrixAuthentication .USER_TOKEN ,
28862853 user_token_scopes = agent_scopes ,
28872854 )
2888- elif authentication in SERVICE_ACCOUNT_AUTHENTICATIONS :
2855+ elif authentication is MatrixAuthentication . SERVICE_ACCOUNT_VIEWER_CONTEXT :
28892856 authorization_oracle = self ._rolled_back_mutation_request (
28902857 endpoint ,
28912858 path ,
@@ -2929,7 +2896,7 @@ def _assert_public_mutation_authentication(
29292896 response .content ,
29302897 )
29312898 return
2932- if authentication in SERVICE_ACCOUNT_AUTHENTICATIONS :
2899+ if authentication is MatrixAuthentication . SERVICE_ACCOUNT_VIEWER_CONTEXT :
29332900 self ._assert_service_account_outcome (endpoint , authorization_oracle , response )
29342901 return
29352902
@@ -2982,13 +2949,10 @@ def _install_matrix_row_tests[E: (PublicGetEndpoint, PublicMutationEndpoint)](
29822949 """Generate real unittest methods because pytest cannot parametrize APITestCase.
29832950
29842951 Each endpoint gets one method that checks every credential as a subtest. A known
2985- service-account gap gets a strict-xfail method per service-account credential so
2986- fixing it on either path still fails.
2952+ service-account gap gets its own strict-xfail method so fixing it still fails.
29872953 """
2988- # The service-account cells enable a feature for the rest of their test, so they run last.
2989- assert (
2990- authentications [- len (SERVICE_ACCOUNT_AUTHENTICATIONS ) :] == SERVICE_ACCOUNT_AUTHENTICATIONS
2991- )
2954+ # The service-account cell enables a feature for the rest of its test, so it runs last.
2955+ assert MatrixAuthentication .SERVICE_ACCOUNT_VIEWER_CONTEXT is authentications [- 1 ]
29922956
29932957 def install (name : str , test : Callable [[AgentTokenPublicGetMatrixTest ], None ]) -> None :
29942958 test .__name__ = name
@@ -3006,23 +2970,20 @@ def test_matrix_row(self: AgentTokenPublicGetMatrixTest) -> None:
30062970
30072971 return test_matrix_row
30082972
3009- def gap_test (
3010- endpoint : E , authentication : MatrixAuthentication
3011- ) -> Callable [[AgentTokenPublicGetMatrixTest ], None ]:
2973+ def gap_test (endpoint : E ) -> Callable [[AgentTokenPublicGetMatrixTest ], None ]:
30122974 def test_gap_cell (self : AgentTokenPublicGetMatrixTest ) -> None :
3013- assert_cell (self , endpoint , authentication )
2975+ assert_cell (self , endpoint , MatrixAuthentication . SERVICE_ACCOUNT_VIEWER_CONTEXT )
30142976
30152977 return _mark_service_account_gap (test_gap_cell , endpoint .test_id )
30162978
30172979 for index , endpoint in enumerate (endpoints ):
30182980 name = f"{ prefix } _{ index :03d} _{ endpoint .test_id } "
30192981 if endpoint .test_id in SERVICE_ACCOUNT_KNOWN_GAPS :
2982+ install (name , row_test (endpoint , authentications [:- 1 ]))
30202983 install (
3021- name ,
3022- row_test (endpoint , authentications [: - len ( SERVICE_ACCOUNT_AUTHENTICATIONS )] ),
2984+ f" { name } _ { MatrixAuthentication . SERVICE_ACCOUNT_VIEWER_CONTEXT . value } " ,
2985+ gap_test (endpoint ),
30232986 )
3024- for authentication in SERVICE_ACCOUNT_AUTHENTICATIONS :
3025- install (f"{ name } _{ authentication .value } " , gap_test (endpoint , authentication ))
30262987 else :
30272988 install (name , row_test (endpoint , authentications ))
30282989
@@ -3095,19 +3056,17 @@ def test_matrix_cell(
30953056
30963057def _install_service_account_user_global_matrix_tests () -> None :
30973058 for index , endpoint in enumerate (USER_GLOBAL_GET_ENDPOINTS ):
3098- for authentication in SERVICE_ACCOUNT_AUTHENTICATIONS :
30993059
3100- def test_matrix_cell (
3101- self : AgentTokenPublicGetMatrixTest ,
3102- endpoint : PublicGetEndpoint = endpoint ,
3103- authentication : MatrixAuthentication = authentication ,
3104- ) -> None :
3105- self ._assert_service_account_user_global_get_denied (endpoint , authentication )
3060+ def test_matrix_cell (
3061+ self : AgentTokenPublicGetMatrixTest ,
3062+ endpoint : PublicGetEndpoint = endpoint ,
3063+ ) -> None :
3064+ self ._assert_service_account_user_global_get_denied (endpoint )
31063065
3107- test_matrix_cell .__name__ = (
3108- f"test_user_global_get_{ index :03d} _{ endpoint .test_id } _ { authentication . value } "
3109- )
3110- setattr (AgentTokenPublicGetMatrixTest , test_matrix_cell .__name__ , test_matrix_cell )
3066+ test_matrix_cell .__name__ = (
3067+ f"test_user_global_get_{ index :03d} _{ endpoint .test_id } _service_account_viewer_context "
3068+ )
3069+ setattr (AgentTokenPublicGetMatrixTest , test_matrix_cell .__name__ , test_matrix_cell )
31113070
31123071
31133072_install_service_account_user_global_matrix_tests ()
0 commit comments