-
Notifications
You must be signed in to change notification settings - Fork 17.3k
[AIP-94] Create a CLI airflowctl client and adopt it in existing commands #68175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
89c4a9f
Route airflow dags/pools/assets CLI commands through the API server v…
bugraoz93 929517f
Add auth-manager get_cli_user tests and tidy CLI migration newsfragment
bugraoz93 b4f88f3
Rename significant with PR number
bugraoz93 4c591db
Add deprecation warnings with airflowctl command replacement
bugraoz93 755af2a
Fix failure when the auth manager isn't initialized
bugraoz93 efd0694
Clarify deprecation wording for migrated airflow CLI commands
bugraoz93 686dafb
Fix airflow assets list with watchers and CLI migration test failures
bugraoz93 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| Airflow CLI commands are moving to talk to the API server | ||
|
|
||
| The CLI is being migrated to reach Airflow through the API server (via the ``airflowctl`` | ||
| client) instead of the metadata database directly. Migrated so far: ``dags trigger``, | ||
| ``dags delete``, ``pools`` (list/get/set/delete/import/export), and ``assets materialize``; | ||
| this fragment is updated as more commands migrate rather than adding new ones. | ||
|
|
||
| These commands now require a reachable API server and mint a short-lived token in memory | ||
| (set ``AIRFLOW_CLI_TOKEN`` for auth managers that cannot mint locally, or for remote servers). | ||
| ``airflow.api.client`` is removed — use ``airflow.cli.api_client.get_cli_api_client``. | ||
|
|
||
| Each migrated command emits a ``RemovedInAirflow4Warning`` and will be removed in a future | ||
| Airflow release; use the equivalent ``airflowctl`` command instead. | ||
|
|
||
| * Types of change | ||
|
|
||
| * [ ] Dag changes | ||
| * [ ] Config changes | ||
| * [ ] API changes | ||
| * [ ] CLI changes | ||
| * [x] Behaviour changes | ||
| * [ ] Plugin changes | ||
| * [ ] Dependency changes | ||
| * [x] Code interface changes |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| # | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| """ | ||
| Provide the :mod:`airflowctl` HTTP API client to the local Airflow CLI. | ||
|
|
||
| The local CLI talks to the API server through the same typed client that ``airflowctl`` | ||
| uses, but without the keyring-backed credential store. For each invocation it mints a | ||
| short-lived JWT **in memory** (via the active auth manager) and builds a client with it; | ||
| nothing is persisted. Set the ``AIRFLOW_CLI_TOKEN`` environment variable to supply a token | ||
| explicitly (required for auth managers whose tokens cannot be minted locally, such as | ||
| Keycloak, or when targeting a remote API server). | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import atexit | ||
| import os | ||
| from collections.abc import Callable | ||
| from functools import wraps | ||
| from typing import TYPE_CHECKING, TypeVar | ||
|
|
||
| import httpx | ||
|
|
||
| # Re-exported so command modules import the client surface from a single place. | ||
| from airflowctl.api.client import NEW_API_CLIENT, Client, ClientKind | ||
|
|
||
| from airflow.configuration import conf | ||
| from airflow.typing_compat import ParamSpec | ||
|
|
||
| if TYPE_CHECKING: | ||
| from airflow.api_fastapi.auth.managers.base_auth_manager import BaseAuthManager | ||
|
|
||
| __all__ = [ | ||
| "NEW_API_CLIENT", | ||
| "Client", | ||
| "ClientKind", | ||
| "get_cli_api_client", | ||
| "provide_api_client", | ||
| ] | ||
|
|
||
| PS = ParamSpec("PS") | ||
| RT = TypeVar("RT") | ||
|
|
||
| # Validity of the in-memory CLI token. It only needs to outlive a single CLI command | ||
| # (including the client's request retries) and is never persisted or logged. | ||
| _CLI_TOKEN_VALID_FOR_SECONDS = 300 | ||
|
|
||
| _api_client: Client | None = None | ||
|
|
||
|
|
||
| def _resolve_base_url() -> str: | ||
| """Resolve the API server base URL from configuration.""" | ||
| base_url = conf.get("api", "base_url", fallback=None) | ||
| if base_url: | ||
| return base_url | ||
| host = conf.get("api", "host", fallback="localhost") or "localhost" | ||
| port = conf.get("api", "port", fallback="8080") or "8080" | ||
| return f"http://{host}:{port}" | ||
|
|
||
|
|
||
| def _mint_cli_token() -> str: | ||
| """ | ||
| Return a token for the CLI to authenticate against the API server. | ||
|
|
||
| Prefers an explicit ``AIRFLOW_CLI_TOKEN`` (the universal override), otherwise mints a | ||
| short-lived JWT through the active auth manager. The token lives only in this process. | ||
| """ | ||
| if token := os.environ.get("AIRFLOW_CLI_TOKEN"): | ||
| return token | ||
|
|
||
| from airflow.api_fastapi.app import get_auth_manager, init_auth_manager | ||
|
|
||
| # The CLI runs outside the API server, so the auth manager singleton is usually not | ||
| # initialized yet; initialize it on demand. ``init_auth_manager`` reuses the cached | ||
| # instance when one already exists, so this is safe to call here. | ||
| try: | ||
| auth_manager: BaseAuthManager = get_auth_manager() | ||
| except RuntimeError: | ||
| auth_manager = init_auth_manager() | ||
| return auth_manager.generate_jwt( | ||
| auth_manager.get_cli_user(), | ||
| expiration_time_in_seconds=_CLI_TOKEN_VALID_FOR_SECONDS, | ||
| ) | ||
|
|
||
|
|
||
| def get_cli_api_client() -> Client: | ||
| """Return the process-wide singleton airflowctl client for the local CLI.""" | ||
| global _api_client | ||
| if _api_client is None: | ||
| _api_client = Client( | ||
| base_url=_resolve_base_url(), | ||
| token=_mint_cli_token(), | ||
| kind=ClientKind.CLI, | ||
| limits=httpx.Limits(max_keepalive_connections=1, max_connections=1), | ||
| ) | ||
| atexit.register(_api_client.close) | ||
| return _api_client | ||
|
|
||
|
|
||
| def provide_api_client(func: Callable[PS, RT]) -> Callable[PS, RT]: | ||
| """ | ||
| Provide the CLI API client to the decorated command function. | ||
|
|
||
| Injects ``api_client=get_cli_api_client()`` when the caller does not pass one. Tests | ||
| (or callers that already hold a client) pass ``api_client=`` explicitly to bypass it. | ||
| """ | ||
|
|
||
| @wraps(func) | ||
| def wrapper(*args, **kwargs) -> RT: | ||
| if "api_client" not in kwargs: | ||
| kwargs["api_client"] = get_cli_api_client() | ||
| return func(*args, **kwargs) | ||
|
|
||
| return wrapper | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.