Skip to content

Commit db72e06

Browse files
sarahbxlukas-bednar
authored andcommitted
[WIP] Multi key type support
1 parent 0314e8b commit db72e06

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

rrmngmnt/ssh.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,12 @@ def __init__(self, executor, timeout=None):
6262
self._ssh = paramiko.SSHClient()
6363
self._ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
6464
if isinstance(self._executor.user, UserWithPKey):
65-
self.pkey = paramiko.RSAKey.from_private_key_file(
66-
self._executor.user.private_key
65+
self.pkey = self._get_pkey(
66+
filename=self._executor.user.private_key
6767
)
6868
elif self._executor.use_pkey:
69-
self.pkey = paramiko.RSAKey.from_private_key_file(
70-
os.getenv(
69+
self.pkey = self._get_pkey(
70+
filename=os.getenv(
7171
"HOST_SSH_KEY", ID_RSA_PRV % os.path.expanduser('~')
7272
)
7373
)
@@ -148,6 +148,21 @@ def open_file(self, path, mode='r', bufsize=-1):
148148
) as fh:
149149
yield fh
150150

151+
@staticmethod
152+
def _get_pkey(filename):
153+
errors = []
154+
for key_type in (
155+
paramiko.RSAKey,
156+
paramiko.ECDSAKey,
157+
paramiko.Ed25519Key,
158+
):
159+
try:
160+
return key_type.from_private_key_file(filename=filename)
161+
except paramiko.ssh_exception.SSHException as exp:
162+
errors.append(str(exp))
163+
continue
164+
raise paramiko.ssh_exception.SSHException(f"Invalid Key {errors}")
165+
151166
class Command(Executor.Command):
152167
"""
153168
This class holds all data related to command execution.

0 commit comments

Comments
 (0)