Skip to content

Keep airflowctl auth token stdout limited to the token - #76134

Merged
shahar1 merged 2 commits into
apache:mainfrom
Shawnsuun:airflowctl-auth-token-stdout
Nov 1, 2026
Merged

shahar1 merged 2 commits into
apache:mainfrom
Shawnsuun:airflowctl-auth-token-stdout

Conversation

@Shawnsuun

Copy link
Copy Markdown
Contributor

airflowctl auth token prints a JWT to stdout so it can be captured, e.g.
TOKEN=$(airflowctl auth token --api-url ...). Three things got in the way of that:

  • The username prompt used input("Username: "), which writes to stdout. When stdout was
    captured, the user never saw the prompt, and the prompt text ended up in TOKEN.
  • Without a TTY (CI, cron, </dev/null) and without --username/--password, the command
    crashed with an EOFError traceback.
  • Token generation failed: ... went to stdout.

Changes:

  • The username prompt is written to stderr. The password prompt already uses getpass.
  • If --username or --password is missing and stdin is not a TTY, the command exits 1 with a
    clear message on stderr. airflowctl auth login already does this check.
  • The failure message goes to stderr.

When the username and password are passed as flags, nothing changes. The integration test command
auth token --username ... --password ... is unaffected.

Behaviour change to note: with no TTY, a missing password now gives a clear error. Before, getpass
fell back to reading stdin (with a GetPassWarning) only when no controlling terminal existed.
This matches auth login, which already refuses that case.

Tests: interactive prompt goes to stderr and stdout holds only the token; missing credentials
without a TTY (none / username only / password only) exit 1 with empty stdout and a clear stderr
message, and never prompt; a failed token request is reported on stderr. As with every command,
the client's generic Server error structlog warning on an API error still goes to stdout. That
is left for a separate change.


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

Generated-by: GitHub Copilot (Claude Opus 5.5) following the guidelines


  • Read the Pull Request Guidelines for more information. Note: commit author/co-author name and email in commits become permanently public when merged.
  • For fundamental code changes, an Airflow Improvement Proposal (AIP) is needed.
  • When adding dependency, check compliance with the ASF 3rd Party License Policy.
  • For significant user-facing changes create newsfragment: {pr_number}.significant.rst, in airflow-core/newsfragments. You can add this file in a follow-up commit after the PR is created so you know the PR number.

`airflowctl auth token` is meant to be captured, e.g.
`TOKEN=$(airflowctl auth token ...)`. The username prompt was written to
stdout via input(), so in that usage the prompt was invisible to the user
and ended up inside the captured value; the failure message also went to
stdout. Without a TTY and without credentials, the command crashed with
an EOFError traceback instead of a clear error.

Prompts and errors now go to stderr, and missing credentials in a
non-interactive context exit with a clear message, mirroring what
`airflowctl auth login` already does.
@Shawnsuun
Shawnsuun force-pushed the airflowctl-auth-token-stdout branch from f6a6a9f to 0313299 Compare October 31, 2026 13:06

@shahar1 shahar1 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.

Moving prompts and errors to stderr is the right fix and the tests pin it well; one behaviour change in the non-TTY path is worth a decision before merge.

Non-interactive runs can no longer feed the password on stdin (airflow-ctl/src/airflowctl/ctl/commands/auth_command.py:124)

The new if not sys.stdin.isatty(): ... sys.exit(1) guard runs before getpass, which itself already copes without a TTY on stdin: unix_getpass reads from /dev/tty when there is one, and otherwise falls back to reading the line from sys.stdin (with a GetPassWarning on stderr). Two invocations that returned a token before now exit 1 with "No credentials provided.":

  • No controlling terminal, password piped in — echo "$PW" | airflowctl auth token --api-url ... --username u in CI, docker exec -i, kubectl exec -i, ssh -T.
  • Terminal present, stdin redirected — --username u </dev/null, or inside a while read / xargs loop; before, getpass prompted on /dev/tty.

