1.1 --- a/OpenSecurity/bin/opensecurity_client_restful_server.py Tue Apr 29 13:00:46 2014 +0200
1.2 +++ b/OpenSecurity/bin/opensecurity_client_restful_server.py Wed Apr 30 12:06:22 2014 +0200
1.3 @@ -35,6 +35,7 @@
1.4 import json
1.5 import os
1.6 import os.path
1.7 +import socket
1.8 import subprocess
1.9 import sys
1.10 import urllib
1.11 @@ -65,6 +66,14 @@
1.12
1.13
1.14 # ------------------------------------------------------------
1.15 +# vars
1.16 +
1.17 +
1.18 +"""The REST server object"""
1.19 +server = None
1.20 +
1.21 +
1.22 +# ------------------------------------------------------------
1.23 # code
1.24
1.25
1.26 @@ -95,7 +104,7 @@
1.27 process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
1.28
1.29 # run process result handling in seprate thread (not to block main one)
1.30 - bouncer = UserResultBouncer(process, remote_ip, '/credentials')
1.31 + bouncer = ProcessResultBouncer(process, remote_ip, '/credentials')
1.32 bouncer.start()
1.33
1.34 return 'user queried for credentials'
1.35 @@ -128,7 +137,7 @@
1.36 process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
1.37
1.38 # run process result handling in seprate thread (not to block main one)
1.39 - bouncer = UserResultBouncer(process, remote_ip, '/keyfile')
1.40 + bouncer = ProcessResultBouncer(process, remote_ip, '/keyfile')
1.41 bouncer.start()
1.42
1.43 return 'user queried for password and keyfile'
1.44 @@ -193,7 +202,7 @@
1.45 process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
1.46
1.47 # run process result handling in seprate thread (not to block main one)
1.48 - bouncer = UserResultBouncer(process, remote_ip, '/password')
1.49 + bouncer = ProcessResultBouncer(process, remote_ip, '/password')
1.50 bouncer.start()
1.51
1.52 return 'user queried for password'
1.53 @@ -250,9 +259,9 @@
1.54 return res
1.55
1.56
1.57 -class UserResultBouncer(threading.Thread):
1.58 +class ProcessResultBouncer(threading.Thread):
1.59
1.60 - """A class to spawn off user I/O waiting and push the data back to the requesting services."""
1.61 + """A class to post the result of a given process - assuming it to be in JSON - to a REST Api."""
1.62
1.63 def __init__(self, process, remote_ip, resource):
1.64
1.65 @@ -299,8 +308,78 @@
1.66 return
1.67
1.68
1.69 -# start
1.70 -if __name__ == "__main__":
1.71 +class RESTServerThread(threading.Thread):
1.72 +
1.73 + """Thread for serving the REST API."""
1.74 +
1.75 + def __init__(self, port):
1.76 +
1.77 + """ctor"""
1.78 + threading.Thread.__init__(self)
1.79 + self._port = port
1.80 +
1.81 + def stop(self):
1.82 +
1.83 + """stop thread"""
1.84 + self.running = False
1.85 +
1.86 +
1.87 + def run(self):
1.88 +
1.89 + """run the thread"""
1.90 + _serve(self._port)
1.91 +
1.92 +
1.93 +
1.94 +def is_already_running(port = 8090):
1.95 +
1.96 + """check if this is started twice"""
1.97 +
1.98 + try:
1.99 + s = socket.create_connection(('127.0.0.1', port), 0.5)
1.100 + except:
1.101 + return False
1.102 +
1.103 + return True
1.104 +
1.105 +
1.106 +def _serve(port):
1.107 +
1.108 + """Start the REST server"""
1.109 +
1.110 + global server
1.111 +
1.112 + # trick the web.py server
1.113 + sys.argv = [__file__, str(port)]
1.114 server = web.application(opensecurity_urls, globals())
1.115 server.run()
1.116
1.117 +
1.118 +def serve(port = 8090, background = False):
1.119 +
1.120 + """Start serving the REST Api
1.121 + port ... port number to listen on
1.122 + background ... cease into background (spawn thread) and return immediately"""
1.123 +
1.124 + # start threaded or direct version
1.125 + if background == True:
1.126 + t = RESTServerThread(port)
1.127 + t.start()
1.128 + else:
1.129 + _serve(port)
1.130 +
1.131 +def stop():
1.132 +
1.133 + """Stop serving the REST Api"""
1.134 +
1.135 + global server
1.136 + if server is None:
1.137 + return
1.138 +
1.139 + server.stop()
1.140 +
1.141 +
1.142 +# start
1.143 +if __name__ == "__main__":
1.144 + serve()
1.145 +