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


##########
providers/fab/src/airflow/providers/fab/auth_manager/fab_auth_manager.py:
##########
@@ -345,6 +348,29 @@ 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."""
+        # Preserve legacy synchronous overrides while the default FAB 
implementation uses async lookup below.
+        deserialize_user_owner = next(cls for cls in type(self).__mro__ if 
"deserialize_user" in cls.__dict__)
+        if deserialize_user_owner is not FabAuthManager:
+            return self.deserialize_user(token)
+
+        user_id = int(token["sub"])

Review Comment:
   Concurrent cache misses for the same user now each run their own query. 
Before, the blocking call serialized them, so only the first one hit the DB. 
With the 30s TTL, a UI page load that fires ~20 API calls can run ~20 queries 
at once every 30 seconds per user, taking a good part of the async pool. Is it 
worth guarding the fetch with a per-user lock?



##########
providers/fab/src/airflow/providers/fab/auth_manager/fab_auth_manager.py:
##########
@@ -345,6 +348,29 @@ 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."""
+        # Preserve legacy synchronous overrides while the default FAB 
implementation uses async lookup below.
+        deserialize_user_owner = next(cls for cls in type(self).__mro__ if 
"deserialize_user" in cls.__dict__)
+        if deserialize_user_owner is not FabAuthManager:
+            return self.deserialize_user(token)
+
+        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"])
+
+        user = await _fetch_user()

Review Comment:
   nit: no need for the nested function, it is only called once
   
   ```suggestion
           async with create_session_async() as session:
               user = (await session.scalars(select(User).where(User.id == 
user_id))).one_or_none()
               user = self._validate_user(user, token["sub"])
   ```



##########
providers/fab/src/airflow/providers/fab/auth_manager/fab_auth_manager.py:
##########
@@ -345,6 +348,29 @@ 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."""
+        # Preserve legacy synchronous overrides while the default FAB 
implementation uses async lookup below.
+        deserialize_user_owner = next(cls for cls in type(self).__mro__ if 
"deserialize_user" in cls.__dict__)
+        if deserialize_user_owner is not FabAuthManager:

Review Comment:
   nit: simpler, and it avoids walking the MRO on every request
   
   ```suggestion
           if type(self).deserialize_user is not 
FabAuthManager.deserialize_user:
   ```



##########
airflow-core/tests/unit/api_fastapi/auth/managers/test_base_auth_manager.py:
##########
@@ -301,20 +301,22 @@ def test_get_authorized_menu_items(self, 
mock_filter_authorized_menu_items, auth
         
"airflow.api_fastapi.auth.managers.base_auth_manager.BaseAuthManager._get_token_validator",
         autospec=True,
     )
-    @patch.object(EmptyAuthManager, "deserialize_user")
+    @patch.object(EmptyAuthManager, "deserialize_user_async")

Review Comment:
   nit: I would keep patching `deserialize_user` here. The test would still 
pass and it would also cover the default `deserialize_user_async`.



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