mirror of
https://github.com/coder/coder.git
synced 2025-07-15 22:20:27 +00:00
chore: rearrange audit logging code into enterprise folder (#3741)
This commit is contained in:
52
coderd/audit/request.go
Normal file
52
coderd/audit/request.go
Normal file
@ -0,0 +1,52 @@
|
||||
package audit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
chimw "github.com/go-chi/chi/v5/middleware"
|
||||
"github.com/google/uuid"
|
||||
|
||||
"cdr.dev/slog"
|
||||
"github.com/coder/coder/coderd/database"
|
||||
)
|
||||
|
||||
type RequestParams struct {
|
||||
Audit Auditor
|
||||
Log slog.Logger
|
||||
|
||||
Action database.AuditAction
|
||||
ResourceType database.ResourceType
|
||||
Actor uuid.UUID
|
||||
}
|
||||
|
||||
type Request[T Auditable] struct {
|
||||
params *RequestParams
|
||||
|
||||
Old T
|
||||
New T
|
||||
}
|
||||
|
||||
// InitRequest initializes an audit log for a request. It returns a function
|
||||
// that should be deferred, causing the audit log to be committed when the
|
||||
// handler returns.
|
||||
func InitRequest[T Auditable](w http.ResponseWriter, p *RequestParams) (*Request[T], func()) {
|
||||
sw, ok := w.(chimw.WrapResponseWriter)
|
||||
if !ok {
|
||||
panic("dev error: http.ResponseWriter is not chimw.WrapResponseWriter")
|
||||
}
|
||||
|
||||
req := &Request[T]{
|
||||
params: p,
|
||||
}
|
||||
|
||||
return req, func() {
|
||||
ctx := context.Background()
|
||||
code := sw.Status()
|
||||
|
||||
err := p.Audit.Export(ctx, database.AuditLog{StatusCode: int32(code)})
|
||||
if err != nil {
|
||||
p.Log.Error(ctx, "export audit log", slog.Error(err))
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user