Add Apex based security scan code
[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 # Enable below for connection debugging
20 # paramiko.util.log_to_file('ssh.log')
21
22 # add installer IP from env
23 INSTALLER_IP = os.getenv('INSTALLER_IP')
24
25 # Set up logger
26 logger = ft_logger.Logger("security_scan").getLogger()
27
28
29 class novaManager:
30     def __init__(self, *args):
31         self.args = args
32
33     def keystonepass(self):
34         com = self.args[0]
35         client = paramiko.SSHClient()
36         client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
37         try:
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)
45         except socket.error:
46             logger.error("Socker Connection failed for "
47                          "undercloud host: {0}").format(INSTALLER_IP)
48         stdin, stdout, stderr = client.exec_command(com)
49         return stdout.read()
50
51
52 class connectionManager:
53     def __init__(self, host, port, user, user_key, *args):
54         self.host = host
55         self.port = port
56         self.user = user
57         self.user_key = user_key
58         self.args = args
59
60     def remotescript(self):
61         localpath = self.args[0]
62         remotepath = self.args[1]
63         com = self.args[2]
64
65         client = paramiko.SSHClient()
66         client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
67         # Connection to undercloud
68         try:
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)
76         except socket.error:
77             logger.error("Socker Connection failed for "
78                          "undercloud host: {0}").format(self.host)
79
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)),
84                                          (local_addr))
85         remote_client = paramiko.SSHClient()
86         remote_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
87         # Tunnel to overcloud
88         try:
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)
99         except socket.error:
100             logger.error("Socker Connection failed for "
101                          "undercloud host: {0}").format(self.host)
102
103         output = ""
104         stdin, stdout, stderr = remote_client.exec_command(com)
105         stdout = stdout.readlines()
106         # remove script
107         sftp.remove(remotepath)
108         remote_client.close()
109         client.close()
110         # Pipe back stout
111         for line in stdout:
112             output = output + line
113         if output != "":
114             return output
115
116     def remotecmd(self):
117         com = self.args[0]
118
119         client = paramiko.SSHClient()
120         client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
121         # Connection to undercloud
122         try:
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)
130         except socket.error:
131             logger.error("Socker Connection failed for "
132                          "undercloud host: {0}").format(self.host)
133
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)),
138                                          (local_addr))
139         remote_client = paramiko.SSHClient()
140         remote_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
141         # Tunnel to overcloud
142         try:
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)
151         except socket.error:
152             logger.error("Socker Connection failed for "
153                          "undercloud host: {0}").format(self.host)
154
155         chan = remote_client.get_transport().open_session()
156         chan.get_pty()
157         f = chan.makefile()
158         chan.exec_command(com)
159         print f.read()
160
161         remote_client.close()
162         client.close()
163
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
172         try:
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)
180         except socket.error:
181             logger.error("Socker Connection failed for "
182                          "undercloud host: {0}").format(self.host)
183
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)),
188                                          (local_addr))
189         remote_client = paramiko.SSHClient()
190         remote_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
191         # Tunnel to overcloud
192         try:
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)
201         except socket.error:
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)))
210         sftp.close()
211         transport.close()