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 # Enable below for connection debugging
20 # paramiko.util.log_to_file('ssh.log')
22 # add installer IP from env
23 INSTALLER_IP = os.getenv('INSTALLER_IP')
26 logger = ft_logger.Logger("security_scan").getLogger()
30 def __init__(self, *args):
33 def keystonepass(self):
35 client = paramiko.SSHClient()
36 client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
38 client.connect(INSTALLER_IP, port=22, username='stack')
39 except paramiko.SSHException:
40 logger.error("Password is invalid for "
41 "undercloud host: {0}").format(INSTALLER_IP)
42 except paramiko.AuthenticationException:
43 logger.error("Authentication failed for "
44 "undercloud host: {0}").format(INSTALLER_IP)
46 logger.error("Socker Connection failed for "
47 "undercloud host: {0}").format(INSTALLER_IP)
48 stdin, stdout, stderr = client.exec_command(com)
52 class connectionManager:
53 def __init__(self, host, port, user, user_key, *args):
57 self.user_key = user_key
60 def remotescript(self):
61 localpath = self.args[0]
62 remotepath = self.args[1]
65 client = paramiko.SSHClient()
66 client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
67 # Connection to undercloud
69 client.connect(INSTALLER_IP, port=22, username='stack')
70 except paramiko.SSHException:
71 logger.error("Authentication failed for "
72 "host: {0}").format(self.host)
73 except paramiko.AuthenticationException:
74 logger.error("Authentication failed for "
75 "host: {0}").format(self.host)
77 logger.error("Socker Connection failed for "
78 "undercloud host: {0}").format(self.host)
80 transport = client.get_transport()
81 local_addr = ('127.0.0.1', 0)
82 channel = transport.open_channel("direct-tcpip",
83 (self.host, int(self.port)),
85 remote_client = paramiko.SSHClient()
86 remote_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
89 remote_client.connect('127.0.0.1', port=22, username=self.user,
90 key_filename=self.user_key, sock=channel)
91 sftp = remote_client.open_sftp()
92 sftp.put(localpath, remotepath)
93 except paramiko.SSHException:
94 logger.error("Authentication failed for "
95 "host: {0}").format(self.host)
96 except paramiko.AuthenticationException:
97 logger.error("Authentication failed for "
98 "host: {0}").format(self.host)
100 logger.error("Socker Connection failed for "
101 "undercloud host: {0}").format(self.host)
104 stdin, stdout, stderr = remote_client.exec_command(com)
105 stdout = stdout.readlines()
107 sftp.remove(remotepath)
108 remote_client.close()
112 output = output + line
119 client = paramiko.SSHClient()
120 client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
121 # Connection to undercloud
123 client.connect(INSTALLER_IP, port=22, username='stack')
124 except paramiko.SSHException:
125 logger.error("Authentication failed for "
126 "host: {0}").format(self.host)
127 except paramiko.AuthenticationException:
128 logger.error("Authentication failed for "
129 "host: {0}").format(self.host)
131 logger.error("Socker Connection failed for "
132 "undercloud host: {0}").format(self.host)
134 transport = client.get_transport()
135 local_addr = ('127.0.0.1', 0) # 0 denotes choose random port
136 channel = transport.open_channel("direct-tcpip",
137 (self.host, int(self.port)),
139 remote_client = paramiko.SSHClient()
140 remote_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
141 # Tunnel to overcloud
143 remote_client.connect('127.0.0.1', port=22, username=self.user,
144 key_filename=self.user_key, sock=channel)
145 except paramiko.SSHException:
146 logger.error("Authentication failed for "
147 "host: {0}").format(self.host)
148 except paramiko.AuthenticationException:
149 logger.error("Authentication failed for "
150 "host: {0}").format(self.host)
152 logger.error("Socker Connection failed for "
153 "undercloud host: {0}").format(self.host)
155 chan = remote_client.get_transport().open_session()
158 chan.exec_command(com)
161 remote_client.close()
164 def download_reports(self):
165 dl_folder = self.args[0]
166 reportfile = self.args[1]
167 reportname = self.args[2]
168 resultsname = self.args[3]
169 client = paramiko.SSHClient()
170 client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
171 # Connection to overcloud
173 client.connect(INSTALLER_IP, port=22, username='stack')
174 except paramiko.SSHException:
175 logger.error("Authentication failed for "
176 "host: {0}").format(self.host)
177 except paramiko.AuthenticationException:
178 logger.error("Authentication failed for "
179 "host: {0}").format(self.host)
181 logger.error("Socker Connection failed for "
182 "undercloud host: {0}").format(self.host)
184 transport = client.get_transport()
185 local_addr = ('127.0.0.1', 0) # 0 denotes choose random port
186 channel = transport.open_channel("direct-tcpip",
187 (self.host, int(self.port)),
189 remote_client = paramiko.SSHClient()
190 remote_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
191 # Tunnel to overcloud
193 remote_client.connect('127.0.0.1', port=22, username=self.user,
194 key_filename=self.user_key, sock=channel)
195 except paramiko.SSHException:
196 logger.error("Authentication failed for "
197 "host: {0}").format(self.host)
198 except paramiko.AuthenticationException:
199 logger.error("Authentication failed for "
200 "host: {0}").format(self.host)
202 logger.error("Socker Connection failed for "
203 "undercloud host: {0}").format(self.host)
204 # Download the reports
205 sftp = remote_client.open_sftp()
206 logger.info('Downloading \"{0}\"...\n').format(reportname)
207 sftp.get(reportfile, ('{0}/{1}'.format(dl_folder, reportname)))
208 logger.info('Downloading \"{0}\"...\n').format(resultsname)
209 sftp.get(reportfile, ('{0}/{1}'.format(dl_folder, resultsname)))