From db72e063d861ac3f3755f273243ead1912de016b Mon Sep 17 00:00:00 2001 From: Sarah Bennert Date: Fri, 1 Apr 2022 07:41:24 -0400 Subject: [PATCH] [WIP] Multi key type support --- rrmngmnt/ssh.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/rrmngmnt/ssh.py b/rrmngmnt/ssh.py index b9661b9..593d23f 100644 --- a/rrmngmnt/ssh.py +++ b/rrmngmnt/ssh.py @@ -62,12 +62,12 @@ def __init__(self, executor, timeout=None): self._ssh = paramiko.SSHClient() self._ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) if isinstance(self._executor.user, UserWithPKey): - self.pkey = paramiko.RSAKey.from_private_key_file( - self._executor.user.private_key + self.pkey = self._get_pkey( + filename=self._executor.user.private_key ) elif self._executor.use_pkey: - self.pkey = paramiko.RSAKey.from_private_key_file( - os.getenv( + self.pkey = self._get_pkey( + filename=os.getenv( "HOST_SSH_KEY", ID_RSA_PRV % os.path.expanduser('~') ) ) @@ -148,6 +148,21 @@ def open_file(self, path, mode='r', bufsize=-1): ) as fh: yield fh + @staticmethod + def _get_pkey(filename): + errors = [] + for key_type in ( + paramiko.RSAKey, + paramiko.ECDSAKey, + paramiko.Ed25519Key, + ): + try: + return key_type.from_private_key_file(filename=filename) + except paramiko.ssh_exception.SSHException as exp: + errors.append(str(exp)) + continue + raise paramiko.ssh_exception.SSHException(f"Invalid Key {errors}") + class Command(Executor.Command): """ This class holds all data related to command execution.