3 # Copyright (c) 2016 Red Hat
4 # Luke Hinds (lhinds@redhat.com)
5 # 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
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # 0.1: OpenSCAP paramiko connection functions
17 import functest.utils.functest_logger as ft_logger
18 import functest.utils.functest_constants as ft_constants
20 # add installer IP from env
21 INSTALLER_IP = ft_constants.CI_INSTALLER_IP
24 logger = ft_logger.Logger("security_scan").getLogger()
25 paramiko.util.log_to_file("/var/log/paramiko.log")
29 def __init__(self, *args):
32 def keystonepass(self):
34 client = paramiko.SSHClient()
35 privatekeyfile = os.path.expanduser('/root/.ssh/id_rsa')
36 selectedkey = paramiko.RSAKey.from_private_key_file(privatekeyfile)
37 client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
39 client.connect(INSTALLER_IP, port=22, username='stack',
41 except paramiko.SSHException:
42 logger.error("Password is invalid for "
43 "undercloud host: {0}".format(INSTALLER_IP))
44 except paramiko.AuthenticationException:
45 logger.error("Authentication failed for "
46 "undercloud host: {0}".format(INSTALLER_IP))
48 logger.error("Socker Connection failed for "
49 "undercloud host: {0}".format(INSTALLER_IP))
50 stdin, stdout, stderr = client.exec_command(com)
55 remotekey = self.args[0]
56 localkey = self.args[1]
57 privatekeyfile = os.path.expanduser('/root/.ssh/id_rsa')
58 selectedkey = paramiko.RSAKey.from_private_key_file(privatekeyfile)
59 transport = paramiko.Transport((INSTALLER_IP, 22))
60 transport.connect(username='stack', pkey=selectedkey)
62 sftp = paramiko.SFTPClient.from_transport(transport)
63 except paramiko.SSHException:
64 logger.error("Authentication failed for "
65 "host: {0}".format(INSTALLER_IP))
66 except paramiko.AuthenticationException:
67 logger.error("Authentication failed for "
68 "host: {0}".format(INSTALLER_IP))
70 logger.error("Socker Connection failed for "
71 "undercloud host: {0}".format(INSTALLER_IP))
72 sftp.get(remotekey, localkey)
77 class ConnectionManager:
78 def __init__(self, host, port, user, localkey, *args):
82 self.localkey = localkey
85 def remotescript(self):
86 localpath = self.args[0]
87 remotepath = self.args[1]
90 client = paramiko.SSHClient()
91 privatekeyfile = os.path.expanduser('/root/.ssh/id_rsa')
92 selectedkey = paramiko.RSAKey.from_private_key_file(privatekeyfile)
93 client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
94 # Connection to undercloud
96 client.connect(INSTALLER_IP, port=22, username='stack',
98 except paramiko.SSHException:
99 logger.error("Authentication failed for "
100 "host: {0}".format(self.host))
101 except paramiko.AuthenticationException:
102 logger.error("Authentication failed for "
103 "host: {0}".format(self.host))
105 logger.error("Socker Connection failed for "
106 "undercloud host: {0}".format(self.host))
108 transport = client.get_transport()
109 local_addr = ('127.0.0.1', 0)
110 channel = transport.open_channel("direct-tcpip",
111 (self.host, int(self.port)),
113 remote_client = paramiko.SSHClient()
114 remote_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
115 # Tunnel to overcloud
117 remote_client.connect('127.0.0.1', port=22, username=self.user,
118 key_filename=self.localkey, sock=channel)
119 sftp = remote_client.open_sftp()
120 sftp.put(localpath, remotepath)
121 except paramiko.SSHException:
122 logger.error("Authentication failed for "
123 "host: {0}".format(self.host))
124 except paramiko.AuthenticationException:
125 logger.error("Authentication failed for "
126 "host: {0}".format(self.host))
128 logger.error("Socker Connection failed for "
129 "undercloud host: {0}".format(self.host))
132 stdin, stdout, stderr = remote_client.exec_command(com)
133 stdout = stdout.readlines()
135 sftp.remove(remotepath)
136 remote_client.close()
140 output = output + line
147 client = paramiko.SSHClient()
148 privatekeyfile = os.path.expanduser('/root/.ssh/id_rsa')
149 selectedkey = paramiko.RSAKey.from_private_key_file(privatekeyfile)
150 client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
151 # Connection to undercloud
153 client.connect(INSTALLER_IP, port=22, username='stack',
155 except paramiko.SSHException:
156 logger.error("Authentication failed for "
157 "host: {0}".format(self.host))
158 except paramiko.AuthenticationException:
159 logger.error("Authentication failed for "
160 "host: {0}".format(self.host))
162 logger.error("Socker Connection failed for "
163 "undercloud host: {0}".format(self.host))
165 transport = client.get_transport()
166 local_addr = ('127.0.0.1', 0) # 0 denotes choose random port
167 channel = transport.open_channel("direct-tcpip",
168 (self.host, int(self.port)),
170 remote_client = paramiko.SSHClient()
171 remote_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
172 # Tunnel to overcloud
174 remote_client.connect('127.0.0.1', port=22, username=self.user,
175 key_filename=self.localkey, sock=channel)
176 except paramiko.SSHException:
177 logger.error("Authentication failed for "
178 "host: {0}".format(self.host))
179 except paramiko.AuthenticationException:
180 logger.error("Authentication failed for "
181 "host: {0}".format(self.host))
183 logger.error("Socker Connection failed for "
184 "undercloud host: {0}".format(self.host))
186 chan = remote_client.get_transport().open_session()
188 feed = chan.makefile()
189 chan.exec_command(com)
192 remote_client.close()
195 def download_reports(self):
196 dl_folder = self.args[0]
197 reportfile = self.args[1]
198 reportname = self.args[2]
199 resultsname = self.args[3]
200 client = paramiko.SSHClient()
201 privatekeyfile = os.path.expanduser('/root/.ssh/id_rsa')
202 selectedkey = paramiko.RSAKey.from_private_key_file(privatekeyfile)
203 client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
204 # Connection to overcloud
206 client.connect(INSTALLER_IP, port=22, username='stack',
208 except paramiko.SSHException:
209 logger.error("Authentication failed for "
210 "host: {0}".format(self.host))
211 except paramiko.AuthenticationException:
212 logger.error("Authentication failed for "
213 "host: {0}".format(self.host))
215 logger.error("Socker Connection failed for "
216 "undercloud host: {0}".format(self.host))
218 transport = client.get_transport()
219 local_addr = ('127.0.0.1', 0) # 0 denotes choose random port
220 channel = transport.open_channel("direct-tcpip",
221 (self.host, int(self.port)),
223 remote_client = paramiko.SSHClient()
224 remote_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
225 # Tunnel to overcloud
227 remote_client.connect('127.0.0.1', port=22, username=self.user,
228 key_filename=self.localkey, sock=channel)
229 except paramiko.SSHException:
230 logger.error("Authentication failed for "
231 "host: {0}".format(self.host))
232 except paramiko.AuthenticationException:
233 logger.error("Authentication failed for "
234 "host: {0}".format(self.host))
236 logger.error("Socker Connection failed for "
237 "undercloud host: {0}".format(self.host))
238 # Download the reports
239 sftp = remote_client.open_sftp()
240 logger.info("Downloading \"{0}\"...".format(reportname))
241 sftp.get(reportfile, ('{0}/{1}'.format(dl_folder, reportname)))
242 logger.info("Downloading \"{0}\"...".format(resultsname))
243 sftp.get(reportfile, ('{0}/{1}'.format(dl_folder, resultsname)))