OpenSecurity/bin/opensecurityd.pyw
author BarthaM@N3SIM1218.D03.arc.local
Fri, 05 Sep 2014 12:28:30 +0100
changeset 221 853af9cfab6a
parent 220 f5805ee62d80
child 223 a4fb6694e6fe
permissions -rwxr-xr-x
Integrated import script (rewritten in python) into opensecurity/vmmanager.py
Improoved user feedback upon import and update as well as logging.
Reduced system shutdown times and ui response times
Improoved the decoupling between UI and OSec subsystem.
Various other fixes
     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 #        Mihai Bartha, <mihai.bartha@ait.ac.at>       
    11 #
    12 # Copyright (C) 2013 AIT Austrian Institute of Technology
    13 # AIT Austrian Institute of Technology GmbH
    14 # Donau-City-Strasse 1 | 1220 Vienna | Austria
    15 # http://www.ait.ac.at
    16 #
    17 # This program is free software; you can redistribute it and/or
    18 # modify it under the terms of the GNU General Public License
    19 # as published by the Free Software Foundation version 2.
    20 # 
    21 # This program is distributed in the hope that it will be useful,
    22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
    23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    24 # GNU General Public License for more details.
    25 # 
    26 # You should have received a copy of the GNU General Public License
    27 # along with this program; if not, write to the Free Software
    28 # Foundation, Inc., 51 Franklin Street, Fifth Floor, 
    29 # Boston, MA  02110-1301, USA.
    30 # ------------------------------------------------------------
    31 
    32 
    33 # ------------------------------------------------------------
    34 # imports
    35 
    36 import json
    37 import os
    38 import os.path
    39 import subprocess
    40 import sys
    41 import tempfile
    42 import web
    43 
    44 import vmmanager
    45 
    46 # local
    47 import __init__ as opensecurity
    48 from cygwin import Cygwin
    49 from environment import Environment
    50 from opensecurity_util import logger, showTrayMessage
    51 
    52 
    53 
    54 # ------------------------------------------------------------
    55 # const
    56 
    57 """All the URLs we know mapping to class handler"""
    58 opensecurity_urls = (
    59     '/browsing',                        'os_browsing',          # http://localhost:8080/browsing                                GET
    60     '/fetch_initial_image',             'os_fetch_image',       # http://localhost:8080/fetch_initial_image                     GET
    61     '/init',                            'os_init',              # http://localhost:8080/init                                    GET
    62     '/initial_image',                   'os_initial_image',     # http://localhost:8080/initial_image                           GET
    63     '/sdvms',                           'os_sdvms',             # http://localhost:8080/sdvms                                   GET, PUT
    64     '/sdvms/(.*)/application/(.*)',     'os_sdvm_application',  # http://localhost:8080/sdvms/[VMNAME]/application/[COMMAND]    GET
    65     '/sdvms/(.*)/ip',                   'os_sdvm_ip',           # http://localhost:8080/sdvms/[VMNAME]/ip                       GET
    66     '/sdvms/(.*)/start',                'os_sdvm_start',        # http://localhost:8080/sdvms/[VMNAME]/start                    GET
    67     '/sdvms/(.*)/stop',                 'os_sdvm_stop',         # http://localhost:8080/sdvms/[VMNAME]/stop                     GET
    68     '/sdvms/(.*)',                      'os_sdvm',              # http://localhost:8080/sdvms/[VMNAME]                          GET, DELETE
    69     '/setup',                           'os_setup',             # http://localhost:8080/setup                                   GET
    70     '/vms',                             'os_vms',               # http://localhost:8080/vms                                     GET
    71     '/vms/(.*)',                        'os_vm',                # http://localhost:8080/vms/[VMNAME]                            GET
    72     '/update_template',                 'os_update_template',   # http://localhost:8080/update_template                         GET
    73     '/terminate',                       'os_terminate',         # http://localhost:8080/terminate                               GET
    74     '/initialize',                      'os_initialize',        # http://localhost:8080/initialize                               GET
    75     '/',                                'os_root'               # http://localhost:8080/                                        GET
    76 )
    77 
    78 
    79 # ------------------------------------------------------------
    80 # vars
    81 
    82 # Global VMManager instance
    83 gvm_mgr = None
    84 
    85 # server instance
    86 server = None
    87 
    88 
    89 # ------------------------------------------------------------
    90 # code
    91 
    92 
    93 class os_browsing:
    94     """OpenSecurity '/browsing' handler
    95     
    96     - GET: Start and prepare a new SecurityVM for Internet Browsing. Return the name of the VM.
    97     """
    98     
    99     def GET(self):
   100         args = web.input()
   101         log_call(web.ctx.environ)
   102         global gvm_mgr
   103         try:
   104             proxy = None
   105             if 'ProxyServer' in args:
   106                 proxy = args['ProxyServer']
   107             result = gvm_mgr.handleBrowsingRequest(proxy)
   108             return result
   109         except:
   110             raise web.internalerror()
   111 
   112        
   113 class os_fetch_image:
   114     """OpenSecurity '/fetch_initial_image' handler
   115     
   116     - GET: fetch the initial image from the X-Net Servers
   117             The initial image is stored in the
   118             Virtual Box default machine path.
   119             The result to this call is a temprary file
   120             which shows the progress (or error state)
   121             of this call.
   122     """
   123     
   124     def GET(self):
   125         
   126         log_call(web.ctx.environ)
   127         global gvm_mgr
   128 
   129         trace_file_name = os.path.join(Environment('OpenSecurity').log_path, 'OpenSecurity_fetch_image.log')
   130         trace_file = open(trace_file_name, 'w+')
   131 
   132         machine_folder = Cygwin.cygPath(gvm_mgr.getMachineFolder()) 
   133         download_initial_image_script = Cygwin.cygPath(os.path.abspath(os.path.join(os.path.split(__file__)[0], 'download_initial_image.sh')))
   134         Cygwin.bashExecute('\\"' + download_initial_image_script + '\\" \'' + machine_folder + '\'', wait_return = False, stdout = trace_file, stderr = trace_file) 
   135 
   136         res = '{ "fetch_log": "' + trace_file_name.replace('\\', '\\\\') + '" }'
   137         return res
   138 
   139 
   140 class os_init:
   141     """OpenSecurity '/init' handler
   142     
   143     - GET: Do initial import of OsecVM.ova
   144     """
   145     
   146     def GET(self):
   147         global gvm_mgr
   148         log_call(web.ctx.environ)
   149 
   150         gvm_mgr.startInitialImport()
   151         res = '{ "init_log": "' + (Environment('OpenSecurity').log_path+'\\OpenSecurity_initial_import.log').replace('\\', '\\\\') + '" }'
   152         return res
   153 
   154 class os_update_template:
   155     """OpenSecurity '/update_template' handler
   156     
   157     - GET: update template vm
   158     """
   159     
   160     def GET(self):
   161         global gvm_mgr
   162         log_call(web.ctx.environ)
   163 
   164         gvm_mgr.startUpdateTemplate()
   165         res = '{ "init_log": "' + (Environment('OpenSecurity').log_path+'\\OpenSecurity_initial_import.log').replace('\\', '\\\\') + '" }'
   166         return res
   167     
   168 class os_initial_image:
   169     """OpenSecurity '/initial_image' handler
   170     
   171     - GET: Return what we have as initial image.
   172     """
   173     
   174     def GET(self):
   175         log_call(web.ctx.environ)
   176         global gvm_mgr
   177         t = os.path.join(gvm_mgr.systemProperties['Default machine folder'], 'OsecVM.ova')
   178         res = ''
   179         if os.path.isfile(t):
   180             res = '{"initial_template": { '
   181             res += '"name": "OsecVM.ova", '
   182             res += '"path": "' + t.replace('\\', '\\\\') + '", '
   183             res += '"size": ' + str(os.path.getsize(t)) + ', ' 
   184             res += '"date": ' + str(os.path.getmtime(t)) + ''
   185             res += '}}'
   186         return res
   187     
   188 class os_root:
   189     """OpenSecurity '/' handler
   190     
   191     - GET: give information about current installation.
   192     """
   193     
   194     def GET(self):
   195         log_call(web.ctx.environ)
   196         global gvm_mgr
   197 
   198         # create a json string and pretty print it
   199         res = '{"os_server": { '
   200         res += '"version": "' + opensecurity.__version__ + '" '
   201         res += ', "virtual box systemproperties": ' + str(gvm_mgr.systemProperties).replace("'", '"') 
   202         res += ', "current temporary folder": "' + tempfile.gettempdir().replace('\\', '\\\\') + '"'
   203         res += ', "current log folder": "' + Environment('OpenSecurity').log_path.replace('\\', '\\\\') + '"'
   204 
   205         try:
   206             res += ', "whoami": "' + Cygwin.bashExecute('whoami')[1].strip() + '"'
   207         except:
   208             res += ', "whoami": "FAILED"'
   209 
   210         try:
   211             res += ', "mount": ' + str(Cygwin.bashExecute('mount')[1].split('\n')[:-1]).replace("'", '"')
   212         except:
   213             res += ', "mount": "FAILED"'
   214 
   215         try:
   216             res += ', "cygpath --windows ~": "' + Cygwin.bashExecute('cygpath --windows ~')[1].strip().replace('\\', '\\\\') + '"'
   217         except:
   218             res += ', "cygpath --windows ~": "FAILED"'
   219 
   220 
   221         res += ', "status message": "' + gvm_mgr.status_message.replace('"', "'") + '"'
   222 
   223         res += '}}'
   224 
   225         # loading it into json and print it again ensures
   226         # we really do have a valid RFC conform json string
   227         # created (as long as the python json module is RFC conform)
   228         return json.dumps(json.loads(res), indent = 4)
   229 
   230 
   231 
   232 class os_sdvm:
   233     """OpenSecurity '/sdvms/[VM]' handler
   234     
   235     - GET: Information about a specific SecurityVM
   236     - DELETE: Remove a specific
   237     """
   238     
   239     def GET(self, name):
   240         log_call(web.ctx.environ)
   241         global gvm_mgr
   242         return json.dumps(gvm_mgr.getVMInfo(name), indent = 4)
   243 
   244     def DELETE(self, name):
   245         log_call(web.ctx.environ)
   246         global gvm_mgr
   247         return gvm_mgr.removeVM(name)
   248             
   249 
   250 class os_sdvm_application:
   251     """OpenSecurity '/sdvms/[VM]/application/[CMD]' handler
   252     
   253     - GET: start application with given command in the VM.
   254     """
   255     
   256     def GET(self, name, command):
   257         log_call(web.ctx.environ)
   258         global gvm_mgr
   259         command = '/' + command
   260         showTrayMessage('Launching application in isolated VM...', 7000)
   261         result = Cygwin.sshExecuteX11(command, gvm_mgr.getHostOnlyIP(name), 'osecuser', Cygwin.cygPath(gvm_mgr.getMachineFolder()) + '/' + name + '/dvm_key'  )
   262         return 'Command ' + str(command) + ' started on VM "' + name + '" with IP ' + gvm_mgr.getHostOnlyIP(name)
   263     
   264 
   265 class os_sdvm_ip:
   266     """OpenSecurity '/sdvms/[VM]/ip' handler
   267     
   268     - GET: give IP of SecurityVM.
   269     """
   270     
   271     def GET(self, name):
   272         log_call(web.ctx.environ)
   273         global gvm_mgr
   274         return gvm_mgr.getHostOnlyIP(name)
   275             
   276 
   277 class os_sdvm_start:
   278     """OpenSecurity '/sdvms/[VM]/start' handler
   279     
   280     - GET: Start specific SecuirtyVM.
   281     """
   282     
   283     def GET(self, name):
   284         log_call(web.ctx.environ)
   285         global gvm_mgr
   286         return gvm_mgr.startVM(name)
   287             
   288 
   289 class os_sdvm_stop:
   290     """OpenSecurity '/sdvms/[VM]/stop' handler
   291     
   292     - GET: stop specific Secuirty VM.
   293     """
   294     
   295     def GET(self, name):
   296         log_call(web.ctx.environ)
   297         global gvm_mgr
   298         return gvm_mgr.stopVM(name)
   299             
   300 
   301 class os_sdvms:
   302     """OpenSecurity '/sdvms' handler
   303     
   304     - GET: list all available secuirty VMs.
   305     - POST: create new security vm.
   306     """
   307     
   308     def GET(self):
   309         """get the list of SDVMs"""
   310         log_call(web.ctx.environ)
   311         global gvm_mgr
   312 
   313         d = {}
   314         for sdvm in gvm_mgr.listSDVM():
   315             d[sdvm] = gvm_mgr.getHostOnlyIP(sdvm)
   316 
   317         return json.dumps(d, indent = 4)
   318             
   319     def POST(self):
   320         """create a new SDVM"""
   321         log_call(web.ctx.environ)
   322         global gvm_mgr
   323         
   324         # get a new vm-name
   325         name = gvm_mgr.generateSDVMName()
   326         try:
   327             gvm_mgr.createVM(name)
   328         except:
   329             raise web.internalerror()
   330             
   331         return name
   332             
   333 
   334 class os_setup:
   335     """OpenSecurity '/setup' handler
   336     
   337     - GET: Give user some info how to setup the OpenSecurity environment
   338     """
   339     
   340     def GET(self):
   341 
   342         log_call(web.ctx.environ)
   343 
   344         page = """
   345         <html>
   346         <body>
   347         <h1>Setup OpenSecurity</h1>
   348         In order to setup OpenSecurity an inital VM image has to be downloaded and imported:<br/>
   349         <ul>
   350             <li>Download initial VM image: <a href="/fetch_initial_image">fetch_initial_image</a>
   351             <li>Import initial VM: <a href="/init">init</a>
   352         </ul>
   353         </body>
   354         </html>
   355         """
   356         return page
   357 
   358 
   359 class os_terminate:
   360     """OpenSecurity '/terminate' handler
   361     
   362     - GET: terminate the opensecurityd.
   363 
   364     TODO: need to find a better way doing this, and not via the
   365           REST api. Maybe hack web.py server code?
   366     """
   367     
   368     def GET(self):
   369         log_call(web.ctx.environ)
   370         global gvm_mgr
   371         gvm_mgr.stop()
   372         gvm_mgr.cleanup()
   373         global server
   374         server.stop()
   375         return None
   376 
   377 class os_initialize:
   378     """OpenSecurity '/initialize' handler
   379     
   380     - GET: initialize / starts the vmmanager.
   381 
   382     """
   383     
   384     def GET(self):
   385         log_call(web.ctx.environ)
   386         global gvm_mgr
   387         gvm_mgr.cleanup()
   388         gvm_mgr.start()
   389         #global server
   390         #server.run()
   391         return None
   392 
   393 class os_vm:
   394     """OpenSecurity '/vms/[VM]' handler
   395     
   396     - GET: list information of arbitrary VM.
   397     """
   398     
   399     def GET(self, name):
   400         log_call(web.ctx.environ)
   401         global gvm_mgr
   402         return gvm_mgr.getVMInfo(name)
   403             
   404 
   405 class os_vms:
   406     """OpenSecurity '/vms' handler
   407     
   408     - GET: list all (also non Security) VMs.
   409     """
   410     
   411     def GET(self):
   412         log_call(web.ctx.environ)
   413         global gvm_mgr
   414         return str(gvm_mgr.listVM()).replace("'",'"')
   415             
   416 
   417 def log_call(web_environ):
   418     """log the incoming call to the REST api"""
   419     try:
   420         call = 'REST ' +  web_environ['REQUEST_METHOD'] + ' ' + web_environ['REQUEST_URI'] + ' from ' + web_environ['REMOTE_ADDR'] + ':' + web_environ['REMOTE_PORT']
   421         logger.debug(call)
   422     except:
   423         pass
   424 
   425 
   426 def main():
   427     """main startup for the opensecuirityd"""
   428 
   429     global gvm_mgr
   430     global server
   431 
   432     logger.debug('Starting OpenSecurity REST server')
   433 
   434     # ensure a VMManger is yet loaded
   435     gvm_mgr = vmmanager.VMManager.getInstance()
   436     gvm_mgr.start()
   437     # tweak sys.argv to control wep.py server start behavior
   438     sys.argv = [__file__, "8080"]
   439     server = web.application(opensecurity_urls, globals(), autoreload = False)
   440     server.run()
   441     
   442     logger.debug('Stopped OpenSecurity REST server')
   443 
   444 
   445 def stop():
   446     """stop the opensecuirityd"""
   447 
   448     # calling sys.exit() raises a SystemExit exception
   449     # of the WSGI Server to let it wind down
   450     # gracefully
   451     sys.exit(0)
   452 
   453 # start
   454 if __name__ == "__main__":
   455     main()
   456     sys.exit(0)
   457