Skip to content

Commit

Permalink
Consolidate HTTP 401/403 Responses for Public API Routes (apache#43932)
Browse files Browse the repository at this point in the history
Moved 401 & 403 responses to top-level router so we can avoid having to add it too all the public api routers
  • Loading branch information
kaxil authored Nov 13, 2024
1 parent 046e57c commit 0d49535
Show file tree
Hide file tree
Showing 17 changed files with 222 additions and 165 deletions.
240 changes: 150 additions & 90 deletions airflow/api_fastapi/core_api/openapi/v1-generated.yaml

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion airflow/api_fastapi/core_api/routes/public/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@

from __future__ import annotations

from fastapi import status

from airflow.api_fastapi.common.router import AirflowRouter
from airflow.api_fastapi.core_api.openapi.exceptions import create_openapi_http_exception_doc
from airflow.api_fastapi.core_api.routes.public.assets import assets_router
from airflow.api_fastapi.core_api.routes.public.backfills import backfills_router
from airflow.api_fastapi.core_api.routes.public.connections import connections_router
Expand All @@ -38,7 +41,10 @@
from airflow.api_fastapi.core_api.routes.public.version import version_router
from airflow.api_fastapi.core_api.routes.public.xcom import xcom_router

public_router = AirflowRouter(prefix="/public")
public_router = AirflowRouter(
prefix="/public",
responses=create_openapi_http_exception_doc([status.HTTP_401_UNAUTHORIZED, status.HTTP_403_FORBIDDEN]),
)


public_router.include_router(assets_router)
Expand Down
13 changes: 1 addition & 12 deletions airflow/api_fastapi/core_api/routes/public/backfills.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@

@backfills_router.get(
path="/",
responses=create_openapi_http_exception_doc([status.HTTP_401_UNAUTHORIZED, status.HTTP_403_FORBIDDEN]),
)
def list_backfills(
dag_id: str,
Expand Down Expand Up @@ -78,9 +77,7 @@ def list_backfills(

@backfills_router.get(
path="/{backfill_id}",
responses=create_openapi_http_exception_doc(
[status.HTTP_401_UNAUTHORIZED, status.HTTP_403_FORBIDDEN, status.HTTP_404_NOT_FOUND]
),
responses=create_openapi_http_exception_doc([status.HTTP_404_NOT_FOUND]),
)
def get_backfill(
backfill_id: str,
Expand All @@ -96,8 +93,6 @@ def get_backfill(
path="/{backfill_id}/pause",
responses=create_openapi_http_exception_doc(
[
status.HTTP_401_UNAUTHORIZED,
status.HTTP_403_FORBIDDEN,
status.HTTP_404_NOT_FOUND,
status.HTTP_409_CONFLICT,
]
Expand All @@ -119,8 +114,6 @@ def pause_backfill(backfill_id, session: Annotated[Session, Depends(get_session)
path="/{backfill_id}/unpause",
responses=create_openapi_http_exception_doc(
[
status.HTTP_401_UNAUTHORIZED,
status.HTTP_403_FORBIDDEN,
status.HTTP_404_NOT_FOUND,
status.HTTP_409_CONFLICT,
]
Expand All @@ -141,8 +134,6 @@ def unpause_backfill(backfill_id, session: Annotated[Session, Depends(get_sessio
path="/{backfill_id}/cancel",
responses=create_openapi_http_exception_doc(
[
status.HTTP_401_UNAUTHORIZED,
status.HTTP_403_FORBIDDEN,
status.HTTP_404_NOT_FOUND,
status.HTTP_409_CONFLICT,
]
Expand Down Expand Up @@ -188,8 +179,6 @@ def cancel_backfill(backfill_id, session: Annotated[Session, Depends(get_session
path="/",
responses=create_openapi_http_exception_doc(
[
status.HTTP_401_UNAUTHORIZED,
status.HTTP_403_FORBIDDEN,
status.HTTP_404_NOT_FOUND,
status.HTTP_409_CONFLICT,
]
Expand Down
18 changes: 4 additions & 14 deletions airflow/api_fastapi/core_api/routes/public/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@
@connections_router.delete(
"/{connection_id}",
status_code=204,
responses=create_openapi_http_exception_doc(
[status.HTTP_401_UNAUTHORIZED, status.HTTP_403_FORBIDDEN, status.HTTP_404_NOT_FOUND]
),
responses=create_openapi_http_exception_doc([status.HTTP_404_NOT_FOUND]),
)
def delete_connection(
connection_id: str,
Expand All @@ -66,9 +64,7 @@ def delete_connection(

@connections_router.get(
"/{connection_id}",
responses=create_openapi_http_exception_doc(
[status.HTTP_401_UNAUTHORIZED, status.HTTP_403_FORBIDDEN, status.HTTP_404_NOT_FOUND]
),
responses=create_openapi_http_exception_doc([status.HTTP_404_NOT_FOUND]),
)
def get_connection(
connection_id: str,
Expand All @@ -87,9 +83,7 @@ def get_connection(

@connections_router.get(
"/",
responses=create_openapi_http_exception_doc(
[status.HTTP_401_UNAUTHORIZED, status.HTTP_403_FORBIDDEN, status.HTTP_404_NOT_FOUND]
),
responses=create_openapi_http_exception_doc([status.HTTP_404_NOT_FOUND]),
)
def get_connections(
limit: QueryLimit,
Expand Down Expand Up @@ -127,9 +121,7 @@ def get_connections(
@connections_router.post(
"/",
status_code=201,
responses=create_openapi_http_exception_doc(
[status.HTTP_401_UNAUTHORIZED, status.HTTP_403_FORBIDDEN, status.HTTP_409_CONFLICT]
),
responses=create_openapi_http_exception_doc([status.HTTP_409_CONFLICT]),
)
def post_connection(
post_body: ConnectionBody,
Expand All @@ -156,8 +148,6 @@ def post_connection(
responses=create_openapi_http_exception_doc(
[
status.HTTP_400_BAD_REQUEST,
status.HTTP_401_UNAUTHORIZED,
status.HTTP_403_FORBIDDEN,
status.HTTP_404_NOT_FOUND,
]
),
Expand Down
6 changes: 0 additions & 6 deletions airflow/api_fastapi/core_api/routes/public/dag_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@
"/{dag_run_id}",
responses=create_openapi_http_exception_doc(
[
status.HTTP_401_UNAUTHORIZED,
status.HTTP_403_FORBIDDEN,
status.HTTP_404_NOT_FOUND,
]
),
Expand All @@ -70,8 +68,6 @@ def get_dag_run(
responses=create_openapi_http_exception_doc(
[
status.HTTP_400_BAD_REQUEST,
status.HTTP_401_UNAUTHORIZED,
status.HTTP_403_FORBIDDEN,
status.HTTP_404_NOT_FOUND,
]
),
Expand All @@ -94,8 +90,6 @@ def delete_dag_run(dag_id: str, dag_run_id: str, session: Annotated[Session, Dep
responses=create_openapi_http_exception_doc(
[
status.HTTP_400_BAD_REQUEST,
status.HTTP_401_UNAUTHORIZED,
status.HTTP_403_FORBIDDEN,
status.HTTP_404_NOT_FOUND,
]
),
Expand Down
2 changes: 0 additions & 2 deletions airflow/api_fastapi/core_api/routes/public/dag_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@
**create_openapi_http_exception_doc(
[
status.HTTP_400_BAD_REQUEST,
status.HTTP_401_UNAUTHORIZED,
status.HTTP_403_FORBIDDEN,
status.HTTP_404_NOT_FOUND,
status.HTTP_406_NOT_ACCEPTABLE,
]
Expand Down
2 changes: 0 additions & 2 deletions airflow/api_fastapi/core_api/routes/public/dag_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@
responses=create_openapi_http_exception_doc(
[
status.HTTP_400_BAD_REQUEST,
status.HTTP_401_UNAUTHORIZED,
status.HTTP_403_FORBIDDEN,
status.HTTP_404_NOT_FOUND,
]
),
Expand Down
4 changes: 1 addition & 3 deletions airflow/api_fastapi/core_api/routes/public/dag_warning.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

from typing import Annotated

from fastapi import Depends, status
from fastapi import Depends
from sqlalchemy import select
from sqlalchemy.orm import Session

Expand All @@ -39,15 +39,13 @@
DAGWarningCollectionResponse,
DAGWarningResponse,
)
from airflow.api_fastapi.core_api.openapi.exceptions import create_openapi_http_exception_doc
from airflow.models import DagWarning

dag_warning_router = AirflowRouter(tags=["DagWarning"])


@dag_warning_router.get(
"/dagWarnings",
responses=create_openapi_http_exception_doc([status.HTTP_401_UNAUTHORIZED, status.HTTP_403_FORBIDDEN]),
)
def list_dag_warnings(
dag_id: QueryDagIdInDagWarningFilter,
Expand Down
11 changes: 0 additions & 11 deletions airflow/api_fastapi/core_api/routes/public/dags.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ def get_dags(

@dags_router.get(
"/tags",
responses=create_openapi_http_exception_doc([status.HTTP_401_UNAUTHORIZED, status.HTTP_403_FORBIDDEN]),
)
def get_dag_tags(
limit: QueryLimit,
Expand Down Expand Up @@ -136,8 +135,6 @@ def get_dag_tags(
responses=create_openapi_http_exception_doc(
[
status.HTTP_400_BAD_REQUEST,
status.HTTP_401_UNAUTHORIZED,
status.HTTP_403_FORBIDDEN,
status.HTTP_404_NOT_FOUND,
status.HTTP_422_UNPROCESSABLE_ENTITY,
]
Expand Down Expand Up @@ -165,8 +162,6 @@ def get_dag(dag_id: str, session: Annotated[Session, Depends(get_session)], requ
responses=create_openapi_http_exception_doc(
[
status.HTTP_400_BAD_REQUEST,
status.HTTP_401_UNAUTHORIZED,
status.HTTP_403_FORBIDDEN,
status.HTTP_404_NOT_FOUND,
]
),
Expand Down Expand Up @@ -195,8 +190,6 @@ def get_dag_details(
responses=create_openapi_http_exception_doc(
[
status.HTTP_400_BAD_REQUEST,
status.HTTP_401_UNAUTHORIZED,
status.HTTP_403_FORBIDDEN,
status.HTTP_404_NOT_FOUND,
]
),
Expand Down Expand Up @@ -234,8 +227,6 @@ def patch_dag(
responses=create_openapi_http_exception_doc(
[
status.HTTP_400_BAD_REQUEST,
status.HTTP_401_UNAUTHORIZED,
status.HTTP_403_FORBIDDEN,
status.HTTP_404_NOT_FOUND,
]
),
Expand Down Expand Up @@ -293,8 +284,6 @@ def patch_dags(
responses=create_openapi_http_exception_doc(
[
status.HTTP_400_BAD_REQUEST,
status.HTTP_401_UNAUTHORIZED,
status.HTTP_403_FORBIDDEN,
status.HTTP_404_NOT_FOUND,
status.HTTP_422_UNPROCESSABLE_ENTITY,
]
Expand Down
4 changes: 1 addition & 3 deletions airflow/api_fastapi/core_api/routes/public/event_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@

@event_logs_router.get(
"/{event_log_id}",
responses=create_openapi_http_exception_doc(
[status.HTTP_401_UNAUTHORIZED, status.HTTP_403_FORBIDDEN, status.HTTP_404_NOT_FOUND]
),
responses=create_openapi_http_exception_doc([status.HTTP_404_NOT_FOUND]),
)
def get_event_log(
event_log_id: int,
Expand Down
5 changes: 1 addition & 4 deletions airflow/api_fastapi/core_api/routes/public/import_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@

@import_error_router.get(
"/{import_error_id}",
responses=create_openapi_http_exception_doc(
[status.HTTP_401_UNAUTHORIZED, status.HTTP_403_FORBIDDEN, status.HTTP_404_NOT_FOUND]
),
responses=create_openapi_http_exception_doc([status.HTTP_404_NOT_FOUND]),
)
def get_import_error(
import_error_id: int,
Expand All @@ -65,7 +63,6 @@ def get_import_error(

@import_error_router.get(
"/",
responses=create_openapi_http_exception_doc([status.HTTP_401_UNAUTHORIZED, status.HTTP_403_FORBIDDEN]),
)
def get_import_errors(
limit: QueryLimit,
Expand Down
2 changes: 0 additions & 2 deletions airflow/api_fastapi/core_api/routes/public/pools.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@
responses=create_openapi_http_exception_doc(
[
status.HTTP_400_BAD_REQUEST,
status.HTTP_401_UNAUTHORIZED,
status.HTTP_403_FORBIDDEN,
status.HTTP_404_NOT_FOUND,
]
),
Expand Down
4 changes: 1 addition & 3 deletions airflow/api_fastapi/core_api/routes/public/task_instances.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@

@task_instances_router.get(
"/{task_id}",
responses=create_openapi_http_exception_doc(
[status.HTTP_401_UNAUTHORIZED, status.HTTP_403_FORBIDDEN, status.HTTP_404_NOT_FOUND]
),
responses=create_openapi_http_exception_doc([status.HTTP_404_NOT_FOUND]),
)
def get_task_instance(
dag_id: str, dag_run_id: str, task_id: str, session: Annotated[Session, Depends(get_session)]
Expand Down
12 changes: 2 additions & 10 deletions airflow/api_fastapi/core_api/routes/public/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@
@variables_router.delete(
"/{variable_key}",
status_code=204,
responses=create_openapi_http_exception_doc(
[status.HTTP_401_UNAUTHORIZED, status.HTTP_403_FORBIDDEN, status.HTTP_404_NOT_FOUND]
),
responses=create_openapi_http_exception_doc([status.HTTP_404_NOT_FOUND]),
)
def delete_variable(
variable_key: str,
Expand All @@ -56,9 +54,7 @@ def delete_variable(

@variables_router.get(
"/{variable_key}",
responses=create_openapi_http_exception_doc(
[status.HTTP_401_UNAUTHORIZED, status.HTTP_403_FORBIDDEN, status.HTTP_404_NOT_FOUND]
),
responses=create_openapi_http_exception_doc([status.HTTP_404_NOT_FOUND]),
)
def get_variable(
variable_key: str,
Expand All @@ -77,7 +73,6 @@ def get_variable(

@variables_router.get(
"/",
responses=create_openapi_http_exception_doc([status.HTTP_401_UNAUTHORIZED, status.HTTP_403_FORBIDDEN]),
)
def get_variables(
limit: QueryLimit,
Expand Down Expand Up @@ -116,8 +111,6 @@ def get_variables(
responses=create_openapi_http_exception_doc(
[
status.HTTP_400_BAD_REQUEST,
status.HTTP_401_UNAUTHORIZED,
status.HTTP_403_FORBIDDEN,
status.HTTP_404_NOT_FOUND,
]
),
Expand Down Expand Up @@ -153,7 +146,6 @@ def patch_variable(
@variables_router.post(
"/",
status_code=status.HTTP_201_CREATED,
responses=create_openapi_http_exception_doc([status.HTTP_401_UNAUTHORIZED, status.HTTP_403_FORBIDDEN]),
)
def post_variable(
post_body: VariableBody,
Expand Down
2 changes: 0 additions & 2 deletions airflow/api_fastapi/core_api/routes/public/xcom.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@
responses=create_openapi_http_exception_doc(
[
status.HTTP_400_BAD_REQUEST,
status.HTTP_401_UNAUTHORIZED,
status.HTTP_403_FORBIDDEN,
status.HTTP_404_NOT_FOUND,
]
),
Expand Down
Loading

0 comments on commit 0d49535

Please sign in to comment.