@@ -96,7 +96,8 @@ func (p *DBTokenProvider) Issue(ctx context.Context, rw http.ResponseWriter, r *
96
96
// // permissions.
97
97
dangerousSystemCtx := dbauthz .AsSystemRestricted (ctx )
98
98
99
- aReq := p .auditInitAutocommitRequest (ctx , rw , r )
99
+ aReq , commitAudit := p .auditInitRequest (ctx , rw , r )
100
+ defer commitAudit ()
100
101
101
102
appReq := issueReq .AppRequest .Normalize ()
102
103
err := appReq .Check ()
@@ -371,14 +372,14 @@ type auditRequest struct {
371
372
dbReq * databaseRequest
372
373
}
373
374
374
- // auditInitAutocommitRequest creates a new audit session and audit log for the
375
- // given request, if one does not already exist. If an audit session already
376
- // exists, it will be updated with the current timestamp. A session is used to
377
- // reduce the number of audit logs created.
375
+ // auditInitRequest creates a new audit session and audit log for the given
376
+ // request, if one does not already exist. If an audit session already exists,
377
+ // it will be updated with the current timestamp. A session is used to reduce
378
+ // the number of audit logs created.
378
379
//
379
380
// A session is unique to the agent, app, user and users IP. If any of these
380
381
// values change, a new session and audit log is created.
381
- func (p * DBTokenProvider ) auditInitAutocommitRequest (ctx context.Context , w http.ResponseWriter , r * http.Request ) (aReq * auditRequest ) {
382
+ func (p * DBTokenProvider ) auditInitRequest (ctx context.Context , w http.ResponseWriter , r * http.Request ) (aReq * auditRequest , commit func () ) {
382
383
// Get the status writer from the request context so we can figure
383
384
// out the HTTP status and autocommit the audit log.
384
385
sw , ok := w .(* tracing.StatusWriter )
@@ -393,7 +394,13 @@ func (p *DBTokenProvider) auditInitAutocommitRequest(ctx context.Context, w http
393
394
394
395
// Set the commit function on the status writer to create an audit
395
396
// log, this ensures that the status and response body are available.
396
- sw .AddDoneFunc (func () {
397
+ var committed bool
398
+ return aReq , func () {
399
+ if committed {
400
+ return
401
+ }
402
+ committed = true
403
+
397
404
if sw .Status == http .StatusSeeOther {
398
405
// Redirects aren't interesting as we will capture the audit
399
406
// log after the redirect.
@@ -548,7 +555,5 @@ func (p *DBTokenProvider) auditInitAutocommitRequest(ctx context.Context, w http
548
555
AdditionalFields : appInfoBytes ,
549
556
})
550
557
}
551
- })
552
-
553
- return aReq
558
+ }
554
559
}
0 commit comments