Skip to content

Commit 800b7e0

Browse files
fix(authz): check authorization through gateway
1 parent c62cff8 commit 800b7e0

2 files changed

Lines changed: 8 additions & 73 deletions

File tree

agentex/src/domain/services/authorization_service.py

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from src.adapters.authorization.adapter_agentex_authz_proxy import (
77
DAgentexAuthorization,
88
)
9-
from src.api.authentication_cache import get_auth_cache
109
from src.api.authentication_middleware import DAuthorizationEnabled
1110
from src.api.schemas.authorization_types import (
1211
AgentexResource,
@@ -107,29 +106,6 @@ async def check(
107106
else self.principal_context
108107
)
109108

110-
use_authorization_cache = resource.type == AgentexResourceType.agent
111-
cached_result = None
112-
if use_authorization_cache:
113-
# Try to get cached result first
114-
auth_cache = await get_auth_cache()
115-
cached_result = await auth_cache.get_authorization_check(
116-
resource_type=str(resource.type),
117-
resource_selector=resource.selector,
118-
operation=str(operation),
119-
principal_context=effective_principal,
120-
)
121-
122-
if cached_result is not None:
123-
logger.info(
124-
"[authorization_service] Using cached result for %s permission on %s:%s: %s",
125-
operation,
126-
resource.type,
127-
resource.selector,
128-
"allowed" if cached_result else "denied",
129-
)
130-
return cached_result
131-
132-
# Not in cache, perform actual check
133109
logger.info(
134110
"[authorization_service] Checking %s permission on %s:%s",
135111
operation,
@@ -142,16 +118,6 @@ async def check(
142118
operation,
143119
)
144120

145-
if use_authorization_cache:
146-
# Cache the result
147-
await auth_cache.set_authorization_check(
148-
resource_type=str(resource.type),
149-
resource_selector=resource.selector,
150-
operation=str(operation),
151-
principal_context=effective_principal,
152-
allowed=result,
153-
)
154-
155121
logger.info(
156122
f"Authorization check for {operation} on {resource.type}:{resource.selector}: {'allowed' if result else 'denied'}"
157123
)

agentex/tests/unit/services/test_authorization_service_cache.py

Lines changed: 8 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from unittest.mock import AsyncMock
33

44
import pytest
5-
from src.api.authentication_cache import reset_auth_cache
65
from src.api.schemas.authorization_types import AgentexResource, AuthorizedOperationType
76
from src.domain.services.authorization_service import AuthorizationService
87

@@ -24,53 +23,23 @@ def _service(principal_context, gateway):
2423
)
2524

2625

27-
@pytest.mark.unit
28-
@pytest.mark.asyncio
29-
async def test_agent_authorization_check_uses_cache():
30-
await reset_auth_cache()
31-
try:
32-
gateway = AsyncMock()
33-
gateway.check.return_value = True
34-
service = _service({"user_id": "user-1", "account_id": "acct-1"}, gateway)
35-
36-
assert (
37-
await service.check(
38-
AgentexResource.agent("agent-1"), AuthorizedOperationType.read
39-
)
40-
is True
41-
)
42-
assert (
43-
await service.check(
44-
AgentexResource.agent("agent-1"), AuthorizedOperationType.read
45-
)
46-
is True
47-
)
48-
49-
assert gateway.check.await_count == 1
50-
finally:
51-
await reset_auth_cache()
52-
53-
5426
@pytest.mark.unit
5527
@pytest.mark.asyncio
5628
@pytest.mark.parametrize(
5729
"resource",
5830
[
31+
AgentexResource.agent("agent-1"),
5932
AgentexResource.task("task-1"),
6033
AgentexResource.api_key("api-key-1"),
6134
AgentexResource.schedule("agent-1/schedule-1"),
6235
],
6336
)
64-
async def test_subresource_authorization_checks_call_gateway_each_time(resource):
65-
await reset_auth_cache()
66-
try:
67-
gateway = AsyncMock()
68-
gateway.check.return_value = True
69-
service = _service({"user_id": "user-1", "account_id": "acct-1"}, gateway)
37+
async def test_authorization_checks_call_gateway_each_time(resource):
38+
gateway = AsyncMock()
39+
gateway.check.return_value = True
40+
service = _service({"user_id": "user-1", "account_id": "acct-1"}, gateway)
7041

71-
assert await service.check(resource, AuthorizedOperationType.read) is True
72-
assert await service.check(resource, AuthorizedOperationType.read) is True
42+
assert await service.check(resource, AuthorizedOperationType.read) is True
43+
assert await service.check(resource, AuthorizedOperationType.read) is True
7344

74-
assert gateway.check.await_count == 2
75-
finally:
76-
await reset_auth_cache()
45+
assert gateway.check.await_count == 2

0 commit comments

Comments
 (0)