Changeset 6777
- Timestamp:
- 26/03/10 14:09:05 (11 years ago)
- Location:
- TI12-security/trunk/NDG_XACML/ndg/xacml
- Files:
-
- 8 added
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
TI12-security/trunk/NDG_XACML/ndg/xacml/core/context/pdp.py
r6776 r6777 20 20 from ndg.xacml.core.context.response import Response 21 21 from ndg.xacml.core.context.result import Result, Decision 22 from ndg.xacml.core.functions import FunctionMap 22 23 from ndg.xacml.parsers import AbstractReader 23 24 … … 39 40 if policy is not None: 40 41 self.policy = policy 42 43 self.matchFunc = FunctionMap.withLoadedMap() 44 41 45 42 46 @classmethod 43 def fromPolicy (cls, source, reader):47 def fromPolicySource(cls, source, readerFactory): 44 48 """Create a new PDP instance with a given policy 45 49 @param source: source for policy 46 50 @type source: type (dependent on the reader set, it could be for example 47 51 a file path string, file object, XML element instance) 48 @param reader: the reader instance to use to read this policy 49 @type reader: ndg.xacml.parsers.AbstractReader derived type 50 """ 51 if not isinstance(reader, AbstractReader): 52 raise TypeError('Expecting %r derived type for "reader" input; got ' 53 '%r instead' % (AbstractReader, type(reader))) 54 52 @param readerFactory: reader factory returns the reader to use to read 53 this policy 54 @type readerFactory: ndg.xacml.parsers.AbstractReader derived type 55 """ 55 56 pdp = cls() 56 pdp.policy = reader.parse(source)57 pdp.policy = Policy.fromSource(source, readerFactory) 57 58 return pdp 58 59 … … 79 80 @rtype: ndg.xacml.core.context.response.Response 80 81 """ 81 response = Response 82 response = Response() 82 83 result = Result() 83 84 response.results.append(result) … … 96 97 97 98 if not self.matchTarget(self.policy.target, request): 98 log.debug('No match for policy target setting Decision=% r',99 log.debug('No match for policy target setting Decision=%s', 99 100 Decision.NOT_APPLICABLE_STR) 100 101 … … 111 112 except: 112 113 log.error('Exception raised evaluating request context, returning ' 113 'Decision=% r:%s',114 'Decision=%s:%s', 114 115 Decision.INDETERMINATE_STR, 115 116 traceback.format_exc()) … … 147 148 return False 148 149 149 @classmethod 150 def matchTargetChild(cls, targetChild, requestChild): 150 def matchTargetChild(self, targetChild, requestChild): 151 151 """Match a child (Subject, Resource, Action or Environment) from the 152 152 request context with a given target's child … … 169 169 170 170 for childMatch in targetChild.matches: 171 attributeValue = childMatch.attributeValue 171 # Get the match function from the Match ID 172 matchFunc = self.matchFunc.get(childMatch.matchId) 173 if matchFunc is NotImplemented: 174 raise NotImplementedError('No match function implemented for ' 175 'MatchId="%s"' % childMatch.matchId) 176 177 if matchFunc is None: 178 raise Exception('Match function namespace %r is not recognised' 179 % childMatch.matchId) 180 181 matchAttributeValue = childMatch.attributeValue.value 172 182 173 183 # Create a match function based on the presence or absence of an … … 178 188 179 189 _attributeMatch = lambda requestChildAttribute: ( 180 requestChildAttribute.attributeValue == attributeValue and 190 matchFunc(matchAttributeValue, 191 requestChildAttribute.attributeValue.value) and 181 192 requestChildAttribute.attributeId == attributeId and 182 193 requestChildAttribute.dataType == dataType … … 192 203 else: 193 204 _attributeMatch = lambda requestChildAttribute: ( 194 requestChildAttribute.attributeValue == attributeValue 205 matchFunc(matchAttributeValue, 206 requestChildAttribute.attributeValue.value) 195 207 ) 196 208 -
TI12-security/trunk/NDG_XACML/ndg/xacml/core/context/request.py
r6776 r6777 15 15 from ndg.xacml.utils import TypedList 16 16 from ndg.xacml.core.context import XacmlContextBase 17 from ndg.xacml.core. subject import Subject18 from ndg.xacml.core. resource import Resource19 from ndg.xacml.core. action import Action20 from ndg.xacml.core. environment import Environment17 from ndg.xacml.core.context.subject import Subject 18 from ndg.xacml.core.context.resource import Resource 19 from ndg.xacml.core.context.action import Action 20 from ndg.xacml.core.context.environment import Environment 21 21 22 22 … … 24 24 """XACML Request class""" 25 25 __slots__ = ('__subjects', '__resources', '__action', '__environment') 26 ELEMENT_LOCAL_NAME = 'Request' 26 27 27 28 def __init__(self): 29 super(Request, self).__init__() 30 28 31 self.__subjects = TypedList(Subject) 29 32 self.__resources = TypedList(Resource) … … 52 55 raise TypeError('Expecting %r type for request "action" ' 53 56 'attribute; got %r' % (Action, type(value))) 57 58 self.__action = value 54 59 55 60 @property … … 63 68 if not isinstance(value, Environment): 64 69 raise TypeError('Expecting %r type for request "environment" ' 65 'attribute; got %r' % (Environment, type(value))) 70 'attribute; got %r' % (Environment, type(value))) 71 72 self.__environment = value -
TI12-security/trunk/NDG_XACML/ndg/xacml/core/context/response.py
r6776 r6777 25 25 26 26 def __init__(self): 27 super(Response, self).__init__() 27 28 self.__results = TypedList(Result) 28 29 -
TI12-security/trunk/NDG_XACML/ndg/xacml/core/context/result.py
r6771 r6777 292 292 293 293 def __init__(self): 294 super(Result, self).__init__() 294 295 self.__decision = None 295 296 self.__status = None … … 322 323 raise TypeError('Expecting %r type for result "decision" ' 323 324 'attribute; got %r' % (Decision, type(value))) 324 325 self.__decision = value 326 325 327 @property 326 328 def status(self): … … 334 336 raise TypeError('Expecting %r type for result "status" ' 335 337 'attribute; got %r' % (Status, type(value))) 338 339 self.__status = value 336 340 337 341 @property 338 def obligation (self):342 def obligations(self): 339 343 """Result obligation""" 340 return self.__obligation 341 342 @obligation .setter343 def obligation (self, value):344 return self.__obligations 345 346 @obligations.setter 347 def obligations(self, value): 344 348 """Result obligation""" 345 349 if not isinstance(value, Obligation): 346 raise TypeError('Expecting %r type for result "obligation " '350 raise TypeError('Expecting %r type for result "obligations" ' 347 351 'attribute; got %r' % (Obligation, type(value))) 352 353 self.__obligations = value -
TI12-security/trunk/NDG_XACML/ndg/xacml/core/policy.py
r6771 r6777 11 11 __revision__ = "$Id: $" 12 12 from ndg.xacml.utils import TypedList 13 from ndg.xacml.parsers import AbstractReaderFactory, AbstractReader 13 14 from ndg.xacml.core import XacmlCoreBase 14 15 from ndg.xacml.core.policydefaults import PolicyDefaults … … 71 72 self.__obligations = TypedList(Obligation) 72 73 74 @classmethod 75 def fromSource(cls, source, readerFactory): 76 """Create a new policy from the input source parsing it using a 77 reader from the required reader factory e.g. ETreeReaderFactory to use 78 ElementTree based parsing 79 80 @param source: source from which to read the policy - file path, 81 file object, XML node or other dependent on the reader factory selected 82 @type source: string, file, XML node type 83 @param readerFactory: factory class returns reader class used to parse 84 the policy 85 @type readerFactory: ndg.xacml.parsers.AbstractReaderFactory 86 @return: new policy instance 87 @rtype: ndg.xacml.core.policy.Policy 88 """ 89 if not issubclass(readerFactory, AbstractReaderFactory): 90 raise TypeError('Expecting %r derived class for reader factory ' 91 'method; got %r' % (AbstractReaderFactory, 92 readerFactory)) 93 94 reader = readerFactory.getReader(cls) 95 if not issubclass(reader, AbstractReader): 96 raise TypeError('Expecting %r derived class for reader class; ' 97 'got %r' % (AbstractReader, reader)) 98 99 return reader.parse(source) 100 73 101 def _getPolicyId(self): 74 102 return self.__policyId -
TI12-security/trunk/NDG_XACML/ndg/xacml/parsers/__init__.py
r6770 r6777 15 15 16 16 from ndg.xacml import XACMLError 17 17 from ndg.xacml.core import XacmlCoreBase 18 18 19 19 20 class XMLParseError(XACMLError): … … 21 22 22 23 23 class AbstractReader :24 """Abstract base class for ElementTree implementation ofXACML reader"""24 class AbstractReader(object): 25 """Abstract base class for XACML reader""" 25 26 __metaclass__ = ABCMeta 26 27 … … 51 52 reader = cls() 52 53 return reader(obj) 54 55 56 class AbstractReaderFactory(object): 57 """Abstract base class XACML reader factory""" 58 __metaclass__ = ABCMeta 59 60 @classmethod 61 @abstractmethod 62 def getReader(cls, xacmlType): 63 """Get the reader class for the given XACML input type 64 @param xacmlType: XACML type to retrieve a reader for 65 @type xacmlType: ndg.xaml.core.XacmlCoreBase derived 66 @return: reader class 67 @rtype: ndg.xacml.parsers.AbstractReader derived type 68 """ 69 if not issubclass(xacmlType, XacmlCoreBase): 70 raise TypeError('Expecting %r derived class for getReader method; ' 71 'got %r' % (XacmlCoreBase, xacmlType)) -
TI12-security/trunk/NDG_XACML/ndg/xacml/parsers/etree/factory.py
r6752 r6777 13 13 log = logging.getLogger(__name__) 14 14 15 from ndg.xacml.parsers import AbstractReaderFactory 15 16 from ndg.xacml.utils.factory import importModuleObject 16 17 … … 26 27 27 28 28 class ReaderFactory( object):29 class ReaderFactory(AbstractReaderFactory): 29 30 """Parser factory for ElementTree based parsers for XACML types""" 30 31 -
TI12-security/trunk/NDG_XACML/ndg/xacml/test/ndg1.xml
r6766 r6777 16 16 <Resource> 17 17 <!-- Pattern match all request URIs beginning with / --> 18 <ResourceMatch MatchId="urn:oasis:names:tc:xacml: 1.0:function:regexp-string-match">18 <ResourceMatch MatchId="urn:oasis:names:tc:xacml:2.0:function:anyURI-regexp-match"> 19 19 <ResourceAttributeDesignator 20 20 AttributeId="urn:siteA:security:authz:1.0:attr:resourceURI" 21 DataType="http://www.w3.org/2001/XMLSchema# string"/>22 <AttributeValue DataType="http://www.w3.org/2001/XMLSchema# string">21 DataType="http://www.w3.org/2001/XMLSchema#anyURI"/> 22 <AttributeValue DataType="http://www.w3.org/2001/XMLSchema#anyURI"> 23 23 ^/.*$ 24 24 </AttributeValue> … … 29 29 30 30 <!-- Deny everything by default --> 31 <!-- 31 32 <Rule RuleId="urn:ndg:security1.0:authz:test:DenyAllRule" Effect="Deny"/> 32 33 --> 33 34 <!-- 34 35 Following rules punch holes through the deny everything rule above -
TI12-security/trunk/NDG_XACML/ndg/xacml/test/test_xacml.py
r6776 r6777 317 317 318 318 resourceAttribute.dataType = "http://www.w3.org/2001/XMLSchema#anyURI" 319 resourceAttribute.attributeValue = \ 319 resourceAttribute.attributeValue = AttributeValue() 320 resourceAttribute.attributeValue.value = \ 320 321 'file://example/med/record/patient/BartSimpson' 321 322 … … 324 325 request.action = Action() 325 326 actionAttribute = Attribute() 326 request.action.a ppend(actionAttribute)327 328 requestAttribute.attributeId = \327 request.action.attributes.append(actionAttribute) 328 329 actionAttribute.attributeId = \ 329 330 "urn:oasis:names:tc:xacml:1.0:action:action-id" 330 requestAttribute.dataType = "http://www.w3.org/2001/XMLSchema#string" 331 requestAttribute.attributeValue = 'read' 331 actionAttribute.dataType = "http://www.w3.org/2001/XMLSchema#string" 332 actionAttribute.attributeValue = AttributeValue() 333 actionAttribute.attributeValue.value = 'read' 332 334 333 335 return request … … 344 346 345 347 def test03AbstractCtxHandler(self): 346 self.assertRaises( AbstractContextHandler(), NotImplementedError)348 self.assertRaises(TypeError, AbstractContextHandler) 347 349 348 350 def test04CreateCtxHandler(self): … … 350 352 351 353 def test04PDPInterface(self): 352 self.assertRaises( PDPInterface(), NotImplementedError)354 self.assertRaises(TypeError, PDPInterface) 353 355 354 356 def test05CreatePDP(self): … … 357 359 358 360 def _createPDPfromPolicy(self): 359 pdp = PDP.fromPolicy (XACML_NDGTEST1_FILEPATH)361 pdp = PDP.fromPolicySource(XACML_NDGTEST1_FILEPATH, ReaderFactory) 360 362 return pdp 361 363 … … 366 368 def test07EvaluatePDP(self): 367 369 request = self._createRequestCtx() 370 pdp = self._createPDPfromPolicy() 368 371 response = pdp.evaluate(request) 369 372 self.assert_(response) 370 373 371 374 from ndg.xacml.core.functions import FunctionMap 375 from ndg.xacml.core.functions.v2.anyuri_regexp_match import AnyURIRegexpMatch 376 377 378 class FunctionTestCase(unittest.TestCase): 379 """Test XACML match functions implementation""" 380 381 def test01(self): 382 funcMap = FunctionMap() 383 funcMap.load() 384 anyUriMatchNs = \ 385 'urn:oasis:names:tc:xacml:2.0:function:anyURI-regexp-match' 386 387 self.assert_(isinstance(funcMap.get(anyUriMatchNs), AnyURIRegexpMatch)) 372 388 373 389 -
TI12-security/trunk/NDG_XACML/ndg/xacml/utils/__init__.py
r6730 r6777 1 """Utilities package for NDG Security1 """Utilities package for NDG XACML 2 2 3 3 NERC DataGrid Project … … 77 77 return super(TypedList, self).append(item) 78 78 79 79 80 class RestrictedKeyNamesDict(dict): 80 81 """Utility class for holding a constrained list of key names … … 104 105 105 106 dict.update(self, d, **kw) 107 108 109 _isIterable = lambda obj: getattr(obj, '__iter__', False) 110 111 112 class VettedDict(dict): 113 """Enforce custom checking on keys and items before addition to the 114 dictionary""" 115 116 def __init__(self, *args): 117 """Initialise setting the allowed type or types for keys and items 118 119 @param args: two arguments: the first is a callable which filters for 120 permissable keys in this dict, the second sets the type or list of 121 types permissable for items in this dict 122 @type args: tuple 123 """ 124 super(VettedDict, self).__init__() 125 126 if len(args) != 2: 127 raise TypeError('__init__() takes 2 arguments, KeyFilter and ' 128 'valueFilter (%d given)' % len(args)) 129 130 # Validation of inputs 131 for arg, argName in zip(args, ('KeyFilter', 'valueFilter')): 132 if not callable(arg): 133 raise TypeError('Expecting callable for %r input; got %r' % 134 (argName, type(arg))) 135 136 self.__KeyFilter, self.__valueFilter = args 137 138 def _verifyKeyValPair(self, key, val): 139 """Check given key value pair and return False if they should be 140 filtered out. Filter functions may also raise an exception if they 141 wish to abort completely 142 """ 143 if not self.__KeyFilter(key): 144 return False 145 146 elif not self.__valueFilter(val): 147 return False 148 149 else: 150 return True 151 152 def __setitem__(self, key, val): 153 """Override base class implementation to enforce type checking""" 154 if self._verifyKeyValPair(key, val): 155 dict.__setitem__(self, key, val) 156 157 def update(self, d, **kw): 158 """Override base class implementation to enforce type checking""" 159 for dictArg in (d, kw): 160 for key, val in dictArg.items(): 161 if not self._verifyKeyValPair(key, val): 162 del dictArg[key] 163 164 dict.update(self, d, **kw) 165
Note: See TracChangeset
for help on using the changeset viewer.