First version. Most definitely a work in progress.
[nodemanager.git] / ticket.py
1 import SocketServer
2 import os
3 import subprocess
4
5 from config import KEY_FILE, TICKET_SERVER_PORT
6 import tools
7
8
9 class TicketServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
10     allow_reuse_address = True
11
12
13 class TicketRequestHandler(SocketServer.StreamRequestHandler):
14     def handle(self):
15         data = self.rfile.read()
16         filename = tools.write_temp_file(lambda thefile:
17                                          thefile.write(TEMPLATE % data))
18         result = subprocess.Popen([XMLSEC1, '--sign',
19                                    '--privkey-pem', KEY_FILE, filename],
20                                   stdout=subprocess.PIPE).stdout
21         self.wfile.write(result.read())
22         result.close()
23 #         os.unlink(filename)
24
25
26 def start():
27     tools.as_daemon_thread(TicketServer(('', TICKET_SERVER_PORT),
28                                         TicketRequestHandler).serve_forever)
29
30
31 XMLSEC1 = '/usr/bin/xmlsec1'
32
33 TEMPLATE = '''<?xml version="1.0" encoding="UTF-8"?>
34 <Envelope xmlns="urn:envelope">
35   <Data>%s</Data>
36   <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
37     <SignedInfo>
38       <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
39       <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
40       <Reference URI="">
41         <Transforms>
42           <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
43         </Transforms>
44         <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
45         <DigestValue></DigestValue>
46       </Reference>
47     </SignedInfo>
48     <SignatureValue/>
49     <KeyInfo>
50         <KeyName/>
51     </KeyInfo>
52   </Signature>
53 </Envelope>
54 '''
55