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 client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
36 client.connect(INSTALLER_IP, port=22, username='stack')
37 except paramiko.SSHException:
38 logger.error("Password is invalid for "
39 "undercloud host: {0}".format(INSTALLER_IP))
40 except paramiko.AuthenticationException:
41 logger.error("Authentication failed for "
42 "undercloud host: {0}".format(INSTALLER_IP))
44 logger.error("Socker Connection failed for "
45 "undercloud host: {0}".format(INSTALLER_IP))
46 stdin, stdout, stderr = client.exec_command(com)
51 remotekey = self.args[0]
52 localkey = self.args[1]
53 client = paramiko.SSHClient()
54 client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
56 client.connect(INSTALLER_IP, port=22, username='stack')
57 sftp = client.open_sftp()
58 sftp.get(remotekey, localkey)
59 except paramiko.SSHException:
60 logger.error("Authentication failed for "
61 "host: {0}".format(self.host))
62 except paramiko.AuthenticationException:
63 logger.error("Authentication failed for "
64 "host: {0}".format(self.host))
66 logger.error("Socker Connection failed for "
67 "undercloud host: {0}".format(self.host))
71 class connectionManager:
72 def __init__(self, host, port, user, localkey, *args):
76 self.localkey = localkey
79 def remotescript(self):
80 localpath = self.args[0]
81 remotepath = self.args[1]
84 client = paramiko.SSHClient()
85 client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
86 # Connection to undercloud
88 client.connect(INSTALLER_IP, port=22, username='stack')
89 except paramiko.SSHException:
90 logger.error("Authentication failed for "
91 "host: {0}".format(self.host))
92 except paramiko.AuthenticationException:
93 logger.error("Authentication failed for "
94 "host: {0}".format(self.host))
96 logger.error("Socker Connection failed for "
97 "undercloud host: {0}".format(self.host))
99 transport = client.get_transport()
100 local_addr = ('127.0.0.1', 0)
101 channel = transport.open_channel("direct-tcpip",
102 (self.host, int(self.port)),
104 remote_client = paramiko.SSHClient()
105 remote_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
106 # Tunnel to overcloud
108 remote_client.connect('127.0.0.1', port=22, username=self.user,
109 key_filename=self.localkey, sock=channel)
110 sftp = remote_client.open_sftp()
111 sftp.put(localpath, remotepath)
112 except paramiko.SSHException:
113 logger.error("Authentication failed for "
114 "host: {0}".format(self.host))
115 except paramiko.AuthenticationException:
116 logger.error("Authentication failed for "
117 "host: {0}".format(self.host))
119 logger.error("Socker Connection failed for "
120 "undercloud host: {0}".format(self.host))
123 stdin, stdout, stderr = remote_client.exec_command(com)
124 stdout = stdout.readlines()
126 sftp.remove(remotepath)
127 remote_client.close()
131 output = output + line
138 client = paramiko.SSHClient()
139 client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
140 # Connection to undercloud
142 client.connect(INSTALLER_IP, port=22, username='stack')
143 except paramiko.SSHException:
144 logger.error("Authentication failed for "
145 "host: {0}".format(self.host))
146 except paramiko.AuthenticationException:
147 logger.error("Authentication failed for "
148 "host: {0}".format(self.host))
150 logger.error("Socker Connection failed for "
151 "undercloud host: {0}".format(self.host))
153 transport = client.get_transport()
154 local_addr = ('127.0.0.1', 0) # 0 denotes choose random port
155 channel = transport.open_channel("direct-tcpip",
156 (self.host, int(self.port)),
158 remote_client = paramiko.SSHClient()
159 remote_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
160 # Tunnel to overcloud
162 remote_client.connect('127.0.0.1', port=22, username=self.user,
163 key_filename=self.localkey, sock=channel)
164 except paramiko.SSHException:
165 logger.error("Authentication failed for "
166 "host: {0}".format(self.host))
167 except paramiko.AuthenticationException:
168 logger.error("Authentication failed for "
169 "host: {0}".format(self.host))
171 logger.error("Socker Connection failed for "
172 "undercloud host: {0}".format(self.host))
174 chan = remote_client.get_transport().open_session()
177 chan.exec_command(com)
180 remote_client.close()
183 def download_reports(self):
184 dl_folder = self.args[0]
185 reportfile = self.args[1]
186 reportname = self.args[2]
187 resultsname = self.args[3]
188 client = paramiko.SSHClient()
189 client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
190 # Connection to overcloud
192 client.connect(INSTALLER_IP, port=22, username='stack')
193 except paramiko.SSHException:
194 logger.error("Authentication failed for "
195 "host: {0}".format(self.host))
196 except paramiko.AuthenticationException:
197 logger.error("Authentication failed for "
198 "host: {0}".format(self.host))
200 logger.error("Socker Connection failed for "
201 "undercloud host: {0}".format(self.host))
203 transport = client.get_transport()
204 local_addr = ('127.0.0.1', 0) # 0 denotes choose random port
205 channel = transport.open_channel("direct-tcpip",
206 (self.host, int(self.port)),
208 remote_client = paramiko.SSHClient()
209 remote_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
210 # Tunnel to overcloud
212 remote_client.connect('127.0.0.1', port=22, username=self.user,
213 key_filename=self.localkey, sock=channel)
214 except paramiko.SSHException:
215 logger.error("Authentication failed for "
216 "host: {0}".format(self.host))
217 except paramiko.AuthenticationException:
218 logger.error("Authentication failed for "
219 "host: {0}".format(self.host))
221 logger.error("Socker Connection failed for "
222 "undercloud host: {0}".format(self.host))
223 # Download the reports
224 sftp = remote_client.open_sftp()
225 logger.info("Downloading \"{0}\"...".format(reportname))
226 sftp.get(reportfile, ('{0}/{1}'.format(dl_folder, reportname)))
227 logger.info("Downloading \"{0}\"...".format(resultsname))
228 sftp.get(reportfile, ('{0}/{1}'.format(dl_folder, resultsname)))