From 03a925025e638a01dfe3051a96e8bccaf0226bb7 Mon Sep 17 00:00:00 2001 From: Nicholas Yang Date: Mon, 22 Jan 2024 12:46:13 +0800 Subject: [PATCH] Fix: sh: guide users to setup key-based ssh authentication when non-interactive authentcation fails (bsc#1219045) --- crmsh/sh.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/crmsh/sh.py b/crmsh/sh.py index 1571d39e2e..7b6d506d47 100644 --- a/crmsh/sh.py +++ b/crmsh/sh.py @@ -56,15 +56,22 @@ def __init__(self, cmd: str, host: typing.Optional[str], user: str, msg: str): self.host = host self.user = user - @staticmethod - def diagnose() -> str: - if user_of_host.instance().use_ssh_agent(): - with StringIO() as buf: + def diagnose(self) -> str: + with StringIO() as buf: + if user_of_host.instance().use_ssh_agent(): if 'SSH_AUTH_SOCK' not in os.environ: buf.write('Environment variable SSH_AUTH_SOCK does not exist.') if 'SUDO_USER' in os.environ: buf.write(' Please check whether ssh-agent is available and consider using "sudo --preserve-env=SSH_AUTH_SOCK".') - return buf.getvalue() + return buf.getvalue() + + +class NonInteractiveSSHAuthorizationError(AuthorizationError): + + def diagnose(self) -> str: + ret = super().diagnose() + if not ret: + return 'Please configure passwordless authentication with "crm cluster init ssh" and "crm cluster join ssh"' class CommandFailure(Error): @@ -335,7 +342,7 @@ def subprocess_run_without_input(self, host: typing.Optional[str], user: typing. **kwargs, ) if self.raise_ssh_error and result.returncode == 255: - raise AuthorizationError(cmd, host, remote_user, Utils.decode_str(result.stderr).strip()) + raise NonInteractiveSSHAuthorizationError(cmd, host, remote_user, Utils.decode_str(result.stderr).strip()) else: return result