source: TI12-security/trunk/NDGSecurity/python/ndg_security_common/ndg/security/common/authz/xacml/etree/reader.py @ 6730

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/NDGSecurity/python/ndg_security_common/ndg/security/common/authz/xacml/etree/reader.py@6730
Revision 6730, 3.0 KB checked in by pjkersha, 10 years ago (diff)
  • Working Credential Wallet refactored for Python 2.6
  • Pruned out more old code: ZSI and Twisted SAOP/WSDL wrappers, Session Manager and WS-Security, Test 'Site B' Attribute Authority - for testing role mapping.
  • Started XACML package ElementTree based parser.
Line 
1'''
2Created on 15 Mar 2010
3
4@author: pjkersha
5'''
6import logging
7log = logging.getLogger(__name__)
8
9from xml.etree import ElementTree
10
11from ndg.security.common.authz.xacml import PolicyComponent, XMLParseError
12from ndg.security.common.authz.xacml.policy import Policy
13from ndg.security.common.authz.xacml.etree import QName
14
15
16class AbstractReader(object):
17    """ElementTree implementation of XACML reader"""
18   
19    def __init__(self):
20        self.__namespace_map_backup = ElementTree._namespace_map.copy()
21        ElementTree._namespace_map[''] = PolicyComponent.XACML_2_0_XMLNS
22       
23    def __del__(self):
24        """Restore original global namespace map"""
25        ElementTree._namespace_map = self.__namespace_map_backup
26       
27    def __call__(self, obj):
28        """Abstract Parse XACML method
29        @raise NotImplementedError:
30        """
31        raise NotImplementedError()
32   
33    def _parse(self, obj):
34        """Parse helper method
35        @param obj: input object to parse
36        @type obj: ElementTree Element, or stream object
37        @return: ElementTree element
38        @rtype: xml.etree.Element
39        """
40        if ElementTree.iselement(obj):
41            elem = obj
42        else:
43            # Treat as stream object
44            elem = ElementTree.parse(obj).getroot()
45           
46        return elem
47   
48   
49class PolicyReader(AbstractReader):
50    """Parse a Polciy Document using ElementTree
51    """
52    def __call__(self, obj):
53        """Parse policy object"""
54        elem = self._parse(obj)
55       
56        policy = Policy()
57        cls = Policy
58       
59        localName = QName.getLocalPart(elem.tag)
60        if localName != cls.DEFAULT_ELEMENT_LOCAL_NAME:
61            raise XMLParseError("No \"%s\" element found" %
62                                    cls.DEFAULT_ELEMENT_LOCAL_NAME)
63       
64        # Unpack attributes from top-level element
65        attributeValues = []
66        for attributeName in (cls.POLICY_ID_ATTRIB_NAME,
67                              cls.RULE_COMBINING_ALG_ID_ATTRIB_NAME):
68            attributeValue = elem.attrib.get(attributeName)
69            if attributeValue is None:
70                raise XMLParseError('No "%s" attribute found in "%s" '
71                                        'element' %
72                                        (attributeName,
73                                         cls.DEFAULT_ELEMENT_LOCAL_NAME))
74               
75            attributeValues.append(attributeValue) 
76                   
77        # Parse element attributes
78        policy.id, policy.ruleCombiningAlg = attributeValues
79       
80        # Parse sub-elements
81        for childElem in elem:
82            localName = QName.getLocalPart(childElem.tag)
83           
84            if localName == cls.DESCRIPTION_LOCALNAME:
85                policy.description = 
86            elif localName == cls.TARGET_LOCALNAME:
87                pass
88            else:
89                raise XMLParseError("XACML Policy child element name %r not "
90                                    "recognised" % localName)
91
92       
93        return policy
Note: See TracBrowser for help on using the repository browser.