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