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


##########
providers/fab/src/airflow/providers/fab/auth_manager/fab_auth_manager.py:
##########
@@ -310,23 +314,39 @@ def get_user(self) -> User:
     def session(self):
         return self.appbuilder.session
 
-    @cachedmethod(lambda self: self.cache, key=lambda _, token: 
int(token["sub"]))
     def deserialize_user(self, token: dict[str, Any]) -> User:
         user_id = int(token["sub"])
+        # Tokens issued before the claim existed count as the initial 
generation.
+        token_generation = token.get(TOKEN_GENERATION_CLAIM, 0)
+        if type(token_generation) is not int:
+            raise ValueError(f"Token of user with id {user_id} has an invalid 
{TOKEN_GENERATION_CLAIM} claim")
+        user = self._get_active_user(user_id)
+        if token_generation > user.token_generation:
+            # The token was issued after a password change that the cached 
user predates.
+            self.cache.pop(user_id, None)
+            user = self._get_active_user(user_id)
+        if token_generation != user.token_generation:
+            raise ValueError(f"Token of user with id {user_id} was issued 
before their last password change")
+        return user
+
+    @cachedmethod(lambda self: self.cache, key=lambda _, user_id: user_id)

Review Comment:
   `TTLCache` is not thread-safe. `deserialize_user` writes to it on the event 
loop, while the new pops run in other threads: the sync `update_user` route 
runs in the threadpool and `reset_password` runs in the Flask app's WSGI 
thread. Can we pass a lock to `cachedmethod` and use the same lock for the pops?



##########
providers/fab/src/airflow/providers/fab/auth_manager/models/__init__.py:
##########
@@ -276,6 +276,9 @@ class User(Model, BaseUser):
     last_login: Mapped[datetime.datetime | None] = mapped_column(DateTime, 
nullable=True)
     login_count: Mapped[int | None] = mapped_column(Integer, nullable=True)
     fail_login_count: Mapped[int | None] = mapped_column(Integer, 
nullable=True)
+    # Incremented on every password change. Tokens and sessions record the 
value they were issued
+    # under and stop being accepted once it moves on.
+    token_generation: Mapped[int] = mapped_column(Integer, nullable=False, 
default=0, server_default="0")

Review Comment:
   Every `User` query now reads this column, so anyone who upgrades the FAB 
provider without running `airflow db migrate` can't log in anymore 
(`ab_user.token_generation` does not exist). Can we add a note in 
`providers/fab/docs/changelog.rst`?



##########
providers/fab/src/airflow/providers/fab/auth_manager/security_manager/override.py:
##########
@@ -626,9 +647,12 @@ def reset_password(self, userid: int, password: str) -> 
bool:
         user = self.get_user_by_id(userid)
         if not user:
             return False
-        user.password = self._hash_password(password)
+        self.set_user_password(user, password)
         self.reset_user_sessions(user)
-        return self.update_user(user)
+        if not self.update_user(user):
+            return False
+        get_fab_auth_manager().cache.pop(user.id, None)

Review Comment:
   Could we do this pop in `update_user` instead, right after the commit? It 
would cover both call sites, and role changes made through the users API would 
stop being served from the cache too.



##########
providers/fab/src/airflow/providers/fab/auth_manager/security_manager/override.py:
##########
@@ -626,9 +647,12 @@ def reset_password(self, userid: int, password: str) -> 
bool:
         user = self.get_user_by_id(userid)
         if not user:
             return False
-        user.password = self._hash_password(password)
+        self.set_user_password(user, password)
         self.reset_user_sessions(user)

Review Comment:
   nit: the `securecookie` warning flashed by `reset_user_sessions` is no 
longer true, `load_session_user` now rejects the old sessions. Can we update it 
so admins don't rotate `secret_key` for nothing?



##########
providers/fab/src/airflow/providers/fab/auth_manager/fab_auth_manager.py:
##########
@@ -310,23 +314,39 @@ def get_user(self) -> User:
     def session(self):
         return self.appbuilder.session
 
-    @cachedmethod(lambda self: self.cache, key=lambda _, token: 
int(token["sub"]))
     def deserialize_user(self, token: dict[str, Any]) -> User:
         user_id = int(token["sub"])
+        # Tokens issued before the claim existed count as the initial 
generation.
+        token_generation = token.get(TOKEN_GENERATION_CLAIM, 0)
+        if type(token_generation) is not int:
+            raise ValueError(f"Token of user with id {user_id} has an invalid 
{TOKEN_GENERATION_CLAIM} claim")
+        user = self._get_active_user(user_id)
+        if token_generation > user.token_generation:
+            # The token was issued after a password change that the cached 
user predates.
+            self.cache.pop(user_id, None)
+            user = self._get_active_user(user_id)
+        if token_generation != user.token_generation:
+            raise ValueError(f"Token of user with id {user_id} was issued 
before their last password change")
+        return user
+
+    @cachedmethod(lambda self: self.cache, key=lambda _, user_id: user_id)
+    def _get_active_user(self, user_id: int) -> User:
+        return self._fetch_active_user(user_id)
 
+    def _fetch_active_user(self, user_id: int) -> User:

Review Comment:
   nit: is `_fetch_active_user` only there for the test spy? The tests could 
count the reads with `assert_queries_count` (already used in 
`test_fab_auth_manager.py`) and keep a single method.



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