Extracted all global parameters into functest_constants.py
[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 import functest.utils.functest_constants as ft_constants
19
20 # add installer IP from env
21 INSTALLER_IP = ft_constants.CI_INSTALLER_IP
22
23 # Set up loggers
24 logger = ft_logger.Logger("security_scan").getLogger()
25 paramiko.util.log_to_file("/var/log/paramiko.log")
26
27
28 class SetUp:
29     def __init__(self, *args):
30         self.args = args
31
32     def keystonepass(self):
33         com = self.args[0]
34         client = paramiko.SSHClient()
35         privatekeyfile = os.path.expanduser('/root/.ssh/id_rsa')
36         selectedkey = paramiko.RSAKey.from_private_key_file(privatekeyfile)
37         client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
38         try:
39             client.connect(INSTALLER_IP, port=22, username='stack',
40                            pkey=selectedkey)
41         except paramiko.SSHException:
42             logger.error("Password is invalid for "
43                          "undercloud host: {0}".format(INSTALLER_IP))
44         except paramiko.AuthenticationException:
45             logger.error("Authentication failed for "
46                          "undercloud host: {0}".format(INSTALLER_IP))
47         except socket.error:
48             logger.error("Socker Connection failed for "
49                          "undercloud host: {0}".format(INSTALLER_IP))
50         stdin, stdout, stderr = client.exec_command(com)
51         return stdout.read()
52         client.close()
53
54     def getockey(self):
55         remotekey = self.args[0]
56         localkey = self.args[1]
57         privatekeyfile = os.path.expanduser('/root/.ssh/id_rsa')
58         selectedkey = paramiko.RSAKey.from_private_key_file(privatekeyfile)
59         transport = paramiko.Transport((INSTALLER_IP, 22))
60         transport.connect(username='stack', pkey=selectedkey)
61         try:
62             sftp = paramiko.SFTPClient.from_transport(transport)
63         except paramiko.SSHException:
64             logger.error("Authentication failed for "
65                          "host: {0}".format(INSTALLER_IP))
66         except paramiko.AuthenticationException:
67             logger.error("Authentication failed for "
68                          "host: {0}".format(INSTALLER_IP))
69         except socket.error:
70             logger.error("Socker Connection failed for "
71                          "undercloud host: {0}".format(INSTALLER_IP))
72         sftp.get(remotekey, localkey)
73         sftp.close()
74         transport.close()
75
76
77 class ConnectionManager:
78     def __init__(self, host, port, user, localkey, *args):
79         self.host = host
80         self.port = port
81         self.user = user
82         self.localkey = localkey
83         self.args = args
84
85     def remotescript(self):
86         localpath = self.args[0]
87         remotepath = self.args[1]
88         com = self.args[2]
89
90         client = paramiko.SSHClient()
91         privatekeyfile = os.path.expanduser('/root/.ssh/id_rsa')
92         selectedkey = paramiko.RSAKey.from_private_key_file(privatekeyfile)
93         client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
94         # Connection to undercloud
95         try:
96             client.connect(INSTALLER_IP, port=22, username='stack',
97                            pkey=selectedkey)
98         except paramiko.SSHException:
99             logger.error("Authentication failed for "
100                          "host: {0}".format(self.host))
101         except paramiko.AuthenticationException:
102             logger.error("Authentication failed for "
103                          "host: {0}".format(self.host))
104         except socket.error:
105             logger.error("Socker Connection failed for "
106                          "undercloud host: {0}".format(self.host))
107
108         transport = client.get_transport()
109         local_addr = ('127.0.0.1', 0)
110         channel = transport.open_channel("direct-tcpip",
111                                          (self.host, int(self.port)),
112                                          (local_addr))
113         remote_client = paramiko.SSHClient()
114         remote_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
115         # Tunnel to overcloud
116         try:
117             remote_client.connect('127.0.0.1', port=22, username=self.user,
118                                   key_filename=self.localkey, sock=channel)
119             sftp = remote_client.open_sftp()
120             sftp.put(localpath, remotepath)
121         except paramiko.SSHException:
122             logger.error("Authentication failed for "
123                          "host: {0}".format(self.host))
124         except paramiko.AuthenticationException:
125             logger.error("Authentication failed for "
126                          "host: {0}".format(self.host))
127         except socket.error:
128             logger.error("Socker Connection failed for "
129                          "undercloud host: {0}".format(self.host))
130
131         output = ""
132         stdin, stdout, stderr = remote_client.exec_command(com)
133         stdout = stdout.readlines()
134         # remove script
135         sftp.remove(remotepath)
136         remote_client.close()
137         client.close()
138         # Pipe back stout
139         for line in stdout:
140             output = output + line
141         if output != "":
142             return output
143
144     def remotecmd(self):
145         com = self.args[0]
146
147         client = paramiko.SSHClient()
148         privatekeyfile = os.path.expanduser('/root/.ssh/id_rsa')
149         selectedkey = paramiko.RSAKey.from_private_key_file(privatekeyfile)
150         client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
151         # Connection to undercloud
152         try:
153             client.connect(INSTALLER_IP, port=22, username='stack',
154                            pkey=selectedkey)
155         except paramiko.SSHException:
156             logger.error("Authentication failed for "
157                          "host: {0}".format(self.host))
158         except paramiko.AuthenticationException:
159             logger.error("Authentication failed for "
160                          "host: {0}".format(self.host))
161         except socket.error:
162             logger.error("Socker Connection failed for "
163                          "undercloud host: {0}".format(self.host))
164
165         transport = client.get_transport()
166         local_addr = ('127.0.0.1', 0)  # 0 denotes choose random port
167         channel = transport.open_channel("direct-tcpip",
168                                          (self.host, int(self.port)),
169                                          (local_addr))
170         remote_client = paramiko.SSHClient()
171         remote_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
172         # Tunnel to overcloud
173         try:
174             remote_client.connect('127.0.0.1', port=22, username=self.user,
175                                   key_filename=self.localkey, sock=channel)
176         except paramiko.SSHException:
177             logger.error("Authentication failed for "
178                          "host: {0}".format(self.host))
179         except paramiko.AuthenticationException:
180             logger.error("Authentication failed for "
181                          "host: {0}".format(self.host))
182         except socket.error:
183             logger.error("Socker Connection failed for "
184                          "undercloud host: {0}".format(self.host))
185
186         chan = remote_client.get_transport().open_session()
187         chan.get_pty()
188         feed = chan.makefile()
189         chan.exec_command(com)
190         print feed.read()
191
192         remote_client.close()
193         client.close()
194
195     def download_reports(self):
196         dl_folder = self.args[0]
197         reportfile = self.args[1]
198         reportname = self.args[2]
199         resultsname = self.args[3]
200         client = paramiko.SSHClient()
201         privatekeyfile = os.path.expanduser('/root/.ssh/id_rsa')
202         selectedkey = paramiko.RSAKey.from_private_key_file(privatekeyfile)
203         client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
204         # Connection to overcloud
205         try:
206             client.connect(INSTALLER_IP, port=22, username='stack',
207                            pkey=selectedkey)
208         except paramiko.SSHException:
209             logger.error("Authentication failed for "
210                          "host: {0}".format(self.host))
211         except paramiko.AuthenticationException:
212             logger.error("Authentication failed for "
213                          "host: {0}".format(self.host))
214         except socket.error:
215             logger.error("Socker Connection failed for "
216                          "undercloud host: {0}".format(self.host))
217
218         transport = client.get_transport()
219         local_addr = ('127.0.0.1', 0)  # 0 denotes choose random port
220         channel = transport.open_channel("direct-tcpip",
221                                          (self.host, int(self.port)),
222                                          (local_addr))
223         remote_client = paramiko.SSHClient()
224         remote_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
225         # Tunnel to overcloud
226         try:
227             remote_client.connect('127.0.0.1', port=22, username=self.user,
228                                   key_filename=self.localkey, sock=channel)
229         except paramiko.SSHException:
230             logger.error("Authentication failed for "
231                          "host: {0}".format(self.host))
232         except paramiko.AuthenticationException:
233             logger.error("Authentication failed for "
234                          "host: {0}".format(self.host))
235         except socket.error:
236             logger.error("Socker Connection failed for "
237                          "undercloud host: {0}".format(self.host))
238         # Download the reports
239         sftp = remote_client.open_sftp()
240         logger.info("Downloading \"{0}\"...".format(reportname))
241         sftp.get(reportfile, ('{0}/{1}'.format(dl_folder, reportname)))
242         logger.info("Downloading \"{0}\"...".format(resultsname))
243         sftp.get(reportfile, ('{0}/{1}'.format(dl_folder, resultsname)))
244         sftp.close()
245         transport.close()