fat-catTW commented on code in PR #76925:
URL: https://github.com/apache/airflow/pull/76925#discussion_r4579168818
##########
airflow-core/src/airflow/api_fastapi/core_api/security.py:
##########
@@ -439,6 +439,26 @@ def depends_permitted_dags_filter(
return depends_permitted_dags_filter
+def permitted_dag_filter_async_factory(
+ method: ResourceMethod, filter_class=PermittedDagFilter
+) -> Callable[[BaseUser, BaseAuthManager], Coroutine[Any, Any,
PermittedDagFilter]]:
+ """
+ Create an async callable for Depends in FastAPI that returns a filter of
the permitted dags for the user.
+
+ :param method: whether filter readable or writable.
+ :return: The async callable that can be used as Depends in FastAPI.
+ """
+
+ async def depends_permitted_dags_filter(
+ user: GetUserDep,
+ auth_manager: AuthManagerDep,
+ ) -> PermittedDagFilter:
+ authorized_dags: set[str] = await
auth_manager.get_authorized_dag_ids_async(user=user, method=method)
Review Comment:
Could we keep existing auth-manager overrides compatible here?
A custom manager may override the documented `get_authorized_dag_ids()`
extension point without implementing the new `get_authorized_dag_ids_async()`,
so this call would fall back to `BaseAuthManager`'s generic logic and could
change `/dagTags` authorization.
Please add a backward-compatible fallback for an existing synchronous
override, while keeping it off the event loop.
##########
airflow-core/src/airflow/api_fastapi/auth/managers/base_auth_manager.py:
##########
@@ -741,22 +730,67 @@ def get_authorized_dag_ids(
isouter=True,
)
)
- # The below type annotation is acceptable on SQLA2.1, but not on 2.0
- rows: Sequence[Row[*tuple[str, str]]] = session.execute(stmt).all() #
type: ignore[type-arg]
+
+ def _filter_authorized_dags_by_team(
+ self,
+ *,
+ rows: Sequence[Row[*tuple[str, str]]], # type: ignore[type-arg]
+ user: T,
+ method: ResourceMethod = "GET",
+ ) -> set[str]:
dags_by_team: dict[str, set[str]] = defaultdict(set)
for dag_id, team_name in rows:
dags_by_team[team_name].add(dag_id)
-
dag_ids: set[str] = set()
for team_name, team_dag_ids in dags_by_team.items():
dag_ids.update(
self.filter_authorized_dag_ids(
dag_ids=team_dag_ids, user=user, method=method,
team_name=team_name
)
)
-
return dag_ids
+ @provide_session
+ def get_authorized_dag_ids(
+ self,
+ *,
+ user: T,
+ method: ResourceMethod = "GET",
+ session: Session = NEW_SESSION,
+ ) -> set[str]:
+ """
+ Get Dags the user has access to.
+
+ :param user: the user
+ :param method: the method to filter on
+ :param session: the session
+ """
+ stmt = self._build_dag_and_team_name_stmt()
+ rows: Sequence[Row[*tuple[str, str]]] = session.execute(stmt).all() #
type: ignore[type-arg]
+ return self._filter_authorized_dags_by_team(rows=rows, user=user,
method=method)
+
+ async def get_authorized_dag_ids_async(
+ self,
+ *,
+ user: T,
+ method: ResourceMethod = "GET",
+ session: AsyncSession | None = None,
+ ) -> set[str]:
+ """
+ Get Dags the user has access to asynchronously.
+
+ :param user: the user
+ :param method: the method to filter on
+ :param session: the async session
+ """
+ stmt = self._build_dag_and_team_name_stmt()
+ if session is not None:
+ rows: Sequence[Row[*tuple[str, str]]] = (await
session.execute(stmt)).all() # type: ignore[type-arg]
+ else:
+ async with create_session_async() as async_session:
+ rows = (await async_session.execute(stmt)).all()
+ return self._filter_authorized_dags_by_team(rows=rows, user=user,
method=method)
Review Comment:
I think this still calls `filter_authorized_dag_ids()` synchronously through
`_filter_authorized_dags_by_team()`.
The base implementation waits for a `ThreadPoolExecutor`, and provider
overrides can perform synchronous authorization I/O, so this runs on the
FastAPI event loop despite the async database query.
Could we also await an async filter hook here, or offload the existing
synchronous filter from the event loop?
--
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]