source: TI12-security/trunk/ndg_xacml/ndg/xacml/parsers/etree/factory.py @ 7661

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/ndg_xacml/ndg/xacml/parsers/etree/factory.py@7661
Revision 7661, 4.2 KB checked in by pjkersha, 10 years ago (diff)

ndg_xacml:

  • added support for custom AttributeValue? DataTypes? - this means that the ESGF GroupRole? attribute type can be added directly into XACML policies.
  • TODO: add support for adding custom functions so that a PDP can correctly apply rules based on custom types like the ESGF GroupRole?.
  • Property svn:keywords set to Id
Line 
1"""NDG XACML ElementTree reader module containing reader base class
2
3NERC DataGrid
4"""
5__author__ = "P J Kershaw"
6__date__ = "19/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$"
12import logging
13log = logging.getLogger(__name__)
14
15from ndg.xacml.parsers import AbstractReaderFactory
16from ndg.xacml.utils.factory import importModuleObject
17from ndg.xacml.utils import VettedDict
18from ndg.xacml.core import XacmlCoreBase
19from ndg.xacml.parsers.etree.reader import ETreeAbstractReader
20
21
22class ETreeReaderClassMap(VettedDict):
23    """Specialised dictionary to hold mappings of XACML classes to their
24    equivalent ElementTree reader classes
25    """
26   
27    def __init__(self):
28        """Force entries to derive from AttributeValue and IDs to
29        be string type
30        """       
31        # Filters are defined as staticmethods but reference via self here to
32        # enable derived class to override them as standard methods without
33        # needing to redefine this __init__ method           
34        VettedDict.__init__(self, self.keyFilter, self.valueFilter)
35       
36    @staticmethod
37    def keyFilter(key):
38        """Enforce XACML base class type keys
39       
40        @param key: URN for attribute
41        @type key: basestring
42        @return: boolean True indicating key is OK
43        @rtype: bool
44        @raise TypeError: incorrect input type
45        """
46        if not issubclass(key, XacmlCoreBase):
47            raise TypeError('Expecting %r derived type for key; got %r' % 
48                            (XacmlCoreBase, type(key))) 
49        return True 
50   
51    @staticmethod
52    def valueFilter(value):
53        """Enforce ElementTree abstract reader derived types for values
54        @param value: attribute value
55        @type value: ndg.xacml.core.attributevalue.AttributeValue derived type
56        @return: boolean True indicating attribute value is correct type
57        @rtype: bool
58        @raise TypeError: incorrect input type
59        """
60        if not issubclass(value, ETreeAbstractReader):
61            raise TypeError('Expecting %r derived type for value; got %r' % 
62                            (ETreeAbstractReader, type(value))) 
63        return True 
64   
65   
66class ReaderFactory(AbstractReaderFactory):
67    """Parser factory for ElementTree based parsers for XACML types"""
68    READER_CLASS_MAP = ETreeReaderClassMap()
69   
70    @classmethod
71    def addReader(cls, xacmlType, readerClass):
72        """Add custom classes and readers
73       
74        @param xacmlType: XACML type to return a parser class for
75        @type xacmlType: type
76        @param readerClass: ElementTree based reader for the input XACML type. 
77        @type readerClass: ndg.xacml.parsers.etree.reader.ETreeAbstractReader
78        derived type
79        """
80        cls.READER_CLASS_MAP[xacmlType] = readerClass
81   
82    @classmethod
83    def getReader(cls, xacmlType):
84        """Return ElementTree based Reader class for the given input
85       
86        @param xacmlType: XACML type to return a parser class for
87        @type xacmlType: type
88        @return: ElementTree based reader for the input XACML type.  The class
89        and module containing the class are inferred from the XACML class name
90        input e.g.
91       
92        ndg.xacml.core.Subject => ndg.xacml.parsers.etree.subjectreader.SubjectReader
93       
94        @rtype: ndg.xacml.parsers.etree.reader.ETreeAbstractReader derived
95        type
96        @raise ImportError: if no reader class found for input type
97        """
98        if xacmlType in cls.READER_CLASS_MAP:
99            # Retrieve from mapping
100            return cls.READER_CLASS_MAP[xacmlType]
101        else:
102            # Infer from the package structure
103            xacmlTypeName = xacmlType.__name__
104            readerClassName = 'ndg.xacml.parsers.etree.%sreader.%sReader' % (
105                                                        xacmlTypeName.lower(),
106                                                        xacmlTypeName)
107            readerClass = importModuleObject(readerClassName)
108            return readerClass
109           
Note: See TracBrowser for help on using the repository browser.