Setting tag sfa-2.0-10
[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     elif type == "component":
77         rl.add("operator")
78     return rl
79
80
81 ##
82 # The Right class represents a single privilege.
83
84
85
86 class Right:
87     ##
88     # Create a new right.
89     #
90     # @param kind is a string naming the right. For example "control"
91
92     def __init__(self, kind, delegate=False):
93         self.kind = kind
94         self.delegate = delegate
95
96     ##
97     # Test to see if this right object is allowed to perform an operation.
98     # Returns True if the operation is allowed, False otherwise.
99     #
100     # @param op_name is a string naming the operation. For example "listslices".
101
102     def can_perform(self, op_name):
103         allowed_ops = privilege_table.get(self.kind.lower(), None)
104         if not allowed_ops:
105             return False
106
107         # if "*" is specified, then all ops are permitted
108         if "*" in allowed_ops:
109             return True
110
111         return (op_name.lower() in allowed_ops)
112
113     ##
114     # Test to see if this right is a superset of a child right. A right is a
115     # superset if every operating that is allowed by the child is also allowed
116     # by this object.
117     #
118     # @param child is a Right object describing the child right
119
120     def is_superset(self, child):
121         my_allowed_ops = privilege_table.get(self.kind.lower(), None)
122         child_allowed_ops = privilege_table.get(child.kind.lower(), None)
123
124         if not self.delegate:
125             return False
126
127         if "*" in my_allowed_ops:
128             return True
129
130         for right in child_allowed_ops:
131             if not right in my_allowed_ops:
132                 return False
133
134         return True
135
136 ##
137 # A Rights object represents a list of privileges.
138
139 class Rights:
140     ##
141     # Create a new rightlist object, containing no rights.
142     #
143     # @param string if string!=None, load the rightlist from the string
144
145     def __init__(self, string=None):
146         self.rights = []
147         if string:
148             self.load_from_string(string)
149
150     def is_empty(self):
151         return self.rights == []
152
153     ##
154     # Add a right to this list
155     #
156     # @param right is either a Right object or a string describing the right
157
158     def add(self, right, delegate=False):
159         if isinstance(right, str):
160             right = Right(right, delegate)
161         self.rights.append(right)
162
163     ##
164     # Load the rightlist object from a string
165
166     def load_from_string(self, string):
167         self.rights = []
168
169         # none == no rights, so leave the list empty
170         if not string:
171             return
172
173         parts = string.split(",")
174         for part in parts:
175             if ':' in part:
176                 spl = part.split(':')
177                 kind = spl[0].strip()
178                 delegate = bool(int(spl[1]))
179             else:
180                 kind = part.strip()
181                 delegate = 0
182             self.rights.append(Right(kind, bool(delegate)))
183
184     ##
185     # Save the rightlist object to a string. It is saved in the format of a
186     # comma-separated list.
187
188     def save_to_string(self):
189         right_names = []
190         for right in self.rights:
191             right_names.append('%s:%d' % (right.kind.strip(), right.delegate))
192
193         return ",".join(right_names)
194
195     ##
196     # Check to see if some right in this list allows an operation. This is
197     # done by evaluating the can_perform function of each operation in the
198     # list.
199     #
200     # @param op_name is an operation to check, for example "listslices"
201
202     def can_perform(self, op_name):
203         
204         for right in self.rights:
205             if right.can_perform(op_name):
206                 return True
207         return False
208
209     ##
210     # Check to see if all of the rights in this rightlist are a superset
211     # of all the rights in a child rightlist. A rightlist is a superset
212     # if there is no operation in the child rightlist that cannot be
213     # performed in the parent rightlist.
214     #
215     # @param child is a rightlist object describing the child
216
217     def is_superset(self, child):
218         for child_right in child.rights:
219             allowed = False
220             for my_right in self.rights:
221                 if my_right.is_superset(child_right):
222                     allowed = True
223                     break
224             if not allowed:
225                 return False
226         return True
227
228
229     ##
230     # set the delegate bit to 'delegate' on
231     # all privileges
232     #
233     # @param delegate boolean (True or False)
234
235     def delegate_all_privileges(self, delegate):
236         for right in self.rights:
237             right.delegate = delegate
238
239     ##
240     # true if all privileges have delegate bit set true
241     # false otherwise
242
243     def get_all_delegate(self):
244         for right in self.rights:
245             if not right.delegate:
246                 return False
247         return True
248