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