Adds key authentication to ssh client
* Adds key param to instance clients * Added string to private key converter in ssh client Change-Id: I7ddec4abfcd3338b7d27827204b99492169d4a78
This commit is contained in:
@@ -32,7 +32,8 @@ class InstanceClientFactory(object):
|
||||
|
||||
@classmethod
|
||||
def get_instance_client(cls, ip_address=None, username=None, password=None,
|
||||
os_distro=None, server_id=None, config=None):
|
||||
os_distro=None, server_id=None, config=None,
|
||||
key=None):
|
||||
"""
|
||||
@summary: Returns utility class based on the OS type of server
|
||||
@param ip_address: IP Address of the server
|
||||
@@ -54,7 +55,7 @@ class InstanceClientFactory(object):
|
||||
|
||||
return instanceClient(ip_address=ip_address, username=username,
|
||||
password=password, os_distro=os_distro,
|
||||
server_id=server_id, config=config)
|
||||
server_id=server_id, config=config, key=key)
|
||||
|
||||
|
||||
class InstanceClient(object):
|
||||
@@ -63,10 +64,10 @@ class InstanceClient(object):
|
||||
"""
|
||||
|
||||
def __init__(self, ip_address=None, password=None, os_distro=None,
|
||||
config=None, username=None, server_id=None):
|
||||
config=None, username=None, server_id=None, key=None):
|
||||
self._client = InstanceClientFactory.get_instance_client(
|
||||
ip_address=ip_address, password=password, os_distro=os_distro,
|
||||
username=username, server_id=server_id, config=config)
|
||||
username=username, server_id=server_id, config=config, key=key)
|
||||
self.client_log = cclogging.getLogger(
|
||||
cclogging.get_object_namespace(self.__class__))
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
"""
|
||||
|
||||
from paramiko import PKey
|
||||
import time
|
||||
import re
|
||||
|
||||
@@ -33,9 +34,11 @@ from cloudcafe.compute.common.exceptions import FileNotFoundException, \
|
||||
class LinuxClient(BasePersistentLinuxClient):
|
||||
|
||||
def __init__(self, ip_address=None, server_id=None, username=None,
|
||||
password=None, config=None, os_distro=None):
|
||||
password=None, config=None, os_distro=None, key=None):
|
||||
self.client_log = cclogging.getLogger(
|
||||
cclogging.get_object_namespace(self.__class__))
|
||||
|
||||
|
||||
ssh_timeout = config.connection_timeout
|
||||
if ip_address is None:
|
||||
raise ServerUnreachable("None")
|
||||
@@ -58,7 +61,8 @@ class LinuxClient(BasePersistentLinuxClient):
|
||||
self.ssh_client = SSHBaseClient(self.ip_address,
|
||||
self.username,
|
||||
self.password,
|
||||
timeout=ssh_timeout)
|
||||
timeout=ssh_timeout,
|
||||
key=key)
|
||||
if not self.ssh_client.test_connection_auth():
|
||||
self.client_log.error("Ssh connection failed for: IP:{0} \
|
||||
Username:{1} Password: {2}".format(self.ip_address,
|
||||
|
||||
@@ -14,9 +14,10 @@ See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
"""
|
||||
|
||||
import exceptions
|
||||
import time
|
||||
import socket
|
||||
import exceptions
|
||||
import StringIO
|
||||
import warnings
|
||||
|
||||
with warnings.catch_warnings():
|
||||
@@ -33,7 +34,8 @@ class SSHBaseClient(BaseClient):
|
||||
|
||||
_log = cclogging.getLogger(__name__)
|
||||
|
||||
def __init__(self, host, username, password, timeout=20, port=22):
|
||||
def __init__(self, host, username, password, timeout=20,
|
||||
port=22, key=None):
|
||||
super(SSHBaseClient, self).__init__()
|
||||
self.host = host
|
||||
self.port = port
|
||||
@@ -41,6 +43,11 @@ class SSHBaseClient(BaseClient):
|
||||
self.password = password
|
||||
self.timeout = int(timeout)
|
||||
self._chan = None
|
||||
if key:
|
||||
key_file = StringIO.StringIO(key)
|
||||
self.key = paramiko.RSAKey.from_private_key(key_file)
|
||||
else:
|
||||
self.key = None
|
||||
|
||||
def _get_ssh_connection(self):
|
||||
"""Returns an ssh connection to the specified host"""
|
||||
@@ -65,6 +72,7 @@ class SSHBaseClient(BaseClient):
|
||||
ssh.connect(hostname=self.host,
|
||||
username=self.username,
|
||||
password=self.password,
|
||||
pkey=self.key,
|
||||
timeout=20,
|
||||
key_filename=[],
|
||||
look_for_keys=False,
|
||||
|
||||
Reference in New Issue
Block a user