@@ -412,6 +412,9 @@ var (
412
412
policy .ActionCreate , policy .ActionDelete , policy .ActionRead , policy .ActionUpdate ,
413
413
policy .ActionWorkspaceStart , policy .ActionWorkspaceStop ,
414
414
},
415
+ rbac .ResourcePrebuiltWorkspace .Type : {
416
+ policy .ActionRead , policy .ActionUpdate , policy .ActionDelete ,
417
+ },
415
418
// Should be able to add the prebuilds system user as a member to any organization that needs prebuilds.
416
419
rbac .ResourceOrganizationMember .Type : {
417
420
policy .ActionCreate ,
@@ -527,9 +530,9 @@ func As(ctx context.Context, actor rbac.Subject) context.Context {
527
530
// running the insertFunc. The insertFunc is expected to return the object that
528
531
// was inserted.
529
532
func insert [
530
- ObjectType any ,
531
- ArgumentType any ,
532
- Insert func (ctx context.Context , arg ArgumentType ) (ObjectType , error ),
533
+ ObjectType any ,
534
+ ArgumentType any ,
535
+ Insert func (ctx context.Context , arg ArgumentType ) (ObjectType , error ),
533
536
](
534
537
logger slog.Logger ,
535
538
authorizer rbac.Authorizer ,
@@ -540,9 +543,9 @@ func insert[
540
543
}
541
544
542
545
func insertWithAction [
543
- ObjectType any ,
544
- ArgumentType any ,
545
- Insert func (ctx context.Context , arg ArgumentType ) (ObjectType , error ),
546
+ ObjectType any ,
547
+ ArgumentType any ,
548
+ Insert func (ctx context.Context , arg ArgumentType ) (ObjectType , error ),
546
549
](
547
550
logger slog.Logger ,
548
551
authorizer rbac.Authorizer ,
@@ -569,10 +572,10 @@ func insertWithAction[
569
572
}
570
573
571
574
func deleteQ [
572
- ObjectType rbac.Objecter ,
573
- ArgumentType any ,
574
- Fetch func (ctx context.Context , arg ArgumentType ) (ObjectType , error ),
575
- Delete func (ctx context.Context , arg ArgumentType ) error ,
575
+ ObjectType rbac.Objecter ,
576
+ ArgumentType any ,
577
+ Fetch func (ctx context.Context , arg ArgumentType ) (ObjectType , error ),
578
+ Delete func (ctx context.Context , arg ArgumentType ) error ,
576
579
](
577
580
logger slog.Logger ,
578
581
authorizer rbac.Authorizer ,
@@ -584,10 +587,10 @@ func deleteQ[
584
587
}
585
588
586
589
func updateWithReturn [
587
- ObjectType rbac.Objecter ,
588
- ArgumentType any ,
589
- Fetch func (ctx context.Context , arg ArgumentType ) (ObjectType , error ),
590
- UpdateQuery func (ctx context.Context , arg ArgumentType ) (ObjectType , error ),
590
+ ObjectType rbac.Objecter ,
591
+ ArgumentType any ,
592
+ Fetch func (ctx context.Context , arg ArgumentType ) (ObjectType , error ),
593
+ UpdateQuery func (ctx context.Context , arg ArgumentType ) (ObjectType , error ),
591
594
](
592
595
logger slog.Logger ,
593
596
authorizer rbac.Authorizer ,
@@ -598,10 +601,10 @@ func updateWithReturn[
598
601
}
599
602
600
603
func update [
601
- ObjectType rbac.Objecter ,
602
- ArgumentType any ,
603
- Fetch func (ctx context.Context , arg ArgumentType ) (ObjectType , error ),
604
- Exec func (ctx context.Context , arg ArgumentType ) error ,
604
+ ObjectType rbac.Objecter ,
605
+ ArgumentType any ,
606
+ Fetch func (ctx context.Context , arg ArgumentType ) (ObjectType , error ),
607
+ Exec func (ctx context.Context , arg ArgumentType ) error ,
605
608
](
606
609
logger slog.Logger ,
607
610
authorizer rbac.Authorizer ,
@@ -619,9 +622,9 @@ func update[
619
622
// user cannot read the resource. This is because the resource details are
620
623
// required to run a proper authorization check.
621
624
func fetchWithAction [
622
- ArgumentType any ,
623
- ObjectType rbac.Objecter ,
624
- DatabaseFunc func (ctx context.Context , arg ArgumentType ) (ObjectType , error ),
625
+ ArgumentType any ,
626
+ ObjectType rbac.Objecter ,
627
+ DatabaseFunc func (ctx context.Context , arg ArgumentType ) (ObjectType , error ),
625
628
](
626
629
logger slog.Logger ,
627
630
authorizer rbac.Authorizer ,
@@ -652,9 +655,9 @@ func fetchWithAction[
652
655
}
653
656
654
657
func fetch [
655
- ArgumentType any ,
656
- ObjectType rbac.Objecter ,
657
- DatabaseFunc func (ctx context.Context , arg ArgumentType ) (ObjectType , error ),
658
+ ArgumentType any ,
659
+ ObjectType rbac.Objecter ,
660
+ DatabaseFunc func (ctx context.Context , arg ArgumentType ) (ObjectType , error ),
658
661
](
659
662
logger slog.Logger ,
660
663
authorizer rbac.Authorizer ,
@@ -667,10 +670,10 @@ func fetch[
667
670
// from SQL 'exec' functions which only return an error.
668
671
// See fetchAndQuery for more information.
669
672
func fetchAndExec [
670
- ObjectType rbac.Objecter ,
671
- ArgumentType any ,
672
- Fetch func (ctx context.Context , arg ArgumentType ) (ObjectType , error ),
673
- Exec func (ctx context.Context , arg ArgumentType ) error ,
673
+ ObjectType rbac.Objecter ,
674
+ ArgumentType any ,
675
+ Fetch func (ctx context.Context , arg ArgumentType ) (ObjectType , error ),
676
+ Exec func (ctx context.Context , arg ArgumentType ) error ,
674
677
](
675
678
logger slog.Logger ,
676
679
authorizer rbac.Authorizer ,
@@ -693,10 +696,10 @@ func fetchAndExec[
693
696
// **before** the query runs. The returns from the fetch are only used to
694
697
// assert rbac. The final return of this function comes from the Query function.
695
698
func fetchAndQuery [
696
- ObjectType rbac.Objecter ,
697
- ArgumentType any ,
698
- Fetch func (ctx context.Context , arg ArgumentType ) (ObjectType , error ),
699
- Query func (ctx context.Context , arg ArgumentType ) (ObjectType , error ),
699
+ ObjectType rbac.Objecter ,
700
+ ArgumentType any ,
701
+ Fetch func (ctx context.Context , arg ArgumentType ) (ObjectType , error ),
702
+ Query func (ctx context.Context , arg ArgumentType ) (ObjectType , error ),
700
703
](
701
704
logger slog.Logger ,
702
705
authorizer rbac.Authorizer ,
@@ -730,9 +733,9 @@ func fetchAndQuery[
730
733
// fetchWithPostFilter is like fetch, but works with lists of objects.
731
734
// SQL filters are much more optimal.
732
735
func fetchWithPostFilter [
733
- ArgumentType any ,
734
- ObjectType rbac.Objecter ,
735
- DatabaseFunc func (ctx context.Context , arg ArgumentType ) ([]ObjectType , error ),
736
+ ArgumentType any ,
737
+ ObjectType rbac.Objecter ,
738
+ DatabaseFunc func (ctx context.Context , arg ArgumentType ) ([]ObjectType , error ),
736
739
](
737
740
authorizer rbac.Authorizer ,
738
741
action policy.Action ,
@@ -3909,7 +3912,14 @@ func (q *querier) InsertWorkspaceBuild(ctx context.Context, arg database.InsertW
3909
3912
action = policy .ActionWorkspaceStop
3910
3913
}
3911
3914
3912
- if err = q .authorizeContext (ctx , action , w ); err != nil {
3915
+ if action == policy .ActionDelete && w .IsPrebuild () {
3916
+ if err := q .authorizeContext (ctx , action , w .PrebuildRBAC ()); err != nil {
3917
+ // Fallback to normal workspace auth check
3918
+ if err = q .authorizeContext (ctx , action , w ); err != nil {
3919
+ return xerrors .Errorf ("authorize context: %w" , err )
3920
+ }
3921
+ }
3922
+ } else if err = q .authorizeContext (ctx , action , w ); err != nil {
3913
3923
return xerrors .Errorf ("authorize context: %w" , err )
3914
3924
}
3915
3925
@@ -3949,7 +3959,15 @@ func (q *querier) InsertWorkspaceBuildParameters(ctx context.Context, arg databa
3949
3959
return err
3950
3960
}
3951
3961
3952
- err = q .authorizeContext (ctx , policy .ActionUpdate , workspace )
3962
+ if workspace .IsPrebuild () {
3963
+ err = q .authorizeContext (ctx , policy .ActionUpdate , workspace .PrebuildRBAC ())
3964
+ // Fallback to normal workspace auth check
3965
+ if err != nil {
3966
+ err = q .authorizeContext (ctx , policy .ActionUpdate , workspace )
3967
+ }
3968
+ } else {
3969
+ err = q .authorizeContext (ctx , policy .ActionUpdate , workspace )
3970
+ }
3953
3971
if err != nil {
3954
3972
return err
3955
3973
}
0 commit comments