source: TI12-security/trunk/ndg_xacml/ndg/xacml/parsers/etree/applyreader.py @ 7661

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/ndg_xacml/ndg/xacml/parsers/etree/applyreader.py@7661
Revision 7661, 6.3 KB checked in by pjkersha, 10 years ago (diff)

ndg_xacml:

  • added support for custom AttributeValue? DataTypes? - this means that the ESGF GroupRole? attribute type can be added directly into XACML policies.
  • TODO: add support for adding custom functions so that a PDP can correctly apply rules based on custom types like the ESGF GroupRole?.
  • Property svn:keywords set to Id
Line 
1"""NDG XACML ElementTree based Apply type reader
2
3NERC DataGrid
4"""
5__author__ = "P J Kershaw"
6__date__ = "19/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$"
12import logging
13log = logging.getLogger(__name__)
14
15from ndg.xacml.core.apply import Apply
16from ndg.xacml.core.attributevalue import AttributeValue
17from ndg.xacml.core.variablereference import VariableReference
18from ndg.xacml.core.attributeselector import AttributeSelector
19from ndg.xacml.core.attributedesignator import (SubjectAttributeDesignator,
20                                                EnvironmentAttributeDesignator,
21                                                ActionAttributeDesignator,
22                                                EnvironmentAttributeDesignator)
23from ndg.xacml.parsers import XMLParseError
24from ndg.xacml.parsers.etree import QName
25from ndg.xacml.parsers.etree.reader import ETreeAbstractReader
26from ndg.xacml.parsers.etree.factory import ReaderFactory
27
28
29class ApplyReader(ETreeAbstractReader):
30    '''ElementTree based XACML Apply type parser
31   
32    @cvar FUNCTION_ELEMENT_LOCAL_NAME: XML local name for function element
33    @type FUNCTION_ELEMENT_LOCAL_NAME: string
34   
35    @cvar VARIABLE_REFERENCE_ELEMENT_LOCAL_NAME: XML local name for variable
36    reference element
37    @type VARIABLE_REFERENCE_ELEMENT_LOCAL_NAME: string
38   
39    @cvar TYPE: XACML class type that this reader will read values into
40    @type TYPE: abc.ABCMeta
41    '''
42    TYPE = Apply
43   
44    # These two are not currently implemented.  When an implementation is made
45    # the ELEMENT_LOCAL_NAME may be referenced from the native class rather than
46    # a class variable here
47    FUNCTION_ELEMENT_LOCAL_NAME = 'Function'
48    VARIABLE_REFERENCE_ELEMENT_LOCAL_NAME = 'VariableReference'
49   
50    def __call__(self, obj):
51        """Parse Apply type object
52
53        @param obj: input object to parse
54        @type obj: ElementTree Element, or stream object
55        @return: ElementTree element
56        @rtype: xml.etree.Element
57        """
58        elem = super(ApplyReader, self)._parse(obj)
59       
60        xacmlType = self.__class__.TYPE
61        applyObj = xacmlType()
62       
63        if QName.getLocalPart(elem.tag) != xacmlType.ELEMENT_LOCAL_NAME:
64            raise XMLParseError("No \"%s\" element found" % 
65                                xacmlType.ELEMENT_LOCAL_NAME)
66           
67        # Unpack *required* attributes from top-level element
68        attributeValues = []
69        for attributeName in (xacmlType.FUNCTION_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' % (attributeName,
74                                                 xacmlType.ELEMENT_LOCAL_NAME))
75               
76            attributeValues.append(attributeValue) 
77                   
78        applyObj.functionId, = attributeValues
79       
80        # Allow for any of the defined Expression sub-types in the child
81        # elements
82        for subElem in elem:
83            localName = QName.getLocalPart(subElem.tag)
84            if localName == xacmlType.ELEMENT_LOCAL_NAME:
85                applyObj.expressions.append(ApplyReader.parse(subElem))
86             
87            elif localName == AttributeValue.ELEMENT_LOCAL_NAME:
88                AttributeValueReader = ReaderFactory.getReader(AttributeValue) 
89                applyObj.expressions.append(AttributeValueReader.parse(subElem))
90                 
91            elif localName == SubjectAttributeDesignator.ELEMENT_LOCAL_NAME:
92                SubjectAttributeDesignatorReader = ReaderFactory.getReader(
93                                                SubjectAttributeDesignator)
94                applyObj.expressions.append(
95                                SubjectAttributeDesignatorReader.parse(subElem))
96               
97            elif localName == EnvironmentAttributeDesignator.ELEMENT_LOCAL_NAME:
98                EnvironmentAttributeDesignatorReader = ReaderFactory.getReader(
99                                                EnvironmentAttributeDesignator)
100                applyObj.expressions.append(
101                            EnvironmentAttributeDesignatorReader.parse(subElem))
102               
103            elif localName == ActionAttributeDesignator.ELEMENT_LOCAL_NAME:
104                ActionAttributeDesignatorReader = ReaderFactory.getReader(
105                                                ActionAttributeDesignator)
106                applyObj.expressions.append(
107                                ActionAttributeDesignatorReader.parse(subElem))
108               
109            elif localName == EnvironmentAttributeDesignator.ELEMENT_LOCAL_NAME:
110                EnvironmentAttributeDesignatorReader = ReaderFactory.getReader(
111                                                EnvironmentAttributeDesignator)
112                applyObj.expressions.append(
113                            EnvironmentAttributeDesignatorReader.parse(subElem))
114               
115            elif localName == AttributeSelector.ELEMENT_LOCAL_NAME:
116                AttributeSelectorReader = ReaderFactory.getReader(
117                                                            AttributeSelector)
118                applyObj.expressions.append(
119                                        AttributeSelectorReader.parse(subElem))
120           
121            elif localName == Condition.ELEMENT_LOCAL_NAME:
122                ConditionReader = ReaderFactory.getReader(Condition)
123                applyObj.expressions.append(ConditionReader.parse(subElem))
124               
125            elif localName == self.__class__.FUNCTION_ELEMENT_LOCAL_NAME:
126                raise NotImplementedError('%r Apply sub-element not '
127                                          'implemented', localName)
128           
129            elif (localName == VariableReference.ELEMENT_LOCAL_NAME):                               
130                raise NotImplementedError('%r Apply sub-element not '
131                                          'implemented', localName)
132            else:
133                raise XMLParseError('%r Apply sub-element not recognised', 
134                                    localName)
135   
136        return applyObj
Note: See TracBrowser for help on using the repository browser.