Skip to content

Commit 1e2eca4

Browse files
add auth to tasks endpoint (apache#47684)
1 parent 5fc1812 commit 1e2eca4

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

airflow/api_fastapi/core_api/openapi/v1-generated.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -6394,6 +6394,8 @@ paths:
63946394
summary: Get Tasks
63956395
description: Get tasks for DAG.
63966396
operationId: get_tasks
6397+
security:
6398+
- OAuth2PasswordBearer: []
63976399
parameters:
63986400
- name: dag_id
63996401
in: path
@@ -6452,6 +6454,8 @@ paths:
64526454
summary: Get Task
64536455
description: Get simplified representation of a task.
64546456
operationId: get_task
6457+
security:
6458+
- OAuth2PasswordBearer: []
64556459
parameters:
64566460
- name: dag_id
64576461
in: path

airflow/api_fastapi/core_api/routes/public/tasks.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020
from operator import attrgetter
2121
from typing import cast
2222

23-
from fastapi import HTTPException, Request, status
23+
from fastapi import Depends, HTTPException, Request, status
2424

25+
from airflow.api_fastapi.auth.managers.models.resource_details import DagAccessEntity
2526
from airflow.api_fastapi.common.router import AirflowRouter
2627
from airflow.api_fastapi.core_api.datamodels.tasks import TaskCollectionResponse, TaskResponse
2728
from airflow.api_fastapi.core_api.openapi.exceptions import create_openapi_http_exception_doc
29+
from airflow.api_fastapi.core_api.security import requires_access_dag
2830
from airflow.exceptions import TaskNotFound
2931
from airflow.models import DAG
3032

@@ -39,6 +41,7 @@
3941
status.HTTP_404_NOT_FOUND,
4042
]
4143
),
44+
dependencies=[Depends(requires_access_dag(method="GET", access_entity=DagAccessEntity.TASK))],
4245
)
4346
def get_tasks(
4447
dag_id: str,
@@ -67,6 +70,7 @@ def get_tasks(
6770
status.HTTP_404_NOT_FOUND,
6871
]
6972
),
73+
dependencies=[Depends(requires_access_dag(method="GET", access_entity=DagAccessEntity.TASK))],
7074
)
7175
def get_task(dag_id: str, task_id, request: Request) -> TaskResponse:
7276
"""Get simplified representation of a task."""

tests/api_fastapi/core_api/routes/public/test_tasks.py

+16
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,14 @@ def test_should_respond_404_when_dag_not_found(self, test_client):
297297
)
298298
assert response.status_code == 404
299299

300+
def test_should_respond_401(self, unauthenticated_test_client):
301+
response = unauthenticated_test_client.get(f"{self.api_prefix}/{self.dag_id}/tasks/{self.task_id}")
302+
assert response.status_code == 401
303+
304+
def test_should_respond_403(self, unauthorized_test_client):
305+
response = unauthorized_test_client.get(f"{self.api_prefix}/{self.dag_id}/tasks/{self.task_id}")
306+
assert response.status_code == 403
307+
300308

301309
class TestGetTasks(TestTaskEndpoint):
302310
def test_should_respond_200(self, test_client):
@@ -540,3 +548,11 @@ def test_should_respond_404(self, test_client):
540548
dag_id = "xxxx_not_existing"
541549
response = test_client.get(f"{self.api_prefix}/{dag_id}/tasks")
542550
assert response.status_code == 404
551+
552+
def test_should_respond_401(self, unauthenticated_test_client):
553+
response = unauthenticated_test_client.get(f"{self.api_prefix}/{self.dag_id}/tasks")
554+
assert response.status_code == 401
555+
556+
def test_should_respond_403(self, unauthorized_test_client):
557+
response = unauthorized_test_client.get(f"{self.api_prefix}/{self.dag_id}/tasks")
558+
assert response.status_code == 403

0 commit comments

Comments
 (0)