source: TI12-security/trunk/NDG_XACML/ndg/xacml/core/__init__.py @ 6790

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/NDG_XACML/ndg/xacml/core/__init__.py@6793
Revision 6790, 3.7 KB checked in by pjkersha, 10 years ago (diff)
  • Added marshalling function for condition <Apply> to marshall inputs for given functionId.
  • implemented string-at-least-one-member-of function.
Line 
1"""NDG XACML core package
2
3NERC 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: $"
12from ndg.xacml.utils import TypedList
13
14   
15class XacmlCoreBase(object):
16    """Base class for all XACML types"""
17    XACML_1_0_NS_PREFIX = "urn:oasis:names:tc:xacml:1.0"
18    XACML_2_0_NS_PREFIX = "urn:oasis:names:tc:xacml:2.0"
19
20    __slots__ = ('__xmlns', '__reader', '__writer', '__elem')
21
22    ELEMENT_LOCAL_NAME = None
23   
24    def __init__(self):
25        self.__xmlns = None
26        self.__reader = None
27        self.__writer = None
28        self.__elem = None
29       
30        if not isinstance(self.__class__.ELEMENT_LOCAL_NAME, basestring):
31            raise NotImplementedError('"ELEMENT_LOCAL_NAME" must be defined in '
32                                      'a derived class')
33       
34    def _getXmlns(self):
35        """XML Namespace for this XACML type"""
36        return self.__xmlns
37
38    def _setXmlns(self, value):
39        """XML Namespace for this XACML type"""
40        if not isinstance(value, basestring):
41            raise TypeError('Expecting string type for "xmlns" '
42                            'attribute; got %r' % type(value))
43        self.__xmlns = value
44
45    xmlns = property(_getXmlns, _setXmlns, 
46                     doc="XML Namespace for policy the document")
47   
48    @property
49    def isValidXmlns(self):
50        return self.xmlns in XacmlCoreBase.XMLNS
51
52    def read(self, obj):
53        """Read using callable assinged to reader property"""
54        if self.__reader is None:
55            raise AttributeError('No reader set for %r' % self.__class__)
56       
57        self.__reader(self, obj)
58
59    @classmethod
60    def Read(cls, obj):
61        """Construct a new Policy""" 
62        xacmlObj = cls()
63        xacmlObj.read(obj)
64        return xacmlObj
65   
66    def write(self, obj):
67        """Read using callable assinged to reader property"""
68        if self.__writer is None:
69            raise AttributeError('No writer set for %r' % self.__class__)
70       
71        self.__writer(self, obj)
72       
73    @property
74    def elem(self):
75        """XML Node for as represented by parser/writer specified with the
76        reader/writer attributes.  Readers of context elements should set this
77        element if a policy uses AttributeSelectors to do XPath queries into
78        the request context
79        """
80        return self.__elem
81       
82    @elem.setter
83    def elem(self, value):
84        """"XML Node for as represented by parser/writer specified with the
85        reader/writer attributes
86       
87        @param value: XML node instance
88        @type value: type (governed by reader/writer set for this XACML object)
89        """
90        self.__elem = value
91           
92
93class XacmlPolicyBase(XacmlCoreBase):
94    """Base class for policy types"""
95    XACML_2_0_POLICY_NS = (XacmlCoreBase.XACML_2_0_NS_PREFIX +
96                           ":policy:schema:os")
97   
98    def __init__(self):
99        super(XacmlPolicyBase, self).__init__()
100        self.xmlns = XacmlPolicyBase.XACML_2_0_POLICY_NS
101           
102       
103class TargetChildBase(XacmlPolicyBase):
104    """Base type for XACML Policy Subject, Resource, Action and Environment
105    types"""
106    MATCH_TYPE = None
107   
108    __slots__ = ('__matches', )
109   
110    def __init__(self):
111        super(TargetChildBase, self).__init__()
112       
113        # Derived types can specify the type for matches via the MATCH_TYPE
114        # class variable
115        self.__matches = TypedList(self.__class__.MATCH_TYPE)
116       
117    @property
118    def matches(self):
119        return self.__matches
Note: See TracBrowser for help on using the repository browser.