-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Modelling a roles to permissions relationship in Rego #903
Comments
@dan6518 there's an example of implementing a role-based access control (RBAC) policy on the OPA website here. The example is a bit messy, but what it essentially does is:
We could refactor the policy to make it slightly clearer.
You could follow a similar approach in your case. The main difference is that the role is not simply a string (or single value), it's multi-dimensional. For example, having role "manager" is not sufficient, the user must have role "manager" with building "B". How do you supply the resource being accessed (e.g., is it a path like in your example)? Regarding the second part of your question, about querying OPA for roles and derived permissions, it should be possible to write a query that finds relevant roles and permissions relatively easily. For example, assuming you have a role-permission mapping like this:
You can run a query for the roles & permissions that a user has, as follows:
The result will contain sets of values for |
Thanks for your reponse! I was able to make a bit of progress when I realized modeling the mapping as part of the data is probably the way to go, like in your example role-permission mapping. I was able to map from user to permissions. I have some more questions though: Let's say I have two sources for roles for my users. They can either be a property on the user directly, or the user can have a group property from which the user inherits a set of roles. In other words, both of these derivations are valid:
In imperative or functional languages I would make the roles->permissions mapping, since it's present in both derivations. I'd concatenate user.roles and user.groups.roles and pass that as the paremeter. Is there an equivalent in Rego to this sort of composition? Or is the canonical way of doing this something more like
What about when I query for the user's roles (or permissions), how do I concatenate the result sets?
I call OPA from an AWS lambda authorizer, so I get to inspect the call made from the client and transform it however I want. It can be a path or anything else really. The important point is that it's split into two parts: The resource and the context. For example:
The reason for this is that I want thousands of dynamically created resources, without having to create/specify API permissions for all of them. Someone who has access to /buildings/B/microwaves should also have access to /buildings/B/apartment/456/storage_unit/15/microwaves. Basically if you have the "admin" role in the context of "building B", that implicitly grants you the "admin" role for all sub-contexts of "building B". There's a hierarchical structure to the contexts though, so it's not quite as bad as it sounds. But this is part of my domain's requirements, which I didn't want to bother you with too much so feel free to ignore this part. If I can get the earlier part of my question to work I can probably extrapolate what I need from there. |
Your
When you create multiple rules with the same name in OPA, we say that the rule is defined incrementally. In the case of Thanks for providing more detail on the use case. Also, the lambda authorizer integration sounds really cool! This has been on our list to look at for a while. |
Closing this for the time being. Feel free to reopen. |
Hello, I don't know if this is the correct place to ask for usage help, so please let me know if I should post this somewhere else instead.
I have followed the HTTP API authorization guide, and it's along the lines of what I want, but I need a more complex authorization model and I'm having a hard time grokking Rego. My model looks more or less like this:
I have a bunch of users, each with a unique user ID and a list of roles. These roles are valid in some context, for example "user A is a Manager (role) in building B". Each role corresponds to a set of API permissions. The example user A should be able to call both "POST /building/B/openDoors" and "POST /building/B/closeDoors" because he is a manager of building B. However an example user C who is a Tenant of Apartment 123 in building B should only be able to call "POST /building/B/apartment/123/openDoors". A user can also have multiple of these roles, in various contexts, like be a manager for two buildings and a tenant in one.
Additionally I need users to be able to query OPA to get a list of their roles, and a list of their derived permissions based on the roles they have. Example user A should be able to query and get the response that he's "manager of building B", and also a query that gives the list of API endpoints he can access.
The request to OPA only contains the user's ID and the resource the user is trying to access.
So I think I need a list of my API permissions, my roles, and a mapping between them, sort of like this (pseudo-ish code):
And an array of users in my data.json file, which looks something like:
This feels like something that should be possible or even simple to do in Rego, but so far I haven't been able to come up with a solution. That is mostly because of my lack of experience with declarative programming, but also because I have had a hard time finding examples to learn from.
Any help/guidance is appreciated.
The text was updated successfully, but these errors were encountered: