Package ndg :: Package xacml :: Package finder :: Module urlpolicyfinder
[hide private]

Source Code for Module ndg.xacml.finder.urlpolicyfinder

  1  """NDG XACML policy finder interpreting ID references as URLs 
  2   
  3  NERC DataGrid 
  4  """ 
  5  __author__ = "R B Wilkinson" 
  6  __date__ = "02/11/11" 
  7  __copyright__ = "(C) 2011 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$" 
 12  import os 
 13   
 14  from ndg.xacml.core.policybase import PolicyBase 
 15  from ndg.xacml.finder.policyfinderbase import PolicyFinderBase 
 16  from ndg.xacml.parsers import XMLParseError 
 17  import ndg.xacml.utils.urlfetcher as urlfetcher 
 18   
19 -class UrlPolicyFinder(PolicyFinderBase):
20 ''' 21 Concrete subclass of PolicyFinderBase that interprets ID references as URLs. 22 ''' 23 24 # File scheme prefix 25 _FILE_SCHEME = 'file://' 26 # Other recognised scheme prefixes 27 _NON_FILE_SCHEMES = ['ftp://', 'http://', 'https://'] 28 # Scheme to use if reference has no scheme prefix 29 _DEFAULT_SCHEME = _FILE_SCHEME 30 # String following scheme in URL 31 _SCHEME_SEPARATOR = '://' 32 # Path start implying relative path. 33 _RELATIVE_PATH_PREFIX = '.' + os.path.sep 34
35 - def __init__(self, basePath):
36 ''' 37 @param basePath: base path for resolving relative references 38 @type basePath str 39 ''' 40 super(UrlPolicyFinder, self).__init__() 41 self.basePath = basePath
42
43 - def findPolicy(self, policyIdReference, common):
44 """ 45 Retrieves a policy for a specified policy ID. 46 @param policyIdReference: policy ID reference 47 @type policyIdReference: str 48 @param common: parsing common data 49 @type common: from ndg.xacml.parsers.common.Common 50 @return: policy 51 @rtype: ndg.xacml.core.policy.Policy 52 @raise XMLParseError: policy of specified ID not found 53 """ 54 policy = self.policyMap.get(policyIdReference, None) 55 if policy is None: 56 policy = self._findPolicyFromReference(policyIdReference, common) 57 # Look up the policy by ID - this means that it will only be found if 58 # the ID declared in the document matches the required ID. 59 policy = self.policyMap.get(policyIdReference, None) 60 if policy is None: 61 raise XMLParseError("Referenced Policy of ID %r not found" % 62 policyIdReference) 63 return policy
64
65 - def findPolicySet(self, policySetIdReference, common):
66 """ 67 Retrieves a policy set for a specified policy set ID. 68 @param policySetIdReference: policy set ID reference 69 @type policySetIdReference: str 70 @param common: parsing common data 71 @type common: from ndg.xacml.parsers.common.Common 72 @return: policy set 73 @rtype: ndg.xacml.core.policy.PolicySet 74 @raise XMLParseError: policy set of specified ID not found 75 """ 76 policySet = self.policySetMap.get(policySetIdReference, None) 77 if policySet is None: 78 policySet = self._findPolicyFromReference(policySetIdReference, 79 common) 80 # Look up the policy set by ID - this means that it will only be found 81 # if the ID declared in the document matches the required ID. 82 policySet = self.policySetMap.get(policySetIdReference, None) 83 if policySet is None: 84 raise XMLParseError("Referenced PolicySet of ID %r not found" % 85 policySetIdReference) 86 return policySet
87
88 - def _findPolicyFromReference(self, reference, common):
89 """ 90 Retrieves a policy or policy set for a specified ID that has not been 91 read already by interpreting the reference as a URL. 92 @param reference: ID reference 93 @type reference: str 94 @param common: parsing common data 95 @type common: from ndg.xacml.parsers.common.Common 96 @return: policy set 97 @rtype: ndg.xacml.core.policy.PolicySet 98 @raise XMLParseError: policy set of specified ID not found 99 """ 100 url = self._makeUrlFromReference(reference) 101 policyDoc = urlfetcher.fetch_stream_from_url(url) 102 policy = PolicyBase.fromNestedSource(policyDoc, common) 103 return policy
104
105 - def _makeUrlFromReference(self, reference):
106 """ 107 Makes a URL from the reference. If it already begins with a known scheme 108 prefix, the reference is not modified, otherwise it is made into a file 109 URL (relative to the base path if it does not start with a path 110 separator). 111 file://dir/file and file://./dir/file are treated as relative paths. 112 file:///dir/file is treated as an absolute path. 113 @param reference: ID reference 114 @type reference: str 115 @return: URL 116 @rtype: str 117 """ 118 # See if reference looks like a URL for a known scheme. 119 if reference.startswith(self._FILE_SCHEME): 120 path = reference[len(self._FILE_SCHEME):] 121 if path.startswith(self._RELATIVE_PATH_PREFIX): 122 # Relative path - convert to absolute. 123 path = path[len(self._RELATIVE_PATH_PREFIX):] 124 url = self._FILE_SCHEME + os.path.join(self.basePath, path) 125 elif not path.startswith(os.path.sep): 126 # Relative path - convert to absolute. 127 url = self._FILE_SCHEME + os.path.join(self.basePath, path) 128 else: 129 url = reference 130 elif [True for scheme in self._NON_FILE_SCHEMES 131 if reference.startswith(scheme)]: 132 url = reference 133 else: 134 # No scheme so use default scheme. 135 if reference.startswith(os.path.sep): 136 url = self._DEFAULT_SCHEME + reference 137 else: 138 url = (self._DEFAULT_SCHEME + 139 os.path.join(self.basePath, reference)) 140 return url
141