server/opensecurityd.py
changeset 15 2e4cb1ebcbed
parent 14 c187aaceca32
child 16 e16d64b5e008
     1.1 --- a/server/opensecurityd.py	Fri Dec 06 12:10:30 2013 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,192 +0,0 @@
     1.4 -#!/bin/env python
     1.5 -# -*- coding: utf-8 -*-
     1.6 -
     1.7 -# ------------------------------------------------------------
     1.8 -# opensecurityd
     1.9 -# 
    1.10 -# the opensecurityd as RESTful server
    1.11 -#
    1.12 -# Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
    1.13 -#
    1.14 -# Copyright (C) 2013 AIT Austrian Institute of Technology
    1.15 -# AIT Austrian Institute of Technology GmbH
    1.16 -# Donau-City-Strasse 1 | 1220 Vienna | Austria
    1.17 -# http://www.ait.ac.at
    1.18 -#
    1.19 -# This program is free software; you can redistribute it and/or
    1.20 -# modify it under the terms of the GNU General Public License
    1.21 -# as published by the Free Software Foundation version 2.
    1.22 -# 
    1.23 -# This program is distributed in the hope that it will be useful,
    1.24 -# but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.25 -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.26 -# GNU General Public License for more details.
    1.27 -# 
    1.28 -# You should have received a copy of the GNU General Public License
    1.29 -# along with this program; if not, write to the Free Software
    1.30 -# Foundation, Inc., 51 Franklin Street, Fifth Floor, 
    1.31 -# Boston, MA  02110-1301, USA.
    1.32 -# ------------------------------------------------------------
    1.33 -
    1.34 -
    1.35 -# ------------------------------------------------------------
    1.36 -# imports
    1.37 -
    1.38 -import os
    1.39 -import os.path
    1.40 -import subprocess
    1.41 -import sys
    1.42 -import web
    1.43 -from vmmanager.vmmanager import VMManager
    1.44 -
    1.45 -# local
    1.46 -from environment import Environment
    1.47 -
    1.48 -
    1.49 -# ------------------------------------------------------------
    1.50 -# const
    1.51 -
    1.52 -__version__ = "0.1"
    1.53 -
    1.54 -
    1.55 -"""All the URLs we know mapping to class handler"""
    1.56 -opensecurity_urls = (
    1.57 -    '/device_change',           'os_device_change',
    1.58 -    '/application',             'os_application',
    1.59 -    '/device',                  'os_device',
    1.60 -    '/device/credentials',      'os_device_credentials',
    1.61 -    '/device/password',         'os_device_password',
    1.62 -    '/',                        'os_root'
    1.63 -)
    1.64 -
    1.65 -
    1.66 -# ------------------------------------------------------------
    1.67 -# code
    1.68 -
    1.69 -gvm_mgr = VMManager()
    1.70 -
    1.71 -
    1.72 -class os_application:
    1.73 -    
    1.74 -    """OpenSecurity '/application' handler.
    1.75 -    
    1.76 -    This is called on GET /application?vm=VM-ID&app=APP-ID
    1.77 -    This tries to access the vm identified with the label VM-ID
    1.78 -    and launched the application identified APP-ID
    1.79 -    """
    1.80 -    
    1.81 -    def GET(self):
    1.82 -        
    1.83 -        # pick the arguments
    1.84 -        args = web.input()
    1.85 -        
    1.86 -        # we _need_ a vm
    1.87 -        if not "vm" in args:
    1.88 -            raise web.badrequest()
    1.89 -        
    1.90 -        # we _need_ a app
    1.91 -        if not "app" in args:
    1.92 -            raise web.badrequest()
    1.93 -        
    1.94 -        ## TODO: HARD CODED STUFF HERE! THIS SHOULD BE FLEXIBLE!
    1.95 -        ssh_private_key = os.path.join(Environment("opensecurity").data_path, 'share', '192.168.56.15.ppk')
    1.96 -        putty_session = '192.168.56.15'
    1.97 -        process_command = ['plink.exe', '-i', ssh_private_key, putty_session, args.app]
    1.98 -        si = subprocess.STARTUPINFO()
    1.99 -        si.dwFlags = subprocess.STARTF_USESHOWWINDOW
   1.100 -        si.wShowWindow = subprocess.SW_HIDE
   1.101 -        print('tyring to launch: ' + ' '.join(process_command))
   1.102 -        process = subprocess.Popen(process_command, shell = True)
   1.103 -        return 'launched: ' + ' '.join(process_command)
   1.104 -
   1.105 -class os_device:
   1.106 -    
   1.107 -    """OpenSecurity '/device' handler"""
   1.108 -    
   1.109 -    def GET(self):
   1.110 -        return "os_device"
   1.111 -
   1.112 -class os_device_change:
   1.113 -    
   1.114 -    """OpenSecurity '/device_change' handler"""
   1.115 -    
   1.116 -    def GET(self):
   1.117 -        print 'received device_change'
   1.118 -        gvm_mgr.cygwin_path = 'c:\\cygwin64\\bin\\'
   1.119 -        gvm_mgr.handleDeviceChange()
   1.120 -        
   1.121 -        #gvm_mgr.configureHostNetworking()
   1.122 -        return "os_device_change"
   1.123 -
   1.124 -
   1.125 -class os_device_credentials:
   1.126 -    
   1.127 -    """OpenSecurity '/device/credentials' handler.
   1.128 -    
   1.129 -    This is called on GET /device/credentials?id=DEVICE-ID.
   1.130 -    Ideally this should pop up a user dialog to insert his
   1.131 -    credentials based the DEVICE-ID
   1.132 -    """
   1.133 -    
   1.134 -    def GET(self):
   1.135 -        
   1.136 -        # pick the arguments
   1.137 -        args = web.input()
   1.138 -        
   1.139 -        # we _need_ a device id
   1.140 -        if not "id" in args:
   1.141 -            raise web.badrequest()
   1.142 -        
   1.143 -        # invoke the user dialog as a subprocess
   1.144 -        dlg_credentials_image = os.path.join(sys.path[0], 'opensecurity-dialog.py')
   1.145 -        process_command = [sys.executable, dlg_credentials_image, 'credentials', 'Please provide credentials for accessing \ndevice: "{0}".'.format(args.id)]
   1.146 -        process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
   1.147 -        result = process.communicate()[0]
   1.148 -        if process.returncode != 0:
   1.149 -            return 'Credentials request has been aborted.'
   1.150 -        
   1.151 -        return result
   1.152 -
   1.153 -
   1.154 -class os_device_password:
   1.155 -    
   1.156 -    """OpenSecurity '/device/password' handler.
   1.157 -    
   1.158 -    This is called on GET /device/password?id=DEVICE-ID.
   1.159 -    Ideally this should pop up a user dialog to insert his
   1.160 -    password based the DEVICE-ID
   1.161 -    """
   1.162 -    
   1.163 -    def GET(self):
   1.164 -        
   1.165 -        # pick the arguments
   1.166 -        args = web.input()
   1.167 -        
   1.168 -        # we _need_ a device id
   1.169 -        if not "id" in args:
   1.170 -            raise web.badrequest()
   1.171 -            
   1.172 -        # invoke the user dialog as a subprocess
   1.173 -        dlg_credentials_image = os.path.join(sys.path[0], 'opensecurity-dialog.py')
   1.174 -        process_command = [sys.executable, dlg_credentials_image, 'password', 'Please provide a password for accessing \ndevice: "{0}".'.format(args.id)]
   1.175 -        process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
   1.176 -        result = process.communicate()[0]
   1.177 -        if process.returncode != 0:
   1.178 -            return 'Credentials request has been aborted.'
   1.179 -        
   1.180 -        return result
   1.181 -
   1.182 -
   1.183 -class os_root:
   1.184 -    
   1.185 -    """OpenSecurity '/' handler"""
   1.186 -    
   1.187 -    def GET(self):
   1.188 -        return "OpenSecurity-Server { \"version\": \"%s\" }" % __version__
   1.189 -
   1.190 -
   1.191 -# start
   1.192 -if __name__ == "__main__":
   1.193 -    server = web.application(opensecurity_urls, globals())
   1.194 -    server.run()
   1.195 -