OpenSecurity/bin/opensecurityd.py
author mb
Wed, 11 Dec 2013 14:34:19 +0100
changeset 41 76d9177ca509
parent 40 b44a603b0b95
child 43 7c2e34bcdf3d
permissions -rw-r--r--
changed handleBrowsingRequest and startVM
     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     '/sdvm_started',                    'os_sdvm_started',      # http://localhost:8080/sdvm_started                            GET
    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     '/',                                'os_root'               # http://localhost:8080/                                        GET
    67 )
    68 
    69 
    70 # ------------------------------------------------------------
    71 # vars
    72 
    73 # Global VMManager instance
    74 gvm_mgr = VMManager.getInstance()
    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         
    86         return "os_device_change"
    87 
    88         
    89 class os_browsing:
    90     """OpenSecurity '/browsing' handler
    91     
    92     - GET: Start and prepare a new SecurityVM for Internet Browsing. Return the name of the VM.
    93     """
    94     
    95     def GET(self):
    96         try:
    97             browsingVM = gvm_mgr.handleBrowsingRequest()
    98             return browsingVM
    99         except:
   100             raise web.internalerror()
   101 
   102 class os_sdvm_started:
   103     """OpenSecurity '/sdvm_started' handler"""
   104     
   105     def GET(self):
   106         remote_ip = web.ctx.environ['REMOTE_ADDR']
   107         gvm_mgr.putStartNotification(remote_ip)
   108         return "os_sdvm_started"
   109         
   110 class os_sdvm:
   111     """OpenSecurity '/sdvms/[VM]' handler
   112     
   113     - GET: Information about a specific SecurityVM
   114     - DELETE: Remove a specific
   115     """
   116     
   117     def GET(self, name):
   118         return gvm_mgr.getVMInfo(name)
   119 
   120     def DELETE(self, name):
   121         return gvm_mgr.removeVM(name)
   122             
   123 
   124 class os_sdvm_application:
   125     """OpenSecurity '/sdvms/[VM]/application/[CMD]' handler
   126     
   127     - GET: start application with given command in the VM.
   128     """
   129     
   130     def GET(self, name, command):
   131         command = '/' + command
   132         print('---> request to launch application in VM -- ' + name + ':' + command + ' <---')
   133         return gvm_mgr.sshGuestX11Execute(name, command)
   134             
   135 
   136 class os_sdvm_ip:
   137     """OpenSecurity '/sdvms/[VM]/ip' handler
   138     
   139     - GET: give IP of SecurityVM.
   140     """
   141     
   142     def GET(self, name):
   143         return gvm_mgr.getHostOnlyIP(name)
   144             
   145 
   146 class os_sdvm_start:
   147     """OpenSecurity '/sdvms/[VM]/start' handler
   148     
   149     - GET: Start specific SecuirtyVM.
   150     """
   151     
   152     def GET(self, name):
   153         return gvm_mgr.startVM(name)
   154             
   155 
   156 class os_sdvm_stop:
   157     """OpenSecurity '/sdvms/[VM]/stop' handler
   158     
   159     - GET: stop specific Secuirty VM.
   160     """
   161     
   162     def GET(self, name):
   163         return gvm_mgr.stopVM(name)
   164             
   165 
   166 class os_sdvms:
   167     """OpenSecurity '/sdvms' handler
   168     
   169     - GET: list all available secuirty VMs.
   170     - POST: create new security vm.
   171     """
   172     
   173     def GET(self):
   174         """get the list of SDVMs"""
   175         return gvm_mgr.listSDVM() 
   176             
   177     def POST(self):
   178         """create a new SDVM"""
   179 
   180         # get a new vm-name
   181         name = gvm_mgr.generateSDVMName()
   182         try:
   183             gvm_mgr.createVM(name)
   184         except:
   185             raise web.internalerror()
   186             
   187         return name
   188             
   189 class os_vm:
   190     """OpenSecurity '/vms/[VM]' handler
   191     
   192     - GET: list information of arbitrary VM.
   193     """
   194     
   195     def GET(self, name):
   196         return gvm_mgr.getVMInfo(name)
   197             
   198 
   199 class os_vms:
   200     """OpenSecurity '/vms' handler
   201     
   202     - GET: list all (also non Security) VMs.
   203     """
   204     
   205     def GET(self):
   206         return gvm_mgr.listVM() 
   207             
   208 
   209 class os_root:
   210     """OpenSecurity '/' handler
   211     
   212     - GET: give information about current installation.
   213     """
   214     
   215     def GET(self):
   216         res = "'os_server': { "
   217         res += "'version': '" + __version__ + "', "
   218         res += "'machine_folder': '" + gvm_mgr.getDefaultMachineFolder() + "' "
   219         res += "}"
   220         return res
   221 
   222 
   223 # start
   224 if __name__ == "__main__":
   225     server = web.application(opensecurity_urls, globals())
   226     server.run()
   227