diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index d4723e3829..f033eaac24 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -1,3 +1,4 @@ +- Felix Hillingshaeuser - Hugo Gonzalez Labrador - Jörn Friedrich Dreyer - Mohitty diff --git a/cmd/reva/ls.go b/cmd/reva/ls.go index ec3b10670a..9384a9160b 100644 --- a/cmd/reva/ls.go +++ b/cmd/reva/ls.go @@ -23,6 +23,7 @@ import ( "os" "path" + rpcpb "github.com/cs3org/go-cs3apis/cs3/rpc" storageproviderv0alphapb "github.com/cs3org/go-cs3apis/cs3/storageprovider/v0alpha" ) @@ -56,6 +57,10 @@ func lsCommand() *command { return err } + if res.Status.Code != rpcpb.Code_CODE_OK { + return formatError(res.Status) + } + infos := res.Infos for _, info := range infos { p := info.Path diff --git a/cmd/reva/share-list.go b/cmd/reva/share-list.go index aac7c5293f..0ffb447000 100644 --- a/cmd/reva/share-list.go +++ b/cmd/reva/share-list.go @@ -19,10 +19,13 @@ package main import ( + "fmt" "os" + "strings" "time" rpcpb "github.com/cs3org/go-cs3apis/cs3/rpc" + storageproviderv0alphapb "github.com/cs3org/go-cs3apis/cs3/storageprovider/v0alpha" usershareproviderv0alphapb "github.com/cs3org/go-cs3apis/cs3/usershareprovider/v0alpha" "github.com/jedib0t/go-pretty/table" ) @@ -30,7 +33,8 @@ import ( func shareListCommand() *command { cmd := newCommand("share-list") cmd.Description = func() string { return "list shares you manage" } - cmd.Usage = func() string { return "Usage: share list [-flags]" } + cmd.Usage = func() string { return "Usage: share-list [-flags]" } + resID := cmd.String("by-resource-id", "", "filter by resource id (storage_id:opaque_id)") cmd.Action = func() error { ctx := getAuthContext() shareClient, err := getUserShareProviderClient() @@ -39,6 +43,25 @@ func shareListCommand() *command { } shareRequest := &usershareproviderv0alphapb.ListSharesRequest{} + if *resID != "" { + // check split by colon (:) + tokens := strings.Split(*resID, ":") + if len(tokens) != 2 { + return fmt.Errorf("resource id invalid") + } + id := &storageproviderv0alphapb.ResourceId{ + StorageId: tokens[0], + OpaqueId: tokens[1], + } + shareRequest.Filters = []*usershareproviderv0alphapb.ListSharesRequest_Filter{ + &usershareproviderv0alphapb.ListSharesRequest_Filter{ + Type: usershareproviderv0alphapb.ListSharesRequest_Filter_LIST_SHARES_REQUEST_FILTER_TYPE_RESOURCE_ID, + Term: &usershareproviderv0alphapb.ListSharesRequest_Filter_ResourceId{ + ResourceId: id, + }, + }, + } + } shareRes, err := shareClient.ListShares(ctx, shareRequest) if err != nil { diff --git a/cmd/revad/gateway.toml b/cmd/revad/gateway.toml index a124910b9c..9f13feb0fc 100644 --- a/cmd/revad/gateway.toml +++ b/cmd/revad/gateway.toml @@ -19,6 +19,7 @@ authsvc = "localhost:9999" usershareprovidersvc = "localhost:9999" appregistrysvc = "localhost:9999" preferencessvc = "localhost:9999" +commit_share_to_storage_grant = true [http] enabled_services = ["ocdavsvc"] diff --git a/cmd/revad/svcs/grpcsvcs/appregistrysvc/appregistrysvc.go b/cmd/revad/svcs/grpcsvcs/appregistrysvc/appregistrysvc.go index 87e3115c75..41eab752c4 100644 --- a/cmd/revad/svcs/grpcsvcs/appregistrysvc/appregistrysvc.go +++ b/cmd/revad/svcs/grpcsvcs/appregistrysvc/appregistrysvc.go @@ -116,7 +116,7 @@ func (s *svc) ListAppProviders(ctx context.Context, req *appregistryv0alphapb.Li } return res, nil } - providers := make([]*appregistryv0alphapb.ProviderInfo, len(pvds)) + providers := make([]*appregistryv0alphapb.ProviderInfo, 0, len(pvds)) for _, pvd := range pvds { providers = append(providers, format(pvd)) } diff --git a/cmd/revad/svcs/grpcsvcs/gatewaysvc/appprovidersvc.go b/cmd/revad/svcs/grpcsvcs/gatewaysvc/appprovidersvc.go index aa8043cc4e..da4322ac93 100644 --- a/cmd/revad/svcs/grpcsvcs/gatewaysvc/appprovidersvc.go +++ b/cmd/revad/svcs/grpcsvcs/gatewaysvc/appprovidersvc.go @@ -27,6 +27,7 @@ import ( storageproviderv0alphapb "github.com/cs3org/go-cs3apis/cs3/storageprovider/v0alpha" "github.com/cs3org/reva/cmd/revad/svcs/grpcsvcs/pool" "github.com/cs3org/reva/pkg/appctx" + "github.com/cs3org/reva/pkg/errtypes" "github.com/pkg/errors" ) @@ -35,7 +36,7 @@ func (s *svc) Open(ctx context.Context, req *appproviderv0alphapb.OpenRequest) ( provider, err := s.findAppProvider(ctx, req.ResourceInfo) if err != nil { log.Err(err).Msg("gatewaysvc: error finding app provider") - if _, ok := err.(*notFoundError); ok { + if _, ok := err.(errtypes.IsNotFound); ok { return &appproviderv0alphapb.OpenResponse{ Status: &rpcpb.Status{ Code: rpcpb.Code_CODE_NOT_FOUND, @@ -90,7 +91,7 @@ func (s *svc) findAppProvider(ctx context.Context, ri *storageproviderv0alphapb. } if res.Status.Code == rpcpb.Code_CODE_NOT_FOUND { - return nil, notFoundError("gatewaysvc: app provider not found for resource:" + ri.String()) + return nil, errtypes.NotFound("gatewaysvc: app provider not found for resource:" + ri.String()) } return nil, errors.New("gatewaysvc: error finding a storage provider") diff --git a/cmd/revad/svcs/grpcsvcs/gatewaysvc/gatewaysvc.go b/cmd/revad/svcs/grpcsvcs/gatewaysvc/gatewaysvc.go index 8d783c9c12..e7617e5827 100644 --- a/cmd/revad/svcs/grpcsvcs/gatewaysvc/gatewaysvc.go +++ b/cmd/revad/svcs/grpcsvcs/gatewaysvc/gatewaysvc.go @@ -86,4 +86,6 @@ type config struct { AppRegistryEndpoint string `mapstructure:"appregistrysvc"` PreferencesEndpoint string `mapstructure:"preferencessvc"` UserShareProviderEndpoint string `mapstructure:"usershareprovidersvc"` + CommitShareToStorageGrant bool `mapstructure:"commit_share_to_storage_grant"` + CommitShareToStorageRef bool `mapstructure:"commit_share_to_storage_ref"` } diff --git a/cmd/revad/svcs/grpcsvcs/gatewaysvc/storageprovidersvc.go b/cmd/revad/svcs/grpcsvcs/gatewaysvc/storageprovidersvc.go index 25939c2647..fb2c1b40f6 100644 --- a/cmd/revad/svcs/grpcsvcs/gatewaysvc/storageprovidersvc.go +++ b/cmd/revad/svcs/grpcsvcs/gatewaysvc/storageprovidersvc.go @@ -27,6 +27,7 @@ import ( storagetypespb "github.com/cs3org/go-cs3apis/cs3/storagetypes" "github.com/cs3org/reva/cmd/revad/svcs/grpcsvcs/pool" "github.com/cs3org/reva/pkg/appctx" + "github.com/cs3org/reva/pkg/errtypes" "github.com/pkg/errors" ) @@ -45,7 +46,7 @@ func (s *svc) InitiateFileDownload(ctx context.Context, req *storageproviderv0al if err != nil { log.Err(err).Msg("gatewaysvc: error finding storage provider") - if _, ok := err.(notFoundError); ok { + if _, ok := err.(errtypes.IsNotFound); ok { return &storageproviderv0alphapb.InitiateFileDownloadResponse{ Status: &rpcpb.Status{ Code: rpcpb.Code_CODE_NOT_FOUND, @@ -87,7 +88,7 @@ func (s *svc) InitiateFileUpload(ctx context.Context, req *storageproviderv0alph if err != nil { log.Err(err).Msg("gatewaysvc: error finding storage provider") - if _, ok := err.(notFoundError); ok { + if _, ok := err.(errtypes.IsNotFound); ok { return &storageproviderv0alphapb.InitiateFileUploadResponse{ Status: &rpcpb.Status{ Code: rpcpb.Code_CODE_NOT_FOUND, @@ -138,7 +139,7 @@ func (s *svc) CreateContainer(ctx context.Context, req *storageproviderv0alphapb if err != nil { log.Err(err).Msg("gatewaysvc: error finding storage provider") - if _, ok := err.(notFoundError); ok { + if _, ok := err.(errtypes.IsNotFound); ok { return &storageproviderv0alphapb.CreateContainerResponse{ Status: &rpcpb.Status{ Code: rpcpb.Code_CODE_NOT_FOUND, @@ -180,7 +181,7 @@ func (s *svc) Delete(ctx context.Context, req *storageproviderv0alphapb.DeleteRe if err != nil { log.Err(err).Msg("gatewaysvc: error finding storage provider") - if _, ok := err.(notFoundError); ok { + if _, ok := err.(errtypes.IsNotFound); ok { return &storageproviderv0alphapb.DeleteResponse{ Status: &rpcpb.Status{ Code: rpcpb.Code_CODE_NOT_FOUND, @@ -231,7 +232,7 @@ func (s *svc) Stat(ctx context.Context, req *storageproviderv0alphapb.StatReques if err != nil { log.Err(err).Msg("gatewaysvc: error finding storage provider") - if _, ok := err.(notFoundError); ok { + if _, ok := err.(errtypes.IsNotFound); ok { return &storageproviderv0alphapb.StatResponse{ Status: &rpcpb.Status{ Code: rpcpb.Code_CODE_NOT_FOUND, @@ -277,7 +278,7 @@ func (s *svc) ListContainer(ctx context.Context, req *storageproviderv0alphapb.L if err != nil { log.Err(err).Msg("gatewaysvc: error finding storage provider") - if _, ok := err.(notFoundError); ok { + if _, ok := err.(errtypes.IsNotFound); ok { return &storageproviderv0alphapb.ListContainerResponse{ Status: &rpcpb.Status{ Code: rpcpb.Code_CODE_NOT_FOUND, @@ -307,7 +308,7 @@ func (s *svc) ListContainer(ctx context.Context, req *storageproviderv0alphapb.L res, err := c.ListContainer(ctx, req) if err != nil { - return nil, errors.Wrap(err, "gatewaysvc: error calling Stat") + return nil, errors.Wrap(err, "gatewaysvc: error calling ListContainer") } return res, nil @@ -319,7 +320,7 @@ func (s *svc) ListFileVersions(ctx context.Context, req *storageproviderv0alphap if err != nil { log.Err(err).Msg("gatewaysvc: error finding storage provider") - if _, ok := err.(notFoundError); ok { + if _, ok := err.(errtypes.IsNotFound); ok { return &storageproviderv0alphapb.ListFileVersionsResponse{ Status: &rpcpb.Status{ Code: rpcpb.Code_CODE_NOT_FOUND, @@ -361,7 +362,7 @@ func (s *svc) RestoreFileVersion(ctx context.Context, req *storageproviderv0alph if err != nil { log.Err(err).Msg("gatewaysvc: error finding storage provider") - if _, ok := err.(notFoundError); ok { + if _, ok := err.(errtypes.IsNotFound); ok { return &storageproviderv0alphapb.RestoreFileVersionResponse{ Status: &rpcpb.Status{ Code: rpcpb.Code_CODE_NOT_FOUND, @@ -436,7 +437,7 @@ func (s *svc) ListGrants(ctx context.Context, req *storageproviderv0alphapb.List if err != nil { log.Err(err).Msg("gatewaysvc: error finding storage provider") - if _, ok := err.(notFoundError); ok { + if _, ok := err.(errtypes.IsNotFound); ok { return &storageproviderv0alphapb.ListGrantsResponse{ Status: &rpcpb.Status{ Code: rpcpb.Code_CODE_NOT_FOUND, @@ -478,7 +479,7 @@ func (s *svc) AddGrant(ctx context.Context, req *storageproviderv0alphapb.AddGra if err != nil { log.Err(err).Msg("gatewaysvc: error finding storage provider") - if _, ok := err.(notFoundError); ok { + if _, ok := err.(errtypes.IsNotFound); ok { return &storageproviderv0alphapb.AddGrantResponse{ Status: &rpcpb.Status{ Code: rpcpb.Code_CODE_NOT_FOUND, @@ -493,7 +494,7 @@ func (s *svc) AddGrant(ctx context.Context, req *storageproviderv0alphapb.AddGra }, nil } - log.Info().Str("address", pi.Address).Str("ref", req.Ref.String()).Msg("storage provider found") + log.Info().Str("address", pi.Address).Str("ref", req.Ref.String()).Str("provider", pi.String()).Msg("storage provider found") // TODO(labkode): check for capabilities here c, err := pool.GetStorageProviderServiceClient(pi.Address) @@ -520,7 +521,7 @@ func (s *svc) UpdateGrant(ctx context.Context, req *storageproviderv0alphapb.Upd if err != nil { log.Err(err).Msg("gatewaysvc: error finding storage provider") - if _, ok := err.(notFoundError); ok { + if _, ok := err.(errtypes.IsNotFound); ok { return &storageproviderv0alphapb.UpdateGrantResponse{ Status: &rpcpb.Status{ Code: rpcpb.Code_CODE_NOT_FOUND, @@ -562,7 +563,7 @@ func (s *svc) RemoveGrant(ctx context.Context, req *storageproviderv0alphapb.Rem if err != nil { log.Err(err).Msg("gatewaysvc: error finding storage provider") - if _, ok := err.(notFoundError); ok { + if _, ok := err.(errtypes.IsNotFound); ok { return &storageproviderv0alphapb.RemoveGrantResponse{ Status: &rpcpb.Status{ Code: rpcpb.Code_CODE_NOT_FOUND, @@ -623,18 +624,13 @@ func (s *svc) find(ctx context.Context, ref *storageproviderv0alphapb.Reference) return nil, err } - if res.Status.Code == rpcpb.Code_CODE_OK { + if res.Status.Code == rpcpb.Code_CODE_OK && res.Provider != nil { return res.Provider, nil } if res.Status.Code == rpcpb.Code_CODE_NOT_FOUND { - return nil, notFoundError("gatewaysvc: storage provider not found for reference:" + ref.String()) + return nil, errtypes.NotFound("gatewaysvc: storage provider not found for reference:" + ref.String()) } return nil, errors.New("gatewaysvc: error finding a storage provider") } - -type notFoundError string - -func (e notFoundError) Error() string { return string(e) } -func (e notFoundError) IsNotFound() {} diff --git a/cmd/revad/svcs/grpcsvcs/gatewaysvc/usershareprovidersvc.go b/cmd/revad/svcs/grpcsvcs/gatewaysvc/usershareprovidersvc.go index afe8d6d8e7..3e6e839771 100644 --- a/cmd/revad/svcs/grpcsvcs/gatewaysvc/usershareprovidersvc.go +++ b/cmd/revad/svcs/grpcsvcs/gatewaysvc/usershareprovidersvc.go @@ -22,12 +22,15 @@ import ( "context" rpcpb "github.com/cs3org/go-cs3apis/cs3/rpc" + storageproviderv0alphapb "github.com/cs3org/go-cs3apis/cs3/storageprovider/v0alpha" usershareproviderv0alphapb "github.com/cs3org/go-cs3apis/cs3/usershareprovider/v0alpha" "github.com/cs3org/reva/cmd/revad/svcs/grpcsvcs/pool" + "github.com/cs3org/reva/cmd/revad/svcs/grpcsvcs/status" "github.com/cs3org/reva/pkg/appctx" "github.com/pkg/errors" ) +// TODO(labkode): add multi-phase commit logic when commit share or commit ref is enabled. func (s *svc) CreateShare(ctx context.Context, req *usershareproviderv0alphapb.CreateShareRequest) (*usershareproviderv0alphapb.CreateShareResponse, error) { log := appctx.GetLogger(ctx) @@ -46,6 +49,40 @@ func (s *svc) CreateShare(ctx context.Context, req *usershareproviderv0alphapb.C return nil, errors.Wrap(err, "gatewaysvc: error calling CreateShare") } + if res.Status.Code != rpcpb.Code_CODE_OK { + return res, nil + } + + // if we don't need to commit we return earlier + if !s.c.CommitShareToStorageGrant && !s.c.CommitShareToStorageRef { + return res, nil + } + + // TODO(labkode): if both commits are enabled they could be done concurrently. + if s.c.CommitShareToStorageGrant { + grantReq := &storageproviderv0alphapb.AddGrantRequest{ + Ref: &storageproviderv0alphapb.Reference{ + Spec: &storageproviderv0alphapb.Reference_Id{ + Id: req.ResourceInfo.Id, + }, + }, + Grant: &storageproviderv0alphapb.Grant{ + Grantee: req.Grant.Grantee, + Permissions: req.Grant.Permissions.Permissions, + }, + } + grantRes, err := s.AddGrant(ctx, grantReq) + if err != nil { + return nil, errors.Wrap(err, "gatewaysvc: error calling AddGrant") + } + if grantRes.Status.Code != rpcpb.Code_CODE_OK { + res := &usershareproviderv0alphapb.CreateShareResponse{ + Status: status.NewInternal(ctx, "error committing share to storage grant"), + } + return res, nil + } + } + return res, nil } @@ -56,9 +93,7 @@ func (s *svc) RemoveShare(ctx context.Context, req *usershareproviderv0alphapb.R if err != nil { log.Err(err).Msg("gatewaysvc: error getting usershareprovider client") return &usershareproviderv0alphapb.RemoveShareResponse{ - Status: &rpcpb.Status{ - Code: rpcpb.Code_CODE_INTERNAL, - }, + Status: status.NewInternal(ctx, "error getting user share provider client"), }, nil } @@ -67,10 +102,62 @@ func (s *svc) RemoveShare(ctx context.Context, req *usershareproviderv0alphapb.R return nil, errors.Wrap(err, "gatewaysvc: error calling RemoveShare") } + // if we don't need to commit we return earlier + if !s.c.CommitShareToStorageGrant && !s.c.CommitShareToStorageRef { + return res, nil + } + + // TODO(labkode): if both commits are enabled they could be done concurrently. + if s.c.CommitShareToStorageGrant { + getShareReq := &usershareproviderv0alphapb.GetShareRequest{ + Ref: req.Ref, + } + getShareRes, err := c.GetShare(ctx, getShareReq) + if err != nil { + return nil, errors.Wrap(err, "gatewaysvc: error calling GetShare") + } + + if getShareRes.Status.Code != rpcpb.Code_CODE_OK { + res := &usershareproviderv0alphapb.RemoveShareResponse{ + Status: status.NewInternal(ctx, "error getting share when committing to the share"), + } + return res, nil + } + + grantReq := &storageproviderv0alphapb.RemoveGrantRequest{ + Ref: &storageproviderv0alphapb.Reference{ + Spec: &storageproviderv0alphapb.Reference_Id{ + Id: getShareRes.Share.ResourceId, + }, + }, + Grant: &storageproviderv0alphapb.Grant{ + Grantee: getShareRes.Share.Grantee, + Permissions: getShareRes.Share.Permissions.Permissions, + }, + } + grantRes, err := s.RemoveGrant(ctx, grantReq) + if err != nil { + return nil, errors.Wrap(err, "gatewaysvc: error calling RemoveGrant") + } + if grantRes.Status.Code != rpcpb.Code_CODE_OK { + res := &usershareproviderv0alphapb.RemoveShareResponse{ + Status: status.NewInternal(ctx, "error removing storage grant"), + } + return res, nil + } + } + return res, nil } +// TODO(labkode): we need to validate share state vs storage grant and storage ref +// If there are any inconsitencies, the share needs to be flag as invalid and a background process +// or active fix needs to be performed. func (s *svc) GetShare(ctx context.Context, req *usershareproviderv0alphapb.GetShareRequest) (*usershareproviderv0alphapb.GetShareResponse, error) { + return s.getShare(ctx, req) +} + +func (s *svc) getShare(ctx context.Context, req *usershareproviderv0alphapb.GetShareRequest) (*usershareproviderv0alphapb.GetShareResponse, error) { log := appctx.GetLogger(ctx) c, err := pool.GetUserShareProviderClient(s.c.UserShareProviderEndpoint) @@ -91,6 +178,7 @@ func (s *svc) GetShare(ctx context.Context, req *usershareproviderv0alphapb.GetS return res, nil } +// TODO(labkode): read GetShare comment. func (s *svc) ListShares(ctx context.Context, req *usershareproviderv0alphapb.ListSharesRequest) (*usershareproviderv0alphapb.ListSharesResponse, error) { log := appctx.GetLogger(ctx) @@ -130,6 +218,51 @@ func (s *svc) UpdateShare(ctx context.Context, req *usershareproviderv0alphapb.U return nil, errors.Wrap(err, "gatewaysvc: error calling UpdateShare") } + // if we don't need to commit we return earlier + if !s.c.CommitShareToStorageGrant && !s.c.CommitShareToStorageRef { + return res, nil + } + + // TODO(labkode): if both commits are enabled they could be done concurrently. + if s.c.CommitShareToStorageGrant { + getShareReq := &usershareproviderv0alphapb.GetShareRequest{ + Ref: req.Ref, + } + getShareRes, err := c.GetShare(ctx, getShareReq) + if err != nil { + return nil, errors.Wrap(err, "gatewaysvc: error calling GetShare") + } + + if getShareRes.Status.Code != rpcpb.Code_CODE_OK { + res := &usershareproviderv0alphapb.UpdateShareResponse{ + Status: status.NewInternal(ctx, "error getting share when committing to the share"), + } + return res, nil + } + + grantReq := &storageproviderv0alphapb.UpdateGrantRequest{ + Ref: &storageproviderv0alphapb.Reference{ + Spec: &storageproviderv0alphapb.Reference_Id{ + Id: getShareRes.Share.ResourceId, + }, + }, + Grant: &storageproviderv0alphapb.Grant{ + Grantee: getShareRes.Share.Grantee, + Permissions: getShareRes.Share.Permissions.Permissions, + }, + } + grantRes, err := s.UpdateGrant(ctx, grantReq) + if err != nil { + return nil, errors.Wrap(err, "gatewaysvc: error calling UpdateGrant") + } + if grantRes.Status.Code != rpcpb.Code_CODE_OK { + res := &usershareproviderv0alphapb.UpdateShareResponse{ + Status: status.NewInternal(ctx, "error updating storage grant"), + } + return res, nil + } + } + return res, nil } diff --git a/cmd/revad/svcs/grpcsvcs/storageprovidersvc/storageprovidersvc.go b/cmd/revad/svcs/grpcsvcs/storageprovidersvc/storageprovidersvc.go index 87dd5767c5..a9c9a1db45 100644 --- a/cmd/revad/svcs/grpcsvcs/storageprovidersvc/storageprovidersvc.go +++ b/cmd/revad/svcs/grpcsvcs/storageprovidersvc/storageprovidersvc.go @@ -69,7 +69,7 @@ func (s *service) Close() error { } func parseXSTypes(xsTypes map[string]uint32) ([]*storageproviderv0alphapb.ResourceChecksumPriority, error) { - var types = make([]*storageproviderv0alphapb.ResourceChecksumPriority, len(xsTypes)) + var types = make([]*storageproviderv0alphapb.ResourceChecksumPriority, 0, len(xsTypes)) for xs, prio := range xsTypes { t := PKG2GRPCXS(xs) if t == storageproviderv0alphapb.ResourceChecksumType_RESOURCE_CHECKSUM_TYPE_INVALID { @@ -226,8 +226,7 @@ func (s *service) GetPath(ctx context.Context, req *storageproviderv0alphapb.Get func (s *service) CreateContainer(ctx context.Context, req *storageproviderv0alphapb.CreateContainerRequest) (*storageproviderv0alphapb.CreateContainerResponse, error) { log := appctx.GetLogger(ctx) - fn := req.Ref.GetPath() - fsfn, _, err := s.unwrap(ctx, fn) + newRef, err := s.unwrap(ctx, req.Ref) if err != nil { log.Error().Err(err).Msg("error unwraping path") status := &rpcpb.Status{Code: rpcpb.Code_CODE_INVALID} @@ -235,13 +234,13 @@ func (s *service) CreateContainer(ctx context.Context, req *storageproviderv0alp return res, nil } - if err := s.storage.CreateDir(ctx, fsfn); err != nil { + if err := s.storage.CreateDir(ctx, newRef.GetPath()); err != nil { if _, ok := err.(errtypes.IsNotFound); ok { status := &rpcpb.Status{Code: rpcpb.Code_CODE_NOT_FOUND} res := &storageproviderv0alphapb.CreateContainerResponse{Status: status} return res, nil } - log.Error().Err(err).Msg("error creating folder " + fn) + log.Error().Err(err).Msg("error creating container: " + req.Ref.String()) status := &rpcpb.Status{Code: rpcpb.Code_CODE_INTERNAL} res := &storageproviderv0alphapb.CreateContainerResponse{Status: status} return res, nil @@ -254,17 +253,14 @@ func (s *service) CreateContainer(ctx context.Context, req *storageproviderv0alp func (s *service) Delete(ctx context.Context, req *storageproviderv0alphapb.DeleteRequest) (*storageproviderv0alphapb.DeleteResponse, error) { log := appctx.GetLogger(ctx) - fn := req.Ref.GetPath() - - fsfn, _, err := s.unwrap(ctx, fn) + newRef, err := s.unwrap(ctx, req.Ref) if err != nil { status := &rpcpb.Status{Code: rpcpb.Code_CODE_INTERNAL} res := &storageproviderv0alphapb.DeleteResponse{Status: status} return res, nil } - ref := &storageproviderv0alphapb.Reference{Spec: &storageproviderv0alphapb.Reference_Path{Path: fsfn}} - if err := s.storage.Delete(ctx, ref); err != nil { + if err := s.storage.Delete(ctx, newRef); err != nil { if _, ok := err.(errtypes.IsNotFound); ok { log.Error().Err(err).Msg("file not found") status := &rpcpb.Status{Code: rpcpb.Code_CODE_NOT_FOUND} @@ -284,28 +280,23 @@ func (s *service) Delete(ctx context.Context, req *storageproviderv0alphapb.Dele func (s *service) Move(ctx context.Context, req *storageproviderv0alphapb.MoveRequest) (*storageproviderv0alphapb.MoveResponse, error) { log := appctx.GetLogger(ctx) - source := req.Source.GetPath() - target := req.Destination.GetPath() - fss, _, err := s.unwrap(ctx, source) + sourceRef, err := s.unwrap(ctx, req.Source) if err != nil { - log.Error().Err(err).Msg("error unwraping path") + log.Error().Err(err).Msg("error unwraping source ref") status := &rpcpb.Status{Code: rpcpb.Code_CODE_INTERNAL} res := &storageproviderv0alphapb.MoveResponse{Status: status} return res, nil } - fst, _, err := s.unwrap(ctx, target) + targetRef, err := s.unwrap(ctx, req.Destination) if err != nil { - log.Error().Err(err).Msg("error unwraping path") + log.Error().Err(err).Msg("error unwraping target ref") status := &rpcpb.Status{Code: rpcpb.Code_CODE_INTERNAL} res := &storageproviderv0alphapb.MoveResponse{Status: status} return res, nil } - sref := &storageproviderv0alphapb.Reference{Spec: &storageproviderv0alphapb.Reference_Path{Path: fss}} - tref := &storageproviderv0alphapb.Reference{Spec: &storageproviderv0alphapb.Reference_Path{Path: fst}} - - if err := s.storage.Move(ctx, sref, tref); err != nil { + if err := s.storage.Move(ctx, sourceRef, targetRef); err != nil { log.Error().Err(err).Msg("error moving file") status := &rpcpb.Status{Code: rpcpb.Code_CODE_INTERNAL} res := &storageproviderv0alphapb.MoveResponse{Status: status} @@ -326,17 +317,15 @@ func (s *service) Stat(ctx context.Context, req *storageproviderv0alphapb.StatRe ) log := appctx.GetLogger(ctx) - fn := req.Ref.GetPath() - fsfn, fctx, err := s.unwrap(ctx, fn) + newRef, err := s.unwrap(ctx, req.Ref) if err != nil { status := &rpcpb.Status{Code: rpcpb.Code_CODE_INVALID} res := &storageproviderv0alphapb.StatResponse{Status: status} return res, nil } - ref := &storageproviderv0alphapb.Reference{Spec: &storageproviderv0alphapb.Reference_Path{Path: fsfn}} - md, err := s.storage.GetMD(ctx, ref) + md, err := s.storage.GetMD(ctx, newRef) if err != nil { if _, ok := err.(errtypes.IsNotFound); ok { log.Warn().Str("ref", req.Ref.String()).Msg("resource not found") @@ -349,9 +338,9 @@ func (s *service) Stat(ctx context.Context, req *storageproviderv0alphapb.StatRe res := &storageproviderv0alphapb.StatResponse{Status: status} return res, nil } - md.Path = s.wrap(ctx, md.Path, fctx) - s.fillInfo(md) + s.wrap(md) + status := &rpcpb.Status{Code: rpcpb.Code_CODE_OK} res := &storageproviderv0alphapb.StatResponse{Status: status, Info: md} return res, nil @@ -360,9 +349,8 @@ func (s *service) Stat(ctx context.Context, req *storageproviderv0alphapb.StatRe func (s *service) ListContainerStream(req *storageproviderv0alphapb.ListContainerStreamRequest, ss storageproviderv0alphapb.StorageProviderService_ListContainerStreamServer) error { ctx := ss.Context() log := appctx.GetLogger(ctx) - fn := req.Ref.GetPath() - fsfn, fctx, err := s.unwrap(ctx, fn) + newRef, err := s.unwrap(ctx, req.Ref) if err != nil { log.Error().Err(err).Msg("error unwraping path") status := &rpcpb.Status{Code: rpcpb.Code_CODE_INTERNAL} @@ -374,8 +362,7 @@ func (s *service) ListContainerStream(req *storageproviderv0alphapb.ListContaine return nil } - ref := &storageproviderv0alphapb.Reference{Spec: &storageproviderv0alphapb.Reference_Path{Path: fsfn}} - mds, err := s.storage.ListFolder(ctx, ref) + mds, err := s.storage.ListFolder(ctx, newRef) if err != nil { log.Error().Err(err).Msg("error listing folder") status := &rpcpb.Status{Code: rpcpb.Code_CODE_INTERNAL} @@ -388,8 +375,7 @@ func (s *service) ListContainerStream(req *storageproviderv0alphapb.ListContaine } for _, md := range mds { - md.Path = s.wrap(ctx, md.Path, fctx) - s.fillInfo(md) + s.wrap(md) res := &storageproviderv0alphapb.ListContainerStreamResponse{ Info: md, Status: &rpcpb.Status{ @@ -407,9 +393,8 @@ func (s *service) ListContainerStream(req *storageproviderv0alphapb.ListContaine func (s *service) ListContainer(ctx context.Context, req *storageproviderv0alphapb.ListContainerRequest) (*storageproviderv0alphapb.ListContainerResponse, error) { log := appctx.GetLogger(ctx) - fn := req.Ref.GetPath() - fsfn, fctx, err := s.unwrap(ctx, fn) + newRef, err := s.unwrap(ctx, req.Ref) if err != nil { log.Error().Err(err).Msg("error unwraping path") status := &rpcpb.Status{Code: rpcpb.Code_CODE_INTERNAL} @@ -417,8 +402,7 @@ func (s *service) ListContainer(ctx context.Context, req *storageproviderv0alpha return res, nil } - ref := &storageproviderv0alphapb.Reference{Spec: &storageproviderv0alphapb.Reference_Path{Path: fsfn}} - mds, err := s.storage.ListFolder(ctx, ref) + mds, err := s.storage.ListFolder(ctx, newRef) if err != nil { log.Error().Err(err).Msg("error listing folder") status := &rpcpb.Status{Code: rpcpb.Code_CODE_INTERNAL} @@ -426,11 +410,9 @@ func (s *service) ListContainer(ctx context.Context, req *storageproviderv0alpha return res, nil } - var infos = make([]*storageproviderv0alphapb.ResourceInfo, len(mds)) + var infos = make([]*storageproviderv0alphapb.ResourceInfo, 0, len(mds)) for _, md := range mds { - - md.Path = s.wrap(ctx, md.Path, fctx) - s.fillInfo(md) + s.wrap(md) infos = append(infos, md) } res := &storageproviderv0alphapb.ListContainerResponse{ @@ -442,9 +424,8 @@ func (s *service) ListContainer(ctx context.Context, req *storageproviderv0alpha func (s *service) ListFileVersions(ctx context.Context, req *storageproviderv0alphapb.ListFileVersionsRequest) (*storageproviderv0alphapb.ListFileVersionsResponse, error) { log := appctx.GetLogger(ctx) - fn := req.Ref.GetPath() - fsfn, _, err := s.unwrap(ctx, fn) + newRef, err := s.unwrap(ctx, req.Ref) if err != nil { log.Error().Err(err).Msg("error unwraping path") status := &rpcpb.Status{Code: rpcpb.Code_CODE_INTERNAL} @@ -452,9 +433,7 @@ func (s *service) ListFileVersions(ctx context.Context, req *storageproviderv0al return res, nil } - ref := &storageproviderv0alphapb.Reference{Spec: &storageproviderv0alphapb.Reference_Path{Path: fsfn}} - - revs, err := s.storage.ListRevisions(ctx, ref) + revs, err := s.storage.ListRevisions(ctx, newRef) if err != nil { log.Error().Err(err).Msg("error listing file versions") status := &rpcpb.Status{Code: rpcpb.Code_CODE_INTERNAL} @@ -469,9 +448,8 @@ func (s *service) ListFileVersions(ctx context.Context, req *storageproviderv0al func (s *service) RestoreFileVersion(ctx context.Context, req *storageproviderv0alphapb.RestoreFileVersionRequest) (*storageproviderv0alphapb.RestoreFileVersionResponse, error) { log := appctx.GetLogger(ctx) - fn := req.Ref.GetPath() - fsfn, _, err := s.unwrap(ctx, fn) + newRef, err := s.unwrap(ctx, req.Ref) if err != nil { log.Error().Err(err).Msg("error unwraping path") status := &rpcpb.Status{Code: rpcpb.Code_CODE_INTERNAL} @@ -479,9 +457,7 @@ func (s *service) RestoreFileVersion(ctx context.Context, req *storageproviderv0 return res, nil } - ref := &storageproviderv0alphapb.Reference{Spec: &storageproviderv0alphapb.Reference_Path{Path: fsfn}} - - if err := s.storage.RestoreRevision(ctx, ref, req.Key); err != nil { + if err := s.storage.RestoreRevision(ctx, newRef, req.Key); err != nil { log.Error().Err(err).Msg("error restoring version") status := &rpcpb.Status{Code: rpcpb.Code_CODE_INTERNAL} res := &storageproviderv0alphapb.RestoreFileVersionResponse{Status: status} @@ -507,8 +483,8 @@ func (s *service) ListRecycleStream(req *storageproviderv0alphapb.ListRecycleStr return nil } + // TODO(labkode): CRITICAL: fill recycle info with storage provider. for _, item := range items { - res := &storageproviderv0alphapb.ListRecycleStreamResponse{ RecycleItem: item, Status: &rpcpb.Status{ @@ -526,6 +502,7 @@ func (s *service) ListRecycleStream(req *storageproviderv0alphapb.ListRecycleStr func (s *service) ListRecycle(ctx context.Context, req *storageproviderv0alphapb.ListRecycleRequest) (*storageproviderv0alphapb.ListRecycleResponse, error) { log := appctx.GetLogger(ctx) items, err := s.storage.ListRecycle(ctx) + // TODO(labkode): CRITICAL: fill recycle info with storage provider. if err != nil { log.Error().Err(err).Msg("error listing recycle") status := &rpcpb.Status{Code: rpcpb.Code_CODE_INTERNAL} @@ -543,6 +520,7 @@ func (s *service) ListRecycle(ctx context.Context, req *storageproviderv0alphapb func (s *service) RestoreRecycleItem(ctx context.Context, req *storageproviderv0alphapb.RestoreRecycleItemRequest) (*storageproviderv0alphapb.RestoreRecycleItemResponse, error) { log := appctx.GetLogger(ctx) + // TODO(labkode): CRITICAL: fill recycle info with storage provider. if err := s.storage.RestoreRecycleItem(ctx, req.Key); err != nil { log.Error().Err(err).Msg("error restoring recycle item") status := &rpcpb.Status{Code: rpcpb.Code_CODE_INTERNAL} @@ -573,7 +551,6 @@ func (s *service) ListGrants(ctx context.Context, req *storageproviderv0alphapb. func (s *service) AddGrant(ctx context.Context, req *storageproviderv0alphapb.AddGrantRequest) (*storageproviderv0alphapb.AddGrantResponse, error) { log := appctx.GetLogger(ctx) - // check grantee type is valid if req.Grant.Grantee.Type == storageproviderv0alphapb.GranteeType_GRANTEE_TYPE_INVALID { log.Warn().Msg("grantee type is invalid") @@ -582,19 +559,15 @@ func (s *service) AddGrant(ctx context.Context, req *storageproviderv0alphapb.Ad return res, nil } - fn := req.Ref.GetPath() - - fsfn, _, err := s.unwrap(ctx, fn) + newRef, err := s.unwrap(ctx, req.Ref) if err != nil { - log.Error().Err(err).Msg("error unwraping path") + log.Error().Err(err).Msg("error unwraping ref") status := &rpcpb.Status{Code: rpcpb.Code_CODE_INTERNAL} res := &storageproviderv0alphapb.AddGrantResponse{Status: status} return res, nil } - ref := &storageproviderv0alphapb.Reference{Spec: &storageproviderv0alphapb.Reference_Path{Path: fsfn}} - - err = s.storage.AddGrant(ctx, ref, req.Grant) + err = s.storage.AddGrant(ctx, newRef, req.Grant) if err != nil { log.Error().Err(err).Msg("error setting acl") status := &rpcpb.Status{Code: rpcpb.Code_CODE_INTERNAL} @@ -617,8 +590,7 @@ func (s *service) UpdateGrant(ctx context.Context, req *storageproviderv0alphapb return res, nil } - fn := req.Ref.GetPath() - fsfn, _, err := s.unwrap(ctx, fn) + newRef, err := s.unwrap(ctx, req.Ref) if err != nil { log.Error().Err(err).Msg("error unwraping path") status := &rpcpb.Status{Code: rpcpb.Code_CODE_INTERNAL} @@ -626,9 +598,7 @@ func (s *service) UpdateGrant(ctx context.Context, req *storageproviderv0alphapb return res, nil } - ref := &storageproviderv0alphapb.Reference{Spec: &storageproviderv0alphapb.Reference_Path{Path: fsfn}} - - if err := s.storage.UpdateGrant(ctx, ref, req.Grant); err != nil { + if err := s.storage.UpdateGrant(ctx, newRef, req.Grant); err != nil { log.Error().Err(err).Msg("error updating acl") status := &rpcpb.Status{Code: rpcpb.Code_CODE_INTERNAL} res := &storageproviderv0alphapb.UpdateGrantResponse{Status: status} @@ -650,8 +620,7 @@ func (s *service) RemoveGrant(ctx context.Context, req *storageproviderv0alphapb return res, nil } - fn := req.Ref.GetPath() - fsfn, _, err := s.unwrap(ctx, fn) + newRef, err := s.unwrap(ctx, req.Ref) if err != nil { log.Error().Err(err).Msg("error unwraping path") status := &rpcpb.Status{Code: rpcpb.Code_CODE_INTERNAL} @@ -659,9 +628,7 @@ func (s *service) RemoveGrant(ctx context.Context, req *storageproviderv0alphapb return res, nil } - ref := &storageproviderv0alphapb.Reference{Spec: &storageproviderv0alphapb.Reference_Path{Path: fsfn}} - - if err := s.storage.RemoveGrant(ctx, ref, req.Grant); err != nil { + if err := s.storage.RemoveGrant(ctx, newRef, req.Grant); err != nil { log.Error().Err(err).Msg("error removing grant") status := &rpcpb.Status{Code: rpcpb.Code_CODE_INTERNAL} res := &storageproviderv0alphapb.RemoveGrantResponse{Status: status} @@ -687,95 +654,45 @@ func (s *service) GetQuota(ctx context.Context, req *storageproviderv0alphapb.Ge return res, nil } -func (s *service) splitFn(fsfn string) (string, string, error) { - tokens := strings.Split(fsfn, "/") - l := len(tokens) - if l == 0 { - return "", "", errors.New("fsfn is not id-based") - } - - fid := tokens[0] - if l > 1 { - return fid, path.Join(tokens[1:]...), nil - } - return fid, "", nil -} - -type fnCtx struct { - mountPrefix string - *derefCtx -} - -type derefCtx struct { - derefPath string - fid string - rootFidFn string -} +func (s *service) unwrap(ctx context.Context, ref *storageproviderv0alphapb.Reference) (*storageproviderv0alphapb.Reference, error) { + if ref.GetId() != nil { + idRef := &storageproviderv0alphapb.Reference{ + Spec: &storageproviderv0alphapb.Reference_Id{ + Id: &storageproviderv0alphapb.ResourceId{ + StorageId: "", // on purpose, we are unwrapping, bottom layers only need OpaqueId. + OpaqueId: ref.GetId().OpaqueId, + }, + }, + } -func (s *service) deref(ctx context.Context, fsfn string) (*derefCtx, error) { - if strings.HasPrefix(fsfn, "/") { - return &derefCtx{derefPath: fsfn}, nil + return idRef, nil } - fid, right, err := s.splitFn(fsfn) - if err != nil { - return nil, err + if ref.GetPath() == "" { + // abort, no valid id nor path + return nil, errors.New("ref is invalid: " + ref.String()) } - id := &storageproviderv0alphapb.ResourceId{ - StorageId: s.mountID, - OpaqueId: fid, - } - - // resolve fid to path in the fs - fnPointByID, err := s.storage.GetPathByID(ctx, id) + fn := ref.GetPath() + fsfn, err := s.trimMountPrefix(fn) if err != nil { return nil, err } - derefPath := path.Join(fnPointByID, right) - return &derefCtx{derefPath: derefPath, fid: fid, rootFidFn: fnPointByID}, nil -} - -func (s *service) unwrap(ctx context.Context, fn string) (string, *fnCtx, error) { - mp, fsfn, err := s.trimMounPrefix(fn) - if err != nil { - return "", nil, err - } - - derefCtx, err := s.deref(ctx, fsfn) - if err != nil { - return "", nil, err - } - - fctx := &fnCtx{ - derefCtx: derefCtx, - mountPrefix: mp, - } - return fsfn, fctx, nil -} - -func (s *service) wrap(ctx context.Context, fsfn string, fctx *fnCtx) string { - if !strings.HasPrefix(fsfn, "/") { - fsfn = strings.TrimPrefix(fsfn, fctx.rootFidFn) - fsfn = path.Join(fctx.fid, fsfn) - fsfn = fctx.mountPrefix + ":" + fsfn - } else { - fsfn = path.Join(fctx.mountPrefix, fsfn) + pathRef := &storageproviderv0alphapb.Reference{ + Spec: &storageproviderv0alphapb.Reference_Path{ + Path: fsfn, + }, } - return fsfn + return pathRef, nil } -func (s *service) trimMounPrefix(fn string) (string, string, error) { - mountID := s.mountID + ":" +func (s *service) trimMountPrefix(fn string) (string, error) { if strings.HasPrefix(fn, s.mountPath) { - return s.mountPath, path.Join("/", strings.TrimPrefix(fn, s.mountPath)), nil - } - if strings.HasPrefix(fn, mountID) { - return mountID, strings.TrimPrefix(fn, mountID), nil + return path.Join("/", strings.TrimPrefix(fn, s.mountPath)), nil } - return "", "", errors.New("fn does not belong to this storage provider: " + fn) + return "", errors.New(fmt.Sprintf("path=%q does not belong to this storage provider mount path=%q"+fn, s.mountPath)) } func getFS(c *config) (storage.FS, error) { @@ -785,6 +702,7 @@ func getFS(c *config) (storage.FS, error) { return nil, fmt.Errorf("driver not found: %s", c.Driver) } -func (s *service) fillInfo(ri *storageproviderv0alphapb.ResourceInfo) { +func (s *service) wrap(ri *storageproviderv0alphapb.ResourceInfo) { ri.Id.StorageId = s.mountID + ri.Path = path.Join(s.mountPath, ri.Path) } diff --git a/cmd/revad/svcs/grpcsvcs/storageregistrysvc/storageregistrysvc.go b/cmd/revad/svcs/grpcsvcs/storageregistrysvc/storageregistrysvc.go index 087e704ff4..4e08bfb3d1 100644 --- a/cmd/revad/svcs/grpcsvcs/storageregistrysvc/storageregistrysvc.go +++ b/cmd/revad/svcs/grpcsvcs/storageregistrysvc/storageregistrysvc.go @@ -97,7 +97,7 @@ func (s *service) ListStorageProviders(ctx context.Context, req *storageregv0alp return res, nil } - providers := make([]*storagetypespb.ProviderInfo, len(pinfos)) + providers := make([]*storagetypespb.ProviderInfo, 0, len(pinfos)) for _, info := range pinfos { fill(info) providers = append(providers, info) @@ -112,10 +112,9 @@ func (s *service) ListStorageProviders(ctx context.Context, req *storageregv0alp func (s *service) GetStorageProvider(ctx context.Context, req *storageregv0alphapb.GetStorageProviderRequest) (*storageregv0alphapb.GetStorageProviderResponse, error) { log := appctx.GetLogger(ctx) - fn := req.Ref.GetPath() - p, err := s.reg.FindProvider(ctx, fn) + p, err := s.reg.FindProvider(ctx, req.Ref) if err != nil { - log.Error().Err(err).Msg("error finding provider") + log.Error().Err(err).Msg("error finding storage provider") res := &storageregv0alphapb.GetStorageProviderResponse{ Status: &rpcpb.Status{Code: rpcpb.Code_CODE_INTERNAL}, } diff --git a/cmd/revad/svcs/grpcsvcs/usershareprovidersvc/usershareprovidersvc.go b/cmd/revad/svcs/grpcsvcs/usershareprovidersvc/usershareprovidersvc.go index 4ad09f3e04..67f53b6dbc 100644 --- a/cmd/revad/svcs/grpcsvcs/usershareprovidersvc/usershareprovidersvc.go +++ b/cmd/revad/svcs/grpcsvcs/usershareprovidersvc/usershareprovidersvc.go @@ -147,8 +147,7 @@ func (s *service) GetShare(ctx context.Context, req *usershareproviderv0alphapb. func (s *service) ListShares(ctx context.Context, req *usershareproviderv0alphapb.ListSharesRequest) (*usershareproviderv0alphapb.ListSharesResponse, error) { log := appctx.GetLogger(ctx) - - shares, err := s.sm.ListShares(ctx, nil) // TODO(labkode): add filter to share manager + shares, err := s.sm.ListShares(ctx, req.Filters) // TODO(labkode): add filter to share manager if err != nil { log.Err(err).Msg("error listing shares") return &usershareproviderv0alphapb.ListSharesResponse{ diff --git a/cmd/revad/svcs/httpsvcs/appregistrysvc/listProvider.go b/cmd/revad/svcs/httpsvcs/appregistrysvc/listProvider.go index f39c6db2d2..6acec49e16 100644 --- a/cmd/revad/svcs/httpsvcs/appregistrysvc/listProvider.go +++ b/cmd/revad/svcs/httpsvcs/appregistrysvc/listProvider.go @@ -65,7 +65,7 @@ func (s *svc) doList(w http.ResponseWriter, r *http.Request) { return } - var rawResponse = make([]string, len(res.Providers)) + var rawResponse = make([]string, 0, len(res.Providers)) for _, provider := range res.Providers { rawResponse = append(rawResponse, provider.Address) } diff --git a/pkg/app/registry/static/static.go b/pkg/app/registry/static/static.go index c3e38c6bd3..f955982d60 100644 --- a/pkg/app/registry/static/static.go +++ b/pkg/app/registry/static/static.go @@ -32,7 +32,7 @@ type registry struct { } func (b *registry) ListProviders(ctx context.Context) ([]*app.ProviderInfo, error) { - var providers = make([]*app.ProviderInfo, len(b.rules)) + var providers = make([]*app.ProviderInfo, 0, len(b.rules)) for _, address := range b.rules { providers = append(providers, &app.ProviderInfo{ Location: address, diff --git a/pkg/appctx/appctx.go b/pkg/appctx/appctx.go index 978e6e596e..3b3d303f6e 100644 --- a/pkg/appctx/appctx.go +++ b/pkg/appctx/appctx.go @@ -21,7 +21,6 @@ package appctx import ( "context" - "github.com/cs3org/reva/pkg/reqid" "github.com/rs/zerolog" ) @@ -35,17 +34,3 @@ func WithLogger(ctx context.Context, l *zerolog.Logger) context.Context { func GetLogger(ctx context.Context) *zerolog.Logger { return zerolog.Ctx(ctx) } - -// WithTrace returns a context with an associated reqid. -func WithTrace(ctx context.Context, t string) context.Context { - return reqid.ContextSetReqID(ctx, t) -} - -// GetTrace returns the trace stored in the context. -func GetTrace(ctx context.Context) string { - t, ok := reqid.ContextGetReqID(ctx) - if ok { - return t - } - return "unknown" -} diff --git a/pkg/eosclient/eosclient.go b/pkg/eosclient/eosclient.go index 4d2596706a..7b49b4cd72 100644 --- a/pkg/eosclient/eosclient.go +++ b/pkg/eosclient/eosclient.go @@ -34,10 +34,10 @@ import ( "github.com/cs3org/reva/pkg/appctx" "github.com/cs3org/reva/pkg/errtypes" - "github.com/cs3org/reva/pkg/reqid" "github.com/cs3org/reva/pkg/storage/acl" "github.com/gofrs/uuid" "github.com/pkg/errors" + "go.opencensus.io/trace" ) const ( @@ -174,8 +174,8 @@ func (c *Client) executeEOS(ctx context.Context, cmd *exec.Cmd) (string, string, cmd.Env = []string{ "EOS_MGM_URL=" + c.opt.URL, } - requestid, _ := reqid.ContextGetReqID(ctx) - cmd.Args = append(cmd.Args, "--comment", requestid) + trace := trace.FromContext(ctx).SpanContext().TraceID.String() + cmd.Args = append(cmd.Args, "--comment", trace) err := cmd.Run() @@ -203,7 +203,7 @@ func (c *Client) executeEOS(ctx context.Context, cmd *exec.Cmd) (string, string, args := fmt.Sprintf("%s", cmd.Args) env := fmt.Sprintf("%s", cmd.Env) - log.Info().Str("args", args).Str("env", env).Int("exit", exitStatus).Msg("eos cmd") + log.Info().Str("args", args).Str("env", env).Int("exit", exitStatus).Str("err", errBuf.String()).Msg("eos cmd") if err != nil && exitStatus != 2 { // don't wrap the errtypes.NotFoundError err = errors.Wrap(err, "error while executing command") diff --git a/pkg/reqid/reqid.go b/pkg/reqid/reqid.go deleted file mode 100644 index 3f3e809da7..0000000000 --- a/pkg/reqid/reqid.go +++ /dev/null @@ -1,55 +0,0 @@ -// Copyright 2018-2019 CERN -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -// In applying this license, CERN does not waive the privileges and immunities -// granted to it by virtue of its status as an Intergovernmental Organization -// or submit itself to any jurisdiction. - -package reqid - -import "context" -import "github.com/gofrs/uuid" - -type key int - -const reqIDKey key = iota - -// ReqIDHeaderName is the header to use when storing the -// request ID into an HTTP or GRPC header. -const ReqIDHeaderName = "x-request-id" - -// ContextGetReqID returns the reqID if set in the given context. -func ContextGetReqID(ctx context.Context) (string, bool) { - u, ok := ctx.Value(reqIDKey).(string) - return u, ok -} - -// ContextMustGetReqID panics if reqID it not in context. -func ContextMustGetReqID(ctx context.Context) string { - t, ok := ContextGetReqID(ctx) - if !ok { - panic("reqID not found in context") - } - return t -} - -// ContextSetReqID stores the reqID in the context. -func ContextSetReqID(ctx context.Context, reqID string) context.Context { - return context.WithValue(ctx, reqIDKey, reqID) -} - -// MintReqID creates a new request id. -func MintReqID() string { - return uuid.Must(uuid.NewV4()).String() -} diff --git a/pkg/share/manager/memory/memory.go b/pkg/share/manager/memory/memory.go index 6989ecf55b..8b65c555b5 100644 --- a/pkg/share/manager/memory/memory.go +++ b/pkg/share/manager/memory/memory.go @@ -218,15 +218,28 @@ func (m *manager) UpdateShare(ctx context.Context, ref *usershareproviderv0alpha return nil, errtypes.NotFound(ref.String()) } -func (m *manager) ListShares(ctx context.Context, md *storageproviderv0alphapb.ResourceInfo) ([]*usershareproviderv0alphapb.Share, error) { +func (m *manager) ListShares(ctx context.Context, filters []*usershareproviderv0alphapb.ListSharesRequest_Filter) ([]*usershareproviderv0alphapb.Share, error) { var ss []*usershareproviderv0alphapb.Share m.lock.Lock() defer m.lock.Unlock() user := user.ContextMustGetUser(ctx) for _, s := range m.shares { - // TODO(labkode): add check for owner. + // TODO(labkode): add check for creator. if user.Id.Idp == s.Owner.Idp && user.Id.OpaqueId == s.Owner.OpaqueId { - ss = append(ss, s) + // no filter we return earlier + if len(filters) == 0 { + ss = append(ss, s) + } else { + // check filters + // TODO(labkode): add the rest of filters. + for _, f := range filters { + if f.Type == usershareproviderv0alphapb.ListSharesRequest_Filter_LIST_SHARES_REQUEST_FILTER_TYPE_RESOURCE_ID { + if s.ResourceId.StorageId == f.GetResourceId().StorageId && s.ResourceId.OpaqueId == f.GetResourceId().OpaqueId { + ss = append(ss, s) + } + } + } + } } } return ss, nil diff --git a/pkg/share/share.go b/pkg/share/share.go index 49d7a151ba..26c02b1c85 100644 --- a/pkg/share/share.go +++ b/pkg/share/share.go @@ -41,7 +41,7 @@ type Manager interface { // ListShares returns the shares created by the user. If md is provided is not nil, // it returns only shares attached to the given resource. - ListShares(ctx context.Context, md *storageproviderv0alphapb.ResourceInfo) ([]*usershareproviderv0alphapb.Share, error) + ListShares(ctx context.Context, filters []*usershareproviderv0alphapb.ListSharesRequest_Filter) ([]*usershareproviderv0alphapb.Share, error) // ListReceivedShares returns the list of shares the user has access. ListReceivedShares(ctx context.Context) ([]*usershareproviderv0alphapb.ReceivedShare, error) diff --git a/pkg/storage/registry/static/static.go b/pkg/storage/registry/static/static.go index 7f7a1201a8..d3e4a44f93 100644 --- a/pkg/storage/registry/static/static.go +++ b/pkg/storage/registry/static/static.go @@ -24,6 +24,7 @@ import ( "github.com/cs3org/reva/pkg/storage/registry/registry" + storageproviderv0alphapb "github.com/cs3org/go-cs3apis/cs3/storageprovider/v0alpha" storagetypespb "github.com/cs3org/go-cs3apis/cs3/storagetypes" "github.com/cs3org/reva/pkg/errtypes" "github.com/cs3org/reva/pkg/storage" @@ -49,24 +50,43 @@ func (b *reg) ListProviders(ctx context.Context) ([]*storagetypespb.ProviderInfo return providers, nil } -func (b *reg) FindProvider(ctx context.Context, fn string) (*storagetypespb.ProviderInfo, error) { +func (b *reg) FindProvider(ctx context.Context, ref *storageproviderv0alphapb.Reference) (*storagetypespb.ProviderInfo, error) { // find longest match var match string - for prefix := range b.rules { - if strings.HasPrefix(fn, prefix) && len(prefix) > len(match) { - match = prefix + + // we try to find first by path as most storage operations will be done on path. + fn := ref.GetPath() + if fn != "" { + for prefix := range b.rules { + if strings.HasPrefix(fn, prefix) && len(prefix) > len(match) { + match = prefix + } } } - if match == "" { - return nil, errtypes.NotFound("storage provider not found for path " + fn) + if match != "" { + return &storagetypespb.ProviderInfo{ + ProviderPath: match, + Address: b.rules[match], + }, nil + } + + // we try with id + id := ref.GetId() + if id == nil { + return nil, errtypes.NotFound("storage provider not found for ref " + ref.String()) } - p := &storagetypespb.ProviderInfo{ - ProviderPath: match, - Address: b.rules[match], + for prefix := range b.rules { + if id.StorageId == prefix { + // TODO(labkode): fill path info based on provider id, if path and storage id points to same id, take that. + return &storagetypespb.ProviderInfo{ + ProviderId: prefix, + Address: b.rules[prefix], + }, nil + } } - return p, nil + return nil, errtypes.NotFound("storage provider not found for ref " + ref.String()) } type config struct { diff --git a/pkg/storage/storage.go b/pkg/storage/storage.go index 4471e0e28b..921838278b 100644 --- a/pkg/storage/storage.go +++ b/pkg/storage/storage.go @@ -53,6 +53,6 @@ type FS interface { // Registry is the interface that storage registries implement // for discovering storage providers type Registry interface { - FindProvider(ctx context.Context, fn string) (*storagetypespb.ProviderInfo, error) + FindProvider(ctx context.Context, ref *storageproviderv0alphapb.Reference) (*storagetypespb.ProviderInfo, error) ListProviders(ctx context.Context) ([]*storagetypespb.ProviderInfo, error) }