Changeset 6731 for TI12-security/trunk


Ignore:
Timestamp:
16/03/10 10:45:01 (10 years ago)
Author:
pjkersha
Message:

Work on XACML !ETree based parsing. Added first basic unit test.

Location:
TI12-security/trunk/NDGSecurity/python
Files:
10 added
7 edited

Legend:

Unmodified
Added
Removed
  • TI12-security/trunk/NDGSecurity/python/ndg_security_common/ndg/security/common/authz/xacml/environment.py

    r6643 r6731  
    99 
    1010class Environment(RequestPropertyBase): 
    11     MATCH_TYPE = EnviornmentMatch 
     11    MATCH_TYPE = EnvironmentMatch 
    1212    ELEMENT_LOCAL_NAME = 'Environment' 
    1313     
  • TI12-security/trunk/NDGSecurity/python/ndg_security_common/ndg/security/common/authz/xacml/etree/__init__.py

    r6730 r6731  
     1from xml.etree import ElementTree 
     2 
    13# Generic ElementTree Helper classes 
    24class QName(ElementTree.QName): 
  • TI12-security/trunk/NDGSecurity/python/ndg_security_common/ndg/security/common/authz/xacml/etree/reader.py

    r6730 r6731  
    66import logging 
    77log = logging.getLogger(__name__) 
     8from abc import ABCMeta, abstractmethod 
    89 
    910from xml.etree import ElementTree 
    1011 
    11 from ndg.security.common.authz.xacml import PolicyComponent, XMLParseError 
    12 from ndg.security.common.authz.xacml.policy import Policy 
    13 from ndg.security.common.authz.xacml.etree import QName 
    1412 
    15  
    16 class AbstractReader(object): 
    17     """ElementTree implementation of XACML reader""" 
    18      
     13class AbstractReader: 
     14    """Abstract base class for ElementTree implementation of XACML reader""" 
     15    __metaclass__ = ABCMeta 
     16      
    1917    def __init__(self): 
    2018        self.__namespace_map_backup = ElementTree._namespace_map.copy() 
     
    2523        ElementTree._namespace_map = self.__namespace_map_backup 
    2624         
     25    @classmethod 
     26    def __subclasshook__(cls, C): 
     27        """Derived class must implement __call__""" 
     28        if cls is AbstractReader: 
     29            if any("__call__" in B.__dict__ for B in C.__mro__): 
     30                return True 
     31             
     32        return NotImplemented 
     33         
     34    @abstractmethod 
    2735    def __call__(self, obj): 
    2836        """Abstract Parse XACML method 
     
    3139        raise NotImplementedError() 
    3240     
    33     def _parse(self, obj): 
     41    @classmethod 
     42    def parse(cls, obj): 
     43        """Parse from input object and return new XACML object""" 
     44        reader = cls() 
     45        return reader(obj) 
     46     
     47    @staticmethod 
     48    def _parse(obj): 
    3449        """Parse helper method 
    3550        @param obj: input object to parse 
     
    4560             
    4661        return elem 
     62                     
    4763     
    48      
    49 class PolicyReader(AbstractReader): 
    50     """Parse a Polciy Document using ElementTree 
    51     """ 
     64class RuleReader(object): 
    5265    def __call__(self, obj): 
    53         """Parse policy object""" 
    54         elem = self._parse(obj) 
    55          
    56         policy = Policy() 
    57         cls = Policy 
    58          
    59         localName = QName.getLocalPart(elem.tag) 
    60         if localName != cls.DEFAULT_ELEMENT_LOCAL_NAME: 
    61             raise XMLParseError("No \"%s\" element found" % 
    62                                     cls.DEFAULT_ELEMENT_LOCAL_NAME) 
    63          
    64         # Unpack attributes from top-level element 
    65         attributeValues = [] 
    66         for attributeName in (cls.POLICY_ID_ATTRIB_NAME, 
    67                               cls.RULE_COMBINING_ALG_ID_ATTRIB_NAME): 
    68             attributeValue = elem.attrib.get(attributeName) 
    69             if attributeValue is None: 
    70                 raise XMLParseError('No "%s" attribute found in "%s" ' 
    71                                         'element' % 
    72                                         (attributeName, 
    73                                          cls.DEFAULT_ELEMENT_LOCAL_NAME)) 
    74                  
    75             attributeValues.append(attributeValue)  
    76                     
    77         # Parse element attributes 
    78         policy.id, policy.ruleCombiningAlg = attributeValues 
    79          
    80         # Parse sub-elements 
    81         for childElem in elem: 
    82             localName = QName.getLocalPart(childElem.tag) 
    83              
    84             if localName == cls.DESCRIPTION_LOCALNAME: 
    85                 policy.description =  
    86             elif localName == cls.TARGET_LOCALNAME: 
    87                 pass 
    88             else: 
    89                 raise XMLParseError("XACML Policy child element name %r not " 
    90                                     "recognised" % localName) 
     66        pass 
    9167 
    92          
    93         return policy 
     68#AbstractReader.register(RuleReader) 
     69 
     70class VariableDefinitionReader(object): 
     71    def __call__(self, obj): 
     72        pass 
  • TI12-security/trunk/NDGSecurity/python/ndg_security_common/ndg/security/common/authz/xacml/policy.py

    r6730 r6731  
    4040class Policy(PolicyComponent): 
    4141    """NDG MSI Policy."""  
     42    DEFAULT_XACML_VERSION = "1.0" 
     43    ELEMENT_LOCAL_NAME = "Policy" 
    4244    POLICY_ID_ATTRIB_NAME = "PolicyId" 
    4345    RULE_COMBINING_ALG_ID_ATTRIB_NAME = "RuleCombiningAlgId" 
    44        
    45     DESCRIPTION_LOCALNAME = "Description" 
    46     TARGET_LOCALNAME = "Target" 
    47     POLICY_DEFAULTS_LOCALNAME = "PolicyDefaults" 
    48     OBLIGATIONS_LOCALNAME = "Obligations" 
    49     RULE_LOCALNAME = "Rule" 
     46    VERSION_ATTRIB_NAME = "Version" 
     47 
     48    DESCRIPTION_LOCAL_NAME = "Description" 
     49    POLICY_DEFAULTS_LOCAL_NAME = "PolicyDefaults" 
     50    COMBINER_PARAMETERS_LOCAL_NAME = "CombinerParameters" 
     51    RULE_COMBINER_PARAMETERS_LOCAL_NAME = "RuleCombinerParameters" 
     52    OBLIGATIONS_LOCAL_NAME = "Obligations" 
    5053     
    5154    # Plan to support permit overrides in a future release 
  • TI12-security/trunk/NDGSecurity/python/ndg_security_common/ndg/security/common/authz/xacml/resource.py

    r6643 r6731  
    44@author: pjkersha 
    55''' 
    6 from ndg.security.common.authz.xacml import PolicyComponent 
     6from ndg.security.common.authz.xacml import RequestPropertyBase 
    77from ndg.security.common.authz.xacml.match import ResourceMatch 
    88 
  • TI12-security/trunk/NDGSecurity/python/ndg_security_common/ndg/security/common/authz/xacml/subject.py

    r6643 r6731  
    44@author: pjkersha 
    55''' 
    6 from ndg.security.common.authz.xacml import PolicyComponent 
     6from ndg.security.common.authz.xacml import RequestPropertyBase 
    77from ndg.security.common.authz.xacml.match import SubjectMatch 
    88 
  • TI12-security/trunk/NDGSecurity/python/ndg_security_common/ndg/security/common/authz/xacml/target.py

    r6643 r6731  
    1313 
    1414class Target(PolicyComponent): 
     15    ELEMENT_LOCAL_NAME = "Target" 
     16     
    1517    __slots__ = ('__actions', '_resources', '__actions', '__environments') 
    1618     
Note: See TracChangeset for help on using the changeset viewer.