a little nicer wrt pep8
[sfa.git] / sfa / trust / rights.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 # This Module implements rights and lists of rights for the SFA. Rights
25 # are implemented by two classes:
26 #
27 # Right - represents a single right
28 #
29 # Rights - represents a list of rights
30 #
31 # A right may allow several different operations. For example, the "info" right
32 # allows "listslices", "listcomponentresources", etc.
33 ##
34
35
36 ##
37 # privilege_table is a list of priviliges and what operations are allowed
38 # per privilege.
39 # Note that "*" is a privilege granted by ProtoGENI slice authorities, and we
40 # give it access to the GENI AM calls
41
42 privilege_table = {"authority": ["register", "remove", "update", "resolve", "list", "getcredential", "*"],
43                    "refresh": ["remove", "update"],
44                    "resolve": ["resolve", "list", "getcredential"],
45                    "sa": ["getticket", "redeemslice", "redeemticket", "createslice", "createsliver", "deleteslice", "deletesliver", "updateslice",
46                           "getsliceresources", "getticket", "loanresources", "stopslice", "startslice", "renewsliver",
47                           "deleteslice", "deletesliver", "resetslice", "listslices", "listnodes", "getpolicy", "sliverstatus"],
48                    "embed": ["getticket", "redeemslice", "redeemticket", "createslice", "createsliver", "renewsliver", "deleteslice",
49                              "deletesliver", "updateslice", "sliverstatus", "getsliceresources", "shutdown"],
50                    "bind": ["getticket", "loanresources", "redeemticket"],
51                    "control": ["updateslice", "createslice", "createsliver", "renewsliver", "sliverstatus", "stopslice", "startslice",
52                                "deleteslice", "deletesliver", "resetslice", "getsliceresources", "getgids"],
53                    "info": ["listslices", "listnodes", "getpolicy"],
54                    "ma": ["setbootstate", "getbootstate", "reboot", "getgids", "gettrustedcerts"],
55                    "operator": ["gettrustedcerts", "getgids"],
56                    "*": ["createsliver", "deletesliver", "sliverstatus", "renewsliver", "shutdown"]}
57
58
59 ##
60 # Determine the rights that an object should have. The rights are entirely
61 # dependent on the type of the object. For example, users automatically
62 # get "refresh", "resolve", and "info".
63 #
64 # @param type the type of the object (user | sa | ma | slice | node)
65 # @param name human readable name of the object (not used at this time)
66 #
67 # @return Rights object containing rights
68
69 def determine_rights(type, name):
70     rl = Rights()
71
72     # rights seem to be somewhat redundant with the type of the credential.
73     # For example, a "sa" credential implies the authority right, because
74     # a sa credential cannot be issued to a user who is not an owner of
75     # the authority
76     if type == "user":
77         rl.add("refresh")
78         rl.add("resolve")
79         rl.add("info")
80     elif type in ["sa", "authority+sa"]:
81         rl.add("authority")
82         rl.add("sa")
83     elif type in ["ma", "authority+ma", "cm", "authority+cm", "sm", "authority+sm"]:
84         rl.add("authority")
85         rl.add("ma")
86     elif type == "authority":
87         rl.add("authority")
88         rl.add("sa")
89         rl.add("ma")
90     elif type == "slice":
91         rl.add("refresh")
92         rl.add("embed")
93         rl.add("bind")
94         rl.add("control")
95         rl.add("info")
96 # wouldn't that be authority+cm instead ?
97     elif type == "component":
98         rl.add("operator")
99     return rl
100
101
102 ##
103 # The Right class represents a single privilege.
104
105
106 class Right:
107     ##
108     # Create a new right.
109     #
110     # @param kind is a string naming the right. For example "control"
111
112     def __init__(self, kind, delegate=False):
113         self.kind = kind
114         self.delegate = delegate
115
116     def __repr__(self): return "<Rgt:%s>" % self.kind
117
118     ##
119     # Test to see if this right object is allowed to perform an operation.
120     # Returns True if the operation is allowed, False otherwise.
121     #
122     # @param op_name is a string naming the operation. For example "listslices".
123
124     def can_perform(self, op_name):
125         allowed_ops = privilege_table.get(self.kind.lower(), None)
126         if not allowed_ops:
127             return False
128
129         # if "*" is specified, then all ops are permitted
130         if "*" in allowed_ops:
131             return True
132
133         return (op_name.lower() in allowed_ops)
134
135     ##
136     # Test to see if this right is a superset of a child right. A right is a
137     # superset if every operating that is allowed by the child is also allowed
138     # by this object.
139     #
140     # @param child is a Right object describing the child right
141
142     def is_superset(self, child):
143         my_allowed_ops = privilege_table.get(self.kind.lower(), None)
144         child_allowed_ops = privilege_table.get(child.kind.lower(), None)
145
146         if not self.delegate:
147             return False
148
149         if "*" in my_allowed_ops:
150             return True
151
152         for right in child_allowed_ops:
153             if not right in my_allowed_ops:
154                 return False
155
156         return True
157
158 ##
159 # A Rights object represents a list of privileges.
160
161
162 class Rights:
163     ##
164     # Create a new rightlist object, containing no rights.
165     #
166     # @param string if string!=None, load the rightlist from the string
167
168     def __init__(self, string=None):
169         self.rights = []
170         if string:
171             self.load_from_string(string)
172
173     def __repr__(self): return "[" + \
174         " ".join(["%s" % r for r in self.rights]) + "]"
175
176     def is_empty(self):
177         return self.rights == []
178
179     ##
180     # Add a right to this list
181     #
182     # @param right is either a Right object or a string describing the right
183
184     def add(self, right, delegate=False):
185         if isinstance(right, str):
186             right = Right(right, delegate)
187         self.rights.append(right)
188
189     ##
190     # Load the rightlist object from a string
191
192     def load_from_string(self, string):
193         self.rights = []
194
195         # none == no rights, so leave the list empty
196         if not string:
197             return
198
199         parts = string.split(",")
200         for part in parts:
201             if ':' in part:
202                 spl = part.split(':')
203                 kind = spl[0].strip()
204                 delegate = bool(int(spl[1]))
205             else:
206                 kind = part.strip()
207                 delegate = 0
208             self.rights.append(Right(kind, bool(delegate)))
209
210     ##
211     # Save the rightlist object to a string. It is saved in the format of a
212     # comma-separated list.
213
214     def save_to_string(self):
215         right_names = []
216         for right in self.rights:
217             right_names.append('%s:%d' % (right.kind.strip(), right.delegate))
218
219         return ",".join(right_names)
220
221     ##
222     # Check to see if some right in this list allows an operation. This is
223     # done by evaluating the can_perform function of each operation in the
224     # list.
225     #
226     # @param op_name is an operation to check, for example "listslices"
227
228     def can_perform(self, op_name):
229
230         for right in self.rights:
231             if right.can_perform(op_name):
232                 return True
233         return False
234
235     ##
236     # Check to see if all of the rights in this rightlist are a superset
237     # of all the rights in a child rightlist. A rightlist is a superset
238     # if there is no operation in the child rightlist that cannot be
239     # performed in the parent rightlist.
240     #
241     # @param child is a rightlist object describing the child
242
243     def is_superset(self, child):
244         for child_right in child.rights:
245             allowed = False
246             for my_right in self.rights:
247                 if my_right.is_superset(child_right):
248                     allowed = True
249                     break
250             if not allowed:
251                 return False
252         return True
253
254     ##
255     # set the delegate bit to 'delegate' on
256     # all privileges
257     #
258     # @param delegate boolean (True or False)
259
260     def delegate_all_privileges(self, delegate):
261         for right in self.rights:
262             right.delegate = delegate
263
264     ##
265     # true if all privileges have delegate bit set true
266     # false otherwise
267
268     def get_all_delegate(self):
269         for right in self.rights:
270             if not right.delegate:
271                 return False
272         return True
273
274     def pretty_rights(self):
275         return "<Rights{}>".format(";".join(["{}".format(r) for r in self.rights]))