Skip to content

Commit e921ec6

Browse files
Gaurav AradhyeSrikanteswaraRao Talluri
authored andcommitted
CLOUDSTACK-7408: Fixed - Private key of the ssh keypair was getting corrupted
Signed-off-by: SrikanteswaraRao Talluri <[email protected]>
1 parent 9e5da75 commit e921ec6

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

tools/marvin/marvin/lib/base.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ def restore(self, apiclient, templateid=None):
552552

553553
def get_ssh_client(
554554
self, ipaddress=None, reconnect=False, port=None,
555-
keyPairFileLocation=None):
555+
keyPairFileLocation=None, knownHostsFilePath=None):
556556
"""Get SSH object of VM"""
557557

558558
# If NAT Rules are not created while VM deployment in Advanced mode
@@ -571,14 +571,16 @@ def get_ssh_client(
571571
self.ssh_port,
572572
self.username,
573573
self.password,
574-
keyPairFileLocation=keyPairFileLocation
574+
keyPairFileLocation=keyPairFileLocation,
575+
knownHostsFilePath=knownHostsFilePath
575576
)
576577
self.ssh_client = self.ssh_client or is_server_ssh_ready(
577578
self.ssh_ip,
578579
self.ssh_port,
579580
self.username,
580581
self.password,
581-
keyPairFileLocation=keyPairFileLocation
582+
keyPairFileLocation=keyPairFileLocation,
583+
knownHostsFilePath=knownHostsFilePath
582584
)
583585
return self.ssh_client
584586

tools/marvin/marvin/lib/utils.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ def cleanup_resources(api_client, resources):
121121
obj.delete(api_client)
122122

123123

124-
def is_server_ssh_ready(ipaddress, port, username, password, retries=20, retryinterv=30, timeout=10.0, keyPairFileLocation=None):
124+
def is_server_ssh_ready(ipaddress, port, username, password, retries=20,
125+
retryinterv=30, timeout=10.0, keyPairFileLocation=None,
126+
knownHostsFilePath=None):
125127
'''
126128
@Name: is_server_ssh_ready
127129
@Input: timeout: tcp connection timeout flag,
@@ -140,7 +142,8 @@ def is_server_ssh_ready(ipaddress, port, username, password, retries=20, retryin
140142
keyPairFiles=keyPairFileLocation,
141143
retries=retries,
142144
delay=retryinterv,
143-
timeout=timeout)
145+
timeout=timeout,
146+
knownHostsFilePath=knownHostsFilePath)
144147
except Exception, e:
145148
raise Exception("SSH connection has Failed. Waited %ss. Error is %s" % (retries * retryinterv, str(e)))
146149
else:

tools/marvin/marvin/sshClient.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
SFTPClient)
2525
import socket
2626
import time
27+
import os
2728
from marvin.cloudstackException import (
2829
internalError,
2930
GetDetailExceptionInfo
@@ -49,7 +50,8 @@ class SshClient(object):
4950
'''
5051

5152
def __init__(self, host, port, user, passwd, retries=60, delay=10,
52-
log_lvl=logging.DEBUG, keyPairFiles=None, timeout=10.0):
53+
log_lvl=logging.DEBUG, keyPairFiles=None, timeout=10.0,
54+
knownHostsFilePath=None):
5355
self.host = None
5456
self.port = 22
5557
self.user = user
@@ -77,6 +79,18 @@ def __init__(self, host, port, user, passwd, retries=60, delay=10,
7779
self.timeout = timeout
7880
if port is not None and port >= 0:
7981
self.port = port
82+
83+
# If the known_hosts file is not at default location,
84+
# then its location can be passed, or else the default
85+
# path will be considered (which is ~/.ssh/known_hosts)
86+
if knownHostsFilePath:
87+
self.knownHostsFilePath = knownHostsFilePath
88+
else:
89+
self.knownHostsFilePath = os.path.expanduser(
90+
os.path.join(
91+
"~",
92+
".ssh",
93+
"known_hosts"))
8094
if self.createConnection() == FAILED:
8195
raise internalError("SSH Connection Failed")
8296

@@ -120,14 +134,14 @@ def createConnection(self):
120134
password=self.passwd,
121135
timeout=self.timeout)
122136
else:
123-
self.ssh.load_host_keys(self.keyPairFiles)
137+
self.ssh.load_host_keys(self.knownHostsFilePath)
124138
self.ssh.connect(hostname=self.host,
125139
port=self.port,
126140
username=self.user,
127141
password=self.passwd,
128142
key_filename=self.keyPairFiles,
129143
timeout=self.timeout,
130-
look_for_keys=True
144+
look_for_keys=False
131145
)
132146
self.logger.debug("===SSH to Host %s port : %s SUCCESSFUL==="
133147
% (str(self.host), str(self.port)))

0 commit comments

Comments
 (0)