in libcloud/compute/ssh.py [0:0]
def connect(self):
conninfo = {
"hostname": self.hostname,
"port": self.port,
"username": self.username,
"allow_agent": False,
"look_for_keys": False,
}
if self.password:
conninfo["password"] = self.password
if self.key_files:
conninfo["key_filename"] = self.key_files
if self.key_material:
conninfo["pkey"] = self._get_pkey_object(key=self.key_material, password=self.password)
if not self.password and not (self.key_files or self.key_material):
conninfo["allow_agent"] = True
conninfo["look_for_keys"] = True
if self.timeout:
conninfo["timeout"] = self.timeout
# This is a workaround for paramiko only supporting key files in
# format staring with "BEGIN RSA PRIVATE KEY".
# If key_files are provided and a key looks like a PEM formatted key
# we try to convert it into a format supported by paramiko
if (
self.key_files
and not isinstance(self.key_files, (list, tuple))
and os.path.isfile(self.key_files)
):
with open(self.key_files) as fp:
key_material = fp.read()
try:
pkey = self._get_pkey_object(key=key_material, password=self.password)
except paramiko.ssh_exception.PasswordRequiredException as e:
raise e
except Exception:
pass
else:
# It appears key is valid, but it was passed in in an invalid
# format. Try to use the converted key directly
del conninfo["key_filename"]
conninfo["pkey"] = pkey
extra = {
"_hostname": self.hostname,
"_port": self.port,
"_username": self.username,
"_timeout": self.timeout,
}
if self.password:
extra["_auth_method"] = "password"
else:
extra["_auth_method"] = "key_file"
if self.key_files:
extra["_key_file"] = self.key_files
self.logger.debug("Connecting to server", extra=extra)
try:
self.client.connect(**conninfo)
except paramiko.ssh_exception.AuthenticationException as e:
# Special case to handle paramiko >= 2.9.0 which supports SHA-2
# variants of the RSA key verification algorithm which don't work
# with older OpenSSH server versions (e.g. default setup on Ubuntu
# 14.04).
# Sadly there is no way for us to catch and retry on more specific
# / granular exception.
# See https://www.paramiko.org/changelog.html for details.
if (
PARAMIKO_VERSION_TUPLE >= (2, 9, 0)
and LIBCLOUD_PARAMIKO_SHA2_BACKWARD_COMPATIBILITY
):
self.logger.warn(SHA2_PUBKEY_NOT_SUPPORTED_AUTH_ERROR_MSG)
conninfo["disabled_algorithms"] = {"pubkeys": ["rsa-sha2-256", "rsa-sha2-512"]}
self.client.connect(**conninfo)
else:
raise e
return True