fix coresched locating cgroup and reduce verbosity
[nodemanager.git] / ticket.py
1 """An extremely simple interface to the signing/verifying capabilities
2 of gnupg.
3
4 You must already have the key in the keyring.
5 """
6
7 from subprocess import PIPE, Popen
8 from xmlrpclib import dumps, loads
9
10 GPG = '/usr/bin/gpg'
11
12 def _popen_gpg(*args):
13     """Return a Popen object to GPG."""
14     return Popen((GPG, '--batch', '--no-tty') + args,
15                  stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True)
16
17 def sign(data):
18     """Return <data> signed with the default GPG key."""
19     msg = dumps((data,), methodresponse = True)
20     p = _popen_gpg('--armor', '--sign', '--keyring', '/etc/planetlab/secring.gpg', '--no-default-keyring')
21     p.stdin.write(msg)
22     p.stdin.close()
23     signed_msg = p.stdout.read()
24     p.stdout.close()
25     p.stderr.close()
26     p.wait()
27     return signed_msg
28
29 def verify(signed_msg):
30     """If <signed_msg> is a valid signed document, return its contents.  Otherwise, return None."""
31     p = _popen_gpg('--decrypt', '--keyring', '/usr/boot/pubring.gpg', '--no-default-keyring')
32     p.stdin.write(signed_msg)
33     p.stdin.close()
34     msg = p.stdout.read()
35     p.stdout.close()
36     p.stderr.close()
37     if p.wait():
38         return None  # verification failed
39     else:
40         data, = loads(msg)[0]
41         return data