Skip to content

Commit 2385558

Browse files
authored
fix finding private key bugs (#243)
1 parent 5e16389 commit 2385558

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

dpdispatcher/ssh_context.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -136,21 +136,27 @@ def _setup_ssh(self):
136136
if self.totp_secret:
137137
ts.auth_interactive(self.username, self.inter_handler)
138138
else:
139-
default_path = os.path.join(os.environ["HOME"], ".ssh", "id_rsa")
140-
path = ""
139+
key_path = os.path.join(os.path.expanduser("~"), ".ssh", "id_rsa")
141140
if self.key_filename:
142-
path = os.path.abspath(self.key_filename)
141+
key_path = os.path.abspath(self.key_filename)
142+
key = None
143+
if os.path.exists(key_path):
144+
try:
145+
key = paramiko.RSAKey.from_private_key_file(key_path)
146+
except paramiko.PasswordRequiredException:
147+
key = paramiko.RSAKey.from_private_key_file(key_path, self.passphrase)
148+
if key:
149+
try:
150+
ts.auth_publickey(self.username, key)
151+
except paramiko.ssh_exception.AuthenticationException:
152+
if self.password:
153+
ts.auth_password(self.username, self.password)
154+
else:
155+
raise RuntimeError("Authentication failed, try to provide password")
156+
elif self.password:
157+
ts.auth_password(self.username, self.password)
143158
else:
144-
path = default_path
145-
try:
146-
key = paramiko.RSAKey.from_private_key_file(path)
147-
except paramiko.PasswordRequiredException:
148-
key = paramiko.RSAKey.from_private_key_file(path, self.passphrase)
149-
try:
150-
ts.auth_publickey(self.username, key)
151-
except paramiko.ssh_exception.AuthenticationException:
152-
if self.password:
153-
ts.auth_password(self.username, self.password)
159+
raise RuntimeError("Please provide at least one form of authentication")
154160
assert(ts.is_active())
155161
#Opening a session creates a channel along the socket to the server
156162
ts.open_session(timeout=self.timeout)

0 commit comments

Comments
 (0)