OpenSecurity/bin/opensecurityd.pyw
author mb
Tue, 18 Feb 2014 10:36:55 +0100
changeset 63 c354ec779b61
child 66 d768c98d1e48
permissions -rw-r--r--
cleanup
     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 from cygwin import Cygwin
    41 
    42 from vmmanager import VMManager
    43 
    44 # local
    45 from environment import Environment
    46 
    47 
    48 # ------------------------------------------------------------
    49 # const
    50 
    51 __version__ = "0.2"
    52 
    53 
    54 """All the URLs we know mapping to class handler"""
    55 opensecurity_urls = (
    56     '/device_change',                   'os_device_change',     # http://localhost:8080/device_change                           GET
    57     '/sdvm_started',                    'os_sdvm_started',      # http://localhost:8080/sdvm_started                            GET
    58     '/browsing',                        'os_browsing',          # http://localhost:8080/browsing                                GET
    59     '/sdvms',                           'os_sdvms',             # http://localhost:8080/sdvms                                   GET, PUT
    60     '/sdvms/(.*)/application/(.*)',     'os_sdvm_application',  # http://localhost:8080/sdvms/[VMNAME]/application/[COMMAND]    GET
    61     '/sdvms/(.*)/ip',                   'os_sdvm_ip',           # http://localhost:8080/sdvms/[VMNAME]/ip                       GET
    62     '/sdvms/(.*)/start',                'os_sdvm_start',        # http://localhost:8080/sdvms/[VMNAME]/start                    GET
    63     '/sdvms/(.*)/stop',                 'os_sdvm_stop',         # http://localhost:8080/sdvms/[VMNAME]/stop                     GET
    64     '/sdvms/(.*)',                      'os_sdvm',              # http://localhost:8080/sdvms/[VMNAME]                          GET, DELETE
    65     '/vms',                             'os_vms',               # http://localhost:8080/vms                                     GET
    66     '/vms/(.*)',                        'os_vm',                # http://localhost:8080/vms/[VMNAME]                            GET
    67     '/',                                'os_root',              # http://localhost:8080/                                        GET
    68     '/update_template',                 'os_update_template'    # http://localhost:8080/update_template                         GET
    69 )
    70 
    71  # ------------------------------------------------------------
    72 # vars
    73 
    74 # Global VMManager instance
    75 gvm_mgr = VMManager.getInstance()
    76 
    77 # ------------------------------------------------------------
    78 # code
    79 
    80 
    81 class os_device_change:
    82     """OpenSecurity '/device_change' handler"""
    83     
    84     def GET(self):
    85         try:
    86             new_ip = gvm_mgr.handleDeviceChange()
    87             return new_ip
    88         except:
    89             raise web.internalerror()
    90 
    91         
    92 class os_browsing:
    93     """OpenSecurity '/browsing' handler
    94     
    95     - GET: Start and prepare a new SecurityVM for Internet Browsing. Return the name of the VM.
    96     """
    97     
    98     def GET(self):
    99         try:
   100             browsingVM = gvm_mgr.handleBrowsingRequest()
   101             return browsingVM
   102         except:
   103             raise web.internalerror()
   104 
   105 class os_sdvm_started:
   106     """OpenSecurity '/sdvm_started' handler"""
   107     
   108     def GET(self):
   109         remote_ip = web.ctx.environ['REMOTE_ADDR']
   110         gvm_mgr.putStartNotification(remote_ip)
   111         return "os_sdvm_started"
   112         
   113 class os_sdvm:
   114     """OpenSecurity '/sdvms/[VM]' handler
   115     
   116     - GET: Information about a specific SecurityVM
   117     - DELETE: Remove a specific
   118     """
   119     
   120     def GET(self, name):
   121         return gvm_mgr.getVMInfo(name)
   122 
   123     def DELETE(self, name):
   124         return gvm_mgr.removeVM(name)
   125             
   126 
   127 class os_sdvm_application:
   128     """OpenSecurity '/sdvms/[VM]/application/[CMD]' handler
   129     
   130     - GET: start application with given command in the VM.
   131     """
   132     
   133     def GET(self, name, command):
   134         command = '/' + command
   135         print('---> request to launch application in VM -- ' + name + ':' + command + ' <---')
   136         result = Cygwin.sshExecuteX11(command, gvm_mgr.getHostOnlyIP(name), 'osecuser', Cygwin.cygPath(gvm_mgr.getMachineFolder()) + '/' + name + '/dvm_key'  )
   137         self.poweroffVM(name)
   138         return gvm_mgr.removeVM(name)
   139     
   140 
   141 class os_sdvm_ip:
   142     """OpenSecurity '/sdvms/[VM]/ip' handler
   143     
   144     - GET: give IP of SecurityVM.
   145     """
   146     
   147     def GET(self, name):
   148         return gvm_mgr.getHostOnlyIP(name)
   149             
   150 
   151 class os_sdvm_start:
   152     """OpenSecurity '/sdvms/[VM]/start' handler
   153     
   154     - GET: Start specific SecuirtyVM.
   155     """
   156     
   157     def GET(self, name):
   158         return gvm_mgr.startVM(name)
   159             
   160 
   161 class os_sdvm_stop:
   162     """OpenSecurity '/sdvms/[VM]/stop' handler
   163     
   164     - GET: stop specific Secuirty VM.
   165     """
   166     
   167     def GET(self, name):
   168         return gvm_mgr.stopVM(name)
   169             
   170 
   171 class os_sdvms:
   172     """OpenSecurity '/sdvms' handler
   173     
   174     - GET: list all available secuirty VMs.
   175     - POST: create new security vm.
   176     """
   177     
   178     def GET(self):
   179         """get the list of SDVMs"""
   180         return gvm_mgr.listSDVM() 
   181             
   182     def POST(self):
   183         """create a new SDVM"""
   184 
   185         # get a new vm-name
   186         name = gvm_mgr.generateSDVMName()
   187         try:
   188             gvm_mgr.createVM(name)
   189         except:
   190             raise web.internalerror()
   191             
   192         return name
   193             
   194 class os_vm:
   195     """OpenSecurity '/vms/[VM]' handler
   196     
   197     - GET: list information of arbitrary VM.
   198     """
   199     
   200     def GET(self, name):
   201         return gvm_mgr.getVMInfo(name)
   202             
   203 
   204 class os_vms:
   205     """OpenSecurity '/vms' handler
   206     
   207     - GET: list all (also non Security) VMs.
   208     """
   209     
   210     def GET(self):
   211         return gvm_mgr.listVM() 
   212             
   213 
   214 class os_root:
   215     """OpenSecurity '/' handler
   216     
   217     - GET: give information about current installation.
   218     """
   219     
   220     def GET(self):
   221         res = "'os_server': { "
   222         res += "'version': '" + __version__ + "', "
   223         res += "'machine_folder': '" + gvm_mgr.getDefaultMachineFolder() + "' "
   224         res += "}"
   225         return res
   226 
   227 class os_update_template:
   228     """OpenSecurity '/update_template' handler
   229     
   230     - GET: update template vm
   231     """
   232     
   233     def GET(self):
   234         #return gvm_mgr.guestExecute('SecurityDVM', 'sudo apt-get -y update')
   235         return gvm_mgr.updateTemplate()
   236 
   237 # start
   238 if __name__ == "__main__":
   239     server = web.application(opensecurity_urls, globals())
   240     server.run()
   241