vincbeck commented on code in PR #76748:
URL: https://github.com/apache/airflow/pull/76748#discussion_r4601918060


##########
providers/fab/src/airflow/providers/fab/auth_manager/fab_auth_manager.py:
##########
@@ -345,6 +348,27 @@ def _fetch_user() -> User:
                     self.session.remove()
                 raise
 
+    async def deserialize_user_async(self, token: dict[str, Any]) -> User:

Review Comment:
   This is a breaking change for `FabAuthManager` subclasses that override 
`deserialize_user`. Our docs (`auth-manager/token.rst`) tell users to subclass 
`FabAuthManager`, and `get_user_from_token` now calls `deserialize_user_async`, 
which goes straight to the DB. So a subclass that adds its own checks in 
`deserialize_user` silently loses them on every API request. Any idea how we 
could keep these overrides working?



##########
airflow-core/src/airflow/api_fastapi/auth/managers/base_auth_manager.py:
##########
@@ -147,6 +147,10 @@ def init(self) -> None:
     def deserialize_user(self, token: dict[str, Any]) -> T:
         """Create a user object from dict."""
 
+    async def deserialize_user_async(self, token: dict[str, Any]) -> T:

Review Comment:
   Should we mention `deserialize_user_async` in 
`airflow-core/docs/core-concepts/auth-manager/index.rst` as well? Auth managers 
doing I/O in `deserialize_user` would want to override it.



##########
providers/fab/src/airflow/providers/fab/auth_manager/fab_auth_manager.py:
##########
@@ -345,6 +348,27 @@ def _fetch_user() -> User:
                     self.session.remove()
                 raise
 
+    async def deserialize_user_async(self, token: dict[str, Any]) -> User:
+        """Deserialize a user for FastAPI JWT authentication without blocking 
its event loop."""
+        user_id = int(token["sub"])
+        try:
+            return self.cache[user_id]
+        except KeyError:
+            pass
+
+        async def _fetch_user() -> User:
+            async with create_session_async() as session:
+                user = (await session.scalars(select(User).where(User.id == 
user_id))).one_or_none()
+                return self._validate_user(user, token["sub"])
+
+        try:
+            user = await _fetch_user()
+        except SQLAlchemyError:

Review Comment:
   Is the retry needed here? In the sync version it is there because the scoped 
session can be poisoned, but `create_session_async` gives a fresh session every 
time. It also doubles the wait when the pool is exhausted, since the pool 
`TimeoutError` is a `SQLAlchemyError`.



-- 
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]

Reply via email to