Skip to content

Commit

Permalink
remove redundant 'array_to_dict' function
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-githubs committed Oct 8, 2024
1 parent d149916 commit dafb5b5
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 35 deletions.
31 changes: 0 additions & 31 deletions global_helpers/panther_base_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,34 +538,3 @@ def key_value_list_to_dict(list_objects: List[dict], key: str, value: str) -> di
# example: [{'key': 'a', 'value': 1}, {'key': 'b', 'value': 2}]
# becomes: {'a': 1, 'b': 2}
return {item[key]: item[value] for item in list_objects}


def array_to_dict(array: List[dict], key: str, error_on_duplicate: bool = False) -> dict:
"""Take a list of dictionaries and convert them into a dictionary based on a unique key.
Args:
array (list of dicts): Array containing the dictionaries to itemize
key (str): Specified which value of each listed dict to use as key in the returned dict
For each dictionary 'D' in 'array', the corresponding key in the returned dict R is the
value D[key], so R[D[key]] = D.
error_on_duplicate (bool, optional): Raise KeyError if 2 dicts share the same value for key.
If False, previous entries are overridden by later entries of 'array' with the same key
value. (Default: False)
Returns:
out (dict): dictionary mapping for each dictionary in 'array'.
Raises:
KeyError: If any of the following occurs:
- a dictionary in 'array' does not have an entry for 'key'
- if 'error_on_duplicate' is True and multiple dictionaries in 'array' have the same
key value
"""
out = {}
for dict_ in array:
keyval = dict_[key]
if error_on_duplicate and keyval in out:
raise KeyError(f"Multiple entries in array share the same value for '{key}': {keyval}")
out[keyval] = dict_

return out
8 changes: 4 additions & 4 deletions rules/gcp_audit_rules/gcp_user_added_to_privileged_group.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from panther_base_helpers import array_to_dict, deep_get
from panther_base_helpers import key_value_list_to_dict

PRIVILEGED_GROUPS = {
# "[email protected]"
Expand All @@ -18,10 +18,10 @@ def rule(event):
return False

# Get the username
params = array_to_dict(event_.get("parameter", []), "name")
params = key_value_list_to_dict(event_.get("parameter", []), "name", "value")
global USER_EMAIL, GROUP_EMAIL # pylint: disable=global-statement
USER_EMAIL = deep_get(params, "USER_EMAIL", "value", default="<UNKNOWN USER>")
GROUP_EMAIL = deep_get(params, "GROUP_EMAIL", "value", default="<UNKNOWN GROUP")
USER_EMAIL = params.get("USER_EMAIL", "<UNKNOWN USER>")
GROUP_EMAIL = params.get("GROUP_EMAIL", "<UNKNOWN GROUP>")

return GROUP_EMAIL in get_privileged_groups()

Expand Down

0 comments on commit dafb5b5

Please sign in to comment.