OpenSecurity/bin/opensecurityd.py
author om
Tue, 10 Dec 2013 14:04:11 +0100
changeset 31 d95fe93d7a83
parent 30 0d5637405430
child 34 fe5fb244f008
permissions -rw-r--r--
opensecurityd can now invoke applications on vm
om@13
     1
#!/bin/env python
om@13
     2
# -*- coding: utf-8 -*-
om@13
     3
om@13
     4
# ------------------------------------------------------------
om@13
     5
# opensecurityd
om@13
     6
# 
om@13
     7
# the opensecurityd as 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@22
    40
om@22
    41
from vmmanager import VMManager
om@13
    42
om@13
    43
# local
om@13
    44
from environment import Environment
om@13
    45
om@13
    46
om@13
    47
# ------------------------------------------------------------
om@13
    48
# const
om@13
    49
om@31
    50
__version__ = "0.2"
om@13
    51
om@13
    52
om@13
    53
"""All the URLs we know mapping to class handler"""
om@13
    54
opensecurity_urls = (
om@31
    55
    '/device_change',                   'os_device_change',     # http://localhost:8080/device_change                           GET
om@31
    56
    '/browsing',                        'os_browsing',          # http://localhost:8080/browsing                                GET
om@31
    57
    '/sdvms',                           'os_sdvms',             # http://localhost:8080/sdvms                                   GET, PUT
om@31
    58
    '/sdvms/(.*)/application/(.*)',     'os_sdvm_application',  # http://localhost:8080/sdvms/[VMNAME]/application/[COMMAND]    GET
om@31
    59
    '/sdvms/(.*)/ip',                   'os_sdvm_ip',           # http://localhost:8080/sdvms/[VMNAME]/ip                       GET
om@31
    60
    '/sdvms/(.*)/start',                'os_sdvm_start',        # http://localhost:8080/sdvms/[VMNAME]/start                    GET
om@31
    61
    '/sdvms/(.*)/stop',                 'os_sdvm_stop',         # http://localhost:8080/sdvms/[VMNAME]/stop                     GET
om@31
    62
    '/sdvms/(.*)',                      'os_sdvm',              # http://localhost:8080/sdvms/[VMNAME]                          GET, DELETE
om@31
    63
    '/vms',                             'os_vms',               # http://localhost:8080/vms                                     GET
om@31
    64
    '/vms/(.*)',                        'os_vm',                # http://localhost:8080/vms/[VMNAME]                            GET
om@31
    65
    '/',                                'os_root'               # http://localhost:8080/                                        GET
om@13
    66
)
om@13
    67
om@13
    68
om@13
    69
# ------------------------------------------------------------
om@13
    70
# vars
om@13
    71
om@13
    72
# Global VMManager instance
om@13
    73
gvm_mgr = VMManager()
om@13
    74
om@13
    75
om@13
    76
# ------------------------------------------------------------
om@13
    77
# code
om@13
    78
om@13
    79
om@13
    80
class os_device_change:
om@13
    81
    """OpenSecurity '/device_change' handler"""
om@13
    82
    
om@13
    83
    def GET(self):
om@17
    84
        gvm_mgr.handleDeviceChange()
om@13
    85
        return "os_device_change"
om@13
    86
om@31
    87
        
om@31
    88
class os_browsing:
om@31
    89
    """OpenSecurity '/browsing' handler
om@31
    90
    
om@31
    91
    - GET: Start and prepare a new SecurityVM for Internet Browsing. Return the name of the VM.
om@31
    92
    """
mb@27
    93
    
mb@27
    94
    def GET(self):
om@31
    95
        try:
om@31
    96
            browsingVM = gvm_mgr.handleBrowsingRequest()
om@31
    97
            gvm_mgr.startVM(browsingVM)
om@31
    98
            return browsingVM
om@31
    99
        except:
om@31
   100
            raise web.internalerror()
om@13
   101
om@31
   102
        
om@22
   103
class os_sdvm:
om@31
   104
    """OpenSecurity '/sdvms/[VM]' handler
om@31
   105
    
om@31
   106
    - GET: Information about a specific SecurityVM
om@31
   107
    - DELETE: Remove a specific
om@31
   108
    """
om@22
   109
    
om@22
   110
    def GET(self, name):
om@22
   111
        return gvm_mgr.getVMInfo(name)
om@22
   112
om@22
   113
    def DELETE(self, name):
