175a38078f3c798779424fa0d1854725a4ae10bd
[releng.git] / modules / opnfv / utils / ssh_utils.py
1 ##############################################################################
2 # Copyright (c) 2015 Ericsson AB and others.
3 # Authors: George Paraskevopoulos (geopar@intracom-telecom.com)
4 #          Jose Lausuch (jose.lausuch@ericsson.com)
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10
11
12 import os
13 import paramiko
14
15 from opnfv.utils import opnfv_logger as logger
16
17 logger = logger.Logger("SSH utils").getLogger()
18 SSH_TIMEOUT = 60
19
20 ''' Monkey Patch paramiko _custom_start_client '''
21 # We are using paramiko 2.1.1 and in the CI in the SFC
22 # test we are facing this issue:
23 # https://github.com/robotframework/SSHLibrary/issues/158
24 # The fix was merged in paramiko 2.1.3 in this PR:
25 # https://github.com/robotframework/SSHLibrary/pull/159/files
26 # Until we upgrade we can use this monkey patch to work
27 # around the issue
28
29
30 def _custom_start_client(self, *args, **kwargs):
31     self.banner_timeout = 45
32     self._orig_start_client(*args, **kwargs)
33
34
35 paramiko.transport.Transport._orig_start_client = \
36     paramiko.transport.Transport.start_client
37 paramiko.transport.Transport.start_client = _custom_start_client
38 ''' Monkey Patch paramiko _custom_start_client '''
39
40
41 def get_ssh_client(hostname,
42                    username,
43                    password=None,
44                    proxy=None,
45                    pkey_file=None):
46     client = None
47     try:
48         if proxy is None:
49             client = paramiko.SSHClient()
50         else:
51             client = ProxyHopClient()
52             proxy_pkey_file = proxy.get('pkey_file', '/root/.ssh/id_rsa')
53             client.configure_jump_host(proxy['ip'],
54                                        proxy['username'],
55                                        proxy['password'],
56                                        proxy_pkey_file)
57         if client is None:
58             raise Exception('Could not connect to client')
59
60         client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
61         if pkey_file is not None:
62             key = paramiko.RSAKey.from_private_key_file(pkey_file)
63             client.load_system_host_keys()
64             client.connect(hostname,
65                            username=username,
66                            pkey=key,
67                            timeout=SSH_TIMEOUT)
68         else:
69             client.connect(hostname,
70                            username=username,
71                            password=password,
72                            timeout=SSH_TIMEOUT)
73
74         return client
75     except Exception as e:
76         logger.error(e)
77         return None
78
79
80 def get_file(ssh_conn, src, dest):
81     try:
82         sftp = ssh_conn.open_sftp()
83         sftp.get(src, dest)
84         return True
85     except Exception as e:
86         logger.error("Error [get_file(ssh_conn, '%s', '%s']: %s" %
87                      (src, dest, e))
88         return None
89
90
91 def put_file(ssh_conn, src, dest):
92     try:
93         sftp = ssh_conn.open_sftp()
94         sftp.put(src, dest)
95         return True
96     except Exception as e:
97         logger.error("Error [put_file(ssh_conn, '%s', '%s']: %s" %
98                      (src, dest, e))
99         return None
100
101
102 class ProxyHopClient(paramiko.SSHClient):
103     '''
104     Connect to a remote server using a proxy hop
105     '''
106
107     def __init__(self, *args, **kwargs):
108         self.proxy_ssh = None
109         self.proxy_transport = None
110         self.proxy_channel = None
111         self.proxy_ip = None
112         self.proxy_ssh_key = None
113         self.local_ssh_key = os.path.join(os.getcwd(), 'id_rsa')
114         super(ProxyHopClient, self).__init__(*args, **kwargs)
115
116     def configure_jump_host(self, jh_ip, jh_user, jh_pass,
117                             jh_ssh_key='/root/.ssh/id_rsa'):
118         self.proxy_ip = jh_ip
119         self.proxy_ssh_key = jh_ssh_key
120         self.local_ssh_key = os.path.join(os.getcwd(),
121                                           jh_ssh_key.split('/')[-1])
122         self.proxy_ssh = paramiko.SSHClient()
123         self.proxy_ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
124         self.proxy_ssh.connect(jh_ip,
125                                username=jh_user,
126                                password=jh_pass,
127                                timeout=SSH_TIMEOUT)
128         self.proxy_transport = self.proxy_ssh.get_transport()
129
130     def connect(self, hostname, port=22, username='root', password=None,
131                 pkey=None, key_filename=None, timeout=None, allow_agent=True,
132                 look_for_keys=True, compress=False, sock=None, gss_auth=False,
133                 gss_kex=False, gss_deleg_creds=True, gss_host=None,
134                 banner_timeout=None):
135         try:
136             if self.proxy_ssh is None:
137                 raise Exception('You must configure the jump '
138                                 'host before calling connect')
139
140             get_file_res = get_file(self.proxy_ssh,
141                                     self.proxy_ssh_key,
142                                     self.local_ssh_key)
143             if get_file_res is None:
144                 raise Exception('Could\'t fetch SSH key from jump host')
145             if self.proxy_ssh_key.split('/')[-1] == 'id_dsa':
146                 proxy_key = (paramiko.DSSKey
147                              .from_private_key_file(self.local_ssh_key))
148             else:
149                 proxy_key = (paramiko.RSAKey
150                              .from_private_key_file(self.local_ssh_key))
151
152             self.proxy_channel = self.proxy_transport.open_channel(
153                 "direct-tcpip",
154                 (hostname, 22),
155                 (self.proxy_ip, 22))
156
157             self.set_missing_host_key_policy(paramiko.AutoAddPolicy())
158             super(ProxyHopClient, self).connect(hostname,
159                                                 username=username,
160                                                 pkey=proxy_key,
161                                                 sock=self.proxy_channel,
162                                                 timeout=timeout)
163             os.remove(self.local_ssh_key)
164         except Exception as e:
165             logger.error(e)