ait/os/bin/opensecurityd/opensecurityd.py
author om
Tue, 12 Nov 2013 11:31:34 +0100
branchom
changeset 2 c9bf2537109a
permissions -rwxr-xr-x
added C/C++ and Python sources
     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 #
    11 # Copyright (C) 2013 AIT Austrian Institute of Technology
    12 # AIT Austrian Institute of Technology GmbH
    13 # Donau-City-Strasse 1 | 1220 Vienna | Austria
    14 # http://www.ait.ac.at
    15 #
    16 # This program is free software; you can redistribute it and/or
    17 # modify it under the terms of the GNU General Public License
    18 # as published by the Free Software Foundation version 2.
    19 # 
    20 # This program is distributed in the hope that it will be useful,
    21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
    22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    23 # GNU General Public License for more details.
    24 # 
    25 # You should have received a copy of the GNU General Public License
    26 # along with this program; if not, write to the Free Software
    27 # Foundation, Inc., 51 Franklin Street, Fifth Floor, 
    28 # Boston, MA  02110-1301, USA.
    29 # ------------------------------------------------------------
    30 
    31 
    32 # ------------------------------------------------------------
    33 # imports
    34 
    35 import os
    36 import os.path
    37 import subprocess
    38 import sys
    39 import web
    40 
    41 # local
    42 from environment import Environment
    43 
    44 
    45 # ------------------------------------------------------------
    46 # const
    47 
    48 
    49 __version__ = "0.1"
    50 
    51 
    52 """All the URLs we know mapping to class handler"""
    53 opensecurity_urls = (
    54     '/application',             'os_application',
    55     '/device',                  'os_device',
    56     '/device/credentials',      'os_device_credentials',
    57     '/device/password',         'os_device_password',
    58     '/',                        'os_root'
    59 )
    60 
    61 
    62 # ------------------------------------------------------------
    63 # code
    64 
    65 
    66 class os_application:
    67     
    68     """OpenSecurity '/application' handler.
    69     
    70     This is called on GET /application?vm=VM-ID&app=APP-ID
    71     This tries to access the vm identified with the label VM-ID
    72     and launched the application identified APP-ID
    73     """
    74     
    75     def GET(self):
    76         
    77         # pick the arguments
    78         args = web.input()
    79         
    80         # we _need_ a vm
    81         if not "vm" in args:
    82             raise web.badrequest()
    83         
    84         # we _need_ a app
    85         if not "app" in args:
    86             raise web.badrequest()
    87         
    88         ## TODO: HARD CODED STUFF HERE! THIS SHOULD BE FLEXIBLE!
    89         ssh_private_key = os.path.join(Environment("opensecurity").data_path, 'share', '192.168.56.15.ppk')
    90         putty_session = '192.168.56.15'
    91         process_command = ['plink.exe', '-i', ssh_private_key, putty_session, args.app]
    92         si = subprocess.STARTUPINFO()
    93         si.dwFlags = subprocess.STARTF_USESHOWWINDOW
    94         si.wShowWindow = subprocess.SW_HIDE
    95         print('tyring to launch: ' + ' '.join(process_command))
    96         process = subprocess.Popen(process_command, shell = True)
    97         return 'launched: ' + ' '.join(process_command)
    98 
    99 
   100 class os_device:
   101     
   102     """OpenSecurity '/device' handler"""
   103     
   104     def GET(self):
   105         return "os_device"
   106 
   107 
   108 class os_device_credentials:
   109     
   110     """OpenSecurity '/device/credentials' handler.
   111     
   112     This is called on GET /device/credentials?id=DEVICE-ID.
   113     Ideally this should pop up a user dialog to insert his
   114     credentials based the DEVICE-ID
   115     """
   116     
   117     def GET(self):
   118         
   119         # pick the arguments
   120         args = web.input()
   121         
   122         # we _need_ a device id
   123         if not "id" in args:
   124             raise web.badrequest()
   125         
   126         # invoke the user dialog as a subprocess
   127         dlg_credentials_image = os.path.join(sys.path[0], 'opensecurity-dialog.py')
   128         process_command = [sys.executable, dlg_credentials_image, 'credentials', 'Please provide credentials for accessing \ndevice: "{0}".'.format(args.id)]
   129         process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
   130         result = process.communicate()[0]
   131         if process.returncode != 0:
   132             return 'Credentials request has been aborted.'
   133         
   134         return result
   135 
   136 
   137 class os_device_password:
   138     
   139     """OpenSecurity '/device/password' handler.
   140     
   141     This is called on GET /device/password?id=DEVICE-ID.
   142     Ideally this should pop up a user dialog to insert his
   143     password based the DEVICE-ID
   144     """
   145     
   146     def GET(self):
   147         
   148         # pick the arguments
   149         args = web.input()
   150         
   151         # we _need_ a device id
   152         if not "id" in args:
   153             raise web.badrequest()
   154             
   155         # invoke the user dialog as a subprocess
   156         dlg_credentials_image = os.path.join(sys.path[0], 'opensecurity-dialog.py')
   157         process_command = [sys.executable, dlg_credentials_image, 'password', 'Please provide a password for accessing \ndevice: "{0}".'.format(args.id)]
   158         process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
   159         result = process.communicate()[0]
   160         if process.returncode != 0:
   161             return 'Credentials request has been aborted.'
   162         
   163         return result
   164 
   165 
   166 class os_root:
   167     
   168     """OpenSecurity '/' handler"""
   169     
   170     def GET(self):
   171         return "OpenSecurity-Server { \"version\": \"%s\" }" % __version__
   172 
   173 
   174 # start
   175 if __name__ == "__main__":
   176     server = web.application(opensecurity_urls, globals())
   177     server.run()
   178