Skip to content

Commit 8a61541

Browse files
committed
commit audit in Issue, revert tracing status writer changes
1 parent c070d74 commit 8a61541

File tree

3 files changed

+17
-26
lines changed

3 files changed

+17
-26
lines changed

coderd/tracing/status_writer.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ type StatusWriter struct {
2727
http.ResponseWriter
2828
Status int
2929
Hijacked bool
30-
doneFuncs []func() // If non-nil, this function will be called when the handler is done.
3130
responseBody []byte
3231

3332
wroteHeader bool
@@ -38,17 +37,9 @@ func StatusWriterMiddleware(next http.Handler) http.Handler {
3837
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
3938
sw := &StatusWriter{ResponseWriter: rw}
4039
next.ServeHTTP(sw, r)
41-
for _, done := range sw.doneFuncs {
42-
done()
43-
}
4440
})
4541
}
4642

47-
func (w *StatusWriter) AddDoneFunc(f func()) {
48-
// Prepend, as if deferred.
49-
w.doneFuncs = append([]func(){f}, w.doneFuncs...)
50-
}
51-
5243
func (w *StatusWriter) WriteHeader(status int) {
5344
if buildinfo.IsDev() || flag.Lookup("test.v") != nil {
5445
if w.wroteHeader {

coderd/tracing/status_writer_test.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,21 +121,16 @@ func TestStatusWriter(t *testing.T) {
121121
t.Parallel()
122122

123123
var (
124-
sw *tracing.StatusWriter
125-
done = false
126-
rr = httptest.NewRecorder()
124+
sw *tracing.StatusWriter
125+
rr = httptest.NewRecorder()
127126
)
128127
tracing.StatusWriterMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
129128
sw = w.(*tracing.StatusWriter)
130-
sw.AddDoneFunc(func() {
131-
done = true
132-
})
133129
w.WriteHeader(http.StatusNoContent)
134130
})).ServeHTTP(rr, httptest.NewRequest("GET", "/", nil))
135131

136132
require.Equal(t, http.StatusNoContent, rr.Code, "rr status code not set")
137133
require.Equal(t, http.StatusNoContent, sw.Status, "sw status code not set")
138-
require.True(t, done, "done func not called")
139134
})
140135
}
141136

coderd/workspaceapps/db.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ func (p *DBTokenProvider) Issue(ctx context.Context, rw http.ResponseWriter, r *
9696
// // permissions.
9797
dangerousSystemCtx := dbauthz.AsSystemRestricted(ctx)
9898

99-
aReq := p.auditInitAutocommitRequest(ctx, rw, r)
99+
aReq, commitAudit := p.auditInitRequest(ctx, rw, r)
100+
defer commitAudit()
100101

101102
appReq := issueReq.AppRequest.Normalize()
102103
err := appReq.Check()
@@ -371,14 +372,14 @@ type auditRequest struct {
371372
dbReq *databaseRequest
372373
}
373374

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.
378379
//
379380
// A session is unique to the agent, app, user and users IP. If any of these
380381
// 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()) {
382383
// Get the status writer from the request context so we can figure
383384
// out the HTTP status and autocommit the audit log.
384385
sw, ok := w.(*tracing.StatusWriter)
@@ -393,7 +394,13 @@ func (p *DBTokenProvider) auditInitAutocommitRequest(ctx context.Context, w http
393394

394395
// Set the commit function on the status writer to create an audit
395396
// 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+
397404
if sw.Status == http.StatusSeeOther {
398405
// Redirects aren't interesting as we will capture the audit
399406
// log after the redirect.
@@ -548,7 +555,5 @@ func (p *DBTokenProvider) auditInitAutocommitRequest(ctx context.Context, w http
548555
AdditionalFields: appInfoBytes,
549556
})
550557
}
551-
})
552-
553-
return aReq
558+
}
554559
}

0 commit comments

Comments
 (0)