OpenSecurity/bin/opensecurityd.pyw
author mb
Tue, 18 Feb 2014 14:38:24 +0100
changeset 66 d768c98d1e48
parent 63 c354ec779b61
child 74 a17c4cf8cd38
permissions -rw-r--r--
removed headless comment
mb@63
     1
#!/bin/env python
mb@63
     2
# -*- coding: utf-8 -*-
mb@63
     3
mb@63
     4
# ------------------------------------------------------------
mb@63
     5
# opensecurityd
mb@63
     6
# 
mb@63
     7
# the opensecurityd as RESTful server
mb@63
     8
#
mb@63
     9
# Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
mb@63
    10
#
mb@63
    11
# Copyright (C) 2013 AIT Austrian Institute of Technology
mb@63
    12
# AIT Austrian Institute of Technology GmbH
mb@63
    13
# Donau-City-Strasse 1 | 1220 Vienna | Austria
mb@63
    14
# http://www.ait.ac.at
mb@63
    15
#
mb@63
    16
# This program is free software; you can redistribute it and/or
mb@63
    17
# modify it under the terms of the GNU General Public License
mb@63
    18
# as published by the Free Software Foundation version 2.
mb@63
    19
# 
mb@63
    20
# This program is distributed in the hope that it will be useful,
mb@63
    21
# but WITHOUT ANY WARRANTY; without even the implied warranty of
mb@63
    22
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
mb@63
    23
# GNU General Public License for more details.
mb@63
    24
# 
mb@63
    25
# You should have received a copy of the GNU General Public License
mb@63
    26
# along with this program; if not, write to the Free Software
mb@63
    27
# Foundation, Inc., 51 Franklin Street, Fifth Floor, 
mb@63
    28
# Boston, MA  02110-1301, USA.
mb@63
    29
# ------------------------------------------------------------
mb@63
    30
mb@63
    31
mb@63
    32
# ------------------------------------------------------------
mb@63
    33
# imports
mb@63
    34
mb@63
    35
import os
mb@63
    36
import os.path
mb@63
    37
import subprocess
mb@63
    38
import sys
mb@63
    39
import web
mb@63
    40
from cygwin import Cygwin
mb@63
    41
mb@63
    42
from vmmanager import VMManager
mb@63
    43
mb@63
    44
# local
mb@63
    45
from environment import Environment
mb@63
    46
mb@63
    47
mb@63
    48
# ------------------------------------------------------------
mb@63
    49
# const
mb@63
    50
mb@63
    51
__version__ = "0.2"
mb@63
    52
mb@63
    53
mb@63
    54
"""All the URLs we know mapping to class handler"""
mb@63
    55
opensecurity_urls = (
mb@63
    56
    '/device_change',                   'os_device_change',     # http://localhost:8080/device_change                           GET
mb@63
    57
    '/sdvm_started',                    'os_sdvm_started',      # http://localhost:8080/sdvm_started                            GET
mb@63
    58
    '/browsing',                        'os_browsing',          # http://localhost:8080/browsing                                GET
mb@63
    59
    '/sdvms',                           'os_sdvms',             # http://localhost:8080/sdvms                                   GET, PUT
mb@63
    60
    '/sdvms/(.*)/application/(.*)',     'os_sdvm_application',  # http://localhost:8080/sdvms/[VMNAME]/application/[COMMAND]    GET
mb@63
    61
    '/sdvms/(.*)/ip',                   'os_sdvm_ip',           # http://localhost:8080/sdvms/[VMNAME]/ip                       GET
mb@63
    62
    '/sdvms/(.*)/start',                'os_sdvm_start',        # http://localhost:8080/sdvms/[VMNAME]/start                    GET
mb@63
    63
    '/sdvms/(.*)/stop',                 'os_sdvm_stop',         # http://localhost:8080/sdvms/[VMNAME]/stop                     GET
mb@63
    64
    '/sdvms/(.*)',                      'os_sdvm',              # http://localhost:8080/sdvms/[VMNAME]                          GET, DELETE
mb@63
    65
    '/vms',                             'os_vms',               # http://localhost:8080/vms                                     GET
mb@63
    66
    '/vms/(.*)',                        'os_vm',                # http://localhost:8080/vms/[VMNAME]                            GET
mb@63
    67
    '/',                                'os_root',              # http://localhost:8080/                                        GET
mb@63
    68
    '/update_template',                 'os_update_template'    # http://localhost:8080/update_template                         GET
mb@63
    69
)
mb@63
    70
