OpenSecurity/bin/opensecurity_client_restful_server.py
author om
Fri, 06 Dec 2013 12:24:24 +0100
changeset 16 e16d64b5e008
parent 14 c187aaceca32
parent 13 4457d7071a23
child 29 3f564e1673bb
permissions -rwxr-xr-x
working on client/server code merge
     1 #!/bin/env python
     2 # -*- coding: utf-8 -*-
     3 
     4 # ------------------------------------------------------------
     5 # opensecurity_client_restful_server
     6 # 
     7 # the OpenSecurity client 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 import opensecurity_server
    44 
    45 
    46 # ------------------------------------------------------------
    47 # const
    48 
    49 
    50 __version__ = "0.1"
    51 
    52 
    53 """All the URLs we know mapping to class handler"""
    54 opensecurity_urls = (
    55     '/application',             'os_application',
    56     '/credentials',             'os_credentials',
    57     '/password',                'os_password',
    58     '/',                        'os_root'
    59 )
    60 
    61 
    62 # ------------------------------------------------------------
    63 # code
    64 
    65 
    66 class os_application:
    67     """OpenSecurity '/application' handler.
    68     
    69     This is called on GET /application?vm=VM-ID&app=APP-ID
    70     This tries to access the vm identified with the label VM-ID
    71     and launched the application identified APP-ID
    72     """
    73     
    74     def GET(self):
    75         
    76         # pick the arguments
    77         args = web.input()
    78         
    79         # we _need_ a vm
    80         if not "vm" in args:
    81             raise web.badrequest()
    82         
    83         # we _need_ a app
    84         if not "app" in args:
    85             raise web.badrequest()
    86         
    87         apps = opensecurity_server.query_apps()
    88         vms = opensecurity_server.query_vms()
    89         
    90         # check if we do have valid vm
    91         v = [v for v in vms if v['name'] == args.vm]
    92         if len(v) == 0:
    93             raise web.notfound('vm not found')
    94         v = v[0]
    95         
    96         # check if we do have a valid app
    97         a = [a for a in apps if a['name'] == args.app]
    98         if len(a) == 0:
    99             raise web.notfound('app not found')
   100         a = a[0]
   101         
   102         # invoke launch with 
   103         res = "starting: launch " + v['user'] + " " + v['ip'] + " " + a['command']
   104 
   105         launch_image = os.path.join(sys.path[0], 'launch.py')
   106         process_command = [sys.executable, launch_image, v['user'], v['ip'], a['command']]
   107         process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
   108         result = process.communicate()[0]
   109         if process.returncode != 0:
   110             return 'Launch of application aborted.'
   111         
   112         return result
   113         
   114 
   115 class os_credentials:
   116     """OpenSecurity '/credentials' handler.
   117     
   118     This is called on GET /credentials?text=TEXT.
   119     Ideally this should pop up a user dialog to insert his
   120     credentials based the given TEXT.
   121     """
   122     
   123     def GET(self):
   124         
   125         # pick the arguments
   126         args = web.input()
   127         
   128         # we _need_ a device id
   129         if not "text" in args:
   130             raise web.badrequest()
   131         
   132         # invoke the user dialog as a subprocess
   133         dlg_credentials_image = os.path.join(sys.path[0], 'opensecurity_dialog.py')
   134         process_command = [sys.executable, dlg_credentials_image, 'credentials', args.text]
   135         process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
   136         result = process.communicate()[0]
   137         if process.returncode != 0:
   138             return 'Credentials request has been aborted.'
   139         
   140         return result
   141 
   142 
   143 class os_password:
   144     """OpenSecurity '/password' handler.
   145     
   146     This is called on GET /password?text=TEXT.
   147     Ideally this should pop up a user dialog to insert his
   148     password based device name.
   149     """
   150     
   151     def GET(self):
   152         
   153         # pick the arguments
   154         args = web.input()
   155         
   156         # we _need_ a device id
   157         if not "text" in args:
   158             raise web.badrequest()
   159             
   160         # invoke the user dialog as a subprocess
   161         dlg_credentials_image = os.path.join(sys.path[0], 'opensecurity_dialog.py')
   162         process_command = [sys.executable, dlg_credentials_image, 'password', args.text]
   163         process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
   164         result = process.communicate()[0]
   165         if process.returncode != 0:
   166             return 'password request has been aborted.'
   167         
   168         return result
   169 
   170 
   171 class os_root:
   172     """OpenSecurity '/' handler"""
   173     
   174     def GET(self):
   175     
   176         res = "OpenSecurity-Client RESTFul Server { \"version\": \"%s\" }" % __version__
   177         
   178         # add some sample links
   179         res = res + """
   180         
   181 USAGE EXAMPLES:
   182         
   183 Request a password: 
   184     (copy paste this into your browser's address field after the host:port)
   185     
   186     /password?text=Give+me+a+password+for+device+%22My+USB+Drive%22+(ID%3A+32090-AAA-X0)
   187     
   188     (eg.: http://127.0.0.1:8090/password?text=Give+me+a+password+for+device+%22My+USB+Drive%22+(ID%3A+32090-AAA-X0))
   189     NOTE: check yout taskbar, the dialog window may not pop up in front of your browser window.
   190     
   191     
   192 Request a combination of user and password:
   193     (copy paste this into your browser's address field after the host:port)
   194     
   195     /credentials?text=Tell+the+NSA+which+credentials+to+use+in+order+to+avoid+hacking+noise+on+wire.
   196     
   197     (eg.: http://127.0.0.1:8090/credentials?text=Tell+the+NSA+which+credentials+to+use+in+order+to+avoid+hacking+noise+on+wire.)
   198     NOTE: check yout taskbar, the dialog window may not pop up in front of your browser window.
   199     
   200 
   201 Start a Browser:
   202     (copy paste this into your browser's address field after the host:port)
   203 
   204     /application?vm=Debian+7&app=Browser
   205 
   206     (e.g. http://127.0.0.1:8090/application?vm=Debian+7&app=Browser)
   207         """
   208     
   209         return res
   210 
   211 
   212 # start
   213 if __name__ == "__main__":
   214     server = web.application(opensecurity_urls, globals())
   215     server.run()