After this, the only non-interactive option is --password on argv, where the secret shows up in ps / /proc/<pid>/cmdline and shell history — so the first case removes the safer way to script this. (I see the guard mirrors the one in login, so this may be intentional; if so, a note in the help text would help.)

If it isn't intended, guarding only the username prompt keeps all the stdout fixes in this PR and restores both paths; it also turns a closed stdin (<&-, where sys.stdin is None) into a clean error instead of an AttributeError:

if not username:
    if not (sys.stdin and sys.stdin.isatty()):
        ...  # current "No credentials provided" message to stderr, sys.exit(1)
    sys.stderr.write("Username: ")
    sys.stderr.flush()
    username = input()
if not password:
    try:
        password = getpass.getpass("Password: ")  # /dev/tty first, then stdin
    except EOFError:
        ...  # "No password provided" to stderr, sys.exit(1)

A test with --username and the password piped on a non-TTY stdin would lock that in (the current username-only case asserts the exit).

Smaller observations

  • airflow-ctl/src/airflowctl/ctl/cli_config.py (the token command description, not in this diff): it still says "Username and password are prompted interactively if not provided." With this PR that is only true on a TTY; worth appending that non-interactive runs must pass the credentials, so --help matches the new error.
  • airflow-ctl/tests/airflow_ctl/ctl/commands/test_auth_command.py:320 (nit): the repo guidance says "Prefer @mock.patch decorators over with mock.patch(...) context managers for patching." The builtins.input / getpass.getpass patches don't vary per case and could be decorators; the sys.stdin patch varies by is_tty, so the context manager is fine there.

Drafted-by: Claude Code (Opus 5.5); reviewed by @shahar1 before posting


This review was drafted by an AI-assisted tool and
confirmed by an Apache Airflow maintainer. The findings
below are observations, not blockers; an Apache Airflow
maintainer — a real person — will take the next look at the
PR. If you think a finding is mis-applied, please reply on
the PR and a maintainer will weigh in.

More on how Apache Airflow handles maintainer review:
Contributing guide.

Only refuse to run when the username is missing and stdin is not a
TTY (or is closed); the password still goes through getpass, which
reads from /dev/tty when available and falls back to stdin. This keeps
'echo "$PW" | airflowctl auth token --username u' and prompts on
/dev/tty with redirected stdin working, so scripts need not put the
password on argv. EOF while reading the password exits 1 with a clear
stderr message. Document the non-interactive options in the help text.

@shahar1 shahar1 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.

Thanks for the quick turnaround. All three points from my earlier review are addressed. The non-TTY guard now covers only the username prompt, so piping the password (or the /dev/tty prompt with stdin redirected) works again, and each path has a test. The help text documents the non-interactive options, and the test patches are decorators.

One optional nit, not blocking: if stdin is closed (<&-), there's no controlling terminal and --username is passed without a password, getpass falls back to sys.stdin.readline() on None and raises AttributeError rather than EOFError, so that case still shows a traceback. Catching (EOFError, AttributeError) would cover it. That's rare enough to leave as is.

Drafted-by: Claude Code (Opus 5.5); reviewed by shahar1 before posting


This review was drafted by an AI-assisted tool and
confirmed by an Apache Airflow maintainer. The maintainer
approving this PR has read the findings and signed off. If
something feels off, please reply on the PR and a maintainer
will follow up.

More on how Apache Airflow handles maintainer review:
Contributing guide.

@shahar1
shahar1 merged commit 283ab98 into apache:main Nov 1, 2026
91 checks passed
@github-actions

github-actions Bot commented Nov 1, 2026

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

