Update Watchtower to use grpc-gateway v2 (#204)

* Migration to grpc gateway v2.
This commit is contained in:
Todd Knight authored and GitHub committed 2020-07-23 11:26:15 -07:00
1 parent 6de1f7ee16
commit f47046f158
67 files changed
+2281 -2070

No files matched your search

+19 -2
View File
@@ -9,7 +9,7 @@ import (
"strings"
"time"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/hashicorp/vault/internalshared/configutil"
"github.com/hashicorp/vault/sdk/helper/strutil"
"github.com/hashicorp/watchtower/api"
@@ -25,6 +25,7 @@ import (
"github.com/hashicorp/watchtower/internal/servers/controller/handlers/roles"
"github.com/hashicorp/watchtower/internal/servers/controller/handlers/scopes"
"github.com/hashicorp/watchtower/internal/servers/controller/handlers/users"
"google.golang.org/protobuf/encoding/protojson"
)
type HandlerProperties struct {
@@ -55,7 +56,23 @@ func handleGrpcGateway(c *Controller) (http.Handler, error) {
// Register*ServiceHandlerServer methods ignore the passed in ctx. Using the baseContext now just in case this changes
// in the future, at which point we'll want to be using the baseContext.
ctx := c.baseContext
mux := runtime.NewServeMux(runtime.WithProtoErrorHandler(handlers.ErrorHandler(c.logger)))
mux := runtime.NewServeMux(
runtime.WithMarshalerOption(runtime.MIMEWildcard, &runtime.HTTPBodyMarshaler{
Marshaler: &runtime.JSONPb{
MarshalOptions: protojson.MarshalOptions{
// Ensures the json marshaler uses the snake casing as defined in the proto field names.
UseProtoNames: true,
// Do not add fields set to zero value to json.
EmitUnpopulated: false,
},
UnmarshalOptions: protojson.UnmarshalOptions{
// Allows requests to contain unknown fields.
DiscardUnknown: true,
},
},
}),
runtime.WithErrorHandler(handlers.ErrorHandler(c.logger)),
)
hcs, err := host_catalogs.NewService(c.StaticHostRepoFn)
if err != nil {
return nil, fmt.Errorf("failed to create host catalog handler service: %w", err)
@@ -7,7 +7,7 @@ import (
"net/http"
"sort"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/hashicorp/go-hclog"
pb "github.com/hashicorp/watchtower/internal/gen/controller/api"
"google.golang.org/genproto/googleapis/rpc/errdetails"
@@ -68,10 +68,10 @@ func statusErrorToApiError(s *status.Status) *pb.Error {
}
// TODO(ICU-194): Remove all information from internal errors.
func ErrorHandler(logger hclog.Logger) runtime.ProtoErrorHandlerFunc {
func ErrorHandler(logger hclog.Logger) runtime.ErrorHandlerFunc {
const errorFallback = `{"error": "failed to marshal error message"}`
return func(ctx context.Context, _ *runtime.ServeMux, marshaler runtime.Marshaler, w http.ResponseWriter, r *http.Request, inErr error) {
if inErr == runtime.ErrUnknownURI {
if inErr == runtime.ErrNotMatch {
// grpc gateway uses this error when the path was not matched, but the error uses codes.Unimplemented which doesn't match the intention.
// Overwrite the error to match our expected behavior.
inErr = status.Error(codes.NotFound, http.StatusText(http.StatusNotFound))
@@ -91,7 +91,7 @@ func ErrorHandler(logger hclog.Logger) runtime.ProtoErrorHandlerFunc {
return
}
w.Header().Set("Content-Type", marshaler.ContentType())
w.Header().Set("Content-Type", marshaler.ContentType(apiErr))
w.WriteHeader(int(apiErr.GetStatus()))
if _, err := w.Write(buf); err != nil {
logger.Error("failed to send response chunk", "error", err)
@@ -9,7 +9,7 @@ import (
"testing"
"github.com/google/go-cmp/cmp"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/hashicorp/go-hclog"
pb "github.com/hashicorp/watchtower/internal/gen/controller/api"
"github.com/stretchr/testify/assert"
@@ -69,7 +69,7 @@ func TestApiErrorHandler(t *testing.T) {
},
{
name: "GrpcGateway Routing Error",
err: runtime.ErrUnknownURI,
err: runtime.ErrNotMatch,
expected: &pb.Error{
Status: http.StatusNotFound,
Code: "NotFound",