From 0be7a3d4e0647dc0d94a34e4fc2f8c364de46602 Mon Sep 17 00:00:00 2001 From: asteroide Date: Thu, 24 Sep 2015 14:39:09 +0200 Subject: Update code from KeystoneMiddleware Github repository (Master). Change-Id: Id28c5bf48b3dbb6c8a08e66411b5785029f6857d --- .../keystonemiddleware/audit.py | 51 +++++++++++++++------- 1 file changed, 35 insertions(+), 16 deletions(-) (limited to 'keystonemiddleware-moon/keystonemiddleware/audit.py') diff --git a/keystonemiddleware-moon/keystonemiddleware/audit.py b/keystonemiddleware-moon/keystonemiddleware/audit.py index f44da80d..e3536092 100644 --- a/keystonemiddleware-moon/keystonemiddleware/audit.py +++ b/keystonemiddleware-moon/keystonemiddleware/audit.py @@ -30,7 +30,7 @@ import sys from oslo_config import cfg from oslo_context import context try: - import oslo.messaging + import oslo_messaging messaging = True except ImportError: messaging = False @@ -46,6 +46,7 @@ from pycadf import reporterstep from pycadf import resource from pycadf import tag from pycadf import timestamp +import six from six.moves import configparser from six.moves.urllib import parse as urlparse import webob.dec @@ -79,6 +80,16 @@ AuditMap = collections.namedtuple('AuditMap', 'default_target_endpoint_type']) +# NOTE(blk-u): Compatibility for Python 2. SafeConfigParser and +# SafeConfigParser.readfp are deprecated in Python 3. Remove this when we drop +# support for Python 2. +if six.PY2: + class _ConfigParser(configparser.SafeConfigParser): + read_file = configparser.SafeConfigParser.readfp +else: + _ConfigParser = configparser.ConfigParser + + class OpenStackAuditApi(object): def __init__(self, cfg_file): @@ -90,8 +101,8 @@ class OpenStackAuditApi(object): if cfg_file: try: - map_conf = configparser.SafeConfigParser() - map_conf.readfp(open(cfg_file)) + map_conf = _ConfigParser() + map_conf.read_file(open(cfg_file)) try: default_target_endpoint_type = map_conf.get( @@ -130,12 +141,19 @@ class OpenStackAuditApi(object): """Take a given Request, parse url path to calculate action type. Depending on req.method: - if POST: path ends with 'action', read the body and use as action; - path ends with known custom_action, take action from config; - request ends with known path, assume is create action; - request ends with unknown path, assume is update action. - if GET: request ends with known path, assume is list action; - request ends with unknown path, assume is read action. + + if POST: + + - path ends with 'action', read the body and use as action; + - path ends with known custom_action, take action from config; + - request ends with known path, assume is create action; + - request ends with unknown path, assume is update action. + + if GET: + + - request ends with known path, assume is list action; + - request ends with unknown path, assume is read action. + if PUT, assume update action. if DELETE, assume delete action. if HEAD, assume read action. @@ -190,13 +208,13 @@ class OpenStackAuditApi(object): endp['name'])), admin_endp=endpoint.Endpoint( name='admin', - url=endp['endpoints'][0]['adminURL']), + url=endp['endpoints'][0].get('adminURL', taxonomy.UNKNOWN)), private_endp=endpoint.Endpoint( name='private', - url=endp['endpoints'][0]['internalURL']), + url=endp['endpoints'][0].get('internalURL', taxonomy.UNKNOWN)), public_endp=endpoint.Endpoint( name='public', - url=endp['endpoints'][0]['publicURL'])) + url=endp['endpoints'][0].get('publicURL', taxonomy.UNKNOWN))) return service @@ -251,10 +269,11 @@ class OpenStackAuditApi(object): default_endpoint = None for endp in catalog: + endpoint_urls = endp['endpoints'][0] admin_urlparse = urlparse.urlparse( - endp['endpoints'][0]['adminURL']) + endpoint_urls.get('adminURL', '')) public_urlparse = urlparse.urlparse( - endp['endpoints'][0]['publicURL']) + endpoint_urls.get('publicURL', '')) req_url = urlparse.urlparse(req.host_url) if (req_url.netloc == admin_urlparse.netloc or req_url.netloc == public_urlparse.netloc): @@ -322,8 +341,8 @@ class AuditMiddleware(object): transport_aliases = self._get_aliases(cfg.CONF.project) if messaging: - self._notifier = oslo.messaging.Notifier( - oslo.messaging.get_transport(cfg.CONF, + self._notifier = oslo_messaging.Notifier( + oslo_messaging.get_transport(cfg.CONF, aliases=transport_aliases), os.path.basename(sys.argv[0])) -- cgit 1.2.3-korg