shahar1 pushed a commit that referenced this pull request Nov 1, 2026
…the token (#76134) (#76195)

* Keep airflowctl auth token stdout limited to the token

`airflowctl auth token` is meant to be captured, e.g.
`TOKEN=$(airflowctl auth token ...)`. The username prompt was written to
stdout via input(), so in that usage the prompt was invisible to the user
and ended up inside the captured value; the failure message also went to
stdout. Without a TTY and without credentials, the command crashed with
an EOFError traceback instead of a clear error.

Prompts and errors now go to stderr, and missing credentials in a
non-interactive context exit with a clear message, mirroring what
`airflowctl auth login` already does.

* Let airflowctl auth token read a piped password without a TTY

Only refuse to run when the username is missing and stdin is not a
TTY (or is closed); the password still goes through getpass, which
reads from /dev/tty when available and falls back to stdin. This keeps
'echo "$PW" | airflowctl auth token --username u' and prompts on
/dev/tty with redirected stdin working, so scripts need not put the
password on argv. EOF while reading the password exits 1 with a clear
stderr message. Document the non-interactive options in the help text.
(cherry picked from commit 283ab98)

Co-authored-by: Shawn Sun <[email protected]>
@Shawnsuun

Copy link
Copy Markdown
Contributor Author

@shahar1 Thanks for the quick re-review. Agreed on the closed-stdin nit: it needs both a closed stdin and no controlling terminal, so I'll leave it as is here rather than widen the except.


Drafted-by: GitHub Copilot (Claude Opus 5.5); reviewed by @Shawnsuun before posting

shahar1 pushed a commit that referenced this pull request Nov 3, 2026
…76347)

The API client logs "Server error" and the tenacity retry warning
through structlog, which prints to stdout when nothing configures it.
A single retried 5xx then puts two log lines in front of the
`-o json` output, while the command still exits 0. The shared error
handler also printed its messages to stdout. #76133 and #76134 moved
the messages of a few commands to stderr, but these two paths are
shared by every command.
shahar1 pushed a commit that referenced this pull request Nov 3, 2026
…st is retried or fails (#76347) (#76380)

The API client logs "Server error" and the tenacity retry warning
through structlog, which prints to stdout when nothing configures it.
A single retried 5xx then puts two log lines in front of the
`-o json` output, while the command still exits 0. The shared error
handler also printed its messages to stdout. #76133 and #76134 moved
the messages of a few commands to stderr, but these two paths are
shared by every command.
(cherry picked from commit 6f7cb34)

Co-authored-by: Andrew Chang <[email protected]>
shahar1 pushed a commit to shahar1/airflow that referenced this pull request Nov 4, 2026
…st is retried or fails (apache#76347) (apache#76380)

The API client logs "Server error" and the tenacity retry warning
through structlog, which prints to stdout when nothing configures it.
A single retried 5xx then puts two log lines in front of the
`-o json` output, while the command still exits 0. The shared error
handler also printed its messages to stdout. apache#76133 and apache#76134 moved
the messages of a few commands to stderr, but these two paths are
shared by every command.
(cherry picked from commit 6f7cb34)

Co-authored-by: Andrew Chang <[email protected]>
shahar1 pushed a commit that referenced this pull request Nov 19, 2026
…the token (#76134) (#76195)

* Keep airflowctl auth token stdout limited to the token

`airflowctl auth token` is meant to be captured, e.g.
`TOKEN=$(airflowctl auth token ...)`. The username prompt was written to
stdout via input(), so in that usage the prompt was invisible to the user
and ended up inside the captured value; the failure message also went to
stdout. Without a TTY and without credentials, the command crashed with
an EOFError traceback instead of a clear error.

Prompts and errors now go to stderr, and missing credentials in a
non-interactive context exit with a clear message, mirroring what
`airflowctl auth login` already does.

* Let airflowctl auth token read a piped password without a TTY

Only refuse to run when the username is missing and stdin is not a
TTY (or is closed); the password still goes through getpass, which
reads from /dev/tty when available and falls back to stdin. This keeps
'echo "$PW" | airflowctl auth token --username u' and prompts on
/dev/tty with redirected stdin working, so scripts need not put the
password on argv. EOF while reading the password exits 1 with a clear
stderr message. Document the non-interactive options in the help text.
(cherry picked from commit 283ab98)

Co-authored-by: Shawn Sun <[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.

2 participants