2 # This Module implements rights and lists of rights for the Geni wrapper. 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", "createslice", "deleteslice", "updateslice", "getsliceresources", "getticket", "loanresources", "stopslice", "startslice", "deleteslice", "resetslice", "listslices", "listnodes", "getpolicy"],
21 "embed": ["getticket", "redeemslice", "createslice", "deleteslice", "updateslice", "getsliceresources"],
22 "bind": ["getticket", "loanresources"],
23 "control": ["updateslice", "createslice", "stopslice", "startslice", "deleteslice", "resetslice", "getsliceresources"],
24 "info": ["listslices", "listnodes", "getpolicy"],
25 "ma": ["setbootstate", "getbootstate", "reboot"]}
29 # Determine tje rights that an object should have. The rights are entirely
30 # dependent on the type of the object. For example, users automatically
31 # get "refresh", "resolve", and "info".
33 # @param type the type of the object (user | sa | ma | slice | node)
34 # @param name human readable name of the object (not used at this time)
36 # @return RightList object containing rights
38 def determine_rights(type, name):
41 # rights seem to be somewhat redundant with the type of the credential.
42 # For example, a "sa" credential implies the authority right, because
43 # a sa credential cannot be issued to a user who is not an owner of
50 rl.add("authority,sa")
52 rl.add("authority,ma")
59 elif type == "component":
65 # The Right class represents a single privilege.
73 # @param kind is a string naming the right. For example "control"
75 def __init__(self, kind):
79 # Test to see if this right object is allowed to perform an operation.
80 # Returns True if the operation is allowed, False otherwise.
82 # @param op_name is a string naming the operation. For example "listslices".
84 def can_perform(self, op_name):
85 allowed_ops = privilege_table.get(self.kind.lower(), None)
89 # if "*" is specified, then all ops are permitted
90 if "*" in allowed_ops:
93 return (op_name.lower() in allowed_ops)
96 # Test to see if this right is a superset of a child right. A right is a
97 # superset if every operating that is allowed by the child is also allowed
100 # @param child is a Right object describing the child right
102 def is_superset(self, child):
103 my_allowed_ops = privilege_table.get(self.kind.lower(), None)
104 child_allowed_ops = privilege_table.get(child.kind.lower(), None)
106 if "*" in my_allowed_ops:
109 for right in child_allowed_ops:
110 if not right in my_allowed_ops:
116 # A RightList object represents a list of privileges.
120 # Create a new rightlist object, containing no rights.
122 # @param string if string!=None, load the rightlist from the string
124 def __init__(self, string=None):
127 self.load_from_string(string)
130 # Add a right to this list
132 # @param right is either a Right object or a string describing the right
134 def add(self, right):
135 if isinstance(right, str):
136 right = Right(kind = right)
137 self.rights.append(right)
140 # Load the rightlist object from a string
142 def load_from_string(self, string):
145 # none == no rights, so leave the list empty
149 parts = string.split(",")
151 self.rights.append(Right(part))
154 # Save the rightlist object to a string. It is saved in the format of a
155 # comma-separated list.
157 def save_to_string(self):
159 for right in self.rights:
160 right_names.append(right.kind)
162 return ",".join(right_names)
165 # Check to see if some right in this list allows an operation. This is
166 # done by evaluating the can_perform function of each operation in the
169 # @param op_name is an operation to check, for example "listslices"
171 def can_perform(self, op_name):
172 for right in self.rights:
173 if right.can_perform(op_name):
178 # Check to see if all of the rights in this rightlist are a superset
179 # of all the rights in a child rightlist. A rightlist is a superset
180 # if there is no operation in the child rightlist that cannot be
181 # performed in the parent rightlist.
183 # @param child is a rightlist object describing the child
185 def is_superset(self, child):
186 for child_right in child.rights:
188 for my_right in self.rights:
189 if my_right.is_superset(child_right):
197 # Determine tje rights that an object should have. The rights are entirely
198 # dependent on the type of the object. For example, users automatically
199 # get "refresh", "resolve", and "info".
201 # @param type the type of the object (user | sa | ma | slice | node)
202 # @param name human readable name of the object (not used at this time)
204 # @return RightList object containing rights
206 def determine_rights(self, type, name):
209 # rights seem to be somewhat redundant with the type of the credential.
210 # For example, a "sa" credential implies the authority right, because
211 # a sa credential cannot be issued to a user who is not an owner of
219 rl.add("authority,sa")
221 rl.add("authority,ma")
222 elif type == "slice":
228 elif type == "component":