mb@66
    71
# ------------------------------------------------------------
mb@63
    72
# vars
mb@63
    73
mb@63
    74
# Global VMManager instance
mb@63
    75
gvm_mgr = VMManager.getInstance()
mb@63
    76
mb@63
    77
# ------------------------------------------------------------
mb@63
    78
# code
mb@63
    79
mb@63
    80
mb@63
    81
class os_device_change:
mb@63
    82
    """OpenSecurity '/device_change' handler"""
mb@63
    83
    
mb@63
    84
    def GET(self):
mb@63
    85
        try:
mb@63
    86
            new_ip = gvm_mgr.handleDeviceChange()
mb@63
    87
            return new_ip
mb@63
    88
        except:
mb@63
    89
            raise web.internalerror()
mb@63
    90
mb@63
    91
        
mb@63
    92
class os_browsing:
mb@63
    93
    """OpenSecurity '/browsing' handler
mb@63
    94
    
mb@63
    95
    - GET: Start and prepare a new SecurityVM for Internet Browsing. Return the name of the VM.
mb@63
    96
    """
mb@63
    97
    
mb@63
    98
    def GET(self):
mb@63
    99
        try:
mb@63
   100
            browsingVM = gvm_mgr.handleBrowsingRequest()
mb@63
   101
            return browsingVM
mb@63
   102
        except:
mb@63
   103
            raise web.internalerror()
mb@63
   104
mb@63
   105
class os_sdvm_started:
mb@63
   106
    """OpenSecurity '/sdvm_started' handler"""
mb@63
   107
    
mb@63
   108
    def GET(self):
mb@63
   109
        remote_ip = web.ctx.environ['REMOTE_ADDR']
mb@63
   110
        gvm_mgr.putStartNotification(remote_ip)
mb@63
   111
        return "os_sdvm_started"
mb@63
   112
        
mb@63
   113
class os_sdvm:
mb@63
   114
    """OpenSecurity '/sdvms/[VM]' handler
mb@63
   115
    
mb@63
   116
    - GET: Information about a specific SecurityVM
mb@63
   117
    - DELETE: Remove a specific
mb@63
   118
    """
mb@63
   119
    
mb@63
   120
    def GET(self, name):
mb@63
   121
        return gvm_mgr.getVMInfo(name)
mb@63
   122
mb@63
   123
    def DELETE(self, name):
mb@63
   124
        return gvm_mgr.removeVM(name)
mb@63
   125
            
mb@63
   126
mb@63
   127
class os_sdvm_application:
mb@63
   128
    """OpenSecurity '/sdvms/[VM]/application/[CMD]' handler
mb@63
   129
    
mb@63
   130
    - GET: start application with given command in the VM.
mb@63
   131
    """
mb@63
   132
    
mb@63
   133
    def GET(self, name, command):
mb@63
   134
        command = '/' + command
mb@63
   135
        print('---> request to launch application in VM -- ' + name + ':' + command + ' <---')
mb@63
   136
        result = Cygwin.sshExecuteX11(command, gvm_mgr.getHostOnlyIP(name), 'osecuser', Cygwin.cygPath(gvm_mgr.getMachineFolder()) + '/' + name + '/dvm_key'  )
mb@63
   137
        self.poweroffVM(name)
mb@63
   138
        return gvm_mgr.removeVM(name)
mb@63
   139
    
mb@63
   140
mb@63
   141
class os_sdvm_ip:
mb@63
   142
    """OpenSecurity '/sdvms/[VM]/ip' handler
mb@63
   143
    
mb@63
   144
    - GET: give IP of SecurityVM.
mb@63
   145
    """
mb@63
   146
    
