source: TI12-security/trunk/NDG_XACML/ndg/xacml/core/target.py @ 6746

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/NDG_XACML/ndg/xacml/core/target.py@6746
Revision 6746, 5.5 KB checked in by pjkersha, 10 years ago (diff)

Added more !ETree readers for the different types.

Line 
1'''
2Created on 24 Feb 2010
3
4@author: pjkersha
5'''
6from ndg.xacml.utils import TypedList
7"""NDG Security Target type definition
8
9NERC DataGrid Project
10"""
11__author__ = "P J Kershaw"
12__date__ = "25/02/10"
13__copyright__ = "(C) 2010 Science and Technology Facilities Council"
14__contact__ = "Philip.Kershaw@stfc.ac.uk"
15__license__ = "BSD - see LICENSE file in top-level directory"
16__contact__ = "Philip.Kershaw@stfc.ac.uk"
17__revision__ = "$Id: $"
18from ndg.xacml.core import PolicyComponent
19from ndg.xacml.core.action import Action
20from ndg.xacml.core.resource import Resource
21from ndg.xacml.core.subject import Subject
22from ndg.xacml.core.environment import Environment
23
24
25class Target(PolicyComponent):
26    ELEMENT_LOCAL_NAME = "Target"
27    SUBJECTS_ELEMENT_LOCAL_NAME = "Subjects"
28    ACTIONS_ELEMENT_LOCAL_NAME = "Actions"
29    RESOURCES_ELEMENT_LOCAL_NAME = "Resources"
30    ENVIRONMENTS_ELEMENT_LOCAL_NAME = "Environments"
31   
32    __slots__ = ('__subjects', '__resources', '__actions', '__environments')
33   
34    def __init__(self):
35        self.__subjects = TypedList(Subject)
36        self.__resources = TypedList(Resource)
37        self.__actions = TypedList(Action)
38        self.__environments = TypedList(Environment)
39   
40    @property
41    def subjects(self):
42        return self.__subjects
43   
44    @property
45    def resources(self):
46        return self.__resources
47   
48    @property
49    def actions(self):
50        return self.__actions
51   
52    @property
53    def environments(self):
54        return self.__environments
55   
56   
57class _Target(PolicyComponent):
58    """Define access behaviour for a resource match a given URI pattern"""
59    URI_PATTERN_LOCALNAME = "URIPattern"
60    ATTRIBUTES_LOCALNAME = "Attributes"
61    ATTRIBUTE_AUTHORITY_LOCALNAME = "AttributeAuthority"
62   
63    __slots__ = (
64        '__uriPattern',
65        '__attributes',
66        '__regEx'       
67    )
68     
69    def __init__(self):
70        super(Target, self).__init__()
71        self.__uriPattern = None
72        self.__attributes = []
73        self.__regEx = None
74       
75    def getUriPattern(self):
76        return self.__uriPattern
77
78    def setUriPattern(self, value):
79        if not isinstance(value, basestring):
80            raise TypeError('Expecting string type for "uriPattern" '
81                            'attribute; got %r' % type(value))
82        self.__uriPattern = value
83
84    uriPattern = property(getUriPattern, 
85                          setUriPattern, 
86                          doc="URI Pattern to match this target")
87
88    def getAttributes(self):
89        return self.__attributes
90
91    def setAttributes(self, value):
92        if (not isinstance(value, TypedList) and 
93            not issubclass(value.elementType, Attribute.__class__)):
94            raise TypeError('Expecting TypedList(Attribute) for "attributes" '
95                            'attribute; got %r' % type(value))
96        self.__attributes = value
97
98    attributes = property(getAttributes, 
99                          setAttributes, 
100                          doc="Attributes restricting access to this target")
101
102    def getRegEx(self):
103        return self.__regEx
104
105    def setRegEx(self, value):
106        self.__regEx = value
107
108    regEx = property(getRegEx, setRegEx, doc="RegEx's Docstring")
109       
110    def parse(self, root):
111       
112        self.xmlns = QName.getNs(root.tag)
113        version1_0attributeAuthorityURI = None
114       
115        for elem in root:
116            localName = QName.getLocalPart(elem.tag)
117            if localName == Target.URI_PATTERN_LOCALNAME:
118                self.uriPattern = elem.text.strip()
119                self.regEx = re.compile(self.uriPattern)
120               
121            elif localName == Target.ATTRIBUTES_LOCALNAME:
122                for attrElem in elem:
123                    if self.xmlns == Target.VERSION_1_1_XMLNS:
124                        self.attributes.append(Attribute.Parse(attrElem))
125                    else:
126                        attribute = Attribute()
127                        attribute.name = attrElem.text.strip()
128                        self.attributes.append(attribute)
129                   
130            elif localName == Target.ATTRIBUTE_AUTHORITY_LOCALNAME:
131                # Expecting first element to contain the URI
132                warnings.warn(
133                        Target.ATTRIBUTE_AUTHORITY_LOCALNAME_DEPRECATED_MSG,
134                        PendingDeprecationWarning)
135               
136                version1_0attributeAuthorityURI = elem[-1].text.strip()
137            else:
138                raise TargetParseError("Invalid Target attribute: %s" % 
139                                       localName)
140               
141        if self.xmlns == Target.VERSION_1_0_XMLNS:
142            msg = ("Setting all attributes with Attribute Authority "
143                   "URI set read using Version 1.0 schema.  This will "
144                   "be deprecated in future releases")
145           
146            warnings.warn(msg, PendingDeprecationWarning)
147            log.warning(msg)
148           
149            if version1_0attributeAuthorityURI is None:
150                raise TargetParseError("Assuming version 1.0 schema "
151                                       "for Attribute Authority URI setting "
152                                       "but no URI has been set")
153               
154            for attribute in self.attributes:
155                attribute.attributeAuthorityURI = \
156                    version1_0attributeAuthorityURI
157   
158    @classmethod
159    def Parse(cls, root):
160        resource = cls()
161        resource.parse(root)
162        return resource
163   
164    def __str__(self):
165        return str(self.uriPattern)
166
Note: See TracBrowser for help on using the repository browser.