1 #----------------------------------------------------------------------
2 # Copyright (c) 2008 Board of Trustees, Princeton University
4 # Permission is hereby granted, free of charge, to any person obtaining
5 # a copy of this software and/or hardware specification (the "Work") to
6 # deal in the Work without restriction, including without limitation the
7 # rights to use, copy, modify, merge, publish, distribute, sublicense,
8 # and/or sell copies of the Work, and to permit persons to whom the Work
9 # is furnished to do so, subject to the following conditions:
11 # The above copyright notice and this permission notice shall be
12 # included in all copies or substantial portions of the Work.
14 # THE WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
18 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE WORK OR THE USE OR OTHER DEALINGS
22 #----------------------------------------------------------------------
24 # This Module implements rights and lists of rights for the SFA. Rights
25 # are implemented by two classes:
27 # Right - represents a single right
29 # Rights - represents a list of rights
31 # A right may allow several different operations. For example, the "info" right
32 # allows "listslices", "listcomponentresources", etc.
38 # privilege_table is a list of priviliges and what operations are allowed
40 # Note that "*" is a privilege granted by ProtoGENI slice authorities, and we
41 # give it access to the GENI AM calls
43 privilege_table = {"authority": ["register", "remove", "update", "resolve", "list", "getcredential", "*"],
44 "refresh": ["remove", "update"],
45 "resolve": ["resolve", "list", "getcredential"],
46 "sa": ["getticket", "redeemslice", "redeemticket", "createslice", "createsliver", "deleteslice", "deletesliver", "updateslice",
47 "getsliceresources", "getticket", "loanresources", "stopslice", "startslice", "renewsliver",
48 "deleteslice", "deletesliver", "resetslice", "listslices", "listnodes", "getpolicy", "sliverstatus"],
49 "embed": ["getticket", "redeemslice", "redeemticket", "createslice", "createsliver", "renewsliver", "deleteslice",
50 "deletesliver", "updateslice", "sliverstatus", "getsliceresources", "shutdown"],
51 "bind": ["getticket", "loanresources", "redeemticket"],
52 "control": ["updateslice", "createslice", "createsliver", "renewsliver", "sliverstatus", "stopslice", "startslice",
53 "deleteslice", "deletesliver", "resetslice", "getsliceresources", "getgids"],
54 "info": ["listslices", "listnodes", "getpolicy"],
55 "ma": ["setbootstate", "getbootstate", "reboot", "getgids", "gettrustedcerts"],
56 "operator": ["gettrustedcerts", "getgids"],
57 "*": ["createsliver", "deletesliver", "sliverstatus", "renewsliver", "shutdown"]}
62 # Determine the rights that an object should have. The rights are entirely
63 # dependent on the type of the object. For example, users automatically
64 # get "refresh", "resolve", and "info".
66 # @param type the type of the object (user | sa | ma | slice | node)
67 # @param name human readable name of the object (not used at this time)
69 # @return Rights object containing rights
71 def determine_rights(type, name):
74 # rights seem to be somewhat redundant with the type of the credential.
75 # For example, a "sa" credential implies the authority right, because
76 # a sa credential cannot be issued to a user who is not an owner of
82 elif type in ["sa", "authority+sa"]:
85 elif type in ["ma", "authority+ma", "cm", "authority+cm", "sm", "authority+sm"]:
88 elif type == "authority":
98 # wouldn't that be authority+cm instead ?
99 elif type == "component":
105 # The Right class represents a single privilege.
111 # Create a new right.
113 # @param kind is a string naming the right. For example "control"
115 def __init__(self, kind, delegate=False):
117 self.delegate = delegate
119 def __repr__ (self): return "<Rgt:%s>"%self.kind
122 # Test to see if this right object is allowed to perform an operation.
123 # Returns True if the operation is allowed, False otherwise.
125 # @param op_name is a string naming the operation. For example "listslices".
127 def can_perform(self, op_name):
128 allowed_ops = privilege_table.get(self.kind.lower(), None)
132 # if "*" is specified, then all ops are permitted
133 if "*" in allowed_ops:
136 return (op_name.lower() in allowed_ops)
139 # Test to see if this right is a superset of a child right. A right is a
140 # superset if every operating that is allowed by the child is also allowed
143 # @param child is a Right object describing the child right
145 def is_superset(self, child):
146 my_allowed_ops = privilege_table.get(self.kind.lower(), None)
147 child_allowed_ops = privilege_table.get(child.kind.lower(), None)
149 if not self.delegate:
152 if "*" in my_allowed_ops:
155 for right in child_allowed_ops:
156 if not right in my_allowed_ops:
162 # A Rights object represents a list of privileges.
166 # Create a new rightlist object, containing no rights.
168 # @param string if string!=None, load the rightlist from the string
170 def __init__(self, string=None):
173 self.load_from_string(string)
175 def __repr__ (self): return "[" + " ".join( ["%s"%r for r in self.rights]) + "]"
178 return self.rights == []
181 # Add a right to this list
183 # @param right is either a Right object or a string describing the right
185 def add(self, right, delegate=False):
186 if isinstance(right, str):
187 right = Right(right, delegate)
188 self.rights.append(right)
191 # Load the rightlist object from a string
193 def load_from_string(self, string):
196 # none == no rights, so leave the list empty
200 parts = string.split(",")
203 spl = part.split(':')
204 kind = spl[0].strip()
205 delegate = bool(int(spl[1]))
209 self.rights.append(Right(kind, bool(delegate)))
212 # Save the rightlist object to a string. It is saved in the format of a
213 # comma-separated list.
215 def save_to_string(self):
217 for right in self.rights:
218 right_names.append('%s:%d' % (right.kind.strip(), right.delegate))
220 return ",".join(right_names)
223 # Check to see if some right in this list allows an operation. This is
224 # done by evaluating the can_perform function of each operation in the
227 # @param op_name is an operation to check, for example "listslices"
229 def can_perform(self, op_name):
231 for right in self.rights:
232 if right.can_perform(op_name):
237 # Check to see if all of the rights in this rightlist are a superset
238 # of all the rights in a child rightlist. A rightlist is a superset
239 # if there is no operation in the child rightlist that cannot be
240 # performed in the parent rightlist.
242 # @param child is a rightlist object describing the child
244 def is_superset(self, child):
245 for child_right in child.rights:
247 for my_right in self.rights:
248 if my_right.is_superset(child_right):
257 # set the delegate bit to 'delegate' on
260 # @param delegate boolean (True or False)
262 def delegate_all_privileges(self, delegate):
263 for right in self.rights:
264 right.delegate = delegate
267 # true if all privileges have delegate bit set true
270 def get_all_delegate(self):
271 for right in self.rights:
272 if not right.delegate: