pyinfra icon indicating copy to clipboard operation
pyinfra copied to clipboard

Fragile sudo handling for @local connector

Open Dexmachi opened this issue 7 months ago • 0 comments

The connector @local has the execute_command_with_sudo_retry function. This function has hardcoded and forced locale string matching, this makes the implementation break in any OS without an English locale.

this is the source code:

def execute_command_with_sudo_retry(
    host: "Host",
    command_arguments: "ConnectorArguments",
    execute_command: Callable[..., tuple[int, CommandOutput]],
) -> tuple[int, CommandOutput]:
    return_code, output = execute_command()

    # If we failed look for a sudo password prompt line and re-submit using the sudo password. Look
    # at all lines here in case anything else gets printed, eg in:
    # https://github.com/pyinfra-dev/pyinfra/issues/1292
    if return_code != 0 and output and output.combined_lines:
        for line in reversed(output.combined_lines):
            if line.line.strip() == "sudo: a password is required":
                # If we need a password, ask the user for it and attach to the host
                # internal connector data for use when executing future commands.
                sudo_password = getpass("{0}sudo password: ".format(host.print_prefix))
                host.connector_data["prompted_sudo_password"] = sudo_password
                return_code, output = execute_command()
                break

    return return_code, output

it has a very unrobust if line.line.strip() == "sudo: a password is required" line. This breaks on any different language other than english.

*** apply native_packages -vvv
Please, enter your sudo password:: 
INFO: [@local] Connected
DEBUG: Activating host: @local
INFO: Collecting host contexts...
DEBUG: Getting fact: server.Command (command=pacman -Qqen || true) (ensure_hosts: None)
DEBUG: --> Running command on localhost: sh -c 'pacman -Qqen || true'
DEBUG: --> Waiting for exit status...
DEBUG: --> Command exit status: 0
DEBUG: [@local] Loaded fact server.Command
DEBUG: Getting fact: server.Command (command=pacman -Qqdn || true) (ensure_hosts: None)
DEBUG: --> Running command on localhost: sh -c 'pacman -Qqdn || true'
DEBUG: --> Waiting for exit status...
DEBUG: --> Command exit status: 0
DEBUG: [@local] Loaded fact server.Command
DEBUG: Getting fact: files.Directory (path=/sys/firmware/efi/) (ensure_hosts: None)
DEBUG: --> Running command on localhost: sh -c '! (test -e /sys/firmware/efi/ || test -L /sys/firmware/efi/ ) || ( stat -c '"'"'user=%U group=%G mode=%A atime=%X mtime=%Y ctime=%Z size=%s %N'"'"' /sys/firmware/efi/ 2> /dev/null || stat -f '"'"'user=%Su group=%Sg mode=%Sp atime=%a mtime=%m ctime=%c size=%z %N%SY'"'"' /sys/firmware/efi/ || ls -ld /sys/firmware/efi/ )'
DEBUG: --> Waiting for exit status...
DEBUG: --> Command exit status: 0
DEBUG: [@local] Loaded fact files.Directory (path=/sys/firmware/efi/)
INFO: Role 'Install Arch Native Packages' on host: '@local' has the following delta:
  ----- To remove: native -----
    - swaks


Do you wish to apply the above deltas? [y/n] (y): y
DEBUG: Adding operation, {'Uninstalling native packages'}, opOrder=(0,), opHash=784a97bf1955d5f7a2b9dd6c1e371e17b73c42bc
INFO: Executing apply plans...
INFO: --> Starting operation: Uninstalling native packages 
DEBUG: Starting operation {'Uninstalling native packages'} on @local
DEBUG: --> Running command on localhost: sudo -H -n sh -c 'pacman -Rcns --noconfirm --noprogressbar swaks'
DEBUG: --> Waiting for exit status...
DEBUG: --> Command exit status: 1
ERROR: [@local] sudo: uma senha é necessária
ERROR: [@local] Error: executed 0 commands
DEBUG: Failing hosts: @local
ERROR: Error executing plans with pyinfra: No hosts remaining!

additionally, there is the #1043 Issue, which also might happen because of this handling.

the easiest way to address this may be setting the LC_ALL to C at the time of calling and checking for error codes instead of string matching, allowing for proper handling of this system.

Dexmachi avatar Apr 21 '26 17:04 Dexmachi