Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error logical.ErrUpstreamRateLimited and return 502 from RespondCommonError #5216

Merged
merged 4 commits into from
Sep 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions logical/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ var (
// ErrMultiAuthzPending is returned if the the request needs more
// authorizations
ErrMultiAuthzPending = errors.New("request needs further approval")

// ErrUpstreamRateLimited is returned when Vault recieves a rate limited
// response from an upstream
ErrUpstreamRateLimited = errors.New("upstream rate limited")
)

type HTTPCodedError interface {
Expand Down
2 changes: 2 additions & 0 deletions logical/response_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ func RespondErrorCommon(req *Request, resp *Response, err error) (int, error) {
statusCode = http.StatusNotFound
case errwrap.Contains(err, ErrInvalidRequest.Error()):
statusCode = http.StatusBadRequest
case errwrap.Contains(err, ErrUpstreamRateLimited.Error()):
statusCode = http.StatusBadGateway
}
}

Expand Down
83 changes: 83 additions & 0 deletions logical/response_util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package logical

import (
"strings"
"testing"
)

func TestResponseUtil_RespondErrorCommon_basic(t *testing.T) {
testCases := []struct {
title string
req *Request
resp *Response
respErr error
expectedStatus int
expectedErr error
}{
{
title: "Throttled, no error",
respErr: ErrUpstreamRateLimited,
resp: &Response{},
expectedStatus: 502,
},
{
title: "Throttled, with error",
respErr: ErrUpstreamRateLimited,
resp: &Response{
Data: map[string]interface{}{
"error": "rate limited",
},
},
expectedStatus: 502,
},
{
title: "Read not found",
req: &Request{
Operation: ReadOperation,
},
respErr: nil,
expectedStatus: 404,
},
{
title: "List with response and no keys",
req: &Request{
Operation: ListOperation,
},
resp: &Response{},
respErr: nil,
expectedStatus: 404,
},
{
title: "List with response and keys",
req: &Request{
Operation: ListOperation,
},
resp: &Response{
Data: map[string]interface{}{
"keys": []string{"some", "things", "here"},
},
},
respErr: nil,
expectedStatus: 0,
},
}

for _, tc := range testCases {
t.Run(tc.title, func(t *testing.T) {
var status int
var err, respErr error
if tc.respErr != nil {
respErr = tc.respErr
}
status, err = RespondErrorCommon(tc.req, tc.resp, respErr)
if status != tc.expectedStatus {
t.Fatalf("Expected (%d) status code, got (%d)", tc.expectedStatus, status)
}
if tc.expectedErr != nil {
if !strings.Contains(tc.expectedErr.Error(), err.Error()) {
t.Fatalf("Expected error to contain:\n%s\n\ngot:\n%s\n", tc.expectedErr, err)
}
}
})
}
}