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.
15 # privilege_table is a list of priviliges and what operations are allowed
18 privilege_table = {"authority": ["register", "remove", "update", "resolve", "list", "getcredential", "*"],
19 "refresh": ["remove", "update"],
20 "resolve": ["resolve", "list", "getcredential", "listresources", "getversion"],
21 "sa": ["getticket", "redeemslice", "redeemticket", "createslice", "deleteslice", "updateslice",
22 "getsliceresources", "getticket", "loanresources", "stopslice", "startslice",
23 "deleteslice", "resetslice", "listslices", "listnodes", "getpolicy", "createsliver"],
24 "embed": ["getticket", "redeemslice", "redeemticket", "createslice", "createsliver", "deleteslice", "updateslice", "getsliceresources"],
25 "bind": ["getticket", "loanresources", "redeemticket"],
26 "control": ["updateslice", "createslice", "createsliver", "stopslice", "startslice", "deleteslice", "resetslice", "getsliceresources", "getgids"],
27 "info": ["listslices", "listnodes", "getpolicy"],
28 "ma": ["setbootstate", "getbootstate", "reboot", "getgids", "gettrustedcerts"],
29 "operator": ["gettrustedcerts", "getgids"]}
33 # Determine tje rights that an object should have. The rights are entirely
34 # dependent on the type of the object. For example, users automatically
35 # get "refresh", "resolve", and "info".
37 # @param type the type of the object (user | sa | ma | slice | node)
38 # @param name human readable name of the object (not used at this time)
40 # @return RightList object containing rights
42 def determine_rights(type, name):
45 # rights seem to be somewhat redundant with the type of the credential.
46 # For example, a "sa" credential implies the authority right, because
47 # a sa credential cannot be issued to a user who is not an owner of
54 rl.add("authority,sa")
56 rl.add("authority,ma")
57 elif type == "authority":
58 rl.add("authority,sa,ma")
65 elif type == "component":
71 # The Right class represents a single privilege.
79 # @param kind is a string naming the right. For example "control"
81 def __init__(self, kind):
85 # Test to see if this right object is allowed to perform an operation.
86 # Returns True if the operation is allowed, False otherwise.
88 # @param op_name is a string naming the operation. For example "listslices".
90 def can_perform(self, op_name):
91 allowed_ops = privilege_table.get(self.kind.lower(), None)
95 # if "*" is specified, then all ops are permitted
96 if "*" in allowed_ops:
99 return (op_name.lower() in allowed_ops)
102 # Test to see if this right is a superset of a child right. A right is a
103 # superset if every operating that is allowed by the child is also allowed
106 # @param child is a Right object describing the child right
108 def is_superset(self, child):
109 my_allowed_ops = privilege_table.get(self.kind.lower(), None)
110 child_allowed_ops = privilege_table.get(child.kind.lower(), None)
112 if "*" in my_allowed_ops:
115 for right in child_allowed_ops:
116 if not right in my_allowed_ops:
122 # A RightList object represents a list of privileges.
126 # Create a new rightlist object, containing no rights.
128 # @param string if string!=None, load the rightlist from the string
130 def __init__(self, string=None):
133 self.load_from_string(string)
136 return self.rights == []
139 # Add a right to this list
141 # @param right is either a Right object or a string describing the right
143 def add(self, right):
144 if isinstance(right, str):
145 right = Right(kind = right)
146 self.rights.append(right)
149 # Load the rightlist object from a string
151 def load_from_string(self, string):
154 # none == no rights, so leave the list empty
158 parts = string.split(",")
160 self.rights.append(Right(part))
163 # Save the rightlist object to a string. It is saved in the format of a
164 # comma-separated list.
166 def save_to_string(self):
168 for right in self.rights:
169 right_names.append(right.kind)
171 return ",".join(right_names)
174 # Check to see if some right in this list allows an operation. This is
175 # done by evaluating the can_perform function of each operation in the
178 # @param op_name is an operation to check, for example "listslices"
180 def can_perform(self, op_name):
181 for right in self.rights:
182 if right.can_perform(op_name):
187 # Check to see if all of the rights in this rightlist are a superset
188 # of all the rights in a child rightlist. A rightlist is a superset
189 # if there is no operation in the child rightlist that cannot be
190 # performed in the parent rightlist.
192 # @param child is a rightlist object describing the child
194 def is_superset(self, child):
195 for child_right in child.rights:
197 for my_right in self.rights:
198 if my_right.is_superset(child_right):
206 # Determine tje rights that an object should have. The rights are entirely
207 # dependent on the type of the object. For example, users automatically
208 # get "refresh", "resolve", and "info".
210 # @param type the type of the object (user | sa | ma | slice | node)
211 # @param name human readable name of the object (not used at this time)
213 # @return RightList object containing rights
215 def determine_rights(self, type, name):
218 # rights seem to be somewhat redundant with the type of the credential.
219 # For example, a "sa" credential implies the authority right, because
220 # a sa credential cannot be issued to a user who is not an owner of
228 rl.add("authority,sa")
230 rl.add("authority,ma")
231 elif type == "authority":
232 rl.add("authority,sa,ma")
233 elif type == "slice":
239 elif type == "component":