diff --git a/changelog/unreleased/dav-unit-tests.md b/changelog/unreleased/dav-unit-tests.md index c7d7a5ad3f..c6e4f9c1a4 100644 --- a/changelog/unreleased/dav-unit-tests.md +++ b/changelog/unreleased/dav-unit-tests.md @@ -3,4 +3,5 @@ Enhancement: Cover ocdav with more unit tests We added unit tests to cover more ocdav handlers: - delete -https://github.com/cs3org/reva/pull/3441 \ No newline at end of file +https://github.com/cs3org/reva/pull/3441 +https://github.com/cs3org/reva/pull/3443 \ No newline at end of file diff --git a/internal/http/services/owncloud/ocdav/copy.go b/internal/http/services/owncloud/ocdav/copy.go index 90a3b88a5e..41cb2030ba 100644 --- a/internal/http/services/owncloud/ocdav/copy.go +++ b/internal/http/services/owncloud/ocdav/copy.go @@ -90,14 +90,7 @@ func (s *svc) handlePathCopy(w http.ResponseWriter, r *http.Request, ns string) sublog := appctx.GetLogger(ctx).With().Str("src", src).Str("dst", dst).Logger() - client, err := s.getClient() - if err != nil { - sublog.Error().Err(err).Msg("error getting grpc client") - w.WriteHeader(http.StatusInternalServerError) - return - } - - srcSpace, status, err := spacelookup.LookUpStorageSpaceForPath(ctx, client, src) + srcSpace, status, err := spacelookup.LookUpStorageSpaceForPath(ctx, s.gwClient, src) if err != nil { sublog.Error().Err(err).Str("path", src).Msg("failed to look up storage space") w.WriteHeader(http.StatusInternalServerError) @@ -107,7 +100,7 @@ func (s *svc) handlePathCopy(w http.ResponseWriter, r *http.Request, ns string) errors.HandleErrorStatus(&sublog, w, status) return } - dstSpace, status, err := spacelookup.LookUpStorageSpaceForPath(ctx, client, dst) + dstSpace, status, err := spacelookup.LookUpStorageSpaceForPath(ctx, s.gwClient, dst) if err != nil { sublog.Error().Err(err).Str("path", dst).Msg("failed to look up storage space") w.WriteHeader(http.StatusInternalServerError) @@ -123,7 +116,7 @@ func (s *svc) handlePathCopy(w http.ResponseWriter, r *http.Request, ns string) return } - if err := s.executePathCopy(ctx, client, w, r, cp); err != nil { + if err := s.executePathCopy(ctx, s.gwClient, w, r, cp); err != nil { sublog.Error().Err(err).Str("depth", cp.depth.String()).Msg("error executing path copy") w.WriteHeader(http.StatusInternalServerError) } @@ -304,13 +297,6 @@ func (s *svc) handleSpacesCopy(w http.ResponseWriter, r *http.Request, spaceID s sublog := appctx.GetLogger(ctx).With().Str("spaceid", spaceID).Str("path", r.URL.Path).Str("destination", dst).Logger() - client, err := s.getClient() - if err != nil { - sublog.Error().Err(err).Msg("error getting grpc client") - w.WriteHeader(http.StatusInternalServerError) - return - } - srcRef, err := spacelookup.MakeStorageSpaceReference(spaceID, r.URL.Path) if err != nil { w.WriteHeader(http.StatusBadRequest) @@ -330,7 +316,7 @@ func (s *svc) handleSpacesCopy(w http.ResponseWriter, r *http.Request, spaceID s return } - err = s.executeSpacesCopy(ctx, w, client, cp) + err = s.executeSpacesCopy(ctx, w, s.gwClient, cp) if err != nil { sublog.Error().Err(err).Str("depth", cp.depth.String()).Msg("error descending directory") w.WriteHeader(http.StatusInternalServerError) @@ -487,14 +473,7 @@ func (s *svc) executeSpacesCopy(ctx context.Context, w http.ResponseWriter, clie } func (s *svc) prepareCopy(ctx context.Context, w http.ResponseWriter, r *http.Request, srcRef, dstRef *provider.Reference, log *zerolog.Logger) *copy { - client, err := s.getClient() - if err != nil { - log.Error().Err(err).Msg("error getting grpc client") - w.WriteHeader(http.StatusInternalServerError) - return nil - } - - isChild, err := s.referenceIsChildOf(ctx, client, dstRef, srcRef) + isChild, err := s.referenceIsChildOf(ctx, s.gwClient, dstRef, srcRef) if err != nil { switch err.(type) { case errtypes.IsNotSupported: @@ -540,7 +519,7 @@ func (s *svc) prepareCopy(ctx context.Context, w http.ResponseWriter, r *http.Re log.Debug().Bool("overwrite", overwrite).Str("depth", depth.String()).Msg("copy") srcStatReq := &provider.StatRequest{Ref: srcRef} - srcStatRes, err := client.Stat(ctx, srcStatReq) + srcStatRes, err := s.gwClient.Stat(ctx, srcStatReq) switch { case err != nil: log.Error().Err(err).Msg("error sending grpc stat request") @@ -558,7 +537,7 @@ func (s *svc) prepareCopy(ctx context.Context, w http.ResponseWriter, r *http.Re } dstStatReq := &provider.StatRequest{Ref: dstRef} - dstStatRes, err := client.Stat(ctx, dstStatReq) + dstStatRes, err := s.gwClient.Stat(ctx, dstStatReq) switch { case err != nil: log.Error().Err(err).Msg("error sending grpc stat request") @@ -584,7 +563,7 @@ func (s *svc) prepareCopy(ctx context.Context, w http.ResponseWriter, r *http.Re // delete existing tree delReq := &provider.DeleteRequest{Ref: dstRef} - delRes, err := client.Delete(ctx, delReq) + delRes, err := s.gwClient.Delete(ctx, delReq) if err != nil { log.Error().Err(err).Msg("error sending grpc delete request") w.WriteHeader(http.StatusInternalServerError) @@ -602,7 +581,7 @@ func (s *svc) prepareCopy(ctx context.Context, w http.ResponseWriter, r *http.Re Path: utils.MakeRelativePath(p), } intStatReq := &provider.StatRequest{Ref: pRef} - intStatRes, err := client.Stat(ctx, intStatReq) + intStatRes, err := s.gwClient.Stat(ctx, intStatReq) if err != nil { log.Error().Err(err).Msg("error sending grpc stat request") w.WriteHeader(http.StatusInternalServerError) diff --git a/internal/http/services/owncloud/ocdav/dav.go b/internal/http/services/owncloud/ocdav/dav.go index 9b50b22dc4..2795346529 100644 --- a/internal/http/services/owncloud/ocdav/dav.go +++ b/internal/http/services/owncloud/ocdav/dav.go @@ -181,18 +181,14 @@ func (h *DavHandler) Handler(s *svc) http.Handler { case "public-files": base := path.Join(ctx.Value(net.CtxKeyBaseURI).(string), "public-files") ctx = context.WithValue(ctx, net.CtxKeyBaseURI, base) - c, err := s.getClient() - if err != nil { - w.WriteHeader(http.StatusNotFound) - return - } var res *gatewayv1beta1.AuthenticateResponse token, _ := router.ShiftPath(r.URL.Path) var hasValidBasicAuthHeader bool var pass string + var err error if _, pass, hasValidBasicAuthHeader = r.BasicAuth(); hasValidBasicAuthHeader { - res, err = handleBasicAuth(r.Context(), c, token, pass) + res, err = handleBasicAuth(r.Context(), s.gwClient, token, pass) } else { q := r.URL.Query() sig := q.Get("signature") @@ -202,7 +198,7 @@ func (h *DavHandler) Handler(s *svc) http.Handler { w.WriteHeader(http.StatusUnauthorized) return } - res, err = handleSignatureAuth(r.Context(), c, token, sig, expiration) + res, err = handleSignatureAuth(r.Context(), s.gwClient, token, sig, expiration) } switch { @@ -236,7 +232,7 @@ func (h *DavHandler) Handler(s *svc) http.Handler { r = r.WithContext(ctx) // the public share manager knew the token, but does the referenced target still exist? - sRes, err := getTokenStatInfo(ctx, c, token) + sRes, err := getTokenStatInfo(ctx, s.gwClient, token) switch { case err != nil: log.Error().Err(err).Msg("error sending grpc stat request") diff --git a/internal/http/services/owncloud/ocdav/delete.go b/internal/http/services/owncloud/ocdav/delete.go index 8478cf8150..223a557898 100644 --- a/internal/http/services/owncloud/ocdav/delete.go +++ b/internal/http/services/owncloud/ocdav/delete.go @@ -40,13 +40,7 @@ func (s *svc) handlePathDelete(w http.ResponseWriter, r *http.Request, ns string fn := path.Join(ns, r.URL.Path) - client, err := s.getClient() - if err != nil { - span.RecordError(err) - return http.StatusInternalServerError, err - } - - space, rpcStatus, err := spacelookup.LookUpStorageSpaceForPath(ctx, client, fn) + space, rpcStatus, err := spacelookup.LookUpStorageSpaceForPath(ctx, s.gwClient, fn) switch { case err != nil: span.RecordError(err) @@ -74,13 +68,7 @@ func (s *svc) handleDelete(ctx context.Context, w http.ResponseWriter, r *http.R return http.StatusBadRequest, errtypes.BadRequest("invalid if header") } - client, err := s.getClient() - if err != nil { - span.RecordError(err) - return http.StatusInternalServerError, err - } - - res, err := client.Delete(ctx, req) + res, err := s.gwClient.Delete(ctx, req) switch { case err != nil: span.RecordError(err) @@ -98,7 +86,7 @@ func (s *svc) handleDelete(ctx context.Context, w http.ResponseWriter, r *http.R status = http.StatusLocked } // check if user has access to resource - sRes, err := client.Stat(ctx, &provider.StatRequest{Ref: ref}) + sRes, err := s.gwClient.Stat(ctx, &provider.StatRequest{Ref: ref}) if err != nil { span.RecordError(err) return http.StatusInternalServerError, err diff --git a/internal/http/services/owncloud/ocdav/get.go b/internal/http/services/owncloud/ocdav/get.go index 2075c15c81..eb523b36f3 100644 --- a/internal/http/services/owncloud/ocdav/get.go +++ b/internal/http/services/owncloud/ocdav/get.go @@ -44,13 +44,7 @@ func (s *svc) handlePathGet(w http.ResponseWriter, r *http.Request, ns string) { sublog := appctx.GetLogger(ctx).With().Str("path", fn).Str("svc", "ocdav").Str("handler", "get").Logger() - client, err := s.getClient() - if err != nil { - sublog.Error().Err(err).Msg("error getting grpc client") - w.WriteHeader(http.StatusInternalServerError) - return - } - space, status, err := spacelookup.LookUpStorageSpaceForPath(ctx, client, fn) + space, status, err := spacelookup.LookUpStorageSpaceForPath(ctx, s.gwClient, fn) if err != nil { sublog.Error().Err(err).Str("path", fn).Msg("failed to look up storage space") w.WriteHeader(http.StatusInternalServerError) @@ -65,17 +59,10 @@ func (s *svc) handlePathGet(w http.ResponseWriter, r *http.Request, ns string) { } func (s *svc) handleGet(ctx context.Context, w http.ResponseWriter, r *http.Request, ref *provider.Reference, dlProtocol string, log zerolog.Logger) { - client, err := s.getClient() - if err != nil { - log.Error().Err(err).Msg("error getting grpc client") - w.WriteHeader(http.StatusInternalServerError) - return - } - sReq := &provider.StatRequest{ Ref: ref, } - sRes, err := client.Stat(ctx, sReq) + sRes, err := s.gwClient.Stat(ctx, sReq) if err != nil { log.Error().Err(err).Msg("error stat resource") w.WriteHeader(http.StatusInternalServerError) @@ -92,7 +79,7 @@ func (s *svc) handleGet(ctx context.Context, w http.ResponseWriter, r *http.Requ } dReq := &provider.InitiateFileDownloadRequest{Ref: ref} - dRes, err := client.InitiateFileDownload(ctx, dReq) + dRes, err := s.gwClient.InitiateFileDownload(ctx, dReq) switch { case err != nil: log.Error().Err(err).Msg("error initiating file download") diff --git a/internal/http/services/owncloud/ocdav/head.go b/internal/http/services/owncloud/ocdav/head.go index 297901d43e..9021b38857 100644 --- a/internal/http/services/owncloud/ocdav/head.go +++ b/internal/http/services/owncloud/ocdav/head.go @@ -47,14 +47,8 @@ func (s *svc) handlePathHead(w http.ResponseWriter, r *http.Request, ns string) fn := path.Join(ns, r.URL.Path) sublog := appctx.GetLogger(ctx).With().Str("path", fn).Logger() - client, err := s.getClient() - if err != nil { - sublog.Error().Err(err).Msg("error getting grpc client") - w.WriteHeader(http.StatusInternalServerError) - return - } - space, status, err := spacelookup.LookUpStorageSpaceForPath(ctx, client, fn) + space, status, err := spacelookup.LookUpStorageSpaceForPath(ctx, s.gwClient, fn) if err != nil { sublog.Error().Err(err).Str("path", fn).Msg("failed to look up storage space") w.WriteHeader(http.StatusInternalServerError) @@ -69,15 +63,9 @@ func (s *svc) handlePathHead(w http.ResponseWriter, r *http.Request, ns string) } func (s *svc) handleHead(ctx context.Context, w http.ResponseWriter, r *http.Request, ref *provider.Reference, log zerolog.Logger) { - client, err := s.getClient() - if err != nil { - log.Error().Err(err).Msg("error getting grpc client") - w.WriteHeader(http.StatusInternalServerError) - return - } req := &provider.StatRequest{Ref: ref} - res, err := client.Stat(ctx, req) + res, err := s.gwClient.Stat(ctx, req) if err != nil { log.Error().Err(err).Msg("error sending grpc stat request") w.WriteHeader(http.StatusInternalServerError) diff --git a/internal/http/services/owncloud/ocdav/locks.go b/internal/http/services/owncloud/ocdav/locks.go index 1dc93d66e4..02ddafe9f2 100644 --- a/internal/http/services/owncloud/ocdav/locks.go +++ b/internal/http/services/owncloud/ocdav/locks.go @@ -387,13 +387,8 @@ func (s *svc) handleLock(w http.ResponseWriter, r *http.Request, ns string) (ret fn := path.Join(ns, r.URL.Path) // TODO do we still need to jail if we query the registry about the spaces? - client, err := s.getClient() - if err != nil { - return http.StatusInternalServerError, err - } - // TODO instead of using a string namespace ns pass in the space with the request? - ref, cs3Status, err := spacelookup.LookupReferenceForPath(ctx, client, fn) + ref, cs3Status, err := spacelookup.LookupReferenceForPath(ctx, s.gwClient, fn) if err != nil { return http.StatusInternalServerError, err } @@ -570,13 +565,8 @@ func (s *svc) handleUnlock(w http.ResponseWriter, r *http.Request, ns string) (s fn := path.Join(ns, r.URL.Path) // TODO do we still need to jail if we query the registry about the spaces? - client, err := s.getClient() - if err != nil { - return http.StatusInternalServerError, err - } - // TODO instead of using a string namespace ns pass in the space with the request? - ref, cs3Status, err := spacelookup.LookupReferenceForPath(ctx, client, fn) + ref, cs3Status, err := spacelookup.LookupReferenceForPath(ctx, s.gwClient, fn) if err != nil { return http.StatusInternalServerError, err } diff --git a/internal/http/services/owncloud/ocdav/meta.go b/internal/http/services/owncloud/ocdav/meta.go index 4d286ff34c..a345ffbd8b 100644 --- a/internal/http/services/owncloud/ocdav/meta.go +++ b/internal/http/services/owncloud/ocdav/meta.go @@ -96,12 +96,6 @@ func (h *MetaHandler) handlePathForUser(w http.ResponseWriter, r *http.Request, id := storagespace.FormatResourceID(*rid) sublog := appctx.GetLogger(ctx).With().Str("path", r.URL.Path).Str("resourceid", id).Logger() sublog.Info().Msg("calling get path for user") - client, err := s.getClient() - if err != nil { - sublog.Error().Err(err).Msg("error getting grpc client") - w.WriteHeader(http.StatusInternalServerError) - return - } pf, status, err := propfind.ReadPropfind(r.Body) if err != nil { @@ -117,7 +111,7 @@ func (h *MetaHandler) handlePathForUser(w http.ResponseWriter, r *http.Request, } pathReq := &provider.GetPathRequest{ResourceId: rid} - pathRes, err := client.GetPath(ctx, pathReq) + pathRes, err := s.gwClient.GetPath(ctx, pathReq) if err != nil { sublog.Error().Err(err).Msg("could not send GetPath grpc request: transport error") w.WriteHeader(http.StatusInternalServerError) diff --git a/internal/http/services/owncloud/ocdav/mkcol.go b/internal/http/services/owncloud/ocdav/mkcol.go index e6a318b4e9..27c6736816 100644 --- a/internal/http/services/owncloud/ocdav/mkcol.go +++ b/internal/http/services/owncloud/ocdav/mkcol.go @@ -45,14 +45,10 @@ func (s *svc) handlePathMkcol(w http.ResponseWriter, r *http.Request, ns string) } } sublog := appctx.GetLogger(ctx).With().Str("path", fn).Logger() - client, err := s.getClient() - if err != nil { - return http.StatusInternalServerError, err - } // stat requested path to make sure it isn't existing yet // NOTE: It could be on another storage provider than the 'parent' of it - sr, err := client.Stat(ctx, &provider.StatRequest{ + sr, err := s.gwClient.Stat(ctx, &provider.StatRequest{ Ref: &provider.Reference{ Path: fn, }, @@ -72,7 +68,7 @@ func (s *svc) handlePathMkcol(w http.ResponseWriter, r *http.Request, ns string) parentPath := path.Dir(fn) - space, rpcStatus, err := spacelookup.LookUpStorageSpaceForPath(ctx, client, parentPath) + space, rpcStatus, err := spacelookup.LookUpStorageSpaceForPath(ctx, s.gwClient, parentPath) switch { case err != nil: return http.StatusInternalServerError, err @@ -113,13 +109,8 @@ func (s *svc) handleMkcol(ctx context.Context, w http.ResponseWriter, r *http.Re return http.StatusUnsupportedMediaType, fmt.Errorf("extended-mkcol not supported") } - client, err := s.getClient() - if err != nil { - return http.StatusInternalServerError, err - } - req := &provider.CreateContainerRequest{Ref: childRef} - res, err := client.CreateContainer(ctx, req) + res, err := s.gwClient.CreateContainer(ctx, req) switch { case err != nil: return http.StatusInternalServerError, err @@ -128,7 +119,7 @@ func (s *svc) handleMkcol(ctx context.Context, w http.ResponseWriter, r *http.Re return 0, nil case res.Status.Code == rpc.Code_CODE_PERMISSION_DENIED: // check if user has access to parent - sRes, err := client.Stat(ctx, &provider.StatRequest{Ref: &provider.Reference{ + sRes, err := s.gwClient.Stat(ctx, &provider.StatRequest{Ref: &provider.Reference{ ResourceId: childRef.GetResourceId(), Path: utils.MakeRelativePath(path.Dir(childRef.Path)), }}) diff --git a/internal/http/services/owncloud/ocdav/move.go b/internal/http/services/owncloud/ocdav/move.go index e9f1fff40d..b333a44f5e 100644 --- a/internal/http/services/owncloud/ocdav/move.go +++ b/internal/http/services/owncloud/ocdav/move.go @@ -66,14 +66,8 @@ func (s *svc) handlePathMove(w http.ResponseWriter, r *http.Request, ns string) dstPath = path.Join(ns, dstPath) sublog := appctx.GetLogger(ctx).With().Str("src", srcPath).Str("dst", dstPath).Logger() - client, err := s.getClient() - if err != nil { - sublog.Error().Err(err).Msg("error getting grpc client") - w.WriteHeader(http.StatusInternalServerError) - return - } - srcSpace, status, err := spacelookup.LookUpStorageSpaceForPath(ctx, client, srcPath) + srcSpace, status, err := spacelookup.LookUpStorageSpaceForPath(ctx, s.gwClient, srcPath) if err != nil { sublog.Error().Err(err).Str("path", srcPath).Msg("failed to look up source storage space") w.WriteHeader(http.StatusInternalServerError) @@ -83,7 +77,7 @@ func (s *svc) handlePathMove(w http.ResponseWriter, r *http.Request, ns string) errors.HandleErrorStatus(&sublog, w, status) return } - dstSpace, status, err := spacelookup.LookUpStorageSpaceForPath(ctx, client, dstPath) + dstSpace, status, err := spacelookup.LookUpStorageSpaceForPath(ctx, s.gwClient, dstPath) if err != nil { sublog.Error().Err(err).Str("path", dstPath).Msg("failed to look up destination storage space") w.WriteHeader(http.StatusInternalServerError) @@ -134,14 +128,7 @@ func (s *svc) handleSpacesMove(w http.ResponseWriter, r *http.Request, srcSpaceI } func (s *svc) handleMove(ctx context.Context, w http.ResponseWriter, r *http.Request, src, dst *provider.Reference, log zerolog.Logger) { - client, err := s.getClient() - if err != nil { - log.Error().Err(err).Msg("error getting grpc client") - w.WriteHeader(http.StatusInternalServerError) - return - } - - isChild, err := s.referenceIsChildOf(ctx, client, dst, src) + isChild, err := s.referenceIsChildOf(ctx, s.gwClient, dst, src) if err != nil { switch err.(type) { case errtypes.IsNotSupported: @@ -171,7 +158,7 @@ func (s *svc) handleMove(ctx context.Context, w http.ResponseWriter, r *http.Req // check src exists srcStatReq := &provider.StatRequest{Ref: src} - srcStatRes, err := client.Stat(ctx, srcStatReq) + srcStatRes, err := s.gwClient.Stat(ctx, srcStatReq) if err != nil { log.Error().Err(err).Msg("error sending grpc stat request") w.WriteHeader(http.StatusInternalServerError) @@ -190,9 +177,9 @@ func (s *svc) handleMove(ctx context.Context, w http.ResponseWriter, r *http.Req // check dst exists dstStatReq := &provider.StatRequest{Ref: dst} - dstStatRes, err := client.Stat(ctx, dstStatReq) + dstStatRes, err := s.gwClient.Stat(ctx, dstStatReq) if err != nil { - log.Error().Err(err).Msg("error getting grpc client") + log.Error().Err(err).Msg("error sending grpc stat request") w.WriteHeader(http.StatusInternalServerError) return } @@ -213,7 +200,7 @@ func (s *svc) handleMove(ctx context.Context, w http.ResponseWriter, r *http.Req // delete existing tree delReq := &provider.DeleteRequest{Ref: dst} - delRes, err := client.Delete(ctx, delReq) + delRes, err := s.gwClient.Delete(ctx, delReq) if err != nil { log.Error().Err(err).Msg("error sending grpc delete request") w.WriteHeader(http.StatusInternalServerError) @@ -230,7 +217,7 @@ func (s *svc) handleMove(ctx context.Context, w http.ResponseWriter, r *http.Req ResourceId: dst.ResourceId, Path: utils.MakeRelativePath(path.Dir(dst.Path)), }} - intStatRes, err := client.Stat(ctx, intStatReq) + intStatRes, err := s.gwClient.Stat(ctx, intStatReq) if err != nil { log.Error().Err(err).Msg("error sending grpc stat request") w.WriteHeader(http.StatusInternalServerError) @@ -250,7 +237,7 @@ func (s *svc) handleMove(ctx context.Context, w http.ResponseWriter, r *http.Req } mReq := &provider.MoveRequest{Source: src, Destination: dst} - mRes, err := client.Move(ctx, mReq) + mRes, err := s.gwClient.Move(ctx, mReq) if err != nil { log.Error().Err(err).Msg("error sending move grpc request") w.WriteHeader(http.StatusInternalServerError) @@ -279,7 +266,7 @@ func (s *svc) handleMove(ctx context.Context, w http.ResponseWriter, r *http.Req return } - dstStatRes, err = client.Stat(ctx, dstStatReq) + dstStatRes, err = s.gwClient.Stat(ctx, dstStatReq) if err != nil { log.Error().Err(err).Msg("error sending grpc stat request") w.WriteHeader(http.StatusInternalServerError) diff --git a/internal/http/services/owncloud/ocdav/ocdav.go b/internal/http/services/owncloud/ocdav/ocdav.go index 2f9803db45..bebc18070a 100644 --- a/internal/http/services/owncloud/ocdav/ocdav.go +++ b/internal/http/services/owncloud/ocdav/ocdav.go @@ -322,10 +322,6 @@ func (s *svc) Handler() http.Handler { }) } -func (s *svc) getClient() (gateway.GatewayAPIClient, error) { - return s.gwClient, nil -} - func (s *svc) ApplyLayout(ctx context.Context, ns string, useLoggedInUserNS bool, requestPath string) (string, string, error) { // If useLoggedInUserNS is false, that implies that the request is coming from // the FilesHandler method invoked by a /dav/files/fileOwner where fileOwner @@ -337,13 +333,8 @@ func (s *svc) ApplyLayout(ctx context.Context, ns string, useLoggedInUserNS bool var requestUsernameOrID string requestUsernameOrID, requestPath = router.ShiftPath(requestPath) - gatewayClient, err := s.getClient() - if err != nil { - return "", "", err - } - // Check if this is a Userid - userRes, err := gatewayClient.GetUser(ctx, &userpb.GetUserRequest{ + userRes, err := s.gwClient.GetUser(ctx, &userpb.GetUserRequest{ UserId: &userpb.UserId{OpaqueId: requestUsernameOrID}, }) if err != nil { @@ -352,7 +343,7 @@ func (s *svc) ApplyLayout(ctx context.Context, ns string, useLoggedInUserNS bool // If it's not a userid try if it is a user name if userRes.Status.Code != rpc.Code_CODE_OK { - res, err := gatewayClient.GetUserByClaim(ctx, &userpb.GetUserByClaimRequest{ + res, err := s.gwClient.GetUserByClaim(ctx, &userpb.GetUserByClaimRequest{ Claim: "username", Value: requestUsernameOrID, }) diff --git a/internal/http/services/owncloud/ocdav/proppatch.go b/internal/http/services/owncloud/ocdav/proppatch.go index 0714bf0a48..bfc5e16cd1 100644 --- a/internal/http/services/owncloud/ocdav/proppatch.go +++ b/internal/http/services/owncloud/ocdav/proppatch.go @@ -55,12 +55,7 @@ func (s *svc) handlePathProppatch(w http.ResponseWriter, r *http.Request, ns str return status, err } - c, err := s.getClient() - if err != nil { - return http.StatusInternalServerError, err - } - - space, rpcStatus, err := spacelookup.LookUpStorageSpaceForPath(ctx, c, fn) + space, rpcStatus, err := spacelookup.LookUpStorageSpaceForPath(ctx, s.gwClient, fn) switch { case err != nil: return http.StatusInternalServerError, err @@ -71,7 +66,7 @@ func (s *svc) handlePathProppatch(w http.ResponseWriter, r *http.Request, ns str } // check if resource exists statReq := &provider.StatRequest{Ref: spacelookup.MakeRelativeReference(space, fn, false)} - statRes, err := c.Stat(ctx, statReq) + statRes, err := s.gwClient.Stat(ctx, statReq) switch { case err != nil: return http.StatusInternalServerError, err @@ -127,12 +122,6 @@ func (s *svc) handleSpacesProppatch(w http.ResponseWriter, r *http.Request, spac } func (s *svc) handleProppatch(ctx context.Context, w http.ResponseWriter, r *http.Request, ref *provider.Reference, patches []Proppatch, log zerolog.Logger) ([]xml.Name, []xml.Name, bool) { - c, err := s.getClient() - if err != nil { - log.Error().Err(err).Msg("error getting grpc client") - w.WriteHeader(http.StatusInternalServerError) - return nil, nil, false - } rreq := &provider.UnsetArbitraryMetadataRequest{ Ref: ref, @@ -172,7 +161,7 @@ func (s *svc) handleProppatch(ctx context.Context, w http.ResponseWriter, r *htt // FIXME: batch this somehow if remove { rreq.ArbitraryMetadataKeys[0] = key - res, err := c.UnsetArbitraryMetadata(ctx, rreq) + res, err := s.gwClient.UnsetArbitraryMetadata(ctx, rreq) if err != nil { log.Error().Err(err).Msg("error sending a grpc UnsetArbitraryMetadata request") w.WriteHeader(http.StatusInternalServerError) @@ -189,7 +178,7 @@ func (s *svc) handleProppatch(ctx context.Context, w http.ResponseWriter, r *htt m := res.Status.Message if res.Status.Code == rpc.Code_CODE_PERMISSION_DENIED { // check if user has access to resource - sRes, err := c.Stat(ctx, &provider.StatRequest{Ref: ref}) + sRes, err := s.gwClient.Stat(ctx, &provider.StatRequest{Ref: ref}) if err != nil { log.Error().Err(err).Msg("error performing stat grpc request") w.WriteHeader(http.StatusInternalServerError) @@ -211,7 +200,7 @@ func (s *svc) handleProppatch(ctx context.Context, w http.ResponseWriter, r *htt return nil, nil, false } if key == "http://owncloud.org/ns/favorite" { - statRes, err := c.Stat(ctx, &provider.StatRequest{Ref: ref}) + statRes, err := s.gwClient.Stat(ctx, &provider.StatRequest{Ref: ref}) if err != nil { w.WriteHeader(http.StatusInternalServerError) return nil, nil, false @@ -226,7 +215,7 @@ func (s *svc) handleProppatch(ctx context.Context, w http.ResponseWriter, r *htt removedProps = append(removedProps, propNameXML) } else { sreq.ArbitraryMetadata.Metadata[key] = value - res, err := c.SetArbitraryMetadata(ctx, sreq) + res, err := s.gwClient.SetArbitraryMetadata(ctx, sreq) if err != nil { log.Error().Err(err).Str("key", key).Str("value", value).Msg("error sending a grpc SetArbitraryMetadata request") w.WriteHeader(http.StatusInternalServerError) @@ -243,7 +232,7 @@ func (s *svc) handleProppatch(ctx context.Context, w http.ResponseWriter, r *htt m := res.Status.Message if res.Status.Code == rpc.Code_CODE_PERMISSION_DENIED { // check if user has access to resource - sRes, err := c.Stat(ctx, &provider.StatRequest{Ref: ref}) + sRes, err := s.gwClient.Stat(ctx, &provider.StatRequest{Ref: ref}) if err != nil { log.Error().Err(err).Msg("error performing stat grpc request") w.WriteHeader(http.StatusInternalServerError) @@ -269,7 +258,7 @@ func (s *svc) handleProppatch(ctx context.Context, w http.ResponseWriter, r *htt delete(sreq.ArbitraryMetadata.Metadata, key) if key == "http://owncloud.org/ns/favorite" { - statRes, err := c.Stat(ctx, &provider.StatRequest{Ref: ref}) + statRes, err := s.gwClient.Stat(ctx, &provider.StatRequest{Ref: ref}) if err != nil || statRes.Info == nil { w.WriteHeader(http.StatusInternalServerError) return nil, nil, false diff --git a/internal/http/services/owncloud/ocdav/put.go b/internal/http/services/owncloud/ocdav/put.go index b813f03389..4a94ad0a16 100644 --- a/internal/http/services/owncloud/ocdav/put.go +++ b/internal/http/services/owncloud/ocdav/put.go @@ -111,13 +111,7 @@ func (s *svc) handlePathPut(w http.ResponseWriter, r *http.Request, ns string) { fn := path.Join(ns, r.URL.Path) sublog := appctx.GetLogger(ctx).With().Str("path", fn).Logger() - client, err := s.getClient() - if err != nil { - sublog.Error().Err(err).Msg("error getting grpc client") - w.WriteHeader(http.StatusInternalServerError) - return - } - space, status, err := spacelookup.LookUpStorageSpaceForPath(ctx, client, fn) + space, status, err := spacelookup.LookUpStorageSpaceForPath(ctx, s.gwClient, fn) if err != nil { sublog.Error().Err(err).Str("path", fn).Msg("failed to look up storage space") w.WriteHeader(http.StatusInternalServerError) @@ -143,13 +137,6 @@ func (s *svc) handlePut(ctx context.Context, w http.ResponseWriter, r *http.Requ return } - client, err := s.getClient() - if err != nil { - log.Error().Err(err).Msg("error getting grpc client") - w.WriteHeader(http.StatusInternalServerError) - return - } - opaqueMap := map[string]*typespb.OpaqueEntry{ net.HeaderUploadLength: { Decoder: "plain", @@ -206,7 +193,7 @@ func (s *svc) handlePut(ctx context.Context, w http.ResponseWriter, r *http.Requ } // where to upload the file? - uRes, err := client.InitiateFileUpload(ctx, uReq) + uRes, err := s.gwClient.InitiateFileUpload(ctx, uReq) if err != nil { log.Error().Err(err).Msg("error initiating file upload") w.WriteHeader(http.StatusInternalServerError) @@ -219,7 +206,7 @@ func (s *svc) handlePut(ctx context.Context, w http.ResponseWriter, r *http.Requ status := http.StatusForbidden m := uRes.Status.Message // check if user has access to parent - sRes, err := client.Stat(ctx, &provider.StatRequest{Ref: &provider.Reference{ + sRes, err := s.gwClient.Stat(ctx, &provider.StatRequest{Ref: &provider.Reference{ ResourceId: ref.ResourceId, Path: utils.MakeRelativePath(path.Dir(ref.Path)), }}) diff --git a/internal/http/services/owncloud/ocdav/report.go b/internal/http/services/owncloud/ocdav/report.go index 6ac2716cc0..3cea9b1f7a 100644 --- a/internal/http/services/owncloud/ocdav/report.go +++ b/internal/http/services/owncloud/ocdav/report.go @@ -64,14 +64,6 @@ func (s *svc) handleReport(w http.ResponseWriter, r *http.Request, ns string) { } func (s *svc) doSearchFiles(w http.ResponseWriter, r *http.Request, sf *reportSearchFiles) { - ctx := r.Context() - log := appctx.GetLogger(ctx) - _, err := s.getClient() - if err != nil { - log.Error().Err(err).Msg("error getting grpc client") - w.WriteHeader(http.StatusInternalServerError) - return - } w.WriteHeader(http.StatusNotImplemented) } @@ -89,16 +81,9 @@ func (s *svc) doFilterFiles(w http.ResponseWriter, r *http.Request, ff *reportFi return } - client, err := s.getClient() - if err != nil { - log.Error().Err(err).Msg("error getting gateway client") - w.WriteHeader(http.StatusInternalServerError) - return - } - infos := make([]*provider.ResourceInfo, 0, len(favorites)) for i := range favorites { - statRes, err := client.Stat(ctx, &providerv1beta1.StatRequest{Ref: &providerv1beta1.Reference{ResourceId: favorites[i]}}) + statRes, err := s.gwClient.Stat(ctx, &providerv1beta1.StatRequest{Ref: &providerv1beta1.Reference{ResourceId: favorites[i]}}) if err != nil { log.Error().Err(err).Msg("error getting resource info") continue diff --git a/internal/http/services/owncloud/ocdav/tpc.go b/internal/http/services/owncloud/ocdav/tpc.go index e48653fe87..1b138c1a8e 100644 --- a/internal/http/services/owncloud/ocdav/tpc.go +++ b/internal/http/services/owncloud/ocdav/tpc.go @@ -134,18 +134,10 @@ func (s *svc) handleTPCPull(ctx context.Context, w http.ResponseWriter, r *http. } sublog.Debug().Bool("overwrite", overwrite).Msg("TPC Pull") - // get Gateway client - client, err := s.getClient() - if err != nil { - sublog.Error().Err(err).Msg("error getting grpc client") - w.WriteHeader(http.StatusInternalServerError) - return - } - // check if destination exists ref := &provider.Reference{Path: dst} dstStatReq := &provider.StatRequest{Ref: ref} - dstStatRes, err := client.Stat(ctx, dstStatReq) + dstStatRes, err := s.gwClient.Stat(ctx, dstStatReq) if err != nil { sublog.Error().Err(err).Msg("error sending grpc stat request") w.WriteHeader(http.StatusInternalServerError) @@ -161,7 +153,7 @@ func (s *svc) handleTPCPull(ctx context.Context, w http.ResponseWriter, r *http. return } - err = s.performHTTPPull(ctx, client, r, w, ns) + err = s.performHTTPPull(ctx, s.gwClient, r, w, ns) if err != nil { sublog.Error().Err(err).Msg("error performing TPC Pull") return @@ -295,17 +287,9 @@ func (s *svc) handleTPCPush(ctx context.Context, w http.ResponseWriter, r *http. sublog.Debug().Bool("overwrite", overwrite).Msg("TPC Push") - // get Gateway client - client, err := s.getClient() - if err != nil { - sublog.Error().Err(err).Msg("error getting grpc client") - w.WriteHeader(http.StatusInternalServerError) - return - } - ref := &provider.Reference{Path: src} srcStatReq := &provider.StatRequest{Ref: ref} - srcStatRes, err := client.Stat(ctx, srcStatReq) + srcStatRes, err := s.gwClient.Stat(ctx, srcStatReq) if err != nil { sublog.Error().Err(err).Msg("error sending grpc stat request") w.WriteHeader(http.StatusInternalServerError) @@ -321,7 +305,7 @@ func (s *svc) handleTPCPush(ctx context.Context, w http.ResponseWriter, r *http. return } - err = s.performHTTPPush(ctx, client, r, w, srcStatRes.Info, ns) + err = s.performHTTPPush(ctx, s.gwClient, r, w, srcStatRes.Info, ns) if err != nil { sublog.Error().Err(err).Msg("error performing TPC Push") return diff --git a/internal/http/services/owncloud/ocdav/trashbin.go b/internal/http/services/owncloud/ocdav/trashbin.go index aa5d31968f..763a229f7c 100644 --- a/internal/http/services/owncloud/ocdav/trashbin.go +++ b/internal/http/services/owncloud/ocdav/trashbin.go @@ -110,15 +110,8 @@ func (h *TrashbinHandler) Handler(s *svc) http.Handler { } r.URL.Path = newPath - client, err := s.getClient() - if err != nil { - log.Error().Err(err).Msg("error getting grpc client") - w.WriteHeader(http.StatusInternalServerError) - return - } - basePath := path.Join(ns, newPath) - space, rpcstatus, err := spacelookup.LookUpStorageSpaceForPath(ctx, client, basePath) + space, rpcstatus, err := spacelookup.LookUpStorageSpaceForPath(ctx, s.gwClient, basePath) switch { case err != nil: log.Error().Err(err).Str("path", basePath).Msg("failed to look up storage space") @@ -158,7 +151,7 @@ func (h *TrashbinHandler) Handler(s *svc) http.Handler { p := path.Join(ns, dst) // The destination can be in another space. E.g. the 'Shares Jail'. - space, rpcstatus, err := spacelookup.LookUpStorageSpaceForPath(ctx, client, p) + space, rpcstatus, err := spacelookup.LookUpStorageSpaceForPath(ctx, s.gwClient, p) if err != nil { log.Error().Err(err).Str("path", p).Msg("failed to look up destination storage space") w.WriteHeader(http.StatusInternalServerError) @@ -223,15 +216,8 @@ func (h *TrashbinHandler) listTrashbin(w http.ResponseWriter, r *http.Request, s return } - client, err := s.getClient() - if err != nil { - sublog.Error().Err(err).Msg("error getting grpc client") - w.WriteHeader(http.StatusInternalServerError) - return - } - // ask gateway for recycle items - getRecycleRes, err := client.ListRecycle(ctx, &provider.ListRecycleRequest{Ref: ref, Key: path.Join(key, itemPath)}) + getRecycleRes, err := s.gwClient.ListRecycle(ctx, &provider.ListRecycleRequest{Ref: ref, Key: path.Join(key, itemPath)}) if err != nil { sublog.Error().Err(err).Msg("error calling ListRecycle") w.WriteHeader(http.StatusInternalServerError) @@ -261,7 +247,7 @@ func (h *TrashbinHandler) listTrashbin(w http.ResponseWriter, r *http.Request, s for len(stack) > 0 { key := stack[len(stack)-1] - getRecycleRes, err := client.ListRecycle(ctx, &provider.ListRecycleRequest{Ref: ref, Key: key}) + getRecycleRes, err := s.gwClient.ListRecycle(ctx, &provider.ListRecycleRequest{Ref: ref, Key: key}) if err != nil { sublog.Error().Err(err).Msg("error calling ListRecycle") w.WriteHeader(http.StatusInternalServerError) @@ -479,15 +465,8 @@ func (h *TrashbinHandler) restore(w http.ResponseWriter, r *http.Request, s *svc return } - client, err := s.getClient() - if err != nil { - sublog.Error().Err(err).Msg("error getting grpc client") - w.WriteHeader(http.StatusInternalServerError) - return - } - dstStatReq := &provider.StatRequest{Ref: dst} - dstStatRes, err := client.Stat(ctx, dstStatReq) + dstStatRes, err := s.gwClient.Stat(ctx, dstStatReq) if err != nil { sublog.Error().Err(err).Msg("error sending grpc stat request") w.WriteHeader(http.StatusInternalServerError) @@ -505,7 +484,7 @@ func (h *TrashbinHandler) restore(w http.ResponseWriter, r *http.Request, s *svc parentRef := &provider.Reference{ResourceId: dst.ResourceId, Path: utils.MakeRelativePath(path.Dir(dst.Path))} parentStatReq := &provider.StatRequest{Ref: parentRef} - parentStatResponse, err := client.Stat(ctx, parentStatReq) + parentStatResponse, err := s.gwClient.Stat(ctx, parentStatReq) if err != nil { sublog.Error().Err(err).Msg("error sending grpc stat request") w.WriteHeader(http.StatusInternalServerError) @@ -536,7 +515,7 @@ func (h *TrashbinHandler) restore(w http.ResponseWriter, r *http.Request, s *svc } // delete existing tree delReq := &provider.DeleteRequest{Ref: dst} - delRes, err := client.Delete(ctx, delReq) + delRes, err := s.gwClient.Delete(ctx, delReq) if err != nil { sublog.Error().Err(err).Msg("error sending grpc delete request") w.WriteHeader(http.StatusInternalServerError) @@ -555,7 +534,7 @@ func (h *TrashbinHandler) restore(w http.ResponseWriter, r *http.Request, s *svc RestoreRef: dst, } - res, err := client.RestoreRecycleItem(ctx, req) + res, err := s.gwClient.RestoreRecycleItem(ctx, req) if err != nil { sublog.Error().Err(err).Msg("error sending a grpc restore recycle item request") w.WriteHeader(http.StatusInternalServerError) @@ -572,7 +551,7 @@ func (h *TrashbinHandler) restore(w http.ResponseWriter, r *http.Request, s *svc return } - dstStatRes, err = client.Stat(ctx, dstStatReq) + dstStatRes, err = s.gwClient.Stat(ctx, dstStatReq) if err != nil { sublog.Error().Err(err).Msg("error sending grpc stat request") w.WriteHeader(http.StatusInternalServerError) @@ -599,20 +578,13 @@ func (h *TrashbinHandler) delete(w http.ResponseWriter, r *http.Request, s *svc, sublog := appctx.GetLogger(ctx).With().Interface("reference", ref).Str("key", key).Str("item_path", itemPath).Logger() - client, err := s.getClient() - if err != nil { - sublog.Error().Err(err).Msg("error getting grpc client") - w.WriteHeader(http.StatusInternalServerError) - return - } - trashPath := path.Join(key, itemPath) req := &provider.PurgeRecycleRequest{ Ref: ref, Key: trashPath, } - res, err := client.PurgeRecycle(ctx, req) + res, err := s.gwClient.PurgeRecycle(ctx, req) if err != nil { sublog.Error().Err(err).Msg("error sending a grpc restore recycle item request") w.WriteHeader(http.StatusInternalServerError) diff --git a/internal/http/services/owncloud/ocdav/tus.go b/internal/http/services/owncloud/ocdav/tus.go index 429c5870cc..9a3eb8c082 100644 --- a/internal/http/services/owncloud/ocdav/tus.go +++ b/internal/http/services/owncloud/ocdav/tus.go @@ -117,18 +117,10 @@ func (s *svc) handleTusPost(ctx context.Context, w http.ResponseWriter, r *http. // TODO check Expect: 100-continue - // check if destination exists or is a file - client, err := s.getClient() - if err != nil { - log.Error().Err(err).Msg("error getting grpc client") - w.WriteHeader(http.StatusInternalServerError) - return - } - sReq := &provider.StatRequest{ Ref: ref, } - sRes, err := client.Stat(ctx, sReq) + sRes, err := s.gwClient.Stat(ctx, sReq) if err != nil { log.Error().Err(err).Msg("error sending grpc stat request") w.WriteHeader(http.StatusInternalServerError) @@ -166,7 +158,7 @@ func (s *svc) handleTusPost(ctx context.Context, w http.ResponseWriter, r *http. return } if uploadLength == 0 { - tfRes, err := client.TouchFile(ctx, &provider.TouchFileRequest{ + tfRes, err := s.gwClient.TouchFile(ctx, &provider.TouchFileRequest{ Ref: ref, }) if err != nil { @@ -204,7 +196,7 @@ func (s *svc) handleTusPost(ctx context.Context, w http.ResponseWriter, r *http. }, } - uRes, err := client.InitiateFileUpload(ctx, uReq) + uRes, err := s.gwClient.InitiateFileUpload(ctx, uReq) if err != nil { log.Error().Err(err).Msg("error initiating file upload") w.WriteHeader(http.StatusInternalServerError) @@ -285,7 +277,7 @@ func (s *svc) handleTusPost(ctx context.Context, w http.ResponseWriter, r *http. if length == 0 || httpRes.Header.Get(net.HeaderUploadOffset) == r.Header.Get(net.HeaderUploadLength) { // get uploaded file metadata - sRes, err := client.Stat(ctx, sReq) + sRes, err := s.gwClient.Stat(ctx, sReq) if err != nil { log.Error().Err(err).Msg("error sending grpc stat request") w.WriteHeader(http.StatusInternalServerError) diff --git a/internal/http/services/owncloud/ocdav/versions.go b/internal/http/services/owncloud/ocdav/versions.go index 50f60502ee..fa47d2f8da 100644 --- a/internal/http/services/owncloud/ocdav/versions.go +++ b/internal/http/services/owncloud/ocdav/versions.go @@ -122,15 +122,8 @@ func (h *VersionsHandler) doListVersions(w http.ResponseWriter, r *http.Request, return } - client, err := s.getClient() - if err != nil { - sublog.Error().Err(err).Msg("error getting grpc client") - w.WriteHeader(http.StatusInternalServerError) - return - } - ref := &provider.Reference{ResourceId: rid} - res, err := client.Stat(ctx, &provider.StatRequest{Ref: ref}) + res, err := s.gwClient.Stat(ctx, &provider.StatRequest{Ref: ref}) if err != nil { sublog.Error().Err(err).Msg("error sending a grpc stat request") w.WriteHeader(http.StatusInternalServerError) @@ -149,7 +142,7 @@ func (h *VersionsHandler) doListVersions(w http.ResponseWriter, r *http.Request, info := res.Info - lvRes, err := client.ListFileVersions(ctx, &provider.ListFileVersionsRequest{Ref: ref}) + lvRes, err := s.gwClient.ListFileVersions(ctx, &provider.ListFileVersionsRequest{Ref: ref}) if err != nil { sublog.Error().Err(err).Msg("error sending list container grpc request") w.WriteHeader(http.StatusInternalServerError) @@ -221,19 +214,12 @@ func (h *VersionsHandler) doRestore(w http.ResponseWriter, r *http.Request, s *s sublog := appctx.GetLogger(ctx).With().Interface("resourceid", rid).Str("key", key).Logger() - client, err := s.getClient() - if err != nil { - sublog.Error().Err(err).Msg("error getting grpc client") - w.WriteHeader(http.StatusInternalServerError) - return - } - req := &provider.RestoreFileVersionRequest{ Ref: &provider.Reference{ResourceId: rid}, Key: key, } - res, err := client.RestoreFileVersion(ctx, req) + res, err := s.gwClient.RestoreFileVersion(ctx, req) if err != nil { sublog.Error().Err(err).Msg("error sending a grpc restore version request") w.WriteHeader(http.StatusInternalServerError)