Joseph Akayesi
10/12/2024, 1:17 PMR.attr.user_id == P.id
however it does not evaluate when R.attr.role == "supervisor"
In the Cerbos playground; this works properly but in Go code; it does not evaluate properly
# yaml-language-server: $schema=<https://api.cerbos.dev/latest/cerbos/policy/v1/Policy.schema.json>
# docs: <https://docs.cerbos.dev/cerbos/latest/policies/resource_policies>
apiVersion: api.cerbos.dev/v1
resourcePolicy:
resource: order
version: default
rules:
- actions:
- update
effect: EFFECT_ALLOW
roles:
- user
- admin
- supervisor
condition:
match:
any:
of:
- expr: request.resource.attr.user_id == request.principal.id
- expr: request.principal.attr.role == "supervisor"
Dennis (Cerbos)
Joseph Akayesi
10/12/2024, 1:32 PMif existingOrder == nil {
<http://oc.Logger.Info|oc.Logger.Info>("order not found", "log_id", logId)
return c.Status(400).JSON(internal.NewErrorResponse("order not found"))
}
resource: = cerbos.NewResource("order", existingOrder.ID).
WithAttr("user_id", user.ID)
principal: = cerbos.NewPrincipal(user.ID, user.Role.String()).WithAttr("role", user.Role.String())
isAllowed, err: = oc.CerbosClient.IsAllowed(ctx, principal, resource, "update")
if err != nil {
oc.Logger.Error("unable to check cerbos policy", "log_id", logId, "err", err)
return c.Status(400).JSON(internal.NewErrorResponse("unable to update order at this time"))
}
if !isAllowed {
<http://oc.Logger.Info|oc.Logger.Info>("user not authorized to update order", "log_id", logId)
return c.Status(400).JSON(internal.NewErrorResponse("user not authorized to update order"))
}
Principal:
{
"id": "user_3c29c3672d794a45815dffb5ea404628",
"roles": "supervisor",
"attr": {
"key": "role",
"value": {
"string_value": "supervisor"
}
}
}
Resource:
{
"kind": "order",
"id": "order_4f5feed5b13b489888e13706cffe7933",
"attr": {
"key": "user_id",
"value": {
"string_value": "user_3c29c3672d794a45815dffb5ea404628"
}
}
}
Dennis (Cerbos)
roles
should be an array.oguzhan
role (P.attr.role)
attribute to the principal
? If not, you could only use the principal roles. Here is an example: https://play.cerbos.dev/p/yDYhPDTyUjy0BK670a7be0ey05rJMpbeJoseph Akayesi
10/12/2024, 1:58 PMJoseph Akayesi
10/12/2024, 2:54 PMoguzhan
action := "update"
principal := cerbos.NewPrincipal(user.ID, user.Role.String())
resource := cerbos.NewResource("order", existingOrder.ID).
WithAttr("user_id", user.ID)
isAllowed, err: = oc.CerbosClient.IsAllowed(ctx, principal, resource, action)
If you did and it doesn’t work, can you share the code again?Joseph Akayesi
10/12/2024, 3:11 PMresource: = cerbos.NewResource("order", existingOrder.ID).
WithAttr("user_id", user.ID)
principal: = cerbos.NewPrincipal(user.ID, user.Role.String())
isAllowed, err: = oc.CerbosClient.IsAllowed(ctx, principal, resource, "update")
Joseph Akayesi
10/12/2024, 3:32 PMoguzhan