fixed install ... finally!
2 # -*- coding: utf-8 -*-
4 # ------------------------------------------------------------
7 # the opensecurityd as RESTful server
9 # Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
11 # Copyright (C) 2013 AIT Austrian Institute of Technology
12 # AIT Austrian Institute of Technology GmbH
13 # Donau-City-Strasse 1 | 1220 Vienna | Austria
14 # http://www.ait.ac.at
16 # This program is free software; you can redistribute it and/or
17 # modify it under the terms of the GNU General Public License
18 # as published by the Free Software Foundation version 2.
20 # This program is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 # GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 51 Franklin Street, Fifth Floor,
28 # Boston, MA 02110-1301, USA.
29 # ------------------------------------------------------------
32 # ------------------------------------------------------------
45 from cygwin import Cygwin
46 from environment import Environment
47 from opensecurity_util import logger
50 # ------------------------------------------------------------
56 """All the URLs we know mapping to class handler"""
58 '/browsing', 'os_browsing', # http://localhost:8080/browsing GET
59 '/fetch_initial_image', 'os_fetch_image', # http://localhost:8080/fetch_initial_image GET
60 '/init', 'os_init', # http://localhost:8080/init GET
61 '/sdvms', 'os_sdvms', # http://localhost:8080/sdvms GET, PUT
62 '/sdvms/(.*)/application/(.*)', 'os_sdvm_application', # http://localhost:8080/sdvms/[VMNAME]/application/[COMMAND] GET
63 '/sdvms/(.*)/ip', 'os_sdvm_ip', # http://localhost:8080/sdvms/[VMNAME]/ip GET
64 '/sdvms/(.*)/start', 'os_sdvm_start', # http://localhost:8080/sdvms/[VMNAME]/start GET
65 '/sdvms/(.*)/stop', 'os_sdvm_stop', # http://localhost:8080/sdvms/[VMNAME]/stop GET
66 '/sdvms/(.*)', 'os_sdvm', # http://localhost:8080/sdvms/[VMNAME] GET, DELETE
67 '/setup', 'os_setup', # http://localhost:8080/setup GET
68 '/vms', 'os_vms', # http://localhost:8080/vms GET
69 '/vms/(.*)', 'os_vm', # http://localhost:8080/vms/[VMNAME] GET
70 '/update_template', 'os_update_template', # http://localhost:8080/update_template GET
71 '/terminate', 'os_terminate', # http://localhost:8080/terminate GET
72 '/', 'os_root' # http://localhost:8080/ GET
76 # ------------------------------------------------------------
79 # Global VMManager instance
83 # ------------------------------------------------------------
88 """OpenSecurity '/browsing' handler
90 - GET: Start and prepare a new SecurityVM for Internet Browsing. Return the name of the VM.
94 log_call(web.ctx.environ)
97 result = gvm_mgr.handleBrowsingRequest()
100 raise web.internalerror()
103 class os_fetch_image:
104 """OpenSecurity '/fetch_initial_image' handler
106 - GET: fetch the initial image from the X-Net Servers
107 The initial image is stored in the
108 Virtual Box default machine path.
109 The result to this call is a temprary file
110 which shows the progress (or error state)
116 log_call(web.ctx.environ)
119 trace_file_name = os.path.join(tempfile.gettempdir(), 'OpenSecurity_fetch_image.log')
120 trace_file = open(trace_file_name, 'w+')
122 machine_folder = Cygwin.cygPath(gvm_mgr.getMachineFolder())
123 download_initial_image_script = '/OpenSecurity/bin/download_initial_image.sh \'' + machine_folder + '\''
124 Cygwin.bashExecute(download_initial_image_script, wait_return = False, stdout = trace_file, stderr = trace_file)
130 Fetch OpenSecurity image log ({0}):<br/>
131 <iframe src="file://{0}" width="100%" height="100%"></iframe>
135 return page.format(trace_file_name)
139 """OpenSecurity '/init' handler
141 - GET: Do initial import of OsecVM.ova
145 log_call(web.ctx.environ)
148 trace_file_name = os.path.join(tempfile.gettempdir(), 'OpenSecurity_initial_import.log')
149 trace_file = open(trace_file_name, 'w+')
151 vm_image = Cygwin.cygPath(gvm_mgr.getMachineFolder()) + '/OsecVM.ova'
152 initial_import_script = '/OpenSecurity/bin/initial_vm.sh \'' + vm_image + '\''
153 Cygwin.bashExecute(initial_import_script, wait_return = False, stdout = trace_file, stderr = trace_file)
159 Import initial OpenSecurity image log ({0}):<br/>
160 <iframe src="file://{0}" width="100%" height="100%"></iframe>
164 return page.format(trace_file_name)
168 """OpenSecurity '/' handler
170 - GET: give information about current installation.
174 log_call(web.ctx.environ)
176 res = "'os_server': { "
177 res += "'version': '" + __version__ + "', "
178 res += "'virtual box systemproperties': '" + str(gvm_mgr.systemProperties) + "', "
179 res += "'current temporary folder': '" + tempfile.gettempdir() + "' "
185 """OpenSecurity '/sdvms/[VM]' handler
187 - GET: Information about a specific SecurityVM
188 - DELETE: Remove a specific
192 log_call(web.ctx.environ)
194 return gvm_mgr.getVMInfo(name)
196 def DELETE(self, name):
197 log_call(web.ctx.environ)
199 return gvm_mgr.removeVM(name)
202 class os_sdvm_application:
203 """OpenSecurity '/sdvms/[VM]/application/[CMD]' handler
205 - GET: start application with given command in the VM.
208 def GET(self, name, command):
209 log_call(web.ctx.environ)
211 command = '/' + command
212 result = Cygwin.sshExecuteX11(command, gvm_mgr.getHostOnlyIP(name), 'osecuser', Cygwin.cygPath(gvm_mgr.getMachineFolder()) + '/' + name + '/dvm_key' )
213 self.poweroffVM(name)
214 return gvm_mgr.removeVM(name)
218 """OpenSecurity '/sdvms/[VM]/ip' handler
220 - GET: give IP of SecurityVM.
224 log_call(web.ctx.environ)
226 return gvm_mgr.getHostOnlyIP(name)
230 """OpenSecurity '/sdvms/[VM]/start' handler
232 - GET: Start specific SecuirtyVM.
236 log_call(web.ctx.environ)
238 return gvm_mgr.startVM(name)
242 """OpenSecurity '/sdvms/[VM]/stop' handler
244 - GET: stop specific Secuirty VM.
248 log_call(web.ctx.environ)
250 return gvm_mgr.stopVM(name)
254 """OpenSecurity '/sdvms' handler
256 - GET: list all available secuirty VMs.
257 - POST: create new security vm.
261 """get the list of SDVMs"""
262 log_call(web.ctx.environ)
264 return gvm_mgr.listSDVM()
267 """create a new SDVM"""
268 log_call(web.ctx.environ)
272 name = gvm_mgr.generateSDVMName()
274 gvm_mgr.createVM(name)
276 raise web.internalerror()
282 """OpenSecurity '/setup' handler
284 - GET: Give user some info how to setup the OpenSecurity envvironment
289 log_call(web.ctx.environ)
294 <h1>Setup OpenSecurity</h1>
295 In order to setup OpenSecurity an inital VM image has to be downloaded and imported:<br/>
297 <li>Download initial VM image: <a href="/fetch_initial_image">fetch_initial_image</a>
298 <li>Import initial VM: <a href="/init">init</a>
307 """OpenSecurity '/terminate' handler
309 - GET: terminate the opensecurityd.
311 YES: this here is bonkers. But the web.py http
312 server runs infinite until a SystemExit exception
313 or KeyboardInterrupt exception is raised.
315 see: site-packages/web/httpserver.py - line 157ff
316 see: site-packages/web/wsgiserver/__init__.py - line 1682ff
318 So, we invoke a sys.exit(0) here to trigger server.stop().
320 TODO: need to find a better way doing this, and not via the
321 REST api. Maybe hack web.py server code?
325 log_call(web.ctx.environ)
333 class os_update_template:
334 """OpenSecurity '/update_template' handler
336 - GET: update template vm
340 #return gvm_mgr.guestExecute('SecurityDVM', 'sudo apt-get -y update')
342 log_call(web.ctx.environ)
343 return gvm_mgr.updateTemplate()
347 """OpenSecurity '/vms/[VM]' handler
349 - GET: list information of arbitrary VM.
353 log_call(web.ctx.environ)
355 return gvm_mgr.getVMInfo(name)
359 """OpenSecurity '/vms' handler
361 - GET: list all (also non Security) VMs.
365 log_call(web.ctx.environ)
367 return gvm_mgr.listVM()
370 def log_call(web_environ):
371 """log the incoming call to the REST api"""
373 call = 'REST ' + web_environ['REQUEST_METHOD'] + ' ' + web_environ['REQUEST_URI'] + ' from ' + web_environ['REMOTE_ADDR'] + ':' + web_environ['REMOTE_PORT']
380 """main startup for the opensecuirityd"""
382 logger.debug('Starting OpenSecurity REST server')
384 # ensure a VMManger is yet loaded
386 gvm_mgr = vmmanager.VMManager.getInstance()
388 # tweak sys.argv to control wep.py server start behavior
389 sys.argv = [__file__, "8080"]
390 server = web.application(opensecurity_urls, globals(), autoreload = False)
393 logger.debug('Stopped OpenSecurity REST server')
397 """stop the opensecuirityd"""
399 # calling sys.exit() raises a SystemExit exception
400 # of the WSGI Server to let it wind down
407 if __name__ == "__main__":