1 | """NDG XACML ElementTree Policy Reader |
---|
2 | |
---|
3 | NERC DataGrid Project |
---|
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 string: type""" |
---|
27 | TYPE = Policy |
---|
28 | |
---|
29 | def __call__(self, obj): |
---|
30 | """Parse policy object""" |
---|
31 | elem = super(PolicyReader, self)._parse(obj) |
---|
32 | |
---|
33 | # XACML type to instantiate |
---|
34 | xacmlType = PolicyReader.TYPE |
---|
35 | policy = xacmlType() |
---|
36 | |
---|
37 | localName = QName.getLocalPart(elem.tag) |
---|
38 | if localName != xacmlType.ELEMENT_LOCAL_NAME: |
---|
39 | raise XMLParseError("No \"%s\" element found" % |
---|
40 | xacmlType.ELEMENT_LOCAL_NAME) |
---|
41 | |
---|
42 | # Unpack *required* attributes from top-level element |
---|
43 | attributeValues = [] |
---|
44 | for attributeName in (xacmlType.POLICY_ID_ATTRIB_NAME, |
---|
45 | xacmlType.RULE_COMBINING_ALG_ID_ATTRIB_NAME): |
---|
46 | attributeValue = elem.attrib.get(attributeName) |
---|
47 | if attributeValue is None: |
---|
48 | raise XMLParseError('No "%s" attribute found in "%s" ' |
---|
49 | 'element' % |
---|
50 | (attributeName, |
---|
51 | xacmlType.ELEMENT_LOCAL_NAME)) |
---|
52 | |
---|
53 | attributeValues.append(attributeValue) |
---|
54 | |
---|
55 | policy.policyId, policy.ruleCombiningAlgId = attributeValues |
---|
56 | |
---|
57 | # Defaults to XACML version 1.0 |
---|
58 | # TODO: version check |
---|
59 | policy.version = (elem.attrib.get(xacmlType.VERSION_ATTRIB_NAME) or |
---|
60 | xacmlType.DEFAULT_XACML_VERSION) |
---|
61 | |
---|
62 | # Parse sub-elements |
---|
63 | for childElem in elem: |
---|
64 | localName = QName.getLocalPart(childElem.tag) |
---|
65 | |
---|
66 | if localName == xacmlType.DESCRIPTION_LOCAL_NAME: |
---|
67 | if childElem.text is not None: |
---|
68 | policy.description = childElem.text.strip() |
---|
69 | |
---|
70 | elif localName == xacmlType.POLICY_DEFAULTS_LOCAL_NAME: |
---|
71 | PolicyDefaultsReader = ReaderFactory.getReader(PolicyDefaults) |
---|
72 | policy.policyDefaults = PolicyDefaultsReader.parse(childElem) |
---|
73 | |
---|
74 | elif localName == Target.ELEMENT_LOCAL_NAME: |
---|
75 | TargetReader = ReaderFactory.getReader(Target) |
---|
76 | policy.target = TargetReader.parse(childElem) |
---|
77 | |
---|
78 | elif localName == xacmlType.COMBINER_PARAMETERS_LOCAL_NAME: |
---|
79 | raise NotImplementedError() |
---|
80 | |
---|
81 | elif localName == xacmlType.RULE_COMBINER_PARAMETERS_LOCAL_NAME: |
---|
82 | raise NotImplementedError() |
---|
83 | |
---|
84 | elif localName == VariableDefinition.ELEMENT_LOCAL_NAME: |
---|
85 | VariableDefinitionReader = ReaderFactory.getReader( |
---|
86 | VariableDefinition) |
---|
87 | variableDefinition = VariableDefinitionReader.parse(childElem) |
---|
88 | |
---|
89 | elif localName == Rule.ELEMENT_LOCAL_NAME: |
---|
90 | RuleReader = ReaderFactory.getReader(Rule) |
---|
91 | policy.rules.append(RuleReader.parse(childElem)) |
---|
92 | |
---|
93 | elif localName == xacmlType.OBLIGATIONS_LOCAL_NAME: |
---|
94 | raise NotImplementedError('Parsing for Obligations element is ' |
---|
95 | 'not implemented') |
---|
96 | |
---|
97 | else: |
---|
98 | raise XMLParseError("XACML Policy child element name %r not " |
---|
99 | "recognised" % localName) |
---|
100 | |
---|
101 | return policy |
---|
102 | |
---|