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