@@ -35,6 +35,7 @@ import (
3535// by querying the database if the request is missing a valid token.
3636type 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
5152var _ 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