merge master again (2.0-10 changelog only)
[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 # Rights - 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 ##
16 # privilege_table is a list of priviliges and what operations are allowed
17 # per privilege.
18 # Note that "*" is a privilege granted by ProtoGENI slice authorities, and we
19 # give it access to the GENI AM calls
20
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"]} 
36
37
38
39 ##
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".
43 #
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)
46 #
47 # @return Rights object containing rights
48
49 def determine_rights(type, name):
50     rl = Rights()
51
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
55     # the authority
56     if type == "user":
57         rl.add("refresh")
58         rl.add("resolve")
59         rl.add("info")
60     elif type in ["sa", "authority+sa"]:
61         rl.add("authority")
62         rl.add("sa")
63     elif type in ["ma", "authority+ma", "cm", "authority+cm", "sm", "authority+sm"]:
64         rl.add("authority")
65         rl.add("ma")
66     elif type == "authority":
67         rl.add("authority")
68         rl.add("sa")
69         rl.add("ma")
70     elif type == "slice":
71         rl.add("refresh")
72         rl.add("embed")
73         rl.add("bind")
74         rl.add("control")
75         rl.add("info")
76 # wouldn't that be authority+cm instead ?
77     elif type == "component":
78         rl.add("operator")
79     return rl
80
81
82 ##
83 # The Right class represents a single privilege.
84
85
86
87 class Right:
88     ##
89     # Create a new right.
90     #
91     # @param kind is a string naming the right. For example "control"
92
93     def __init__(self, kind, delegate=False):
94         self.kind = kind
95         self.delegate = delegate
96
97     ##
98     # Test to see if this right object is allowed to perform an operation.
99     # Returns True if the operation is allowed, False otherwise.
100     #
101     # @param op_name is a string naming the operation. For example "listslices".
102
103     def can_perform(self, op_name):
104         allowed_ops = privilege_table.get(self.kind.lower(), None)
105         if not allowed_ops:
106             return False
107
108         # if "*" is specified, then all ops are permitted
109         if "*" in allowed_ops:
110             return True
111
112         return (op_name.lower() in allowed_ops)
113
114     ##
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
117     # by this object.
118     #
119     # @param child is a Right object describing the child right
120
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)
124
125         if not self.delegate:
126             return False
127
128         if "*" in my_allowed_ops:
129             return True
130
131         for right in child_allowed_ops:
132             if not right in my_allowed_ops:
133                 return False
134
135         return True
136
137 ##
138 # A Rights object represents a list of privileges.
139
140 class Rights:
141     ##
142     # Create a new rightlist object, containing no rights.
143     #
144     # @param string if string!=None, load the rightlist from the string
145
146     def __init__(self, string=None):
147         self.rights = []
148         if string:
149             self.load_from_string(string)
150
151     def is_empty(self):
152         return self.rights == []
153
154     ##
155     # Add a right to this list
156     #
157     # @param right is either a Right object or a string describing the right
158
159     def add(self, right, delegate=False):
160         if isinstance(right, str):
161             right = Right(right, delegate)
162         self.rights.append(right)
163
164     ##
165     # Load the rightlist object from a string
166
167     def load_from_string(self, string):
168         self.rights = []
169
170         # none == no rights, so leave the list empty
171         if not string:
172             return
173
174         parts = string.split(",")
175         for part in parts:
176             if ':' in part:
177                 spl = part.split(':')
178                 kind = spl[0].strip()
179                 delegate = bool(int(spl[1]))
180             else:
181                 kind = part.strip()
182                 delegate = 0
183             self.rights.append(Right(kind, bool(delegate)))
184
185     ##
186     # Save the rightlist object to a string. It is saved in the format of a
187     # comma-separated list.
188
189     def save_to_string(self):
190         right_names = []
191         for right in self.rights:
192             right_names.append('%s:%d' % (right.kind.strip(), right.delegate))
193
194         return ",".join(right_names)
195
196     ##
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
199     # list.
200     #
201     # @param op_name is an operation to check, for example "listslices"
202
203     def can_perform(self, op_name):
204         
205         for right in self.rights:
206             if right.can_perform(op_name):
207                 return True
208         return False
209
210     ##
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.
215     #
216     # @param child is a rightlist object describing the child
217
218     def is_superset(self, child):
219         for child_right in child.rights:
220             allowed = False
221             for my_right in self.rights:
222                 if my_right.is_superset(child_right):
223                     allowed = True
224                     break
225             if not allowed:
226                 return False
227         return True
228
229
230     ##
231     # set the delegate bit to 'delegate' on
232     # all privileges
233     #
234     # @param delegate boolean (True or False)
235
236     def delegate_all_privileges(self, delegate):
237         for right in self.rights:
238             right.delegate = delegate
239
240     ##
241     # true if all privileges have delegate bit set true
242     # false otherwise
243
244     def get_all_delegate(self):
245         for right in self.rights:
246             if not right.delegate:
247                 return False
248         return True
249