propagate caller when a method calls another method
[plcapi.git] / PLC / Methods / GetSliceTicket.py
1 # $Id$
2 # $URL$
3
4 import time
5
6 from PLC.Faults import *
7 from PLC.Method import Method
8 from PLC.Parameter import Parameter, Mixed
9 from PLC.Slices import Slice, Slices
10 from PLC.Auth import Auth
11 from PLC.GPG import gpg_sign, gpg_verify
12 from PLC.InitScripts import InitScript, InitScripts
13
14 from PLC.Methods.GetSlivers import get_slivers
15
16 class GetSliceTicket(Method):
17     """
18     Returns a ticket for, or signed representation of, the specified
19     slice. Slice tickets may be used to manually instantiate or update
20     a slice on a node. Present this ticket to the local Node Manager
21     interface to redeem it.
22
23     If the slice has not been added to a node with AddSliceToNodes,
24     and the ticket is redeemed on that node, it will be deleted the
25     next time the Node Manager contacts the API.
26
27     Users may only obtain tickets for slices of which they are
28     members. PIs may obtain tickets for any of the slices at their
29     sites, or any slices of which they are members. Admins may obtain
30     tickets for any slice.
31
32     Returns 1 if successful, faults otherwise.
33     """
34
35     roles = ['admin', 'pi', 'user', 'peer']
36
37     accepts = [
38         Auth(),
39         Mixed(Slice.fields['slice_id'],
40               Slice.fields['name']),
41         ]
42
43     returns = Parameter(str, 'Signed slice ticket')
44
45     def call(self, auth, slice_id_or_name):
46         slices = Slices(self.api, [slice_id_or_name])
47         if not slices:
48             raise PLCInvalidArgument, "No such slice"
49         slice = slices[0]
50
51         # Allow peers to obtain tickets for their own slices
52         if slice['peer_id'] is not None:
53             if not isinstance(self.caller, Peer):
54                 raise PLCInvalidArgument, "Not a local slice"
55             elif slice['peer_id'] != self.caller['peer_id']:
56                 raise PLCInvalidArgument, "Only the authoritative peer may obtain tickets for that slice"
57
58         # Tickets are the canonicalized XML-RPC methodResponse
59         # representation of a partial GetSlivers() response, i.e.,
60
61         initscripts = InitScripts(self.api, {'enabled': True})
62
63         data = {
64             'timestamp': int(time.time()),
65             'initscripts': initscripts,
66             'slivers': get_slivers(self.api, self.caller, auth, [slice['slice_id']]),
67             }
68
69         # Sign ticket
70         signed_ticket = gpg_sign((data,),
71                                  self.api.config.PLC_ROOT_GPG_KEY,
72                                  self.api.config.PLC_ROOT_GPG_KEY_PUB,
73                                  methodresponse = True,
74                                  detach_sign = False)
75
76         # Verify ticket
77         gpg_verify(signed_ticket,
78                    self.api.config.PLC_ROOT_GPG_KEY_PUB)
79
80         return signed_ticket