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

Ensure stray shares get ignored #1064

Merged
merged 21 commits into from
Aug 12, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
94 changes: 50 additions & 44 deletions internal/grpc/services/gateway/storageprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,13 @@ func (s *svc) getHome(ctx context.Context) string {
return "/home"
}
func (s *svc) InitiateFileDownload(ctx context.Context, req *provider.InitiateFileDownloadRequest) (*gateway.InitiateFileDownloadResponse, error) {
p, err := s.getPath(ctx, req.Ref)
if err != nil {
log := appctx.GetLogger(ctx)
p, st := s.getPath(ctx, req.Ref)
if st.Code != rpc.Code_CODE_OK {
log.Error().Str("rpc_code", st.Code.String()).
Msgf("error initiating file download id: %v", req.Ref.GetId())
return &gateway.InitiateFileDownloadResponse{
Status: status.NewInternal(ctx, err, "gateway: error gettng path for ref"),
Status: st,
}, nil
}

Expand All @@ -133,7 +136,7 @@ func (s *svc) InitiateFileDownload(ctx context.Context, req *provider.InitiateFi
return s.initiateFileDownload(ctx, req)
}

log := appctx.GetLogger(ctx)

if s.isSharedFolder(ctx, p) || s.isShareName(ctx, p) {
log.Debug().Msgf("path:%s points to shared folder or share name", p)
err := errtypes.PermissionDenied("gateway: cannot download share folder or share name: path=" + p)
Expand Down Expand Up @@ -254,18 +257,20 @@ func (s *svc) initiateFileDownload(ctx context.Context, req *provider.InitiateFi
}

func (s *svc) InitiateFileUpload(ctx context.Context, req *provider.InitiateFileUploadRequest) (*gateway.InitiateFileUploadResponse, error) {
p, err := s.getPath(ctx, req.Ref)
if err != nil {
log := appctx.GetLogger(ctx)
p, st := s.getPath(ctx, req.Ref)
if st.Code != rpc.Code_CODE_OK {
log.Error().Str("rpc_code", st.Code.String()).
Msgf("error initiating file upload id: %v", req.Ref.GetId())
return &gateway.InitiateFileUploadResponse{
Status: status.NewInternal(ctx, err, "gateway: error gettng path for ref"),
Status: st,
}, nil
}

if !s.inSharedFolder(ctx, p) {
return s.initiateFileUpload(ctx, req)
}

log := appctx.GetLogger(ctx)
if s.isSharedFolder(ctx, p) || s.isShareName(ctx, p) {
log.Debug().Msgf("path:%s points to shared folder or share name", p)
err := errtypes.PermissionDenied("gateway: cannot upload to share folder or share name: path=" + p)
Expand Down Expand Up @@ -422,18 +427,20 @@ func (s *svc) GetPath(ctx context.Context, req *provider.GetPathRequest) (*provi
}

func (s *svc) CreateContainer(ctx context.Context, req *provider.CreateContainerRequest) (*provider.CreateContainerResponse, error) {
p, err := s.getPath(ctx, req.Ref)
if err != nil {
log := appctx.GetLogger(ctx)
p, st := s.getPath(ctx, req.Ref)
if st.Code != rpc.Code_CODE_OK {
log.Error().Str("rpc_code", st.Code.String()).
Msgf("error creating container on reference id: %v", req.Ref.GetId())
return &provider.CreateContainerResponse{
Status: status.NewInternal(ctx, err, "gateway: error gettng path for ref"),
Status: st,
}, nil
}

if !s.inSharedFolder(ctx, p) {
return s.createContainer(ctx, req)
}

log := appctx.GetLogger(ctx)
if s.isSharedFolder(ctx, p) || s.isShareName(ctx, p) {
log.Debug().Msgf("path:%s points to shared folder or share name", p)
err := errtypes.PermissionDenied("gateway: cannot create container on share folder or share name: path=" + p)
Expand Down Expand Up @@ -527,18 +534,20 @@ func (s *svc) inSharedFolder(ctx context.Context, p string) bool {
}

func (s *svc) Delete(ctx context.Context, req *provider.DeleteRequest) (*provider.DeleteResponse, error) {
p, err := s.getPath(ctx, req.Ref)
if err != nil {
log := appctx.GetLogger(ctx)
p, st := s.getPath(ctx, req.Ref)
if st.Code != rpc.Code_CODE_OK {
refs marked this conversation as resolved.
Show resolved Hide resolved
log.Error().Str("rpc_code", st.Code.String()).
Msgf("error deleting reference id: %v", req.Ref.GetId())
return &provider.DeleteResponse{
Status: status.NewInternal(ctx, err, "gateway: error gettng path for ref"),
Status: st,
}, nil
}

if !s.inSharedFolder(ctx, p) {
return s.delete(ctx, req)
}

log := appctx.GetLogger(ctx)
if s.isSharedFolder(ctx, p) {
// TODO(labkode): deleting share names should be allowed, means unmounting.
log.Debug().Msgf("path:%s points to shared folder or share name", p)
Expand Down Expand Up @@ -643,20 +652,20 @@ func (s *svc) delete(ctx context.Context, req *provider.DeleteRequest) (*provide

func (s *svc) Move(ctx context.Context, req *provider.MoveRequest) (*provider.MoveResponse, error) {
log := appctx.GetLogger(ctx)

p, err := s.getPath(ctx, req.Source)
if err != nil {
log.Err(err).Msg("gateway: error moving")
p, st := s.getPath(ctx, req.Source)
if st.Code != rpc.Code_CODE_OK {
log.Error().Str("rpc_code", st.Code.String()).
Msgf("error moving reference id: %v to `%v`", req.Source.GetId(), req.Destination.String())
return &provider.MoveResponse{
Status: status.NewInternal(ctx, err, "gateway: error gettng path for ref"),
Status: st,
}, nil
}

dp, err := s.getPath(ctx, req.Destination)
if err != nil {
log.Err(err).Msg("gateway: error moving")
// log.Err(err).Msg("gateway: error moving")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

obsolete, or need to be re-enabled?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

obsolete

return &provider.MoveResponse{
Status: status.NewInternal(ctx, err, "gateway: error gettng path for ref"),
Status: st,
}, nil
}

Expand Down Expand Up @@ -844,10 +853,13 @@ func (s *svc) stat(ctx context.Context, req *provider.StatRequest) (*provider.St
}

func (s *svc) Stat(ctx context.Context, req *provider.StatRequest) (*provider.StatResponse, error) {
p, err := s.getPath(ctx, req.Ref, req.ArbitraryMetadataKeys...)
if err != nil {
log := appctx.GetLogger(ctx)
p, st := s.getPath(ctx, req.Ref, req.ArbitraryMetadataKeys...)
if st.Code != rpc.Code_CODE_OK {
log.Error().Str("rpc_code", st.Code.String()).
Msgf("error during STAT id: %v", req.Ref.GetId())
return &provider.StatResponse{
Status: status.NewInternal(ctx, err, "gateway: error getting path for ref"),
Status: st,
}, nil
}

Expand All @@ -860,8 +872,6 @@ func (s *svc) Stat(ctx context.Context, req *provider.StatRequest) (*provider.St
return s.stat(ctx, req)
}

log := appctx.GetLogger(ctx)

// we need to provide the info of the target, not the reference.
if s.isShareName(ctx, p) {
res, err := s.stat(ctx, req)
Expand Down Expand Up @@ -1080,10 +1090,13 @@ func (s *svc) listContainer(ctx context.Context, req *provider.ListContainerRequ
}

func (s *svc) ListContainer(ctx context.Context, req *provider.ListContainerRequest) (*provider.ListContainerResponse, error) {
p, err := s.getPath(ctx, req.Ref, req.ArbitraryMetadataKeys...)
if err != nil {
log := appctx.GetLogger(ctx)
p, st := s.getPath(ctx, req.Ref, req.ArbitraryMetadataKeys...)
if st.Code != rpc.Code_CODE_OK {
log.Error().Str("rpc_code", st.Code.String()).
Msgf("error listing directory id: %v", req.Ref.GetId())
return &provider.ListContainerResponse{
Status: status.NewInternal(ctx, err, "gateway: error getting path for ref"),
Status: st,
}, nil
}

Expand Down Expand Up @@ -1119,7 +1132,6 @@ func (s *svc) ListContainer(ctx context.Context, req *provider.ListContainerRequ
return lcr, nil
}

log := appctx.GetLogger(ctx)

// we need to provide the info of the target, not the reference.
if s.isShareName(ctx, p) {
Expand Down Expand Up @@ -1268,28 +1280,22 @@ func (s *svc) ListContainer(ctx context.Context, req *provider.ListContainerRequ
panic("gateway: stating an unknown path:" + p)
}

func (s *svc) getPath(ctx context.Context, ref *provider.Reference, keys ...string) (string, error) {
func (s *svc) getPath(ctx context.Context, ref *provider.Reference, keys ...string) (string, *rpc.Status) {
if ref.GetPath() != "" {
return ref.GetPath(), nil
return ref.GetPath(), &rpc.Status{Code: rpc.Code_CODE_OK}
}

if ref.GetId() != nil && ref.GetId().GetOpaqueId() != "" {
req := &provider.StatRequest{Ref: ref, ArbitraryMetadataKeys: keys}
res, err := s.stat(ctx, req)
if err != nil {
err = errors.Wrap(err, "gateway: error stating ref:"+ref.String())
return "", err
}

if res.Status.Code != rpc.Code_CODE_OK {
err := status.NewErrorFromCode(res.Status.Code, "gateway")
return "", err
if res.Status.Code != rpc.Code_CODE_OK || err != nil {
return "", res.Status
}

return res.Info.Path, nil
return res.Info.Path, res.Status
}

return "", errors.New("gateway: ref is invalid:" + ref.String())
return "", &rpc.Status{Code: rpc.Code_CODE_INTERNAL}
}

// /home/MyShares/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1295,7 +1295,13 @@ func (h *Handler) listUserShares(r *http.Request, filters []*collaboration.ListS
}

if statResponse.Status.Code != rpc.Code_CODE_OK {
return nil, errors.New("could not stat share target")
if statResponse.Status.Code == rpc.Code_CODE_NOT_FOUND {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also add this in listPublicShares?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

totally. I'd get hands on it tomorrow morning first thing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually giving this a second thought, as this PR has been messy enough with the testing I would like to create a new one for these changes and keep changes as "atomic" as possible. This PR is blocking us from finishing http://github.com/owncloud/ocis/pull/409 and the earlier we merge it the better 🙄

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh okay. No worries then. I'll merge it

// TODO share target was not found, we should not error here.
// return nil, errors.New(fmt.Sprintf("could not stat share target: %v, code: %v", s.ResourceId, statResponse.Status))
continue
}

return nil, errors.New(fmt.Sprintf("could not stat share target: %v, code: %v", s.ResourceId, statResponse.Status))
}

err = h.addFileInfo(ctx, share, statResponse.Info)
Expand Down