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