Skip to content

Commit 9636fcb

Browse files
authored
Authorize POST /assets/events on the asset named in the body (#73007)
The route dependency reads the asset id from the path, but this endpoint carries it in the request body, so the auth manager was only asked whether the caller may post to any asset at all. An auth manager that scopes assets by id, name, or uri could not deny an event for an asset the caller may not touch, and the response still returned that asset's name and uri.
1 parent c902fea commit 9636fcb

3 files changed

Lines changed: 71 additions & 9 deletions

File tree

‎airflow-core/src/airflow/api_fastapi/core_api/routes/public/assets.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,10 @@ def get_asset_events(
396396
@assets_router.post(
397397
"/assets/events",
398398
responses=create_openapi_http_exception_doc([status.HTTP_404_NOT_FOUND]),
399-
dependencies=[Depends(requires_access_asset(method="POST")), Depends(action_logging())],
399+
dependencies=[
400+
Depends(requires_access_asset(method="POST", asset_id_from_body=True)),
401+
Depends(action_logging()),
402+
],
400403
)
401404
def create_asset_event(
402405
body: CreateAssetEventsBody,

‎airflow-core/src/airflow/api_fastapi/core_api/security.py‎

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
)
5757
from airflow.api_fastapi.common.db.common import SessionDep
5858
from airflow.api_fastapi.core_api.base import OrmClause
59+
from airflow.api_fastapi.core_api.datamodels.assets import CreateAssetEventsBody
5960
from airflow.api_fastapi.core_api.datamodels.common import (
6061
BulkAction,
6162
BulkActionOnExistence,
@@ -1082,19 +1083,40 @@ def _build_asset_details(asset_id: str | None) -> AssetDetails:
10821083
return AssetDetails(id=asset_id, name=name, uri=uri)
10831084

10841085

1085-
def requires_access_asset(method: ResourceMethod) -> Callable[[Request, BaseUser], None]:
1086-
def inner(
1087-
request: Request,
1088-
user: GetUserDep,
1089-
) -> None:
1090-
details = _build_asset_details(request.path_params.get("asset_id"))
1086+
def requires_access_asset(method: ResourceMethod, *, asset_id_from_body: bool = False) -> Callable[..., None]:
1087+
"""
1088+
Authorize the caller on the asset targeted by the request.
1089+
1090+
:param method: the method to perform
1091+
:param asset_id_from_body: read ``asset_id`` from a ``CreateAssetEventsBody`` request body instead of
1092+
the path. The dependency parameter must be named ``body`` to share the route's body.
1093+
"""
1094+
1095+
def _authorize(asset_id: str | None, user: BaseUser) -> None:
1096+
details = _build_asset_details(asset_id)
10911097

10921098
_requires_access(
10931099
is_authorized_callback=lambda: get_auth_manager().is_authorized_asset(
10941100
method=method, details=details, user=user
10951101
),
10961102
)
10971103

1104+
if asset_id_from_body:
1105+
1106+
def inner_from_body(
1107+
body: CreateAssetEventsBody,
1108+
user: GetUserDep,
1109+
) -> None:
1110+
_authorize(str(body.asset_id), user)
1111+
1112+
return inner_from_body
1113+
1114+
def inner(
1115+
request: Request,
1116+
user: GetUserDep,
1117+
) -> None:
1118+
_authorize(request.path_params.get("asset_id"), user)
1119+
10981120
return inner
10991121

11001122

‎airflow-core/tests/unit/api_fastapi/core_api/routes/public/test_assets.py‎

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2029,12 +2029,49 @@ def test_should_respond_200(self, test_client, session):
20292029
}
20302030
check_last_log(session, dag_id=None, event="create_asset_event", logical_date=None)
20312031

2032+
@mock.patch(
2033+
"airflow.api_fastapi.auth.managers.simple.simple_auth_manager.SimpleAuthManager.is_authorized_asset",
2034+
autospec=True,
2035+
)
2036+
def test_should_authorize_on_the_asset_named_in_the_body(
2037+
self, mock_is_authorized_asset, test_client, session
2038+
):
2039+
"""The asset id lives in the body, so the route must resolve it and authorize on the full asset."""
2040+
(asset,) = self.create_assets(num=1, session=session)
2041+
mock_is_authorized_asset.return_value = True
2042+
2043+
response = test_client.post("/assets/events", json={"asset_id": asset.id})
2044+
2045+
assert response.status_code == 200
2046+
mock_is_authorized_asset.assert_called_once_with(
2047+
mock.ANY,
2048+
method="POST",
2049+
details=AssetDetails(id=str(asset.id), name="simple1", uri="s3://bucket/key/1"),
2050+
user=mock.ANY,
2051+
)
2052+
2053+
@mock.patch(
2054+
"airflow.api_fastapi.auth.managers.simple.simple_auth_manager.SimpleAuthManager.is_authorized_asset",
2055+
autospec=True,
2056+
)
2057+
def test_should_respond_403_when_not_authorized_on_the_asset(
2058+
self, mock_is_authorized_asset, test_client, session
2059+
):
2060+
(asset,) = self.create_assets(num=1, session=session)
2061+
mock_is_authorized_asset.return_value = False
2062+
2063+
response = test_client.post("/assets/events", json={"asset_id": asset.id})
2064+
2065+
assert response.status_code == 403
2066+
assert session.scalar(select(func.count()).select_from(AssetEvent)) == 0
2067+
20322068
def test_should_respond_401(self, unauthenticated_test_client):
20332069
response = unauthenticated_test_client.post("/assets/events", json={"asset_uri": "s3://bucket/key/1"})
20342070
assert response.status_code == 401
20352071

2036-
def test_should_respond_403(self, unauthorized_test_client):
2037-
response = unauthorized_test_client.post("/assets/events", json={"asset_uri": "s3://bucket/key/1"})
2072+
def test_should_respond_403(self, unauthorized_test_client, session):
2073+
(asset,) = self.create_assets(num=1, session=session)
2074+
response = unauthorized_test_client.post("/assets/events", json={"asset_id": asset.id})
20382075
assert response.status_code == 403
20392076

20402077
def test_invalid_attr_not_allowed(self, test_client, session):

0 commit comments

Comments
 (0)