OpenSecurity/bin/opensecurityd.pyw
author BarthaM@N3SIM1218.D03.arc.local
Thu, 17 Jul 2014 10:20:10 +0100
changeset 212 59ebaa44c12c
parent 204 0c3136908f4f
child 213 2e0b94e12bfc
permissions -rwxr-xr-x
Modified update_template to cope with unattached .vmdk
Added start method to vmmanager
Modified vmmanager to not start automatically over getInstance() invocation
Modified cygwin to corectly get the root folder (OpenSecurity//bin)
     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         log_call(web.ctx.environ)
   101         global gvm_mgr
   102         try:
   103             result = gvm_mgr.handleBrowsingRequest()
   104             return result
   105         except:
   106             raise web.internalerror()
   107 
   108        
   109 class os_fetch_image:
   110     """OpenSecurity '/fetch_initial_image' handler
   111     
   112     - GET: fetch the initial image from the X-Net Servers
   113             The initial image is stored in the
   114             Virtual Box default machine path.
   115             The result to this call is a temprary file
   116             which shows the progress (or error state)
   117             of this call.
   118     """
   119     
   120     def GET(self):
   121         
   122         log_call(web.ctx.environ)
   123         global gvm_mgr
   124 
   125         trace_file_name = os.path.join(Environment('OpenSecurity').log_path, 'OpenSecurity_fetch_image.log')
   126         trace_file = open(trace_file_name, 'w+')
   127 
   128         machine_folder = Cygwin.cygPath(gvm_mgr.getMachineFolder()) 
   129         download_initial_image_script = Cygwin.cygPath(os.path.abspath(os.path.join(os.path.split(__file__)[0], 'download_initial_image.sh')))
   130         Cygwin.bashExecute('\\"' + download_initial_image_script + '\\" \'' + machine_folder + '\'', wait_return = False, stdout = trace_file, stderr = trace_file) 
   131 
   132         res = '{ "fetch_log": "' + trace_file_name.replace('\\', '\\\\') + '" }'
   133         return res
   134 
   135 
   136 class os_init:
   137     """OpenSecurity '/init' handler
   138     
   139     - GET: Do initial import of OsecVM.ova
   140     """
   141     
   142     def GET(self):
   143         log_call(web.ctx.environ)
   144         global gvm_mgr
   145 
   146         gvm_mgr.stop()
   147         gvm_mgr.cleanup()
   148         
   149         if gvm_mgr.vmRootName in gvm_mgr.listVM():
   150             gvm_mgr.poweroffVM(gvm_mgr.vmRootName)
   151             tmplateUUID = gvm_mgr.getTemplateUUID()
   152             if tmplateUUID != None:
   153                 logger.debug('found parent uuid ' + tmplateUUID)
   154                 gvm_mgr.storageDetach(gvm_mgr.vmRootName)
   155                 gvm_mgr.removeSnapshots(tmplateUUID)
   156                 gvm_mgr.removeImage(tmplateUUID)
   157             else:
   158                 logger.debug('parent uuid not found')
   159             gvm_mgr.removeVM(gvm_mgr.vmRootName)
   160         
   161         trace_file_name = os.path.join(Environment('OpenSecurity').log_path, 'OpenSecurity_initial_import.log')
   162         trace_file = open(trace_file_name, 'w+')
   163 
   164         vm_image = Cygwin.cygPath(gvm_mgr.getMachineFolder()) + '/OsecVM.ova'
   165         initial_import_script = Cygwin.cygPath(os.path.abspath(os.path.join(os.path.split(__file__)[0], 'initial_vm.sh')))
   166         Cygwin.bashExecute('\\"' + initial_import_script + '\\" \'' + vm_image + '\'', wait_return = False, stdout = trace_file, stderr = trace_file) 
   167 
   168         res = '{ "init_log": "' + trace_file_name.replace('\\', '\\\\') + '" }'
   169         return res
   170 
   171 
   172 class os_initial_image:
   173     """OpenSecurity '/initial_image' handler
   174     
   175     - GET: Return what we have as initial image.
   176     """
   177     
   178     def GET(self):
   179         log_call(web.ctx.environ)
   180         global gvm_mgr
   181         t = os.path.join(gvm_mgr.systemProperties['Default machine folder'], 'OsecVM.ova')
   182         res = ''
   183         if os.path.isfile(t):
   184             res = '{"initial_template": { '
   185             res += '"name": "OsecVM.ova", '
   186             res += '"path": "' + t.replace('\\', '\\\\') + '", '
   187             res += '"size": ' + str(os.path.getsize(t)) + ', ' 
   188             res += '"date": ' + str(os.path.getmtime(t)) + ''
   189             res += '}}'
   190         return res
   191 
   192 
   193 class os_root:
   194     """OpenSecurity '/' handler
   195     
   196     - GET: give information about current installation.
   197     """
   198     
   199     def GET(self):
   200         log_call(web.ctx.environ)
   201         global gvm_mgr
   202 
   203         # create a json string and pretty print it
   204         res = '{"os_server": { '
   205         res += '"version": "' + opensecurity.__version__ + '" '
   206         res += ', "virtual box systemproperties": ' + str(gvm_mgr.systemProperties).replace("'", '"') 
   207         res += ', "current temporary folder": "' + tempfile.gettempdir().replace('\\', '\\\\') + '"'
   208         res += ', "current log folder": "' + Environment('OpenSecurity').log_path.replace('\\', '\\\\') + '"'
   209 
   210         try:
   211             res += ', "whoami": "' + Cygwin.bashExecute('whoami')[1].strip() + '"'
   212         except:
   213             res += ', "whoami": "FAILED"'
   214 
   215         try:
   216             res += ', "mount": ' + str(Cygwin.bashExecute('mount')[1].split('\n')[:-1]).replace("'", '"')
   217         except:
   218             res += ', "mount": "FAILED"'
   219 
   220         try:
   221             res += ', "cygpath --windows ~": "' + Cygwin.bashExecute('cygpath --windows ~')[1].strip().replace('\\', '\\\\') + '"'
   222         except:
   223             res += ', "cygpath --windows ~": "FAILED"'
   224 
   225 
   226         res += ', "status message": "' + gvm_mgr.status_message.replace('"', "'") + '"'
   227 
   228         res += '}}'
   229 
   230         # loading it into json and print it again ensures
   231         # we really do have a valid RFC conform json string
   232         # created (as long as the python json module is RFC conform)
   233         return json.dumps(json.loads(res), indent = 4)
   234 
   235 
   236 
   237 class os_sdvm:
   238     """OpenSecurity '/sdvms/[VM]' handler
   239     
   240     - GET: Information about a specific SecurityVM
   241     - DELETE: Remove a specific
   242     """
   243     
   244     def GET(self, name):
   245         log_call(web.ctx.environ)
   246         global gvm_mgr
   247         return json.dumps(gvm_mgr.getVMInfo(name), indent = 4)
   248 
   249     def DELETE(self, name):
   250         log_call(web.ctx.environ)
   251         global gvm_mgr
   252         return gvm_mgr.removeVM(name)
   253             
   254 
   255 class os_sdvm_application:
   256     """OpenSecurity '/sdvms/[VM]/application/[CMD]' handler
   257     
   258     - GET: start application with given command in the VM.
   259     """
   260     
   261     def GET(self, name, command):
   262         log_call(web.ctx.environ)
   263         global gvm_mgr
   264         command = '/' + command
   265         showTrayMessage('Launching application in isolated VM...', 7000)
   266         result = Cygwin.sshExecuteX11(command, gvm_mgr.getHostOnlyIP(name), 'osecuser', Cygwin.cygPath(gvm_mgr.getMachineFolder()) + '/' + name + '/dvm_key'  )
   267         return 'Command ' + str(command) + ' started on VM "' + name + '" with IP ' + gvm_mgr.getHostOnlyIP(name)
   268     
   269 
   270 class os_sdvm_ip:
   271     """OpenSecurity '/sdvms/[VM]/ip' handler
   272     
   273     - GET: give IP of SecurityVM.
   274     """
   275     
   276     def GET(self, name):
   277         log_call(web.ctx.environ)
   278         global gvm_mgr
   279         return gvm_mgr.getHostOnlyIP(name)
   280             
   281 
   282 class os_sdvm_start:
   283     """OpenSecurity '/sdvms/[VM]/start' handler
   284     
   285     - GET: Start specific SecuirtyVM.
   286     """
   287     
   288     def GET(self, name):
   289         log_call(web.ctx.environ)
   290         global gvm_mgr
   291         return gvm_mgr.startVM(name)
   292             
   293 
   294 class os_sdvm_stop:
   295     """OpenSecurity '/sdvms/[VM]/stop' handler
   296     
   297     - GET: stop specific Secuirty VM.
   298     """
   299     
   300     def GET(self, name):
   301         log_call(web.ctx.environ)
   302         global gvm_mgr
   303         return gvm_mgr.stopVM(name)
   304             
   305 
   306 class os_sdvms:
   307     """OpenSecurity '/sdvms' handler
   308     
   309     - GET: list all available secuirty VMs.
   310     - POST: create new security vm.
   311     """
   312     
   313     def GET(self):
   314         """get the list of SDVMs"""
   315         log_call(web.ctx.environ)
   316         global gvm_mgr
   317 
   318         d = {}
   319         for sdvm in gvm_mgr.listSDVM():
   320             d[sdvm] = gvm_mgr.getHostOnlyIP(sdvm)
   321 
   322         return json.dumps(d, indent = 4)
   323             
   324     def POST(self):
   325         """create a new SDVM"""
   326         log_call(web.ctx.environ)
   327         global gvm_mgr
   328         
   329         # get a new vm-name
   330         name = gvm_mgr.generateSDVMName()
   331         try:
   332             gvm_mgr.createVM(name)
   333         except:
   334             raise web.internalerror()
   335             
   336         return name
   337             
   338 
   339 class os_setup:
   340     """OpenSecurity '/setup' handler
   341     
   342     - GET: Give user some info how to setup the OpenSecurity environment
   343     """
   344     
   345     def GET(self):
   346 
   347         log_call(web.ctx.environ)
   348 
   349         page = """
   350         <html>
   351         <body>
   352         <h1>Setup OpenSecurity</h1>
   353         In order to setup OpenSecurity an inital VM image has to be downloaded and imported:<br/>
   354         <ul>
   355             <li>Download initial VM image: <a href="/fetch_initial_image">fetch_initial_image</a>
   356             <li>Import initial VM: <a href="/init">init</a>
   357         </ul>
   358         </body>
   359         </html>
   360         """
   361         return page
   362 
   363 
   364 class os_terminate:
   365     """OpenSecurity '/terminate' handler
   366     
   367     - GET: terminate the opensecurityd.
   368 
   369     TODO: need to find a better way doing this, and not via the
   370           REST api. Maybe hack web.py server code?
   371     """
   372     
   373     def GET(self):
   374         log_call(web.ctx.environ)
   375         global gvm_mgr
   376         gvm_mgr.stop()
   377         gvm_mgr.cleanup()
   378         global server
   379         server.stop()
   380         return None
   381 
   382 class os_initialize:
   383     """OpenSecurity '/initialize' handler
   384     
   385     - GET: initialize / starts the vmmanager.
   386 
   387     """
   388     
   389     def GET(self):
   390         log_call(web.ctx.environ)
   391         global gvm_mgr
   392         gvm_mgr.cleanup()
   393         gvm_mgr.start()
   394         global server
   395         server.run()
   396         return None
   397 
   398 class os_update_template:
   399     """OpenSecurity '/update_template' handler
   400     
   401     - GET: update template vm
   402     """
   403     
   404     def GET(self):
   405         #return gvm_mgr.guestExecute('SecurityDVM', 'sudo apt-get -y update')
   406         global gvm_mgr
   407         log_call(web.ctx.environ)
   408         return gvm_mgr.updateTemplate()
   409 
   410 
   411 class os_vm:
   412     """OpenSecurity '/vms/[VM]' handler
   413     
   414     - GET: list information of arbitrary VM.
   415     """
   416     
   417     def GET(self, name):
   418         log_call(web.ctx.environ)
   419         global gvm_mgr
   420         return gvm_mgr.getVMInfo(name)
   421             
   422 
   423 class os_vms:
   424     """OpenSecurity '/vms' handler
   425     
   426     - GET: list all (also non Security) VMs.
   427     """
   428     
   429     def GET(self):
   430         log_call(web.ctx.environ)
   431         global gvm_mgr
   432         return str(gvm_mgr.listVM()).replace("'",'"')
   433             
   434 
   435 def log_call(web_environ):
   436     """log the incoming call to the REST api"""
   437     try:
   438         call = 'REST ' +  web_environ['REQUEST_METHOD'] + ' ' + web_environ['REQUEST_URI'] + ' from ' + web_environ['REMOTE_ADDR'] + ':' + web_environ['REMOTE_PORT']
   439         logger.debug(call)
   440     except:
   441         pass
   442 
   443 
   444 def main():
   445     """main startup for the opensecuirityd"""
   446 
   447     global gvm_mgr
   448     global server
   449 
   450     logger.debug('Starting OpenSecurity REST server')
   451 
   452     # ensure a VMManger is yet loaded
   453     gvm_mgr = vmmanager.VMManager.getInstance()
   454     gvm_mgr.start()
   455     # tweak sys.argv to control wep.py server start behavior
   456     sys.argv = [__file__, "8080"]
   457     server = web.application(opensecurity_urls, globals(), autoreload = False)
   458     server.run()
   459     
   460     logger.debug('Stopped OpenSecurity REST server')
   461 
   462 
   463 def stop():
   464     """stop the opensecuirityd"""
   465 
   466     # calling sys.exit() raises a SystemExit exception
   467     # of the WSGI Server to let it wind down
   468     # gracefully
   469     sys.exit(0)
   470 
   471 # start
   472 if __name__ == "__main__":
   473     main()
   474     sys.exit(0)
   475