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 # Rights - 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
18 # Note that "*" is a privilege granted by ProtoGENI slice authorities, and we
19 # give it access to the GENI AM calls
21 privilege_table = {"authority": ["register", "remove", "update", "resolve", "list", "getcredential", "*"],
22 "refresh": ["remove", "update"],
23 "resolve": ["resolve", "list", "getcredential"],
24 "sa": ["getticket", "redeemslice", "redeemticket", "createslice", "createsliver", "deleteslice", "deletesliver", "updateslice",
25 "getsliceresources", "getticket", "loanresources", "stopslice", "startslice", "renewsliver",
26 "deleteslice", "deletesliver", "resetslice", "listslices", "listnodes", "getpolicy", "sliverstatus"],
27 "embed": ["getticket", "redeemslice", "redeemticket", "createslice", "createsliver", "renewsliver", "deleteslice",
28 "deletesliver", "updateslice", "sliverstatus", "getsliceresources", "shutdown"],
29 "bind": ["getticket", "loanresources", "redeemticket"],
30 "control": ["updateslice", "createslice", "createsliver", "renewsliver", "sliverstatus", "stopslice", "startslice",
31 "deleteslice", "deletesliver", "resetslice", "getsliceresources", "getgids"],
32 "info": ["listslices", "listnodes", "getpolicy"],
33 "ma": ["setbootstate", "getbootstate", "reboot", "getgids", "gettrustedcerts"],
34 "operator": ["gettrustedcerts", "getgids"],
35 "*": ["createsliver", "deletesliver", "sliverstatus", "renewsliver", "shutdown"]}
40 # Determine the rights that an object should have. The rights are entirely
41 # dependent on the type of the object. For example, users automatically
42 # get "refresh", "resolve", and "info".
44 # @param type the type of the object (user | sa | ma | slice | node)
45 # @param name human readable name of the object (not used at this time)
47 # @return Rights object containing rights
49 def determine_rights(type, name):
52 # rights seem to be somewhat redundant with the type of the credential.
53 # For example, a "sa" credential implies the authority right, because
54 # a sa credential cannot be issued to a user who is not an owner of
60 elif type in ["sa", "authority+sa"]:
63 elif type in ["ma", "authority+ma", "cm", "authority+cm", "sm", "authority+sm"]:
66 elif type == "authority":
76 # wouldn't that be authority+cm instead ?
77 elif type == "component":
83 # The Right class represents a single privilege.
91 # @param kind is a string naming the right. For example "control"
93 def __init__(self, kind, delegate=False):
95 self.delegate = delegate
98 # Test to see if this right object is allowed to perform an operation.
99 # Returns True if the operation is allowed, False otherwise.
101 # @param op_name is a string naming the operation. For example "listslices".
103 def can_perform(self, op_name):
104 allowed_ops = privilege_table.get(self.kind.lower(), None)
108 # if "*" is specified, then all ops are permitted
109 if "*" in allowed_ops:
112 return (op_name.lower() in allowed_ops)
115 # Test to see if this right is a superset of a child right. A right is a
116 # superset if every operating that is allowed by the child is also allowed
119 # @param child is a Right object describing the child right
121 def is_superset(self, child):
122 my_allowed_ops = privilege_table.get(self.kind.lower(), None)
123 child_allowed_ops = privilege_table.get(child.kind.lower(), None)
125 if not self.delegate:
128 if "*" in my_allowed_ops:
131 for right in child_allowed_ops:
132 if not right in my_allowed_ops:
138 # A Rights object represents a list of privileges.
142 # Create a new rightlist object, containing no rights.
144 # @param string if string!=None, load the rightlist from the string
146 def __init__(self, string=None):
149 self.load_from_string(string)
152 return self.rights == []
155 # Add a right to this list
157 # @param right is either a Right object or a string describing the right
159 def add(self, right, delegate=False):
160 if isinstance(right, str):
161 right = Right(right, delegate)
162 self.rights.append(right)
165 # Load the rightlist object from a string
167 def load_from_string(self, string):
170 # none == no rights, so leave the list empty
174 parts = string.split(",")
177 spl = part.split(':')
178 kind = spl[0].strip()
179 delegate = bool(int(spl[1]))
183 self.rights.append(Right(kind, bool(delegate)))
186 # Save the rightlist object to a string. It is saved in the format of a
187 # comma-separated list.
189 def save_to_string(self):
191 for right in self.rights:
192 right_names.append('%s:%d' % (right.kind.strip(), right.delegate))
194 return ",".join(right_names)
197 # Check to see if some right in this list allows an operation. This is
198 # done by evaluating the can_perform function of each operation in the
201 # @param op_name is an operation to check, for example "listslices"
203 def can_perform(self, op_name):
205 for right in self.rights:
206 if right.can_perform(op_name):
211 # Check to see if all of the rights in this rightlist are a superset
212 # of all the rights in a child rightlist. A rightlist is a superset
213 # if there is no operation in the child rightlist that cannot be
214 # performed in the parent rightlist.
216 # @param child is a rightlist object describing the child
218 def is_superset(self, child):
219 for child_right in child.rights:
221 for my_right in self.rights:
222 if my_right.is_superset(child_right):
231 # set the delegate bit to 'delegate' on
234 # @param delegate boolean (True or False)
236 def delegate_all_privileges(self, delegate):
237 for right in self.rights:
238 right.delegate = delegate
241 # true if all privileges have delegate bit set true
244 def get_all_delegate(self):
245 for right in self.rights:
246 if not right.delegate: