OpenSecurity/bin/opensecurityd.py
changeset 16 e16d64b5e008
parent 15 2e4cb1ebcbed
parent 13 4457d7071a23
child 17 0b4efa323de3
     1.1 --- a/OpenSecurity/bin/opensecurityd.py	Fri Dec 06 12:15:18 2013 +0100
     1.2 +++ b/OpenSecurity/bin/opensecurityd.py	Fri Dec 06 12:24:24 2013 +0100
     1.3 @@ -52,137 +52,64 @@
     1.4  """All the URLs we know mapping to class handler"""
     1.5  opensecurity_urls = (
     1.6      '/device_change',           'os_device_change',
     1.7 -    '/application',             'os_application',
     1.8 -    '/device',                  'os_device',
     1.9 -    '/device/credentials',      'os_device_credentials',
    1.10 -    '/device/password',         'os_device_password',
    1.11 +    '/sdvms',                   'os_sdvms',
    1.12 +    '/vms',                     'os_vms',
    1.13 +    '/vms/(.*)',                'os_vm',
    1.14      '/',                        'os_root'
    1.15  )
    1.16  
    1.17  
    1.18  # ------------------------------------------------------------
    1.19 +# vars
    1.20 +
    1.21 +# Global VMManager instance
    1.22 +gvm_mgr = VMManager()
    1.23 +
    1.24 +
    1.25 +# ------------------------------------------------------------
    1.26  # code
    1.27  
    1.28 -gvm_mgr = VMManager()
    1.29 -
    1.30 -
    1.31 -class os_application:
    1.32 -    
    1.33 -    """OpenSecurity '/application' handler.
    1.34 -    
    1.35 -    This is called on GET /application?vm=VM-ID&app=APP-ID
    1.36 -    This tries to access the vm identified with the label VM-ID
    1.37 -    and launched the application identified APP-ID
    1.38 -    """
    1.39 -    
    1.40 -    def GET(self):
    1.41 -        
    1.42 -        # pick the arguments
    1.43 -        args = web.input()
    1.44 -        
    1.45 -        # we _need_ a vm
    1.46 -        if not "vm" in args:
    1.47 -            raise web.badrequest()
    1.48 -        
    1.49 -        # we _need_ a app
    1.50 -        if not "app" in args:
    1.51 -            raise web.badrequest()
    1.52 -        
    1.53 -        ## TODO: HARD CODED STUFF HERE! THIS SHOULD BE FLEXIBLE!
    1.54 -        ssh_private_key = os.path.join(Environment("opensecurity").data_path, 'share', '192.168.56.15.ppk')
    1.55 -        putty_session = '192.168.56.15'
    1.56 -        process_command = ['plink.exe', '-i', ssh_private_key, putty_session, args.app]
    1.57 -        si = subprocess.STARTUPINFO()
    1.58 -        si.dwFlags = subprocess.STARTF_USESHOWWINDOW
    1.59 -        si.wShowWindow = subprocess.SW_HIDE
    1.60 -        print('tyring to launch: ' + ' '.join(process_command))
    1.61 -        process = subprocess.Popen(process_command, shell = True)
    1.62 -        return 'launched: ' + ' '.join(process_command)
    1.63 -
    1.64 -class os_device:
    1.65 -    
    1.66 -    """OpenSecurity '/device' handler"""
    1.67 -    
    1.68 -    def GET(self):
    1.69 -        return "os_device"
    1.70  
    1.71  class os_device_change:
    1.72 -    
    1.73      """OpenSecurity '/device_change' handler"""
    1.74      
    1.75      def GET(self):
    1.76 +        #gvm_mgr.configureHostNetworking()
    1.77          print 'received device_change'
    1.78 -        gvm_mgr.cygwin_path = 'c:\\cygwin64\\bin\\'
    1.79 -        gvm_mgr.handleDeviceChange()
    1.80 -        
    1.81 -        #gvm_mgr.configureHostNetworking()
    1.82          return "os_device_change"
    1.83  
    1.84  
    1.85 -class os_device_credentials:
    1.86 -    
    1.87 -    """OpenSecurity '/device/credentials' handler.
    1.88 -    
    1.89 -    This is called on GET /device/credentials?id=DEVICE-ID.
    1.90 -    Ideally this should pop up a user dialog to insert his
    1.91 -    credentials based the DEVICE-ID
    1.92 -    """
    1.93 +class os_sdvms:
    1.94 +    """OpenSecurity '/sdvms' handler"""
    1.95      
    1.96      def GET(self):
    1.97 -        
    1.98 -        # pick the arguments
    1.99 -        args = web.input()
   1.100 -        
   1.101 -        # we _need_ a device id
   1.102 -        if not "id" in args:
   1.103 -            raise web.badrequest()
   1.104 -        
   1.105 -        # invoke the user dialog as a subprocess
   1.106 -        dlg_credentials_image = os.path.join(sys.path[0], 'opensecurity-dialog.py')
   1.107 -        process_command = [sys.executable, dlg_credentials_image, 'credentials', 'Please provide credentials for accessing \ndevice: "{0}".'.format(args.id)]
   1.108 -        process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
   1.109 -        result = process.communicate()[0]
   1.110 -        if process.returncode != 0:
   1.111 -            return 'Credentials request has been aborted.'
   1.112 -        
   1.113 -        return result
   1.114 +        return gvm_mgr.listSDVM() 
   1.115 +            
   1.116  
   1.117 +class os_vm:
   1.118 +    """OpenSecurity '/vms/VM' handler"""
   1.119 +    
   1.120 +    def GET(self, name):
   1.121 +        return gvm_mgr.getVMInfo(name)
   1.122 +            
   1.123  
   1.124 -class os_device_password:
   1.125 -    
   1.126 -    """OpenSecurity '/device/password' handler.
   1.127 -    
   1.128 -    This is called on GET /device/password?id=DEVICE-ID.
   1.129 -    Ideally this should pop up a user dialog to insert his
   1.130 -    password based the DEVICE-ID
   1.131 -    """
   1.132 +class os_vms:
   1.133 +    """OpenSecurity '/vms' handler"""
   1.134      
   1.135      def GET(self):
   1.136 -        
   1.137 -        # pick the arguments
   1.138 -        args = web.input()
   1.139 -        
   1.140 -        # we _need_ a device id
   1.141 -        if not "id" in args:
   1.142 -            raise web.badrequest()
   1.143 +        return gvm_mgr.listVM() 
   1.144              
   1.145 -        # invoke the user dialog as a subprocess
   1.146 -        dlg_credentials_image = os.path.join(sys.path[0], 'opensecurity-dialog.py')
   1.147 -        process_command = [sys.executable, dlg_credentials_image, 'password', 'Please provide a password for accessing \ndevice: "{0}".'.format(args.id)]
   1.148 -        process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
   1.149 -        result = process.communicate()[0]
   1.150 -        if process.returncode != 0:
   1.151 -            return 'Credentials request has been aborted.'
   1.152 -        
   1.153 -        return result
   1.154 -
   1.155  
   1.156  class os_root:
   1.157 -    
   1.158      """OpenSecurity '/' handler"""
   1.159      
   1.160      def GET(self):
   1.161 -        return "OpenSecurity-Server { \"version\": \"%s\" }" % __version__
   1.162 +        res = "'os_server': { "
   1.163 +        res += "'version': '" + __version__ + "', "
   1.164 +        res += "'virtualbox_path': '" + gvm_mgr.vBoxPath + "', "
   1.165 +        res += "'machine_folder': '" + gvm_mgr.getDefaultMachineFolder() + "' "
   1.166 +        res += "}"
   1.167 +        return res
   1.168  
   1.169  
   1.170  # start