Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: Webserver returns 500 for POST requests to api/dag/*/dagrun from anonymous user #36275

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
19 changes: 19 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,22 @@ 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(config={"AUTH_ROLE_PUBLIC": "Admin"}, testing=True)
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
pateash marked this conversation as resolved.
Show resolved Hide resolved
Loading