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

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

  1  """NDG XACML ElementTree Policy Set Reader 
  2   
  3  NERC DataGrid 
  4  """ 
  5  __author__ = "R B Wilkinson" 
  6  __date__ = "01/11/11" 
  7  __copyright__ = "(C) 2011 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.parsers import XMLParseError 
 13  from ndg.xacml.core.policy import Policy 
 14  from ndg.xacml.core.policydefaults import PolicyDefaults 
 15  from ndg.xacml.core.policyset import PolicySet 
 16  from ndg.xacml.core.variabledefinition import VariableDefinition 
 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 PolicySetReader(ETreeAbstractReader):
24 """Parse a Policy Document using ElementTree 25 @cvar TYPE: XACML type to instantiate from parsed object 26 @type TYPE: type""" 27 TYPE = PolicySet 28
29 - def __call__(self, obj, common):
30 """Parse policy set 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.PolicySet 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(PolicySetReader, self)._parse(obj) 43 44 return self.processElement(elem, common)
45
46 - def processElement(self, elem, common):
47 """Parse policy set object 48 49 @param elem: root element of policy set 50 @type elem: ElementTree Element 51 @return: new XACML expression instance 52 @rtype: ndg.xacml.core.policy.PolicySet derived type 53 @raise XMLParseError: error reading element 54 @raise NotImplementedError: parsing is not implemented for rule 55 combiner, combiner parameters and obligations elements. 56 """ 57 # XACML type to instantiate 58 xacmlType = self.TYPE 59 policySet = xacmlType() 60 61 localName = QName.getLocalPart(elem.tag) 62 if localName != xacmlType.ELEMENT_LOCAL_NAME: 63 raise XMLParseError("No \"%s\" element found" % 64 xacmlType.ELEMENT_LOCAL_NAME) 65 66 # Unpack *required* attributes from top-level element 67 attributeValues = [] 68 for attributeName in (xacmlType.POLICY_SET_ID_ATTRIB_NAME, 69 xacmlType.POLICY_COMBINING_ALG_ID_ATTRIB_NAME): 70 attributeValue = elem.attrib.get(attributeName) 71 if attributeValue is None: 72 raise XMLParseError('No "%s" attribute found in "%s" ' 73 'element' % 74 (attributeName, 75 xacmlType.ELEMENT_LOCAL_NAME)) 76 77 attributeValues.append(attributeValue) 78 79 policySet.policySetId, policySet.policyCombiningAlgId = attributeValues 80 81 # Defaults to XACML version 1.0 82 # TODO: version check 83 policySet.version = (elem.attrib.get(xacmlType.VERSION_ATTRIB_NAME) or 84 xacmlType.DEFAULT_XACML_VERSION) 85 86 # Parse sub-elements 87 for childElem in getElementChildren(elem): 88 localName = QName.getLocalPart(childElem.tag) 89 90 if localName == xacmlType.DESCRIPTION_LOCAL_NAME: 91 if childElem.text is not None: 92 policySet.description = childElem.text.strip() 93 94 elif localName == xacmlType.POLICY_SET_DEFAULTS_LOCAL_NAME: 95 PolicyDefaultsReader = ReaderFactory.getReader(PolicyDefaults) 96 policySet.policyDefaults = PolicyDefaultsReader.parse(childElem, 97 common) 98 99 elif localName == Target.ELEMENT_LOCAL_NAME: 100 TargetReader = ReaderFactory.getReader(Target) 101 policySet.target = TargetReader.parse(childElem, common) 102 103 elif localName == xacmlType.COMBINER_PARAMETERS_LOCAL_NAME: 104 raise NotImplementedError() 105 106 elif localName == xacmlType.POLICY_COMBINER_PARAMETERS_LOCAL_NAME: 107 raise NotImplementedError() 108 109 elif (localName == 110 xacmlType.POLICY_SET_COMBINER_PARAMETERS_LOCAL_NAME): 111 raise NotImplementedError() 112 113 elif localName == VariableDefinition.ELEMENT_LOCAL_NAME: 114 VariableDefinitionReader = ReaderFactory.getReader( 115 VariableDefinition) 116 variableDefinition = VariableDefinitionReader.parse(childElem, 117 common) 118 119 elif localName == Policy.ELEMENT_LOCAL_NAME: 120 PolicyReader = ReaderFactory.getReader(Policy) 121 policy = PolicyReader.parse(childElem, common) 122 policySet.policies.append(policy) 123 124 elif localName == Policy.POLICY_ID_REFERENCE: 125 policyIdReference = childElem.text 126 policySet.policies.append(self._getReferencedPolicy(common, 127 policyIdReference)) 128 129 elif localName == PolicySet.ELEMENT_LOCAL_NAME: 130 PolicySetReader = ReaderFactory.getReader(PolicySet) 131 policySetChild = PolicySetReader.parse(childElem, common) 132 policySet.policies.append(policySetChild) 133 134 elif localName == PolicySet.POLICY_SET_ID_REFERENCE: 135 policySetIdReference = childElem.text 136 policySet.policies.append(self._getReferencedPolicySet(common, 137 policySetIdReference)) 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 PolicySet child element name %r not " 145 "recognised" % localName) 146 147 # Record reference in case of references to this policy set. 148 common.policyFinder.addPolicySetReference(policySet) 149 150 return policySet
151
152 - def _getReferencedPolicy(self, common, policyIdReference):
153 """Retrieve policy referenced by ID. 154 @param common: parsing common data 155 @type common: from ndg.xacml.parsers.common.Common 156 @param policyIdReference: policy ID 157 @type policyIdReference: str 158 @return: policy 159 @rtype: ndg.xacml.core.policy.Policy derived type 160 """ 161 return common.policyFinder.findPolicy(policyIdReference, common)
162
163 - def _getReferencedPolicySet(self, common, policySetIdReference):
164 """Retrieve policy set referenced by ID. 165 @param common: parsing common data 166 @type common: from ndg.xacml.parsers.common.Common 167 @param policySetIdReference: policy ID 168 @type policySetIdReference: str 169 @return: policy set 170 @rtype: ndg.xacml.core.policy.PolicySet derived type 171 """ 172 return common.policyFinder.findPolicySet(policySetIdReference, common)
173