From b3239b01f8268dab44e8fc8821efbe415d6badbd Mon Sep 17 00:00:00 2001 From: Chris Twomey Date: Fri, 22 Mar 2024 12:09:10 +0000 Subject: [PATCH] Option to not log raw request body --- audit/audit.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/audit/audit.go b/audit/audit.go index 628d179..815fbe7 100644 --- a/audit/audit.go +++ b/audit/audit.go @@ -49,6 +49,8 @@ type Options struct { SkipQueryParams bool SkipformParams bool SkipRequestBody bool + // Option to not include raw request data with the log + SkipLogRawBody bool } // Audit entry. @@ -214,8 +216,14 @@ func (a *Audit) filterFields(r *http.Request, opts Options) { if bodyIsJSON && len(a.RequestBody) > 0 { err := json.Unmarshal(a.RequestBody, &body) if err != nil { - opts.Auditor.Log(r, errors.Wrapf(err, "could not parse body for %s %s: %s", - r.Method, r.URL.String(), stringutil.Left(string(a.RequestBody), 4000))) + msgFmt := "could not parse body for %s %s" + msgArgs := []interface{}{r.Method, r.URL.String()} + + if !opts.SkipLogRawBody { + msgFmt += ": %s" + msgArgs = append(msgArgs, stringutil.Left(string(a.RequestBody), 4000)) + } + opts.Auditor.Log(r, errors.Wrapf(err, msgFmt, msgArgs...)) } }