Package ndg :: Package xacml :: Package core
[hide private]

Source Code for Package ndg.xacml.core

  1  """NDG XACML core package  
  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: __init__.py 8010 2012-01-30 16:24:06Z rwilkinson $" 
 12  from ndg.xacml.utils import TypedList 
13 14 15 -class XacmlCoreBase(object):
16 """Base class for all XACML types 17 18 @cvar XACML_1_0_NS_PREFIX: XACML version 1.0 namespace prefix 19 @type XACML_1_0_NS_PREFIX: string 20 @cvar XACML_2_0_NS_PREFIX: XACML version 2.0 namespace prefix 21 @type XACML_2_0_NS_PREFIX: string 22 @cvar XMLNS: list of valid XACML namespaces 23 @type XMLNS: tuple 24 @cvar ELEMENT_LOCAL_NAME: XML element local name for the given type 25 @type ELEMENT_LOCAL_NAME: NoneType but implement as string in derived 26 classes 27 28 @ivar __xmlns: XML namespace for the XACML type 29 @type __xmlns: NoneType / basestring 30 31 @ivar __elem: XML element 32 @type __elem: NoneType / dependent on Python XML parser used 33 """ 34 XACML_1_0_NS_PREFIX = "urn:oasis:names:tc:xacml:1.0" 35 XACML_2_0_NS_PREFIX = "urn:oasis:names:tc:xacml:2.0" 36 37 XMLNS = (XACML_1_0_NS_PREFIX, XACML_2_0_NS_PREFIX) 38 39 __slots__ = ('__xmlns', '__reader', '__writer', '__elem') 40 41 ELEMENT_LOCAL_NAME = None 42
43 - def __init__(self):
44 """Element local name check makes this a virtual method 45 46 @raise NotImplementedError: derived classes must set 47 ELEMENT_LOCAL_NAME to a string 48 """ 49 self.__xmlns = None 50 self.__elem = None 51 self.__reader = None 52 self.__writer = None 53 54 if not isinstance(self.__class__.ELEMENT_LOCAL_NAME, basestring): 55 raise NotImplementedError('"ELEMENT_LOCAL_NAME" must be defined in ' 56 'a derived class')
57
58 - def _getXmlns(self):
59 """Get XML Namespace for this XACML type 60 @return: the XML namespace set 61 @rtype: basestring/NoneType 62 """ 63 return self.__xmlns
64
65 - def _setXmlns(self, value):
66 """Set XML Namespace for this XACML type 67 @param value: the XML namespace to set 68 @type value: basestring/NoneType 69 """ 70 if not isinstance(value, basestring): 71 raise TypeError('Expecting string type for "xmlns" ' 72 'attribute; got %r' % type(value)) 73 self.__xmlns = value
74 75 xmlns = property(_getXmlns, _setXmlns, 76 doc="XML Namespace for policy the document") 77 78 @property
79 - def isValidXmlns(self):
80 """Check XML namespace fits with the known XACML namespaces 81 @return: True if valid, False otherwise 82 @rtype: bool 83 """ 84 return self.xmlns in XacmlCoreBase.XMLNS
85 86 @property
87 - def elem(self):
88 """XML Node for as represented by parser/writer specified with the 89 reader/writer attributes. Readers of context elements should set this 90 element if a policy uses AttributeSelectors to do XPath queries into 91 the request context 92 """ 93 return self.__elem
94 95 @elem.setter
96 - def elem(self, value):
97 """"XML Node for as represented by parser/writer specified with the 98 reader/writer attributes 99 100 @param value: XML node instance 101 @type value: type (governed by reader/writer set for this XACML object) 102 """ 103 self.__elem = value
104
105 - def __getstate__(self):
106 '''Enable pickling 107 108 @return: object's attribute dictionary 109 @rtype: dict 110 ''' 111 _dict = {} 112 for attrName in XacmlCoreBase.__slots__: 113 # Ugly hack to allow for derived classes setting private member 114 # variables 115 if attrName.startswith('__'): 116 attrName = "_XacmlCoreBase" + attrName 117 118 _dict[attrName] = getattr(self, attrName) 119 120 return _dict
121
122 123 -class XacmlPolicyBase(XacmlCoreBase):
124 """Base class for policy types 125 126 @cvar XACML_2_0_POLICY_NS: XACML 2.0 policy XML namespace 127 @type XACML_2_0_POLICY_NS: string 128 """ 129 XACML_2_0_POLICY_NS = (XacmlCoreBase.XACML_2_0_NS_PREFIX + 130 ":policy:schema:os") 131 __slots__ = () 132
133 - def __init__(self):
134 """Initialise parent class xmlns attribute based on this classes' 135 policy namespace 136 """ 137 super(XacmlPolicyBase, self).__init__() 138 self.xmlns = XacmlPolicyBase.XACML_2_0_POLICY_NS
139
140 141 -class TargetChildBase(XacmlPolicyBase):
142 """Abstract Base class for XACML Policy Subject, Resource, Action and 143 Environment types: e.g. ndg.xacml.core.subject.Subject 144 145 @cvar MATCH_TYPE: Set the type for match attributes in the derived class 146 implementation e.g. ResourceMatch, SubjectMatch etc. 147 @type MATCH_TYPE: NoneType - derived class must implement 148 149 @ivar __matches: list of matches for this target 150 @type __matches: ndg.xacml.core.utils.TypedList 151 """ 152 MATCH_TYPE = None 153 154 __slots__ = ('__matches', ) 155
156 - def __init__(self):
157 super(TargetChildBase, self).__init__() 158 159 # Derived types can specify the type for matches via the MATCH_TYPE 160 # class variable 161 if self.__class__.MATCH_TYPE is None: 162 raise NotImplementedError('Match type attribute must be specified ' 163 'in a derived class') 164 self.__matches = TypedList(self.__class__.MATCH_TYPE)
165 166 @property
167 - def matches(self):
168 """Get matches list for this target 169 """ 170 return self.__matches
171 172 173 XACML_1_0_PREFIX = "urn:oasis:names:tc:xacml:1.0:"
174 175 -class Identifiers(object):
176 """XACML Identifiers"""
177 - class Subject(object):
178 """XAMCL Subject Identifiers""" 179 AUTHN_LOCALITY_DNS_NAME = XACML_1_0_PREFIX + \ 180 "subject:authn-locality:dns-name" 181 AUTHN_LOCALITY_IP_ADDRESS = XACML_1_0_PREFIX + \ 182 "subject:authn-locality:ip-address" 183 AUTHN_METHOD = XACML_1_0_PREFIX + "subject:authentication-method" 184 AUTHN_TIME = XACML_1_0_PREFIX + "subject:authentication-time" 185 KEY_INFO = XACML_1_0_PREFIX + "subject:key-info" 186 REQUEST_TIME = XACML_1_0_PREFIX + "subject:request-time" 187 SESSION_START_TIME = XACML_1_0_PREFIX + "subject:session-start-time" 188 SUBJECT_ID = XACML_1_0_PREFIX + "subject:subject-id" 189 SUBJECT_ID_QUALIFIER = XACML_1_0_PREFIX + "subject:subject-id-qualifier"
190
191 - class SubjectCategory(object):
192 """XACML Subject Category Identifiers""" 193 ACCESS_SUBJECT = XACML_1_0_PREFIX + "subject-category:access-subject" 194 CODEBASE = XACML_1_0_PREFIX + "subject-category:codebase" 195 INTERMEDIARY_SUBJECT = XACML_1_0_PREFIX + \ 196 "subject-category:intermediary-subject" 197 RECIPIENT_SUBJECT = XACML_1_0_PREFIX + \ 198 "subject-category:recipient-subject" 199 REQUESTING_MACHINE = XACML_1_0_PREFIX + \ 200 "subject-category:requesting-machine"
201
202 - class Resource(object):
203 """XACML Resource Identifiers""" 204 RESOURCE_LOCATION = XACML_1_0_PREFIX + "resource:resource-location" 205 RESOURCE_ID = XACML_1_0_PREFIX + "resource:resource-id" 206 SIMPLE_FILE_NAME = XACML_1_0_PREFIX + "resource:simple-file-name"
207
208 - class Action(object):
209 """XACML Action Identifiers""" 210 ACTION_ID = XACML_1_0_PREFIX + "action:action-id" 211 IMPLIED_ACTION = XACML_1_0_PREFIX + "action:implied-action"
212
213 - class Environment(object):
214 """XACML Environment Identifiers""" 215 CURRENT_TIME = XACML_1_0_PREFIX + "environment:current-time" 216 CURRENT_DATE = XACML_1_0_PREFIX + "environment:current-date" 217 CURRENT_DATETIME = XACML_1_0_PREFIX + "environment:current-dateTime"
218