This is an automated email from the ASF dual-hosted git repository.

husseinawala pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new 71bc871d35 Bugfix: Webserver returns 500 for POST requests to 
api/dag/*/dagrun from anonymous user (#36275)
71bc871d35 is described below

commit 71bc871d35cd3b562a49ce8f209098e2e24c1ef8
Author: Ashish Patel <[email protected]>
AuthorDate: Sun Dec 24 01:38:23 2023 +0530

    Bugfix: Webserver returns 500 for POST requests to api/dag/*/dagrun from 
anonymous user (#36275)
    
    * airflow#36110 -  bugfix
    
    * return type fixed
    
    * airflow#36110 -  bugfix
    
    * airflow#36110 -  fixes
    
    * airflow#36110 -  fixes
    
    * airflow#36110 -  adding test
    
    * airflow#36110 -  adding test
    
    * Fix unit test
    
    * Don't call get_id twice
    
    * Update app configuration after initialization
    
    ---------
    
    Co-authored-by: hussein-awala <[email protected]>
    Co-authored-by: Tzu-ping Chung <[email protected]>
---
 airflow/auth/managers/base_auth_manager.py           |  6 ++++--
 .../api_connexion/endpoints/test_dag_run_endpoint.py | 20 ++++++++++++++++++++
 2 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/airflow/auth/managers/base_auth_manager.py 
b/airflow/auth/managers/base_auth_manager.py
index 466e728774..25a3b514d4 100644
--- a/airflow/auth/managers/base_auth_manager.py
+++ b/airflow/auth/managers/base_auth_manager.py
@@ -99,13 +99,15 @@ class BaseAuthManager(LoggingMixin):
     def get_user(self) -> BaseUser | None:
         """Return the user associated to the user in session."""
 
-    def get_user_id(self) -> str:
+    def get_user_id(self) -> str | None:
         """Return the user ID associated to the user in session."""
         user = self.get_user()
         if not user:
             self.log.error("Calling 'get_user_id()' but the user is not signed 
in.")
             raise AirflowException("The user must be signed in.")
-        return str(user.get_id())
+        if user_id := user.get_id():
+            return str(user_id)
+        return None
 
     def init(self) -> None:
         """
diff --git a/tests/api_connexion/endpoints/test_dag_run_endpoint.py 
b/tests/api_connexion/endpoints/test_dag_run_endpoint.py
index 0a8015df9d..2c4c393dd3 100644
--- a/tests/api_connexion/endpoints/test_dag_run_endpoint.py
+++ b/tests/api_connexion/endpoints/test_dag_run_endpoint.py
@@ -1861,3 +1861,23 @@ class TestSetDagRunNote(TestDagRunEndpoint):
             environ_overrides={"REMOTE_USER": "test"},
         )
         assert response.status_code == 404
+
+    @conf_vars(
+        {
+            ("api", "auth_backends"): "airflow.api.auth.backend.default",
+        }
+    )
+    def test_should_respond_200_with_anonymous_user(self, dag_maker, session):
+        from airflow.www import app as application
+
+        app = application.create_app(testing=True)
+        app.config["AUTH_ROLE_PUBLIC"] = "Admin"
+        dag_runs = self._create_test_dag_run(DagRunState.SUCCESS)
+        session.add_all(dag_runs)
+        session.commit()
+        created_dr = dag_runs[0]
+        response = app.test_client().patch(
+            
f"api/v1/dags/{created_dr.dag_id}/dagRuns/TEST_DAG_RUN_ID_1/setNote",
+            json={"note": "I am setting a note with anonymous user"},
+        )
+        assert response.status_code == 200

Reply via email to