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