2 # This Module implements rights and lists of rights for the SFA. Rights
3 # are implemented by two classes:
5 # Right - represents a single right
7 # RightList - represents a list of rights
9 # A right may allow several different operations. For example, the "info" right
10 # allows "listslices", "listcomponentresources", etc.
16 # privilege_table is a list of priviliges and what operations are allowed
19 privilege_table = {"authority": ["register", "remove", "update", "resolve", "list", "getcredential", "*"],
20 "refresh": ["remove", "update"],
21 "resolve": ["resolve", "list", "getcredential", "listresources", "getversion"],
22 "sa": ["getticket", "redeemslice", "redeemticket", "createslice", "deleteslice", "updateslice",
23 "getsliceresources", "getticket", "loanresources", "stopslice", "startslice",
24 "deleteslice", "resetslice", "listslices", "listnodes", "getpolicy", "createsliver"],
25 "embed": ["getticket", "redeemslice", "redeemticket", "createslice", "createsliver", "deleteslice", "updateslice", "getsliceresources"],
26 "bind": ["getticket", "loanresources", "redeemticket"],
27 "control": ["updateslice", "createslice", "createsliver", "stopslice", "startslice", "deleteslice", "resetslice", "getsliceresources", "getgids"],
28 "info": ["listslices", "listnodes", "getpolicy"],
29 "ma": ["setbootstate", "getbootstate", "reboot", "getgids", "gettrustedcerts"],
30 "operator": ["gettrustedcerts", "getgids"]}
35 # Determine tje rights that an object should have. The rights are entirely
36 # dependent on the type of the object. For example, users automatically
37 # get "refresh", "resolve", and "info".
39 # @param type the type of the object (user | sa | ma | slice | node)
40 # @param name human readable name of the object (not used at this time)
42 # @return RightList object containing rights
44 def determine_rights(type, name):
47 # rights seem to be somewhat redundant with the type of the credential.
48 # For example, a "sa" credential implies the authority right, because
49 # a sa credential cannot be issued to a user who is not an owner of
61 elif type == "authority":
71 elif type == "component":
77 # The Right class represents a single privilege.
85 # @param kind is a string naming the right. For example "control"
87 def __init__(self, kind, delegate=False):
89 self.delegate = delegate
92 # Test to see if this right object is allowed to perform an operation.
93 # Returns True if the operation is allowed, False otherwise.
95 # @param op_name is a string naming the operation. For example "listslices".
97 def can_perform(self, op_name):
98 allowed_ops = privilege_table.get(self.kind.lower(), None)
102 # if "*" is specified, then all ops are permitted
103 if "*" in allowed_ops:
106 return (op_name.lower() in allowed_ops)
109 # Test to see if this right is a superset of a child right. A right is a
110 # superset if every operating that is allowed by the child is also allowed
113 # @param child is a Right object describing the child right
115 def is_superset(self, child):
116 my_allowed_ops = privilege_table.get(self.kind.lower(), None)
117 child_allowed_ops = privilege_table.get(child.kind.lower(), None)
119 if not self.delegate:
122 if "*" in my_allowed_ops:
125 for right in child_allowed_ops:
126 if not right in my_allowed_ops:
132 # A RightList object represents a list of privileges.
136 # Create a new rightlist object, containing no rights.
138 # @param string if string!=None, load the rightlist from the string
140 def __init__(self, string=None):
143 self.load_from_string(string)
146 return self.rights == []
149 # Add a right to this list
151 # @param right is either a Right object or a string describing the right
153 def add(self, right, delegate=False):
154 if isinstance(right, str):
155 right = Right(right, delegate)
156 self.rights.append(right)
159 # Load the rightlist object from a string
161 def load_from_string(self, string):
164 # none == no rights, so leave the list empty
168 parts = string.split(",")
171 spl = part.split(':')
172 kind = spl[0].strip()
173 delegate = bool(int(spl[1]))
177 self.rights.append(Right(kind, bool(delegate)))
180 # Save the rightlist object to a string. It is saved in the format of a
181 # comma-separated list.
183 def save_to_string(self):
185 for right in self.rights:
186 right_names.append('%s:%d' % (right.kind.strip(), right.delegate))
188 return ",".join(right_names)
191 # Check to see if some right in this list allows an operation. This is
192 # done by evaluating the can_perform function of each operation in the
195 # @param op_name is an operation to check, for example "listslices"
197 def can_perform(self, op_name):
198 for right in self.rights:
199 if right.can_perform(op_name):
204 # Check to see if all of the rights in this rightlist are a superset
205 # of all the rights in a child rightlist. A rightlist is a superset
206 # if there is no operation in the child rightlist that cannot be
207 # performed in the parent rightlist.
209 # @param child is a rightlist object describing the child
211 def is_superset(self, child):
212 for child_right in child.rights:
214 for my_right in self.rights:
215 if my_right.is_superset(child_right):
223 # set the delegate bit to 'delegate' on
226 # @param delegate boolean (True or False)
228 def delegate_all_privileges(self, delegate):
229 for right in self.rights:
230 right.delegate = delegate
233 # true if all privileges have delegate bit set true
236 def get_all_delegate(self):
237 for right in self.rights:
238 if not right.delegate:
245 # Determine the rights that an object should have. The rights are entirely
246 # dependent on the type of the object. For example, users automatically
247 # get "refresh", "resolve", and "info".
249 # @param type the type of the object (user | sa | ma | slice | node)
250 # @param name human readable name of the object (not used at this time)
252 # @return RightList object containing rights
254 def determine_rights(self, type, name):
257 # rights seem to be somewhat redundant with the type of the credential.
258 # For example, a "sa" credential implies the authority right, because
259 # a sa credential cannot be issued to a user who is not an owner of
272 elif type == "authority":
276 elif type == "slice":
282 elif type == "component":