1 | """NDG Security Condition type definition |
---|
2 | |
---|
3 | NERC DataGrid |
---|
4 | """ |
---|
5 | __author__ = "P J Kershaw" |
---|
6 | __date__ = "19/03/10" |
---|
7 | __copyright__ = "(C) 2010 Science and Technology Facilities Council" |
---|
8 | __contact__ = "Philip.Kershaw@stfc.ac.uk" |
---|
9 | __license__ = "BSD - see LICENSE file in top-level directory" |
---|
10 | __contact__ = "Philip.Kershaw@stfc.ac.uk" |
---|
11 | __revision__ = "$Id$" |
---|
12 | from ndg.xacml.utils import TypedList |
---|
13 | from ndg.xacml.core.expression import Expression |
---|
14 | from ndg.xacml.core.functions import (FunctionMap, functionMap, |
---|
15 | UnsupportedStdFunctionError, |
---|
16 | UnsupportedFunctionError) |
---|
17 | |
---|
18 | |
---|
19 | class Apply(Expression): |
---|
20 | """XACML Apply type |
---|
21 | |
---|
22 | @cvar ELEMENT_LOCAL_NAME: XML element local name |
---|
23 | @type ELEMENT_LOCAL_NAME: string |
---|
24 | @cvar FUNCTION_ID_ATTRIB_NAME: function ID XML attribute name |
---|
25 | @type FUNCTION_ID_ATTRIB_NAME: string |
---|
26 | |
---|
27 | @ivar __functionId: URN corresponding to function to be applied |
---|
28 | @type __functionId: basestring/NoneType |
---|
29 | @ivar __function: function to be applied |
---|
30 | @type __function: ndg.xacml.core.functions.AbstractFunction derived type |
---|
31 | @ivar __functionMap: function mapping object to map URNs to function class |
---|
32 | implementations |
---|
33 | @type __functionMap: ndg.xacml.core.functions.FunctionMap |
---|
34 | @ivar __loadFunctionFromId: boolean determines whether or not to load |
---|
35 | function classes for given function URN in functionId set property method |
---|
36 | @type __loadFunctionFromId: bool |
---|
37 | @ivar __expressions: list of expressions contained in the Apply statement |
---|
38 | @type __expressions: ndg.xacml.utils.TypedList |
---|
39 | """ |
---|
40 | ELEMENT_LOCAL_NAME = 'Apply' |
---|
41 | FUNCTION_ID_ATTRIB_NAME = 'FunctionId' |
---|
42 | |
---|
43 | __slots__ = ( |
---|
44 | '__functionId', |
---|
45 | '__function', |
---|
46 | '__functionMap', |
---|
47 | '__loadFunctionFromId', |
---|
48 | '__expressions' |
---|
49 | ) |
---|
50 | |
---|
51 | def __init__(self): |
---|
52 | """Initialise attributes""" |
---|
53 | super(Apply, self).__init__() |
---|
54 | self.__functionId = None |
---|
55 | self.__function = None |
---|
56 | self.__functionMap = functionMap |
---|
57 | self.__loadFunctionFromId = True |
---|
58 | self.__expressions = TypedList(Expression) |
---|
59 | |
---|
60 | @property |
---|
61 | def loadFunctionFromId(self): |
---|
62 | """Set to False to stop the functionId property set method automatically |
---|
63 | trying to load the corresponding function for the given functionId |
---|
64 | |
---|
65 | @return: flag setting |
---|
66 | @rtype: bool""" |
---|
67 | return self.__loadFunctionFromId |
---|
68 | |
---|
69 | @loadFunctionFromId.setter |
---|
70 | def loadFunctionFromId(self, value): |
---|
71 | """Set to False to stop the functionId property set method automatically |
---|
72 | trying to load the corresponding function for the given functionId |
---|
73 | |
---|
74 | @param value: flag setting |
---|
75 | @type value: bool |
---|
76 | @raise TypeError: incorrect input type |
---|
77 | """ |
---|
78 | if not isinstance(value, bool): |
---|
79 | raise TypeError('Expecting %r type for "loadFunctionFromId" ' |
---|
80 | 'attribute; got %r' % (bool, type(value))) |
---|
81 | |
---|
82 | self.__loadFunctionFromId = value |
---|
83 | |
---|
84 | def _get_functionId(self): |
---|
85 | """Get function ID |
---|
86 | @return: function ID for this Apply statement |
---|
87 | @rtype: basestring/NoneType |
---|
88 | """ |
---|
89 | return self.__functionId |
---|
90 | |
---|
91 | def _set_functionId(self, value): |
---|
92 | """Set function ID |
---|
93 | @param value: function URN |
---|
94 | @type value: basestring |
---|
95 | @raise TypeError: incorrect input type |
---|
96 | """ |
---|
97 | if not isinstance(value, basestring): |
---|
98 | raise TypeError('Expecting %r type for "functionId" ' |
---|
99 | 'attribute; got %r' % (basestring, type(value))) |
---|
100 | |
---|
101 | self.__functionId = value |
---|
102 | |
---|
103 | # Also retrieve function for this function ID if a map has been set |
---|
104 | if self.__loadFunctionFromId: |
---|
105 | self.setFunctionFromMap(self.__functionMap) |
---|
106 | |
---|
107 | functionId = property(_get_functionId, _set_functionId, None, |
---|
108 | "Apply type Function ID") |
---|
109 | |
---|
110 | def setFunctionFromMap(self, functionMap): |
---|
111 | """Set the function from a function map - a dictionary of function ID to |
---|
112 | function mappings. The function is looked up based on the "functionId" |
---|
113 | attribute. This method is automatically called when the functionId set |
---|
114 | property method is invoked. To switch off this behaviour set |
---|
115 | |
---|
116 | loadFunctionFromId = False |
---|
117 | |
---|
118 | @param functionMap: function mapping object to map URNs to function |
---|
119 | class implementations |
---|
120 | @type functionMap: ndg.xacml.core.functions.FunctionMap |
---|
121 | |
---|
122 | @raise UnsupportedStdFunctionError: policy references a function type |
---|
123 | which is in the XACML spec. but is not supported by this implementation |
---|
124 | @raise UnsupportedFunctionError: policy references a function type which |
---|
125 | is not supported by this implementation |
---|
126 | """ |
---|
127 | if self.functionId is None: |
---|
128 | raise AttributeError('"functionId" attribute must be set in order ' |
---|
129 | 'to retrieve the required function') |
---|
130 | |
---|
131 | # Get function class for this <Apply> statement |
---|
132 | functionClass = functionMap.get(self.functionId) |
---|
133 | if functionClass is NotImplemented: |
---|
134 | raise UnsupportedStdFunctionError('No match function class ' |
---|
135 | 'implemented for MatchId="%s"' % |
---|
136 | self.functionId) |
---|
137 | elif functionClass is None: |
---|
138 | raise UnsupportedFunctionError('<Apply> function namespace %r is ' |
---|
139 | 'not recognised' % |
---|
140 | self.functionId) |
---|
141 | |
---|
142 | self.__function = functionClass() |
---|
143 | |
---|
144 | @property |
---|
145 | def functionMap(self): |
---|
146 | """functionMap object for PDP to retrieve functions from given XACML |
---|
147 | function URNs |
---|
148 | @return: function mapping object |
---|
149 | @rtype: ndg.xacml.core.functions.FunctionMap |
---|
150 | """ |
---|
151 | return self.__functionMap |
---|
152 | |
---|
153 | @functionMap.setter |
---|
154 | def functionMap(self, value): |
---|
155 | '''functionMap object for PDP to retrieve functions from given XACML |
---|
156 | function URNs |
---|
157 | @param value: function mapping object to map URNs to function |
---|
158 | class implementations |
---|
159 | @type value: ndg.xacml.core.functions.FunctionMap |
---|
160 | @raise TypeError: incorrect type for input value |
---|
161 | ''' |
---|
162 | if not isinstance(value, FunctionMap): |
---|
163 | raise TypeError('Expecting %r derived type for "functionMap" ' |
---|
164 | 'input; got %r instead' % (FunctionMap, |
---|
165 | type(value))) |
---|
166 | self.__functionMap = value |
---|
167 | |
---|
168 | @property |
---|
169 | def function(self): |
---|
170 | """Get Function for this <Apply> instance |
---|
171 | @return: function to be applied |
---|
172 | @rtype: ndg.xacml.core.functions.AbstractFunction derived type |
---|
173 | """ |
---|
174 | return self.__function |
---|
175 | |
---|
176 | @property |
---|
177 | def expressions(self): |
---|
178 | """List of expression sub-elements |
---|
179 | @return: list of expressions contained in the Apply statement |
---|
180 | @rtype: ndg.xacml.utils.TypedList |
---|
181 | """ |
---|
182 | return self.__expressions |
---|
183 | |
---|
184 | def evaluate(self, context): |
---|
185 | """Evaluate a given <Apply> statement in a rule condition |
---|
186 | |
---|
187 | @param context: the request context |
---|
188 | @type context: ndg.xacml.core.context.request.Request |
---|
189 | @return: attribute value(s) resulting from execution of this expression |
---|
190 | in a condition |
---|
191 | @rtype: AttributeValue/NoneType |
---|
192 | """ |
---|
193 | |
---|
194 | # Marshal inputs |
---|
195 | funcInputs = [None]*len(self.expressions) |
---|
196 | |
---|
197 | for i, expression in enumerate(self.expressions): |
---|
198 | funcInputs[i] = expression.evaluate(context) |
---|
199 | |
---|
200 | # Execute function on the retrieved inputs |
---|
201 | result = self.function.evaluate(*tuple(funcInputs)) |
---|
202 | |
---|
203 | # Pass the result back to the parent <Apply> element |
---|
204 | return result |
---|
205 | |
---|