chore: make authz recorder opt in (#20310)

The authz recorder is causing a lot of memory to be allocated, and is a
memory leak for websocket connections.

This change makes it opt-in on a per request basis (ontop of `isDev`).
To get the authz headers, use `Copy as cURL` on chrome and append the
header `x-authz-checks=true`.
This commit is contained in:
Steven Masley
2025-10-21 14:15:37 +00:00
committed by GitHub
parent 650dc860bd
commit 86f0f39863
8 changed files with 43 additions and 7 deletions
+3
View File
@@ -13977,6 +13977,9 @@ const docTemplate = `{
"docs_url": {
"$ref": "#/definitions/serpent.URL"
},
"enable_authz_recording": {
"type": "boolean"
},
"enable_terraform_debug_mode": {
"type": "boolean"
},
+3
View File
@@ -12595,6 +12595,9 @@
"docs_url": {
"$ref": "#/definitions/serpent.URL"
},
"enable_authz_recording": {
"type": "boolean"
},
"enable_terraform_debug_mode": {
"type": "boolean"
},
+1 -1
View File
@@ -493,7 +493,7 @@ func New(options *Options) *API {
// We add this middleware early, to make sure that authorization checks made
// by other middleware get recorded.
if buildinfo.IsDev() {
r.Use(httpmw.RecordAuthzChecks)
r.Use(httpmw.RecordAuthzChecks(options.DeploymentValues.EnableAuthzRecording.Value()))
}
ctx, cancel := context.WithCancel(context.Background())
+17 -6
View File
@@ -4,6 +4,7 @@ package httpmw
import (
"net/http"
"strconv"
"github.com/go-chi/chi/v5"
@@ -39,14 +40,24 @@ func AsAuthzSystem(mws ...func(http.Handler) http.Handler) func(http.Handler) ht
}
}
// RecordAuthzChecks enables recording all of the authorization checks that
// RecordAuthzChecks enables recording all the authorization checks that
// occurred in the processing of a request. This is mostly helpful for debugging
// and understanding what permissions are required for a given action.
//
// Can either be toggled on by a deployment wide configuration value, or opt-in on
// a per-request basis by setting the `x-record-authz-checks` header to a truthy value.
//
// Requires using a Recorder Authorizer.
func RecordAuthzChecks(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
r = r.WithContext(rbac.WithAuthzCheckRecorder(r.Context()))
next.ServeHTTP(rw, r)
})
//
//nolint:revive
func RecordAuthzChecks(always bool) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
if enabled, _ := strconv.ParseBool(r.Header.Get("x-record-authz-checks")); enabled || always {
r = r.WithContext(rbac.WithAuthzCheckRecorder(r.Context()))
}
next.ServeHTTP(rw, r)
})
}
}
+14
View File
@@ -487,6 +487,7 @@ type DeploymentValues struct {
Sessions SessionLifetime `json:"session_lifetime,omitempty" typescript:",notnull"`
DisablePasswordAuth serpent.Bool `json:"disable_password_auth,omitempty" typescript:",notnull"`
Support SupportConfig `json:"support,omitempty" typescript:",notnull"`
EnableAuthzRecording serpent.Bool `json:"enable_authz_recording,omitempty" typescript:",notnull"`
ExternalAuthConfigs serpent.Struct[[]ExternalAuthConfig] `json:"external_auth,omitempty" typescript:",notnull"`
SSHConfig SSHConfig `json:"config_ssh,omitempty" typescript:",notnull"`
WgtunnelHost serpent.String `json:"wgtunnel_host,omitempty" typescript:",notnull"`
@@ -3293,6 +3294,19 @@ Write out the current server config as YAML to stdout.`,
YAML: "key",
Hidden: true,
},
{
Name: "Enable Authorization Recordings",
Description: "All api requests will have a header including all authorization calls made during the request. " +
"This is used for debugging purposes and only available for dev builds.",
Required: false,
Flag: "enable-authz-recordings",
Env: "CODER_ENABLE_AUTHZ_RECORDINGS",
Default: "false",
Value: &c.EnableAuthzRecording,
// Do not show this option ever. It is a developer tool only, and not to be
// used externally.
Hidden: true,
},
}
return opts
+1
View File
@@ -237,6 +237,7 @@ curl -X GET http://coder-server:8080/api/v2/deployment/config \
"scheme": "string",
"user": {}
},
"enable_authz_recording": true,
"enable_terraform_debug_mode": true,
"ephemeral_deployment": true,
"experiments": [
+3
View File
@@ -2892,6 +2892,7 @@ CreateWorkspaceRequest provides options for creating a new workspace. Only one o
"scheme": "string",
"user": {}
},
"enable_authz_recording": true,
"enable_terraform_debug_mode": true,
"ephemeral_deployment": true,
"experiments": [
@@ -3397,6 +3398,7 @@ CreateWorkspaceRequest provides options for creating a new workspace. Only one o
"scheme": "string",
"user": {}
},
"enable_authz_recording": true,
"enable_terraform_debug_mode": true,
"ephemeral_deployment": true,
"experiments": [
@@ -3731,6 +3733,7 @@ CreateWorkspaceRequest provides options for creating a new workspace. Only one o
| `disable_password_auth` | boolean | false | | |
| `disable_path_apps` | boolean | false | | |
| `docs_url` | [serpent.URL](#serpenturl) | false | | |
| `enable_authz_recording` | boolean | false | | |
| `enable_terraform_debug_mode` | boolean | false | | |
| `ephemeral_deployment` | boolean | false | | |
| `experiments` | array of string | false | | |
+1
View File
@@ -1755,6 +1755,7 @@ export interface DeploymentValues {
readonly session_lifetime?: SessionLifetime;
readonly disable_password_auth?: boolean;
readonly support?: SupportConfig;
readonly enable_authz_recording?: boolean;
readonly external_auth?: SerpentStruct<ExternalAuthConfig[]>;
readonly config_ssh?: SSHConfig;
readonly wgtunnel_host?: string;