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()
25 paramiko.util.log_to_file("/var/log/paramiko.log")
29 def __init__(self, *args):
32 def keystonepass(self):
34 client = paramiko.SSHClient()
35 client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
37 client.connect(INSTALLER_IP, port=22, username='stack')
38 except paramiko.SSHException:
39 logger.error("Password is invalid for "
40 "undercloud host: {0}").format(INSTALLER_IP)
41 except paramiko.AuthenticationException:
42 logger.error("Authentication failed for "
43 "undercloud host: {0}").format(INSTALLER_IP)
45 logger.error("Socker Connection failed for "
46 "undercloud host: {0}").format(INSTALLER_IP)
47 stdin, stdout, stderr = client.exec_command(com)
51 class connectionManager:
52 def __init__(self, host, port, user, user_key, *args):
56 self.user_key = user_key
59 def remotescript(self):
60 localpath = self.args[0]
61 remotepath = self.args[1]
64 client = paramiko.SSHClient()
65 client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
66 # Connection to undercloud
68 client.connect(INSTALLER_IP, port=22, username='stack')
69 except paramiko.SSHException:
70 logger.error("Authentication failed for "
71 "host: {0}").format(self.host)
72 except paramiko.AuthenticationException:
73 logger.error("Authentication failed for "
74 "host: {0}").format(self.host)
76 logger.error("Socker Connection failed for "
77 "undercloud host: {0}").format(self.host)
79 transport = client.get_transport()
80 local_addr = ('127.0.0.1', 0)
81 channel = transport.open_channel("direct-tcpip",
82 (self.host, int(self.port)),
84 remote_client = paramiko.SSHClient()
85 remote_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
88 remote_client.connect('127.0.0.1', port=22, username=self.user,
89 key_filename=self.user_key, sock=channel)
90 sftp = remote_client.open_sftp()
91 sftp.put(localpath, remotepath)
92 except paramiko.SSHException:
93 logger.error("Authentication failed for "
94 "host: {0}").format(self.host)
95 except paramiko.AuthenticationException:
96 logger.error("Authentication failed for "
97 "host: {0}").format(self.host)
99 logger.error("Socker Connection failed for "
100 "undercloud host: {0}").format(self.host)
103 stdin, stdout, stderr = remote_client.exec_command(com)
104 stdout = stdout.readlines()
106 sftp.remove(remotepath)
107 remote_client.close()
111 output = output + line
118 client = paramiko.SSHClient()
119 client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
120 # Connection to undercloud
122 client.connect(INSTALLER_IP, port=22, username='stack')
123 except paramiko.SSHException:
124 logger.error("Authentication failed for "
125 "host: {0}").format(self.host)
126 except paramiko.AuthenticationException:
127 logger.error("Authentication failed for "
128 "host: {0}").format(self.host)
130 logger.error("Socker Connection failed for "
131 "undercloud host: {0}").format(self.host)
133 transport = client.get_transport()
134 local_addr = ('127.0.0.1', 0) # 0 denotes choose random port
135 channel = transport.open_channel("direct-tcpip",
136 (self.host, int(self.port)),
138 remote_client = paramiko.SSHClient()
139 remote_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
140 # Tunnel to overcloud
142 remote_client.connect('127.0.0.1', port=22, username=self.user,
143 key_filename=self.user_key, sock=channel)
144 except paramiko.SSHException:
145 logger.error("Authentication failed for "
146 "host: {0}").format(self.host)
147 except paramiko.AuthenticationException:
148 logger.error("Authentication failed for "
149 "host: {0}").format(self.host)
151 logger.error("Socker Connection failed for "
152 "undercloud host: {0}").format(self.host)
154 chan = remote_client.get_transport().open_session()
157 chan.exec_command(com)
160 remote_client.close()
163 def download_reports(self):
164 dl_folder = self.args[0]
165 reportfile = self.args[1]
166 reportname = self.args[2]
167 resultsname = self.args[3]
168 client = paramiko.SSHClient()
169 client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
170 # Connection to overcloud
172 client.connect(INSTALLER_IP, port=22, username='stack')
173 except paramiko.SSHException:
174 logger.error("Authentication failed for "
175 "host: {0}").format(self.host)
176 except paramiko.AuthenticationException:
177 logger.error("Authentication failed for "
178 "host: {0}").format(self.host)
180 logger.error("Socker Connection failed for "
181 "undercloud host: {0}").format(self.host)
183 transport = client.get_transport()
184 local_addr = ('127.0.0.1', 0) # 0 denotes choose random port
185 channel = transport.open_channel("direct-tcpip",
186 (self.host, int(self.port)),
188 remote_client = paramiko.SSHClient()
189 remote_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
190 # Tunnel to overcloud
192 remote_client.connect('127.0.0.1', port=22, username=self.user,
193 key_filename=self.user_key, sock=channel)
194 except paramiko.SSHException:
195 logger.error("Authentication failed for "
196 "host: {0}").format(self.host)
197 except paramiko.AuthenticationException:
198 logger.error("Authentication failed for "
199 "host: {0}").format(self.host)
201 logger.error("Socker Connection failed for "
202 "undercloud host: {0}").format(self.host)
203 # Download the reports
204 sftp = remote_client.open_sftp()
205 logger.info('Downloading \"{0}\"...\n').format(reportname)
206 sftp.get(reportfile, ('{0}/{1}'.format(dl_folder, reportname)))
207 logger.info('Downloading \"{0}\"...\n').format(resultsname)
208 sftp.get(reportfile, ('{0}/{1}'.format(dl_folder, resultsname)))