Lots of credential updates.. can actually perform list now. Delegation/parents/verif...
[sfa.git] / sfa / trust / rights.py
1 ##
2 # This Module implements rights and lists of rights for the SFA. 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 ##
15 # privilege_table is a list of priviliges and what operations are allowed
16 # per privilege.
17
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"]}
30
31
32 ##
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".
36 #
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)
39 #
40 # @return RightList object containing rights
41
42 def determine_rights(type, name):
43     rl = RightList()
44
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
48     # the authority
49     if type == "user":
50         rl.add("refresh")
51         rl.add("resolve")
52         rl.add("info")
53     elif type == "sa":
54         rl.add("authority,sa")
55     elif type == "ma":
56         rl.add("authority,ma")
57     elif type == "authority":
58         rl.add("authority,sa,ma")
59     elif type == "slice":
60         rl.add("refresh")
61         rl.add("embed")
62         rl.add("bind")
63         rl.add("control")
64         rl.add("info")
65     elif type == "component":
66         rl.add("operator")
67     return rl
68
69
70 ##
71 # The Right class represents a single privilege.
72
73
74
75 class Right:
76    ##
77    # Create a new right.
78    #
79    # @param kind is a string naming the right. For example "control"
80
81    def __init__(self, kind):
82       self.kind = kind
83
84    ##
85    # Test to see if this right object is allowed to perform an operation.
86    # Returns True if the operation is allowed, False otherwise.
87    #
88    # @param op_name is a string naming the operation. For example "listslices".
89
90    def can_perform(self, op_name):
91       allowed_ops = privilege_table.get(self.kind.lower(), None)
92       if not allowed_ops:
93          return False
94
95       # if "*" is specified, then all ops are permitted
96       if "*" in allowed_ops:
97          return True
98
99       return (op_name.lower() in allowed_ops)
100
101    ##
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
104    # by this object.
105    #
106    # @param child is a Right object describing the child right
107
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)
111
112       if "*" in my_allowed_ops:
113           return True
114
115       for right in child_allowed_ops:
116           if not right in my_allowed_ops:
117               return False
118
119       return True
120
121 ##
122 # A RightList object represents a list of privileges.
123
124 class RightList:
125     ##
126     # Create a new rightlist object, containing no rights.
127     #
128     # @param string if string!=None, load the rightlist from the string
129
130     def __init__(self, string=None):
131         self.rights = []
132         if string:
133             self.load_from_string(string)
134
135     def is_empty(self):
136         return self.rights == []
137
138     ##
139     # Add a right to this list
140     #
141     # @param right is either a Right object or a string describing the right
142
143     def add(self, right):
144         if isinstance(right, str):
145             right = Right(kind = right)
146         self.rights.append(right)
147
148     ##
149     # Load the rightlist object from a string
150
151     def load_from_string(self, string):
152         self.rights = []
153
154         # none == no rights, so leave the list empty
155         if not string:
156             return
157
158         parts = string.split(",")
159         for part in parts:
160             self.rights.append(Right(part))
161
162     ##
163     # Save the rightlist object to a string. It is saved in the format of a
164     # comma-separated list.
165
166     def save_to_string(self):
167         right_names = []
168         for right in self.rights:
169             right_names.append(right.kind)
170
171         return ",".join(right_names)
172
173     ##
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
176     # list.
177     #
178     # @param op_name is an operation to check, for example "listslices"
179
180     def can_perform(self, op_name):
181         for right in self.rights:
182             if right.can_perform(op_name):
183                 return True
184         return False
185
186     ##
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.
191     #
192     # @param child is a rightlist object describing the child
193
194     def is_superset(self, child):
195         for child_right in child.rights:
196             allowed = False
197             for my_right in self.rights:
198                 if my_right.is_superset(child_right):
199                     allowed = True
200             if not allowed:
201                 return False
202         return True
203
204
205     ##
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".
209     #
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)
212     #
213     # @return RightList object containing rights
214
215     def determine_rights(self, type, name):
216         rl = RightList()
217
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
221         # the authority
222
223         if type == "user":
224             rl.add("refresh")
225             rl.add("resolve")
226             rl.add("info")
227         elif type == "sa":
228             rl.add("authority,sa")
229         elif type == "ma":
230             rl.add("authority,ma")
231         elif type == "authority":
232             rl.add("authority,sa,ma")
233         elif type == "slice":
234             rl.add("refresh")
235             rl.add("embed")
236             rl.add("bind")
237             rl.add("control")
238             rl.add("info")
239         elif type == "component":
240             rl.add("operator")
241
242         return rl