Skip to content

Commit fe0e466

Browse files
committed
🧹
1 parent d59ecbe commit fe0e466

File tree

3 files changed

+26
-23
lines changed

3 files changed

+26
-23
lines changed

coderd/httpapi/httpapi.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,6 @@ func Write(ctx context.Context, rw http.ResponseWriter, status int, response int
210210
// configured on server startup for development and testing builds.
211211
// - If this header is missing from a response, make sure the response is
212212
// being written by calling `httpapi.Write`!
213-
// - An empty x-authz-checks header can be valid! Some requests don't
214-
// require authorization.
215213
rw.Header().Set("x-authz-checks", rec.String())
216214
}
217215

@@ -231,7 +229,7 @@ func WriteIndent(ctx context.Context, rw http.ResponseWriter, status int, respon
231229
defer span.End()
232230

233231
if rec, ok := rbac.GetAuthzCheckRecorder(ctx); ok {
234-
rw.Header().Set("x-dbauthz-checks", rec.String())
232+
rw.Header().Set("x-authz-checks", rec.String())
235233
}
236234

237235
rw.Header().Set("Content-Type", "application/json; charset=utf-8")

coderd/rbac/authz.go

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ import (
2424
"github.com/coder/coder/v2/coderd/rbac/regosql"
2525
"github.com/coder/coder/v2/coderd/rbac/regosql/sqltypes"
2626
"github.com/coder/coder/v2/coderd/tracing"
27-
"github.com/coder/coder/v2/coderd/util/ptr"
2827
"github.com/coder/coder/v2/coderd/util/slice"
29-
"github.com/coder/coder/v2/coderd/util/syncmap"
3028
)
3129

3230
type AuthCall struct {
@@ -770,16 +768,21 @@ func (c *authRecorder) Prepare(ctx context.Context, subject Subject, action poli
770768

771769
type authzCheckRecorderKey struct{}
772770

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
777776
}
778777

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{})
783786
}
784787

785788
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,
819822
return
820823
}
821824

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})
823828
}
824829

825830
func GetAuthzCheckRecorder(ctx context.Context) (*AuthzCheckRecorder, bool) {
@@ -833,8 +838,15 @@ func GetAuthzCheckRecorder(ctx context.Context) (*AuthzCheckRecorder, bool) {
833838

834839
// String serializes all of the checks recorded, using the following syntax:
835840
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 {
838850
checks = append(checks, fmt.Sprintf("%v=%v", check, result))
839851
}
840852
return strings.Join(checks, "; ")

coderd/util/syncmap/map.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package syncmap
22

33
import (
4-
"iter"
54
"sync"
65
)
76

@@ -78,9 +77,3 @@ func (m *Map[K, V]) Range(f func(key K, value V) bool) {
7877
return f(key.(K), value.(V))
7978
})
8079
}
81-
82-
func (m *Map[K, V]) Seq() iter.Seq2[K, V] {
83-
return func(yield func(K, V) bool) {
84-
m.Range(yield)
85-
}
86-
}

0 commit comments

Comments
 (0)