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.
14 # privilege_table is a list of priviliges and what operations are allowed
17 privilege_table = {"authority": ["register", "remove", "update", "resolve", "list", "getcredential", "*"],
18 "refresh": ["remove", "update"],
19 "resolve": ["resolve", "list", "getcredential"],
20 "sa": ["getticket", "redeemslice", "redeemticket", "createslice", "deleteslice", "updateslice",
21 "getsliceresources", "getticket", "loanresources", "stopslice", "startslice",
22 "deleteslice", "resetslice", "listslices", "listnodes", "getpolicy"],
23 "embed": ["getticket", "redeemslice", "redeemticket", "createslice", "deleteslice", "updateslice", "getsliceresources"],
24 "bind": ["getticket", "loanresources", "redeemticket"],
25 "control": ["updateslice", "createslice", "stopslice", "startslice", "deleteslice", "resetslice", "getsliceresources", "getgids"],
26 "info": ["listslices", "listnodes", "getpolicy"],
27 "ma": ["setbootstate", "getbootstate", "reboot", "getgids", "gettrustedcerts"],
28 "operator": ["gettrustedcerts", "getgids"]}
32 # Determine tje rights that an object should have. The rights are entirely
33 # dependent on the type of the object. For example, users automatically
34 # get "refresh", "resolve", and "info".
36 # @param type the type of the object (user | sa | ma | slice | node)
37 # @param name human readable name of the object (not used at this time)
39 # @return RightList object containing rights
41 def determine_rights(type, name):
44 # rights seem to be somewhat redundant with the type of the credential.
45 # For example, a "sa" credential implies the authority right, because
46 # a sa credential cannot be issued to a user who is not an owner of
53 rl.add("authority,sa")
55 rl.add("authority,ma")
56 elif type == "authority":
57 rl.add("authority,sa,ma")
64 elif type == "component":
70 # The Right class represents a single privilege.
78 # @param kind is a string naming the right. For example "control"
80 def __init__(self, kind):
84 # Test to see if this right object is allowed to perform an operation.
85 # Returns True if the operation is allowed, False otherwise.
87 # @param op_name is a string naming the operation. For example "listslices".
89 def can_perform(self, op_name):
90 allowed_ops = privilege_table.get(self.kind.lower(), None)
94 # if "*" is specified, then all ops are permitted
95 if "*" in allowed_ops:
98 return (op_name.lower() in allowed_ops)
101 # Test to see if this right is a superset of a child right. A right is a
102 # superset if every operating that is allowed by the child is also allowed
105 # @param child is a Right object describing the child right
107 def is_superset(self, child):
108 my_allowed_ops = privilege_table.get(self.kind.lower(), None)
109 child_allowed_ops = privilege_table.get(child.kind.lower(), None)
111 if "*" in my_allowed_ops:
114 for right in child_allowed_ops:
115 if not right in my_allowed_ops:
121 # A RightList object represents a list of privileges.
125 # Create a new rightlist object, containing no rights.
127 # @param string if string!=None, load the rightlist from the string
129 def __init__(self, string=None):
132 self.load_from_string(string)
135 return self.rights == []
138 # Add a right to this list
140 # @param right is either a Right object or a string describing the right
142 def add(self, right):
143 if isinstance(right, str):
144 right = Right(kind = right)
145 self.rights.append(right)
148 # Load the rightlist object from a string
150 def load_from_string(self, string):
153 # none == no rights, so leave the list empty
157 parts = string.split(",")
159 self.rights.append(Right(part))
162 # Save the rightlist object to a string. It is saved in the format of a
163 # comma-separated list.
165 def save_to_string(self):
167 for right in self.rights:
168 right_names.append(right.kind)
170 return ",".join(right_names)
173 # Check to see if some right in this list allows an operation. This is
174 # done by evaluating the can_perform function of each operation in the
177 # @param op_name is an operation to check, for example "listslices"
179 def can_perform(self, op_name):
180 for right in self.rights:
181 if right.can_perform(op_name):
186 # Check to see if all of the rights in this rightlist are a superset
187 # of all the rights in a child rightlist. A rightlist is a superset
188 # if there is no operation in the child rightlist that cannot be
189 # performed in the parent rightlist.
191 # @param child is a rightlist object describing the child
193 def is_superset(self, child):
194 for child_right in child.rights:
196 for my_right in self.rights:
197 if my_right.is_superset(child_right):
205 # Determine tje rights that an object should have. The rights are entirely
206 # dependent on the type of the object. For example, users automatically
207 # get "refresh", "resolve", and "info".
209 # @param type the type of the object (user | sa | ma | slice | node)
210 # @param name human readable name of the object (not used at this time)
212 # @return RightList object containing rights
214 def determine_rights(self, type, name):
217 # rights seem to be somewhat redundant with the type of the credential.
218 # For example, a "sa" credential implies the authority right, because
219 # a sa credential cannot be issued to a user who is not an owner of
227 rl.add("authority,sa")
229 rl.add("authority,ma")
230 elif type == "authority":
231 rl.add("authority,sa,ma")
232 elif type == "slice":
238 elif type == "component":