amoghrajesh commented on code in PR #66073: URL: https://github.com/apache/airflow/pull/66073#discussion_r3176674352
########## airflow-core/src/airflow/api_fastapi/execution_api/routes/task_state.py: ########## @@ -0,0 +1,136 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +from __future__ import annotations + +from typing import Annotated +from uuid import UUID + +from cadwyn import VersionedAPIRouter +from fastapi import HTTPException, Path, Query, Security, status +from sqlalchemy.orm import Session + +from airflow._shared.state import TaskScope +from airflow.api_fastapi.common.db.common import SessionDep +from airflow.api_fastapi.execution_api.datamodels.task_state import ( + TaskStatePutBody, + TaskStateResponse, +) +from airflow.api_fastapi.execution_api.security import require_auth +from airflow.models.taskinstance import TaskInstance as TI +from airflow.state import get_state_backend + +router = VersionedAPIRouter( + responses={ + status.HTTP_401_UNAUTHORIZED: {"description": "Unauthorized"}, + status.HTTP_403_FORBIDDEN: {"description": "Access denied"}, + status.HTTP_404_NOT_FOUND: {"description": "Not found"}, + }, + dependencies=[Security(require_auth, scopes=["ti:self"])], Review Comment: Handled in 7bd1f4c64a ########## airflow-core/src/airflow/api_fastapi/execution_api/routes/asset_state.py: ########## @@ -0,0 +1,121 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +""" +Execution API routes for asset state. + +Asset state is keyed by asset *name* (not integer id) in the URL — asset names +are unique, and callers (task SDK accessors) have the name from their Asset +object without needing a DB lookup. The route resolves name → asset_id +internally for the state backend scope. + +Per-task asset registration checks are intentionally not implemented here +(deferred to AIP-93 — see TODO comment below). +""" + +from __future__ import annotations + +from typing import Annotated + +from cadwyn import VersionedAPIRouter +from fastapi import HTTPException, Path, status +from sqlalchemy import select + +from airflow._shared.state import AssetScope +from airflow.api_fastapi.common.db.common import SessionDep +from airflow.api_fastapi.execution_api.datamodels.asset_state import ( + AssetStatePutBody, + AssetStateResponse, +) +from airflow.models.asset import AssetModel +from airflow.state import get_state_backend + +# TODO(AIP-103): enforce that the requesting task is registered with the asset +# (via task_inlet_asset_reference or task_outlet_asset_reference) before +# allowing reads/writes. Currently any task with a valid execution token can +# access any asset's state — the same gap exists in /assets and /asset-events. +# Proper fix is a unified asset-registration check across all asset routes, +# not just here. +router = VersionedAPIRouter( + responses={ + status.HTTP_401_UNAUTHORIZED: {"description": "Unauthorized"}, + status.HTTP_404_NOT_FOUND: {"description": "Not found"}, + }, +) + + +def _resolve_asset_id(name: str, session: SessionDep) -> int: + """Resolve asset name → integer asset_id, 404 if not found.""" + asset_id = session.scalar(select(AssetModel.id).where(AssetModel.name == name, AssetModel.active.has())) + if asset_id is None: + raise HTTPException( + status_code=status.HTTP_404_NOT_FOUND, + detail={"reason": "not_found", "message": f"Asset {name!r} not found"}, + ) + return asset_id + + [email protected]("/{name}/{key}") Review Comment: Handled in 7bd1f4c64a ########## airflow-core/src/airflow/api_fastapi/execution_api/routes/asset_state.py: ########## @@ -0,0 +1,121 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +""" +Execution API routes for asset state. + +Asset state is keyed by asset *name* (not integer id) in the URL — asset names +are unique, and callers (task SDK accessors) have the name from their Asset +object without needing a DB lookup. The route resolves name → asset_id +internally for the state backend scope. + +Per-task asset registration checks are intentionally not implemented here +(deferred to AIP-93 — see TODO comment below). +""" + +from __future__ import annotations + +from typing import Annotated + +from cadwyn import VersionedAPIRouter +from fastapi import HTTPException, Path, status +from sqlalchemy import select + +from airflow._shared.state import AssetScope +from airflow.api_fastapi.common.db.common import SessionDep +from airflow.api_fastapi.execution_api.datamodels.asset_state import ( + AssetStatePutBody, + AssetStateResponse, +) +from airflow.models.asset import AssetModel +from airflow.state import get_state_backend + +# TODO(AIP-103): enforce that the requesting task is registered with the asset Review Comment: Handled in 7bd1f4c64a -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
