python3 - 2to3 + miscell obvious tweaks
[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
28
29 from sfa.trust.certificate import Certificate
30 from sfa.trust.gid import GID
31
32 from sfa.util.py23 import xmlrpc_client
33
34 # Ticket is tuple:
35 #   (gidCaller, gidObject, attributes, rspec, delegate)
36 #
37 #    gidCaller = GID of the caller performing the operation
38 #    gidObject = GID of the slice
39 #    attributes = slice attributes (keys, vref, instantiation, etc)
40 #    rspec = resources
41
42
43 class SfaTicket(Certificate):
44     gidCaller = None
45     gidObject = None
46     attributes = {}
47     rspec = {}
48     delegate = False
49
50     def __init__(self, create=False, subject=None, string=None, filename=None):
51         Certificate.__init__(self, create, subject, string, filename)
52
53     def set_gid_caller(self, gid):
54         self.gidCaller = gid
55
56     def get_gid_caller(self):
57         if not self.gidCaller:
58             self.decode()
59         return self.gidCaller
60
61     def set_gid_object(self, gid):
62         self.gidObject = gid
63
64     def get_gid_object(self):
65         if not self.gidObject:
66             self.decode()
67         return self.gidObject
68
69     def set_attributes(self, gid):
70         self.attributes = gid
71
72     def get_attributes(self):
73         if not self.attributes:
74             self.decode()
75         return self.attributes
76
77     def set_rspec(self, gid):
78         self.rspec = gid
79
80     def get_rspec(self):
81         if not self.rspec:
82             self.decode()
83         return self.rspec
84
85     def set_delegate(self, delegate):
86         self.delegate = delegate
87
88     def get_delegate(self):
89         if not self.delegate:
90             self.decode()
91         return self.delegate
92
93     def encode(self):
94         dict = {"gidCaller": None,
95                 "gidObject": None,
96                 "attributes": self.attributes,
97                 "rspec": self.rspec,
98                 "delegate": self.delegate}
99         if self.gidCaller:
100             dict["gidCaller"] = self.gidCaller.save_to_string(
101                 save_parents=True)
102         if self.gidObject:
103             dict["gidObject"] = self.gidObject.save_to_string(
104                 save_parents=True)
105         str = "URI:" + xmlrpc_client.dumps((dict,), allow_none=True)
106         self.set_data(str)
107
108     def decode(self):
109         data = self.get_data()
110         if data:
111             dict = xmlrpc_client.loads(self.get_data()[4:])[0][0]
112         else:
113             dict = {}
114
115         self.attributes = dict.get("attributes", {})
116         self.rspec = dict.get("rspec", {})
117         self.delegate = dict.get("delegate", False)
118
119         gidCallerStr = dict.get("gidCaller", None)
120         if gidCallerStr:
121             self.gidCaller = GID(string=gidCallerStr)
122         else:
123             self.gidCaller = None
124
125         gidObjectStr = dict.get("gidObject", None)
126         if gidObjectStr:
127             self.gidObject = GID(string=gidObjectStr)
128         else:
129             self.gidObject = None
130
131     def dump(self, dump_parents=False):
132         print("TICKET", self.get_subject())
133
134         print("  gidCaller:")
135         gidCaller = self.get_gid_caller()
136         if gidCaller:
137             gidCaller.dump(8, dump_parents)
138
139         print("  gidObject:")
140         gidObject = self.get_gid_object()
141         if gidObject:
142             gidObject.dump(8, dump_parents)
143
144         print("  attributes:")
145         for attrname in list(self.get_attributes().keys()):
146             print("        ", attrname, self.get_attributes()[attrname])
147
148         print("       rspec:")
149         print("        ", self.get_rspec())
150
151         if self.parent and dump_parents:
152             print("PARENT", end=' ')
153             self.parent.dump(dump_parents)