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$" |
---|
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 |
---|
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): |
---|
30 | """Parse policy object |
---|
31 | |
---|
32 | @param obj: input object to parse |
---|
33 | @type obj: ElementTree Element, or stream object |
---|
34 | @return: new XACML expression instance |
---|
35 | @rtype: ndg.xacml.core.policy.Policy derived type |
---|
36 | @raise XMLParseError: error reading element |
---|
37 | @raise NotImplementedError: parsing is not implemented for rule |
---|
38 | combiner, combiner parameters and obligations elements. |
---|
39 | """ |
---|
40 | elem = super(PolicyReader, self)._parse(obj) |
---|
41 | |
---|
42 | # XACML type to instantiate |
---|
43 | xacmlType = PolicyReader.TYPE |
---|
44 | policy = xacmlType() |
---|
45 | |
---|
46 | localName = QName.getLocalPart(elem.tag) |
---|
47 | if localName != xacmlType.ELEMENT_LOCAL_NAME: |
---|
48 | raise XMLParseError("No \"%s\" element found" % |
---|
49 | xacmlType.ELEMENT_LOCAL_NAME) |
---|
50 | |
---|
51 | # Unpack *required* attributes from top-level element |
---|
52 | attributeValues = [] |
---|
53 | for attributeName in (xacmlType.POLICY_ID_ATTRIB_NAME, |
---|
54 | xacmlType.RULE_COMBINING_ALG_ID_ATTRIB_NAME): |
---|
55 | attributeValue = elem.attrib.get(attributeName) |
---|
56 | if attributeValue is None: |
---|
57 | raise XMLParseError('No "%s" attribute found in "%s" ' |
---|
58 | 'element' % |
---|
59 | (attributeName, |
---|
60 | xacmlType.ELEMENT_LOCAL_NAME)) |
---|
61 | |
---|
62 | attributeValues.append(attributeValue) |
---|
63 | |
---|
64 | policy.policyId, policy.ruleCombiningAlgId = attributeValues |
---|
65 | |
---|
66 | # Defaults to XACML version 1.0 |
---|
67 | # TODO: version check |
---|
68 | policy.version = (elem.attrib.get(xacmlType.VERSION_ATTRIB_NAME) or |
---|
69 | xacmlType.DEFAULT_XACML_VERSION) |
---|
70 | |
---|
71 | # Parse sub-elements |
---|
72 | for childElem in elem: |
---|
73 | localName = QName.getLocalPart(childElem.tag) |
---|
74 | |
---|
75 | if localName == xacmlType.DESCRIPTION_LOCAL_NAME: |
---|
76 | if childElem.text is not None: |
---|
77 | policy.description = childElem.text.strip() |
---|
78 | |
---|
79 | elif localName == xacmlType.POLICY_DEFAULTS_LOCAL_NAME: |
---|
80 | PolicyDefaultsReader = ReaderFactory.getReader(PolicyDefaults) |
---|
81 | policy.policyDefaults = PolicyDefaultsReader.parse(childElem) |
---|
82 | |
---|
83 | elif localName == Target.ELEMENT_LOCAL_NAME: |
---|
84 | TargetReader = ReaderFactory.getReader(Target) |
---|
85 | policy.target = TargetReader.parse(childElem) |
---|
86 | |
---|
87 | elif localName == xacmlType.COMBINER_PARAMETERS_LOCAL_NAME: |
---|
88 | raise NotImplementedError() |
---|
89 | |
---|
90 | elif localName == xacmlType.RULE_COMBINER_PARAMETERS_LOCAL_NAME: |
---|
91 | raise NotImplementedError() |
---|
92 | |
---|
93 | elif localName == VariableDefinition.ELEMENT_LOCAL_NAME: |
---|
94 | VariableDefinitionReader = ReaderFactory.getReader( |
---|
95 | VariableDefinition) |
---|
96 | variableDefinition = VariableDefinitionReader.parse(childElem) |
---|
97 | |
---|
98 | elif localName == Rule.ELEMENT_LOCAL_NAME: |
---|
99 | RuleReader = ReaderFactory.getReader(Rule) |
---|
100 | rule = RuleReader.parse(childElem) |
---|
101 | if rule.id in [_rule.id for _rule in policy.rules]: |
---|
102 | raise XMLParseError("Duplicate Rule ID %r found" % rule.id) |
---|
103 | |
---|
104 | policy.rules.append(rule) |
---|
105 | |
---|
106 | elif localName == xacmlType.OBLIGATIONS_LOCAL_NAME: |
---|
107 | raise NotImplementedError('Parsing for Obligations element is ' |
---|
108 | 'not implemented') |
---|
109 | |
---|
110 | else: |
---|
111 | raise XMLParseError("XACML Policy child element name %r not " |
---|
112 | "recognised" % localName) |
---|
113 | |
---|
114 | return policy |
---|
115 | |
---|