Skip to content

Commit 773b135

Browse files
authored
Merge pull request #209 from permitio/omer/per-11075-fixes-after-all-merge
Fix schema and rebase issues
2 parents a108d62 + efe903b commit 773b135

File tree

4 files changed

+68
-18
lines changed

4 files changed

+68
-18
lines changed

horizon/enforcer/api.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from fastapi import APIRouter, Depends, Header
88
from fastapi import HTTPException
99
from fastapi import Request, Response, status
10+
from fastapi.encoders import jsonable_encoder
1011
from opal_client.config import opal_client_config
1112
from opal_client.logger import logger
1213
from opal_client.policy_store.base_policy_store_client import BasePolicyStoreClient
@@ -307,7 +308,7 @@ async def conditional_is_allowed(
307308

308309

309310
async def _is_allowed_data_manager(
310-
query: BaseSchema | None,
311+
query: BaseSchema | list[BaseSchema] | None,
311312
request: Request,
312313
*,
313314
path: str = "/check",
@@ -316,9 +317,10 @@ async def _is_allowed_data_manager(
316317
):
317318
headers = transform_headers(request)
318319
url = f"{sidecar_config.DATA_MANAGER_SERVICE_URL}/v1/authz{path}"
319-
payload = None if query is None else {"input": query.dict()}
320+
payload = None if query is None else {"input": jsonable_encoder(query)}
320321
exc = None
321-
_set_use_debugger(payload)
322+
if query is not None and isinstance(query, dict):
323+
_set_use_debugger(payload)
322324
try:
323325
logger.info(f"calling Data Manager at '{url}' with input: {payload}")
324326
async with aiohttp.ClientSession() as session:
@@ -603,25 +605,19 @@ async def is_allowed_bulk(
603605
queries: list[AuthorizationQuery],
604606
x_permit_sdk_language: Optional[str] = Depends(notify_seen_sdk),
605607
):
606-
bulk_query = BulkAuthorizationQuery(checks=queries)
607608
if sidecar_config.ENABLE_EXTERNAL_DATA_MANAGER:
608609
response = await _is_allowed_data_manager(
609-
bulk_query, request, path="/check/bulk"
610+
queries, request, path="/check/bulk"
610611
)
611612
raw_result = json.loads(response.body)
612-
log_query_result(bulk_query, response, is_inner=True)
613613
else:
614+
bulk_query = BulkAuthorizationQuery(checks=queries)
614615
response = await _is_allowed(bulk_query, request, BULK_POLICY_PACKAGE)
615-
raw_result = json.loads(response.body).get("result", {})
616+
raw_result = json.loads(response.body).get("result", {}).get("allow", [])
616617
log_query_result(bulk_query, response)
617618
try:
618-
processed_query = (
619-
get_v1_processed_query(raw_result)
620-
or get_v2_processed_query(raw_result)
621-
or {}
622-
)
623619
result = BulkAuthorizationResult(
624-
allow=raw_result.get("allow", []),
620+
allow=raw_result,
625621
)
626622
except Exception:
627623
result = BulkAuthorizationResult(

horizon/local/api.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
ListRoleAssignmentsPDPBody,
2121
WrappedResponse,
2222
ListRoleAssignmentsPagination,
23+
RoleAssignmentFactDBFact,
2324
)
2425

2526

@@ -170,7 +171,10 @@ async def legacy_list_role_assignments() -> list[RoleAssignment]:
170171
return await legacy_list_role_assignments()
171172
else:
172173
res = await policy_store.list_facts_by_type("role_assignments")
173-
return parse_obj_as(list[RoleAssignment], await res.json())
174+
res_json = parse_obj_as(
175+
list[RoleAssignmentFactDBFact], await res.json()
176+
)
177+
return [fact.into_role_assignment() for fact in res_json]
174178
else:
175179
return await legacy_list_role_assignments()
176180

horizon/local/schemas.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,49 @@ class Config:
8484

8585
class WrappedResponse(BaseSchema):
8686
result: list[RoleAssignment]
87+
88+
89+
class FactDBFact(BaseSchema):
90+
type: str
91+
attributes: dict[str, Any]
92+
93+
94+
class RoleAssignmentFactDBFact(FactDBFact):
95+
@property
96+
def user(self) -> str:
97+
return self.attributes.get("actor", "").removeprefix("user:")
98+
99+
@property
100+
def role(self) -> str:
101+
return self.attributes.get("role", "")
102+
103+
@property
104+
def tenant(self) -> str:
105+
return self.attributes.get("tenant", "")
106+
107+
@property
108+
def resource_instance(self) -> str:
109+
return self.attributes.get("resource", "")
110+
111+
def into_role_assignment(self) -> RoleAssignment:
112+
return RoleAssignment(
113+
user=self.user,
114+
role=self.role,
115+
tenant=self.tenant,
116+
resource_instance=self.resource_instance,
117+
)
118+
119+
class Config:
120+
schema_extra = {
121+
"example": {
122+
"type": "role_assignments",
123+
"attributes": {
124+
"actor": "user:author-user",
125+
"id": "user:author-user-author-document:doc-1",
126+
"last_modified": "2024-09-23 09:10:10 +0000 UTC",
127+
"resource": "document:doc-1",
128+
"role": "author",
129+
"tenant": "default",
130+
},
131+
}
132+
}

horizon/tests/test_local_api.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,14 @@ async def test_list_role_assignments_external_data_store() -> None:
142142
repeat=True,
143143
payload=[
144144
{
145-
"user": "user1",
146-
"role": "role1",
147-
"tenant": "tenant1",
148-
"resource_instance": "resource_instance1",
145+
"type": "role_assignment",
146+
"attributes": {
147+
"actor": "user:user1",
148+
"role": "role1",
149+
"tenant": "tenant1",
150+
"resource": "resource_instance1",
151+
"id": "user:user1-role1-resource_instance1",
152+
},
149153
}
150154
],
151155
)

0 commit comments

Comments
 (0)