Package ndg :: Package xacml :: Package parsers :: Package etree :: Module policyreader
[hide private]

Source Code for Module ndg.xacml.parsers.etree.policyreader

  1  """NDG XACML ElementTree Policy Reader   
  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: policyreader.py 8028 2012-02-27 14:38:01Z rwilkinson $" 
 12  from ndg.xacml.parsers import XMLParseError 
 13  from ndg.xacml.core.policy import Policy 
 14  from ndg.xacml.core.policydefaults import PolicyDefaults 
 15  from ndg.xacml.core.variabledefinition import VariableDefinition 
 16  from ndg.xacml.core.rule import Rule 
 17  from ndg.xacml.core.target import Target 
 18  from ndg.xacml.parsers.etree import QName, getElementChildren 
 19  from ndg.xacml.parsers.etree.reader import ETreeAbstractReader 
 20  from ndg.xacml.parsers.etree.factory import ReaderFactory 
21 22 23 -class PolicyReader(ETreeAbstractReader):
24 """Parse a Policy Document using ElementTree 25 @cvar TYPE: XACML type to instantiate from parsed object 26 @type TYPE: type""" 27 TYPE = Policy 28
29 - def __call__(self, obj, common):
30 """Parse policy object 31 32 @param obj: input object to parse 33 @type obj: ElementTree Element, or stream object 34 @param common: parsing common data 35 @type common: from ndg.xacml.parsers.common.Common 36 @return: new XACML expression instance 37 @rtype: ndg.xacml.core.policy.Policy derived type 38 @raise XMLParseError: error reading element 39 @raise NotImplementedError: parsing is not implemented for rule 40 combiner, combiner parameters and obligations elements. 41 """ 42 elem = super(PolicyReader, self)._parse(obj) 43 44 return self.processElement(elem, common)
45 46 @classmethod
47 - def parse(cls, obj, common=None):
48 """Parse from input object and return new XACML object 49 As a special case, allow the common data to be None. This is because for 50 parsing a policy rather than a policy set, no common data is needed. 51 @param obj: input source - file name, stream object or other 52 @type obj: string, stream or other 53 @param common: parsing common data 54 @type common: from ndg.xacml.parsers.common.Common 55 @return: new XACML object 56 @rtype: XacmlCoreBase sub type 57 """ 58 return super(ETreeAbstractReader, cls).parse(obj, common)
59
60 - def processElement(self, elem, common):
61 """Parse policy object 62 63 @param elem: root element of policy 64 @type elem: ElementTree Element 65 @param common: parsing common data 66 @type common: from ndg.xacml.parsers.common.Common 67 @return: new XACML expression instance 68 @rtype: ndg.xacml.core.policy.Policy derived type 69 @raise XMLParseError: error reading element 70 @raise NotImplementedError: parsing is not implemented for rule 71 combiner, combiner parameters and obligations elements. 72 """ 73 # XACML type to instantiate 74 xacmlType = self.TYPE 75 policy = xacmlType() 76 77 localName = QName.getLocalPart(elem.tag) 78 if localName != xacmlType.ELEMENT_LOCAL_NAME: 79 raise XMLParseError("No \"%s\" element found" % 80 xacmlType.ELEMENT_LOCAL_NAME) 81 82 # Unpack *required* attributes from top-level element 83 attributeValues = [] 84 for attributeName in (xacmlType.POLICY_ID_ATTRIB_NAME, 85 xacmlType.RULE_COMBINING_ALG_ID_ATTRIB_NAME): 86 attributeValue = elem.attrib.get(attributeName) 87 if attributeValue is None: 88 raise XMLParseError('No "%s" attribute found in "%s" ' 89 'element' % 90 (attributeName, 91 xacmlType.ELEMENT_LOCAL_NAME)) 92 93 attributeValues.append(attributeValue) 94 95 policy.policyId, policy.ruleCombiningAlgId = attributeValues 96 97 # Defaults to XACML version 1.0 98 # TODO: version check 99 policy.version = (elem.attrib.get(xacmlType.VERSION_ATTRIB_NAME) or 100 xacmlType.DEFAULT_XACML_VERSION) 101 102 # Parse sub-elements 103 for childElem in getElementChildren(elem): 104 localName = QName.getLocalPart(childElem.tag) 105 106 if localName == xacmlType.DESCRIPTION_LOCAL_NAME: 107 if childElem.text is not None: 108 policy.description = childElem.text.strip() 109 110 elif localName == xacmlType.POLICY_DEFAULTS_LOCAL_NAME: 111 PolicyDefaultsReader = ReaderFactory.getReader(PolicyDefaults) 112 policy.policyDefaults = PolicyDefaultsReader.parse(childElem, 113 common) 114 115 elif localName == Target.ELEMENT_LOCAL_NAME: 116 TargetReader = ReaderFactory.getReader(Target) 117 policy.target = TargetReader.parse(childElem, common) 118 119 elif localName == xacmlType.COMBINER_PARAMETERS_LOCAL_NAME: 120 raise NotImplementedError() 121 122 elif localName == xacmlType.RULE_COMBINER_PARAMETERS_LOCAL_NAME: 123 raise NotImplementedError() 124 125 elif localName == VariableDefinition.ELEMENT_LOCAL_NAME: 126 VariableDefinitionReader = ReaderFactory.getReader( 127 VariableDefinition) 128 variableDefinition = VariableDefinitionReader.parse(childElem, 129 common) 130 131 elif localName == Rule.ELEMENT_LOCAL_NAME: 132 RuleReader = ReaderFactory.getReader(Rule) 133 rule = RuleReader.parse(childElem, common) 134 if rule.id in [_rule.id for _rule in policy.rules]: 135 raise XMLParseError("Duplicate Rule ID %r found" % rule.id) 136 137 policy.rules.append(rule) 138 139 elif localName == xacmlType.OBLIGATIONS_LOCAL_NAME: 140 raise NotImplementedError('Parsing for Obligations element is ' 141 'not implemented') 142 143 else: 144 raise XMLParseError("XACML Policy child element name %r not " 145 "recognised" % localName) 146 147 # Record reference in case of references to this policy. 148 # Allow for there not being a policy finder since this is not needed if 149 # if the root is a policy rather than a policy set. 150 if common is not None and hasattr(common, 'policyFinder'): 151 common.policyFinder.addPolicyReference(policy) 152 153 return policy
154