Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support appending to data lists #788

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion policy/lib/rule_data.rego
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,27 @@ rule_data_defaults := {
],
}

# The idea here is you can customize an existing list in a rule
# data field by appending values to it
#
rule_data(key_name) := r {
# Only if the original data is an array
original_list := _rule_data(key_name)
is_array(original_list)

# See if there's any custom data to append
append_list := _append_rule_data(key_name)

# The custom data must also be a list
is_array(append_list)

# Return original list with custom extra values appended to it
r := array.concat(original_list, append_list)
} else := r {
# Return the rule data value unmodified
r := _rule_data(key_name)
}

# Returns the "first found" of the following:
# data.rule_data__configuration__[key_name]
# data.rule_data_custom[key_name]
Expand All @@ -75,7 +96,7 @@ rule_data_defaults := {
#
# And falls back to an empty list if the key is not found anywhere.
#
rule_data(key_name) := value {
_rule_data(key_name) := value {
# Expected to be defined under `configuration.rule_data` in the
# ECP configuration data being used when EC is run.
value := data.rule_data__configuration__[key_name]
Expand All @@ -94,3 +115,18 @@ rule_data(key_name) := value {
# If the key is not found, default to an empty list
value := []
}

# Returns items found in
# data.append_rule_data_custom[key_name]
# data.append_rule_data__configuration__[key_name]
#
_append_rule_data(key_name) := value {
# If both `data.append_rule_data__configuration__[key_name]` and
# `data.append_rule_data_custom[key_name] are present then
# the user would reasonably expect them both to be appended
value := array.concat(data.append_rule_data_custom[key_name], data.append_rule_data__configuration__[key_name])
} else := value {
value := data.append_rule_data_custom[key_name]
} else := value {
value := data.append_rule_data__configuration__[key_name]
}
21 changes: 21 additions & 0 deletions policy/lib/rule_data_test.rego
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,27 @@ test_rule_data {
with lib.rule_data_defaults as {"key3": 10}
}

test_appending_custom_rule_data {
lib.assert_equal(
[
["a", "b", "c", "d"],
["a", "d"],
["e"],
"zap",
],
[
# Test a few scenarios
lib.rule_data("foo"),
lib.rule_data("bar"),
lib.rule_data("baz"),
# Can't append to a non-array
lib.rule_data("zip"),
],
) with data.rule_data as {"foo": ["a", "b"], "bar": ["a"], "zip": "zap"}
with data.append_rule_data_custom as {"foo": ["c"], "baz": ["e"], "zip": ["zup"]}
with data.append_rule_data__configuration__ as {"foo": ["d"], "bar": ["d"]}
}

# Need this for 100% coverage
test_rule_data_defaults {
lib.assert_not_empty(lib.rule_data_defaults)
Expand Down