Skip to content

Commit 007f2df

Browse files
authored
fix: use API, not request context to insert audit/connection logs (#20829)
Fixes: #20744 Upsert audit and connection log entries with a context derived from the API context, rather than the individual request so that we don't error out if the request is canceled or the client hangs up (e.g. if we return an error).
1 parent 48b8e22 commit 007f2df

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

coderd/coderd.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,7 @@ func New(options *Options) *API {
610610
dbRolluper: options.DatabaseRolluper,
611611
}
612612
api.WorkspaceAppsProvider = workspaceapps.NewDBTokenProvider(
613+
ctx,
613614
options.Logger.Named("workspaceapps"),
614615
options.AccessURL,
615616
options.Authorizer,

coderd/workspaceapps/db.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
// by querying the database if the request is missing a valid token.
3636
type DBTokenProvider struct {
3737
Logger slog.Logger
38+
ctx context.Context
3839

3940
// DashboardURL is the main dashboard access URL for error pages.
4041
DashboardURL *url.URL
@@ -50,7 +51,8 @@ type DBTokenProvider struct {
5051

5152
var _ SignedTokenProvider = &DBTokenProvider{}
5253

53-
func NewDBTokenProvider(log slog.Logger,
54+
func NewDBTokenProvider(ctx context.Context,
55+
log slog.Logger,
5456
accessURL *url.URL,
5557
authz rbac.Authorizer,
5658
connectionLogger *atomic.Pointer[connectionlog.ConnectionLogger],
@@ -70,6 +72,7 @@ func NewDBTokenProvider(log slog.Logger,
7072

7173
return &DBTokenProvider{
7274
Logger: log,
75+
ctx: ctx,
7376
DashboardURL: accessURL,
7477
Authorizer: authz,
7578
ConnectionLogger: connectionLogger,
@@ -94,7 +97,7 @@ func (p *DBTokenProvider) Issue(ctx context.Context, rw http.ResponseWriter, r *
9497
// // permissions.
9598
dangerousSystemCtx := dbauthz.AsSystemRestricted(ctx)
9699

97-
aReq, commitAudit := p.connLogInitRequest(ctx, rw, r)
100+
aReq, commitAudit := p.connLogInitRequest(rw, r)
98101
defer commitAudit()
99102

100103
appReq := issueReq.AppRequest.Normalize()
@@ -406,7 +409,7 @@ type connLogRequest struct {
406409
//
407410
// A session is unique to the agent, app, user and users IP. If any of these
408411
// values change, a new session and connect log is created.
409-
func (p *DBTokenProvider) connLogInitRequest(ctx context.Context, w http.ResponseWriter, r *http.Request) (aReq *connLogRequest, commit func()) {
412+
func (p *DBTokenProvider) connLogInitRequest(w http.ResponseWriter, r *http.Request) (aReq *connLogRequest, commit func()) {
410413
// Get the status writer from the request context so we can figure
411414
// out the HTTP status and autocommit the audit log.
412415
sw, ok := w.(*tracing.StatusWriter)
@@ -422,6 +425,9 @@ func (p *DBTokenProvider) connLogInitRequest(ctx context.Context, w http.Respons
422425
// this ensures that the status and response body are available.
423426
var committed bool
424427
return aReq, func() {
428+
// We want to log/audit the connection attempt even if the request context has expired.
429+
ctx, cancel := context.WithCancel(p.ctx)
430+
defer cancel()
425431
if committed {
426432
return
427433
}

0 commit comments

Comments
 (0)