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
19 # add installer IP from env
20 INSTALLER_IP = os.getenv('INSTALLER_IP')
23 logger = ft_logger.Logger("security_scan").getLogger()
24 paramiko.util.log_to_file("/var/log/paramiko.log")
28 def __init__(self, *args):
31 def keystonepass(self):
33 client = paramiko.SSHClient()
34 privatekeyfile = os.path.expanduser('/root/.ssh/id_rsa')
35 selectedkey = paramiko.RSAKey.from_private_key_file(privatekeyfile)
36 client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
38 client.connect(INSTALLER_IP, port=22, username='stack',
40 except paramiko.SSHException:
41 logger.error("Password is invalid for "
42 "undercloud host: {0}".format(INSTALLER_IP))
43 except paramiko.AuthenticationException:
44 logger.error("Authentication failed for "
45 "undercloud host: {0}".format(INSTALLER_IP))
47 logger.error("Socker Connection failed for "
48 "undercloud host: {0}".format(INSTALLER_IP))
49 stdin, stdout, stderr = client.exec_command(com)
54 remotekey = self.args[0]
55 localkey = self.args[1]
56 privatekeyfile = os.path.expanduser('/root/.ssh/id_rsa')
57 selectedkey = paramiko.RSAKey.from_private_key_file(privatekeyfile)
58 transport = paramiko.Transport((INSTALLER_IP, 22))
59 transport.connect(username='stack', pkey=selectedkey)
61 sftp = paramiko.SFTPClient.from_transport(transport)
62 except paramiko.SSHException:
63 logger.error("Authentication failed for "
64 "host: {0}".format(INSTALLER_IP))
65 except paramiko.AuthenticationException:
66 logger.error("Authentication failed for "
67 "host: {0}".format(INSTALLER_IP))
69 logger.error("Socker Connection failed for "
70 "undercloud host: {0}".format(INSTALLER_IP))
71 sftp.get(remotekey, localkey)
76 class ConnectionManager:
77 def __init__(self, host, port, user, localkey, *args):
81 self.localkey = localkey
84 def remotescript(self):
85 localpath = self.args[0]
86 remotepath = self.args[1]
89 client = paramiko.SSHClient()
90 privatekeyfile = os.path.expanduser('/root/.ssh/id_rsa')
91 selectedkey = paramiko.RSAKey.from_private_key_file(privatekeyfile)
92 client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
93 # Connection to undercloud
95 client.connect(INSTALLER_IP, port=22, username='stack',
97 except paramiko.SSHException:
98 logger.error("Authentication failed for "
99 "host: {0}".format(self.host))
100 except paramiko.AuthenticationException:
101 logger.error("Authentication failed for "
102 "host: {0}".format(self.host))
104 logger.error("Socker Connection failed for "
105 "undercloud host: {0}".format(self.host))
107 transport = client.get_transport()
108 local_addr = ('127.0.0.1', 0)
109 channel = transport.open_channel("direct-tcpip",
110 (self.host, int(self.port)),
112 remote_client = paramiko.SSHClient()
113 remote_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
114 # Tunnel to overcloud
116 remote_client.connect('127.0.0.1', port=22, username=self.user,
117 key_filename=self.localkey, sock=channel)
118 sftp = remote_client.open_sftp()
119 sftp.put(localpath, remotepath)
120 except paramiko.SSHException:
121 logger.error("Authentication failed for "
122 "host: {0}".format(self.host))
123 except paramiko.AuthenticationException:
124 logger.error("Authentication failed for "
125 "host: {0}".format(self.host))
127 logger.error("Socker Connection failed for "
128 "undercloud host: {0}".format(self.host))
131 stdin, stdout, stderr = remote_client.exec_command(com)
132 stdout = stdout.readlines()
134 sftp.remove(remotepath)
135 remote_client.close()
139 output = output + line
146 client = paramiko.SSHClient()
147 privatekeyfile = os.path.expanduser('/root/.ssh/id_rsa')
148 selectedkey = paramiko.RSAKey.from_private_key_file(privatekeyfile)
149 client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
150 # Connection to undercloud
152 client.connect(INSTALLER_IP, port=22, username='stack',
154 except paramiko.SSHException:
155 logger.error("Authentication failed for "
156 "host: {0}".format(self.host))
157 except paramiko.AuthenticationException:
158 logger.error("Authentication failed for "
159 "host: {0}".format(self.host))
161 logger.error("Socker Connection failed for "
162 "undercloud host: {0}".format(self.host))
164 transport = client.get_transport()
165 local_addr = ('127.0.0.1', 0) # 0 denotes choose random port
166 channel = transport.open_channel("direct-tcpip",
167 (self.host, int(self.port)),
169 remote_client = paramiko.SSHClient()
170 remote_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
171 # Tunnel to overcloud
173 remote_client.connect('127.0.0.1', port=22, username=self.user,
174 key_filename=self.localkey, sock=channel)
175 except paramiko.SSHException:
176 logger.error("Authentication failed for "
177 "host: {0}".format(self.host))
178 except paramiko.AuthenticationException:
179 logger.error("Authentication failed for "
180 "host: {0}".format(self.host))
182 logger.error("Socker Connection failed for "
183 "undercloud host: {0}".format(self.host))
185 chan = remote_client.get_transport().open_session()
187 feed = chan.makefile()
188 chan.exec_command(com)
191 remote_client.close()
194 def download_reports(self):
195 dl_folder = self.args[0]
196 reportfile = self.args[1]
197 reportname = self.args[2]
198 resultsname = self.args[3]
199 client = paramiko.SSHClient()
200 privatekeyfile = os.path.expanduser('/root/.ssh/id_rsa')
201 selectedkey = paramiko.RSAKey.from_private_key_file(privatekeyfile)
202 client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
203 # Connection to overcloud
205 client.connect(INSTALLER_IP, port=22, username='stack',
207 except paramiko.SSHException:
208 logger.error("Authentication failed for "
209 "host: {0}".format(self.host))
210 except paramiko.AuthenticationException:
211 logger.error("Authentication failed for "
212 "host: {0}".format(self.host))
214 logger.error("Socker Connection failed for "
215 "undercloud host: {0}".format(self.host))
217 transport = client.get_transport()
218 local_addr = ('127.0.0.1', 0) # 0 denotes choose random port
219 channel = transport.open_channel("direct-tcpip",
220 (self.host, int(self.port)),
222 remote_client = paramiko.SSHClient()
223 remote_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
224 # Tunnel to overcloud
226 remote_client.connect('127.0.0.1', port=22, username=self.user,
227 key_filename=self.localkey, sock=channel)
228 except paramiko.SSHException:
229 logger.error("Authentication failed for "
230 "host: {0}".format(self.host))
231 except paramiko.AuthenticationException:
232 logger.error("Authentication failed for "
233 "host: {0}".format(self.host))
235 logger.error("Socker Connection failed for "
236 "undercloud host: {0}".format(self.host))
237 # Download the reports
238 sftp = remote_client.open_sftp()
239 logger.info("Downloading \"{0}\"...".format(reportname))
240 sftp.get(reportfile, ('{0}/{1}'.format(dl_folder, reportname)))
241 logger.info("Downloading \"{0}\"...".format(resultsname))
242 sftp.get(reportfile, ('{0}/{1}'.format(dl_folder, resultsname)))