Package ndg :: Package xacml :: Package core :: Module attributeselector
[hide private]

Source Code for Module ndg.xacml.core.attributeselector

  1  """NDG XACML AttributeSelector type 
  2   
  3  NERC DataGrid 
  4  """ 
  5  __author__ = "P J Kershaw" 
  6  __date__ = "16/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: attributeselector.py 8010 2012-01-30 16:24:06Z rwilkinson $" 
 12  import logging 
 13  log = logging.getLogger(__name__) 
 14   
 15  from ndg.xacml.core.attributevalue import AttributeValueClassFactory 
 16  from ndg.xacml.core.context.exceptions import MissingAttributeError 
 17  from ndg.xacml.core.context.request import Request 
 18  from ndg.xacml.core.expression import Expression 
 19  from ndg.xacml.parsers import XMLParseError 
 20  from ndg.xacml.utils import TypedList 
21 22 23 -class AttributeSelector(Expression):
24 '''XACML Attribute Selector type 25 @cvar ELEMENT_LOCAL_NAME: XML local name for this element 26 @type ELEMENT_LOCAL_NAME: string 27 @cvar REQUEST_CONTEXT_PATH_ATTRIB_NAME: request context XML attribute name 28 @type REQUEST_CONTEXT_PATH_ATTRIB_NAME: string 29 @cvar MUST_BE_PRESENT_ATTRIB_NAME: must be present XML attribute name 30 @type MUST_BE_PRESENT_ATTRIB_NAME: string 31 32 @ivar __attributeValueFactory: When evaluating matches, use an attribute 33 value class factory to create attribute values for match bag of the correct 34 DataType to respect type based rule functions 35 @type __attributeValueFactory: ndg.xacml.core.attributevalue.AttributeValueClassFactory 36 ''' 37 ELEMENT_LOCAL_NAME = 'AttributeSelector' 38 MUST_BE_PRESENT_ATTRIB_NAME = 'MustBePresent' 39 REQUEST_CONTEXT_PATH_ATTRIB_NAME = 'RequestContextPath' 40 41 __slots__ = ('__mustBePresent', 42 '__requestContextPath', 43 '__attributeValueFactory' 44 ) 45
46 - def __init__(self):
47 '''XACML Attribute Selector type 48 ''' 49 super(AttributeSelector, self).__init__() 50 self.__mustBePresent = None 51 self.__requestContextPath = None 52 53 # When evaluating matches, use an attribute value class factory to 54 # create attribute values for match bag of the correct DataType to 55 # respect type based rule functions 56 self.__attributeValueFactory = AttributeValueClassFactory()
57 58 @property
59 - def requestContextPath(self):
60 """Get request context path 61 @return: request context path 62 @rtype: basestring 63 """ 64 return self.__requestContextPath
65 66 @requestContextPath.setter
67 - def requestContextPath(self, value):
68 """Set request context path 69 @param value: request context path 70 @type value: basestring 71 @raise TypeError: incorrect input type 72 """ 73 if not isinstance(value, basestring): 74 raise TypeError('Expecting %r type for "requestContextPath" ' 75 'attribute; got %r' % (basestring, type(value))) 76 77 self.__requestContextPath = value
78 79 @property
80 - def mustBePresent(self):
81 """Get Must Be Present flag 82 @return: must be present flag 83 @rtype: bool 84 """ 85 return self.__mustBePresent
86 87 @mustBePresent.setter
88 - def mustBePresent(self, value):
89 """Set Must Be Present flag 90 @param value: must be present flag 91 @type value: bool 92 @raise TypeError: incorrect input type 93 """ 94 if not isinstance(value, bool): 95 raise TypeError('Expecting %r type for "mustBePresent" ' 96 'attribute; got %r' % (bool, type(value))) 97 98 self.__mustBePresent = value
99 100 @property
101 - def attributeValueFactory(self):
102 """Get Attribute Value factory function 103 104 @return: attribute value factory instance 105 @rtype: ndg.xacml.core.attributevalue.AttributeValueClassFactory 106 """ 107 return self.__attributeValueFactory
108
109 - def evaluate(self, context):
110 """Evaluates an XPath expression with the context node being the request 111 using the XPath selector set in the context. 112 @type context: ndg.xacml.core.context.request.Request 113 @param context: request context 114 @rtype: bag of dataType 115 @return: bag of matched values 116 """ 117 if not isinstance(context, Request): 118 raise TypeError('Expecting %r type for context input; got %r' % 119 (Request, type(context))) 120 121 log.debug("In AttributeSelector for path %r", self.requestContextPath) 122 123 if not context.attributeSelector: 124 raise ValueError('Attribute selector not set in Request object.') 125 126 # Create the return value bag. 127 dataType = self.dataType 128 attributeValueClass = self.attributeValueFactory(dataType) 129 if attributeValueClass is None: 130 raise XMLParseError("No Attribute Value class available for " 131 "type %r" % dataType) 132 attributeValueBag = TypedList(attributeValueClass) 133 134 # Get the matched values and add to the bag as attributes of the 135 # required type. 136 values = context.attributeSelector.selectText(self.requestContextPath) 137 for value in values: 138 attributeValue = attributeValueClass() 139 attributeValue.dataType = dataType 140 attributeValue.value = value 141 attributeValueBag.append(attributeValue) 142 143 if len(attributeValueBag) == 0 and self.mustBePresent: 144 raise MissingAttributeError('"MustBePresent" is set for ' 145 'AttrubuteSelector for path %r' % 146 self.requestContextPath) 147 148 return attributeValueBag
149