7
7
"net/http"
8
8
"strconv"
9
9
"strings"
10
+ "sync"
10
11
"testing"
11
12
"time"
12
13
@@ -548,6 +549,7 @@ type authCall struct {
548
549
}
549
550
550
551
type RecordingAuthorizer struct {
552
+ sync.RWMutex
551
553
Called []authCall
552
554
Wrapped rbac.Authorizer
553
555
}
@@ -569,6 +571,8 @@ func (*RecordingAuthorizer) Pair(action rbac.Action, object rbac.Objecter) Actio
569
571
}
570
572
571
573
func (r * RecordingAuthorizer ) AllAsserted () error {
574
+ r .RLock ()
575
+ defer r .RUnlock ()
572
576
missed := 0
573
577
for _ , c := range r .Called {
574
578
if ! c .asserted {
@@ -587,6 +591,8 @@ func (r *RecordingAuthorizer) AllAsserted() error {
587
591
// It will not assert the same call twice, so if there is a duplicate assertion,
588
592
// the pair will need to be passed in twice.
589
593
func (r * RecordingAuthorizer ) UnorderedAssertActor (t * testing.T , actor rbac.Subject , dids ... ActionObjectPair ) {
594
+ r .RLock ()
595
+ defer r .RUnlock ()
590
596
for _ , did := range dids {
591
597
found := false
592
598
InnerCalledLoop:
@@ -612,6 +618,8 @@ func (r *RecordingAuthorizer) UnorderedAssertActor(t *testing.T, actor rbac.Subj
612
618
// AssertActor asserts in order. If the order of authz calls does not match,
613
619
// this will fail.
614
620
func (r * RecordingAuthorizer ) AssertActor (t * testing.T , actor rbac.Subject , did ... ActionObjectPair ) {
621
+ r .RLock ()
622
+ defer r .RUnlock ()
615
623
ptr := 0
616
624
for i , call := range r .Called {
617
625
if ptr == len (did ) {
@@ -640,6 +648,8 @@ func (r *RecordingAuthorizer) _AuthorizeSQL(ctx context.Context, subject rbac.Su
640
648
}
641
649
642
650
func (r * RecordingAuthorizer ) Authorize (ctx context.Context , subject rbac.Subject , action rbac.Action , object rbac.Object ) error {
651
+ r .Lock ()
652
+ defer r .Unlock ()
643
653
r .Called = append (r .Called , authCall {
644
654
Actor : subject ,
645
655
Action : action ,
@@ -668,32 +678,30 @@ func (r *RecordingAuthorizer) reset() {
668
678
}
669
679
670
680
type fakePreparedAuthorizer struct {
671
- Original * RecordingAuthorizer
672
- Subject rbac.Subject
673
- Action rbac.Action
674
- HardCodedSQLString string
675
- HardCodedRegoString string
681
+ sync.RWMutex
682
+ Original * RecordingAuthorizer
683
+ Subject rbac.Subject
684
+ Action rbac.Action
685
+ HardCodedSQLString string
686
+ ShouldCompileToSQL bool
676
687
}
677
688
678
689
func (f * fakePreparedAuthorizer ) Authorize (ctx context.Context , object rbac.Object ) error {
679
- return f .Original ._AuthorizeSQL (ctx , f .Subject , f .Action , object )
690
+ f .RLock ()
691
+ defer f .RUnlock ()
692
+ if f .ShouldCompileToSQL {
693
+ return f .Original ._AuthorizeSQL (ctx , f .Subject , f .Action , object )
694
+ }
695
+ return f .Original .Authorize (ctx , f .Subject , f .Action , object )
680
696
}
681
697
682
698
// CompileToSQL returns a compiled version of the authorizer that will work for
683
699
// in memory databases. This fake version will not work against a SQL database.
684
- func (fakePreparedAuthorizer ) CompileToSQL (_ context.Context , _ regosql.ConvertConfig ) (string , error ) {
685
- return "" , xerrors .New ("not implemented" )
686
- }
687
-
688
- func (f * fakePreparedAuthorizer ) Eval (object rbac.Object ) bool {
689
- return f .Original ._AuthorizeSQL (context .Background (), f .Subject , f .Action , object ) == nil
690
- }
691
-
692
- func (f fakePreparedAuthorizer ) RegoString () string {
693
- if f .HardCodedRegoString != "" {
694
- return f .HardCodedRegoString
695
- }
696
- panic ("not implemented" )
700
+ func (f * fakePreparedAuthorizer ) CompileToSQL (_ context.Context , _ regosql.ConvertConfig ) (string , error ) {
701
+ f .Lock ()
702
+ f .ShouldCompileToSQL = true
703
+ f .Unlock ()
704
+ return f .HardCodedSQLString , nil
697
705
}
698
706
699
707
// LastCall is implemented to support legacy tests.
0 commit comments