1 '''
2 Created on 24 Feb 2010
3
4 @author: pjkersha
5 '''
6 from ndg.xacml.utils import TypedList
7 """NDG Security Target type definition
8
9 NERC DataGrid
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: target.py 7955 2011-12-21 18:29:45Z rwilkinson $"
18 import logging
19 log = logging.getLogger(__name__)
20
21 from ndg.xacml.core import XacmlCoreBase
22 from ndg.xacml.core.action import Action
23 from ndg.xacml.core.resource import Resource
24 from ndg.xacml.core.subject import Subject
25 from ndg.xacml.core.environment import Environment
26
27
28 -class Target(XacmlCoreBase):
29 """XACML Target element
30
31 @cvar ELEMENT_LOCAL_NAME: XML local name for this element
32 @type ELEMENT_LOCAL_NAME: string
33 @cvar SUBJECTS_ELEMENT_LOCAL_NAME: XML local name for the subjects element
34 @type SUBJECTS_ELEMENT_LOCAL_NAME: string
35 @cvar ACTIONS_ELEMENT_LOCAL_NAME: XML local name for the actions element
36 @type ACTIONS_ELEMENT_LOCAL_NAME: string
37 @cvar RESOURCES_ELEMENT_LOCAL_NAME: XML local name for the resources element
38 @type RESOURCES_ELEMENT_LOCAL_NAME: string
39 @cvar ENVIRONMENTS_ELEMENT_LOCAL_NAME: XML local name for the environments
40 element
41 @type ENVIRONMENTS_ELEMENT_LOCAL_NAME: string
42 @cvar CHILD_ATTRS: list of the XML child element names for <Target/>
43 @type CHILD_ATTRS: tuple
44
45 @ivar __subjects: list of subjects for this target
46 @type __subjects: ndg.xacml.utils.TypedList
47 @ivar __resources: list of resources for this target
48 @type __resources: ndg.xacml.utils.TypedList
49 @ivar __actions: list of actions for this target
50 @type __actions: ndg.xacml.utils.TypedList
51 @ivar __environments: list of environment settings for this target
52 @type __environments: ndg.xacml.utils.TypedList
53 """
54 ELEMENT_LOCAL_NAME = "Target"
55 SUBJECTS_ELEMENT_LOCAL_NAME = "Subjects"
56 ACTIONS_ELEMENT_LOCAL_NAME = "Actions"
57 RESOURCES_ELEMENT_LOCAL_NAME = "Resources"
58 ENVIRONMENTS_ELEMENT_LOCAL_NAME = "Environments"
59 CHILD_ATTRS = ('subjects', 'resources', 'actions', 'environments')
60
61 __slots__ = ('__subjects', '__resources', '__actions', '__environments')
62
69
70 @property
72 """Get subjects
73 @return: list of subjects for this target
74 @rtype: ndg.xacml.utils.TypedList
75 """
76 return self.__subjects
77
78 @property
80 """Get resources
81 @return: list of resources for this target
82 @rtype: ndg.xacml.utils.TypedList
83 """
84 return self.__resources
85
86 @property
88 """Get actions
89 @return: list of actions for this target
90 @rtype: ndg.xacml.utils.TypedList
91 """
92 return self.__actions
93
94 @property
96 """Get environments
97 @return: list of environments for this target
98 @rtype: ndg.xacml.utils.TypedList
99 """
100 return self.__environments
101
102 - def match(self, request):
103 """Generic method to match a <Target> element to the request context
104
105 @param request: XACML request context
106 @type request: ndg.xacml.core.context.request.Request
107 @return: True if request context matches the given target,
108 False otherwise
109 @rtype: bool
110 """
111
112
113
114
115
116
117
118
119
120
121
122
123
124 statusValues = [False]*len(self.__class__.CHILD_ATTRS)
125
126
127
128 for i, attrName in enumerate(self.__class__.CHILD_ATTRS):
129
130
131
132
133
134
135 targetElem = getattr(self, attrName)
136 if len(targetElem) == 0:
137 statusValues[i] = True
138 continue
139
140
141
142
143
144
145
146
147
148
149
150
151 for targetSubElem in targetElem:
152 if self._matchChild(targetSubElem, request):
153
154
155
156 statusValues[i] = True
157
158
159
160
161 return all(statusValues)
162
164 """Match a request child element (a <Subject>, <Resource>, <Action> or
165 <Environment>) with the corresponding target's <Subject>, <Resource>,
166 <Action> or <Environment>.
167
168 @param targetChild: Target Subject, Resource, Action or Environment
169 object
170 @type targetChild: ndg.xacml.core.TargetChildBase
171 @param request: Request context object
172 @type request: ndg.xacml.core.context.request.Request
173 @return: True if request context matches something in the target
174 @rtype: bool
175 @raise UnsupportedStdFunctionError: policy references a function type
176 which is in the XACML spec. but is not supported by this implementation
177 @raise UnsupportedFunctionError: policy references a function type which
178 is not supported by this implementation
179 """
180 if targetChild is None:
181
182 return True
183
184 matchStatusValues = [True]*len(targetChild.matches)
185
186
187
188
189
190
191
192
193
194 for i, childMatch in enumerate(targetChild.matches):
195 matchStatusValues[i] = childMatch.evaluate(request)
196
197
198 return all(matchStatusValues)
199