-
Notifications
You must be signed in to change notification settings - Fork 881
feat: Check permissions endpoint #1389
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
Conversation
Allows FE to query backend for permission capabilities. Batch requests supported
Codecov Report
@@ Coverage Diff @@
## main #1389 +/- ##
==========================================
+ Coverage 66.93% 66.99% +0.06%
==========================================
Files 288 284 -4
Lines 18856 19006 +150
Branches 241 241
==========================================
+ Hits 12622 12734 +112
- Misses 4944 4959 +15
- Partials 1290 1313 +23
Continue to review full report at Codecov.
|
} | ||
|
||
response := make(codersdk.UserPermissionCheckResponse) | ||
for k, v := range params.Checks { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems odd to echo user-specified keys back in the response. Do we require this endpoint to check for N permissions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. It's a batch request, so the map key is to return the right true/false
for each permission check. So the keys will likely be human friendly names such as read-my-workspace
, the response will return "read-my-workspace": true
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand, but what is the use-case that attempting to fetch the workspace and checking the status code wouldn't catch?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is primarily for deciding which UI options to show.
Currently the admin Manage
dropdown would use this to determine if it should show, and what buttons.
This is probably not going to be exactly how it's done, but something like this. The manage dropdown has 4 different options, you can do a permission check for each one and conditionally show the ui elements the user can access. If any return true, you show the Manage
button.
{
"manage-all-organizations": true,
"manage-all-users": true,
"read-all-auditlogs": true,
"modify-deployment-settings: false,
}
In the past, the UI used the user's roles, but that means the UI has to know what an admin
can and cannot do. In this world, if we ever support custom roles, that becomes impossible. This is the new approach, where the UI comes up with actions associated with UI elements, and queries the backend to handle the Authorize()
check. Keeping it centralized in 1 place where RBAC logic is done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this standard amongst other products with RBAC? Something feels fishy to me which isn't enough to prevent this, but maybe is sign to look for validation with other products.
GitHub appears to display UI options based off role, but I know that diverges from our abstraction a bit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What I like most on not being attached to the role itself but the permission is in the future, if we want, we can create custom roles without having to update the front end at all.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this standard amongst other products with RBAC?
Kubernetes has SubjectAccessReview and SelfSubjectAccessReview API endpoints, for example: https://kubernetes.io/docs/reference/access-authn-authz/authorization/#checking-api-access
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@spikecurtis good example that is exactly what we are doing here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kylecarbs What do you think now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still feel it's odd to echo the key-value pairs, but I don't think it should block progress! Seems fine to me.
readonly owner_id?: string | ||
readonly organization_id?: string | ||
readonly resource_id?: string | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you write some docs on how to use this? The example request is helpful but for instance, where do we find the resource type and id, and where does the arbitrary name come in (I think it's the string that keys the record below but having this written somewhere will help when I forget that later lol)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup! Random comment on this, I am going to add the docs to the codersdk
struct. These comments are not pulled over to the typescript, but they could in theory be copied over. If that is something that would be helpful, we can pull comments to the typescript too.
I'll link to the docs when I commit them 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lines 81 to 127 in 79c0886
// UserPermissionCheckRequest is a structure instead of a map because | |
// go-playground/validate can only validate structs. If you attempt to pass | |
// a map into 'httpapi.Read', you will get an invalid type error. | |
type UserPermissionCheckRequest struct { | |
// Checks is a map keyed with an arbitrary string to a permission check. | |
// The key can be any string that is helpful to the caller, and allows | |
// multiple permission checks to be run in a single request. | |
// The key ensures that each permission check has the same key in the | |
// response. | |
Checks map[string]UserPermissionCheck `json:"checks"` | |
} | |
// UserPermissionCheck is used to check if a user can do a given action | |
// to a given set of objects. | |
type UserPermissionCheck struct { | |
// Object can represent a "set" of objects, such as: | |
// - All workspaces in an organization | |
// - All workspaces owned by me | |
// - All workspaces across the entire product | |
// When defining an object, use the most specific language when possible to | |
// produce the smallest set. Meaning to set as many fields on 'Object' as | |
// you can. Example, if you want to check if you can update all workspaces | |
// owned by 'me', try to also add an 'OrganizationID' to the settings. | |
// Omitting the 'OrganizationID' could produce the incorrect value, as | |
// workspaces have both `user` and `organization` owners. | |
Object UserPermissionCheckObject `json:"object"` | |
// Action can be 'create', 'read', 'update', or 'delete' | |
Action string `json:"action"` | |
} | |
type UserPermissionCheckObject struct { | |
// ResourceType is the name of the resource. | |
// './coderd/rbac/object.go' has the list of valid resource types. | |
ResourceType string `json:"resource_type,omitempty"` | |
// OwnerID (optional) is a user_id. It adds the set constraint to all resources owned | |
// by a given user. | |
OwnerID string `json:"owner_id,omitempty"` | |
// OrganizationID (optional) is an organization_id. It adds the set constraint to | |
// all resources owned by a given organization. | |
OrganizationID string `json:"organization_id,omitempty"` | |
// ResourceID (optional) reduces the set to a singular resource. This assigns | |
// a resource ID to the resource type, eg: a single workspace. | |
// The rbac library will not fetch the resource from the database, so if you | |
// are using this option, you should also set the 'OwnerID' and 'OrganizationID' | |
// if possible. Be as specific as possible using all the fields relevant. | |
ResourceID string `json:"resource_id,omitempty"` | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome! And how do I know which resource types are possible, or which resource id a given thing has?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, it is not exported into the TS types but you can see them right here
Lines 11 to 35 in 57bb108
var ( | |
ResourceWorkspace = Object{ | |
Type: "workspace", | |
} | |
ResourceTemplate = Object{ | |
Type: "template", | |
} | |
// ResourceUserRole might be expanded later to allow more granular permissions | |
// to modifying roles. For now, this covers all possible roles, so having this permission | |
// allows granting/deleting **ALL** roles. | |
ResourceUserRole = Object{ | |
Type: "user_role", | |
} | |
ResourceUserPasswordRole = Object{ | |
Type: "user_password", | |
} | |
// ResourceWildcard represents all resource types | |
ResourceWildcard = Object{ | |
Type: WildcardSymbol, | |
} | |
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So eventually the parts of the admin menu will be in there, right? I can see how the resource id of a workspace would be the workspace id, but I was having trouble figuring out how I'd ask if a user can see the Users page, for instance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You eventually can do this:
...
"read-all-users":{
"object":{
"resource_type":"users"
},
"action":"read"
},
Right now that is not possible because we don't have the resource "users" but I will make that during this ticket #884
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, so that one won't need a resource id. So when I don't specify an owner/org/resource id, I'm asking for everything, but supplying ids can narrow the scope - is that right? Would be nice to have a note on that, although maybe it's more obvious to most people.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@presleyp correct.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
for k, v := range params.Checks { | ||
if v.Object.ResourceType == "" { | ||
httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{ | ||
Message: "'resource_type' must be defined", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is resource type required? It has a ?
in the typescript
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is. @Emyrk is that possible to have this reflected in the TS types?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, fixed.Ah
Closes #984 |
* feat: Check permissions endpoint Allows FE to query backend for permission capabilities. Batch requests supported
Allows FE to query backend for permission capabilities.
Batch requests supported
Example Request
Response