OpenSecurity/bin/opensecurityd.py
author om
Tue, 10 Dec 2013 14:04:11 +0100
changeset 31 d95fe93d7a83
parent 30 0d5637405430
child 34 fe5fb244f008
permissions -rw-r--r--
opensecurityd can now invoke applications on vm
     1 #!/bin/env python
     2 # -*- coding: utf-8 -*-
     3 
     4 # ------------------------------------------------------------
     5 # opensecurityd
     6 # 
     7 # the opensecurityd as RESTful server
     8 #
     9 # Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
    10 #
    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
    15 #
    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.
    19 # 
    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.
    24 # 
    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 # ------------------------------------------------------------
    30 
    31 
    32 # ------------------------------------------------------------
    33 # imports
    34 
    35 import os
    36 import os.path
    37 import subprocess
    38 import sys
    39 import web
    40 
    41 from vmmanager import VMManager
    42 
    43 # local
    44 from environment import Environment
    45 
    46 
    47 # ------------------------------------------------------------
    48 # const
    49 
    50 __version__ = "0.2"
    51 
    52 
    53 """All the URLs we know mapping to class handler"""
    54 opensecurity_urls = (
    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
    66 )
    67 
    68 
    69 # ------------------------------------------------------------
    70 # vars
    71 
    72 # Global VMManager instance
    73 gvm_mgr = VMManager()
    74 
    75 
    76 # ------------------------------------------------------------
    77 # code
    78 
    79 
    80 class os_device_change:
    81     """OpenSecurity '/device_change' handler"""
    82     
    83     def GET(self):
    84         gvm_mgr.handleDeviceChange()
    85         return "os_device_change"
    86 
    87         
    88 class os_browsing:
    89     """OpenSecurity '/browsing' handler
    90     
    91     - GET: Start and prepare a new SecurityVM for Internet Browsing. Return the name of the VM.
    92     """
    93     
    94     def GET(self):
    95         try:
    96             browsingVM = gvm_mgr.handleBrowsingRequest()
    97             gvm_mgr.startVM(browsingVM)
    98             return browsingVM
    99         except:
   100             raise web.internalerror()
   101 
   102         
   103 class os_sdvm:
   104     """OpenSecurity '/sdvms/[VM]' handler
   105     
   106     - GET: Information about a specific SecurityVM
   107     - DELETE: Remove a specific
   108     """
   109     
   110     def GET(self, name):
   111         return gvm_mgr.getVMInfo(name)
   112 
   113     def DELETE(self, name):
   114         return gvm_mgr.removeVM(name)
   115             
   116 
   117 class os_sdvm_application:
   118     """OpenSecurity '/sdvms/[VM]/application/[CMD]' handler
   119     
   120     - GET: start application with given command in the VM.
   121     """
   122     
   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)
   127             
   128 
   129 class os_sdvm_ip:
   130     """OpenSecurity '/sdvms/[VM]/ip' handler
   131     
   132     - GET: give IP of SecurityVM.
   133     """
   134     
   135     def GET(self, name):
   136         return gvm_mgr.getHostOnlyIP(name)
   137             
   138 
   139 class os_sdvm_start:
   140     """OpenSecurity '/sdvms/[VM]/start' handler
   141     
   142     - GET: Start specific SecuirtyVM.
   143     """
   144     
   145     def GET(self, name):
   146         return gvm_mgr.startVM(name)
   147             
   148 
   149 class os_sdvm_stop:
   150     """OpenSecurity '/sdvms/[VM]/stop' handler
   151     
   152     - GET: stop specific Secuirty VM.
   153     """
   154     
   155     def GET(self, name):
   156         return gvm_mgr.stopVM(name)
   157             
   158 
   159 class os_sdvms:
   160     """OpenSecurity '/sdvms' handler
   161     
   162     - GET: list all available secuirty VMs.
   163     - POST: create new security vm.
   164     """
   165     
   166     def GET(self):
   167         """get the list of SDVMs"""
   168         return gvm_mgr.listSDVM() 
   169             
   170     def POST(self):
   171         """create a new SDVM"""
   172 
   173         # get a new vm-name
   174         name = gvm_mgr.generateSDVMName()
   175         try:
   176             gvm_mgr.createVM(name)
   177         except:
   178             raise web.internalerror()
   179             
   180         return name
   181             
   182 class os_vm:
   183     """OpenSecurity '/vms/[VM]' handler
   184     
   185     - GET: list information of arbitrary VM.
   186     """
   187     
   188     def GET(self, name):
   189         return gvm_mgr.getVMInfo(name)
   190             
   191 
   192 class os_vms:
   193     """OpenSecurity '/vms' handler
   194     
   195     - GET: list all (also non Security) VMs.
   196     """
   197     
   198     def GET(self):
   199         return gvm_mgr.listVM() 
   200             
   201 
   202 class os_root:
   203     """OpenSecurity '/' handler
   204     
   205     - GET: give information about current installation.
   206     """
   207     
   208     def GET(self):
   209         res = "'os_server': { "
   210         res += "'version': '" + __version__ + "', "
   211         res += "'machine_folder': '" + gvm_mgr.getDefaultMachineFolder() + "' "
   212         res += "}"
   213         return res
   214 
   215 
   216 # start
   217 if __name__ == "__main__":
   218     server = web.application(opensecurity_urls, globals())
   219     server.run()
   220