bc8584b422ec0d94bacdcb4ccac985fcc04335bd
[sfa.git] / sfa / trust / sfaticket.py
1 #----------------------------------------------------------------------
2 # Copyright (c) 2008 Board of Trustees, Princeton University
3 #
4 # Permission is hereby granted, free of charge, to any person obtaining
5 # a copy of this software and/or hardware specification (the "Work") to
6 # deal in the Work without restriction, including without limitation the
7 # rights to use, copy, modify, merge, publish, distribute, sublicense,
8 # and/or sell copies of the Work, and to permit persons to whom the Work
9 # is furnished to do so, subject to the following conditions:
10 #
11 # The above copyright notice and this permission notice shall be
12 # included in all copies or substantial portions of the Work.
13 #
14 # THE WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
18 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE WORK OR THE USE OR OTHER DEALINGS
21 # IN THE WORK.
22 #----------------------------------------------------------------------
23 #
24 # implements SFA tickets
25 #
26
27 import xmlrpclib
28
29 from sfa.trust.certificate import Certificate
30 from sfa.trust.gid import GID
31
32 # Ticket is tuple:
33 #   (gidCaller, gidObject, attributes, rspec, delegate)
34 #
35 #    gidCaller = GID of the caller performing the operation
36 #    gidObject = GID of the slice
37 #    attributes = slice attributes (keys, vref, instantiation, etc)
38 #    rspec = resources
39
40 class SfaTicket(Certificate):
41     gidCaller = None
42     gidObject = None
43     attributes = {}
44     rspec = {}
45     delegate = False
46
47     def __init__(self, create=False, subject=None, string=None, filename=None):
48         Certificate.__init__(self, create, subject, string, filename)
49
50     def set_gid_caller(self, gid):
51         self.gidCaller = gid
52
53     def get_gid_caller(self):
54         if not self.gidCaller:
55             self.decode()
56         return self.gidCaller
57
58     def set_gid_object(self, gid):
59         self.gidObject = gid
60
61     def get_gid_object(self):
62         if not self.gidObject:
63             self.decode()
64         return self.gidObject
65
66     def set_attributes(self, gid):
67         self.attributes = gid
68
69     def get_attributes(self):
70         if not self.attributes:
71             self.decode()
72         return self.attributes
73
74     def set_rspec(self, gid):
75         self.rspec = gid
76
77     def get_rspec(self):
78         if not self.rspec:
79             self.decode()
80         return self.rspec
81
82     def set_delegate(self, delegate):
83         self.delegate = delegate
84
85     def get_delegate(self):
86         if not self.delegate:
87             self.decode()
88         return self.delegate
89
90     def encode(self):
91         dict = {"gidCaller": None,
92                 "gidObject": None,
93                 "attributes": self.attributes,
94                 "rspec": self.rspec,
95                 "delegate": self.delegate}
96         if self.gidCaller:
97             dict["gidCaller"] = self.gidCaller.save_to_string(save_parents=True)
98         if self.gidObject:
99             dict["gidObject"] = self.gidObject.save_to_string(save_parents=True)
100         str = "URI:" + xmlrpclib.dumps((dict,), allow_none=True)
101         self.set_data(str)
102
103     def decode(self):
104         data = self.get_data()
105         if data:
106             dict = xmlrpclib.loads(self.get_data()[4:])[0][0]
107         else:
108             dict = {}
109
110         self.attributes = dict.get("attributes", {})
111         self.rspec = dict.get("rspec", {})
112         self.delegate = dict.get("delegate", False)
113
114         gidCallerStr = dict.get("gidCaller", None)
115         if gidCallerStr:
116             self.gidCaller = GID(string=gidCallerStr)
117         else:
118             self.gidCaller = None
119
120         gidObjectStr = dict.get("gidObject", None)
121         if gidObjectStr:
122             self.gidObject = GID(string=gidObjectStr)
123         else:
124             self.gidObject = None
125
126     def dump(self, dump_parents=False):
127         print "TICKET", self.get_subject()
128
129         print "  gidCaller:"
130         gidCaller = self.get_gid_caller()
131         if gidCaller:
132             gidCaller.dump(8, dump_parents)
133
134         print "  gidObject:"
135         gidObject = self.get_gid_object()
136         if gidObject:
137             gidObject.dump(8, dump_parents)
138
139         print "  attributes:"
140         for attrname in self.get_attributes().keys():
141             print "        ", attrname, self.get_attributes()[attrname]
142
143         print "       rspec:"
144         print "        ", self.get_rspec()
145
146         if self.parent and dump_parents:
147            print "PARENT",
148            self.parent.dump(dump_parents)