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 # ------------------------------------------------------------
40 from cygwin import Cygwin
45 from environment import Environment
46 from opensecurity_util import logger
49 # ------------------------------------------------------------
55 """All the URLs we know mapping to class handler"""
57 '/browsing', 'os_browsing', # http://localhost:8080/browsing GET
58 '/sdvms', 'os_sdvms', # http://localhost:8080/sdvms GET, PUT
59 '/sdvms/(.*)/application/(.*)', 'os_sdvm_application', # http://localhost:8080/sdvms/[VMNAME]/application/[COMMAND] GET
60 '/sdvms/(.*)/ip', 'os_sdvm_ip', # http://localhost:8080/sdvms/[VMNAME]/ip GET
61 '/sdvms/(.*)/start', 'os_sdvm_start', # http://localhost:8080/sdvms/[VMNAME]/start GET
62 '/sdvms/(.*)/stop', 'os_sdvm_stop', # http://localhost:8080/sdvms/[VMNAME]/stop GET
63 '/sdvms/(.*)', 'os_sdvm', # http://localhost:8080/sdvms/[VMNAME] GET, DELETE
64 '/vms', 'os_vms', # http://localhost:8080/vms GET
65 '/vms/(.*)', 'os_vm', # http://localhost:8080/vms/[VMNAME] GET
66 '/update_template', 'os_update_template', # http://localhost:8080/update_template GET
67 '/terminate', 'os_terminate', # http://localhost:8080/terminate GET
68 '/', 'os_root' # http://localhost:8080/ GET
72 # ------------------------------------------------------------
75 # Global VMManager instance
79 # ------------------------------------------------------------
84 """OpenSecurity '/browsing' handler
86 - GET: Start and prepare a new SecurityVM for Internet Browsing. Return the name of the VM.
90 log_call(web.ctx.environ)
93 browsingVM = gvm_mgr.handleBrowsingRequest()
96 raise web.internalerror()
100 """OpenSecurity '/' handler
102 - GET: give information about current installation.
106 log_call(web.ctx.environ)
108 res = "'os_server': { "
109 res += "'version': '" + __version__ + "', "
110 res += "'machine_folder': '" + gvm_mgr.getDefaultMachineFolder() + "' "
116 """OpenSecurity '/sdvms/[VM]' handler
118 - GET: Information about a specific SecurityVM
119 - DELETE: Remove a specific
123 log_call(web.ctx.environ)
125 return gvm_mgr.getVMInfo(name)
127 def DELETE(self, name):
128 log_call(web.ctx.environ)
130 return gvm_mgr.removeVM(name)
133 class os_sdvm_application:
134 """OpenSecurity '/sdvms/[VM]/application/[CMD]' handler
136 - GET: start application with given command in the VM.
139 def GET(self, name, command):
140 log_call(web.ctx.environ)
142 command = '/' + command
143 result = Cygwin.sshExecuteX11(command, gvm_mgr.getHostOnlyIP(name), 'osecuser', Cygwin.cygPath(gvm_mgr.getMachineFolder()) + '/' + name + '/dvm_key' )
144 self.poweroffVM(name)
145 return gvm_mgr.removeVM(name)
149 """OpenSecurity '/sdvms/[VM]/ip' handler
151 - GET: give IP of SecurityVM.
155 log_call(web.ctx.environ)
157 return gvm_mgr.getHostOnlyIP(name)
161 """OpenSecurity '/sdvms/[VM]/start' handler
163 - GET: Start specific SecuirtyVM.
167 log_call(web.ctx.environ)
169 return gvm_mgr.startVM(name)
173 """OpenSecurity '/sdvms/[VM]/stop' handler
175 - GET: stop specific Secuirty VM.
179 log_call(web.ctx.environ)
181 return gvm_mgr.stopVM(name)
185 """OpenSecurity '/sdvms' handler
187 - GET: list all available secuirty VMs.
188 - POST: create new security vm.
192 """get the list of SDVMs"""
193 log_call(web.ctx.environ)
195 return gvm_mgr.listSDVM()
198 """create a new SDVM"""
199 log_call(web.ctx.environ)
203 name = gvm_mgr.generateSDVMName()
205 gvm_mgr.createVM(name)
207 raise web.internalerror()
213 """OpenSecurity '/terminate' handler
215 - GET: terminate the opensecurityd.
217 YES: this here is bonkers. But the web.py http
218 server runs infinite until a SystemExit exception
219 or KeyboardInterrupt exception is raised.
221 see: site-packages/web/httpserver.py - line 157ff
222 see: site-packages/web/wsgiserver/__init__.py - line 1682ff
224 So, we invoke a sys.exit(0) here to trigger server.stop().
226 TODO: need to find a better way doing this, and not via the
227 REST api. Maybe hack web.py server code?
231 log_call(web.ctx.environ)
237 class os_update_template:
238 """OpenSecurity '/update_template' handler
240 - GET: update template vm
244 #return gvm_mgr.guestExecute('SecurityDVM', 'sudo apt-get -y update')
246 log_call(web.ctx.environ)
247 return gvm_mgr.updateTemplate()
251 """OpenSecurity '/vms/[VM]' handler
253 - GET: list information of arbitrary VM.
257 log_call(web.ctx.environ)
259 return gvm_mgr.getVMInfo(name)
263 """OpenSecurity '/vms' handler
265 - GET: list all (also non Security) VMs.
269 log_call(web.ctx.environ)
271 return gvm_mgr.listVM()
274 def log_call(web_environ):
275 """log the incoming call to the REST api"""
277 call = 'REST ' + web_environ['REQUEST_METHOD'] + ' ' + web_environ['REQUEST_URI'] + ' from ' + web_environ['REMOTE_ADDR'] + ':' + web_environ['REMOTE_PORT']
284 """main startup for the opensecuirityd"""
286 logger.debug('Starting OpenSecurity REST server')
288 # ensure a VMManger is yet loaded
290 gvm_mgr = vmmanager.VMManager.getInstance()
292 server = web.application(opensecurity_urls, globals(), autoreload = False)
295 logger.debug('Stopped OpenSecurity REST server')
299 if __name__ == "__main__":