switch from sa/ma to authority, fix update_membership_list
[sfa.git] / geni / util / rights.py
1 ##
2 # This Module implements rights and lists of rights for the Geni wrapper. Rights
3 # are implemented by two classes:
4 #
5 # Right - represents a single right
6 #
7 # RightList - represents a list of rights
8 #
9 # A right may allow several different operations. For example, the "info" right
10 # allows "listslices", "listcomponentresources", etc.
11 ##
12
13 ##
14 # privilege_table is a list of priviliges and what operations are allowed
15 # per privilege.
16
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"]}
26
27
28 ##
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".
32 #
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)
35 #
36 # @return RightList object containing rights
37
38 def determine_rights(type, name):
39     rl = RightList()
40
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
44     # the authority
45     if type == "user":
46         rl.add("refresh")
47         rl.add("resolve")
48         rl.add("info")
49     elif type == "sa":
50         rl.add("authority,sa")
51     elif type == "ma":
52         rl.add("authority,ma")
53     elif type == "slice":
54         rl.add("refresh")
55         rl.add("embed")
56         rl.add("bind")
57         rl.add("control")
58         rl.add("info")
59     elif type == "component":
60         rl.add("operator")
61     return rl
62
63
64 ##
65 # The Right class represents a single privilege.
66
67
68
69 class Right:
70    ##
71    # Create a new right.
72    #
73    # @param kind is a string naming the right. For example "control"
74
75    def __init__(self, kind):
76       self.kind = kind
77
78    ##
79    # Test to see if this right object is allowed to perform an operation.
80    # Returns True if the operation is allowed, False otherwise.
81    #
82    # @param op_name is a string naming the operation. For example "listslices".
83
84    def can_perform(self, op_name):
85       allowed_ops = privilege_table.get(self.kind.lower(), None)
86       if not allowed_ops:
87          return False
88
89       # if "*" is specified, then all ops are permitted
90       if "*" in allowed_ops:
91          return True
92
93       return (op_name.lower() in allowed_ops)
94
95    ##
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
98    # by this object.
99    #
100    # @param child is a Right object describing the child right
101
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)
105
106       if "*" in my_allowed_ops:
107           return True
108
109       for right in child_allowed_ops:
110           if not right in my_allowed_ops:
111               return False
112
113       return True
114
115 ##
116 # A RightList object represents a list of privileges.
117
118 class RightList:
119     ##
120     # Create a new rightlist object, containing no rights.
121     #
122     # @param string if string!=None, load the rightlist from the string
123
124     def __init__(self, string=None):
125         self.rights = []
126         if string:
127             self.load_from_string(string)
128
129     def is_empty(self):
130         return self.rights == []
131
132     ##
133     # Add a right to this list
134     #
135     # @param right is either a Right object or a string describing the right
136
137     def add(self, right):
138         if isinstance(right, str):
139             right = Right(kind = right)
140         self.rights.append(right)
141
142     ##
143     # Load the rightlist object from a string
144
145     def load_from_string(self, string):
146         self.rights = []
147
148         # none == no rights, so leave the list empty
149         if not string:
150             return
151
152         parts = string.split(",")
153         for part in parts:
154             self.rights.append(Right(part))
155
156     ##
157     # Save the rightlist object to a string. It is saved in the format of a
158     # comma-separated list.
159
160     def save_to_string(self):
161         right_names = []
162         for right in self.rights:
163             right_names.append(right.kind)
164
165         return ",".join(right_names)
166
167     ##
168     # Check to see if some right in this list allows an operation. This is
169     # done by evaluating the can_perform function of each operation in the
170     # list.
171     #
172     # @param op_name is an operation to check, for example "listslices"
173
174     def can_perform(self, op_name):
175         for right in self.rights:
176             if right.can_perform(op_name):
177                 return True
178         return False
179
180     ##
181     # Check to see if all of the rights in this rightlist are a superset
182     # of all the rights in a child rightlist. A rightlist is a superset
183     # if there is no operation in the child rightlist that cannot be
184     # performed in the parent rightlist.
185     #
186     # @param child is a rightlist object describing the child
187
188     def is_superset(self, child):
189         for child_right in child.rights:
190             allowed = False
191             for my_right in self.rights:
192                 if my_right.is_superset(child_right):
193                     allowed = True
194             if not allowed:
195                 return False
196         return True
197
198
199     ##
200     # Determine tje rights that an object should have. The rights are entirely
201     # dependent on the type of the object. For example, users automatically
202     # get "refresh", "resolve", and "info".
203     #
204     # @param type the type of the object (user | sa | ma | slice | node)
205     # @param name human readable name of the object (not used at this time)
206     #
207     # @return RightList object containing rights
208
209     def determine_rights(self, type, name):
210         rl = RightList()
211
212         # rights seem to be somewhat redundant with the type of the credential.
213         # For example, a "sa" credential implies the authority right, because
214         # a sa credential cannot be issued to a user who is not an owner of
215         # the authority
216
217         if type == "user":
218             rl.add("refresh")
219             rl.add("resolve")
220             rl.add("info")
221         elif type == "sa":
222             rl.add("authority,sa")
223         elif type == "ma":
224             rl.add("authority,ma")
225         elif type == "slice":
226             rl.add("refresh")
227             rl.add("embed")
228             rl.add("bind")
229             rl.add("control")
230             rl.add("info")
231         elif type == "component":
232             rl.add("operator")
233
234         return rl