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