Refactor: moved flatten function to utils
Previously, the keystone.common.authorization module contained the function "flatten", which would transform a dictionary (possibly nested) into a single level dictionary. Given that the "flattening" of a dictionary is a more general concept, not related to authorization, I thought it would be more intuitive to have it in the keystone.common.utils module. Change-Id: Ic1f6fa70634ade41e392ee7c5cd8adac913fd0b8
This commit is contained in:
committed by
Gerrit Code Review
parent
9b580d286a
commit
e40b9cd06e
@@ -16,8 +16,6 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import collections
|
||||
|
||||
from keystone.contrib import federation
|
||||
from keystone import exception
|
||||
from keystone.openstack.common.gettextutils import _
|
||||
@@ -43,23 +41,6 @@ It is a dictionary with the following attributes:
|
||||
LOG = log.getLogger(__name__)
|
||||
|
||||
|
||||
def flatten(d, parent_key=''):
|
||||
"""Flatten a nested dictionary
|
||||
|
||||
Converts a dictionary with nested values to a single level flat
|
||||
dictionary, with dotted notation for each key.
|
||||
|
||||
"""
|
||||
items = []
|
||||
for k, v in d.items():
|
||||
new_key = parent_key + '.' + k if parent_key else k
|
||||
if isinstance(v, collections.MutableMapping):
|
||||
items.extend(flatten(v, new_key).items())
|
||||
else:
|
||||
items.append((new_key, v))
|
||||
return dict(items)
|
||||
|
||||
|
||||
def is_v3_token(token):
|
||||
# V3 token data are encapsulated into "token" key while
|
||||
# V2 token data are encapsulated into "access" key.
|
||||
|
||||
@@ -147,7 +147,7 @@ def protected(callback=None):
|
||||
policy_dict.update(kwargs)
|
||||
self.policy_api.enforce(creds,
|
||||
action,
|
||||
authorization.flatten(policy_dict))
|
||||
utils.flatten_dict(policy_dict))
|
||||
LOG.debug(_('RBAC: Authorization granted'))
|
||||
return f(self, context, *args, **kwargs)
|
||||
return inner
|
||||
@@ -188,7 +188,7 @@ def filterprotected(*filters):
|
||||
|
||||
self.policy_api.enforce(creds,
|
||||
action,
|
||||
authorization.flatten(target))
|
||||
utils.flatten_dict(target))
|
||||
|
||||
LOG.debug(_('RBAC: Authorization granted'))
|
||||
else:
|
||||
@@ -452,7 +452,7 @@ class V3Controller(wsgi.Application):
|
||||
attr = filter['name']
|
||||
value = filter['value']
|
||||
refs = [r for r in refs if _attr_match(
|
||||
authorization.flatten(r).get(attr), value)]
|
||||
utils.flatten_dict(r).get(attr), value)]
|
||||
else:
|
||||
# It might be an inexact filter
|
||||
refs = [r for r in refs if _inexact_attr_match(
|
||||
@@ -612,7 +612,7 @@ class V3Controller(wsgi.Application):
|
||||
policy_dict.update(prep_info['input_attr'])
|
||||
self.policy_api.enforce(creds,
|
||||
action,
|
||||
authorization.flatten(policy_dict))
|
||||
utils.flatten_dict(policy_dict))
|
||||
LOG.debug(_('RBAC: Authorization granted'))
|
||||
|
||||
@classmethod
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
# under the License.
|
||||
|
||||
import calendar
|
||||
import collections
|
||||
import grp
|
||||
import hashlib
|
||||
import json
|
||||
@@ -40,6 +41,23 @@ CONF = config.CONF
|
||||
LOG = log.getLogger(__name__)
|
||||
|
||||
|
||||
def flatten_dict(d, parent_key=''):
|
||||
"""Flatten a nested dictionary
|
||||
|
||||
Converts a dictionary with nested values to a single level flat
|
||||
dictionary, with dotted notation for each key.
|
||||
|
||||
"""
|
||||
items = []
|
||||
for k, v in d.items():
|
||||
new_key = parent_key + '.' + k if parent_key else k
|
||||
if isinstance(v, collections.MutableMapping):
|
||||
items.extend(flatten_dict(v, new_key).items())
|
||||
else:
|
||||
items.append((new_key, v))
|
||||
return dict(items)
|
||||
|
||||
|
||||
def read_cached_file(filename, cache_info, reload_func=None):
|
||||
"""Read from a file if it has been modified.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user