In OPA, I take an input of type { roles, action, object }, and I return a matching permission which contains data regarding what the user is allowed to do based on the input
This is rather simple if no two roles have the same permission:
package policies
import data.role_permissions
matching_permission := permission {
role := input.roles[_]
permissions := role_permissions[role]
permission := permissions[_]
permission.action == input.action
permission.object == input.object
}
The problem arises if the input roles has multiple roles that match the same permission, in that case I get an error: complete rules must not produce multiple outputs. Which makes sense, so I modified the policy to match an array of permissions:
matching_perms[permission] {
role := input.roles[_]
permissions := role_permissions[role]
permission := permissions[_]
permission.action == input.action
permission.object == input.object
}
But I still need to return a singe permission object. What I want to do is merge any extra keys into a single item, with my own logic for each merge conflict
However, I cannot seem to merge the arrays of extra keys in this case.
How would I, for instance, get the following output with this data?:
// data.json
{
"role_permissions": {
"a": [{ "action": "read", "object": "threats", "levels": [1, 2, 3] }],
"b": [{ "action": "read", "object": "threats", "levels": [4, 5] }]
}
}
// input
{
"roles": ["a", "b"],
"action": "read",
"object": "threats"
}
// desired output
{
"action": "read",
"object": "threats",
"levels": [1, 2, 3, 4, 5] // <-- Merged into one result
}
Basically, how can I merge the permission objects' array fields to create a single object?