om@22
   114
        return gvm_mgr.removeVM(name)
om@22
   115
            
om@22
   116
om@31
   117
class os_sdvm_application:
om@31
   118
    """OpenSecurity '/sdvms/[VM]/application/[CMD]' handler
om@31
   119
    
om@31
   120
    - GET: start application with given command in the VM.
om@31
   121
    """
om@31
   122
    
om@31
   123
    def GET(self, name, command):
om@31
   124
        command = '/' + command
om@31
   125
        print('---> request to launch application in VM -- ' + name + ':' + command + ' <---')
om@31
   126
        return gvm_mgr.sshGuestX11Execute(name, command)
om@31
   127
            
om@31
   128
om@30
   129
class os_sdvm_ip:
om@31
   130
    """OpenSecurity '/sdvms/[VM]/ip' handler
om@31
   131
    
om@31
   132
    - GET: give IP of SecurityVM.
om@31
   133
    """
om@30
   134
    
om@30
   135
    def GET(self, name):
om@30
   136
        return gvm_mgr.getHostOnlyIP(name)
om@30
   137
            
om@30
   138
om@30
   139
class os_sdvm_start:
om@31
   140
    """OpenSecurity '/sdvms/[VM]/start' handler
om@31
   141
    
om@31
   142
    - GET: Start specific SecuirtyVM.
om@31
   143
    """
om@30
   144
    
om@30
   145
    def GET(self, name):
om@30
   146
        return gvm_mgr.startVM(name)
om@30
   147
            
om@30
   148
om@30
   149
class os_sdvm_stop:
om@31
   150
    """OpenSecurity '/sdvms/[VM]/stop' handler
om@31
   151
    
om@31
   152
    - GET: stop specific Secuirty VM.
om@31
   153
    """
om@30
   154
    
om@30
   155
    def GET(self, name):
om@30
   156
        return gvm_mgr.stopVM(name)
om@30
   157
            
om@30
   158
om@13
   159
class os_sdvms:
om@31
   160
    """OpenSecurity '/sdvms' handler
om@31
   161
    
om@31
   162
    - GET: list all available secuirty VMs.
om@31
   163
    - POST: create new security vm.
om@31
   164
    """
om@13
   165
    
om@13
   166
    def GET(self):
om@17
   167
        """get the list of SDVMs"""
om@13
   168
        return gvm_mgr.listSDVM() 
om@13
   169
            
om@31
   170
    def POST(self):
om@17
   171
        """create a new SDVM"""
om@22
   172
om@30
   173
        # get a new vm-name
om@30
   174
        name = gvm_mgr.generateSDVMName()
om@30
   175
        try:
om@30
   176
            gvm_mgr.createVM(name)
om@30
   177
        except:
om@30
   178
            raise web.internalerror()
om@17
   179
            
om@30
   180
        return name
om@22
   181
            
om@13
   182
class os_vm:
om@31
   183
    """OpenSecurity '/vms/[VM]' handler
om@31
   184
    
om@31
   185
    - GET: list information of arbitrary VM.
om@31
   186
    """
om@13
   187
    
om@13
   188
    def GET(self, name):
om@13
   189
        return gvm_mgr.getVMInfo(name)
om@13
   190
            
om@13
   191
om@13
   192
class os_vms:
om@31
   193
    """OpenSecurity '/vms' handler
om@31
   194
    
om@31
   195
    - GET: list all (also non Security) VMs.
om@31
   196
    """
om@13
   197
    
om@13
   198
    def GET(self):
om@13
   199
        return gvm_mgr.listVM() 
om@13
   200
            
om@13
   201
om@13
   202
class os_root:
om@31
   203
    """OpenSecurity '/' handler
om@31
   204
    
om@31
   205
    - GET: give information about current installation.
om@31
   206
    """
om@13
   207
    
om@13
   208
    def GET(self):
om@13
   209
        res = "'os_server': { "
om@13
   210
        res += "'version': '" + __version__ + "', "
om@13
   211
        res += "'machine_folder': '" + gvm_mgr.getDefaultMachineFolder() + "' "
om@13
   212
        res += "}"
om@13
   213
        return res
om@13
   214
om@13
   215
om@13
   216
# start
om@13
   217
if __name__ == "__main__":
om@13
   218
    server = web.application(opensecurity_urls, globals())
om@13
   219
    server.run()
om@13
   220