mirror of
https://github.com/coder/coder.git
synced 2025-07-13 21:36:50 +00:00
feat: Add initial AuthzQuerier implementation (#5919)
feat: Add initial AuthzQuerier implementation - Adds package database/dbauthz that adds a database.Store implementation where each method goes through AuthZ checks - Implements all database.Store methods on AuthzQuerier - Updates and fixes unit tests where required - Updates coderd initialization to use AuthzQuerier if codersdk.ExperimentAuthzQuerier is enabled
This commit is contained in:
37
coderd/httpmw/authz.go
Normal file
37
coderd/httpmw/authz.go
Normal file
@ -0,0 +1,37 @@
|
||||
package httpmw
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/coder/coder/coderd/database/dbauthz"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
// AsAuthzSystem is a chained handler that temporarily sets the dbauthz context
|
||||
// to System for the inner handlers, and resets the context afterwards.
|
||||
//
|
||||
// TODO: Refactor the middleware functions to not require this.
|
||||
// This is a bit of a kludge for now as some middleware functions require
|
||||
// usage as a system user in some cases, but not all cases. To avoid large
|
||||
// refactors, we use this middleware to temporarily set the context to a system.
|
||||
func AsAuthzSystem(mws ...func(http.Handler) http.Handler) func(http.Handler) http.Handler {
|
||||
chain := chi.Chain(mws...)
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
before, beforeExists := dbauthz.ActorFromContext(r.Context())
|
||||
if !beforeExists {
|
||||
// AsRemoveActor will actually remove the actor from the context.
|
||||
before = dbauthz.AsRemoveActor
|
||||
}
|
||||
|
||||
// nolint:gocritic // AsAuthzSystem needs to do this.
|
||||
r = r.WithContext(dbauthz.AsSystem(ctx))
|
||||
chain.Handler(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
||||
r = r.WithContext(dbauthz.As(r.Context(), before))
|
||||
next.ServeHTTP(rw, r)
|
||||
})).ServeHTTP(rw, r)
|
||||
})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user