Skip to content

Commit

Permalink
Merge branch 'feature/CH-152' of github.com:MetaCell/cloud-harness in…
Browse files Browse the repository at this point in the history
…to feature/CH-152
  • Loading branch information
filippomc committed Oct 3, 2024
2 parents bf2b204 + 59446a2 commit aacfe5d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion libraries/cloudharness-common/cloudharness/auth/quota.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def get_user_quotas(application_config: ApplicationConfig = None, user_id: str =
valid_keys_map = {key for key in base_quotas}

try:
return get_user_attributes(user_id, valid_keys_map=valid_keys_map, default_attributes=base_quotas)
return get_user_attributes(user_id, valid_keys=valid_keys_map, default_attributes=base_quotas)

except UserNotFound as e:
log.warning("Quotas not available: error retrieving user: %s", user_id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ def addChild(self, child):
self.children.append(child)


def _filter_attrs(attrs, valid_keys_map):
def _filter_attrs(attrs, valid_keys):
# only use the attributes defined by the valid keys map
valid_attrs = {}
if attrs is None:
return valid_attrs
for key in attrs:
if key in valid_keys_map:
if key in valid_keys:
# map to value
valid_attrs.update({key: attrs[key][0]})
return valid_attrs


def _construct_attribute_tree(groups, valid_keys_map) -> KCAttributeNode:
def _construct_attribute_tree(groups, valid_keys) -> KCAttributeNode:
"""Construct a tree of attributes from the user groups"""
root = KCAttributeNode("root", {})
for group in groups:
Expand All @@ -49,7 +49,7 @@ def _construct_attribute_tree(groups, valid_keys_map) -> KCAttributeNode:
# add the child with it's attributes and last segment name
n = KCAttributeNode(
paths[len(paths) - 1],
_filter_attrs(group["attributes"], valid_keys_map)
_filter_attrs(group["attributes"], valid_keys)
)
r.addChild(n)
return root
Expand Down Expand Up @@ -103,12 +103,12 @@ def _compute_attributes_from_tree(node: KCAttributeNode, transform_value_fn=lamb
return node.attrs


def get_user_attributes(user_id: str = None, valid_keys_map={}, default_attributes={}, transform_value_fn=lambda x: x) -> dict:
def get_user_attributes(user_id: str = None, valid_keys={}, default_attributes={}, transform_value_fn=lambda x: x) -> dict:
"""Get the user attributes from Keycloak recursively from the user attributes and groups
Args:
user_id (str): the Keycloak user id or username to get the quotas for
valid_keys_map (dict): the valid keys to use for the attributes
valid_keys (iterable): the valid keys to use for the attributes
default_attributes (dict): the default attributes to use if the user does not have the attribute
Returns:
Expand All @@ -130,8 +130,8 @@ def get_user_attributes(user_id: str = None, valid_keys_map={}, default_attribut
group_quotas = _compute_attributes_from_tree(
_construct_attribute_tree(
user["userGroups"],
valid_keys_map), transform_value_fn)
user_attrs = _filter_attrs(user["attributes"], valid_keys_map)
valid_keys), transform_value_fn)
user_attrs = _filter_attrs(user["attributes"], valid_keys)
for key in group_quotas:
if key not in user_attrs:
user_attrs.update({key: group_quotas[key]})
Expand Down

0 comments on commit aacfe5d

Please sign in to comment.