Repository navigation
fix(connectors.util): prompt for sudo password up front with --use-sudo-password - #1961
Merged
Fizzadar merged 1 commit intoSep 22, 2026
Merged
Conversation
…do-password config.USE_SUDO_PASSWORD (set by --use-sudo-password) was no longer read anywhere, so pyinfra always tried passwordless sudo first and only prompted after seeing a password-required failure. Prompt before the first sudo command instead when no sudo password is already known. Fixes pyinfra-dev#1850
Fizzadar
approved these changes
Sep 22, 2026
Fizzadar
left a comment
Member
There was a problem hiding this comment.
Nice catch, thank you @anandghegde!
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
--use-sudo-passwordsetsconfig.USE_SUDO_PASSWORD, but nothing has read that value since_use_sudo_passwordbecame_sudo_passwordin v3. So the flag did nothing: pyinfra always ransudo -nfirst and only asked for the password after thesudo: a password is requiredretry kicked in.This change checks
config.USE_SUDO_PASSWORDinmake_unix_command_for_host. If sudo is on and no password is known yet (no_sudo_password/SUDO_PASSWORDand nothing prompted earlier), it asks for the password before building the first command. The answer goes intoprompted_sudo_password, the same place the retry path stores it, so later commands reuse it.USE_SUDO_PASSWORDis added back toConfigDefaultsso it has a typed default. Without the flag, nothing changes.Related: #1689 also changes sudo handling in
connectors/util.py, but inexecute_command_with_sudo_retry, the askpass script andmake_unix_command. This PR only touchesmake_unix_command_for_hostand adds tests at the end oftest_util.py, so the two shouldn't conflict.How I tested:
tests/test_connectors/test_util.py. The prompt test fails on3.x(it getssudo -H -n sh -c uptime) and passes with the fix. Two more tests cover no prompt when a password is already given and no prompt without the flag.uv run pytest -m 'not end_to_end'(2056 passed),ruff check,ruff format --check,mypy,lint_arguments_sync.pyall pass. I didn't have shellcheck installed, and this PR doesn't change any shell scripts.python:3.12-slimcontainer with a user whose sudo needs a password. The command waspyinfra -y -vv --sudo --use-sudo-password @local exec -- id, run as that user:3.x:>>> sudo -H -n sh -c id→sudo: a password is required→ prompt → askpass retry>>> env SUDO_ASKPASS=... sudo -H -A -k sh -c id, with no passwordless attempt first--use-sudo-password: same as before (triessudo -n, then prompts)AI disclosure: I made this with Claude Code. It helped find the cause, write the fix and tests, and run the checks and container test above. I reviewed the change and I understand it.
Fixes #1850
3.xat this time)scripts/dev-test.sh)scripts/dev-lint.sh)conventional commits format