Skip to content

Commit 806c284

Browse files
committed
add cancellable audit log v2
1 parent 5ffc829 commit 806c284

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

coderd/audit/request.go

+14-11
Original file line numberDiff line numberDiff line change
@@ -267,18 +267,21 @@ func requireOrgID[T Auditable](ctx context.Context, id uuid.UUID, log slog.Logge
267267
return id
268268
}
269269

270-
// InitRequestWithCancel returns 2 functions. The first commits the audit log,
271-
// the second cancels any future calls to commit.
272-
func InitRequestWithCancel[T Auditable](w http.ResponseWriter, p *RequestParams) (aReq *Request[T], commit func(), cancel func()) {
273-
req, commit := InitRequest[T](w, p)
274-
canceled := false
275-
return req, func() {
276-
if !canceled {
277-
commit()
278-
}
279-
}, func() {
280-
canceled = true
270+
// InitRequestWithCancel returns a commit function with a boolean arg.
271+
// If the arg is false, future calls to commit() will not create an audit log
272+
// entry.
273+
func InitRequestWithCancel[T Auditable](w http.ResponseWriter, p *RequestParams) (*Request[T], func(commit bool)) {
274+
req, commitF := InitRequest[T](w, p)
275+
cancelled := false
276+
return req, func(commit bool) {
277+
if !commit {
278+
cancelled = true
279+
return
281280
}
281+
if !cancelled {
282+
commitF()
283+
}
284+
}
282285
}
283286

284287
// InitRequest initializes an audit log for a request. It returns a function

enterprise/coderd/scim.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -272,14 +272,14 @@ func (api *API) scimPatchUser(rw http.ResponseWriter, r *http.Request) {
272272
}
273273

274274
auditor := *api.AGPL.Auditor.Load()
275-
aReq, commitAudit, cancelAudit := audit.InitRequestWithCancel[database.User](rw, &audit.RequestParams{
275+
aReq, commitAudit := audit.InitRequestWithCancel[database.User](rw, &audit.RequestParams{
276276
Audit: auditor,
277277
Log: api.Logger,
278278
Request: r,
279279
Action: database.AuditActionWrite,
280280
})
281281

282-
defer commitAudit()
282+
defer commitAudit(true)
283283

284284
id := chi.URLParam(r, "id")
285285

@@ -338,7 +338,7 @@ func (api *API) scimPatchUser(rw http.ResponseWriter, r *http.Request) {
338338
dbUser = userNew
339339
} else {
340340
// Do not push an audit log if there is no change.
341-
cancelAudit()
341+
commitAudit(false)
342342
}
343343

344344
aReq.New = dbUser

0 commit comments

Comments
 (0)