Skip to content

Commit

Permalink
Merge pull request #3072 from hashicorp/req-forwarding-recover
Browse files Browse the repository at this point in the history
Recover from panics during request forwarding
  • Loading branch information
chrishoffman authored Jul 28, 2017
2 parents 6c6f132 + 0ac923d commit b5cabc2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
4 changes: 2 additions & 2 deletions builtin/logical/ssh/path_sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,10 +391,10 @@ func (b *backend) calculateTTL(data *framework.FieldData, role *sshRole) (time.D
func (b *creationBundle) sign() (retCert *ssh.Certificate, retErr error) {
defer func() {
if r := recover(); r != nil {
err, ok := r.(error)
errMsg, ok := r.(string)
if ok {
retCert = nil
retErr = err
retErr = errors.New(errMsg)
}
}
}()
Expand Down
20 changes: 16 additions & 4 deletions vault/request_forwarding.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net"
"net/http"
"net/url"
"runtime"
"sync"
"sync/atomic"
"time"
Expand Down Expand Up @@ -352,12 +353,23 @@ func (s *forwardedRequestRPCServer) ForwardRequest(ctx context.Context, freq *fo
// meets the interface requirements.
w := forwarding.NewRPCResponseWriter()

s.handler.ServeHTTP(w, req)
resp := &forwarding.Response{}

resp := &forwarding.Response{
StatusCode: uint32(w.StatusCode()),
Body: w.Body().Bytes(),
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("forwarding: panic serving request", "path", req.URL.Path, "error", err, "stacktrace", buf)
}
}()
s.handler.ServeHTTP(w, req)
}
runRequest()
resp.StatusCode = uint32(w.StatusCode())
resp.Body = w.Body().Bytes()

header := w.Header()
if header != nil {
Expand Down

0 comments on commit b5cabc2

Please sign in to comment.