|
16 | 16 | # under the License. |
17 | 17 | from __future__ import annotations |
18 | 18 |
|
19 | | -from collections.abc import Sequence |
20 | | -from typing import TYPE_CHECKING |
| 19 | +from fastapi import Depends, Request, status |
| 20 | +from itsdangerous import URLSafeSerializer |
21 | 21 |
|
22 | | -from fastapi import Depends, HTTPException, Request, status |
23 | | -from itsdangerous import BadSignature, URLSafeSerializer |
24 | | -from sqlalchemy import select |
25 | | - |
26 | | -from airflow.api_fastapi.auth.managers.models.resource_details import DagDetails |
27 | 22 | from airflow.api_fastapi.common.db.common import SessionDep |
28 | 23 | from airflow.api_fastapi.common.router import AirflowRouter |
29 | 24 | from airflow.api_fastapi.core_api.openapi.exceptions import create_openapi_http_exception_doc |
30 | | -from airflow.api_fastapi.core_api.security import requires_access_dag |
| 25 | +from airflow.api_fastapi.core_api.security import requires_access_dag_from_file_token |
31 | 26 | from airflow.api_fastapi.logging.decorators import action_logging |
32 | | -from airflow.models.dag import DagModel |
33 | 27 | from airflow.models.dagbag import DagPriorityParsingRequest |
34 | 28 |
|
35 | | -if TYPE_CHECKING: |
36 | | - from airflow.api_fastapi.auth.managers.models.batch_apis import IsAuthorizedDagRequest |
37 | | - |
38 | 29 | dag_parsing_router = AirflowRouter(tags=["DAG Parsing"], prefix="/parseDagFile/{file_token}") |
39 | 30 |
|
40 | 31 |
|
41 | 32 | @dag_parsing_router.put( |
42 | 33 | "", |
43 | 34 | responses=create_openapi_http_exception_doc([status.HTTP_404_NOT_FOUND]), |
44 | 35 | status_code=status.HTTP_201_CREATED, |
45 | | - dependencies=[Depends(requires_access_dag(method="PUT")), Depends(action_logging())], |
| 36 | + dependencies=[Depends(requires_access_dag_from_file_token(method="PUT")), Depends(action_logging())], |
46 | 37 | ) |
47 | | -def reparse_dag_file( |
48 | | - file_token: str, |
49 | | - session: SessionDep, |
50 | | - request: Request, |
51 | | -) -> None: |
| 38 | +def reparse_dag_file(file_token: str, session: SessionDep, request: Request) -> None: |
52 | 39 | """Request re-parsing a Dag file.""" |
53 | | - secret_key = request.app.state.secret_key |
54 | | - auth_s = URLSafeSerializer(secret_key) |
55 | | - try: |
56 | | - payload = auth_s.loads(file_token) |
57 | | - except BadSignature: |
58 | | - raise HTTPException(status.HTTP_404_NOT_FOUND, "File not found") |
59 | | - |
60 | | - bundle_name = payload["bundle_name"] |
61 | | - relative_fileloc = payload["relative_fileloc"] |
62 | | - |
63 | | - requests: Sequence[IsAuthorizedDagRequest] = [ |
64 | | - {"method": "PUT", "details": DagDetails(id=dag_id)} |
65 | | - for dag_id in session.scalars( |
66 | | - select(DagModel.dag_id).where( |
67 | | - DagModel.bundle_name == bundle_name, DagModel.relative_fileloc == relative_fileloc |
68 | | - ) |
69 | | - ) |
70 | | - ] |
71 | | - if not requests: |
72 | | - raise HTTPException(status.HTTP_404_NOT_FOUND, "File not found") |
73 | | - |
74 | | - parsing_request = DagPriorityParsingRequest(bundle_name=bundle_name, relative_fileloc=relative_fileloc) |
| 40 | + payload = URLSafeSerializer(request.app.state.secret_key).loads(file_token) |
| 41 | + parsing_request = DagPriorityParsingRequest( |
| 42 | + bundle_name=payload["bundle_name"], relative_fileloc=payload["relative_fileloc"] |
| 43 | + ) |
75 | 44 | session.add(parsing_request) |
0 commit comments