OpenSecurity/bin/opensecurityd.py
author mb
Tue, 10 Dec 2013 14:50:14 +0100
changeset 34 fe5fb244f008
parent 31 d95fe93d7a83
child 35 ba1ca3e5870b
permissions -rw-r--r--
added /sdvm_started notification edpoint and isSDVMStarted vmmanager method.
changed VMManager to sinleton. retrieve with VMManager.getInstance()
     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 # ------------------------------------------------------------
    78 # code
    79 
    80 
    81 class os_device_change:
    82     """OpenSecurity '/device_change' handler"""
    83     
    84     def GET(self):
    85         gvm_mgr.handleDeviceChange()
    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             gvm_mgr.startVM(browsingVM)
    99             return browsingVM
   100         except:
   101             raise web.internalerror()
   102 
   103 class os_sdvm_started:
   104     """OpenSecurity '/sdvm_started' handler"""
   105     
   106     def GET(self):
   107         remote_ip = web.ctx.environ['REMOTE_ADDR']
   108         gvm_mgr.putStartNotification(remote_ip)
   109         return "os_sdvm_started"
   110         
   111 class os_sdvm:
   112     """OpenSecurity '/sdvms/[VM]' handler
   113     
   114     - GET: Information about a specific SecurityVM
   115     - DELETE: Remove a specific
   116     """
   117     
   118     def GET(self, name):
   119         return gvm_mgr.getVMInfo(name)
   120 
   121     def DELETE(self, name):
   122         return gvm_mgr.removeVM(name)
   123             
   124 
   125 class os_sdvm_application:
   126     """OpenSecurity '/sdvms/[VM]/application/[CMD]' handler
   127     
   128     - GET: start application with given command in the VM.
   129     """
   130     
   131     def GET(self, name, command):
   132         command = '/' + command
   133         print('---> request to launch application in VM -- ' + name + ':' + command + ' <---')
   134         return gvm_mgr.sshGuestX11Execute(name, command)
   135             
   136 
   137 class os_sdvm_ip:
   138     """OpenSecurity '/sdvms/[VM]/ip' handler
   139     
   140     - GET: give IP of SecurityVM.
   141     """
   142     
   143     def GET(self, name):
   144         return gvm_mgr.getHostOnlyIP(name)
   145             
   146 
   147 class os_sdvm_start:
   148     """OpenSecurity '/sdvms/[VM]/start' handler
   149     
   150     - GET: Start specific SecuirtyVM.
   151     """
   152     
   153     def GET(self, name):
   154         return gvm_mgr.startVM(name)
   155             
   156 
   157 class os_sdvm_stop:
   158     """OpenSecurity '/sdvms/[VM]/stop' handler
   159     
   160     - GET: stop specific Secuirty VM.
   161     """
   162     
   163     def GET(self, name):
   164         return gvm_mgr.stopVM(name)
   165             
   166 
   167 class os_sdvms:
   168     """OpenSecurity '/sdvms' handler
   169     
   170     - GET: list all available secuirty VMs.
   171     - POST: create new security vm.
   172     """
   173     
   174     def GET(self):
   175         """get the list of SDVMs"""
   176         return gvm_mgr.listSDVM() 
   177             
   178     def POST(self):
   179         """create a new SDVM"""
   180 
   181         # get a new vm-name
   182         name = gvm_mgr.generateSDVMName()
   183         try:
   184             gvm_mgr.createVM(name)
   185         except:
   186             raise web.internalerror()
   187             
   188         return name
   189             
   190 class os_vm:
   191     """OpenSecurity '/vms/[VM]' handler
   192     
   193     - GET: list information of arbitrary VM.
   194     """
   195     
   196     def GET(self, name):
   197         return gvm_mgr.getVMInfo(name)
   198             
   199 
   200 class os_vms:
   201     """OpenSecurity '/vms' handler
   202     
   203     - GET: list all (also non Security) VMs.
   204     """
   205     
   206     def GET(self):
   207         return gvm_mgr.listVM() 
   208             
   209 
   210 class os_root:
   211     """OpenSecurity '/' handler
   212     
   213     - GET: give information about current installation.
   214     """
   215     
   216     def GET(self):
   217         res = "'os_server': { "
   218         res += "'version': '" + __version__ + "', "
   219         res += "'machine_folder': '" + gvm_mgr.getDefaultMachineFolder() + "' "
   220         res += "}"
   221         return res
   222 
   223 
   224 # start
   225 if __name__ == "__main__":
   226     server = web.application(opensecurity_urls, globals())
   227     server.run()
   228