1 | """NDG XACML ElementTree based generic reader for subject, resource, action and |
---|
2 | environment match types |
---|
3 | |
---|
4 | NERC DataGrid |
---|
5 | """ |
---|
6 | __author__ = "P J Kershaw" |
---|
7 | __date__ = "16/03/10" |
---|
8 | __copyright__ = "(C) 2010 Science and Technology Facilities Council" |
---|
9 | __contact__ = "Philip.Kershaw@stfc.ac.uk" |
---|
10 | __license__ = "BSD - see LICENSE file in top-level directory" |
---|
11 | __contact__ = "Philip.Kershaw@stfc.ac.uk" |
---|
12 | __revision__ = "$Id$" |
---|
13 | from ndg.xacml.core.attributevalue import AttributeValue |
---|
14 | from ndg.xacml.core.attributeselector import AttributeSelector |
---|
15 | from ndg.xacml.parsers import XMLParseError |
---|
16 | from ndg.xacml.parsers.etree import QName |
---|
17 | from ndg.xacml.parsers.etree.reader import ETreeAbstractReader |
---|
18 | from ndg.xacml.parsers.etree.factory import ReaderFactory |
---|
19 | |
---|
20 | |
---|
21 | class MatchReaderBase(ETreeAbstractReader): |
---|
22 | '''ElementTree based XACML generic Match parser for subject, resource, |
---|
23 | action and environment match types |
---|
24 | |
---|
25 | @cvar ATTRIBUTE_DESIGNATOR_TYPE: type for attribute designator sub-elements: |
---|
26 | derived class should set to relevant type e.g. for SubjectMatch, |
---|
27 | SubjectAttributeDesignator |
---|
28 | @type ATTRIBUTE_DESIGNATOR_TYPE: NoneType |
---|
29 | ''' |
---|
30 | ATTRIBUTE_DESIGNATOR_TYPE = None |
---|
31 | |
---|
32 | def __init__(self): |
---|
33 | """Virtual method |
---|
34 | |
---|
35 | @raise NotImplementedError: ATTRIBUTE_DESIGNATOR_TYPE must be set in a |
---|
36 | derived class |
---|
37 | """ |
---|
38 | if self.__class__.ATTRIBUTE_DESIGNATOR_TYPE is None: |
---|
39 | raise NotImplementedError('Extend this class setting the ' |
---|
40 | '"ATTRIBUTE_DESIGNATOR_TYPE" class ' |
---|
41 | 'variable') |
---|
42 | |
---|
43 | super(MatchReaderBase, self).__init__() |
---|
44 | |
---|
45 | def __call__(self, obj): |
---|
46 | """Parse *Match object (where * = Subject, Resource, Environment or |
---|
47 | Action |
---|
48 | |
---|
49 | @param obj: input object to parse |
---|
50 | @type obj: ElementTree Element, or stream object |
---|
51 | @return: new XACML match instance |
---|
52 | @rtype: ndg.xacml.core.matchreader.MatchReaderBase derived type |
---|
53 | @raise XMLParseError: error reading element |
---|
54 | """ |
---|
55 | elem = super(MatchReaderBase, self)._parse(obj) |
---|
56 | |
---|
57 | xacmlType = self.__class__.TYPE |
---|
58 | match = xacmlType() |
---|
59 | |
---|
60 | localName = QName.getLocalPart(elem.tag) |
---|
61 | if localName != xacmlType.ELEMENT_LOCAL_NAME: |
---|
62 | raise XMLParseError("No \"%s\" element found" % |
---|
63 | xacmlType.ELEMENT_LOCAL_NAME) |
---|
64 | |
---|
65 | # Unpack *required* attributes from top-level element |
---|
66 | attributeValues = [] |
---|
67 | for attributeName in (xacmlType.MATCH_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' % (attributeName, |
---|
72 | xacmlType.ELEMENT_LOCAL_NAME)) |
---|
73 | |
---|
74 | attributeValues.append(attributeValue) |
---|
75 | |
---|
76 | match.matchId, = attributeValues |
---|
77 | |
---|
78 | # Assign specific attribute designator type from derived class |
---|
79 | attributeDesignatorType = self.__class__.ATTRIBUTE_DESIGNATOR_TYPE |
---|
80 | attributeDesignatorReaderType = ReaderFactory.getReader( |
---|
81 | attributeDesignatorType) |
---|
82 | |
---|
83 | # Parse match elements |
---|
84 | for childElem in elem: |
---|
85 | localName = QName.getLocalPart(childElem.tag) |
---|
86 | |
---|
87 | if localName == xacmlType.ATTRIBUTE_VALUE_ELEMENT_LOCAL_NAME: |
---|
88 | AttributeValueReader = ReaderFactory.getReader(AttributeValue) |
---|
89 | match.attributeValue = AttributeValueReader.parse(childElem) |
---|
90 | |
---|
91 | elif localName == attributeDesignatorType.ELEMENT_LOCAL_NAME: |
---|
92 | if match.attributeSelector is not None: |
---|
93 | raise XMLParseError("XACML %r child element may only be " |
---|
94 | "either a %r or %r element NOT both" % |
---|
95 | (xacmlType.ELEMENT_LOCAL_NAME, |
---|
96 | attributeDesignatorType.ELEMENT_LOCAL_NAME, |
---|
97 | AttributeSelector.ELEMENT_LOCAL_NAME)) |
---|
98 | |
---|
99 | match.attributeDesignator = attributeDesignatorReaderType.parse( |
---|
100 | childElem) |
---|
101 | |
---|
102 | elif localName == AttributeSelector.ELEMENT_LOCAL_NAME: |
---|
103 | if match.attributeDesignator is not None: |
---|
104 | raise XMLParseError("XACML %r child element may only be " |
---|
105 | "either a %r or %r element NOT both" % |
---|
106 | (xacmlType.ELEMENT_LOCAL_NAME, |
---|
107 | attributeDesignatorType.ELEMENT_LOCAL_NAME, |
---|
108 | AttributeSelector.ELEMENT_LOCAL_NAME)) |
---|
109 | |
---|
110 | AttributeSelectorReader = ReaderFactory.getReader( |
---|
111 | AttributeSelector) |
---|
112 | |
---|
113 | match.attributeSelector = AttributeSelectorReader.parse( |
---|
114 | childElem) |
---|
115 | else: |
---|
116 | raise XMLParseError("XACML %r child element name %r not " |
---|
117 | "recognised" % |
---|
118 | (xacmlType.MATCH_TYPE.ELEMENT_LOCAL_NAME, |
---|
119 | localName)) |
---|
120 | |
---|
121 | return match |
---|
122 | |
---|
123 | |
---|
124 | |
---|
125 | |
---|