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