Skip to content

Commit 5604517

Browse files
committed
handle template acl perms
1 parent 746ff70 commit 5604517

File tree

5 files changed

+27
-6
lines changed

5 files changed

+27
-6
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- With the "use" verb now existing for templates, we need to update the acl's to
2+
-- include "use" where the permissions set ["read"] is present.
3+
-- The other permission set is ["*"] which is unaffected.
4+
5+
UPDATE
6+
templates
7+
SET
8+
group_acl = replace(group_acl::text, '["read", "use"]', '["read"]')::jsonb,
9+
user_acl = replace(user_acl::text, '["read", "use"]', '["read"]')::jsonb
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-- With the "use" verb now existing for templates, we need to update the acl's to
2+
-- include "use" where the permissions set ["read"] is present.
3+
-- The other permission set is ["*"] which is unaffected.
4+
5+
UPDATE
6+
templates
7+
SET
8+
-- Instead of trying to write a complicated SQL query to update the JSONB
9+
-- object, a string replace is much simpler and easier to understand.
10+
-- Both pieces of text are JSON arrays, so this safe to do.
11+
group_acl = replace(group_acl::text, '["read"]', '["read", "use"]')::jsonb,
12+
user_acl = replace(user_acl::text, '["read"]', '["read", "use"]')::jsonb

coderd/rbac/policy/policy.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,7 @@ var RBACPermissions = map[string]PermissionDefinition{
133133
},
134134
"template": {
135135
Actions: map[Action]ActionDefinition{
136-
ActionCreate: actDef("create a template"),
137-
// TODO: Create a use permission maybe?
136+
ActionCreate: actDef("create a template"),
138137
ActionUse: actDef("use the template to create a workspace"),
139138
ActionRead: actDef("read template"),
140139
ActionUpdate: actDef("update a template"),

coderd/rbac/roles.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ func ReloadBuiltinRoles(opts *RoleOptions) {
318318
Identifier: RoleTemplateAdmin(),
319319
DisplayName: "Template Admin",
320320
Site: Permissions(map[string][]policy.Action{
321-
ResourceTemplate.Type: {policy.ActionCreate, policy.ActionRead, policy.ActionUpdate, policy.ActionDelete, policy.ActionViewInsights},
321+
ResourceTemplate.Type: ResourceTemplate.AvailableActions(),
322322
// CRUD all files, even those they did not upload.
323323
ResourceFile.Type: {policy.ActionCreate, policy.ActionRead},
324324
ResourceWorkspace.Type: {policy.ActionRead},
@@ -476,7 +476,7 @@ func ReloadBuiltinRoles(opts *RoleOptions) {
476476
Site: []Permission{},
477477
Org: map[string][]Permission{
478478
organizationID.String(): Permissions(map[string][]policy.Action{
479-
ResourceTemplate.Type: {policy.ActionCreate, policy.ActionRead, policy.ActionUpdate, policy.ActionDelete, policy.ActionViewInsights},
479+
ResourceTemplate.Type: ResourceTemplate.AvailableActions(),
480480
ResourceFile.Type: {policy.ActionCreate, policy.ActionRead},
481481
ResourceWorkspace.Type: {policy.ActionRead},
482482
// Assigning template perms requires this permission.

enterprise/coderd/templates.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/coder/coder/v2/coderd/httpapi"
1717
"github.com/coder/coder/v2/coderd/httpmw"
1818
"github.com/coder/coder/v2/coderd/rbac/policy"
19+
"github.com/coder/coder/v2/coderd/util/slice"
1920
"github.com/coder/coder/v2/codersdk"
2021
)
2122

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

327328
func convertToTemplateRole(actions []policy.Action) codersdk.TemplateRole {
328329
switch {
329-
case len(actions) == 1 && actions[0] == policy.ActionRead:
330+
case len(actions) == 2 && slice.SameElements(actions, []policy.Action{policy.ActionUse, policy.ActionRead}):
330331
return codersdk.TemplateRoleUse
331332
case len(actions) == 1 && actions[0] == policy.WildcardSymbol:
332333
return codersdk.TemplateRoleAdmin
@@ -340,7 +341,7 @@ func convertSDKTemplateRole(role codersdk.TemplateRole) []policy.Action {
340341
case codersdk.TemplateRoleAdmin:
341342
return []policy.Action{policy.WildcardSymbol}
342343
case codersdk.TemplateRoleUse:
343-
return []policy.Action{policy.ActionRead}
344+
return []policy.Action{policy.ActionRead, policy.ActionUse}
344345
}
345346

346347
return nil

0 commit comments

Comments
 (0)