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