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