OpenSecurity/bin/opensecurity_client_restful_server.py
author om
Fri, 06 Dec 2013 10:51:15 +0100
changeset 13 4457d7071a23
child 16 e16d64b5e008
permissions -rw-r--r--
adopted server code and merged client into "bin"
om@13
     1
#!/bin/env python
om@13
     2
# -*- coding: utf-8 -*-
om@13
     3
om@13
     4
# ------------------------------------------------------------
om@13
     5
# opensecurity_client_restful_server
om@13
     6
# 
om@13
     7
# the OpenSecurity client RESTful server
om@13
     8
#
om@13
     9
# Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
om@13
    10
#
om@13
    11
# Copyright (C) 2013 AIT Austrian Institute of Technology
om@13
    12
# AIT Austrian Institute of Technology GmbH
om@13
    13
# Donau-City-Strasse 1 | 1220 Vienna | Austria
om@13
    14
# http://www.ait.ac.at
om@13
    15
#
om@13
    16
# This program is free software; you can redistribute it and/or
om@13
    17
# modify it under the terms of the GNU General Public License
om@13
    18
# as published by the Free Software Foundation version 2.
om@13
    19
# 
om@13
    20
# This program is distributed in the hope that it will be useful,
om@13
    21
# but WITHOUT ANY WARRANTY; without even the implied warranty of
om@13
    22
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
om@13
    23
# GNU General Public License for more details.
om@13
    24
# 
om@13
    25
# You should have received a copy of the GNU General Public License
om@13
    26
# along with this program; if not, write to the Free Software
om@13
    27
# Foundation, Inc., 51 Franklin Street, Fifth Floor, 
om@13
    28
# Boston, MA  02110-1301, USA.
om@13
    29
# ------------------------------------------------------------
om@13
    30
om@13
    31
om@13
    32
# ------------------------------------------------------------
om@13
    33
# imports
om@13
    34
om@13
    35
import os
om@13
    36
import os.path
om@13
    37
import subprocess
om@13
    38
import sys
om@13
    39
import web
om@13
    40
om@13
    41
# local
om@13
    42
from environment import Environment
om@13
    43
import opensecurity_server
om@13
    44
om@13
    45
om@13
    46
# ------------------------------------------------------------
om@13
    47
# const
om@13
    48
om@13
    49
om@13
    50
__version__ = "0.1"
om@13
    51
om@13
    52
om@13
    53
"""All the URLs we know mapping to class handler"""
om@13
    54
opensecurity_urls = (
om@13
    55
    '/application',             'os_application',
om@13
    56
    '/credentials',             'os_credentials',
om@13
    57
    '/password',                'os_password',
om@13
    58
    '/',                        'os_root'
om@13
    59
)
om@13
    60
om@13
    61
om@13
    62
# ------------------------------------------------------------
om@13
    63
# code
om@13
    64
om@13
    65
om@13
    66
class os_application:
om@13
    67
    """OpenSecurity '/application' handler.
om@13
    68
    
om@13
    69
    This is called on GET /application?vm=VM-ID&app=APP-ID
om@13
    70
    This tries to access the vm identified with the label VM-ID
om@13
    71
    and launched the application identified APP-ID
om@13
    72
    """
om@13
    73
    
om@13
    74
    def GET(self):
om@13
    75
        
om@13
    76
        # pick the arguments
om@13
    77
        args = web.input()
om@13
    78
        
om@13
    79
        # we _need_ a vm
om@13
    80
        if not "vm" in args:
om@13
    81
            raise web.badrequest()
om@13
    82
        
om@13
    83
        # we _need_ a app
om@13
    84
        if not "app" in args:
om@13
    85
            raise web.badrequest()
om@13
    86
        
om@13
    87
        apps = opensecurity_server.query_apps()
om@13
    88
        vms = opensecurity_server.query_vms()
om@13
    89
        
om@13
    90
        # check if we do have valid vm
om@13
    91
        v = [v for v in vms if v['name'] == args.vm]
om@13
    92
        if len(v) == 0:
om@13
    93
            raise web.notfound('vm not found')
om@13
    94
        v = v[0]
om@13
    95
        
om@13
    96
        # check if we do have a valid app
om@13
    97
        a = [a for a in apps if a['name'] == args.app]
om@13
    98
        if len(a) == 0:
om@13
    99
            raise web.notfound('app not found')
om@13
   100
        a = a[0]
om@13
   101
        
om@13
   102
        # invoke launch with 
om@13
   103
        res = "starting: launch " + v['user'] + " " + v['ip'] + " " + a['command']
om@13
   104
om@13
   105
        launch_image = os.path.join(sys.path[0], 'launch.py')
om@13
   106
        process_command = [sys.executable, launch_image, v['user'], v['ip'], a['command']]
