Skip to content

Fix airflowctl ignoring --env for non-auth commands - #73808

Merged
shahar1 merged 6 commits into
apache:mainfrom
liwenjie200543:fix/ctl-env
Oct 27, 2026
Merged

shahar1 merged 6 commits into
apache:mainfrom
liwenjie200543:fix/ctl-env

Conversation

@liwenjie200543

Copy link
Copy Markdown
Contributor

provide_api_client forwarded only api_token from the parsed args namespace, so every @provide_api_client-decorated command resolved credentials against the production environment no matter what --env the user passed. version --remote, which calls get_client directly, ignored --env the same way even though the flag is attached to the command (cli_config.py:1313). Net effect: airflowctl dags list --env staging silently used production credentials — the failure mode described in the issue.

closes: #70519

What changed

  • get_client() gains api_environment and forwards it to Credentials, whose own contract (AIRFLOW_CLI_ENVIRONMENT beats the explicit value) is preserved.
  • The provide_api_client wrapper reads env off the parsed args namespace, falling back to "production" — the same default ARG_AUTH_ENVIRONMENT and Credentials use, so callers that never pass --env are unaffected.
  • version_command.version_info forwards --env for its direct get_client call.

This supersedes #70584, which was closed unmerged under the 5-open-PR limit rather than for its approach; the decorator and version --remote paths are now both covered by tests.

Tests

  • TestGetClientEnvironment (airflow-ctl/tests/airflow_ctl/api/test_client.py): default stays production; explicit api_environment="staging" resolves staging.json; same for NO_AUTH; AIRFLOW_CLI_ENVIRONMENT keeps precedence; the decorator forwards env from the args namespace.
  • test_version_command.py: the two mocked-call assertions updated for the new kwarg, plus a new test_ctl_version_remote_with_env covering --env staging.
  • Red/green: the four environment tests fail on main and pass on this branch (verified by stashing the source fix while keeping the tests).

Was generative AI tooling used to co-author this PR?
  • Yes (please specify the tool below)

Generated-by: [ZCode (GLM-5.3-Flash)] following the guidelines

provide_api_client forwarded only api_token from the parsed args, so
every decorated command resolved credentials for the production
environment regardless of --env, and version --remote, which calls
get_client directly, behaved the same. Pass the parsed env through
get_client into Credentials, which still honours AIRFLOW_CLI_ENVIRONMENT
over the explicit value.

closes: apache#70519
@boring-cyborg

boring-cyborg Bot commented Sep 27, 2026

Copy link
Copy Markdown

Congratulations on your first Pull Request and welcome to the Apache Airflow community! If you have any issues or are unsure about any anything please check our Contributors' Guide
Here are some useful points:

  • Pay attention to the quality of your code (ruff, mypy and type annotations). Our prek-hooks will help you with that.
  • In case of a new feature add useful documentation (in docstrings or in docs/ directory). Adding a new operator? Check this short guide Consider adding an example Dag that shows how users should use it.
  • Consider using Breeze environment for testing locally, it's a heavy docker but it ships with a working Airflow and a lot of integrations.
  • Be patient and persistent. It might take some time to get a review or get the final approval from Committers.
  • Please follow ASF Code of Conduct for all communication including (but not limited to) comments on Pull Requests, Mailing list and Slack.
  • Be sure to read the Airflow Coding style.
  • Always keep your Pull Requests rebased, otherwise your build might fail due to changes not related to your commits.
    Apache Airflow is a community-driven project and together we are making it better 🚀.
    In case of doubts contact the developers at:
    Mailing List: [email protected]
    Slack: https://s.apache.org/airflow-slack

@liwenjie200543

Copy link
Copy Markdown
Contributor Author

Hi! This PR closes #70519 — airflowctl was ignoring --env on non-auth commands and silently resolving credentials against the production environment; the fix covers both the provide_api_client wrapper and the version --remote direct-call path, with red/green-verified tests.

It's my first PR to Airflow, so the CI workflows are queued waiting for approval — could a committer please approve the workflow run so the checks can execute? Any feedback on the approach is also very welcome. Thank you!

Assert the parsed hostname instead of substring membership so the
host under test is pinned exactly and CodeQL's
python/url-substring-sanitization no longer flags the assertions.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Test imports violate repository checks, and newly added tests contain prohibited issue references and non-behavioral coverage.

Review effort: Balanced
Findings: 1 Medium severity · 5 Low severity

Open (6)
What changed in this PR

Fixes airflowctl environment selection so non-auth commands use credentials for the requested environment.

Changes:

  • Propagates --env through API-client creation.
  • Updates remote version lookup.
  • Adds regression coverage for environment resolution.
File Description
airflow-ctl/​src/​airflowctl/​api/​client.py Forwards environment selection to credentials.
airflow-ctl/​src/​airflowctl/​ctl/​commands/​version_command.py Applies environment selection to remote version requests.
airflow-ctl/​tests/​airflow_ctl/​api/​test_client.py Tests client and decorator environment handling.
airflow-ctl/​tests/​airflow_ctl/​ctl/​commands/​test_version_command.py Tests remote version environment forwarding.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

liwenjie200543 and others added 4 commits October 1, 2026 14:38
- Move SimpleNamespace and provide_api_client imports to the module top
  (imports at top of file, no test-body imports).
- Drop the GitHub issue reference and narrating docstrings from tests.
- Remove test_defaults_to_production: it covers the pre-existing
  production default, which the testing standards exclude (every test
  must fail without the PR's change).
# Conflicts:
#	airflow-ctl/tests/airflow_ctl/api/test_client.py
@shahar1
shahar1 merged commit aee26d4 into apache:main Oct 27, 2026
87 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

Backport successfully created: airflow-ctl/v0-1-test

Note: As of Merging PRs targeted for Airflow 3.X
the committer who merges the PR is responsible for backporting the PRs that are bug fixes (generally speaking) to the maintenance branches.

In matter of doubt please ask in #release-management Slack channel.

Status Branch Result
✅ airflow-ctl/v0-1-test PR Link

github-actions Bot pushed a commit to aws-mwaa/upstream-to-airflow that referenced this pull request Oct 27, 2026
…mmands (apache#73808)

Honor --env when resolving airflowctl API credentials

Forward the selected environment through decorated commands and version --remote, so staging requests use staging credentials.
(cherry picked from commit aee26d4)

Co-authored-by: kuse <[email protected]>
shahar1 pushed a commit that referenced this pull request Oct 27, 2026
…mmands (#73808) (#75746)

Honor --env when resolving airflowctl API credentials

Forward the selected environment through decorated commands and version --remote, so staging requests use staging credentials.
(cherry picked from commit aee26d4)

Co-authored-by: kuse <[email protected]>
shahar1 pushed a commit that referenced this pull request Nov 19, 2026
…mmands (#73808) (#75746)

Honor --env when resolving airflowctl API credentials

Forward the selected environment through decorated commands and version --remote, so staging requests use staging credentials.
(cherry picked from commit aee26d4)

Co-authored-by: kuse <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

airflowctl ignores --env/AIRFLOW_CLI_ENVIRONMENT on non-auth commands — always uses production credentials

4 participants