Skip to content
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

Fix sudo password handling #1094

Merged
merged 3 commits into from
May 11, 2024
Merged

Fix sudo password handling #1094

merged 3 commits into from
May 11, 2024

Conversation

CD3
Copy link
Contributor

@CD3 CD3 commented May 4, 2024

First, thank you for pyinfra. I have been using pyinfra 2 to manage my personal dev machines after trying fabric and ansible, and really liked it, but with version 3's addition of _if, it is perfect for what I need.

The only issue I had when switching to 3.x was that the sudo password pompt no longer worked. Pyinfra would ask for the sudo password, but then immediately fail.

The problem was that the connector utility function that handled setting the sudo password was checking to see if _sudo_password was in the argument list, but not checking if it was set. Something like (i've shorted some var names)

a['_sudo_password'] = a.get( '_sudo_password', h.data.get('prompted_sudo_password')

but a['_sudo_password'] is set to None, so the prompted password never get used.

The other issue was that the command string that was generated to pass the sudo password to an askpass executable was not quoting the password

PYINFRA_SUDO_PASSWORD=password

which does not work if the password contains special shell characters like `;' or spaces.

CD3 added 2 commits May 3, 2024 14:15
Commands that need to run with sudo are given the password by setting
the PYINFRA_SUDO_PASSWORD environment variable and using a script that
returns the value as the `askpass` executable.

example:

PYINFRA_SUDO_PASSWORD=p@ssworD sudo -H -A -k sh -c 'echo hi'

If the user's sudo password contains spaces or special characters that
are interpreted by the shell, then the password needs to be quoted

PYINFRA_SUDO_PASSWORD='p@ss worD' sudo -H -A -k sh -c 'echo hi'
If the sudo password was needed, the user was prompted, but they
password they entered never ended up getting used because of a logic
error.

The prompted password would only be used if the _sudo_password argument
was missing. If no sudo password was given, it is set to None. So
_sudo_password is present in the arguents, but does not have a value.

This fix checks if the _sudo_password argument is None and sets it using
the prompted password if so.
pyinfra/connectors/util.py Outdated Show resolved Hide resolved
Need to use shelex.quote (rather than just single quoting) to handle
passwords that contain quotes.
Copy link
Member

@Fizzadar Fizzadar left a comment

Choose a reason for hiding this comment

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

Fantastic stuff @CD3, thank you for fixing this AND adding a great test case 🎉

@Fizzadar Fizzadar merged commit 9b6c214 into pyinfra-dev:3.x May 11, 2024
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants