Changeset 6806
- Timestamp:
- 14/04/10 16:39:48 (11 years ago)
- Location:
- TI12-security/trunk/NDG_XACML/ndg/xacml/core
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
TI12-security/trunk/NDG_XACML/ndg/xacml/core/condition.py
r6796 r6806 39 39 type(value)) 40 40 self.__expression = value 41 42 def evaluate(self, context): 43 """Evaluate this rule condition 44 @param context: the request context 45 @type context: ndg.xacml.core.request.Request 46 @return: True/False status for whether the rule condition matched or 47 not 48 @rtype: bool 49 """ 50 result = self.expression.evaluate(context) 51 52 return result -
TI12-security/trunk/NDG_XACML/ndg/xacml/core/context/pdp.py
r6805 r6806 23 23 from ndg.xacml.core.context.result import Result, Decision 24 24 from ndg.xacml.core.context.result import StatusCode 25 from ndg.xacml.core.functions import FunctionMap26 25 from ndg.xacml.core.exceptions import (UnsupportedStdFunctionError, 27 26 UnsupportedFunctionError) … … 50 49 """ 51 50 __slots__ = ('__policy', '__request') 52 TARGET_CHILD_ATTRS = ('subjects', 'resources', 'actions', 'environments')53 51 54 52 def __init__(self, policy=None): … … 61 59 if policy is not None: 62 60 self.policy = policy 63 64 self.__functionMap = FunctionMap.withLoadedMap()65 61 66 62 self.__request = None … … 133 129 log.debug('Checking policy target for match with request...') 134 130 131 target = self.policy.target 135 132 136 if not self.matchTarget(self.policy.target, request): 133 if target is None: 134 log.debug('No target set so no match with request context') 135 result.decision = Decision.NOT_APPLICABLE 136 return response 137 138 if not target.match(request): 137 139 log.debug('No match for policy target setting %r decision', 138 140 Decision.NOT_APPLICABLE_STR) … … 146 148 ruleStatusValues = [False]*len(self.policy.rules) 147 149 for i, rule in enumerate(self.policy.rules): 148 log.debug('Checking policy rule %r for match...', rule.id) 149 if not self.matchTarget(rule.target, request): 150 log.debug('No match to request context for target in rule ' 151 '%r', rule.id) 152 ruleStatusValues[i] = True 153 continue 154 155 # Apply the condition 156 ruleStatusValues[i] = self.evaluateCondition(rule.condition) 150 ruleStatusValues[i] = rule.evaluate() 151 # log.debug('Checking policy rule %r for match...', rule.id) 152 # if not self.matchTarget(rule.target, request): 153 # log.debug('No match to request context for target in rule ' 154 # '%r', rule.id) 155 # ruleStatusValues[i] = True 156 # continue 157 # 158 # # Apply the condition 159 # ruleStatusValues[i] = self.evaluateCondition(rule.condition) 157 160 158 161 except PDPError, e: … … 176 179 177 180 return response 178 179 def matchTarget(self, target, request):180 """Generic method to match a <Target> element to the request context181 182 @param target: XACML target element183 @type target: ndg.xacml.core.target.Target184 @param request: XACML request context185 @type request: ndg.xacml.core.context.request.Request186 @return: True if request context matches the given target,187 False otherwise188 @rtype: bool189 """190 if target is None:191 log.debug('No target set so no match with request context')192 return False193 194 # From section 5.5 of the XACML 2.0 Core Spec:195 #196 # For the parent of the <Target> element to be applicable to the197 # decision request, there MUST be at least one positive match between198 # each section of the <Target> element and the corresponding section of199 # the <xacml-context:Request> element.200 #201 # Also, 7.6:202 #203 # The target value SHALL be "Match" if the subjects, resources, actions204 # and environments specified in the target all match values in the205 # request context.206 statusValues = [False]*len(self.__class__.TARGET_CHILD_ATTRS)207 208 # Iterate for target subjects, resources, actions and environments209 # elements210 for i, attrName in enumerate(self.__class__.TARGET_CHILD_ATTRS):211 # If any one of the <Target> children is missing then it counts as212 # a match e.g. for <Subjects> child element - Section 5.5:213 #214 # <Subjects> [Optional] Matching specification for the subject215 # attributes in the context. If this element is missing,216 # then the target SHALL match all subjects.217 targetElem = getattr(target, attrName)218 if len(targetElem) == 0:219 statusValues[i] = True220 continue221 222 # Iterate over each for example, subject in the list of subjects:223 # <Target>224 # <Subjects>225 # <Subject>226 # ...227 # </Subject>228 # <Subject>229 # ...230 # </Subject>231 # ...232 # or resource in the list of resources and so on233 for targetSubElem in targetElem:234 235 # For the given subject/resource/action/environment check for a236 # match with the equivalent in the request237 requestElem = getattr(request, attrName)238 for requestSubElem in requestElem:239 if self.matchTargetChild(targetSubElem, requestSubElem):240 # Within the list of e.g. subjects if one subject241 # matches then this counts as a subject match overall242 # for this target243 statusValues[i] = True244 245 # Target matches if all the children (i.e. subjects, resources, actions246 # and environment sections) have at least one match. Otherwise it247 # doesn't count as a match248 return all(statusValues)249 250 def matchTargetChild(self, targetChild, requestChild):251 """Match a request child element (a <Subject>, <Resource>, <Action> or252 <Environment>) with the corresponding target's <Subject>, <Resource>,253 <Action> or <Environment>.254 255 @param targetChild: Target Subject, Resource, Action or Environment256 object257 @type targetChild: ndg.xacml.core.TargetChildBase258 @param requestChild: Request Subject, Resource, Action or Environment259 object260 @type requestChild: ndg.xacml.core.context.RequestChildBase261 @return: True if request context matches something in the target262 @rtype: bool263 @raise UnsupportedElementError: AttributeSelector processing is not264 currently supported. If an AttributeSelector is found in the policy,265 this exception will be raised.266 @raise UnsupportedStdFunctionError: policy references a function type267 which is in the XACML spec. but is not supported by this implementation268 @raise UnsupportedFunctionError: policy references a function type which269 is not supported by this implementation270 """271 if targetChild is None:272 # Default if target child is not set is to match all children273 return True274 275 matchStatusValues = [True]*len(targetChild.matches)276 277 # Section 7.6278 #279 # A subject, resource, action or environment SHALL match a value in the280 # request context if the value of all its <SubjectMatch>,281 # <ResourceMatch>, <ActionMatch> or <EnvironmentMatch> elements,282 # respectively, are "True".283 #284 # e.g. for <SubjectMatch>es in <Subject> ...285 for childMatch, matchStatus in zip(targetChild.matches,286 matchStatusValues):287 288 # Get the match function from the Match ID289 matchFunctionClass = self.__functionMap.get(childMatch.matchId)290 if matchFunctionClass is NotImplemented:291 raise UnsupportedStdFunctionError('No match function class '292 'implemented for MatchId="%s"'293 % childMatch.matchId)294 elif matchFunctionClass is None:295 raise UnsupportedFunctionError('Match function namespace %r is '296 'not recognised' %297 childMatch.matchId)298 299 matchAttributeValue = childMatch.attributeValue.value300 301 # Create a match function based on the presence or absence of an302 # AttributeDesignator or AttributeSelector303 if childMatch.attributeDesignator is not None:304 _attributeMatch = self.attributeDesignatorMatchFuncFactory(305 matchFunctionClass(),306 childMatch.attributeValue,307 childMatch.attributeDesignator)308 309 elif childMatch.attributeSelector is not None:310 # Nb. This will require that the request provide a reference to311 # it's XML representation and an abstraction of the XML parser312 # for executing XPath searches into that representation313 raise UnsupportedElementError('This PDP implementation does '314 'not support <AttributeSelector> '315 'elements')316 else:317 _attributeMatch = lambda requestChildAttribute: (318 matchFunc.evaluate(matchAttributeValue,319 requestChildAttribute.attributeValue.value)320 )321 322 # Iterate through each attribute in the request in turn matching it323 # against the target using the generated _attributeMatch function324 #325 # Any Match element NOT matching will result in an overall status of326 # no match.327 #328 # Continue iterating through the whole list even if a False status329 # is found. The other attributes need to be checked in case an330 # error occurs. In this case the top-level PDP exception handling331 # block will catch it and set an overall decision of INDETERMINATE332 attrMatchStatusValues = [False]*len(requestChild.attributes)333 334 for attribute, attrMatchStatus in zip(requestChild.attributes,335 attrMatchStatusValues):336 attrMatchStatus = _attributeMatch(attribute)337 if attrMatchStatus == True:338 if log.getEffectiveLevel() <= logging.DEBUG:339 log.debug('Request attribute %r set to %r matches '340 'target',341 attribute.attributeId,342 [a.value for a in attribute.attributeValues])343 344 matchStatus = all(attrMatchStatusValues)345 346 # Any match => overall match347 return any(matchStatusValues)348 349 def attributeDesignatorMatchFuncFactory(self,350 matchFunc,351 matchAttributeValue,352 attributeDesignator):353 """Define a match function to match a given request attribute against354 the input attribute value and AttributeDesignator defined in a policy355 target356 """357 attributeId = attributeDesignator.attributeId358 dataType = attributeDesignator.dataType359 360 # Issuer is an optional match - see core spec. 7.2.4361 issuer = attributeDesignator.issuer362 if issuer is not None:363 # Issuer found - set lambda to match this against the364 # issuer setting in the request365 _issuerMatch = lambda requestChildIssuer: (366 issuer == requestChildIssuer)367 else:368 # No issuer set - lambda returns True regardless369 _issuerMatch = lambda requestChildIssuer: True370 371 372 _attributeMatch = lambda attribute: (373 any([matchFunc.evaluate(matchAttributeValue, attrVal)374 for attrVal in attribute.attributeValues]) and375 attribute.attributeId == attributeId and376 attribute.dataType == dataType and377 _issuerMatch(attribute.issuer)378 )379 380 return _attributeMatch381 382 def evaluateCondition(self, condition):383 """Evaluate a rule condition384 @param condition: rule condition385 @type condition: ndg.xacml.core.condition.Condition386 @return: True/False status for whether the rule condition matched or387 not388 @rtype: bool389 """390 391 # Spec:392 #393 # "The condition value SHALL be "True" if the <Condition> element is394 # absent"395 if condition is None:396 return True397 398 applyElem = condition.expression399 if not isinstance(applyElem, Apply):400 raise UnsupportedElementError('%r type <Condition> sub-element '401 'processing is not supported in this '402 'implementation' % applyElem)403 404 result = applyElem.evaluate(self.request)405 406 return result407 181 408 182 -
TI12-security/trunk/NDG_XACML/ndg/xacml/core/match.py
r6774 r6806 86 86 matchId = property(_getMatchId, _setMatchId, None, "Match Id") 87 87 88 def evaluate(self): 89 """Evaluate the match object against the relevant element in the request 90 context 91 """ 92 # Get the match function from the Match ID 93 # matchFunctionClass = self.__functionMap.get(self.matchId) 94 # if matchFunctionClass is NotImplemented: 95 # raise UnsupportedStdFunctionError('No match function class ' 96 # 'implemented for MatchId="%s"' 97 # % self.matchId) 98 # elif matchFunctionClass is None: 99 # raise UnsupportedFunctionError('Match function namespace %r is ' 100 # 'not recognised' % 101 # self.matchId) 102 # 103 # matchAttributeValue = self.attributeValue.value 104 105 # Create a match function based on the presence or absence of an 106 # AttributeDesignator or AttributeSelector 107 if self.attributeDesignator is not None: 108 # _attributeMatch = self.attributeDesignatorMatchFuncFactory( 109 # matchFunctionClass(), 110 # self.attributeValue, 111 # self.attributeDesignator) 112 113 elif self.attributeSelector is not None: 114 # Nb. This will require that the request provide a reference to 115 # it's XML representation and an abstraction of the XML parser 116 # for executing XPath searches into that representation 117 raise UnsupportedElementError('This PDP implementation does ' 118 'not support <AttributeSelector> ' 119 'elements') 120 else: 121 _attributeMatch = lambda requestChildAttribute: ( 122 matchFunc.evaluate(matchAttributeValue, 123 requestChildAttribute.attributeValue.value) 124 ) 125 126 # Iterate through each attribute in the request in turn matching it 127 # against the target using the generated _attributeMatch function 128 # 129 # Any Match element NOT matching will result in an overall status of 130 # no match. 131 # 132 # Continue iterating through the whole list even if a False status 133 # is found. The other attributes need to be checked in case an 134 # error occurs. In this case the top-level PDP exception handling 135 # block will catch it and set an overall decision of INDETERMINATE 136 attrMatchStatusValues = [False]*len(requestChild.attributes) 137 138 for attribute, attrMatchStatus in zip(requestChild.attributes, 139 attrMatchStatusValues): 140 attrMatchStatus = _attributeMatch(attribute) 141 if attrMatchStatus == True: 142 if log.getEffectiveLevel() <= logging.DEBUG: 143 log.debug('Request attribute %r set to %r matches ' 144 'target', 145 attribute.attributeId, 146 [a.value for a in attribute.attributeValues]) 147 148 matchStatus = all(attrMatchStatusValues) 149 150 return matchStatus 151 88 152 89 153 class SubjectMatch(MatchBase): -
TI12-security/trunk/NDG_XACML/ndg/xacml/core/rule.py
r6790 r6806 166 166 raise TypeError('Expecting %r type for "id" ' 167 167 'attribute; got %r' % (basestring, type(value))) 168 168 169 169 self.__id = value 170 170 … … 195 195 description = property(_getDescription, _setDescription, 196 196 doc="Rule Description text") 197 198 def evaluate(self, context): 199 """Evaluate a rule 200 @param context: the request context 201 @type context: ndg.xacml.core.request.Request 202 @return: result of the evaluation - the decision for this rule 203 @rtype: 204 """ 205 log.debug('Evaluating rule %r ...', rule.id) 206 207 if not self.matchTarget(self.target, request): 208 log.debug('No match to request context for target in rule ' 209 '%r', self.id) 210 #ruleStatusValues[i] = True 211 continue 212 213 # Apply the condition 214 ruleStatusValues[i] = self.condition.evaluate() 215 -
TI12-security/trunk/NDG_XACML/ndg/xacml/core/target.py
r6770 r6806 16 16 __contact__ = "Philip.Kershaw@stfc.ac.uk" 17 17 __revision__ = "$Id: $" 18 import logging 19 log = logging.getLogger(__name__) 20 18 21 from ndg.xacml.core import XacmlCoreBase 19 22 from ndg.xacml.core.action import Action … … 24 27 25 28 class Target(XacmlCoreBase): 29 """XACML Target element""" 26 30 ELEMENT_LOCAL_NAME = "Target" 27 31 SUBJECTS_ELEMENT_LOCAL_NAME = "Subjects" … … 29 33 RESOURCES_ELEMENT_LOCAL_NAME = "Resources" 30 34 ENVIRONMENTS_ELEMENT_LOCAL_NAME = "Environments" 31 35 CHILD_ATTRS = ('subjects', 'resources', 'actions', 'environments') 36 32 37 __slots__ = ('__subjects', '__resources', '__actions', '__environments') 33 38 34 39 def __init__(self): 40 """Initial attributes""" 35 41 self.__subjects = TypedList(Subject) 36 42 self.__resources = TypedList(Resource) … … 53 59 def environments(self): 54 60 return self.__environments 55 56 57 class _Target(XacmlCoreBase): 58 """Define access behaviour for a resource match a given URI pattern""" 59 URI_PATTERN_LOCALNAME = "URIPattern" 60 ATTRIBUTES_LOCALNAME = "Attributes" 61 ATTRIBUTE_AUTHORITY_LOCALNAME = "AttributeAuthority" 62 63 __slots__ = ( 64 '__uriPattern', 65 '__attributes', 66 '__regEx' 67 ) 68 69 def __init__(self): 70 super(Target, self).__init__() 71 self.__uriPattern = None 72 self.__attributes = [] 73 self.__regEx = None 74 75 def getUriPattern(self): 76 return self.__uriPattern 77 78 def setUriPattern(self, value): 79 if not isinstance(value, basestring): 80 raise TypeError('Expecting string type for "uriPattern" ' 81 'attribute; got %r' % type(value)) 82 self.__uriPattern = value 83 84 uriPattern = property(getUriPattern, 85 setUriPattern, 86 doc="URI Pattern to match this target") 87 88 def getAttributes(self): 89 return self.__attributes 90 91 def setAttributes(self, value): 92 if (not isinstance(value, TypedList) and 93 not issubclass(value.elementType, Attribute.__class__)): 94 raise TypeError('Expecting TypedList(Attribute) for "attributes" ' 95 'attribute; got %r' % type(value)) 96 self.__attributes = value 97 98 attributes = property(getAttributes, 99 setAttributes, 100 doc="Attributes restricting access to this target") 101 102 def getRegEx(self): 103 return self.__regEx 104 105 def setRegEx(self, value): 106 self.__regEx = value 107 108 regEx = property(getRegEx, setRegEx, doc="RegEx's Docstring") 109 110 def parse(self, root): 111 112 self.xmlns = QName.getNs(root.tag) 113 version1_0attributeAuthorityURI = None 114 115 for elem in root: 116 localName = QName.getLocalPart(elem.tag) 117 if localName == Target.URI_PATTERN_LOCALNAME: 118 self.uriPattern = elem.text.strip() 119 self.regEx = re.compile(self.uriPattern) 61 62 def match(self, request): 63 """Generic method to match a <Target> element to the request context 64 65 @param target: XACML target element 66 @type target: ndg.xacml.core.target.Target 67 @param request: XACML request context 68 @type request: ndg.xacml.core.context.request.Request 69 @return: True if request context matches the given target, 70 False otherwise 71 @rtype: bool 72 """ 73 74 # From section 5.5 of the XACML 2.0 Core Spec: 75 # 76 # For the parent of the <Target> element to be applicable to the 77 # decision request, there MUST be at least one positive match between 78 # each section of the <Target> element and the corresponding section of 79 # the <xacml-context:Request> element. 80 # 81 # Also, 7.6: 82 # 83 # The target value SHALL be "Match" if the subjects, resources, actions 84 # and environments specified in the target all match values in the 85 # request context. 86 statusValues = [False]*len(self.__class__.CHILD_ATTRS) 87 88 # Iterate for target subjects, resources, actions and environments 89 # elements 90 for i, attrName in enumerate(self.__class__.CHILD_ATTRS): 91 # If any one of the <Target> children is missing then it counts as 92 # a match e.g. for <Subjects> child element - Section 5.5: 93 # 94 # <Subjects> [Optional] Matching specification for the subject 95 # attributes in the context. If this element is missing, 96 # then the target SHALL match all subjects. 97 targetElem = getattr(self, attrName) 98 if len(targetElem) == 0: 99 statusValues[i] = True 100 continue 101 102 # Iterate over each for example, subject in the list of subjects: 103 # <Target> 104 # <Subjects> 105 # <Subject> 106 # ... 107 # </Subject> 108 # <Subject> 109 # ... 110 # </Subject> 111 # ... 112 # or resource in the list of resources and so on 113 for targetSubElem in targetElem: 120 114 121 elif localName == Target.ATTRIBUTES_LOCALNAME: 122 for attrElem in elem: 123 if self.xmlns == Target.VERSION_1_1_XMLNS: 124 self.attributes.append(Attribute.Parse(attrElem)) 125 else: 126 attribute = Attribute() 127 attribute.name = attrElem.text.strip() 128 self.attributes.append(attribute) 129 130 elif localName == Target.ATTRIBUTE_AUTHORITY_LOCALNAME: 131 # Expecting first element to contain the URI 132 warnings.warn( 133 Target.ATTRIBUTE_AUTHORITY_LOCALNAME_DEPRECATED_MSG, 134 PendingDeprecationWarning) 115 # For the given subject/resource/action/environment check for a 116 # match with the equivalent in the request 117 requestElem = getattr(request, attrName) 118 for requestSubElem in requestElem: 119 if self._matchChild(targetSubElem, requestSubElem): 120 # Within the list of e.g. subjects if one subject 121 # matches then this counts as a subject match overall 122 # for this target 123 statusValues[i] = True 124 125 # Target matches if all the children (i.e. subjects, resources, actions 126 # and environment sections) have at least one match. Otherwise it 127 # doesn't count as a match 128 return all(statusValues) 129 130 def _matchChild(self, targetChild, requestChild): 131 """Match a request child element (a <Subject>, <Resource>, <Action> or 132 <Environment>) with the corresponding target's <Subject>, <Resource>, 133 <Action> or <Environment>. 134 135 @param targetChild: Target Subject, Resource, Action or Environment 136 object 137 @type targetChild: ndg.xacml.core.TargetChildBase 138 @param requestChild: Request Subject, Resource, Action or Environment 139 object 140 @type requestChild: ndg.xacml.core.context.RequestChildBase 141 @return: True if request context matches something in the target 142 @rtype: bool 143 @raise UnsupportedElementError: AttributeSelector processing is not 144 currently supported. If an AttributeSelector is found in the policy, 145 this exception will be raised. 146 @raise UnsupportedStdFunctionError: policy references a function type 147 which is in the XACML spec. but is not supported by this implementation 148 @raise UnsupportedFunctionError: policy references a function type which 149 is not supported by this implementation 150 """ 151 if targetChild is None: 152 # Default if target child is not set is to match all children 153 return True 154 155 matchStatusValues = [True]*len(targetChild.matches) 156 157 # Section 7.6 158 # 159 # A subject, resource, action or environment SHALL match a value in the 160 # request context if the value of all its <SubjectMatch>, 161 # <ResourceMatch>, <ActionMatch> or <EnvironmentMatch> elements, 162 # respectively, are "True". 163 # 164 # e.g. for <SubjectMatch>es in <Subject> ... 165 for childMatch, matchStatus in zip(targetChild.matches, 166 matchStatusValues): 167 168 matchStatusValues[i] = childMatch.evaluate(requestChild) 169 170 # Any match => overall match 171 return any(matchStatusValues) 172 135 173 136 version1_0attributeAuthorityURI = elem[-1].text.strip() 137 else: 138 raise TargetParseError("Invalid Target attribute: %s" % 139 localName) 140 141 if self.xmlns == Target.VERSION_1_0_XMLNS: 142 msg = ("Setting all attributes with Attribute Authority " 143 "URI set read using Version 1.0 schema. This will " 144 "be deprecated in future releases") 145 146 warnings.warn(msg, PendingDeprecationWarning) 147 log.warning(msg) 148 149 if version1_0attributeAuthorityURI is None: 150 raise TargetParseError("Assuming version 1.0 schema " 151 "for Attribute Authority URI setting " 152 "but no URI has been set") 153 154 for attribute in self.attributes: 155 attribute.attributeAuthorityURI = \ 156 version1_0attributeAuthorityURI 157 158 @classmethod 159 def Parse(cls, root): 160 resource = cls() 161 resource.parse(root) 162 return resource 163 164 def __str__(self): 165 return str(self.uriPattern) 166 174 def attributeDesignatorMatchFuncFactory(self, 175 matchFunc, 176 matchAttributeValue, 177 attributeDesignator): 178 """Define a match function to match a given request attribute against 179 the input attribute value and AttributeDesignator defined in a policy 180 target 181 """ 182 attributeId = attributeDesignator.attributeId 183 dataType = attributeDesignator.dataType 184 185 # Issuer is an optional match - see core spec. 7.2.4 186 issuer = attributeDesignator.issuer 187 if issuer is not None: 188 # Issuer found - set lambda to match this against the 189 # issuer setting in the request 190 _issuerMatch = lambda requestChildIssuer: ( 191 issuer == requestChildIssuer) 192 else: 193 # No issuer set - lambda returns True regardless 194 _issuerMatch = lambda requestChildIssuer: True 195 196 197 _attributeMatch = lambda attribute: ( 198 any([matchFunc.evaluate(matchAttributeValue, attrVal) 199 for attrVal in attribute.attributeValues]) and 200 attribute.attributeId == attributeId and 201 attribute.dataType == dataType and 202 _issuerMatch(attribute.issuer) 203 ) 204 205 return _attributeMatch
Note: See TracChangeset
for help on using the changeset viewer.