Fix flake8 violations
[functest.git] / testcases / 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         client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
35         try:
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))
43         except socket.error:
44             logger.error("Socker Connection failed for "
45                          "undercloud host: {0}".format(INSTALLER_IP))
46         stdin, stdout, stderr = client.exec_command(com)
47         return stdout.read()
48         client.close()
49
50     def getOCKey(self):
51         remotekey = self.args[0]
52         localkey = self.args[1]
53         client = paramiko.SSHClient()
54         client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
55         try:
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))
65         except socket.error:
66             logger.error("Socker Connection failed for "
67                          "undercloud host: {0}".format(self.host))
68         client.close()
69
70
71 class connectionManager:
72     def __init__(self, host, port, user, localkey, *args):
73         self.host = host
74         self.port = port
75         self.user = user
76         self.localkey = localkey
77         self.args = args
78
79     def remotescript(self):
80         localpath = self.args[0]
81         remotepath = self.args[1]
82         com = self.args[2]
83
84         client = paramiko.SSHClient()
85         client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
86         # Connection to undercloud
87         try:
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))
95         except socket.error:
96             logger.error("Socker Connection failed for "
97                          "undercloud host: {0}".format(self.host))
98
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)),
103                                          (local_addr))
104         remote_client = paramiko.SSHClient()
105         remote_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
106         # Tunnel to overcloud
107         try:
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))
118         except socket.error:
119             logger.error("Socker Connection failed for "
120                          "undercloud host: {0}".format(self.host))
121
122         output = ""
123         stdin, stdout, stderr = remote_client.exec_command(com)
124         stdout = stdout.readlines()
125         # remove script
126         sftp.remove(remotepath)
127         remote_client.close()
128         client.close()
129         # Pipe back stout
130         for line in stdout:
131             output = output + line
132         if output != "":
133             return output
134
135     def remotecmd(self):
136         com = self.args[0]
137
138         client = paramiko.SSHClient()
139         client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
140         # Connection to undercloud
141         try:
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))
149         except socket.error:
150             logger.error("Socker Connection failed for "
151                          "undercloud host: {0}".format(self.host))
152
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)),
157                                          (local_addr))
158         remote_client = paramiko.SSHClient()
159         remote_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
160         # Tunnel to overcloud
161         try:
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))
170         except socket.error:
171             logger.error("Socker Connection failed for "
172                          "undercloud host: {0}".format(self.host))
173
174         chan = remote_client.get_transport().open_session()
175         chan.get_pty()
176         f = chan.makefile()
177         chan.exec_command(com)
178         print f.read()
179
180         remote_client.close()
181         client.close()
182
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
191         try:
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))
199         except socket.error:
200             logger.error("Socker Connection failed for "
201                          "undercloud host: {0}".format(self.host))
202
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)),
207                                          (local_addr))
208         remote_client = paramiko.SSHClient()
209         remote_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
210         # Tunnel to overcloud
211         try:
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))
220         except socket.error:
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)))
229         sftp.close()
230         transport.close()