Skip to content

Commit

Permalink
Bugfix: Webserver returns 500 for POST requests to api/dag/*/dagrun f…
Browse files Browse the repository at this point in the history
…rom 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]>
(cherry picked from commit 71bc871)
  • Loading branch information
pateash authored and ephraimbuddy committed Jan 11, 2024
1 parent 6624ade commit 5dad650
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
6 changes: 4 additions & 2 deletions airflow/auth/managers/base_auth_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,15 @@ def get_user_display_name(self) -> str:
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:
"""
Expand Down
20 changes: 20 additions & 0 deletions tests/api_connexion/endpoints/test_dag_run_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -1861,3 +1861,23 @@ def test_should_respond_404(self):
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

0 comments on commit 5dad650

Please sign in to comment.