You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "potiuk (via GitHub)" <gi...@apache.org> on 2023/08/12 11:50:06 UTC

[GitHub] [airflow] potiuk opened a new pull request, #33347: Remove user sessions when resetting password

potiuk opened a new pull request, #33347:
URL: https://github.com/apache/airflow/pull/33347

   When user's password is reset, we also remove all DB sessions for that user.
   
   <!--
    Licensed to the Apache Software Foundation (ASF) under one
    or more contributor license agreements.  See the NOTICE file
    distributed with this work for additional information
    regarding copyright ownership.  The ASF licenses this file
    to you under the Apache License, Version 2.0 (the
    "License"); you may not use this file except in compliance
    with the License.  You may obtain a copy of the License at
   
      http://www.apache.org/licenses/LICENSE-2.0
   
    Unless required by applicable law or agreed to in writing,
    software distributed under the License is distributed on an
    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    KIND, either express or implied.  See the License for the
    specific language governing permissions and limitations
    under the License.
    -->
   
   <!--
   Thank you for contributing! Please make sure that your code changes
   are covered with tests. And in case of new features or big changes
   remember to adjust the documentation.
   
   Feel free to ping committers for the review!
   
   In case of an existing issue, reference it using one of the following:
   
   closes: #ISSUE
   related: #ISSUE
   
   How to write a good git commit message:
   http://chris.beams.io/posts/git-commit/
   -->
   
   
   
   <!-- Please keep an empty line above the dashes. -->
   ---
   **^ Add meaningful description above**
   Read the **[Pull Request Guidelines](https://github.com/apache/airflow/blob/main/CONTRIBUTING.rst#pull-request-guidelines)** for more information.
   In case of fundamental code changes, an Airflow Improvement Proposal ([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals)) is needed.
   In case of a new dependency, check compliance with the [ASF 3rd Party License Policy](https://www.apache.org/legal/resolved.html#category-x).
   In case of backwards incompatible changes please leave a note in a newsfragment file, named `{pr_number}.significant.rst` or `{issue_number}.significant.rst`, in [newsfragments](https://github.com/apache/airflow/tree/main/newsfragments).
   


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [airflow] potiuk merged pull request #33347: Remove user sessions when resetting password

Posted by "potiuk (via GitHub)" <gi...@apache.org>.
potiuk merged PR #33347:
URL: https://github.com/apache/airflow/pull/33347


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [airflow] potiuk commented on pull request #33347: Remove user sessions when resetting password

Posted by "potiuk (via GitHub)" <gi...@apache.org>.
potiuk commented on PR #33347:
URL: https://github.com/apache/airflow/pull/33347#issuecomment-1676393213

   Few screenshots:
   
   * Case for securecookie:
   
   <img width="1333" alt="Screenshot 2023-08-13 at 17 23 06" src="https://github.com/apache/airflow/assets/595491/2d3030e1-def9-4961-b07d-c85450c5d13e">
   
   * Huge number of rows (8!)
   
   <img width="1266" alt="Screenshot 2023-08-13 at 17 25 40" src="https://github.com/apache/airflow/assets/595491/3f0bf960-1fd8-42e4-8f3d-b01ee7f0268b">
   
   I already added the missing space BTW.
   
   * The `airflow db clean` command
   
   <img width="1012" alt="Screenshot 2023-08-13 at 17 28 24" src="https://github.com/apache/airflow/assets/595491/4300bb84-b727-4c5b-8185-cecdd1872ded">
   
   


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [airflow] hussein-awala commented on a diff in pull request #33347: Remove user sessions when resetting password

Posted by "hussein-awala (via GitHub)" <gi...@apache.org>.
hussein-awala commented on code in PR #33347:
URL: https://github.com/apache/airflow/pull/33347#discussion_r1292833103


##########
airflow/utils/db_cleanup.py:
##########
@@ -115,6 +116,9 @@ def readable_config(self):
     _TableConfig(table_name="celery_tasksetmeta", recency_column_name="date_done"),
 ]
 
+if conf.get("webserver", "session_backend") == "database":
+    config_list.append(_TableConfig(table_name="session", recency_column_name="expiry"))
+

Review Comment:
   We could improve the cleanup of this table later in a separate PR, where keeping some expired session is not useful for the user, and in the current implementation, the cleanup command will delete the session expired before `clean_before_timestamp` and not all the expired session if the user use the same timestamp for all the tables. Maybe a new doc for the recommendations could be suffisant.



##########
airflow/auth/managers/fab/security_manager/override.py:
##########
@@ -230,8 +243,51 @@ def reset_password(self, userid, password):
         """
         user = self.get_user_by_id(userid)
         user.password = generate_password_hash(password)
+        self.reset_user_sessions(user)
         self.update_user(user)
 
+    def reset_user_sessions(self, user: User) -> None:
+        if isinstance(self.appbuilder.get_app.session_interface, AirflowDatabaseSessionInterface):
+            interface = self.appbuilder.get_app.session_interface
+            session = interface.db.session
+            user_session_model = interface.sql_session_model
+            num_sessions = session.query(user_session_model).count()
+            if num_sessions > MAX_NUM_DATABASE_USER_SESSIONS:
+                flash(
+                    Markup(
+                        f"The old sessions for user {user.username} have <b>NOT</b> been deleted!<br>"
+                        f"You have a lot ({num_sessions}) of user sessions in the 'SESSIONS' table in "
+                        f"your database.<br> "
+                        "This indicates that this deployment might have an automated API calls that create "
+                        "and not reuse sessions.<br>You should consider reusing sessions or cleaning them "
+                        "periodically using db clean.<br>"
+                        "Make sure to reset password for the user again after cleaning the session table "
+                        "to remove old sessions of the user."
+                    ),
+                    "warning",
+                )
+            else:
+                for s in session.query(user_session_model).all():
+                    if s.expiry < datetime.utcnow():
+                        # housekeeping: delete old sessions that have expired
+                        session.delete(s)

Review Comment:
   IMO we should remove this block, session table cleanup shouldn't be a liability for the webserver.
   The user must maintain his database and clean it. If we want to support cleaning the table by the webserver, we need to create a periodic task instead of triggering it on an API call. WDYT?



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [airflow] potiuk commented on a diff in pull request #33347: Remove user sessions when resetting password

Posted by "potiuk (via GitHub)" <gi...@apache.org>.
potiuk commented on code in PR #33347:
URL: https://github.com/apache/airflow/pull/33347#discussion_r1292834810


##########
airflow/utils/db_cleanup.py:
##########
@@ -115,6 +116,9 @@ def readable_config(self):
     _TableConfig(table_name="celery_tasksetmeta", recency_column_name="date_done"),
 ]
 
+if conf.get("webserver", "session_backend") == "database":
+    config_list.append(_TableConfig(table_name="session", recency_column_name="expiry"))
+

Review Comment:
   Yes. We could add a new command for that, but I wanted to keep the scope of this one to be "bugfix" level - so not to add new features, in case we do not manage to get it in for 2.7.0 and would like to merge it for 2.7.1 



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [airflow] hussein-awala commented on a diff in pull request #33347: Remove user sessions when resetting password

Posted by "hussein-awala (via GitHub)" <gi...@apache.org>.
hussein-awala commented on code in PR #33347:
URL: https://github.com/apache/airflow/pull/33347#discussion_r1292836445


##########
airflow/auth/managers/fab/security_manager/override.py:
##########
@@ -230,8 +242,47 @@ def reset_password(self, userid, password):
         """
         user = self.get_user_by_id(userid)
         user.password = generate_password_hash(password)
+        self.reset_user_sessions(user)
         self.update_user(user)
 
+    def reset_user_sessions(self, user: User) -> None:
+        if isinstance(self.appbuilder.get_app.session_interface, AirflowDatabaseSessionInterface):
+            interface = self.appbuilder.get_app.session_interface
+            session = interface.db.session
+            user_session_model = interface.sql_session_model
+            num_sessions = session.query(user_session_model).count()
+            if num_sessions > MAX_NUM_DATABASE_USER_SESSIONS:
+                flash(
+                    Markup(
+                        f"The old sessions for user {user.username} have <b>NOT</b> been deleted!<br>"
+                        f"You have a lot ({num_sessions}) of user sessions in the 'SESSIONS' table in "
+                        f"your database.<br> "
+                        "This indicates that this deployment might have an automated API calls that create "
+                        "and not reuse sessions.<br>You should consider reusing sessions or cleaning them "
+                        "periodically using db clean.<br>"
+                        "Make sure to reset password for the user again after cleaning the session table "
+                        "to remove old sessions of the user."
+                    ),
+                    "warning",
+                )
+            else:
+                for s in session.query(user_session_model).all():

Review Comment:
   ```suggestion
                   for s in session.query(user_session_model):
   ```



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [airflow] potiuk commented on a diff in pull request #33347: Remove user sessions when resetting password

Posted by "potiuk (via GitHub)" <gi...@apache.org>.
potiuk commented on code in PR #33347:
URL: https://github.com/apache/airflow/pull/33347#discussion_r1292836716


##########
airflow/auth/managers/fab/security_manager/override.py:
##########
@@ -230,8 +242,47 @@ def reset_password(self, userid, password):
         """
         user = self.get_user_by_id(userid)
         user.password = generate_password_hash(password)
+        self.reset_user_sessions(user)
         self.update_user(user)
 
+    def reset_user_sessions(self, user: User) -> None:
+        if isinstance(self.appbuilder.get_app.session_interface, AirflowDatabaseSessionInterface):
+            interface = self.appbuilder.get_app.session_interface
+            session = interface.db.session
+            user_session_model = interface.sql_session_model
+            num_sessions = session.query(user_session_model).count()
+            if num_sessions > MAX_NUM_DATABASE_USER_SESSIONS:
+                flash(
+                    Markup(
+                        f"The old sessions for user {user.username} have <b>NOT</b> been deleted!<br>"
+                        f"You have a lot ({num_sessions}) of user sessions in the 'SESSIONS' table in "
+                        f"your database.<br> "
+                        "This indicates that this deployment might have an automated API calls that create "
+                        "and not reuse sessions.<br>You should consider reusing sessions or cleaning them "
+                        "periodically using db clean.<br>"
+                        "Make sure to reset password for the user again after cleaning the session table "
+                        "to remove old sessions of the user."
+                    ),
+                    "warning",
+                )
+            else:
+                for s in session.query(user_session_model).all():

Review Comment:
   Right. THOUSANDS of sessions in memory :)



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [airflow] potiuk commented on a diff in pull request #33347: Remove user sessions when resetting password

Posted by "potiuk (via GitHub)" <gi...@apache.org>.
potiuk commented on code in PR #33347:
URL: https://github.com/apache/airflow/pull/33347#discussion_r1292835533


##########
airflow/utils/db_cleanup.py:
##########
@@ -115,6 +116,9 @@ def readable_config(self):
     _TableConfig(table_name="celery_tasksetmeta", recency_column_name="date_done"),
 ]
 
+if conf.get("webserver", "session_backend") == "database":
+    config_list.append(_TableConfig(table_name="session", recency_column_name="expiry"))
+

Review Comment:
   Added task for the dedicated clean-all-expired-sessions command https://github.com/apache/airflow/issues/33370



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [airflow] potiuk commented on pull request #33347: Remove user sessions when resetting password

Posted by "potiuk (via GitHub)" <gi...@apache.org>.
potiuk commented on PR #33347:
URL: https://github.com/apache/airflow/pull/33347#issuecomment-1676429000

   Added task for the dedicated `clean-all-expired-sessions` command https://github.com/apache/airflow/issues/33370


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [airflow] potiuk commented on a diff in pull request #33347: Remove user sessions when resetting password

Posted by "potiuk (via GitHub)" <gi...@apache.org>.
potiuk commented on code in PR #33347:
URL: https://github.com/apache/airflow/pull/33347#discussion_r1292834537


##########
airflow/auth/managers/fab/security_manager/override.py:
##########
@@ -230,8 +243,51 @@ def reset_password(self, userid, password):
         """
         user = self.get_user_by_id(userid)
         user.password = generate_password_hash(password)
+        self.reset_user_sessions(user)
         self.update_user(user)
 
+    def reset_user_sessions(self, user: User) -> None:
+        if isinstance(self.appbuilder.get_app.session_interface, AirflowDatabaseSessionInterface):
+            interface = self.appbuilder.get_app.session_interface
+            session = interface.db.session
+            user_session_model = interface.sql_session_model
+            num_sessions = session.query(user_session_model).count()
+            if num_sessions > MAX_NUM_DATABASE_USER_SESSIONS:
+                flash(
+                    Markup(
+                        f"The old sessions for user {user.username} have <b>NOT</b> been deleted!<br>"
+                        f"You have a lot ({num_sessions}) of user sessions in the 'SESSIONS' table in "
+                        f"your database.<br> "
+                        "This indicates that this deployment might have an automated API calls that create "
+                        "and not reuse sessions.<br>You should consider reusing sessions or cleaning them "
+                        "periodically using db clean.<br>"
+                        "Make sure to reset password for the user again after cleaning the session table "
+                        "to remove old sessions of the user."
+                    ),
+                    "warning",
+                )
+            else:
+                for s in session.query(user_session_model).all():
+                    if s.expiry < datetime.utcnow():
+                        # housekeeping: delete old sessions that have expired
+                        session.delete(s)

Review Comment:
   Agree. I think I added it yet before I've added "clean" command and  the warnigns / documentation updates. I think the documentation is more than enough for the user to run the cleanup and automated housekeeping here is quite a bit different feature, that also has a potential for some problematic side-effects. Let me remove it.



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [airflow] potiuk commented on a diff in pull request #33347: Remove user sessions when resetting password

Posted by "potiuk (via GitHub)" <gi...@apache.org>.
potiuk commented on code in PR #33347:
URL: https://github.com/apache/airflow/pull/33347#discussion_r1292835090


##########
airflow/auth/managers/fab/security_manager/override.py:
##########
@@ -230,8 +243,51 @@ def reset_password(self, userid, password):
         """
         user = self.get_user_by_id(userid)
         user.password = generate_password_hash(password)
+        self.reset_user_sessions(user)
         self.update_user(user)
 
+    def reset_user_sessions(self, user: User) -> None:
+        if isinstance(self.appbuilder.get_app.session_interface, AirflowDatabaseSessionInterface):
+            interface = self.appbuilder.get_app.session_interface
+            session = interface.db.session
+            user_session_model = interface.sql_session_model
+            num_sessions = session.query(user_session_model).count()
+            if num_sessions > MAX_NUM_DATABASE_USER_SESSIONS:
+                flash(
+                    Markup(
+                        f"The old sessions for user {user.username} have <b>NOT</b> been deleted!<br>"
+                        f"You have a lot ({num_sessions}) of user sessions in the 'SESSIONS' table in "
+                        f"your database.<br> "
+                        "This indicates that this deployment might have an automated API calls that create "
+                        "and not reuse sessions.<br>You should consider reusing sessions or cleaning them "
+                        "periodically using db clean.<br>"
+                        "Make sure to reset password for the user again after cleaning the session table "
+                        "to remove old sessions of the user."
+                    ),
+                    "warning",
+                )
+            else:
+                for s in session.query(user_session_model).all():
+                    if s.expiry < datetime.utcnow():
+                        # housekeeping: delete old sessions that have expired
+                        session.delete(s)

Review Comment:
   Removed.



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [airflow] hussein-awala commented on a diff in pull request #33347: Remove user sessions when resetting password

Posted by "hussein-awala (via GitHub)" <gi...@apache.org>.
hussein-awala commented on code in PR #33347:
URL: https://github.com/apache/airflow/pull/33347#discussion_r1292836884


##########
airflow/auth/managers/fab/security_manager/override.py:
##########
@@ -230,8 +242,47 @@ def reset_password(self, userid, password):
         """
         user = self.get_user_by_id(userid)
         user.password = generate_password_hash(password)
+        self.reset_user_sessions(user)
         self.update_user(user)
 
+    def reset_user_sessions(self, user: User) -> None:
+        if isinstance(self.appbuilder.get_app.session_interface, AirflowDatabaseSessionInterface):
+            interface = self.appbuilder.get_app.session_interface
+            session = interface.db.session
+            user_session_model = interface.sql_session_model
+            num_sessions = session.query(user_session_model).count()
+            if num_sessions > MAX_NUM_DATABASE_USER_SESSIONS:
+                flash(
+                    Markup(
+                        f"The old sessions for user {user.username} have <b>NOT</b> been deleted!<br>"
+                        f"You have a lot ({num_sessions}) of user sessions in the 'SESSIONS' table in "
+                        f"your database.<br> "
+                        "This indicates that this deployment might have an automated API calls that create "
+                        "and not reuse sessions.<br>You should consider reusing sessions or cleaning them "
+                        "periodically using db clean.<br>"
+                        "Make sure to reset password for the user again after cleaning the session table "
+                        "to remove old sessions of the user."
+                    ),
+                    "warning",
+                )
+            else:
+                for s in session.query(user_session_model).all():

Review Comment:
   Exactly!



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org