Profiling rego - rule reevaluation when returning data from it #381
-
Hi folks, I was profiling our rego and I've noticed that our rules are evaluated multiple times, unexpected to me. This is the data.json, one role can have permissions to multiple resources {
"static": {
"role_data": {
"role3": {
"permissions": {
"resource1": {
"read": true,
"write": true
}
}
},
"role10": {
"permissions": {
"resource1": {
"read": true,
"write": true
}
}
},
"role11": {
"permissions": {
"resource1": {
"read": true,
"write": true
}
}
},
"role12": {
"permissions": {
"resource1": {
"read": true,
"write": true
}
}
}
}
}
} And two policies: main.rego
subject.rego
We return the matched roles as we need the result in a different rule that is missed here on purpose. The input is (some parts are missed on purpose) {
"resource": {
"action": "read",
"type": "resource1"
},
"subject": {
"roles": [
{"id": "role3"},
{"id": "role10"},
{"id": "role11"},
{"id": "role12"}
]
}
} The logic is: we provide the user assignments on input, the action and the resource type and OPA evaluates the RBAC policy, i.e if the user can do that action on the given resource having the provided assignments We profile the policies with And we get back
The 16 evaluations of If I change the subject.rego to just evaluate the matched roles and not return them
I get the expected 4 evaluations:
Version: 0.50.2 |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
Excuse the brief answer, but a common pitfall is that data.static.role_data[role.id].permissions[input.resource.type][input.resource.action] really is expanded to something like these four expressions a = role.id
b = input.resource.type
c = input.resource.action
data.static.role_data[a].permissions[b][c] and when the profiling is done, the counts of EVAL/REDO for each of these expressions are added up. This easily misleads. |
Beta Was this translation helpful? Give feedback.
-
@srenatus thank you, it makes sense. One way I can reduce the numbers is by flattening the But going too far, I get recursion err, any idea why? 1 error occurred: bundle/resources/subject.rego:3: rego_recursion_error: rule data.subject.matched_roles is recursive: data.subject.matched_roles -> data.subject.matched_roles data.json {
"role3": {
"resource1": {
"read": true,
"write": true
}
},
"role10": {
"resource1": {
"read": true,
"write": true
}
},
"role11": {
"resource1": {
"read": true,
"write": true
}
},
"role12": {
"resource1": {
"read": true,
"write": true
}
}
} rego:
|
Beta Was this translation helpful? Give feedback.
-
Thank you @srenatus , I've got my answers, I'm closing this one. |
Beta Was this translation helpful? Give feedback.
Excuse the brief answer, but a common pitfall is that
really is expanded to something like these four expressions
and when the profiling is done, the counts of EVAL/REDO for each of these expressions are added up. This easily misleads.