Skip to content

Commit

Permalink
ocm: simplified error handling (#4810)
Browse files Browse the repository at this point in the history
  • Loading branch information
glpatcern authored Sep 10, 2024
1 parent 5114d33 commit 122da0b
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 27 deletions.
5 changes: 5 additions & 0 deletions changelog/unreleased/ocm-error-handling.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Bugfix: simplified error handling

Minor rewording and simplification, following cs3org/OCM-API#90 and cs3org/OCM-API#91

https://github.com/cs3org/reva/pull/4810
10 changes: 3 additions & 7 deletions internal/grpc/services/ocminvitemanager/ocminvitemanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,7 @@ func (s *service) ForwardInvite(ctx context.Context, req *invitepb.ForwardInvite
switch {
case errors.Is(err, ocmd.ErrTokenInvalid):
return &invitepb.ForwardInviteResponse{
Status: status.NewInvalid(ctx, "token not valid"),
}, nil
case errors.Is(err, ocmd.ErrTokenNotFound):
return &invitepb.ForwardInviteResponse{
Status: status.NewNotFound(ctx, "token not found"),
Status: status.NewInvalid(ctx, "token invalid or not found"),
}, nil
case errors.Is(err, ocmd.ErrUserAlreadyAccepted):
return &invitepb.ForwardInviteResponse{
Expand Down Expand Up @@ -240,7 +236,7 @@ func (s *service) AcceptInvite(ctx context.Context, req *invitepb.AcceptInviteRe
if err != nil {
if errors.Is(err, invite.ErrTokenNotFound) {
return &invitepb.AcceptInviteResponse{
Status: status.NewNotFound(ctx, "token not found"),
Status: status.NewInvalid(ctx, "token invalid or not found"),
}, nil
}
return &invitepb.AcceptInviteResponse{
Expand All @@ -250,7 +246,7 @@ func (s *service) AcceptInvite(ctx context.Context, req *invitepb.AcceptInviteRe

if !isTokenValid(token) {
return &invitepb.AcceptInviteResponse{
Status: status.NewInvalid(ctx, "token is not valid"),
Status: status.NewInvalid(ctx, "token invalid or not found"),
}, nil
}

Expand Down
9 changes: 4 additions & 5 deletions internal/http/services/experimental/sciencemesh/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,16 +178,15 @@ func (h *tokenHandler) AcceptInvite(w http.ResponseWriter, r *http.Request) {
if forwardInviteResponse.Status.Code != rpc.Code_CODE_OK {
switch forwardInviteResponse.Status.Code {
case rpc.Code_CODE_NOT_FOUND:
reqres.WriteError(w, r, reqres.APIErrorNotFound, "token not found", nil)
return
fallthrough
case rpc.Code_CODE_INVALID_ARGUMENT:
reqres.WriteError(w, r, reqres.APIErrorInvalidParameter, "token has expired", nil)
reqres.WriteError(w, r, reqres.APIErrorInvalidParameter, "invalid or non existing token", nil)
return
case rpc.Code_CODE_ALREADY_EXISTS:
reqres.WriteError(w, r, reqres.APIErrorAlreadyExist, "user already known", nil)
reqres.WriteError(w, r, reqres.APIErrorAlreadyExist, "invitation already accepted", nil)
return
case rpc.Code_CODE_PERMISSION_DENIED:
reqres.WriteError(w, r, reqres.APIErrorUnauthenticated, "remove service not trusted", nil)
reqres.WriteError(w, r, reqres.APIErrorUnauthenticated, "remote service not trusted", nil)
return
default:
reqres.WriteError(w, r, reqres.APIErrorServerError, "unexpected error: "+forwardInviteResponse.Status.Message, errors.New(forwardInviteResponse.Status.Message))
Expand Down
14 changes: 4 additions & 10 deletions internal/http/services/opencloudmesh/ocmd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,16 @@ import (
)

// ErrTokenInvalid is the error returned by the invite-accepted
// endpoint when the token is not valid.
var ErrTokenInvalid = errors.New("the invitation token is invalid")
// endpoint when the token is not valid or not existing.
var ErrTokenInvalid = errors.New("the invitation token is invalid or not found")

// ErrServiceNotTrusted is the error returned by the invite-accepted
// endpoint when the service is not trusted to accept invitations.
var ErrServiceNotTrusted = errors.New("service is not trusted to accept invitations")

// ErrUserAlreadyAccepted is the error returned by the invite-accepted
// endpoint when a user is already know by the remote cloud.
var ErrUserAlreadyAccepted = errors.New("user already accepted an invitation token")

// ErrTokenNotFound is the error returned by the invite-accepted
// endpoint when the request is done using a not existing token.
var ErrTokenNotFound = errors.New("token not found")
// endpoint when a token was already used by a user in the remote cloud.
var ErrUserAlreadyAccepted = errors.New("invitation already accepted")

// ErrInvalidParameters is the error returned by the shares endpoint
// when the request does not contain required properties.
Expand Down Expand Up @@ -250,8 +246,6 @@ func (c *OCMClient) parseInviteAcceptedResponse(r *http.Response) (*User, error)
return &u, nil
case http.StatusBadRequest:
return nil, ErrTokenInvalid
case http.StatusNotFound:
return nil, ErrTokenNotFound
case http.StatusConflict:
return nil, ErrUserAlreadyAccepted
case http.StatusForbidden:
Expand Down
9 changes: 7 additions & 2 deletions internal/http/services/opencloudmesh/ocmd/shares.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,15 @@ func getResourceTypeFromOCMRequest(t string) providerpb.ResourceType {
}

func getOCMShareType(t string) ocm.ShareType {
if t == "user" {
switch t {
case "user":
return ocm.ShareType_SHARE_TYPE_USER
case "group":
return ocm.ShareType_SHARE_TYPE_GROUP
default:
// for now assume user share if not provided
return ocm.ShareType_SHARE_TYPE_USER
}
return ocm.ShareType_SHARE_TYPE_GROUP
}

func getAndResolveProtocols(p Protocols, r *http.Request) ([]*ocm.Protocol, error) {
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/grpc/ocm_invitation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ var _ = Describe("ocm invitation workflow", func() {
})
})

Describe("marie accept a not existing token", func() {
Describe("marie accept a non existing token", func() {
var cleanup func()
BeforeEach(func() {
variables, cleanup, err = initData(driver, nil, nil)
Expand All @@ -276,12 +276,12 @@ var _ = Describe("ocm invitation workflow", func() {
It("will not complete the invitation workflow", func() {
forwardRes, err := cesnetgw.ForwardInvite(ctxMarie, &invitepb.ForwardInviteRequest{
InviteToken: &invitepb.InviteToken{
Token: "not-existing-token",
Token: "non-existing-token",
},
OriginSystemProvider: cernbox,
})
Expect(err).ToNot(HaveOccurred())
Expect(forwardRes.Status.Code).To(Equal(rpc.Code_CODE_NOT_FOUND))
Expect(forwardRes.Status.Code).To(Equal(rpc.Code_CODE_INVALID_ARGUMENT))
})
})

Expand Down
File renamed without changes.

0 comments on commit 122da0b

Please sign in to comment.