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 # ------------------------------------------------------------
41 from vmmanager import VMManager
44 from environment import Environment
47 # ------------------------------------------------------------
53 """All the URLs we know mapping to class handler"""
55 '/device_change', 'os_device_change', # http://localhost:8080/device_change GET
56 '/browsing', 'os_browsing', # http://localhost:8080/browsing GET
57 '/sdvms', 'os_sdvms', # http://localhost:8080/sdvms GET, PUT
58 '/sdvms/(.*)/application/(.*)', 'os_sdvm_application', # http://localhost:8080/sdvms/[VMNAME]/application/[COMMAND] GET
59 '/sdvms/(.*)/ip', 'os_sdvm_ip', # http://localhost:8080/sdvms/[VMNAME]/ip GET
60 '/sdvms/(.*)/start', 'os_sdvm_start', # http://localhost:8080/sdvms/[VMNAME]/start GET
61 '/sdvms/(.*)/stop', 'os_sdvm_stop', # http://localhost:8080/sdvms/[VMNAME]/stop GET
62 '/sdvms/(.*)', 'os_sdvm', # http://localhost:8080/sdvms/[VMNAME] GET, DELETE
63 '/vms', 'os_vms', # http://localhost:8080/vms GET
64 '/vms/(.*)', 'os_vm', # http://localhost:8080/vms/[VMNAME] GET
65 '/', 'os_root' # http://localhost:8080/ GET
69 # ------------------------------------------------------------
72 # Global VMManager instance
76 # ------------------------------------------------------------
80 class os_device_change:
81 """OpenSecurity '/device_change' handler"""
84 gvm_mgr.handleDeviceChange()
85 return "os_device_change"
89 """OpenSecurity '/browsing' handler
91 - GET: Start and prepare a new SecurityVM for Internet Browsing. Return the name of the VM.
96 browsingVM = gvm_mgr.handleBrowsingRequest()
97 gvm_mgr.startVM(browsingVM)
100 raise web.internalerror()
104 """OpenSecurity '/sdvms/[VM]' handler
106 - GET: Information about a specific SecurityVM
107 - DELETE: Remove a specific
111 return gvm_mgr.getVMInfo(name)
113 def DELETE(self, name):
114 return gvm_mgr.removeVM(name)
117 class os_sdvm_application:
118 """OpenSecurity '/sdvms/[VM]/application/[CMD]' handler
120 - GET: start application with given command in the VM.
123 def GET(self, name, command):
124 command = '/' + command
125 print('---> request to launch application in VM -- ' + name + ':' + command + ' <---')
126 return gvm_mgr.sshGuestX11Execute(name, command)
130 """OpenSecurity '/sdvms/[VM]/ip' handler
132 - GET: give IP of SecurityVM.
136 return gvm_mgr.getHostOnlyIP(name)
140 """OpenSecurity '/sdvms/[VM]/start' handler
142 - GET: Start specific SecuirtyVM.
146 return gvm_mgr.startVM(name)
150 """OpenSecurity '/sdvms/[VM]/stop' handler
152 - GET: stop specific Secuirty VM.
156 return gvm_mgr.stopVM(name)
160 """OpenSecurity '/sdvms' handler
162 - GET: list all available secuirty VMs.
163 - POST: create new security vm.
167 """get the list of SDVMs"""
168 return gvm_mgr.listSDVM()
171 """create a new SDVM"""
174 name = gvm_mgr.generateSDVMName()
176 gvm_mgr.createVM(name)
178 raise web.internalerror()
183 """OpenSecurity '/vms/[VM]' handler
185 - GET: list information of arbitrary VM.
189 return gvm_mgr.getVMInfo(name)
193 """OpenSecurity '/vms' handler
195 - GET: list all (also non Security) VMs.
199 return gvm_mgr.listVM()
203 """OpenSecurity '/' handler
205 - GET: give information about current installation.
209 res = "'os_server': { "
210 res += "'version': '" + __version__ + "', "
211 res += "'machine_folder': '" + gvm_mgr.getDefaultMachineFolder() + "' "
217 if __name__ == "__main__":
218 server = web.application(opensecurity_urls, globals())