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