18ca96d80812360b0d02f2e1f9d5dbb3519fe7ad
[functest.git] / functest / opnfv_tests / security_scan / connect.py
1 #!/usr/bin/python
2 #
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
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # 0.1: OpenSCAP paramiko connection functions
12
13 import os
14 import socket
15 import paramiko
16
17 import functest.utils.functest_logger as ft_logger
18
19 # add installer IP from env
20 INSTALLER_IP = os.getenv('INSTALLER_IP')
21
22 # Set up loggers
23 logger = ft_logger.Logger("security_scan").getLogger()
24 paramiko.util.log_to_file("/var/log/paramiko.log")
25
26
27 class SetUp:
28     def __init__(self, *args):
29         self.args = args
30
31     def keystonepass(self):
32         com = self.args[0]
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())
37         try:
38             client.connect(INSTALLER_IP, port=22, username='stack',
39                            pkey=selectedkey)
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))
46         except socket.error:
47             logger.error("Socker Connection failed for "
48                          "undercloud host: {0}".format(INSTALLER_IP))
49         stdin, stdout, stderr = client.exec_command(com)
50         return stdout.read()
51         client.close()
52
53     def getockey(self):
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)
60         try:
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))
68         except socket.error:
69             logger.error("Socker Connection failed for "
70                          "undercloud host: {0}".format(INSTALLER_IP))
71         sftp.get(remotekey, localkey)
72         sftp.close()
73         transport.close()
74
75
76 class ConnectionManager:
77     def __init__(self, host, port, user, localkey, *args):
78         self.host = host
79         self.port = port
80         self.user = user
81         self.localkey = localkey
82         self.args = args
83
84     def remotescript(self):
85         localpath = self.args[0]
86         remotepath = self.args[1]
87         com = self.args[2]
88
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
94         try:
95             client.connect(INSTALLER_IP, port=22, username='stack',
96                            pkey=selectedkey)
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))
103         except socket.error:
104             logger.error("Socker Connection failed for "
105                          "undercloud host: {0}".format(self.host))
106
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)),
111                                          (local_addr))
112         remote_client = paramiko.SSHClient()
113         remote_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
114         # Tunnel to overcloud
115         try:
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))
126         except socket.error:
127             logger.error("Socker Connection failed for "
128                          "undercloud host: {0}".format(self.host))
129
130         output = ""
131         stdin, stdout, stderr = remote_client.exec_command(com)
132         stdout = stdout.readlines()
133         # remove script
134         sftp.remove(remotepath)
135         remote_client.close()
136         client.close()
137         # Pipe back stout
138         for line in stdout:
139             output = output + line
140         if output != "":
141             return output
142
143     def remotecmd(self):
144         com = self.args[0]
145
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
151         try:
152             client.connect(INSTALLER_IP, port=22, username='stack',
153                            pkey=selectedkey)
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))
160         except socket.error:
161             logger.error("Socker Connection failed for "
162                          "undercloud host: {0}".format(self.host))
163
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)),
168                                          (local_addr))
169         remote_client = paramiko.SSHClient()
170         remote_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
171         # Tunnel to overcloud
172         try:
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))
181         except socket.error:
182             logger.error("Socker Connection failed for "
183                          "undercloud host: {0}".format(self.host))
184
185         chan = remote_client.get_transport().open_session()
186         chan.get_pty()
187         feed = chan.makefile()
188         chan.exec_command(com)
189         print feed.read()
190
191         remote_client.close()
192         client.close()
193
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
204         try:
205             client.connect(INSTALLER_IP, port=22, username='stack',
206                            pkey=selectedkey)
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))
213         except socket.error:
214             logger.error("Socker Connection failed for "
215                          "undercloud host: {0}".format(self.host))
216
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)),
221                                          (local_addr))
222         remote_client = paramiko.SSHClient()
223         remote_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
224         # Tunnel to overcloud
225         try:
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))
234         except socket.error:
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)))
243         sftp.close()
244         transport.close()