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")
27 def __init__(self, *args):
30 def keystonepass(self):
32 client = paramiko.SSHClient()
33 client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
35 client.connect(INSTALLER_IP, port=22, username='stack')
36 except paramiko.SSHException:
37 logger.error("Password is invalid for "
38 "undercloud host: {0}".format(INSTALLER_IP))
39 except paramiko.AuthenticationException:
40 logger.error("Authentication failed for "
41 "undercloud host: {0}".format(INSTALLER_IP))
43 logger.error("Socker Connection failed for "
44 "undercloud host: {0}".format(INSTALLER_IP))
45 stdin, stdout, stderr = client.exec_command(com)
50 remotekey = self.args[0]
51 localkey = self.args[1]
52 client = paramiko.SSHClient()
53 client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
55 client.connect(INSTALLER_IP, port=22, username='stack')
56 sftp = client.open_sftp()
57 sftp.get(remotekey, localkey)
58 except paramiko.SSHException:
59 logger.error("Authentication failed for "
60 "host: {0}".format(self.host))
61 except paramiko.AuthenticationException:
62 logger.error("Authentication failed for "
63 "host: {0}".format(self.host))
65 logger.error("Socker Connection failed for "
66 "undercloud host: {0}".format(self.host))
70 class connectionManager:
71 def __init__(self, host, port, user, localkey, *args):
75 self.localkey = localkey
78 def remotescript(self):
79 localpath = self.args[0]
80 remotepath = self.args[1]
83 client = paramiko.SSHClient()
84 client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
85 # Connection to undercloud
87 client.connect(INSTALLER_IP, port=22, username='stack')
88 except paramiko.SSHException:
89 logger.error("Authentication failed for "
90 "host: {0}".format(self.host))
91 except paramiko.AuthenticationException:
92 logger.error("Authentication failed for "
93 "host: {0}".format(self.host))
95 logger.error("Socker Connection failed for "
96 "undercloud host: {0}".format(self.host))
98 transport = client.get_transport()
99 local_addr = ('127.0.0.1', 0)
100 channel = transport.open_channel("direct-tcpip",
101 (self.host, int(self.port)),
103 remote_client = paramiko.SSHClient()
104 remote_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
105 # Tunnel to overcloud
107 remote_client.connect('127.0.0.1', port=22, username=self.user,
108 key_filename=self.localkey, sock=channel)
109 sftp = remote_client.open_sftp()
110 sftp.put(localpath, remotepath)
111 except paramiko.SSHException:
112 logger.error("Authentication failed for "
113 "host: {0}".format(self.host))
114 except paramiko.AuthenticationException:
115 logger.error("Authentication failed for "
116 "host: {0}".format(self.host))
118 logger.error("Socker Connection failed for "
119 "undercloud host: {0}".format(self.host))
122 stdin, stdout, stderr = remote_client.exec_command(com)
123 stdout = stdout.readlines()
125 sftp.remove(remotepath)
126 remote_client.close()
130 output = output + line
137 client = paramiko.SSHClient()
138 client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
139 # Connection to undercloud
141 client.connect(INSTALLER_IP, port=22, username='stack')
142 except paramiko.SSHException:
143 logger.error("Authentication failed for "
144 "host: {0}".format(self.host))
145 except paramiko.AuthenticationException:
146 logger.error("Authentication failed for "
147 "host: {0}".format(self.host))
149 logger.error("Socker Connection failed for "
150 "undercloud host: {0}".format(self.host))
152 transport = client.get_transport()
153 local_addr = ('127.0.0.1', 0) # 0 denotes choose random port
154 channel = transport.open_channel("direct-tcpip",
155 (self.host, int(self.port)),
157 remote_client = paramiko.SSHClient()
158 remote_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
159 # Tunnel to overcloud
161 remote_client.connect('127.0.0.1', port=22, username=self.user,
162 key_filename=self.localkey, sock=channel)
163 except paramiko.SSHException:
164 logger.error("Authentication failed for "
165 "host: {0}".format(self.host))
166 except paramiko.AuthenticationException:
167 logger.error("Authentication failed for "
168 "host: {0}".format(self.host))
170 logger.error("Socker Connection failed for "
171 "undercloud host: {0}".format(self.host))
173 chan = remote_client.get_transport().open_session()
176 chan.exec_command(com)
179 remote_client.close()
182 def download_reports(self):
183 dl_folder = self.args[0]
184 reportfile = self.args[1]
185 reportname = self.args[2]
186 resultsname = self.args[3]
187 client = paramiko.SSHClient()
188 client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
189 # Connection to overcloud
191 client.connect(INSTALLER_IP, port=22, username='stack')
192 except paramiko.SSHException:
193 logger.error("Authentication failed for "
194 "host: {0}".format(self.host))
195 except paramiko.AuthenticationException:
196 logger.error("Authentication failed for "
197 "host: {0}".format(self.host))
199 logger.error("Socker Connection failed for "
200 "undercloud host: {0}".format(self.host))
202 transport = client.get_transport()
203 local_addr = ('127.0.0.1', 0) # 0 denotes choose random port
204 channel = transport.open_channel("direct-tcpip",
205 (self.host, int(self.port)),
207 remote_client = paramiko.SSHClient()
208 remote_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
209 # Tunnel to overcloud
211 remote_client.connect('127.0.0.1', port=22, username=self.user,
212 key_filename=self.localkey, sock=channel)
213 except paramiko.SSHException:
214 logger.error("Authentication failed for "
215 "host: {0}".format(self.host))
216 except paramiko.AuthenticationException:
217 logger.error("Authentication failed for "
218 "host: {0}".format(self.host))
220 logger.error("Socker Connection failed for "
221 "undercloud host: {0}".format(self.host))
222 # Download the reports
223 sftp = remote_client.open_sftp()
224 logger.info("Downloading \"{0}\"...".format(reportname))
225 sftp.get(reportfile, ('{0}/{1}'.format(dl_folder, reportname)))
226 logger.info("Downloading \"{0}\"...".format(resultsname))
227 sftp.get(reportfile, ('{0}/{1}'.format(dl_folder, resultsname)))