Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions airflow-core/pyproject.toml
Original file line numberDiff line numberDiff line change
Expand Up@@ -84,7 +84,7 @@ dependencies = [
"asgiref>=2.3.0; python_version < '3.14'",
"asgiref>=3.11.1; python_version >= '3.14'",
"attrs>=22.1.0, !=25.2.0",
"cadwyn>=6.1.1",
"cadwyn>=7.1.0",
"colorlog>=6.8.2",
"cron-descriptor>=1.2.24",
"croniter>=2.0.2",
Expand All@@ -95,11 +95,7 @@ dependencies = [
"cryptography>=44.0.3",
"deprecated>=1.2.13",
"dill>=0.2.2",
# Cap below 0.137.0: FastAPI 0.137 switched to lazy router inclusion, which breaks cadwyn's
# versioned router generation (RouterGenerationError) and fails api-server / dag-processor
# startup. Relax once cadwyn supports FastAPI 0.137. See
# https://github.com/apache/airflow/issues/68562
"fastapi[standard-no-fastapi-cloud-cli]>=0.129.0,<0.137.0",
"fastapi[standard-no-fastapi-cloud-cli]>=0.137.1",
"uvicorn>=0.37.0",
"starlette>=1.0.1",
"httpx>=0.25.0",
Expand Down
33 changes: 10 additions & 23 deletions airflow-core/src/airflow/api_fastapi/execution_api/app.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -259,30 +259,9 @@ async def _extract_w3c_trace_context(
otel_context.detach(token)


def _inject_trace_context_dep(routes, mode: str) -> None:
dep = Depends(_extract_w3c_trace_context)
for route in routes:
if not isinstance(route, APIRoute):
continue
# Idempotent: create_task_execution_api_app() runs more than once per process
# (cached_app + InProcessExecutionAPI), and execution_api_router is shared
# module state, so strip any prior injection first.
route.dependencies[:] = [
d for d in route.dependencies if getattr(d, "dependency", None) is not _extract_w3c_trace_context
]
match mode:
case "unsafe-always":
route.dependencies.insert(0, dep)
case "only-authenticated":
from airflow.api_fastapi.execution_api.security import require_auth

if any(getattr(d, "dependency", None) is require_auth for d in route.dependencies):
route.dependencies.append(dep)


def create_task_execution_api_app() -> FastAPI:
"""Create FastAPI app for task execution API."""
from airflow.api_fastapi.execution_api.routes import execution_api_router
from airflow.api_fastapi.execution_api.routes import build_execution_api_router
from airflow.api_fastapi.execution_api.versions import bundle
from airflow.configuration import conf

Expand All@@ -305,8 +284,16 @@ def custom_generate_unique_id(route: APIRoute):
app.add_middleware(CorrelationIdMiddleware)
app.add_middleware(JWTReissueMiddleware)

# FastAPI (>=0.137) freezes a router's dependencies into each route at include time, so the
# trace-context dependency has to be supplied while the router tree is assembled. "unsafe-always"
# extracts on every request (before auth); "only-authenticated" extracts only after a successful
# require_auth; "never" extracts nothing.
mode = conf.get("execution_api", "otel_trace_propagation", fallback="only-authenticated")
_inject_trace_context_dep(execution_api_router.routes, mode)
trace_context_dep = Depends(_extract_w3c_trace_context)
execution_api_router = build_execution_api_router(
pre_auth_dependencies=[trace_context_dep] if mode == "unsafe-always" else (),
post_auth_dependencies=[trace_context_dep] if mode == "only-authenticated" else (),
)

app.generate_and_include_versioned_routers(execution_api_router)

Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -16,6 +16,9 @@
# under the License.
from __future__ import annotations

from collections.abc import Sequence
from typing import Any

from cadwyn import VersionedAPIRouter
from fastapi import APIRouter, Security

Expand All@@ -37,34 +40,57 @@
)
from airflow.api_fastapi.execution_api.security import require_auth

execution_api_router = APIRouter()
# health.router declares its full paths ("/health", "/health/ping") and is included without a
# prefix, unlike the routers below. A root route registered as @router.get("") under an include-time
# prefix=... raises "Prefix and path cannot be both empty" once FastAPI switched to lazy router
# inclusion (>=0.137); see https://github.com/apache/airflow/issues/68562. Don't reintroduce a prefix here.
execution_api_router.include_router(health.router, tags=["Health"])

# _Every_ single endpoint under here must be authenticated. Some do further checks on top of these
authenticated_router = VersionedAPIRouter(dependencies=[Security(require_auth)]) # type: ignore[list-item]
def build_execution_api_router(
*,
pre_auth_dependencies: Sequence[Any] = (),
post_auth_dependencies: Sequence[Any] = (),
) -> APIRouter:
"""
Assemble the Task Execution API router tree.

authenticated_router.include_router(assets.router, prefix="/assets", tags=["Assets"])
authenticated_router.include_router(asset_events.router, prefix="/asset-events", tags=["Asset Events"])
authenticated_router.include_router(
connection_tests.router, prefix="/connection-tests", tags=["Connection Tests"]
)
authenticated_router.include_router(connections.router, prefix="/connections", tags=["Connections"])
authenticated_router.include_router(dag_runs.router, prefix="/dag-runs", tags=["Dag Runs"])
authenticated_router.include_router(dags.router, prefix="/dags", tags=["Dags"])
authenticated_router.include_router(task_instances.router, prefix="/task-instances", tags=["Task Instances"])
authenticated_router.include_router(
task_reschedules.router, prefix="/task-reschedules", tags=["Task Reschedules"]
)
authenticated_router.include_router(variables.router, prefix="/variables", tags=["Variables"])
authenticated_router.include_router(xcoms.router, prefix="/xcoms", tags=["XComs"])
authenticated_router.include_router(hitl.router, prefix="/hitlDetails", tags=["Human in the Loop"])
authenticated_router.include_router(task_state_store.router, prefix="/store/ti", tags=["Task State Store"])
authenticated_router.include_router(
asset_state_store.router, prefix="/store/asset", tags=["Asset State Store"]
)
``pre_auth_dependencies`` run before authentication on every route (health included);
``post_auth_dependencies`` run after ``require_auth`` and therefore only for successfully
authenticated requests. They must be supplied here, at build time: FastAPI (>=0.137) snapshots a
router's dependencies into each route when ``include_router`` is called, so dependencies attached
afterwards to a shared, already-assembled router never take effect.
"""
# _Every_ single endpoint under here must be authenticated. Some do further checks on top of these.
authenticated_router = VersionedAPIRouter(
dependencies=[Security(require_auth), *post_auth_dependencies] # type: ignore[list-item]
)

authenticated_router.include_router(assets.router, prefix="/assets", tags=["Assets"])
authenticated_router.include_router(asset_events.router, prefix="/asset-events", tags=["Asset Events"])
authenticated_router.include_router(
connection_tests.router, prefix="/connection-tests", tags=["Connection Tests"]
)
authenticated_router.include_router(connections.router, prefix="/connections", tags=["Connections"])
authenticated_router.include_router(dag_runs.router, prefix="/dag-runs", tags=["Dag Runs"])
authenticated_router.include_router(dags.router, prefix="/dags", tags=["Dags"])
authenticated_router.include_router(
task_instances.router, prefix="/task-instances", tags=["Task Instances"]
)
authenticated_router.include_router(
task_reschedules.router, prefix="/task-reschedules", tags=["Task Reschedules"]
)
authenticated_router.include_router(variables.router, prefix="/variables", tags=["Variables"])
authenticated_router.include_router(xcoms.router, prefix="/xcoms", tags=["XComs"])
authenticated_router.include_router(hitl.router, prefix="/hitlDetails", tags=["Human in the Loop"])
authenticated_router.include_router(
task_state_store.router, prefix="/store/ti", tags=["Task State Store"]
)
authenticated_router.include_router(
asset_state_store.router, prefix="/store/asset", tags=["Asset State Store"]
)

execution_api_router.include_router(authenticated_router)
execution_api_router = APIRouter()
# health.router declares its full paths ("/health", "/health/ping") and is included without a
# prefix, unlike the routers above. A root route registered as @router.get("") under an include-time
# prefix=... raises "Prefix and path cannot be both empty" once FastAPI switched to lazy router
# inclusion (>=0.137); see https://github.com/apache/airflow/issues/68562. Don't reintroduce a prefix here.
execution_api_router.include_router(
health.router, tags=["Health"], dependencies=list(pre_auth_dependencies)
)
execution_api_router.include_router(authenticated_router, dependencies=list(pre_auth_dependencies))
return execution_api_router
14 changes: 0 additions & 14 deletions airflow-core/tests/unit/api_fastapi/execution_api/test_app.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -190,20 +190,6 @@ def test_multiple_requests_with_different_correlation_ids(self, client):
class TestTraceContextPropagation:
"""Exercise ``execution_api.otel_trace_propagation`` on the real Execution API app."""

@pytest.fixture(autouse=True)
def _restore_router_dependencies(self):
from airflow.api_fastapi.execution_api.routes import execution_api_router

snapshot = {
id(route): list(route.dependencies)
for route in execution_api_router.routes
if isinstance(route, APIRoute)
}
yield
for route in execution_api_router.routes:
if isinstance(route, APIRoute):
route.dependencies[:] = snapshot[id(route)]

@staticmethod
def _build_app(mode: str):
with conf_vars({("execution_api", "otel_trace_propagation"): mode}):
Expand Down
16 changes: 8 additions & 8 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Add copy buttons to all
 blocks
(function() {
function addCopyButtons() {
document.querySelectorAll('pre code').forEach(function(codeBlock) {
if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;
codeBlock.parentElement.setAttribute('data-copy-added', 'true');
var btn = document.createElement('button');
btn.textContent = 'Copy';
btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';
btn.onmouseover = function() { this.style.opacity = '1'; };
btn.onmouseout = function() { this.style.opacity = '0.7'; };
btn.onclick = function() {
navigator.clipboard.writeText(codeBlock.textContent).then(function() {
btn.textContent = 'Copied!';
setTimeout(function() { btn.textContent = 'Copy'; }, 1500);
});
};
codeBlock.parentElement.style.position = 'relative';
codeBlock.parentElement.appendChild(btn);
});
}
addCopyButtons();
// Re-run on dynamic content
var observer = new MutationObserver(addCopyButtons);
observer.observe(document.body, { childList: true, subtree: true });
})();
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Restore FastAPI 0.137 support in the Task Execution API by Revanth14 · Pull Request #68826 · apache/airflow · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions airflow-core/pyproject.toml
Original file line numberDiff line numberDiff line change
Expand Up@@ -84,7 +84,7 @@ dependencies = [
"asgiref>=2.3.0; python_version < '3.14'",
"asgiref>=3.11.1; python_version >= '3.14'",
"attrs>=22.1.0, !=25.2.0",
"cadwyn>=6.1.1",
"cadwyn>=7.1.0",
"colorlog>=6.8.2",
"cron-descriptor>=1.2.24",
"croniter>=2.0.2",
Expand All@@ -95,11 +95,7 @@ dependencies = [
"cryptography>=44.0.3",
"deprecated>=1.2.13",
"dill>=0.2.2",
# Cap below 0.137.0: FastAPI 0.137 switched to lazy router inclusion, which breaks cadwyn's
# versioned router generation (RouterGenerationError) and fails api-server / dag-processor
# startup. Relax once cadwyn supports FastAPI 0.137. See
# https://github.com/apache/airflow/issues/68562
"fastapi[standard-no-fastapi-cloud-cli]>=0.129.0,<0.137.0",
"fastapi[standard-no-fastapi-cloud-cli]>=0.137.1",
"uvicorn>=0.37.0",
"starlette>=1.0.1",
"httpx>=0.25.0",
Expand Down
33 changes: 10 additions & 23 deletions airflow-core/src/airflow/api_fastapi/execution_api/app.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -259,30 +259,9 @@ async def _extract_w3c_trace_context(
otel_context.detach(token)


def _inject_trace_context_dep(routes, mode: str) -> None:
dep = Depends(_extract_w3c_trace_context)
for route in routes:
if not isinstance(route, APIRoute):
continue
# Idempotent: create_task_execution_api_app() runs more than once per process
# (cached_app + InProcessExecutionAPI), and execution_api_router is shared
# module state, so strip any prior injection first.
route.dependencies[:] = [
d for d in route.dependencies if getattr(d, "dependency", None) is not _extract_w3c_trace_context
]
match mode:
case "unsafe-always":
route.dependencies.insert(0, dep)
case "only-authenticated":
from airflow.api_fastapi.execution_api.security import require_auth

if any(getattr(d, "dependency", None) is require_auth for d in route.dependencies):
route.dependencies.append(dep)


def create_task_execution_api_app() -> FastAPI:
"""Create FastAPI app for task execution API."""
from airflow.api_fastapi.execution_api.routes import execution_api_router
from airflow.api_fastapi.execution_api.routes import build_execution_api_router
from airflow.api_fastapi.execution_api.versions import bundle
from airflow.configuration import conf

Expand All@@ -305,8 +284,16 @@ def custom_generate_unique_id(route: APIRoute):
app.add_middleware(CorrelationIdMiddleware)
app.add_middleware(JWTReissueMiddleware)

# FastAPI (>=0.137) freezes a router's dependencies into each route at include time, so the
# trace-context dependency has to be supplied while the router tree is assembled. "unsafe-always"
# extracts on every request (before auth); "only-authenticated" extracts only after a successful
# require_auth; "never" extracts nothing.
mode = conf.get("execution_api", "otel_trace_propagation", fallback="only-authenticated")
_inject_trace_context_dep(execution_api_router.routes, mode)
trace_context_dep = Depends(_extract_w3c_trace_context)
execution_api_router = build_execution_api_router(
pre_auth_dependencies=[trace_context_dep] if mode == "unsafe-always" else (),
post_auth_dependencies=[trace_context_dep] if mode == "only-authenticated" else (),
)

app.generate_and_include_versioned_routers(execution_api_router)

Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -16,6 +16,9 @@
# under the License.
from __future__ import annotations

from collections.abc import Sequence
from typing import Any

from cadwyn import VersionedAPIRouter
from fastapi import APIRouter, Security

Expand All@@ -37,34 +40,57 @@
)
from airflow.api_fastapi.execution_api.security import require_auth

execution_api_router = APIRouter()
# health.router declares its full paths ("/health", "/health/ping") and is included without a
# prefix, unlike the routers below. A root route registered as @router.get("") under an include-time
# prefix=... raises "Prefix and path cannot be both empty" once FastAPI switched to lazy router
# inclusion (>=0.137); see https://github.com/apache/airflow/issues/68562. Don't reintroduce a prefix here.
execution_api_router.include_router(health.router, tags=["Health"])

# _Every_ single endpoint under here must be authenticated. Some do further checks on top of these
authenticated_router = VersionedAPIRouter(dependencies=[Security(require_auth)]) # type: ignore[list-item]
def build_execution_api_router(
*,
pre_auth_dependencies: Sequence[Any] = (),
post_auth_dependencies: Sequence[Any] = (),
) -> APIRouter:
"""
Assemble the Task Execution API router tree.

authenticated_router.include_router(assets.router, prefix="/assets", tags=["Assets"])
authenticated_router.include_router(asset_events.router, prefix="/asset-events", tags=["Asset Events"])
authenticated_router.include_router(
connection_tests.router, prefix="/connection-tests", tags=["Connection Tests"]
)
authenticated_router.include_router(connections.router, prefix="/connections", tags=["Connections"])
authenticated_router.include_router(dag_runs.router, prefix="/dag-runs", tags=["Dag Runs"])
authenticated_router.include_router(dags.router, prefix="/dags", tags=["Dags"])
authenticated_router.include_router(task_instances.router, prefix="/task-instances", tags=["Task Instances"])
authenticated_router.include_router(
task_reschedules.router, prefix="/task-reschedules", tags=["Task Reschedules"]
)
authenticated_router.include_router(variables.router, prefix="/variables", tags=["Variables"])
authenticated_router.include_router(xcoms.router, prefix="/xcoms", tags=["XComs"])
authenticated_router.include_router(hitl.router, prefix="/hitlDetails", tags=["Human in the Loop"])
authenticated_router.include_router(task_state_store.router, prefix="/store/ti", tags=["Task State Store"])
authenticated_router.include_router(
asset_state_store.router, prefix="/store/asset", tags=["Asset State Store"]
)
``pre_auth_dependencies`` run before authentication on every route (health included);
``post_auth_dependencies`` run after ``require_auth`` and therefore only for successfully
authenticated requests. They must be supplied here, at build time: FastAPI (>=0.137) snapshots a
router's dependencies into each route when ``include_router`` is called, so dependencies attached
afterwards to a shared, already-assembled router never take effect.
"""
# _Every_ single endpoint under here must be authenticated. Some do further checks on top of these.
authenticated_router = VersionedAPIRouter(
dependencies=[Security(require_auth), *post_auth_dependencies] # type: ignore[list-item]
)

authenticated_router.include_router(assets.router, prefix="/assets", tags=["Assets"])
authenticated_router.include_router(asset_events.router, prefix="/asset-events", tags=["Asset Events"])
authenticated_router.include_router(
connection_tests.router, prefix="/connection-tests", tags=["Connection Tests"]
)
authenticated_router.include_router(connections.router, prefix="/connections", tags=["Connections"])
authenticated_router.include_router(dag_runs.router, prefix="/dag-runs", tags=["Dag Runs"])
authenticated_router.include_router(dags.router, prefix="/dags", tags=["Dags"])
authenticated_router.include_router(
task_instances.router, prefix="/task-instances", tags=["Task Instances"]
)
authenticated_router.include_router(
task_reschedules.router, prefix="/task-reschedules", tags=["Task Reschedules"]
)
authenticated_router.include_router(variables.router, prefix="/variables", tags=["Variables"])
authenticated_router.include_router(xcoms.router, prefix="/xcoms", tags=["XComs"])
authenticated_router.include_router(hitl.router, prefix="/hitlDetails", tags=["Human in the Loop"])
authenticated_router.include_router(
task_state_store.router, prefix="/store/ti", tags=["Task State Store"]
)
authenticated_router.include_router(
asset_state_store.router, prefix="/store/asset", tags=["Asset State Store"]
)

execution_api_router.include_router(authenticated_router)
execution_api_router = APIRouter()
# health.router declares its full paths ("/health", "/health/ping") and is included without a
# prefix, unlike the routers above. A root route registered as @router.get("") under an include-time
# prefix=... raises "Prefix and path cannot be both empty" once FastAPI switched to lazy router
# inclusion (>=0.137); see https://github.com/apache/airflow/issues/68562. Don't reintroduce a prefix here.
execution_api_router.include_router(
health.router, tags=["Health"], dependencies=list(pre_auth_dependencies)
)
execution_api_router.include_router(authenticated_router, dependencies=list(pre_auth_dependencies))
return execution_api_router
14 changes: 0 additions & 14 deletions airflow-core/tests/unit/api_fastapi/execution_api/test_app.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -190,20 +190,6 @@ def test_multiple_requests_with_different_correlation_ids(self, client):
class TestTraceContextPropagation:
"""Exercise ``execution_api.otel_trace_propagation`` on the real Execution API app."""

@pytest.fixture(autouse=True)
def _restore_router_dependencies(self):
from airflow.api_fastapi.execution_api.routes import execution_api_router

snapshot = {
id(route): list(route.dependencies)
for route in execution_api_router.routes
if isinstance(route, APIRoute)
}
yield
for route in execution_api_router.routes:
if isinstance(route, APIRoute):
route.dependencies[:] = snapshot[id(route)]

@staticmethod
def _build_app(mode: str):
with conf_vars({("execution_api", "otel_trace_propagation"): mode}):
Expand Down
16 changes: 8 additions & 8 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Force GitHub README to respect dark mode (function() { var style = document.createElement('style'); style.textContent = ' .markdown-body { color-scheme: dark light; } .markdown-body pre { background: #161b22 !important; } .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; } .markdown-body table th, .markdown-body table td { border-color: #30363d !important; } .markdown-body img { background: #0d1117; } .markdown-body blockquote { border-left-color: #8b949e; } .markdown-body hr { border-color: #30363d; } '; document.head.appendChild(style); })(); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' Restore FastAPI 0.137 support in the Task Execution API by Revanth14 · Pull Request #68826 · apache/airflow · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions airflow-core/pyproject.toml
Original file line numberDiff line numberDiff line change
Expand Up@@ -84,7 +84,7 @@ dependencies = [
"asgiref>=2.3.0; python_version < '3.14'",
"asgiref>=3.11.1; python_version >= '3.14'",
"attrs>=22.1.0, !=25.2.0",
"cadwyn>=6.1.1",
"cadwyn>=7.1.0",
"colorlog>=6.8.2",
"cron-descriptor>=1.2.24",
"croniter>=2.0.2",
Expand All@@ -95,11 +95,7 @@ dependencies = [
"cryptography>=44.0.3",
"deprecated>=1.2.13",
"dill>=0.2.2",
# Cap below 0.137.0: FastAPI 0.137 switched to lazy router inclusion, which breaks cadwyn's
# versioned router generation (RouterGenerationError) and fails api-server / dag-processor
# startup. Relax once cadwyn supports FastAPI 0.137. See
# https://github.com/apache/airflow/issues/68562
"fastapi[standard-no-fastapi-cloud-cli]>=0.129.0,<0.137.0",
"fastapi[standard-no-fastapi-cloud-cli]>=0.137.1",
"uvicorn>=0.37.0",
"starlette>=1.0.1",
"httpx>=0.25.0",
Expand Down
33 changes: 10 additions & 23 deletions airflow-core/src/airflow/api_fastapi/execution_api/app.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -259,30 +259,9 @@ async def _extract_w3c_trace_context(
otel_context.detach(token)


def _inject_trace_context_dep(routes, mode: str) -> None:
dep = Depends(_extract_w3c_trace_context)
for route in routes:
if not isinstance(route, APIRoute):
continue
# Idempotent: create_task_execution_api_app() runs more than once per process
# (cached_app + InProcessExecutionAPI), and execution_api_router is shared
# module state, so strip any prior injection first.
route.dependencies[:] = [
d for d in route.dependencies if getattr(d, "dependency", None) is not _extract_w3c_trace_context
]
match mode:
case "unsafe-always":
route.dependencies.insert(0, dep)
case "only-authenticated":
from airflow.api_fastapi.execution_api.security import require_auth

if any(getattr(d, "dependency", None) is require_auth for d in route.dependencies):
route.dependencies.append(dep)


def create_task_execution_api_app() -> FastAPI:
"""Create FastAPI app for task execution API."""
from airflow.api_fastapi.execution_api.routes import execution_api_router
from airflow.api_fastapi.execution_api.routes import build_execution_api_router
from airflow.api_fastapi.execution_api.versions import bundle
from airflow.configuration import conf

Expand All@@ -305,8 +284,16 @@ def custom_generate_unique_id(route: APIRoute):
app.add_middleware(CorrelationIdMiddleware)
app.add_middleware(JWTReissueMiddleware)

# FastAPI (>=0.137) freezes a router's dependencies into each route at include time, so the
# trace-context dependency has to be supplied while the router tree is assembled. "unsafe-always"
# extracts on every request (before auth); "only-authenticated" extracts only after a successful
# require_auth; "never" extracts nothing.
mode = conf.get("execution_api", "otel_trace_propagation", fallback="only-authenticated")
_inject_trace_context_dep(execution_api_router.routes, mode)
trace_context_dep = Depends(_extract_w3c_trace_context)
execution_api_router = build_execution_api_router(
pre_auth_dependencies=[trace_context_dep] if mode == "unsafe-always" else (),
post_auth_dependencies=[trace_context_dep] if mode == "only-authenticated" else (),
)

app.generate_and_include_versioned_routers(execution_api_router)

Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -16,6 +16,9 @@
# under the License.
from __future__ import annotations

from collections.abc import Sequence
from typing import Any

from cadwyn import VersionedAPIRouter
from fastapi import APIRouter, Security

Expand All@@ -37,34 +40,57 @@
)
from airflow.api_fastapi.execution_api.security import require_auth

execution_api_router = APIRouter()
# health.router declares its full paths ("/health", "/health/ping") and is included without a
# prefix, unlike the routers below. A root route registered as @router.get("") under an include-time
# prefix=... raises "Prefix and path cannot be both empty" once FastAPI switched to lazy router
# inclusion (>=0.137); see https://github.com/apache/airflow/issues/68562. Don't reintroduce a prefix here.
execution_api_router.include_router(health.router, tags=["Health"])

# _Every_ single endpoint under here must be authenticated. Some do further checks on top of these
authenticated_router = VersionedAPIRouter(dependencies=[Security(require_auth)]) # type: ignore[list-item]
def build_execution_api_router(
*,
pre_auth_dependencies: Sequence[Any] = (),
post_auth_dependencies: Sequence[Any] = (),
) -> APIRouter:
"""
Assemble the Task Execution API router tree.

authenticated_router.include_router(assets.router, prefix="/assets", tags=["Assets"])
authenticated_router.include_router(asset_events.router, prefix="/asset-events", tags=["Asset Events"])
authenticated_router.include_router(
connection_tests.router, prefix="/connection-tests", tags=["Connection Tests"]
)
authenticated_router.include_router(connections.router, prefix="/connections", tags=["Connections"])
authenticated_router.include_router(dag_runs.router, prefix="/dag-runs", tags=["Dag Runs"])
authenticated_router.include_router(dags.router, prefix="/dags", tags=["Dags"])
authenticated_router.include_router(task_instances.router, prefix="/task-instances", tags=["Task Instances"])
authenticated_router.include_router(
task_reschedules.router, prefix="/task-reschedules", tags=["Task Reschedules"]
)
authenticated_router.include_router(variables.router, prefix="/variables", tags=["Variables"])
authenticated_router.include_router(xcoms.router, prefix="/xcoms", tags=["XComs"])
authenticated_router.include_router(hitl.router, prefix="/hitlDetails", tags=["Human in the Loop"])
authenticated_router.include_router(task_state_store.router, prefix="/store/ti", tags=["Task State Store"])
authenticated_router.include_router(
asset_state_store.router, prefix="/store/asset", tags=["Asset State Store"]
)
``pre_auth_dependencies`` run before authentication on every route (health included);
``post_auth_dependencies`` run after ``require_auth`` and therefore only for successfully
authenticated requests. They must be supplied here, at build time: FastAPI (>=0.137) snapshots a
router's dependencies into each route when ``include_router`` is called, so dependencies attached
afterwards to a shared, already-assembled router never take effect.
"""
# _Every_ single endpoint under here must be authenticated. Some do further checks on top of these.
authenticated_router = VersionedAPIRouter(
dependencies=[Security(require_auth), *post_auth_dependencies] # type: ignore[list-item]
)

authenticated_router.include_router(assets.router, prefix="/assets", tags=["Assets"])
authenticated_router.include_router(asset_events.router, prefix="/asset-events", tags=["Asset Events"])
authenticated_router.include_router(
connection_tests.router, prefix="/connection-tests", tags=["Connection Tests"]
)
authenticated_router.include_router(connections.router, prefix="/connections", tags=["Connections"])
authenticated_router.include_router(dag_runs.router, prefix="/dag-runs", tags=["Dag Runs"])
authenticated_router.include_router(dags.router, prefix="/dags", tags=["Dags"])
authenticated_router.include_router(
task_instances.router, prefix="/task-instances", tags=["Task Instances"]
)
authenticated_router.include_router(
task_reschedules.router, prefix="/task-reschedules", tags=["Task Reschedules"]
)
authenticated_router.include_router(variables.router, prefix="/variables", tags=["Variables"])
authenticated_router.include_router(xcoms.router, prefix="/xcoms", tags=["XComs"])
authenticated_router.include_router(hitl.router, prefix="/hitlDetails", tags=["Human in the Loop"])
authenticated_router.include_router(
task_state_store.router, prefix="/store/ti", tags=["Task State Store"]
)
authenticated_router.include_router(
asset_state_store.router, prefix="/store/asset", tags=["Asset State Store"]
)

execution_api_router.include_router(authenticated_router)
execution_api_router = APIRouter()
# health.router declares its full paths ("/health", "/health/ping") and is included without a
# prefix, unlike the routers above. A root route registered as @router.get("") under an include-time
# prefix=... raises "Prefix and path cannot be both empty" once FastAPI switched to lazy router
# inclusion (>=0.137); see https://github.com/apache/airflow/issues/68562. Don't reintroduce a prefix here.
execution_api_router.include_router(
health.router, tags=["Health"], dependencies=list(pre_auth_dependencies)
)
execution_api_router.include_router(authenticated_router, dependencies=list(pre_auth_dependencies))
return execution_api_router
14 changes: 0 additions & 14 deletions airflow-core/tests/unit/api_fastapi/execution_api/test_app.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -190,20 +190,6 @@ def test_multiple_requests_with_different_correlation_ids(self, client):
class TestTraceContextPropagation:
"""Exercise ``execution_api.otel_trace_propagation`` on the real Execution API app."""

@pytest.fixture(autouse=True)
def _restore_router_dependencies(self):
from airflow.api_fastapi.execution_api.routes import execution_api_router

snapshot = {
id(route): list(route.dependencies)
for route in execution_api_router.routes
if isinstance(route, APIRoute)
}
yield
for route in execution_api_router.routes:
if isinstance(route, APIRoute):
route.dependencies[:] = snapshot[id(route)]

@staticmethod
def _build_app(mode: str):
with conf_vars({("execution_api", "otel_trace_propagation"): mode}):
Expand Down
16 changes: 8 additions & 8 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Highlight search terms from Google/DuckDuckGo/Bing referrer (function() { var ref = document.referrer; var terms = []; if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) { var url = new URL(ref); var q = url.searchParams.get('q') || url.searchParams.get('p'); if (q) { terms = q.split(/\s+/).filter(function(t) { return t.length > 2; }); } } if (terms.length === 0) return; var style = document.createElement('style'); style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }'; document.head.appendChild(style); function highlight(node) { if (node.nodeType === 3) { // text node var text = node.textContent; var found = false; terms.forEach(function(term) { var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\]\\]/g, '\\') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' Restore FastAPI 0.137 support in the Task Execution API by Revanth14 · Pull Request #68826 · apache/airflow · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions airflow-core/pyproject.toml
Original file line numberDiff line numberDiff line change
Expand Up@@ -84,7 +84,7 @@ dependencies = [
"asgiref>=2.3.0; python_version < '3.14'",
"asgiref>=3.11.1; python_version >= '3.14'",
"attrs>=22.1.0, !=25.2.0",
"cadwyn>=6.1.1",
"cadwyn>=7.1.0",
"colorlog>=6.8.2",
"cron-descriptor>=1.2.24",
"croniter>=2.0.2",
Expand All@@ -95,11 +95,7 @@ dependencies = [
"cryptography>=44.0.3",
"deprecated>=1.2.13",
"dill>=0.2.2",
# Cap below 0.137.0: FastAPI 0.137 switched to lazy router inclusion, which breaks cadwyn's
# versioned router generation (RouterGenerationError) and fails api-server / dag-processor
# startup. Relax once cadwyn supports FastAPI 0.137. See
# https://github.com/apache/airflow/issues/68562
"fastapi[standard-no-fastapi-cloud-cli]>=0.129.0,<0.137.0",
"fastapi[standard-no-fastapi-cloud-cli]>=0.137.1",
"uvicorn>=0.37.0",
"starlette>=1.0.1",
"httpx>=0.25.0",
Expand Down
33 changes: 10 additions & 23 deletions airflow-core/src/airflow/api_fastapi/execution_api/app.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -259,30 +259,9 @@ async def _extract_w3c_trace_context(
otel_context.detach(token)


def _inject_trace_context_dep(routes, mode: str) -> None:
dep = Depends(_extract_w3c_trace_context)
for route in routes:
if not isinstance(route, APIRoute):
continue
# Idempotent: create_task_execution_api_app() runs more than once per process
# (cached_app + InProcessExecutionAPI), and execution_api_router is shared
# module state, so strip any prior injection first.
route.dependencies[:] = [
d for d in route.dependencies if getattr(d, "dependency", None) is not _extract_w3c_trace_context
]
match mode:
case "unsafe-always":
route.dependencies.insert(0, dep)
case "only-authenticated":
from airflow.api_fastapi.execution_api.security import require_auth

if any(getattr(d, "dependency", None) is require_auth for d in route.dependencies):
route.dependencies.append(dep)


def create_task_execution_api_app() -> FastAPI:
"""Create FastAPI app for task execution API."""
from airflow.api_fastapi.execution_api.routes import execution_api_router
from airflow.api_fastapi.execution_api.routes import build_execution_api_router
from airflow.api_fastapi.execution_api.versions import bundle
from airflow.configuration import conf

Expand All@@ -305,8 +284,16 @@ def custom_generate_unique_id(route: APIRoute):
app.add_middleware(CorrelationIdMiddleware)
app.add_middleware(JWTReissueMiddleware)

# FastAPI (>=0.137) freezes a router's dependencies into each route at include time, so the
# trace-context dependency has to be supplied while the router tree is assembled. "unsafe-always"
# extracts on every request (before auth); "only-authenticated" extracts only after a successful
# require_auth; "never" extracts nothing.
mode = conf.get("execution_api", "otel_trace_propagation", fallback="only-authenticated")
_inject_trace_context_dep(execution_api_router.routes, mode)
trace_context_dep = Depends(_extract_w3c_trace_context)
execution_api_router = build_execution_api_router(
pre_auth_dependencies=[trace_context_dep] if mode == "unsafe-always" else (),
post_auth_dependencies=[trace_context_dep] if mode == "only-authenticated" else (),
)

app.generate_and_include_versioned_routers(execution_api_router)

Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -16,6 +16,9 @@
# under the License.
from __future__ import annotations

from collections.abc import Sequence
from typing import Any

from cadwyn import VersionedAPIRouter
from fastapi import APIRouter, Security

Expand All@@ -37,34 +40,57 @@
)
from airflow.api_fastapi.execution_api.security import require_auth

execution_api_router = APIRouter()
# health.router declares its full paths ("/health", "/health/ping") and is included without a
# prefix, unlike the routers below. A root route registered as @router.get("") under an include-time
# prefix=... raises "Prefix and path cannot be both empty" once FastAPI switched to lazy router
# inclusion (>=0.137); see https://github.com/apache/airflow/issues/68562. Don't reintroduce a prefix here.
execution_api_router.include_router(health.router, tags=["Health"])

# _Every_ single endpoint under here must be authenticated. Some do further checks on top of these
authenticated_router = VersionedAPIRouter(dependencies=[Security(require_auth)]) # type: ignore[list-item]
def build_execution_api_router(
*,
pre_auth_dependencies: Sequence[Any] = (),
post_auth_dependencies: Sequence[Any] = (),
) -> APIRouter:
"""
Assemble the Task Execution API router tree.

authenticated_router.include_router(assets.router, prefix="/assets", tags=["Assets"])
authenticated_router.include_router(asset_events.router, prefix="/asset-events", tags=["Asset Events"])
authenticated_router.include_router(
connection_tests.router, prefix="/connection-tests", tags=["Connection Tests"]
)
authenticated_router.include_router(connections.router, prefix="/connections", tags=["Connections"])
authenticated_router.include_router(dag_runs.router, prefix="/dag-runs", tags=["Dag Runs"])
authenticated_router.include_router(dags.router, prefix="/dags", tags=["Dags"])
authenticated_router.include_router(task_instances.router, prefix="/task-instances", tags=["Task Instances"])
authenticated_router.include_router(
task_reschedules.router, prefix="/task-reschedules", tags=["Task Reschedules"]
)
authenticated_router.include_router(variables.router, prefix="/variables", tags=["Variables"])
authenticated_router.include_router(xcoms.router, prefix="/xcoms", tags=["XComs"])
authenticated_router.include_router(hitl.router, prefix="/hitlDetails", tags=["Human in the Loop"])
authenticated_router.include_router(task_state_store.router, prefix="/store/ti", tags=["Task State Store"])
authenticated_router.include_router(
asset_state_store.router, prefix="/store/asset", tags=["Asset State Store"]
)
``pre_auth_dependencies`` run before authentication on every route (health included);
``post_auth_dependencies`` run after ``require_auth`` and therefore only for successfully
authenticated requests. They must be supplied here, at build time: FastAPI (>=0.137) snapshots a
router's dependencies into each route when ``include_router`` is called, so dependencies attached
afterwards to a shared, already-assembled router never take effect.
"""
# _Every_ single endpoint under here must be authenticated. Some do further checks on top of these.
authenticated_router = VersionedAPIRouter(
dependencies=[Security(require_auth), *post_auth_dependencies] # type: ignore[list-item]
)

authenticated_router.include_router(assets.router, prefix="/assets", tags=["Assets"])
authenticated_router.include_router(asset_events.router, prefix="/asset-events", tags=["Asset Events"])
authenticated_router.include_router(
connection_tests.router, prefix="/connection-tests", tags=["Connection Tests"]
)
authenticated_router.include_router(connections.router, prefix="/connections", tags=["Connections"])
authenticated_router.include_router(dag_runs.router, prefix="/dag-runs", tags=["Dag Runs"])
authenticated_router.include_router(dags.router, prefix="/dags", tags=["Dags"])
authenticated_router.include_router(
task_instances.router, prefix="/task-instances", tags=["Task Instances"]
)
authenticated_router.include_router(
task_reschedules.router, prefix="/task-reschedules", tags=["Task Reschedules"]
)
authenticated_router.include_router(variables.router, prefix="/variables", tags=["Variables"])
authenticated_router.include_router(xcoms.router, prefix="/xcoms", tags=["XComs"])
authenticated_router.include_router(hitl.router, prefix="/hitlDetails", tags=["Human in the Loop"])
authenticated_router.include_router(
task_state_store.router, prefix="/store/ti", tags=["Task State Store"]
)
authenticated_router.include_router(
asset_state_store.router, prefix="/store/asset", tags=["Asset State Store"]
)

execution_api_router.include_router(authenticated_router)
execution_api_router = APIRouter()
# health.router declares its full paths ("/health", "/health/ping") and is included without a
# prefix, unlike the routers above. A root route registered as @router.get("") under an include-time
# prefix=... raises "Prefix and path cannot be both empty" once FastAPI switched to lazy router
# inclusion (>=0.137); see https://github.com/apache/airflow/issues/68562. Don't reintroduce a prefix here.
execution_api_router.include_router(
health.router, tags=["Health"], dependencies=list(pre_auth_dependencies)
)
execution_api_router.include_router(authenticated_router, dependencies=list(pre_auth_dependencies))
return execution_api_router
14 changes: 0 additions & 14 deletions airflow-core/tests/unit/api_fastapi/execution_api/test_app.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -190,20 +190,6 @@ def test_multiple_requests_with_different_correlation_ids(self, client):
class TestTraceContextPropagation:
"""Exercise ``execution_api.otel_trace_propagation`` on the real Execution API app."""

@pytest.fixture(autouse=True)
def _restore_router_dependencies(self):
from airflow.api_fastapi.execution_api.routes import execution_api_router

snapshot = {
id(route): list(route.dependencies)
for route in execution_api_router.routes
if isinstance(route, APIRoute)
}
yield
for route in execution_api_router.routes:
if isinstance(route, APIRoute):
route.dependencies[:] = snapshot[id(route)]

@staticmethod
def _build_app(mode: str):
with conf_vars({("execution_api", "otel_trace_propagation"): mode}):
Expand Down
16 changes: 8 additions & 8 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ' Restore FastAPI 0.137 support in the Task Execution API by Revanth14 · Pull Request #68826 · apache/airflow · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions airflow-core/pyproject.toml
Original file line numberDiff line numberDiff line change
Expand Up@@ -84,7 +84,7 @@ dependencies = [
"asgiref>=2.3.0; python_version < '3.14'",
"asgiref>=3.11.1; python_version >= '3.14'",
"attrs>=22.1.0, !=25.2.0",
"cadwyn>=6.1.1",
"cadwyn>=7.1.0",
"colorlog>=6.8.2",
"cron-descriptor>=1.2.24",
"croniter>=2.0.2",
Expand All@@ -95,11 +95,7 @@ dependencies = [
"cryptography>=44.0.3",
"deprecated>=1.2.13",
"dill>=0.2.2",
# Cap below 0.137.0: FastAPI 0.137 switched to lazy router inclusion, which breaks cadwyn's
# versioned router generation (RouterGenerationError) and fails api-server / dag-processor
# startup. Relax once cadwyn supports FastAPI 0.137. See
# https://github.com/apache/airflow/issues/68562
"fastapi[standard-no-fastapi-cloud-cli]>=0.129.0,<0.137.0",
"fastapi[standard-no-fastapi-cloud-cli]>=0.137.1",
"uvicorn>=0.37.0",
"starlette>=1.0.1",
"httpx>=0.25.0",
Expand Down
33 changes: 10 additions & 23 deletions airflow-core/src/airflow/api_fastapi/execution_api/app.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -259,30 +259,9 @@ async def _extract_w3c_trace_context(
otel_context.detach(token)


def _inject_trace_context_dep(routes, mode: str) -> None:
dep = Depends(_extract_w3c_trace_context)
for route in routes:
if not isinstance(route, APIRoute):
continue
# Idempotent: create_task_execution_api_app() runs more than once per process
# (cached_app + InProcessExecutionAPI), and execution_api_router is shared
# module state, so strip any prior injection first.
route.dependencies[:] = [
d for d in route.dependencies if getattr(d, "dependency", None) is not _extract_w3c_trace_context
]
match mode:
case "unsafe-always":
route.dependencies.insert(0, dep)
case "only-authenticated":
from airflow.api_fastapi.execution_api.security import require_auth

if any(getattr(d, "dependency", None) is require_auth for d in route.dependencies):
route.dependencies.append(dep)


def create_task_execution_api_app() -> FastAPI:
"""Create FastAPI app for task execution API."""
from airflow.api_fastapi.execution_api.routes import execution_api_router
from airflow.api_fastapi.execution_api.routes import build_execution_api_router
from airflow.api_fastapi.execution_api.versions import bundle
from airflow.configuration import conf

Expand All@@ -305,8 +284,16 @@ def custom_generate_unique_id(route: APIRoute):
app.add_middleware(CorrelationIdMiddleware)
app.add_middleware(JWTReissueMiddleware)

# FastAPI (>=0.137) freezes a router's dependencies into each route at include time, so the
# trace-context dependency has to be supplied while the router tree is assembled. "unsafe-always"
# extracts on every request (before auth); "only-authenticated" extracts only after a successful
# require_auth; "never" extracts nothing.
mode = conf.get("execution_api", "otel_trace_propagation", fallback="only-authenticated")
_inject_trace_context_dep(execution_api_router.routes, mode)
trace_context_dep = Depends(_extract_w3c_trace_context)
execution_api_router = build_execution_api_router(
pre_auth_dependencies=[trace_context_dep] if mode == "unsafe-always" else (),
post_auth_dependencies=[trace_context_dep] if mode == "only-authenticated" else (),
)

app.generate_and_include_versioned_routers(execution_api_router)

Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -16,6 +16,9 @@
# under the License.
from __future__ import annotations

from collections.abc import Sequence
from typing import Any

from cadwyn import VersionedAPIRouter
from fastapi import APIRouter, Security

Expand All@@ -37,34 +40,57 @@
)
from airflow.api_fastapi.execution_api.security import require_auth

execution_api_router = APIRouter()
# health.router declares its full paths ("/health", "/health/ping") and is included without a
# prefix, unlike the routers below. A root route registered as @router.get("") under an include-time
# prefix=... raises "Prefix and path cannot be both empty" once FastAPI switched to lazy router
# inclusion (>=0.137); see https://github.com/apache/airflow/issues/68562. Don't reintroduce a prefix here.
execution_api_router.include_router(health.router, tags=["Health"])

# _Every_ single endpoint under here must be authenticated. Some do further checks on top of these
authenticated_router = VersionedAPIRouter(dependencies=[Security(require_auth)]) # type: ignore[list-item]
def build_execution_api_router(
*,
pre_auth_dependencies: Sequence[Any] = (),
post_auth_dependencies: Sequence[Any] = (),
) -> APIRouter:
"""
Assemble the Task Execution API router tree.

authenticated_router.include_router(assets.router, prefix="/assets", tags=["Assets"])
authenticated_router.include_router(asset_events.router, prefix="/asset-events", tags=["Asset Events"])
authenticated_router.include_router(
connection_tests.router, prefix="/connection-tests", tags=["Connection Tests"]
)
authenticated_router.include_router(connections.router, prefix="/connections", tags=["Connections"])
authenticated_router.include_router(dag_runs.router, prefix="/dag-runs", tags=["Dag Runs"])
authenticated_router.include_router(dags.router, prefix="/dags", tags=["Dags"])
authenticated_router.include_router(task_instances.router, prefix="/task-instances", tags=["Task Instances"])
authenticated_router.include_router(
task_reschedules.router, prefix="/task-reschedules", tags=["Task Reschedules"]
)
authenticated_router.include_router(variables.router, prefix="/variables", tags=["Variables"])
authenticated_router.include_router(xcoms.router, prefix="/xcoms", tags=["XComs"])
authenticated_router.include_router(hitl.router, prefix="/hitlDetails", tags=["Human in the Loop"])
authenticated_router.include_router(task_state_store.router, prefix="/store/ti", tags=["Task State Store"])
authenticated_router.include_router(
asset_state_store.router, prefix="/store/asset", tags=["Asset State Store"]
)
``pre_auth_dependencies`` run before authentication on every route (health included);
``post_auth_dependencies`` run after ``require_auth`` and therefore only for successfully
authenticated requests. They must be supplied here, at build time: FastAPI (>=0.137) snapshots a
router's dependencies into each route when ``include_router`` is called, so dependencies attached
afterwards to a shared, already-assembled router never take effect.
"""
# _Every_ single endpoint under here must be authenticated. Some do further checks on top of these.
authenticated_router = VersionedAPIRouter(
dependencies=[Security(require_auth), *post_auth_dependencies] # type: ignore[list-item]
)

authenticated_router.include_router(assets.router, prefix="/assets", tags=["Assets"])
authenticated_router.include_router(asset_events.router, prefix="/asset-events", tags=["Asset Events"])
authenticated_router.include_router(
connection_tests.router, prefix="/connection-tests", tags=["Connection Tests"]
)
authenticated_router.include_router(connections.router, prefix="/connections", tags=["Connections"])
authenticated_router.include_router(dag_runs.router, prefix="/dag-runs", tags=["Dag Runs"])
authenticated_router.include_router(dags.router, prefix="/dags", tags=["Dags"])
authenticated_router.include_router(
task_instances.router, prefix="/task-instances", tags=["Task Instances"]
)
authenticated_router.include_router(
task_reschedules.router, prefix="/task-reschedules", tags=["Task Reschedules"]
)
authenticated_router.include_router(variables.router, prefix="/variables", tags=["Variables"])
authenticated_router.include_router(xcoms.router, prefix="/xcoms", tags=["XComs"])
authenticated_router.include_router(hitl.router, prefix="/hitlDetails", tags=["Human in the Loop"])
authenticated_router.include_router(
task_state_store.router, prefix="/store/ti", tags=["Task State Store"]
)
authenticated_router.include_router(
asset_state_store.router, prefix="/store/asset", tags=["Asset State Store"]
)

execution_api_router.include_router(authenticated_router)
execution_api_router = APIRouter()
# health.router declares its full paths ("/health", "/health/ping") and is included without a
# prefix, unlike the routers above. A root route registered as @router.get("") under an include-time
# prefix=... raises "Prefix and path cannot be both empty" once FastAPI switched to lazy router
# inclusion (>=0.137); see https://github.com/apache/airflow/issues/68562. Don't reintroduce a prefix here.
execution_api_router.include_router(
health.router, tags=["Health"], dependencies=list(pre_auth_dependencies)
)
execution_api_router.include_router(authenticated_router, dependencies=list(pre_auth_dependencies))
return execution_api_router
14 changes: 0 additions & 14 deletions airflow-core/tests/unit/api_fastapi/execution_api/test_app.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -190,20 +190,6 @@ def test_multiple_requests_with_different_correlation_ids(self, client):
class TestTraceContextPropagation:
"""Exercise ``execution_api.otel_trace_propagation`` on the real Execution API app."""

@pytest.fixture(autouse=True)
def _restore_router_dependencies(self):
from airflow.api_fastapi.execution_api.routes import execution_api_router

snapshot = {
id(route): list(route.dependencies)
for route in execution_api_router.routes
if isinstance(route, APIRoute)
}
yield
for route in execution_api_router.routes:
if isinstance(route, APIRoute):
route.dependencies[:] = snapshot[id(route)]

@staticmethod
def _build_app(mode: str):
with conf_vars({("execution_api", "otel_trace_propagation"): mode}):
Expand Down
16 changes: 8 additions & 8 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' Restore FastAPI 0.137 support in the Task Execution API by Revanth14 · Pull Request #68826 · apache/airflow · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions airflow-core/pyproject.toml
Original file line numberDiff line numberDiff line change
Expand Up@@ -84,7 +84,7 @@ dependencies = [
"asgiref>=2.3.0; python_version < '3.14'",
"asgiref>=3.11.1; python_version >= '3.14'",
"attrs>=22.1.0, !=25.2.0",
"cadwyn>=6.1.1",
"cadwyn>=7.1.0",
"colorlog>=6.8.2",
"cron-descriptor>=1.2.24",
"croniter>=2.0.2",
Expand All@@ -95,11 +95,7 @@ dependencies = [
"cryptography>=44.0.3",
"deprecated>=1.2.13",
"dill>=0.2.2",
# Cap below 0.137.0: FastAPI 0.137 switched to lazy router inclusion, which breaks cadwyn's
# versioned router generation (RouterGenerationError) and fails api-server / dag-processor
# startup. Relax once cadwyn supports FastAPI 0.137. See
# https://github.com/apache/airflow/issues/68562
"fastapi[standard-no-fastapi-cloud-cli]>=0.129.0,<0.137.0",
"fastapi[standard-no-fastapi-cloud-cli]>=0.137.1",
"uvicorn>=0.37.0",
"starlette>=1.0.1",
"httpx>=0.25.0",
Expand Down
33 changes: 10 additions & 23 deletions airflow-core/src/airflow/api_fastapi/execution_api/app.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -259,30 +259,9 @@ async def _extract_w3c_trace_context(
otel_context.detach(token)


def _inject_trace_context_dep(routes, mode: str) -> None:
dep = Depends(_extract_w3c_trace_context)
for route in routes:
if not isinstance(route, APIRoute):
continue
# Idempotent: create_task_execution_api_app() runs more than once per process
# (cached_app + InProcessExecutionAPI), and execution_api_router is shared
# module state, so strip any prior injection first.
route.dependencies[:] = [
d for d in route.dependencies if getattr(d, "dependency", None) is not _extract_w3c_trace_context
]
match mode:
case "unsafe-always":
route.dependencies.insert(0, dep)
case "only-authenticated":
from airflow.api_fastapi.execution_api.security import require_auth

if any(getattr(d, "dependency", None) is require_auth for d in route.dependencies):
route.dependencies.append(dep)


def create_task_execution_api_app() -> FastAPI:
"""Create FastAPI app for task execution API."""
from airflow.api_fastapi.execution_api.routes import execution_api_router
from airflow.api_fastapi.execution_api.routes import build_execution_api_router
from airflow.api_fastapi.execution_api.versions import bundle
from airflow.configuration import conf

Expand All@@ -305,8 +284,16 @@ def custom_generate_unique_id(route: APIRoute):
app.add_middleware(CorrelationIdMiddleware)
app.add_middleware(JWTReissueMiddleware)

# FastAPI (>=0.137) freezes a router's dependencies into each route at include time, so the
# trace-context dependency has to be supplied while the router tree is assembled. "unsafe-always"
# extracts on every request (before auth); "only-authenticated" extracts only after a successful
# require_auth; "never" extracts nothing.
mode = conf.get("execution_api", "otel_trace_propagation", fallback="only-authenticated")
_inject_trace_context_dep(execution_api_router.routes, mode)
trace_context_dep = Depends(_extract_w3c_trace_context)
execution_api_router = build_execution_api_router(
pre_auth_dependencies=[trace_context_dep] if mode == "unsafe-always" else (),
post_auth_dependencies=[trace_context_dep] if mode == "only-authenticated" else (),
)

app.generate_and_include_versioned_routers(execution_api_router)

Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -16,6 +16,9 @@
# under the License.
from __future__ import annotations

from collections.abc import Sequence
from typing import Any

from cadwyn import VersionedAPIRouter
from fastapi import APIRouter, Security

Expand All@@ -37,34 +40,57 @@
)
from airflow.api_fastapi.execution_api.security import require_auth

execution_api_router = APIRouter()
# health.router declares its full paths ("/health", "/health/ping") and is included without a
# prefix, unlike the routers below. A root route registered as @router.get("") under an include-time
# prefix=... raises "Prefix and path cannot be both empty" once FastAPI switched to lazy router
# inclusion (>=0.137); see https://github.com/apache/airflow/issues/68562. Don't reintroduce a prefix here.
execution_api_router.include_router(health.router, tags=["Health"])

# _Every_ single endpoint under here must be authenticated. Some do further checks on top of these
authenticated_router = VersionedAPIRouter(dependencies=[Security(require_auth)]) # type: ignore[list-item]
def build_execution_api_router(
*,
pre_auth_dependencies: Sequence[Any] = (),
post_auth_dependencies: Sequence[Any] = (),
) -> APIRouter:
"""
Assemble the Task Execution API router tree.

authenticated_router.include_router(assets.router, prefix="/assets", tags=["Assets"])
authenticated_router.include_router(asset_events.router, prefix="/asset-events", tags=["Asset Events"])
authenticated_router.include_router(
connection_tests.router, prefix="/connection-tests", tags=["Connection Tests"]
)
authenticated_router.include_router(connections.router, prefix="/connections", tags=["Connections"])
authenticated_router.include_router(dag_runs.router, prefix="/dag-runs", tags=["Dag Runs"])
authenticated_router.include_router(dags.router, prefix="/dags", tags=["Dags"])
authenticated_router.include_router(task_instances.router, prefix="/task-instances", tags=["Task Instances"])
authenticated_router.include_router(
task_reschedules.router, prefix="/task-reschedules", tags=["Task Reschedules"]
)
authenticated_router.include_router(variables.router, prefix="/variables", tags=["Variables"])
authenticated_router.include_router(xcoms.router, prefix="/xcoms", tags=["XComs"])
authenticated_router.include_router(hitl.router, prefix="/hitlDetails", tags=["Human in the Loop"])
authenticated_router.include_router(task_state_store.router, prefix="/store/ti", tags=["Task State Store"])
authenticated_router.include_router(
asset_state_store.router, prefix="/store/asset", tags=["Asset State Store"]
)
``pre_auth_dependencies`` run before authentication on every route (health included);
``post_auth_dependencies`` run after ``require_auth`` and therefore only for successfully
authenticated requests. They must be supplied here, at build time: FastAPI (>=0.137) snapshots a
router's dependencies into each route when ``include_router`` is called, so dependencies attached
afterwards to a shared, already-assembled router never take effect.
"""
# _Every_ single endpoint under here must be authenticated. Some do further checks on top of these.
authenticated_router = VersionedAPIRouter(
dependencies=[Security(require_auth), *post_auth_dependencies] # type: ignore[list-item]
)

authenticated_router.include_router(assets.router, prefix="/assets", tags=["Assets"])
authenticated_router.include_router(asset_events.router, prefix="/asset-events", tags=["Asset Events"])
authenticated_router.include_router(
connection_tests.router, prefix="/connection-tests", tags=["Connection Tests"]
)
authenticated_router.include_router(connections.router, prefix="/connections", tags=["Connections"])
authenticated_router.include_router(dag_runs.router, prefix="/dag-runs", tags=["Dag Runs"])
authenticated_router.include_router(dags.router, prefix="/dags", tags=["Dags"])
authenticated_router.include_router(
task_instances.router, prefix="/task-instances", tags=["Task Instances"]
)
authenticated_router.include_router(
task_reschedules.router, prefix="/task-reschedules", tags=["Task Reschedules"]
)
authenticated_router.include_router(variables.router, prefix="/variables", tags=["Variables"])
authenticated_router.include_router(xcoms.router, prefix="/xcoms", tags=["XComs"])
authenticated_router.include_router(hitl.router, prefix="/hitlDetails", tags=["Human in the Loop"])
authenticated_router.include_router(
task_state_store.router, prefix="/store/ti", tags=["Task State Store"]
)
authenticated_router.include_router(
asset_state_store.router, prefix="/store/asset", tags=["Asset State Store"]
)

execution_api_router.include_router(authenticated_router)
execution_api_router = APIRouter()
# health.router declares its full paths ("/health", "/health/ping") and is included without a
# prefix, unlike the routers above. A root route registered as @router.get("") under an include-time
# prefix=... raises "Prefix and path cannot be both empty" once FastAPI switched to lazy router
# inclusion (>=0.137); see https://github.com/apache/airflow/issues/68562. Don't reintroduce a prefix here.
execution_api_router.include_router(
health.router, tags=["Health"], dependencies=list(pre_auth_dependencies)
)
execution_api_router.include_router(authenticated_router, dependencies=list(pre_auth_dependencies))
return execution_api_router
14 changes: 0 additions & 14 deletions airflow-core/tests/unit/api_fastapi/execution_api/test_app.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -190,20 +190,6 @@ def test_multiple_requests_with_different_correlation_ids(self, client):
class TestTraceContextPropagation:
"""Exercise ``execution_api.otel_trace_propagation`` on the real Execution API app."""

@pytest.fixture(autouse=True)
def _restore_router_dependencies(self):
from airflow.api_fastapi.execution_api.routes import execution_api_router

snapshot = {
id(route): list(route.dependencies)
for route in execution_api_router.routes
if isinstance(route, APIRoute)
}
yield
for route in execution_api_router.routes:
if isinstance(route, APIRoute):
route.dependencies[:] = snapshot[id(route)]

@staticmethod
def _build_app(mode: str):
with conf_vars({("execution_api", "otel_trace_propagation"): mode}):
Expand Down
16 changes: 8 additions & 8 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' Restore FastAPI 0.137 support in the Task Execution API by Revanth14 · Pull Request #68826 · apache/airflow · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions airflow-core/pyproject.toml
Original file line numberDiff line numberDiff line change
Expand Up@@ -84,7 +84,7 @@ dependencies = [
"asgiref>=2.3.0; python_version < '3.14'",
"asgiref>=3.11.1; python_version >= '3.14'",
"attrs>=22.1.0, !=25.2.0",
"cadwyn>=6.1.1",
"cadwyn>=7.1.0",
"colorlog>=6.8.2",
"cron-descriptor>=1.2.24",
"croniter>=2.0.2",
Expand All@@ -95,11 +95,7 @@ dependencies = [
"cryptography>=44.0.3",
"deprecated>=1.2.13",
"dill>=0.2.2",
# Cap below 0.137.0: FastAPI 0.137 switched to lazy router inclusion, which breaks cadwyn's
# versioned router generation (RouterGenerationError) and fails api-server / dag-processor
# startup. Relax once cadwyn supports FastAPI 0.137. See
# https://github.com/apache/airflow/issues/68562
"fastapi[standard-no-fastapi-cloud-cli]>=0.129.0,<0.137.0",
"fastapi[standard-no-fastapi-cloud-cli]>=0.137.1",
"uvicorn>=0.37.0",
"starlette>=1.0.1",
"httpx>=0.25.0",
Expand Down
33 changes: 10 additions & 23 deletions airflow-core/src/airflow/api_fastapi/execution_api/app.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -259,30 +259,9 @@ async def _extract_w3c_trace_context(
otel_context.detach(token)


def _inject_trace_context_dep(routes, mode: str) -> None:
dep = Depends(_extract_w3c_trace_context)
for route in routes:
if not isinstance(route, APIRoute):
continue
# Idempotent: create_task_execution_api_app() runs more than once per process
# (cached_app + InProcessExecutionAPI), and execution_api_router is shared
# module state, so strip any prior injection first.
route.dependencies[:] = [
d for d in route.dependencies if getattr(d, "dependency", None) is not _extract_w3c_trace_context
]
match mode:
case "unsafe-always":
route.dependencies.insert(0, dep)
case "only-authenticated":
from airflow.api_fastapi.execution_api.security import require_auth

if any(getattr(d, "dependency", None) is require_auth for d in route.dependencies):
route.dependencies.append(dep)


def create_task_execution_api_app() -> FastAPI:
"""Create FastAPI app for task execution API."""
from airflow.api_fastapi.execution_api.routes import execution_api_router
from airflow.api_fastapi.execution_api.routes import build_execution_api_router
from airflow.api_fastapi.execution_api.versions import bundle
from airflow.configuration import conf

Expand All@@ -305,8 +284,16 @@ def custom_generate_unique_id(route: APIRoute):
app.add_middleware(CorrelationIdMiddleware)
app.add_middleware(JWTReissueMiddleware)

# FastAPI (>=0.137) freezes a router's dependencies into each route at include time, so the
# trace-context dependency has to be supplied while the router tree is assembled. "unsafe-always"
# extracts on every request (before auth); "only-authenticated" extracts only after a successful
# require_auth; "never" extracts nothing.
mode = conf.get("execution_api", "otel_trace_propagation", fallback="only-authenticated")
_inject_trace_context_dep(execution_api_router.routes, mode)
trace_context_dep = Depends(_extract_w3c_trace_context)
execution_api_router = build_execution_api_router(
pre_auth_dependencies=[trace_context_dep] if mode == "unsafe-always" else (),
post_auth_dependencies=[trace_context_dep] if mode == "only-authenticated" else (),
)

app.generate_and_include_versioned_routers(execution_api_router)

Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -16,6 +16,9 @@
# under the License.
from __future__ import annotations

from collections.abc import Sequence
from typing import Any

from cadwyn import VersionedAPIRouter
from fastapi import APIRouter, Security

Expand All@@ -37,34 +40,57 @@
)
from airflow.api_fastapi.execution_api.security import require_auth

execution_api_router = APIRouter()
# health.router declares its full paths ("/health", "/health/ping") and is included without a
# prefix, unlike the routers below. A root route registered as @router.get("") under an include-time
# prefix=... raises "Prefix and path cannot be both empty" once FastAPI switched to lazy router
# inclusion (>=0.137); see https://github.com/apache/airflow/issues/68562. Don't reintroduce a prefix here.
execution_api_router.include_router(health.router, tags=["Health"])

# _Every_ single endpoint under here must be authenticated. Some do further checks on top of these
authenticated_router = VersionedAPIRouter(dependencies=[Security(require_auth)]) # type: ignore[list-item]
def build_execution_api_router(
*,
pre_auth_dependencies: Sequence[Any] = (),
post_auth_dependencies: Sequence[Any] = (),
) -> APIRouter:
"""
Assemble the Task Execution API router tree.

authenticated_router.include_router(assets.router, prefix="/assets", tags=["Assets"])
authenticated_router.include_router(asset_events.router, prefix="/asset-events", tags=["Asset Events"])
authenticated_router.include_router(
connection_tests.router, prefix="/connection-tests", tags=["Connection Tests"]
)
authenticated_router.include_router(connections.router, prefix="/connections", tags=["Connections"])
authenticated_router.include_router(dag_runs.router, prefix="/dag-runs", tags=["Dag Runs"])
authenticated_router.include_router(dags.router, prefix="/dags", tags=["Dags"])
authenticated_router.include_router(task_instances.router, prefix="/task-instances", tags=["Task Instances"])
authenticated_router.include_router(
task_reschedules.router, prefix="/task-reschedules", tags=["Task Reschedules"]
)
authenticated_router.include_router(variables.router, prefix="/variables", tags=["Variables"])
authenticated_router.include_router(xcoms.router, prefix="/xcoms", tags=["XComs"])
authenticated_router.include_router(hitl.router, prefix="/hitlDetails", tags=["Human in the Loop"])
authenticated_router.include_router(task_state_store.router, prefix="/store/ti", tags=["Task State Store"])
authenticated_router.include_router(
asset_state_store.router, prefix="/store/asset", tags=["Asset State Store"]
)
``pre_auth_dependencies`` run before authentication on every route (health included);
``post_auth_dependencies`` run after ``require_auth`` and therefore only for successfully
authenticated requests. They must be supplied here, at build time: FastAPI (>=0.137) snapshots a
router's dependencies into each route when ``include_router`` is called, so dependencies attached
afterwards to a shared, already-assembled router never take effect.
"""
# _Every_ single endpoint under here must be authenticated. Some do further checks on top of these.
authenticated_router = VersionedAPIRouter(
dependencies=[Security(require_auth), *post_auth_dependencies] # type: ignore[list-item]
)

authenticated_router.include_router(assets.router, prefix="/assets", tags=["Assets"])
authenticated_router.include_router(asset_events.router, prefix="/asset-events", tags=["Asset Events"])
authenticated_router.include_router(
connection_tests.router, prefix="/connection-tests", tags=["Connection Tests"]
)
authenticated_router.include_router(connections.router, prefix="/connections", tags=["Connections"])
authenticated_router.include_router(dag_runs.router, prefix="/dag-runs", tags=["Dag Runs"])
authenticated_router.include_router(dags.router, prefix="/dags", tags=["Dags"])
authenticated_router.include_router(
task_instances.router, prefix="/task-instances", tags=["Task Instances"]
)
authenticated_router.include_router(
task_reschedules.router, prefix="/task-reschedules", tags=["Task Reschedules"]
)
authenticated_router.include_router(variables.router, prefix="/variables", tags=["Variables"])
authenticated_router.include_router(xcoms.router, prefix="/xcoms", tags=["XComs"])
authenticated_router.include_router(hitl.router, prefix="/hitlDetails", tags=["Human in the Loop"])
authenticated_router.include_router(
task_state_store.router, prefix="/store/ti", tags=["Task State Store"]
)
authenticated_router.include_router(
asset_state_store.router, prefix="/store/asset", tags=["Asset State Store"]
)

execution_api_router.include_router(authenticated_router)
execution_api_router = APIRouter()
# health.router declares its full paths ("/health", "/health/ping") and is included without a
# prefix, unlike the routers above. A root route registered as @router.get("") under an include-time
# prefix=... raises "Prefix and path cannot be both empty" once FastAPI switched to lazy router
# inclusion (>=0.137); see https://github.com/apache/airflow/issues/68562. Don't reintroduce a prefix here.
execution_api_router.include_router(
health.router, tags=["Health"], dependencies=list(pre_auth_dependencies)
)
execution_api_router.include_router(authenticated_router, dependencies=list(pre_auth_dependencies))
return execution_api_router
14 changes: 0 additions & 14 deletions airflow-core/tests/unit/api_fastapi/execution_api/test_app.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -190,20 +190,6 @@ def test_multiple_requests_with_different_correlation_ids(self, client):
class TestTraceContextPropagation:
"""Exercise ``execution_api.otel_trace_propagation`` on the real Execution API app."""

@pytest.fixture(autouse=True)
def _restore_router_dependencies(self):
from airflow.api_fastapi.execution_api.routes import execution_api_router

snapshot = {
id(route): list(route.dependencies)
for route in execution_api_router.routes
if isinstance(route, APIRoute)
}
yield
for route in execution_api_router.routes:
if isinstance(route, APIRoute):
route.dependencies[:] = snapshot[id(route)]

@staticmethod
def _build_app(mode: str):
with conf_vars({("execution_api", "otel_trace_propagation"): mode}):
Expand Down
16 changes: 8 additions & 8 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Universal Dark Mode - works on any site (function() { var enabled = true; function applyDarkMode() { if (!enabled) return; // Create style element if it doesn't exist var style = document.getElementById('universal-dark-mode-style'); if (!style) { style = document.createElement('style'); style.id = 'universal-dark-mode-style'; document.head.appendChild(style); } // Dark mode CSS - inverts colors but preserves images/video style.textContent = ' /* Invert everything except media */ html { filter: invert(1) hue-rotate(180deg) !important; background: #1a1a2e !important; } /* Restore images, videos, iframes, canvas */ img, video, iframe, canvas, svg, picture, [style*="background-image"] { filter: invert(1) hue-rotate(180deg) !important; } /* Preserve specific elements that should not be inverted */ .no-dark-mode, .no-dark-mode *, [data-theme="light"], [data-theme="light"], .ace_editor, .ace_editor *, .CodeMirror, .CodeMirror *, .monaco-editor, .monaco-editor *, .markdown-body pre, .markdown-body pre *, .highlight, .highlight *, pre code, pre code * { filter: none !important; } /* Fix common UI elements */ .modal, .popup, .dropdown-menu, .tooltip, .popover { filter: invert(1) hue-rotate(180deg) !important; background: #2d2d44 !important; border-color: #444 !important; } /* Scrollbars */ ::-webkit-scrollbar { background: #1a1a2e !important; } ::-webkit-scrollbar-thumb { background: #444 !important; } ::-webkit-scrollbar-thumb:hover { background: #555 !important; } /* Selection */ ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; } ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; } '; } function removeDarkMode() { var style = document.getElementById('universal-dark-mode-style'); if (style) style.remove(); } // Toggle with Alt+Shift+D document.addEventListener('keydown', function(e) { if (e.altKey && e.shiftKey && e.key === 'D') { e.preventDefault(); enabled = !enabled; if (enabled) { applyDarkMode(); console.log('[Universal Dark Mode] Enabled'); } else { removeDarkMode(); console.log('[Universal Dark Mode] Disabled'); } } }); // Apply on load applyDarkMode(); // Re-apply on dynamic content var observer = new MutationObserver(function(mutations) { if (enabled && !document.getElementById('universal-dark-mode-style')) { applyDarkMode(); } }); observer.observe(document.head, { childList: true }); console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle'); })(); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })(); Restore FastAPI 0.137 support in the Task Execution API by Revanth14 · Pull Request #68826 · apache/airflow · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions airflow-core/pyproject.toml
Original file line numberDiff line numberDiff line change
Expand Up@@ -84,7 +84,7 @@ dependencies = [
"asgiref>=2.3.0; python_version < '3.14'",
"asgiref>=3.11.1; python_version >= '3.14'",
"attrs>=22.1.0, !=25.2.0",
"cadwyn>=6.1.1",
"cadwyn>=7.1.0",
"colorlog>=6.8.2",
"cron-descriptor>=1.2.24",
"croniter>=2.0.2",
Expand All@@ -95,11 +95,7 @@ dependencies = [
"cryptography>=44.0.3",
"deprecated>=1.2.13",
"dill>=0.2.2",
# Cap below 0.137.0: FastAPI 0.137 switched to lazy router inclusion, which breaks cadwyn's
# versioned router generation (RouterGenerationError) and fails api-server / dag-processor
# startup. Relax once cadwyn supports FastAPI 0.137. See
# https://github.com/apache/airflow/issues/68562
"fastapi[standard-no-fastapi-cloud-cli]>=0.129.0,<0.137.0",
"fastapi[standard-no-fastapi-cloud-cli]>=0.137.1",
"uvicorn>=0.37.0",
"starlette>=1.0.1",
"httpx>=0.25.0",
Expand Down
33 changes: 10 additions & 23 deletions airflow-core/src/airflow/api_fastapi/execution_api/app.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -259,30 +259,9 @@ async def _extract_w3c_trace_context(
otel_context.detach(token)


def _inject_trace_context_dep(routes, mode: str) -> None:
dep = Depends(_extract_w3c_trace_context)
for route in routes:
if not isinstance(route, APIRoute):
continue
# Idempotent: create_task_execution_api_app() runs more than once per process
# (cached_app + InProcessExecutionAPI), and execution_api_router is shared
# module state, so strip any prior injection first.
route.dependencies[:] = [
d for d in route.dependencies if getattr(d, "dependency", None) is not _extract_w3c_trace_context
]
match mode:
case "unsafe-always":
route.dependencies.insert(0, dep)
case "only-authenticated":
from airflow.api_fastapi.execution_api.security import require_auth

if any(getattr(d, "dependency", None) is require_auth for d in route.dependencies):
route.dependencies.append(dep)


def create_task_execution_api_app() -> FastAPI:
"""Create FastAPI app for task execution API."""
from airflow.api_fastapi.execution_api.routes import execution_api_router
from airflow.api_fastapi.execution_api.routes import build_execution_api_router
from airflow.api_fastapi.execution_api.versions import bundle
from airflow.configuration import conf

Expand All@@ -305,8 +284,16 @@ def custom_generate_unique_id(route: APIRoute):
app.add_middleware(CorrelationIdMiddleware)
app.add_middleware(JWTReissueMiddleware)

# FastAPI (>=0.137) freezes a router's dependencies into each route at include time, so the
# trace-context dependency has to be supplied while the router tree is assembled. "unsafe-always"
# extracts on every request (before auth); "only-authenticated" extracts only after a successful
# require_auth; "never" extracts nothing.
mode = conf.get("execution_api", "otel_trace_propagation", fallback="only-authenticated")
_inject_trace_context_dep(execution_api_router.routes, mode)
trace_context_dep = Depends(_extract_w3c_trace_context)
execution_api_router = build_execution_api_router(
pre_auth_dependencies=[trace_context_dep] if mode == "unsafe-always" else (),
post_auth_dependencies=[trace_context_dep] if mode == "only-authenticated" else (),
)

app.generate_and_include_versioned_routers(execution_api_router)

Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -16,6 +16,9 @@
# under the License.
from __future__ import annotations

from collections.abc import Sequence
from typing import Any

from cadwyn import VersionedAPIRouter
from fastapi import APIRouter, Security

Expand All@@ -37,34 +40,57 @@
)
from airflow.api_fastapi.execution_api.security import require_auth

execution_api_router = APIRouter()
# health.router declares its full paths ("/health", "/health/ping") and is included without a
# prefix, unlike the routers below. A root route registered as @router.get("") under an include-time
# prefix=... raises "Prefix and path cannot be both empty" once FastAPI switched to lazy router
# inclusion (>=0.137); see https://github.com/apache/airflow/issues/68562. Don't reintroduce a prefix here.
execution_api_router.include_router(health.router, tags=["Health"])

# _Every_ single endpoint under here must be authenticated. Some do further checks on top of these
authenticated_router = VersionedAPIRouter(dependencies=[Security(require_auth)]) # type: ignore[list-item]
def build_execution_api_router(
*,
pre_auth_dependencies: Sequence[Any] = (),
post_auth_dependencies: Sequence[Any] = (),
) -> APIRouter:
"""
Assemble the Task Execution API router tree.

authenticated_router.include_router(assets.router, prefix="/assets", tags=["Assets"])
authenticated_router.include_router(asset_events.router, prefix="/asset-events", tags=["Asset Events"])
authenticated_router.include_router(
connection_tests.router, prefix="/connection-tests", tags=["Connection Tests"]
)
authenticated_router.include_router(connections.router, prefix="/connections", tags=["Connections"])
authenticated_router.include_router(dag_runs.router, prefix="/dag-runs", tags=["Dag Runs"])
authenticated_router.include_router(dags.router, prefix="/dags", tags=["Dags"])
authenticated_router.include_router(task_instances.router, prefix="/task-instances", tags=["Task Instances"])
authenticated_router.include_router(
task_reschedules.router, prefix="/task-reschedules", tags=["Task Reschedules"]
)
authenticated_router.include_router(variables.router, prefix="/variables", tags=["Variables"])
authenticated_router.include_router(xcoms.router, prefix="/xcoms", tags=["XComs"])
authenticated_router.include_router(hitl.router, prefix="/hitlDetails", tags=["Human in the Loop"])
authenticated_router.include_router(task_state_store.router, prefix="/store/ti", tags=["Task State Store"])
authenticated_router.include_router(
asset_state_store.router, prefix="/store/asset", tags=["Asset State Store"]
)
``pre_auth_dependencies`` run before authentication on every route (health included);
``post_auth_dependencies`` run after ``require_auth`` and therefore only for successfully
authenticated requests. They must be supplied here, at build time: FastAPI (>=0.137) snapshots a
router's dependencies into each route when ``include_router`` is called, so dependencies attached
afterwards to a shared, already-assembled router never take effect.
"""
# _Every_ single endpoint under here must be authenticated. Some do further checks on top of these.
authenticated_router = VersionedAPIRouter(
dependencies=[Security(require_auth), *post_auth_dependencies] # type: ignore[list-item]
)

authenticated_router.include_router(assets.router, prefix="/assets", tags=["Assets"])
authenticated_router.include_router(asset_events.router, prefix="/asset-events", tags=["Asset Events"])
authenticated_router.include_router(
connection_tests.router, prefix="/connection-tests", tags=["Connection Tests"]
)
authenticated_router.include_router(connections.router, prefix="/connections", tags=["Connections"])
authenticated_router.include_router(dag_runs.router, prefix="/dag-runs", tags=["Dag Runs"])
authenticated_router.include_router(dags.router, prefix="/dags", tags=["Dags"])
authenticated_router.include_router(
task_instances.router, prefix="/task-instances", tags=["Task Instances"]
)
authenticated_router.include_router(
task_reschedules.router, prefix="/task-reschedules", tags=["Task Reschedules"]
)
authenticated_router.include_router(variables.router, prefix="/variables", tags=["Variables"])
authenticated_router.include_router(xcoms.router, prefix="/xcoms", tags=["XComs"])
authenticated_router.include_router(hitl.router, prefix="/hitlDetails", tags=["Human in the Loop"])
authenticated_router.include_router(
task_state_store.router, prefix="/store/ti", tags=["Task State Store"]
)
authenticated_router.include_router(
asset_state_store.router, prefix="/store/asset", tags=["Asset State Store"]
)

execution_api_router.include_router(authenticated_router)
execution_api_router = APIRouter()
# health.router declares its full paths ("/health", "/health/ping") and is included without a
# prefix, unlike the routers above. A root route registered as @router.get("") under an include-time
# prefix=... raises "Prefix and path cannot be both empty" once FastAPI switched to lazy router
# inclusion (>=0.137); see https://github.com/apache/airflow/issues/68562. Don't reintroduce a prefix here.
execution_api_router.include_router(
health.router, tags=["Health"], dependencies=list(pre_auth_dependencies)
)
execution_api_router.include_router(authenticated_router, dependencies=list(pre_auth_dependencies))
return execution_api_router
14 changes: 0 additions & 14 deletions airflow-core/tests/unit/api_fastapi/execution_api/test_app.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -190,20 +190,6 @@ def test_multiple_requests_with_different_correlation_ids(self, client):
class TestTraceContextPropagation:
"""Exercise ``execution_api.otel_trace_propagation`` on the real Execution API app."""

@pytest.fixture(autouse=True)
def _restore_router_dependencies(self):
from airflow.api_fastapi.execution_api.routes import execution_api_router

snapshot = {
id(route): list(route.dependencies)
for route in execution_api_router.routes
if isinstance(route, APIRoute)
}
yield
for route in execution_api_router.routes:
if isinstance(route, APIRoute):
route.dependencies[:] = snapshot[id(route)]

@staticmethod
def _build_app(mode: str):
with conf_vars({("execution_api", "otel_trace_propagation"): mode}):
Expand Down
16 changes: 8 additions & 8 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading