Add stack trace to audit logging panic recovery (#18121)

This commit is contained in:
Tom Proctor 2022-11-30 17:59:05 +00:00 committed by GitHub
parent b03da5157e
commit 48987ce052
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 8 deletions

3
changelog/18121.txt Normal file
View File

@ -0,0 +1,3 @@
```release-note:improvement
audit: Include stack trace when audit logging recovers from a panic.
```

View File

@ -3,6 +3,7 @@ package vault
import (
"context"
"fmt"
"runtime/debug"
"sync"
"time"
@ -105,7 +106,7 @@ func (a *AuditBroker) LogRequest(ctx context.Context, in *logical.LogInput, head
defer func() {
if r := recover(); r != nil {
a.logger.Error("panic during logging", "request_path", in.Request.Path, "error", r)
a.logger.Error("panic during logging", "request_path", in.Request.Path, "error", r, "stacktrace", string(debug.Stack()))
retErr = multierror.Append(retErr, fmt.Errorf("panic generating audit log"))
}
@ -176,7 +177,7 @@ func (a *AuditBroker) LogResponse(ctx context.Context, in *logical.LogInput, hea
defer func() {
if r := recover(); r != nil {
a.logger.Error("panic during logging", "request_path", in.Request.Path, "error", r)
a.logger.Error("panic during logging", "request_path", in.Request.Path, "error", r, "stacktrace", string(debug.Stack()))
retErr = multierror.Append(retErr, fmt.Errorf("panic generating audit log"))
}

View File

@ -4,7 +4,7 @@ import (
"context"
"net/http"
"os"
"runtime"
"runtime/debug"
"sync/atomic"
"time"
@ -41,12 +41,8 @@ func (s *forwardedRequestRPCServer) ForwardRequest(ctx context.Context, freq *fo
runRequest := func() {
defer func() {
// Logic here comes mostly from the Go source code
if err := recover(); err != nil {
const size = 64 << 10
buf := make([]byte, size)
buf = buf[:runtime.Stack(buf, false)]
s.core.logger.Error("panic serving forwarded request", "path", req.URL.Path, "error", err, "stacktrace", string(buf))
s.core.logger.Error("panic serving forwarded request", "path", req.URL.Path, "error", err, "stacktrace", string(debug.Stack()))
}
}()
s.handler.ServeHTTP(w, req)