om@13
   107
        process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
om@13
   108
        result = process.communicate()[0]
om@13
   109
        if process.returncode != 0:
om@13
   110
            return 'Launch of application aborted.'
om@13
   111
        
om@13
   112
        return result
om@13
   113
        
om@13
   114
om@13
   115
class os_credentials:
om@13
   116
    """OpenSecurity '/credentials' handler.
om@13
   117
    
om@13
   118
    This is called on GET /credentials?text=TEXT.
om@13
   119
    Ideally this should pop up a user dialog to insert his
om@13
   120
    credentials based the given TEXT.
om@13
   121
    """
om@13
   122
    
om@13
   123
    def GET(self):
om@13
   124
        
om@13
   125
        # pick the arguments
om@13
   126
        args = web.input()
om@13
   127
        
om@13
   128
        # we _need_ a device id
om@13
   129
        if not "text" in args:
om@13
   130
            raise web.badrequest()
om@13
   131
        
om@13
   132
        # invoke the user dialog as a subprocess
om@13
   133
        dlg_credentials_image = os.path.join(sys.path[0], 'opensecurity_dialog.py')
om@13
   134
        process_command = [sys.executable, dlg_credentials_image, 'credentials', args.text]
om@13
   135
        process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
om@13
   136
        result = process.communicate()[0]
om@13
   137
        if process.returncode != 0:
om@13
   138
            return 'Credentials request has been aborted.'
om@13
   139
        
om@13
   140
        return result
om@13
   141
om@13
   142
om@13
   143
class os_password:
om@13
   144
    """OpenSecurity '/password' handler.
om@13
   145
    
om@13
   146
    This is called on GET /password?text=TEXT.
om@13
   147
    Ideally this should pop up a user dialog to insert his
om@13
   148
    password based device name.
om@13
   149
    """
om@13
   150
    
om@13
   151
    def GET(self):
om@13
   152
        
om@13
   153
        # pick the arguments
om@13
   154
        args = web.input()
om@13
   155
        
om@13
   156
        # we _need_ a device id
om@13
   157
        if not "text" in args:
om@13
   158
            raise web.badrequest()
om@13
   159
            
om@13
   160
        # invoke the user dialog as a subprocess
om@13
   161
        dlg_credentials_image = os.path.join(sys.path[0], 'opensecurity_dialog.py')
om@13
   162
        process_command = [sys.executable, dlg_credentials_image, 'password', args.text]
om@13
   163
        process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
om@13
   164
        result = process.communicate()[0]
om@13
   165
        if process.returncode != 0:
om@13
   166
            return 'password request has been aborted.'
om@13
   167
        
om@13
   168
        return result
om@13
   169
om@13
   170
om@13
   171
class os_root:
om@13
   172
    """OpenSecurity '/' handler"""
om@13
   173
    
om@13
   174
    def GET(self):
om@13
   175
    
om@13
   176
        res = "OpenSecurity-Client RESTFul Server { \"version\": \"%s\" }" % __version__
om@13
   177
        
om@13
   178
        # add some sample links
om@13
   179
        res = res + """
om@13
   180
        
om@13
   181
USAGE EXAMPLES:
om@13
   182
        
om@13
   183
Request a password: 
om@13
   184
    (copy paste this into your browser's address field after the host:port)
om@13
   185
    
om@13
   186
    /password?text=Give+me+a+password+for+device+%22My+USB+Drive%22+(ID%3A+32090-AAA-X0)
om@13
   187
    
om@13
   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))
om@13
   189
    NOTE: check yout taskbar, the dialog window may not pop up in front of your browser window.
om@13
   190
    
om@13
   191
    
om@13
   192
Request a combination of user and password:
om@13
   193
    (copy paste this into your browser's address field after the host:port)
om@13
   194
    
om@13
   195
    /credentials?text=Tell+the+NSA+which+credentials+to+use+in+order+to+avoid+hacking+noise+on+wire.
om@13
   196
    
om@13
   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.)
om@13
   198
    NOTE: check yout taskbar, the dialog window may not pop up in front of your browser window.
om@13
   199
    
om@13
   200
om@13
   201
Start a Browser:
om@13
   202
    (copy paste this into your browser's address field after the host:port)
om@13
   203
om@13
   204
    /application?vm=Debian+7&app=Browser
om@13
   205
om@13
   206
    (e.g. http://127.0.0.1:8090/application?vm=Debian+7&app=Browser)
om@13
   207
        """
om@13
   208
    
om@13
   209
        return res
om@13
   210
om@13
   211
om@13
   212
# start
om@13
   213
if __name__ == "__main__":
om@13
   214
    server = web.application(opensecurity_urls, globals())
om@13
   215
    server.run()