From 36253668bf8ee5784cf1aefba051db9418a14686 Mon Sep 17 00:00:00 2001 From: Willy Kloucek Date: Thu, 7 Oct 2021 12:59:11 +0200 Subject: [PATCH] map GRPC error codes to REVA errors --- changelog/unreleased/map-grpc-error-codes.md | 5 ++++ pkg/rgrpc/status/status.go | 24 ++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 changelog/unreleased/map-grpc-error-codes.md diff --git a/changelog/unreleased/map-grpc-error-codes.md b/changelog/unreleased/map-grpc-error-codes.md new file mode 100644 index 0000000000..cc1fe2e20c --- /dev/null +++ b/changelog/unreleased/map-grpc-error-codes.md @@ -0,0 +1,5 @@ +Bugfix: Map GRPC error codes to REVA errors + +We've fixed the error return behaviour in the gateway which would return GRPC error codes from the auth middleware. Now it returns REVA errors which other parts of REVA are also able to understand. + +https://github.com/cs3org/reva/pull/2140 diff --git a/pkg/rgrpc/status/status.go b/pkg/rgrpc/status/status.go index 57f9076ba3..8881ba984c 100644 --- a/pkg/rgrpc/status/status.go +++ b/pkg/rgrpc/status/status.go @@ -29,6 +29,8 @@ import ( "github.com/cs3org/reva/pkg/appctx" "github.com/cs3org/reva/pkg/errtypes" "go.opentelemetry.io/otel/trace" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" ) // NewOK returns a Status with CODE_OK. @@ -169,6 +171,28 @@ func NewStatusFromErrType(ctx context.Context, msg string, err error) *rpc.Statu case errtypes.BadRequest: return NewInvalidArg(ctx, "gateway: "+msg+":"+err.Error()) } + + // map GRPC status codes coming from the auth middleware + grpcErr := err + for { + st, ok := status.FromError(grpcErr) + if ok { + switch st.Code() { + case codes.NotFound: + return NewNotFound(ctx, "gateway: "+msg+": "+err.Error()) + case codes.Unauthenticated: + return NewUnauthenticated(ctx, err, "gateway: "+msg+": "+err.Error()) + case codes.PermissionDenied: + return NewPermissionDenied(ctx, err, "gateway: "+msg+": "+err.Error()) + } + } + // the actual error can be wrapped multiple times + grpcErr = errors.Unwrap(grpcErr) + if grpcErr == nil { + break + } + } + return NewInternal(ctx, err, "gateway: "+msg+":"+err.Error()) }