mb@63
   147
    def GET(self, name):
mb@63
   148
        return gvm_mgr.getHostOnlyIP(name)
mb@63
   149
            
mb@63
   150
mb@63
   151
class os_sdvm_start:
mb@63
   152
    """OpenSecurity '/sdvms/[VM]/start' handler
mb@63
   153
    
mb@63
   154
    - GET: Start specific SecuirtyVM.
mb@63
   155
    """
mb@63
   156
    
mb@63
   157
    def GET(self, name):
mb@63
   158
        return gvm_mgr.startVM(name)
mb@63
   159
            
mb@63
   160
mb@63
   161
class os_sdvm_stop:
mb@63
   162
    """OpenSecurity '/sdvms/[VM]/stop' handler
mb@63
   163
    
mb@63
   164
    - GET: stop specific Secuirty VM.
mb@63
   165
    """
mb@63
   166
    
mb@63
   167
    def GET(self, name):
mb@63
   168
        return gvm_mgr.stopVM(name)
mb@63
   169
            
mb@63
   170
mb@63
   171
class os_sdvms:
mb@63
   172
    """OpenSecurity '/sdvms' handler
mb@63
   173
    
mb@63
   174
    - GET: list all available secuirty VMs.
mb@63
   175
    - POST: create new security vm.
mb@63
   176
    """
mb@63
   177
    
mb@63
   178
    def GET(self):
mb@63
   179
        """get the list of SDVMs"""
mb@63
   180
        return gvm_mgr.listSDVM() 
mb@63
   181
            
mb@63
   182
    def POST(self):
mb@63
   183
        """create a new SDVM"""
mb@63
   184
mb@63
   185
        # get a new vm-name
mb@63
   186
        name = gvm_mgr.generateSDVMName()
mb@63
   187
        try:
mb@63
   188
            gvm_mgr.createVM(name)
mb@63
   189
        except:
mb@63
   190
            raise web.internalerror()
mb@63
   191
            
mb@63
   192
        return name
mb@63
   193
            
mb@63
   194
class os_vm:
mb@63
   195
    """OpenSecurity '/vms/[VM]' handler
mb@63
   196
    
mb@63
   197
    - GET: list information of arbitrary VM.
mb@63
   198
    """
mb@63
   199
    
mb@63
   200
    def GET(self, name):
mb@63
   201
        return gvm_mgr.getVMInfo(name)
mb@63
   202
            
mb@63
   203
mb@63
   204
class os_vms:
mb@63
   205
    """OpenSecurity '/vms' handler
mb@63
   206
    
mb@63
   207
    - GET: list all (also non Security) VMs.
mb@63
   208
    """
mb@63
   209
    
mb@63
   210
    def GET(self):
mb@63
   211
        return gvm_mgr.listVM() 
mb@63
   212
            
mb@63
   213
mb@63
   214
class os_root:
mb@63
   215
    """OpenSecurity '/' handler
mb@63
   216
    
mb@63
   217
    - GET: give information about current installation.
mb@63
   218
    """
mb@63
   219
    
mb@63
   220
    def GET(self):
mb@63
   221
        res = "'os_server': { "
mb@63
   222
        res += "'version': '" + __version__ + "', "
mb@63
   223
        res += "'machine_folder': '" + gvm_mgr.getDefaultMachineFolder() + "' "
mb@63
   224
        res += "}"
mb@63
   225
        return res
mb@63
   226
mb@63
   227
class os_update_template:
mb@63
   228
    """OpenSecurity '/update_template' handler
mb@63
   229
    
mb@63
   230
    - GET: update template vm
mb@63
   231
    """
mb@63
   232
    
mb@63
   233
    def GET(self):
mb@63
   234
        #return gvm_mgr.guestExecute('SecurityDVM', 'sudo apt-get -y update')
mb@63
   235
        return gvm_mgr.updateTemplate()
mb@63
   236
mb@63
   237
# start
mb@63
   238
if __name__ == "__main__":
mb@63
   239
    server = web.application(opensecurity_urls, globals())
mb@63
   240
    server.run()
mb@63
   241