Skip to content

Commit edcd4a5

Browse files
authored
Hide assets the caller may not read from the UI dependencies graph (#73008)
* Hide assets the caller may not read from the UI dependencies graph The dependencies graph scoped its nodes by which Dags the caller may read, but never by which assets. A caller who can read a Dag saw every asset that Dag produces or consumes, name included, even when the auth manager scopes that asset away from them; the data graph would also expand outward through such an asset. Asset names commonly encode bucket, table or dataset names, so this is the same class of gap the rest of the asset API closed. * Hide an unenriched asset leaf from the data dependencies graph An asset_expression leaf whose id the Dag processor has not filled in yet cannot be authorized, so it must be hidden like any other asset the caller may not read. The data graph instead parsed the id out of the node, which failed the request outright rather than dropping the node.
1 parent a27cd33 commit edcd4a5

3 files changed

Lines changed: 262 additions & 21 deletions

File tree

‎airflow-core/src/airflow/api_fastapi/core_api/routes/ui/dependencies.py‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@
2727
from airflow.api_fastapi.common.router import AirflowRouter
2828
from airflow.api_fastapi.core_api.datamodels.ui.common import BaseGraphResponse
2929
from airflow.api_fastapi.core_api.openapi.exceptions import create_openapi_http_exception_doc
30-
from airflow.api_fastapi.core_api.security import ReadableDagsFilterDep, requires_access_dag
30+
from airflow.api_fastapi.core_api.security import (
31+
ReadableAssetsFilterDep,
32+
ReadableDagsFilterDep,
33+
requires_access_dag,
34+
)
3135
from airflow.api_fastapi.core_api.services.ui.dependencies import (
3236
extract_single_connected_component,
3337
get_data_dependencies,
@@ -50,6 +54,7 @@
5054
def get_dependencies(
5155
session: SessionDep,
5256
readable_dags_filter: ReadableDagsFilterDep,
57+
readable_assets_filter: ReadableAssetsFilterDep,
5358
node_id: str | None = None,
5459
dependency_type: Literal["scheduling", "data"] = "scheduling",
5560
) -> BaseGraphResponse:
@@ -65,12 +70,21 @@ def get_dependencies(
6570
except ValueError:
6671
raise HTTPException(status.HTTP_400_BAD_REQUEST, f"Invalid asset node_id: {node_id}")
6772

68-
data = get_data_dependencies(asset_id, session, readable_dags_filter.value)
73+
data = get_data_dependencies(
74+
asset_id,
75+
session,
76+
readable_dag_ids=readable_dags_filter.value,
77+
readable_asset_ids=readable_assets_filter.value or set(),
78+
)
6979
if not data["nodes"]:
7080
raise HTTPException(status.HTTP_404_NOT_FOUND, f"Asset with id {asset_id} was not found")
7181
return BaseGraphResponse(**data)
7282

73-
data = get_scheduling_dependencies(readable_dags_filter.value, session)
83+
data = get_scheduling_dependencies(
84+
session,
85+
readable_dag_ids=readable_dags_filter.value,
86+
readable_asset_ids=readable_assets_filter.value or set(),
87+
)
7488

7589
if node_id is not None:
7690
try:

‎airflow-core/src/airflow/api_fastapi/core_api/services/ui/dependencies.py‎

Lines changed: 62 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ def _asset_node_id_and_label(asset: dict) -> tuple[str, str]:
3939
asset_type = asset["type"]
4040

4141
if asset_type == "asset":
42-
return f"asset:{asset['id']}", asset["name"]
42+
# A leaf the Dag processor has not enriched yet has no id; hide_unreadable_assets drops it.
43+
return f"asset:{asset.get('id')}", asset["name"]
4344
if asset_type in ("asset-alias", "asset-name-ref"):
4445
return f"{asset_type}:{asset['name']}", asset["name"]
4546
if asset_type == "asset-uri-ref":
@@ -189,7 +190,41 @@ def extract_single_connected_component(
189190
return {"nodes": nodes, "edges": edges}
190191

191192

192-
def get_scheduling_dependencies(readable_dag_ids: set[str] | None, session: Session) -> dict[str, list[dict]]:
193+
def _is_readable_asset_node(node_id: str, readable_asset_ids: set[int]) -> bool:
194+
suffix = node_id.removeprefix("asset:")
195+
return suffix.isdigit() and int(suffix) in readable_asset_ids
196+
197+
198+
def hide_unreadable_assets(
199+
graph: dict[str, list[dict]], readable_asset_ids: set[int]
200+
) -> dict[str, list[dict]]:
201+
"""
202+
Drop asset nodes the caller may not read, and every edge touching them, from a graph.
203+
204+
Alias and ref nodes carry no asset id to authorize on, so they are left in place. An asset node
205+
without a numeric id, from an asset_expression the Dag processor has not re-enriched yet, cannot
206+
be authorized and is hidden as well.
207+
"""
208+
hidden = {
209+
node["id"]
210+
for node in graph["nodes"]
211+
if node["type"] == "asset" and not _is_readable_asset_node(node["id"], readable_asset_ids)
212+
}
213+
if not hidden:
214+
return graph
215+
return {
216+
"nodes": [node for node in graph["nodes"] if node["id"] not in hidden],
217+
"edges": [
218+
edge
219+
for edge in graph["edges"]
220+
if edge["source_id"] not in hidden and edge["target_id"] not in hidden
221+
],
222+
}
223+
224+
225+
def get_scheduling_dependencies(
226+
session: Session, *, readable_dag_ids: set[str] | None, readable_asset_ids: set[int]
227+
) -> dict[str, list[dict]]:
193228
"""Get scheduling dependencies between Dags."""
194229
from airflow.models.serialized_dag import SerializedDagModel
195230

@@ -307,10 +342,13 @@ def get_scheduling_dependencies(readable_dag_ids: set[str] | None, session: Sess
307342
if team_name:
308343
node["team"] = team_name
309344

310-
return {
311-
"nodes": list(nodes_dict.values()),
312-
"edges": [{"source_id": source, "target_id": target} for source, target in sorted(edge_tuples)],
313-
}
345+
return hide_unreadable_assets(
346+
{
347+
"nodes": list(nodes_dict.values()),
348+
"edges": [{"source_id": source, "target_id": target} for source, target in sorted(edge_tuples)],
349+
},
350+
readable_asset_ids,
351+
)
314352

315353

316354
def _entry_task_id_from_serialized_data(data: dict | None) -> str | None:
@@ -391,7 +429,11 @@ def _get_dag_entry_points(dag_ids: set[str], session: Session) -> dict[str, tupl
391429

392430

393431
def get_data_dependencies(
394-
asset_id: int, session: Session, readable_dag_ids: set[str] | None = None
432+
asset_id: int,
433+
session: Session,
434+
*,
435+
readable_dag_ids: set[str] | None = None,
436+
readable_asset_ids: set[int],
395437
) -> dict[str, list[dict]]:
396438
"""Get full task dependencies for an asset."""
397439
from sqlalchemy import select, tuple_, union_all
@@ -411,6 +453,10 @@ def get_data_dependencies(
411453
# reachable through other readable dags) even though the user has no legitimate
412454
# lineage connection to it. A readable_dag_ids value of None means no filter is
413455
# applied (the user has unrestricted dag read access).
456+
# Same empty result as an unrelated asset, so an unreadable asset id is not distinguishable
457+
# from a nonexistent one.
458+
if asset_id not in readable_asset_ids:
459+
return {"nodes": [], "edges": []}
414460
if readable_dag_ids is not None:
415461
connected_dag_ids_query = union_all(
416462
select(TaskOutletAssetReference.dag_id).where(TaskOutletAssetReference.asset_id == asset_id),
@@ -442,6 +488,7 @@ def get_data_dependencies(
442488
while frontier:
443489
round_asset_ids = frontier - processed_assets
444490
processed_assets |= round_asset_ids
491+
round_asset_ids &= readable_asset_ids
445492
if not round_asset_ids:
446493
break
447494

@@ -572,7 +619,7 @@ def get_data_dependencies(
572619
nodes_dict.setdefault(node["id"], node)
573620
# Continue the BFS through any other concrete assets this gate combines
574621
# with, so their own producers are traced too.
575-
if node["id"].startswith("asset:"):
622+
if _is_readable_asset_node(node["id"], readable_asset_ids):
576623
sibling_asset_id = int(node["id"].removeprefix("asset:"))
577624
if sibling_asset_id not in processed_assets:
578625
next_frontier.add(sibling_asset_id)
@@ -661,7 +708,10 @@ def get_data_dependencies(
661708
if team_name:
662709
node["team"] = team_name
663710

664-
return {
665-
"nodes": list(nodes_dict.values()),
666-
"edges": [{"source_id": source, "target_id": target} for source, target in edge_set],
667-
}
711+
return hide_unreadable_assets(
712+
{
713+
"nodes": list(nodes_dict.values()),
714+
"edges": [{"source_id": source, "target_id": target} for source, target in edge_set],
715+
},
716+
readable_asset_ids,
717+
)

0 commit comments

Comments
 (0)