OpenSecurity/bin/opensecurityd.pyw
author Oliver Maurhart <oliver.maurhart@ait.ac.at>
Mon, 03 Mar 2014 15:05:17 +0100
changeset 86 a169498c5314
parent 79 617009c32da0
child 87 d5b04809faca
permissions -rwxr-xr-x
working on a better installment
     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 = None
    77 
    78 
    79 # ------------------------------------------------------------
    80 # code
    81 
    82 
    83 #class os_device_change:
    84 #    """OpenSecurity '/device_change' handler"""
    85 #    
    86 #    def GET(self):
    87 #        log_call(web.ctx.environ)
    88 #        try:
    89 #            new_ip = gvm_mgr.handleDeviceChange()
    90 #            return new_ip
    91 #        except:
    92 #            raise web.internalerror()
    93 
    94         
    95 class os_browsing:
    96     """OpenSecurity '/browsing' handler
    97     
    98     - GET: Start and prepare a new SecurityVM for Internet Browsing. Return the name of the VM.
    99     """
   100     
   101     def GET(self):
   102         log_call(web.ctx.environ)
   103         try:
   104             browsingVM = vmmanager().handleBrowsingRequest()
   105             return browsingVM
   106         except:
   107             raise web.internalerror()
   108 
   109 #class os_sdvm_started:
   110 #    """OpenSecurity '/sdvm_started' handler"""
   111 #    
   112 #    def GET(self):
   113 #        log_call(web.ctx.environ)
   114 #        remote_ip = web.ctx.environ['REMOTE_ADDR']
   115 #        gvm_mgr.putStartNotification(remote_ip)
   116 #        return "os_sdvm_started"
   117         
   118 class os_sdvm:
   119     """OpenSecurity '/sdvms/[VM]' handler
   120     
   121     - GET: Information about a specific SecurityVM
   122     - DELETE: Remove a specific
   123     """
   124     
   125     def GET(self, name):
   126         log_call(web.ctx.environ)
   127         return vmmanager().getVMInfo(name)
   128 
   129     def DELETE(self, name):
   130         log_call(web.ctx.environ)
   131         return vmmanager().removeVM(name)
   132             
   133 
   134 class os_sdvm_application:
   135     """OpenSecurity '/sdvms/[VM]/application/[CMD]' handler
   136     
   137     - GET: start application with given command in the VM.
   138     """
   139     
   140     def GET(self, name, command):
   141         log_call(web.ctx.environ)
   142         command = '/' + command
   143         result = Cygwin.sshExecuteX11(command, vmmanager().getHostOnlyIP(name), 'osecuser', Cygwin.cygPath(gvm_mgr.getMachineFolder()) + '/' + name + '/dvm_key'  )
   144         self.poweroffVM(name)
   145         return vmmanager().removeVM(name)
   146     
   147 
   148 class os_sdvm_ip:
   149     """OpenSecurity '/sdvms/[VM]/ip' handler
   150     
   151     - GET: give IP of SecurityVM.
   152     """
   153     
   154     def GET(self, name):
   155         log_call(web.ctx.environ)
   156         return vmmanager().getHostOnlyIP(name)
   157             
   158 
   159 class os_sdvm_start:
   160     """OpenSecurity '/sdvms/[VM]/start' handler
   161     
   162     - GET: Start specific SecuirtyVM.
   163     """
   164     
   165     def GET(self, name):
   166         log_call(web.ctx.environ)
   167         return vmmanager().startVM(name)
   168             
   169 
   170 class os_sdvm_stop:
   171     """OpenSecurity '/sdvms/[VM]/stop' handler
   172     
   173     - GET: stop specific Secuirty VM.
   174     """
   175     
   176     def GET(self, name):
   177         log_call(web.ctx.environ)
   178         return vmmanager().stopVM(name)
   179             
   180 
   181 class os_sdvms:
   182     """OpenSecurity '/sdvms' handler
   183     
   184     - GET: list all available secuirty VMs.
   185     - POST: create new security vm.
   186     """
   187     
   188     def GET(self):
   189         """get the list of SDVMs"""
   190         log_call(web.ctx.environ)
   191         return vmmanager().listSDVM() 
   192             
   193     def POST(self):
   194         """create a new SDVM"""
   195         log_call(web.ctx.environ)
   196         
   197         # get a new vm-name
   198         name = vmmanager().generateSDVMName()
   199         try:
   200             vmmanager().createVM(name)
   201         except:
   202             raise web.internalerror()
   203             
   204         return name
   205             
   206 class os_vm:
   207     """OpenSecurity '/vms/[VM]' handler
   208     
   209     - GET: list information of arbitrary VM.
   210     """
   211     
   212     def GET(self, name):
   213         log_call(web.ctx.environ)
   214         return vmmanager().getVMInfo(name)
   215             
   216 
   217 class os_vms:
   218     """OpenSecurity '/vms' handler
   219     
   220     - GET: list all (also non Security) VMs.
   221     """
   222     
   223     def GET(self):
   224         log_call(web.ctx.environ)
   225         return vmmanager().listVM() 
   226             
   227 
   228 class os_root:
   229     """OpenSecurity '/' handler
   230     
   231     - GET: give information about current installation.
   232     """
   233     
   234     def GET(self):
   235         log_call(web.ctx.environ)
   236         res = "'os_server': { "
   237         res += "'version': '" + __version__ + "', "
   238         res += "'machine_folder': '" + vmmanager().getDefaultMachineFolder() + "' "
   239         res += "}"
   240         return res
   241 
   242 class os_update_template:
   243     """OpenSecurity '/update_template' handler
   244     
   245     - GET: update template vm
   246     """
   247     
   248     def GET(self):
   249         #return gvm_mgr.guestExecute('SecurityDVM', 'sudo apt-get -y update')
   250         log_call(web.ctx.environ)
   251         return vmmanager().updateTemplate()
   252 
   253 
   254 def log_call(web_environ):
   255     """log the incoming call to the REST api"""
   256     try:
   257         call = 'REST ' +  web_environ['REQUEST_METHOD'] + ' ' + web_environ['REQUEST_URI'] + ' from ' + web_environ['REMOTE_ADDR'] + ':' + web_environ['REMOTE_PORT']
   258         logger.debug(call)
   259     except:
   260         pass
   261 
   262 
   263 def main():
   264     """main startup for the opensecuirityd"""
   265     server = web.application(opensecurity_urls, globals())
   266     server.run()
   267 
   268 
   269 def vmmanager():
   270 
   271     """helper method to make lazy init of VMManager"""
   272     if gvm_mgr is None:
   273         gvm_mgr = VMManager.getInstance()
   274     return gvm_mgr    
   275 
   276 
   277 # start
   278 if __name__ == "__main__":
   279     main()
   280