@@ -444,7 +444,6 @@ func NewAuthTester(ctx context.Context, t *testing.T, client *codersdk.Client, a
444
444
func (a * AuthTester ) Test (ctx context.Context , assertRoute map [string ]RouteCheck , skipRoutes map [string ]string ) {
445
445
// Always fail auth from this point forward
446
446
a .authorizer .Wrapped = & FakeAuthorizer {
447
- Original : a .authorizer ,
448
447
AlwaysReturn : rbac .ForbiddenWithInternal (xerrors .New ("fake implementation" ), nil , nil ),
449
448
}
450
449
@@ -639,40 +638,38 @@ func (r *RecordingAuthorizer) AssertActor(t *testing.T, actor rbac.Subject, did
639
638
assert .Equalf (t , len (did ), ptr , "assert actor: didn't find all actions, %d missing actions" , len (did )- ptr )
640
639
}
641
640
642
- // _AuthorizeSQL does not record the call. This matches the postgres behavior
643
- // of not calling Authorize()
644
- func (r * RecordingAuthorizer ) _AuthorizeSQL (ctx context.Context , subject rbac.Subject , action rbac.Action , object rbac.Object ) error {
645
- if r .Wrapped == nil {
646
- panic ("Developer error: RecordingAuthorizer.Wrapped is nil" )
647
- }
648
- return r .Wrapped .Authorize (ctx , subject , action , object )
649
- }
650
-
651
- func (r * RecordingAuthorizer ) Authorize (ctx context.Context , subject rbac.Subject , action rbac.Action , object rbac.Object ) error {
641
+ func (r * RecordingAuthorizer ) RecordAuthorize (ctx context.Context , subject rbac.Subject , action rbac.Action , object rbac.Object ) {
652
642
r .Lock ()
653
643
defer r .Unlock ()
654
644
r .Called = append (r .Called , authCall {
655
645
Actor : subject ,
656
646
Action : action ,
657
647
Object : object ,
658
648
})
649
+ }
650
+
651
+ func (r * RecordingAuthorizer ) Authorize (ctx context.Context , subject rbac.Subject , action rbac.Action , object rbac.Object ) error {
652
+ r .RecordAuthorize (ctx , subject , action , object )
659
653
if r .Wrapped == nil {
660
654
panic ("Developer error: RecordingAuthorizer.Wrapped is nil" )
661
655
}
662
656
return r .Wrapped .Authorize (ctx , subject , action , object )
663
657
}
664
658
665
- func (r * RecordingAuthorizer ) Prepare (_ context.Context , subject rbac.Subject , action rbac.Action , _ string ) (rbac.PreparedAuthorized , error ) {
659
+ func (r * RecordingAuthorizer ) Prepare (ctx context.Context , subject rbac.Subject , action rbac.Action , objectType string ) (rbac.PreparedAuthorized , error ) {
666
660
r .RLock ()
667
661
defer r .RUnlock ()
668
662
if r .Wrapped == nil {
669
663
panic ("Developer error: RecordingAuthorizer.Wrapped is nil" )
670
664
}
671
- return & fakePreparedAuthorizer {
672
- Original : r ,
673
- Subject : subject ,
674
- Action : action ,
675
- HardCodedSQLString : "true" ,
665
+
666
+ prep , err := r .Wrapped .Prepare (ctx , subject , action , objectType )
667
+ if err != nil {
668
+ return nil , err
669
+ }
670
+ return & PreparedRecorder {
671
+ rec : r ,
672
+ prepped : prep ,
676
673
}, nil
677
674
}
678
675
@@ -682,46 +679,63 @@ func (r *RecordingAuthorizer) Reset() {
682
679
r .Called = nil
683
680
}
684
681
682
+ // lastCall is implemented to support legacy tests.
683
+ // Deprecated
684
+ func (r * RecordingAuthorizer ) lastCall () * authCall {
685
+ r .RLock ()
686
+ defer r .RUnlock ()
687
+ if len (r .Called ) == 0 {
688
+ return nil
689
+ }
690
+ return & r .Called [len (r .Called )- 1 ]
691
+ }
692
+
693
+ type PreparedRecorder struct {
694
+ rec * RecordingAuthorizer
695
+ prepped rbac.PreparedAuthorized
696
+ subject rbac.Subject
697
+ action rbac.Action
698
+
699
+ rw sync.Mutex
700
+ usingSQL bool
701
+ }
702
+
703
+ func (s * PreparedRecorder ) Authorize (ctx context.Context , object rbac.Object ) error {
704
+ s .rw .Lock ()
705
+ defer s .rw .Unlock ()
706
+
707
+ if ! s .usingSQL {
708
+ s .rec .RecordAuthorize (ctx , s .subject , s .action , object )
709
+ }
710
+ return s .prepped .Authorize (ctx , object )
711
+ }
712
+ func (s * PreparedRecorder ) CompileToSQL (ctx context.Context , cfg regosql.ConvertConfig ) (string , error ) {
713
+ s .rw .Lock ()
714
+ defer s .rw .Unlock ()
715
+
716
+ s .usingSQL = true
717
+ return s .prepped .CompileToSQL (ctx , cfg )
718
+ }
719
+
685
720
type fakePreparedAuthorizer struct {
686
721
sync.RWMutex
687
- Original * RecordingAuthorizer
722
+ Original * FakeAuthorizer
688
723
Subject rbac.Subject
689
724
Action rbac.Action
690
- HardCodedSQLString string
691
725
ShouldCompileToSQL bool
692
726
}
693
727
694
728
func (f * fakePreparedAuthorizer ) Authorize (ctx context.Context , object rbac.Object ) error {
695
- f .RLock ()
696
- defer f .RUnlock ()
697
- if f .ShouldCompileToSQL {
698
- return f .Original ._AuthorizeSQL (ctx , f .Subject , f .Action , object )
699
- }
700
729
return f .Original .Authorize (ctx , f .Subject , f .Action , object )
701
730
}
702
731
703
732
// CompileToSQL returns a compiled version of the authorizer that will work for
704
733
// in memory databases. This fake version will not work against a SQL database.
705
734
func (f * fakePreparedAuthorizer ) CompileToSQL (_ context.Context , _ regosql.ConvertConfig ) (string , error ) {
706
- f .Lock ()
707
- f .ShouldCompileToSQL = true
708
- f .Unlock ()
709
- return f .HardCodedSQLString , nil
710
- }
711
-
712
- // lastCall is implemented to support legacy tests.
713
- // Deprecated
714
- func (r * RecordingAuthorizer ) lastCall () * authCall {
715
- r .RLock ()
716
- defer r .RUnlock ()
717
- if len (r .Called ) == 0 {
718
- return nil
719
- }
720
- return & r .Called [len (r .Called )- 1 ]
735
+ return "not a valid sql string" , nil
721
736
}
722
737
723
738
type FakeAuthorizer struct {
724
- Original * RecordingAuthorizer
725
739
// AlwaysReturn is the error that will be returned by Authorize.
726
740
AlwaysReturn error
727
741
}
@@ -732,11 +746,14 @@ func (d *FakeAuthorizer) Authorize(_ context.Context, _ rbac.Subject, _ rbac.Act
732
746
return d .AlwaysReturn
733
747
}
734
748
749
+ func (d * FakeAuthorizer ) CompileToSQL (_ context.Context , _ regosql.ConvertConfig ) (string , error ) {
750
+ return "not a valid sql string" , nil
751
+ }
752
+
735
753
func (d * FakeAuthorizer ) Prepare (_ context.Context , subject rbac.Subject , action rbac.Action , _ string ) (rbac.PreparedAuthorized , error ) {
736
754
return & fakePreparedAuthorizer {
737
- Original : d .Original ,
738
- Subject : subject ,
739
- Action : action ,
740
- HardCodedSQLString : "true" ,
755
+ Original : d ,
756
+ Subject : subject ,
757
+ Action : action ,
741
758
}, nil
742
759
}
0 commit comments