* refactor (controller): Use a grpc server for the grpc-gateway This starts a grpc server for the grpc-gateway. The new gateway server uses an in-memory listener which also supports "dialing", so the http proxy can connect to the in-memory listener. This change allows us to use all grpc server features and was in part motivated by a need for an interceptor of inbound requests. With that said, having a "proper" grpc server for the gateway will provide a lot more flexibility and capabilities moving forward. The request context will still be verified via middleware in the http proxy (see wrapHandlerWithCommonFuncs). This verified context will be serialized into a protobuf and sent along to the grpc-gateway via metadata where an interceptor will add the verified request info to the request's context (see requestCtxInterceptor). The interceptor will verify the serialized request info via a shared ticket with it's companion in-memory http proxy. FYI: the ticket is simply random base62 string (see db.NewPrivateId). This change also necessitated a minor change to services. We need to return an empty response instead of nil, nil when there are no errors and the response is empty. * refactor (handlers): Rename gRPC Gateway filter Originally, this was called an OutgoingInterceptor, which is an unfortunate name since it's not an interceptor. Now that we're refactoring the gateway stuff and have proper interceptors, renaming this should remove ambiguity * refactor: Rework how 204 with empty msg is handled Recent changes to grpc gateway has necessitated a change to how 204s are handled by the gateway * tests (bats): Check status codes for delete operations
251 lines
6.5 KiB
Go
251 lines
6.5 KiB
Go
package handlers
|
|
|
|
import (
|
|
"context"
|
|
stderrors "errors"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
|
|
"github.com/hashicorp/boundary/internal/errors"
|
|
pb "github.com/hashicorp/boundary/internal/gen/controller/api"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
"google.golang.org/protobuf/testing/protocmp"
|
|
)
|
|
|
|
func TestApiErrorHandler(t *testing.T) {
|
|
ctx := context.Background()
|
|
req, err := http.NewRequest("GET", "madeup/for/the/test", nil)
|
|
require.NoError(t, err)
|
|
mux := runtime.NewServeMux()
|
|
inMarsh, outMarsh := runtime.MarshalerForRequest(mux, req)
|
|
|
|
tested := ErrorHandler()
|
|
|
|
testCases := []struct {
|
|
name string
|
|
err error
|
|
expected ApiError
|
|
}{
|
|
{
|
|
name: "Not Found",
|
|
err: NotFoundErrorf("Test"),
|
|
expected: ApiError{
|
|
Status: http.StatusNotFound,
|
|
Inner: &pb.Error{
|
|
Kind: "NotFound",
|
|
Message: "Test",
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "Invalid Fields",
|
|
err: InvalidArgumentErrorf("Test", map[string]string{
|
|
"k1": "v1",
|
|
"k2": "v2",
|
|
}),
|
|
expected: ApiError{
|
|
Status: http.StatusBadRequest,
|
|
Inner: &pb.Error{
|
|
Kind: "InvalidArgument",
|
|
Message: "Test",
|
|
Details: &pb.ErrorDetails{
|
|
RequestFields: []*pb.FieldError{
|
|
{
|
|
Name: "k1",
|
|
Description: "v1",
|
|
},
|
|
{
|
|
Name: "k2",
|
|
Description: "v2",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "GrpcGateway Routing Error",
|
|
err: runtime.ErrNotMatch,
|
|
expected: ApiError{
|
|
Status: http.StatusNotFound,
|
|
Inner: &pb.Error{
|
|
Kind: "NotFound",
|
|
Message: http.StatusText(http.StatusNotFound),
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "Unimplemented error",
|
|
err: status.Error(codes.Unimplemented, "Test"),
|
|
expected: ApiError{
|
|
Status: http.StatusMethodNotAllowed,
|
|
Inner: &pb.Error{
|
|
Kind: "Unimplemented",
|
|
Message: "Test",
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "Unknown error",
|
|
err: stderrors.New("Some random error"),
|
|
expected: ApiError{
|
|
Status: http.StatusInternalServerError,
|
|
Inner: &pb.Error{
|
|
Kind: "Internal",
|
|
Message: "Some random error",
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "Domain error Db invalid public id",
|
|
err: errors.E(ctx, errors.WithCode(errors.InvalidPublicId)),
|
|
expected: ApiError{
|
|
Status: http.StatusInternalServerError,
|
|
Inner: &pb.Error{
|
|
Kind: "Internal",
|
|
Message: "invalid public id, parameter violation: error #102",
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "Domain error Db invalid parameter",
|
|
err: errors.E(ctx, errors.WithCode(errors.InvalidParameter)),
|
|
expected: ApiError{
|
|
Status: http.StatusInternalServerError,
|
|
Inner: &pb.Error{
|
|
Kind: "Internal",
|
|
Message: "invalid parameter, parameter violation: error #100",
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "Domain error Db invalid field mask",
|
|
err: errors.E(ctx, errors.WithCode(errors.InvalidFieldMask)),
|
|
expected: ApiError{
|
|
Status: http.StatusBadRequest,
|
|
Inner: &pb.Error{
|
|
Kind: "InvalidArgument",
|
|
Message: "Error in provided request",
|
|
Details: &pb.ErrorDetails{RequestFields: []*pb.FieldError{{Name: "update_mask", Description: "Invalid update mask provided."}}},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "Domain error Db empty field mask",
|
|
err: errors.E(ctx, errors.WithCode(errors.EmptyFieldMask)),
|
|
expected: ApiError{
|
|
Status: http.StatusBadRequest,
|
|
Inner: &pb.Error{
|
|
Kind: "InvalidArgument",
|
|
Message: "Error in provided request",
|
|
Details: &pb.ErrorDetails{RequestFields: []*pb.FieldError{{Name: "update_mask", Description: "Invalid update mask provided."}}},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "Domain error Db not unqiue",
|
|
err: errors.E(ctx, errors.WithCode(errors.NotUnique)),
|
|
expected: ApiError{
|
|
Status: http.StatusBadRequest,
|
|
Inner: &pb.Error{
|
|
Kind: "InvalidArgument",
|
|
Message: genericUniquenessMsg,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "Domain error Db record not found",
|
|
err: errors.E(ctx, errors.WithCode(errors.RecordNotFound)),
|
|
expected: ApiError{
|
|
Status: http.StatusNotFound,
|
|
Inner: &pb.Error{
|
|
Kind: "NotFound",
|
|
Message: genericNotFoundMsg,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "Domain error Db multiple records",
|
|
err: errors.E(ctx, errors.WithCode(errors.MultipleRecords)),
|
|
expected: ApiError{
|
|
Status: http.StatusInternalServerError,
|
|
Inner: &pb.Error{
|
|
Kind: "Internal",
|
|
Message: "multiple records, search issue: error #1101",
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "Domain error account already associated",
|
|
err: errors.E(ctx, errors.WithCode(errors.AccountAlreadyAssociated)),
|
|
expected: ApiError{
|
|
Status: http.StatusBadRequest,
|
|
Inner: &pb.Error{
|
|
Kind: "InvalidArgument",
|
|
Message: "account already associated with another user, parameter violation: error #114",
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "Wrapped domain error",
|
|
err: errors.E(ctx, errors.WithCode(errors.InvalidAddress), errors.WithMsg("test msg"), errors.WithWrap(errors.E(ctx, errors.WithCode(errors.NotNull), errors.WithMsg("inner msg")))),
|
|
expected: ApiError{
|
|
Status: http.StatusInternalServerError,
|
|
Inner: &pb.Error{
|
|
Kind: "Internal",
|
|
Message: "test msg: parameter violation: error #101: inner msg: integrity violation: error #1001",
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "Forbidden domain error",
|
|
err: errors.E(ctx, errors.WithCode(errors.Forbidden), errors.WithMsg("test msg")),
|
|
expected: ApiError{
|
|
Status: http.StatusForbidden,
|
|
Inner: &pb.Error{
|
|
Kind: "Internal",
|
|
Message: "test msg: unknown: error #403",
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "Wrapped forbidden domain error",
|
|
err: fmt.Errorf("got error: %w", errors.E(ctx, errors.WithCode(errors.Forbidden), errors.WithMsg("test msg"))),
|
|
expected: ApiError{
|
|
Status: http.StatusForbidden,
|
|
Inner: &pb.Error{
|
|
Kind: "Internal",
|
|
Message: "got error: test msg: unknown: error #403",
|
|
},
|
|
},
|
|
},
|
|
}
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
assert, require := assert.New(t), require.New(t)
|
|
w := httptest.NewRecorder()
|
|
tested(ctx, mux, outMarsh, w, req, tc.err)
|
|
resp := w.Result()
|
|
assert.EqualValues(tc.expected.Status, resp.StatusCode)
|
|
|
|
got, err := ioutil.ReadAll(resp.Body)
|
|
require.NoError(err)
|
|
|
|
gotErr := &pb.Error{}
|
|
err = inMarsh.Unmarshal(got, gotErr)
|
|
require.NoError(err)
|
|
|
|
assert.Equal(tc.expected.Status, int32(resp.StatusCode))
|
|
assert.Empty(cmp.Diff(tc.expected.Inner, gotErr, protocmp.Transform()))
|
|
})
|
|
}
|
|
}
|