Generate dictionary with a string as a key and an array of strings as value

31 Views Asked by At

I have the following Rego policy:

package authz

import future.keywords.in

# The permissions the user has for each property
permissions[property_id][permission] {
    some property_id, property_roles in input.subject.property_roles
    some permission, roles in data.common.permissions_roles
    property_roles == roles
}

The following input:

{
    "subject": {
        "property_roles": {
            "K1": [
                "R1"
            ]
        }
    }
}

and the following data:

{
    "common": {
        "permissions_roles": {
            "property.create": [
                "R0"
            ],
            "service.read": [
                "R1"
            ],
            "service.modify": [
                "R1"
            ]
        }
    }
}

The playground can be found here.

The goal is to build a dictionary with a string as a key and an array of strings as value, something like:

{
    "permissions": {
        "K1": ["service.modify", "service.read"]
    }
}

So far, I can get this:

{
    "permissions": {
        "K1": {
            "service.modify": true,
            "service.read": true
        }
    }
}

I also tried to use permissions[property_id] = [permission] which is what I would have expected to do, but then I get the following error:

policy.rego:6: eval_conflict_error: object keys must be unique

I hope somebody can suggest a better way to achieve the goal!

0

There are 0 best solutions below