om@3
|
1 |
#!/bin/env python
|
om@3
|
2 |
# -*- coding: utf-8 -*-
|
om@3
|
3 |
|
om@3
|
4 |
# ------------------------------------------------------------
|
om@3
|
5 |
# opensecurity_client_restful_server
|
om@3
|
6 |
#
|
om@3
|
7 |
# the OpenSecurity client 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 |
import opensecurity_server
|
om@3
|
44 |
|
om@3
|
45 |
|
om@3
|
46 |
# ------------------------------------------------------------
|
om@3
|
47 |
# const
|
om@3
|
48 |
|
om@3
|
49 |
|
om@3
|
50 |
__version__ = "0.1"
|
om@3
|
51 |
|
om@3
|
52 |
|
om@3
|
53 |
"""All the URLs we know mapping to class handler"""
|
om@3
|
54 |
opensecurity_urls = (
|
om@3
|
55 |
'/application', 'os_application',
|
om@3
|
56 |
'/credentials', 'os_credentials',
|
om@3
|
57 |
'/password', 'os_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 |
"""OpenSecurity '/application' handler.
|
om@3
|
68 |
|
om@3
|
69 |
This is called on GET /application?vm=VM-ID&app=APP-ID
|
om@3
|
70 |
This tries to access the vm identified with the label VM-ID
|
om@3
|
71 |
and launched the application identified APP-ID
|
om@3
|
72 |
"""
|
om@3
|
73 |
|
om@3
|
74 |
def GET(self):
|
om@3
|
75 |
|
om@3
|
76 |
# pick the arguments
|
om@3
|
77 |
args = web.input()
|
om@3
|
78 |
|
om@3
|
79 |
# we _need_ a vm
|
om@3
|
80 |
if not "vm" in args:
|
om@3
|
81 |
raise web.badrequest()
|
om@3
|
82 |
|
om@3
|
83 |
# we _need_ a app
|
om@3
|
84 |
if not "app" in args:
|
om@3
|
85 |
raise web.badrequest()
|
om@3
|
86 |
|
om@3
|
87 |
apps = opensecurity_server.query_apps()
|
om@3
|
88 |
vms = opensecurity_server.query_vms()
|
om@3
|
89 |
|
om@3
|
90 |
# check if we do have valid vm
|
om@3
|
91 |
v = [v for v in vms if v['name'] == args.vm]
|
om@3
|
92 |
if len(v) == 0:
|
om@3
|
93 |
raise web.notfound('vm not found')
|
om@3
|
94 |
v = v[0]
|
om@3
|
95 |
|
om@3
|
96 |
# check if we do have a valid app
|
om@3
|
97 |
a = [a for a in apps if a['name'] == args.app]
|
om@3
|
98 |
if len(a) == 0:
|
om@3
|
99 |
raise web.notfound('app not found')
|
om@3
|
100 |
a = a[0]
|
om@3
|
101 |
|
om@3
|
102 |
# invoke launch with
|
om@3
|
103 |
res = "starting: launch " + v['user'] + " " + v['ip'] + " " + a['command']
|
om@3
|
104 |
|
om@3
|
105 |
launch_image = os.path.join(sys.path[0], 'launch.py')
|
om@3
|
106 |
process_command = [sys.executable, launch_image, v['user'], v['ip'], a['command']]
|
om@3
|
107 |
process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
|
om@3
|
108 |
result = process.communicate()[0]
|
om@3
|
109 |
if process.returncode != 0:
|
om@3
|
110 |
return 'Launch of application aborted.'
|
om@3
|
111 |
|
om@3
|
112 |
return result
|
om@3
|
113 |
|
om@3
|
114 |
|
om@3
|
115 |
class os_credentials:
|
om@3
|
116 |
"""OpenSecurity '/credentials' handler.
|
om@3
|
117 |
|
om@3
|
118 |
This is called on GET /credentials?text=TEXT.
|
om@3
|
119 |
Ideally this should pop up a user dialog to insert his
|
om@3
|
120 |
credentials based the given TEXT.
|
om@3
|
121 |
"""
|
om@3
|
122 |
|
om@3
|
123 |
def GET(self):
|
om@3
|
124 |
|
om@3
|
125 |
# pick the arguments
|
om@3
|
126 |
args = web.input()
|
om@3
|
127 |
|
om@3
|
128 |
# we _need_ a device id
|
om@3
|
129 |
if not "text" in args:
|
om@3
|
130 |
raise web.badrequest()
|
om@3
|
131 |
|
om@3
|
132 |
# invoke the user dialog as a subprocess
|
om@3
|
133 |
dlg_credentials_image = os.path.join(sys.path[0], 'opensecurity_dialog.py')
|
om@3
|
134 |
process_command = [sys.executable, dlg_credentials_image, 'credentials', args.text]
|
om@3
|
135 |
process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
|
om@3
|
136 |
result = process.communicate()[0]
|
om@3
|
137 |
if process.returncode != 0:
|
om@3
|
138 |
return 'Credentials request has been aborted.'
|
om@3
|
139 |
|
om@3
|
140 |
return result
|
om@3
|
141 |
|
om@3
|
142 |
|
om@3
|
143 |
class os_password:
|
om@3
|
144 |
"""OpenSecurity '/password' handler.
|
om@3
|
145 |
|
om@3
|
146 |
This is called on GET /password?text=TEXT.
|
om@3
|
147 |
Ideally this should pop up a user dialog to insert his
|
om@3
|
148 |
password based device name.
|
om@3
|
149 |
"""
|
om@3
|
150 |
|
om@3
|
151 |
def GET(self):
|
om@3
|
152 |
|
om@3
|
153 |
# pick the arguments
|
om@3
|
154 |
args = web.input()
|
om@3
|
155 |
|
om@3
|
156 |
# we _need_ a device id
|
om@3
|
157 |
if not "text" in args:
|
om@3
|
158 |
raise web.badrequest()
|
om@3
|
159 |
|
om@3
|
160 |
# invoke the user dialog as a subprocess
|
om@3
|
161 |
dlg_credentials_image = os.path.join(sys.path[0], 'opensecurity_dialog.py')
|
om@3
|
162 |
process_command = [sys.executable, dlg_credentials_image, 'password', args.text]
|
om@3
|
163 |
process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
|
om@3
|
164 |
result = process.communicate()[0]
|
om@3
|
165 |
if process.returncode != 0:
|
om@3
|
166 |
return 'password request has been aborted.'
|
om@3
|
167 |
|
om@3
|
168 |
return result
|
om@3
|
169 |
|
om@3
|
170 |
|
om@3
|
171 |
class os_root:
|
om@3
|
172 |
"""OpenSecurity '/' handler"""
|
om@3
|
173 |
|
om@3
|
174 |
def GET(self):
|
om@3
|
175 |
|
om@3
|
176 |
res = "OpenSecurity-Client RESTFul Server { \"version\": \"%s\" }" % __version__
|
om@3
|
177 |
|
om@3
|
178 |
# add some sample links
|
om@3
|
179 |
res = res + """
|
om@3
|
180 |
|
om@3
|
181 |
USAGE EXAMPLES:
|
om@3
|
182 |
|
om@3
|
183 |
Request a password:
|
om@3
|
184 |
(copy paste this into your browser's address field after the host:port)
|
om@3
|
185 |
|
om@3
|
186 |
/password?text=Give+me+a+password+for+device+%22My+USB+Drive%22+(ID%3A+32090-AAA-X0)
|
om@3
|
187 |
|
om@3
|
188 |
(eg.: http://127.0.0.1:8090/password?text=Give+me+a+password+for+device+%22My+USB+Drive%22+(ID%3A+32090-AAA-X0))
|
om@3
|
189 |
NOTE: check yout taskbar, the dialog window may not pop up in front of your browser window.
|
om@3
|
190 |
|
om@3
|
191 |
|
om@3
|
192 |
Request a combination of user and password:
|
om@3
|
193 |
(copy paste this into your browser's address field after the host:port)
|
om@3
|
194 |
|
om@3
|
195 |
/credentials?text=Tell+the+NSA+which+credentials+to+use+in+order+to+avoid+hacking+noise+on+wire.
|
om@3
|
196 |
|
om@3
|
197 |
(eg.: http://127.0.0.1:8090/credentials?text=Tell+the+NSA+which+credentials+to+use+in+order+to+avoid+hacking+noise+on+wire.)
|
om@3
|
198 |
NOTE: check yout taskbar, the dialog window may not pop up in front of your browser window.
|
om@3
|
199 |
|
om@3
|
200 |
|
om@3
|
201 |
Start a Browser:
|
om@3
|
202 |
(copy paste this into your browser's address field after the host:port)
|
om@3
|
203 |
|
om@3
|
204 |
/application?vm=Debian+7&app=Browser
|
om@3
|
205 |
|
om@3
|
206 |
(e.g. http://127.0.0.1:8090/application?vm=Debian+7&app=Browser)
|
om@3
|
207 |
"""
|
om@3
|
208 |
|
om@3
|
209 |
return res
|
om@3
|
210 |
|
om@3
|
211 |
|
om@3
|
212 |
# start
|
om@3
|
213 |
if __name__ == "__main__":
|
om@3
|
214 |
server = web.application(opensecurity_urls, globals())
|
om@3
|
215 |
server.run()
|