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