deeper pass on xmlrpclib vs xmlrpc.client as well as configparser
[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 from __future__ import print_function
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 class SfaTicket(Certificate):
43     gidCaller = None
44     gidObject = None
45     attributes = {}
46     rspec = {}
47     delegate = False
48
49     def __init__(self, create=False, subject=None, string=None, filename=None):
50         Certificate.__init__(self, create, subject, string, filename)
51
52     def set_gid_caller(self, gid):
53         self.gidCaller = gid
54
55     def get_gid_caller(self):
56         if not self.gidCaller:
57             self.decode()
58         return self.gidCaller
59
60     def set_gid_object(self, gid):
61         self.gidObject = gid
62
63     def get_gid_object(self):
64         if not self.gidObject:
65             self.decode()
66         return self.gidObject
67
68     def set_attributes(self, gid):
69         self.attributes = gid
70
71     def get_attributes(self):
72         if not self.attributes:
73             self.decode()
74         return self.attributes
75
76     def set_rspec(self, gid):
77         self.rspec = gid
78
79     def get_rspec(self):
80         if not self.rspec:
81             self.decode()
82         return self.rspec
83
84     def set_delegate(self, delegate):
85         self.delegate = delegate
86
87     def get_delegate(self):
88         if not self.delegate:
89             self.decode()
90         return self.delegate
91
92     def encode(self):
93         dict = {"gidCaller": None,
94                 "gidObject": None,
95                 "attributes": self.attributes,
96                 "rspec": self.rspec,
97                 "delegate": self.delegate}
98         if self.gidCaller:
99             dict["gidCaller"] = self.gidCaller.save_to_string(save_parents=True)
100         if self.gidObject:
101             dict["gidObject"] = self.gidObject.save_to_string(save_parents=True)
102         str = "URI:" + xmlrpc_client.dumps((dict,), allow_none=True)
103         self.set_data(str)
104
105     def decode(self):
106         data = self.get_data()
107         if data:
108             dict = xmlrpc_client.loads(self.get_data()[4:])[0][0]
109         else:
110             dict = {}
111
112         self.attributes = dict.get("attributes", {})
113         self.rspec = dict.get("rspec", {})
114         self.delegate = dict.get("delegate", False)
115
116         gidCallerStr = dict.get("gidCaller", None)
117         if gidCallerStr:
118             self.gidCaller = GID(string=gidCallerStr)
119         else:
120             self.gidCaller = None
121
122         gidObjectStr = dict.get("gidObject", None)
123         if gidObjectStr:
124             self.gidObject = GID(string=gidObjectStr)
125         else:
126             self.gidObject = None
127
128     def dump(self, dump_parents=False):
129         print("TICKET", self.get_subject())
130
131         print("  gidCaller:")
132         gidCaller = self.get_gid_caller()
133         if gidCaller:
134             gidCaller.dump(8, dump_parents)
135
136         print("  gidObject:")
137         gidObject = self.get_gid_object()
138         if gidObject:
139             gidObject.dump(8, dump_parents)
140
141         print("  attributes:")
142         for attrname in self.get_attributes().keys():
143             print("        ", attrname, self.get_attributes()[attrname])
144
145         print("       rspec:")
146         print("        ", self.get_rspec())
147
148         if self.parent and dump_parents:
149            print("PARENT", end=' ')
150            self.parent.dump(dump_parents)