OpenSecurity/bin/opensecurityd.py
author mb
Tue, 10 Dec 2013 14:56:11 +0100
changeset 35 ba1ca3e5870b
parent 34 fe5fb244f008
parent 33 79ed9495fa88
child 40 b44a603b0b95
permissions -rw-r--r--
isingleton vmmaneger. get with VMManager.getInstance()
/sdvm_started endpoint isSDVMStarted() methon in VMManager
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
mb@34
    56
    '/sdvm_started',                    'os_sdvm_started',      # http://localhost:8080/sdvm_started                            GET
om@31
    57
    '/browsing',                        'os_browsing',          # http://localhost:8080/browsing                                GET
om@31
    58
    '/sdvms',                           'os_sdvms',             # http://localhost:8080/sdvms                                   GET, PUT
om@31
    59
    '/sdvms/(.*)/application/(.*)',     'os_sdvm_application',  # http://localhost:8080/sdvms/[VMNAME]/application/[COMMAND]    GET
om@31
    60
    '/sdvms/(.*)/ip',                   'os_sdvm_ip',           # http://localhost:8080/sdvms/[VMNAME]/ip                       GET
om@31
    61
    '/sdvms/(.*)/start',                'os_sdvm_start',        # http://localhost:8080/sdvms/[VMNAME]/start                    GET
om@31
    62
    '/sdvms/(.*)/stop',                 'os_sdvm_stop',         # http://localhost:8080/sdvms/[VMNAME]/stop                     GET
om@31
    63
    '/sdvms/(.*)',                      'os_sdvm',              # http://localhost:8080/sdvms/[VMNAME]                          GET, DELETE
om@31
    64
    '/vms',                             'os_vms',               # http://localhost:8080/vms                                     GET
om@31
    65
    '/vms/(.*)',                        'os_vm',                # http://localhost:8080/vms/[VMNAME]                            GET
om@31
    66
    '/',                                'os_root'               # http://localhost:8080/                                        GET
om@13
    67
)
om@13
    68
om@13
    69
om@13
    70
# ------------------------------------------------------------
om@13
    71
# vars
om@13
    72
om@13
    73
# Global VMManager instance
mb@33
    74
gvm_mgr = VMManager.getInstance()
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()
mb@27
   101
mb@27
   102
class os_sdvm_started:
mb@27
   103
    """OpenSecurity '/sdvm_started' handler"""
mb@27
   104
    
mb@27
   105
    def GET(self):
mb@34
   106
        remote_ip = web.ctx.environ['REMOTE_ADDR']
mb@34
   107
        gvm_mgr.putStartNotification(remote_ip)
mb@27
   108
        return "os_sdvm_started"
om@31
   109
        
om@22
   110
class os_sdvm:
om@31
   111
    """OpenSecurity '/sdvms/[VM]' handler
om@31
   112
    
om@31
   113
    - GET: Information about a specific SecurityVM
om@31
   114
    - DELETE: Remove a specific
om@31
   115
    """
om@22
   116
    
om@22
   117
    def GET(self, name):
om@22
   118
        return gvm_mgr.getVMInfo(name)
om@22
   119
om@22
   120
    def DELETE(self, name):
om@22
   121
        return gvm_mgr.removeVM(name)
om@22
   122
            
om@22
   123
om@31
   124
class os_sdvm_application:
om@31
   125
    """OpenSecurity '/sdvms/[VM]/application/[CMD]' handler
om@31
   126
    
om@31
   127
    - GET: start application with given command in the VM.
om@31
   128
    """
om@31
   129
    
om@31
   130
    def GET(self, name, command):
om@31
   131
        command = '/' + command
om@31
   132
        print('---> request to launch application in VM -- ' + name + ':' + command + ' <---')
om@31
   133
        return gvm_mgr.sshGuestX11Execute(name, command)
om@31
   134
            
om@31
   135
om@30
   136
class os_sdvm_ip:
om@31
   137
    """OpenSecurity '/sdvms/[VM]/ip' handler
om@31
   138
    
om@31
   139
    - GET: give IP of SecurityVM.
om@31
   140
    """
om@30
   141
    
om@30
   142
    def GET(self, name):
om@30
   143
        return gvm_mgr.getHostOnlyIP(name)
om@30
   144
            
om@30
   145
om@30
   146
class os_sdvm_start:
om@31
   147
    """OpenSecurity '/sdvms/[VM]/start' handler
om@31
   148
    
om@31
   149
    - GET: Start specific SecuirtyVM.
om@31
   150
    """
om@30
   151
    
om@30
   152
    def GET(self, name):
om@30
   153
        return gvm_mgr.startVM(name)
om@30
   154
            
om@30
   155
om@30
   156
class os_sdvm_stop:
om@31
   157
    """OpenSecurity '/sdvms/[VM]/stop' handler
om@31
   158
    
om@31
   159
    - GET: stop specific Secuirty VM.
om@31
   160
    """
om@30
   161
    
om@30
   162
    def GET(self, name):
om@30
   163
        return gvm_mgr.stopVM(name)
om@30
   164
            
om@30
   165
om@13
   166
class os_sdvms:
om@31
   167
    """OpenSecurity '/sdvms' handler
om@31
   168
    
om@31
   169
    - GET: list all available secuirty VMs.
om@31
   170
    - POST: create new security vm.
om@31
   171
    """
om@13
   172
    
om@13
   173
    def GET(self):
om@17
   174
        """get the list of SDVMs"""
om@13
   175
        return gvm_mgr.listSDVM() 
om@13
   176
            
om@31
   177
    def POST(self):
om@17
   178
        """create a new SDVM"""
om@22
   179
om@30
   180
        # get a new vm-name
om@30
   181
        name = gvm_mgr.generateSDVMName()
om@30
   182
        try:
om@30
   183
            gvm_mgr.createVM(name)
om@30
   184
        except:
om@30
   185
            raise web.internalerror()
om@17
   186
            
om@30
   187
        return name
om@22
   188
            
om@13
   189
class os_vm:
om@31
   190
    """OpenSecurity '/vms/[VM]' handler
om@31
   191
    
om@31
   192
    - GET: list information of arbitrary VM.
om@31
   193
    """
om@13
   194
    
om@13
   195
    def GET(self, name):
om@13
   196
        return gvm_mgr.getVMInfo(name)
om@13
   197
            
om@13
   198
om@13
   199
class os_vms:
om@31
   200
    """OpenSecurity '/vms' handler
om@31
   201
    
om@31
   202
    - GET: list all (also non Security) VMs.
om@31
   203
    """
om@13
   204
    
om@13
   205
    def GET(self):
om@13
   206
        return gvm_mgr.listVM() 
om@13
   207
            
om@13
   208
om@13
   209
class os_root:
om@31
   210
    """OpenSecurity '/' handler
om@31
   211
    
om@31
   212
    - GET: give information about current installation.
om@31
   213
    """
om@13
   214
    
om@13
   215
    def GET(self):
om@13
   216
        res = "'os_server': { "
om@13
   217
        res += "'version': '" + __version__ + "', "
om@13
   218
        res += "'machine_folder': '" + gvm_mgr.getDefaultMachineFolder() + "' "
om@13
   219
        res += "}"
om@13
   220
        return res
om@13
   221
om@13
   222
om@13
   223
# start
om@13
   224
if __name__ == "__main__":
om@13
   225
    server = web.application(opensecurity_urls, globals())
om@13
   226
    server.run()
om@13
   227