Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitdb80b4d

Browse files
committed
feat: POC for allowing TemplateAdmin to delete prebuild workspaces via auth layer
1 parentaf4a668 commitdb80b4d

File tree

15 files changed

+218
-20
lines changed

15 files changed

+218
-20
lines changed

‎coderd/apidoc/docs.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎coderd/apidoc/swagger.json

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎coderd/database/dbauthz/dbauthz.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,9 @@ var (
412412
policy.ActionCreate,policy.ActionDelete,policy.ActionRead,policy.ActionUpdate,
413413
policy.ActionWorkspaceStart,policy.ActionWorkspaceStop,
414414
},
415+
rbac.ResourcePrebuiltWorkspace.Type: {
416+
policy.ActionRead,policy.ActionUpdate,policy.ActionDelete,
417+
},
415418
// Should be able to add the prebuilds system user as a member to any organization that needs prebuilds.
416419
rbac.ResourceOrganizationMember.Type: {
417420
policy.ActionCreate,
@@ -3909,7 +3912,14 @@ func (q *querier) InsertWorkspaceBuild(ctx context.Context, arg database.InsertW
39093912
action=policy.ActionWorkspaceStop
39103913
}
39113914

3912-
iferr=q.authorizeContext(ctx,action,w);err!=nil {
3915+
ifaction==policy.ActionDelete&&w.IsPrebuild() {
3916+
iferr:=q.authorizeContext(ctx,action,w.PrebuildRBAC());err!=nil {
3917+
// Fallback to normal workspace auth check
3918+
iferr=q.authorizeContext(ctx,action,w);err!=nil {
3919+
returnxerrors.Errorf("authorize context: %w",err)
3920+
}
3921+
}
3922+
}elseiferr=q.authorizeContext(ctx,action,w);err!=nil {
39133923
returnxerrors.Errorf("authorize context: %w",err)
39143924
}
39153925

@@ -3927,7 +3937,14 @@ func (q *querier) InsertWorkspaceBuild(ctx context.Context, arg database.InsertW
39273937
// to use a non-active version then we must fail the request.
39283938
ifaccessControl.RequireActiveVersion {
39293939
ifarg.TemplateVersionID!=t.ActiveVersionID {
3930-
iferr=q.authorizeContext(ctx,policy.ActionUpdate,t);err!=nil {
3940+
ifw.IsPrebuild() {
3941+
iferr:=q.authorizeContext(ctx,policy.ActionUpdate,w.PrebuildRBAC());err!=nil {
3942+
// Fallback to normal workspace auth check
3943+
iferr=q.authorizeContext(ctx,policy.ActionUpdate,t);err!=nil {
3944+
returnxerrors.Errorf("cannot use non-active version: %w",err)
3945+
}
3946+
}
3947+
}elseiferr=q.authorizeContext(ctx,policy.ActionUpdate,t);err!=nil {
39313948
returnxerrors.Errorf("cannot use non-active version: %w",err)
39323949
}
39333950
}
@@ -3949,7 +3966,15 @@ func (q *querier) InsertWorkspaceBuildParameters(ctx context.Context, arg databa
39493966
returnerr
39503967
}
39513968

3952-
err=q.authorizeContext(ctx,policy.ActionUpdate,workspace)
3969+
ifworkspace.IsPrebuild() {
3970+
err=q.authorizeContext(ctx,policy.ActionUpdate,workspace.PrebuildRBAC())
3971+
// Fallback to normal workspace auth check
3972+
iferr!=nil {
3973+
err=q.authorizeContext(ctx,policy.ActionUpdate,workspace)
3974+
}
3975+
}else {
3976+
err=q.authorizeContext(ctx,policy.ActionUpdate,workspace)
3977+
}
39533978
iferr!=nil {
39543979
returnerr
39553980
}

‎coderd/database/modelmethods.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,26 @@ func (w Workspace) WorkspaceTable() WorkspaceTable {
226226
}
227227

228228
func (wWorkspace)RBACObject() rbac.Object {
229+
// if w.IsPrebuild() {
230+
//return w.PrebuildRBAC()
231+
//}
229232
returnw.WorkspaceTable().RBACObject()
230233
}
231234

235+
func (wWorkspace)IsPrebuild()bool {
236+
// TODO: avoid import cycle
237+
returnw.OwnerID==uuid.MustParse("c42fdf75-3097-471c-8c33-fb52454d81c0")
238+
}
239+
240+
func (wWorkspace)PrebuildRBAC() rbac.Object {
241+
ifw.IsPrebuild() {
242+
returnrbac.ResourcePrebuiltWorkspace.WithID(w.ID).
243+
InOrg(w.OrganizationID).
244+
WithOwner(w.OwnerID.String())
245+
}
246+
returnw.RBACObject()
247+
}
248+
232249
func (wWorkspaceTable)RBACObject() rbac.Object {
233250
ifw.DormantAt.Valid {
234251
returnw.DormantRBAC()

‎coderd/rbac/object_gen.go

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎coderd/rbac/policy/policy.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@ var RBACPermissions = map[string]PermissionDefinition{
102102
"workspace_dormant": {
103103
Actions:workspaceActions,
104104
},
105+
"prebuilt_workspace": {
106+
Actions:map[Action]ActionDefinition{
107+
ActionRead:actDef("read prebuilt workspace"),
108+
ActionUpdate:actDef("update prebuilt workspace"),
109+
ActionDelete:actDef("delete prebuilt workspace"),
110+
},
111+
},
105112
"workspace_proxy": {
106113
Actions:map[Action]ActionDefinition{
107114
ActionCreate:actDef("create a workspace proxy"),

‎coderd/rbac/roles.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,9 @@ func ReloadBuiltinRoles(opts *RoleOptions) {
335335
ResourceAssignOrgRole.Type: {policy.ActionRead},
336336
ResourceTemplate.Type:ResourceTemplate.AvailableActions(),
337337
// CRUD all files, even those they did not upload.
338-
ResourceFile.Type: {policy.ActionCreate,policy.ActionRead},
339-
ResourceWorkspace.Type: {policy.ActionRead},
338+
ResourceFile.Type: {policy.ActionCreate,policy.ActionRead},
339+
ResourceWorkspace.Type: {policy.ActionRead},
340+
ResourcePrebuiltWorkspace.Type: {policy.ActionRead,policy.ActionUpdate,policy.ActionDelete},
340341
// CRUD to provisioner daemons for now.
341342
ResourceProvisionerDaemon.Type: {policy.ActionCreate,policy.ActionRead,policy.ActionUpdate,policy.ActionDelete},
342343
// Needs to read all organizations since
@@ -493,9 +494,10 @@ func ReloadBuiltinRoles(opts *RoleOptions) {
493494
Site: []Permission{},
494495
Org:map[string][]Permission{
495496
organizationID.String():Permissions(map[string][]policy.Action{
496-
ResourceTemplate.Type:ResourceTemplate.AvailableActions(),
497-
ResourceFile.Type: {policy.ActionCreate,policy.ActionRead},
498-
ResourceWorkspace.Type: {policy.ActionRead},
497+
ResourceTemplate.Type:ResourceTemplate.AvailableActions(),
498+
ResourceFile.Type: {policy.ActionCreate,policy.ActionRead},
499+
ResourceWorkspace.Type: {policy.ActionRead},
500+
ResourcePrebuiltWorkspace.Type: {policy.ActionRead,policy.ActionUpdate,policy.ActionDelete},
499501
// Assigning template perms requires this permission.
500502
ResourceOrganization.Type: {policy.ActionRead},
501503
ResourceOrganizationMember.Type: {policy.ActionRead},

‎coderd/workspacebuilds.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,16 @@ func (api *API) postWorkspaceBuilds(rw http.ResponseWriter, r *http.Request) {
404404
ctx,
405405
tx,
406406
func(action policy.Action,object rbac.Objecter)bool {
407+
ifobject.RBACObject().Type==rbac.ResourceWorkspace.Type&&action==policy.ActionDelete {
408+
workspaceObj,ok:=object.(database.Workspace)
409+
ifok {
410+
prebuild:=workspaceObj.PrebuildRBAC()
411+
// Fallback to normal workspace auth check
412+
ifauth:=api.Authorize(r,action,prebuild);auth {
413+
returnauth
414+
}
415+
}
416+
}
407417
returnapi.Authorize(r,action,object)
408418
},
409419
audit.WorkspaceBuildBaggageFromRequest(r),

‎codersdk/rbacresources_gen.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎docs/reference/api/members.md

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎docs/reference/api/schemas.md

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎enterprise/coderd/prebuilds/claim_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -415,18 +415,18 @@ func templateWithAgentAndPresetsWithPrebuilds(desiredInstances int32) *echo.Resp
415415
Instances:desiredInstances,
416416
},
417417
},
418-
{
419-
Name:"preset-b",
420-
Parameters: []*proto.PresetParameter{
421-
{
422-
Name:"k1",
423-
Value:"v2",
424-
},
425-
},
426-
Prebuild:&proto.Prebuild{
427-
Instances:desiredInstances,
428-
},
429-
},
418+
//{
419+
//Name: "preset-b",
420+
//Parameters: []*proto.PresetParameter{
421+
//{
422+
//Name: "k1",
423+
//Value: "v2",
424+
//},
425+
//},
426+
//Prebuild: &proto.Prebuild{
427+
//Instances: desiredInstances,
428+
//},
429+
//},
430430
},
431431
},
432432
},

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp