Repository navigation
Keep airflowctl auth token stdout limited to the token - #76134
Conversation
`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.
f6a6a9f to
0313299
Compare
shahar1
left a comment
There was a problem hiding this comment.
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 uin CI,docker exec -i,kubectl exec -i,ssh -T. - Terminal present, stdin redirected —
--username u </dev/null, or inside awhile read/xargsloop; before,getpassprompted 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(thetokencommanddescription, 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--helpmatches the new error.airflow-ctl/tests/airflow_ctl/ctl/commands/test_auth_command.py:320(nit): the repo guidance says "Prefer@mock.patchdecorators overwith mock.patch(...)context managers for patching." Thebuiltins.input/getpass.getpasspatches don't vary per case and could be decorators; thesys.stdinpatch varies byis_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
left a comment
There was a problem hiding this comment.
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.
Backport successfully created: airflow-ctl/v0-1-testNote: As of Merging PRs targeted for Airflow 3.X In matter of doubt please ask in #release-management Slack channel.
|
…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]>
|
@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 Drafted-by: GitHub Copilot (Claude Opus 5.5); reviewed by @Shawnsuun before posting |
…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.
…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]>
…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]>
…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]>
airflowctl auth tokenprints 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:input("Username: "), which writes to stdout. When stdout wascaptured, the user never saw the prompt, and the prompt text ended up in
TOKEN.</dev/null) and without--username/--password, the commandcrashed with an
EOFErrortraceback.Token generation failed: ...went to stdout.Changes:
getpass.--usernameor--passwordis missing and stdin is not a TTY, the command exits 1 with aclear message on stderr.
airflowctl auth loginalready does this check.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,
getpassfell 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 errorstructlog warning on an API error still goes to stdout. Thatis left for a separate change.
Was generative AI tooling used to co-author this PR?
Generated-by: GitHub Copilot (Claude Opus 5.5) following the guidelines
{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.