Skip to content
Merged
Prev Previous commit
Next Next commit
handle template acl perms
  • Loading branch information
Emyrk committed Jan 13, 2025
commit a0f7f253d605d2fc19d10ca751a36378f83b0b79
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- With the "use" verb now existing for templates, we need to update the acl's to
-- include "use" where the permissions set ["read"] is present.
-- The other permission set is ["*"] which is unaffected.

UPDATE
templates
SET
group_acl = replace(group_acl::text, '["read", "use"]', '["read"]')::jsonb,
user_acl = replace(user_acl::text, '["read", "use"]', '["read"]')::jsonb
12 changes: 12 additions & 0 deletions coderd/database/migrations/000283_template_read_to_use.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- With the "use" verb now existing for templates, we need to update the acl's to
-- include "use" where the permissions set ["read"] is present.
-- The other permission set is ["*"] which is unaffected.

UPDATE
templates
SET
-- Instead of trying to write a complicated SQL query to update the JSONB
-- object, a string replace is much simpler and easier to understand.
-- Both pieces of text are JSON arrays, so this safe to do.
group_acl = replace(group_acl::text, '["read"]', '["read", "use"]')::jsonb,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to have ACLs like ["read", "update"] in this list? Wouldn't those need to be updated?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No it is not. The only values a user can pass in is in this enum list:
https://github.com/coder/coder/blob/main/codersdk/templates.go#L169-L176

The admin role sets ['*']. So we only have 2 cases in the database today

user_acl = replace(user_acl::text, '["read"]', '["read", "use"]')::jsonb
3 changes: 1 addition & 2 deletions coderd/rbac/policy/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@ var RBACPermissions = map[string]PermissionDefinition{
},
"template": {
Actions: map[Action]ActionDefinition{
ActionCreate: actDef("create a template"),
// TODO: Create a use permission maybe?
ActionCreate: actDef("create a template"),
ActionUse: actDef("use the template to create a workspace"),
ActionRead: actDef("read template"),
ActionUpdate: actDef("update a template"),
Expand Down
4 changes: 2 additions & 2 deletions coderd/rbac/roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ func ReloadBuiltinRoles(opts *RoleOptions) {
Identifier: RoleTemplateAdmin(),
DisplayName: "Template Admin",
Site: Permissions(map[string][]policy.Action{
ResourceTemplate.Type: {policy.ActionCreate, policy.ActionRead, policy.ActionUpdate, policy.ActionDelete, policy.ActionViewInsights},
ResourceTemplate.Type: ResourceTemplate.AvailableActions(),
// CRUD all files, even those they did not upload.
ResourceFile.Type: {policy.ActionCreate, policy.ActionRead},
ResourceWorkspace.Type: {policy.ActionRead},
Expand Down Expand Up @@ -476,7 +476,7 @@ func ReloadBuiltinRoles(opts *RoleOptions) {
Site: []Permission{},
Org: map[string][]Permission{
organizationID.String(): Permissions(map[string][]policy.Action{
ResourceTemplate.Type: {policy.ActionCreate, policy.ActionRead, policy.ActionUpdate, policy.ActionDelete, policy.ActionViewInsights},
ResourceTemplate.Type: ResourceTemplate.AvailableActions(),
ResourceFile.Type: {policy.ActionCreate, policy.ActionRead},
ResourceWorkspace.Type: {policy.ActionRead},
// Assigning template perms requires this permission.
Expand Down
5 changes: 3 additions & 2 deletions enterprise/coderd/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/coder/coder/v2/coderd/httpapi"
"github.com/coder/coder/v2/coderd/httpmw"
"github.com/coder/coder/v2/coderd/rbac/policy"
"github.com/coder/coder/v2/coderd/util/slice"
"github.com/coder/coder/v2/codersdk"
)

Expand Down Expand Up @@ -326,7 +327,7 @@ func validateTemplateRole(role codersdk.TemplateRole) error {

func convertToTemplateRole(actions []policy.Action) codersdk.TemplateRole {
switch {
case len(actions) == 1 && actions[0] == policy.ActionRead:
case len(actions) == 2 && slice.SameElements(actions, []policy.Action{policy.ActionUse, policy.ActionRead}):
return codersdk.TemplateRoleUse
case len(actions) == 1 && actions[0] == policy.WildcardSymbol:
return codersdk.TemplateRoleAdmin
Expand All @@ -340,7 +341,7 @@ func convertSDKTemplateRole(role codersdk.TemplateRole) []policy.Action {
case codersdk.TemplateRoleAdmin:
return []policy.Action{policy.WildcardSymbol}
case codersdk.TemplateRoleUse:
return []policy.Action{policy.ActionRead}
return []policy.Action{policy.ActionRead, policy.ActionUse}
}

return nil
Expand Down