@@ -24,9 +24,7 @@ import (
24
24
"github.com/coder/coder/v2/coderd/rbac/regosql"
25
25
"github.com/coder/coder/v2/coderd/rbac/regosql/sqltypes"
26
26
"github.com/coder/coder/v2/coderd/tracing"
27
- "github.com/coder/coder/v2/coderd/util/ptr"
28
27
"github.com/coder/coder/v2/coderd/util/slice"
29
- "github.com/coder/coder/v2/coderd/util/syncmap"
30
28
)
31
29
32
30
type AuthCall struct {
@@ -770,16 +768,21 @@ func (c *authRecorder) Prepare(ctx context.Context, subject Subject, action poli
770
768
771
769
type authzCheckRecorderKey struct {}
772
770
773
- func WithAuthzCheckRecorder (ctx context.Context ) context.Context {
774
- return context .WithValue (ctx , authzCheckRecorderKey {}, ptr .Ref (AuthzCheckRecorder {
775
- checks : syncmap.Map [string , bool ]{},
776
- }))
771
+ type AuthzCheckRecorder struct {
772
+ // lock guards checks
773
+ lock sync.Mutex
774
+ // checks is a list preformatted authz check IDs and their result
775
+ checks []recordedCheck
777
776
}
778
777
779
- type AuthzCheckRecorder struct {
780
- // Checks is a map from preformatted authz check IDs to their authorization
781
- // status (true => authorized, false => not authorized)
782
- checks syncmap.Map [string , bool ]
778
+ type recordedCheck struct {
779
+ name string
780
+ // true => authorized, false => not authorized
781
+ result bool
782
+ }
783
+
784
+ func WithAuthzCheckRecorder (ctx context.Context ) context.Context {
785
+ return context .WithValue (ctx , authzCheckRecorderKey {}, & AuthzCheckRecorder {})
783
786
}
784
787
785
788
func recordAuthzCheck (ctx context.Context , action policy.Action , object Object , authorized bool ) {
@@ -819,7 +822,9 @@ func recordAuthzCheck(ctx context.Context, action policy.Action, object Object,
819
822
return
820
823
}
821
824
822
- r .checks .Store (b .String (), authorized )
825
+ r .lock .Lock ()
826
+ defer r .lock .Unlock ()
827
+ r .checks = append (r .checks , recordedCheck {name : b .String (), result : authorized })
823
828
}
824
829
825
830
func GetAuthzCheckRecorder (ctx context.Context ) (* AuthzCheckRecorder , bool ) {
@@ -833,8 +838,15 @@ func GetAuthzCheckRecorder(ctx context.Context) (*AuthzCheckRecorder, bool) {
833
838
834
839
// String serializes all of the checks recorded, using the following syntax:
835
840
func (r * AuthzCheckRecorder ) String () string {
836
- checks := make ([]string , 0 )
837
- for check , result := range r .checks .Seq () {
841
+ r .lock .Lock ()
842
+ defer r .lock .Unlock ()
843
+
844
+ if len (r .checks ) == 0 {
845
+ return "nil"
846
+ }
847
+
848
+ checks := make ([]string , 0 , len (r .checks ))
849
+ for check , result := range r .checks {
838
850
checks = append (checks , fmt .Sprintf ("%v=%v" , check , result ))
839
851
}
840
852
return strings .Join (checks , "; " )
0 commit comments