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

Try to improve error handling on share creating #3223

Merged
merged 1 commit into from
Sep 12, 2022
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
7 changes: 7 additions & 0 deletions changelog/unreleased/improve-create-share-err.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Enhancement: Improve CreateShare grpc error reporting

The errorcode returned by the share provider when creating a share where the sharee
is already the owner of the shared target is a bit more explicit now. Also debug logging
was added for this.

https://github.com/cs3org/reva/pull/3223
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (s *service) CreateShare(ctx context.Context, req *collaboration.CreateShar
share, err := s.sm.Share(ctx, req.ResourceInfo, req.Grant)
if err != nil {
return &collaboration.CreateShareResponse{
Status: status.NewInternal(ctx, "error creating share"),
Status: status.NewStatusFromErrType(ctx, "error creating share", err),
}, nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1410,6 +1410,7 @@ func (h *Handler) getResourceInfo(ctx context.Context, client gateway.GatewayAPI
}

func (h *Handler) createCs3Share(ctx context.Context, w http.ResponseWriter, r *http.Request, client gateway.GatewayAPIClient, req *collaboration.CreateShareRequest) (*collaboration.Share, *ocsError) {
logger := appctx.GetLogger(ctx)
exists, err := h.granteeExists(ctx, req.Grant.Grantee, req.ResourceInfo.Id)
if err != nil {
return nil, &ocsError{
Expand Down Expand Up @@ -1438,17 +1439,26 @@ func (h *Handler) createCs3Share(ctx context.Context, w http.ResponseWriter, r *
}
}
if createShareResponse.Status.Code != rpc.Code_CODE_OK {
if createShareResponse.Status.Code == rpc.Code_CODE_NOT_FOUND {
logger.Debug().Interface("Code", createShareResponse.Status.Code).Str("message", createShareResponse.Status.Message).Msg("grpc create share request failed")
switch createShareResponse.Status.Code {
case rpc.Code_CODE_NOT_FOUND:
return nil, &ocsError{
Code: response.MetaNotFound.StatusCode,
Message: "not found",
Error: nil,
}
}
return nil, &ocsError{
Code: response.MetaServerError.StatusCode,
Message: "grpc create share request failed",
Error: nil,
case rpc.Code_CODE_INVALID_ARGUMENT:
return nil, &ocsError{
Code: response.MetaBadRequest.StatusCode,
Message: createShareResponse.Status.Message,
Error: nil,
}
default:
return nil, &ocsError{
Code: response.MetaServerError.StatusCode,
Message: "grpc create share request failed",
Error: nil,
}
}
}
return createShareResponse.Share, nil
Expand Down
5 changes: 5 additions & 0 deletions pkg/share/manager/cs3/cs3.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,11 @@ func (m *Manager) Share(ctx context.Context, md *provider.ResourceInfo, g *colla
return nil, err
}
user := ctxpkg.ContextMustGetUser(ctx)
// do not allow share to myself or the owner if share is for a user
if g.Grantee.Type == provider.GranteeType_GRANTEE_TYPE_USER &&
(utils.UserEqual(g.Grantee.GetUserId(), user.Id) || utils.UserEqual(g.Grantee.GetUserId(), md.Owner)) {
return nil, errtypes.BadRequest("cs3: owner/creator and grantee are the same")
}
ts := utils.TSNow()

share := &collaboration.Share{
Expand Down
2 changes: 1 addition & 1 deletion pkg/share/manager/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ func (m *mgr) Share(ctx context.Context, md *provider.ResourceInfo, g *collabora
// TODO(labkode): should not this be caught already at the gw level?
if g.Grantee.Type == provider.GranteeType_GRANTEE_TYPE_USER &&
(utils.UserEqual(g.Grantee.GetUserId(), user.Id) || utils.UserEqual(g.Grantee.GetUserId(), md.Owner)) {
return nil, errors.New("json: owner/creator and grantee are the same")
return nil, errtypes.BadRequest("json: owner/creator and grantee are the same")
}

// check if share already exists.
Expand Down
2 changes: 1 addition & 1 deletion pkg/share/manager/jsoncs3/jsoncs3.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func (m *Manager) Share(ctx context.Context, md *provider.ResourceInfo, g *colla
// TODO: should this not already be caught at the gw level?
if g.Grantee.Type == provider.GranteeType_GRANTEE_TYPE_USER &&
(utils.UserEqual(g.Grantee.GetUserId(), user.Id) || utils.UserEqual(g.Grantee.GetUserId(), md.Owner)) {
return nil, errors.New("json: owner/creator and grantee are the same")
return nil, errtypes.BadRequest("jsoncs3: owner/creator and grantee are the same")
}

// check if share already exists.
Expand Down
3 changes: 1 addition & 2 deletions pkg/share/manager/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package memory

import (
"context"
"errors"
"fmt"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -83,7 +82,7 @@ func (m *manager) Share(ctx context.Context, md *provider.ResourceInfo, g *colla

if g.Grantee.Type == provider.GranteeType_GRANTEE_TYPE_USER &&
(utils.UserEqual(g.Grantee.GetUserId(), user.Id) || utils.UserEqual(g.Grantee.GetUserId(), md.Owner)) {
return nil, errors.New("memory: owner/creator and grantee are the same")
return nil, errtypes.BadRequest("memory: owner/creator and grantee are the same")
}

// check if share already exists.
Expand Down
2 changes: 1 addition & 1 deletion pkg/share/manager/owncloudsql/owncloudsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (m *mgr) Share(ctx context.Context, md *provider.ResourceInfo, g *collabora
// TODO(labkode): should not this be caught already at the gw level?
if g.Grantee.Type == provider.GranteeType_GRANTEE_TYPE_USER &&
(utils.UserEqual(g.Grantee.GetUserId(), user.Id) || utils.UserEqual(g.Grantee.GetUserId(), md.Owner)) {
return nil, errors.New("owncloudsql: owner/creator and grantee are the same")
return nil, errtypes.BadRequest("owncloudsql: owner/creator and grantee are the same")
}

// check if share already exists.
Expand Down