Skip to content

Commit

Permalink
Expose OCM received shares as a local mount (#3668)
Browse files Browse the repository at this point in the history
  • Loading branch information
gmgigi96 authored Feb 21, 2023
1 parent 3e919cc commit 6a78ec2
Show file tree
Hide file tree
Showing 14 changed files with 1,026 additions and 227 deletions.
3 changes: 3 additions & 0 deletions changelog/unreleased/ocm-mount-received-shares.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Enhancement: Expose OCM received shares as a local mount

https://github.com/cs3org/reva/pull/3668
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
github.com/cheggaaa/pb v1.0.29
github.com/coreos/go-oidc v2.2.1+incompatible
github.com/cs3org/cato v0.0.0-20200828125504-e418fc54dd5e
github.com/cs3org/go-cs3apis v0.0.0-20230220105024-9b045290a172
github.com/cs3org/go-cs3apis v0.0.0-20230221082129-bcf2b5cf8870
github.com/dgraph-io/ristretto v0.1.1
github.com/dolthub/go-mysql-server v0.14.0
github.com/eventials/go-tus v0.0.0-20200718001131-45c7ec8f5d59
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -305,10 +305,10 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/creack/pty v1.1.11/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/cs3org/cato v0.0.0-20200828125504-e418fc54dd5e h1:tqSPWQeueWTKnJVMJffz4pz0o1WuQxJ28+5x5JgaHD8=
github.com/cs3org/cato v0.0.0-20200828125504-e418fc54dd5e/go.mod h1:XJEZ3/EQuI3BXTp/6DUzFr850vlxq11I6satRtz0YQ4=
github.com/cs3org/go-cs3apis v0.0.0-20230214162720-ac2ceb2ad50e h1:w4A601AS6pC+3eHb9XDZe5Ctpi4cFyoFnsed2Yisd8Q=
github.com/cs3org/go-cs3apis v0.0.0-20230214162720-ac2ceb2ad50e/go.mod h1:UXha4TguuB52H14EMoSsCqDj7k8a/t7g4gVP+bgY5LY=
github.com/cs3org/go-cs3apis v0.0.0-20230220105024-9b045290a172 h1:S2WbxNSNhrrQGIlvfNkMAmekcQtINvmU51gsKFZEKPo=
github.com/cs3org/go-cs3apis v0.0.0-20230220105024-9b045290a172/go.mod h1:UXha4TguuB52H14EMoSsCqDj7k8a/t7g4gVP+bgY5LY=
github.com/cs3org/go-cs3apis v0.0.0-20230221082129-bcf2b5cf8870 h1:MUYOLg0HxBYDmrtZONje+49yanhqGKmYvishv7GaSvw=
github.com/cs3org/go-cs3apis v0.0.0-20230221082129-bcf2b5cf8870/go.mod h1:UXha4TguuB52H14EMoSsCqDj7k8a/t7g4gVP+bgY5LY=
github.com/cyberdelia/templates v0.0.0-20141128023046-ca7fffd4298c/go.mod h1:GyV+0YP4qX0UQ7r2MoYZ+AvYDp12OF5yg4q8rGnyNh4=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand Down
13 changes: 7 additions & 6 deletions internal/grpc/services/ocmcore/ocmcore.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,13 @@ func (s *service) CreateOCMCoreShare(ctx context.Context, req *ocmcore.CreateOCM
UserId: req.ShareWith,
},
},
ShareType: req.ShareType,
Owner: req.Owner,
Creator: req.Sender,
Protocols: req.Protocols,
Expiration: req.Expiration,
State: ocm.ShareState_SHARE_STATE_PENDING,
ResourceType: req.ResourceType,
ShareType: req.ShareType,
Owner: req.Owner,
Creator: req.Sender,
Protocols: req.Protocols,
Expiration: req.Expiration,
State: ocm.ShareState_SHARE_STATE_PENDING,
})
if err != nil {
// TODO: identify errors
Expand Down
38 changes: 35 additions & 3 deletions pkg/ocm/share/repository/sql/conversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ import (
// ShareType is the type of the share.
type ShareType int

// ItemType is the type of the shares resource.
type ItemType int

// AccessMethod is method granted by the sharer to access
// the shared resource.
type AccessMethod int
Expand Down Expand Up @@ -80,6 +83,13 @@ const (
TransferProtocol
)

const (
// ItemTypeFile is used when the shared resource is a file.
ItemTypeFile ItemType = iota
// ItemTypeFolder is used when the shared resource is a folder.
ItemTypeFolder
)

func convertFromCS3OCMShareType(shareType ocm.ShareType) ShareType {
switch shareType {
case ocm.ShareType_SHARE_TYPE_USER:
Expand Down Expand Up @@ -141,6 +151,7 @@ type dbReceivedShare struct {
Name string
Prefix string
ItemSource string
ItemType ItemType
ShareWith string
Owner string
Initiator string
Expand Down Expand Up @@ -242,9 +253,10 @@ func convertToCS3OCMReceivedShare(s *dbReceivedShare, p []*ocm.Protocol) *ocm.Re
Mtime: &types.Timestamp{
Seconds: uint64(s.Mtime),
},
ShareType: ocm.ShareType_SHARE_TYPE_USER,
State: convertToCS3OCMShareState(s.State),
Protocols: p,
ResourceType: convertToCS3ResourceType(s.ItemType),
ShareType: ocm.ShareType_SHARE_TYPE_USER,
State: convertToCS3OCMShareState(s.State),
Protocols: p,
}
if s.Expiration != 0 {
share.Expiration = &types.Timestamp{
Expand Down Expand Up @@ -279,3 +291,23 @@ func convertToCS3Protocol(p *dbProtocol) *ocm.Protocol {
}
return nil
}

func convertToCS3ResourceType(t ItemType) provider.ResourceType {
switch t {
case ItemTypeFile:
return provider.ResourceType_RESOURCE_TYPE_FILE
case ItemTypeFolder:
return provider.ResourceType_RESOURCE_TYPE_CONTAINER
}
return provider.ResourceType_RESOURCE_TYPE_INVALID
}

func convertFromCS3ResourceType(t provider.ResourceType) ItemType {
switch t {
case provider.ResourceType_RESOURCE_TYPE_FILE:
return ItemTypeFile
case provider.ResourceType_RESOURCE_TYPE_CONTAINER:
return ItemTypeFolder
}
return -1
}
1 change: 1 addition & 0 deletions pkg/ocm/share/repository/sql/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ CREATE TABLE IF NOT EXISTS ocm_received_shares (
name VARCHAR(255) NOT NULL,
fileid_prefix VARCHAR(255) NOT NULL,
item_source VARCHAR(255) NOT NULL,
item_type TINYINT NOT NULL,
share_with VARCHAR(255) NOT NULL,
owner VARCHAR(255) NOT NULL,
initiator VARCHAR(255) NOT NULL,
Expand Down
16 changes: 8 additions & 8 deletions pkg/ocm/share/repository/sql/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,8 +493,8 @@ func storeProtocol(tx *sql.Tx, shareID int64, p Protocol) (int64, error) {
// StoreReceivedShare stores a received share.
func (m *mgr) StoreReceivedShare(ctx context.Context, s *ocm.ReceivedShare) (*ocm.ReceivedShare, error) {
if err := transaction(ctx, m.db, func(tx *sql.Tx) error {
query := "INSERT INTO ocm_received_shares SET name=?,fileid_prefix=?,item_source=?,share_with=?,owner=?,initiator=?,ctime=?,mtime=?,type=?,state=?"
params := []any{s.Name, s.ResourceId.StorageId, s.ResourceId.OpaqueId, s.Grantee.GetUserId().OpaqueId, formatUserID(s.Owner), formatUserID(s.Creator), s.Ctime.Seconds, s.Mtime.Seconds, convertFromCS3OCMShareType(s.ShareType), convertFromCS3OCMShareState(s.State)}
query := "INSERT INTO ocm_received_shares SET name=?,fileid_prefix=?,item_source=?,item_type=?,share_with=?,owner=?,initiator=?,ctime=?,mtime=?,type=?,state=?"
params := []any{s.Name, s.ResourceId.StorageId, s.ResourceId.OpaqueId, convertFromCS3ResourceType(s.ResourceType), s.Grantee.GetUserId().OpaqueId, formatUserID(s.Owner), formatUserID(s.Creator), s.Ctime.Seconds, s.Mtime.Seconds, convertFromCS3OCMShareType(s.ShareType), convertFromCS3OCMShareState(s.State)}

if s.Expiration != nil {
query += ",expiration=?"
Expand Down Expand Up @@ -545,7 +545,7 @@ func (m *mgr) StoreReceivedShare(ctx context.Context, s *ocm.ReceivedShare) (*oc

// ListReceivedShares returns the list of shares the user has access.
func (m *mgr) ListReceivedShares(ctx context.Context, user *userpb.User) ([]*ocm.ReceivedShare, error) {
query := "SELECT id, name, fileid_prefix, item_source, share_with, owner, initiator, ctime, mtime, expiration, type, state FROM ocm_received_shares WHERE share_with=?"
query := "SELECT id, name, fileid_prefix, item_source, item_type, share_with, owner, initiator, ctime, mtime, expiration, type, state FROM ocm_received_shares WHERE share_with=?"

rows, err := m.db.QueryContext(ctx, query, user.Id.OpaqueId)
if err != nil {
Expand All @@ -556,7 +556,7 @@ func (m *mgr) ListReceivedShares(ctx context.Context, user *userpb.User) ([]*ocm
shares := []*ocm.ReceivedShare{}
var ids []any
for rows.Next() {
if err := rows.Scan(&s.ID, &s.Name, &s.Prefix, &s.ItemSource, &s.ShareWith, &s.Owner, &s.Initiator, &s.Ctime, &s.Mtime, &s.Expiration, &s.Type, &s.State); err != nil {
if err := rows.Scan(&s.ID, &s.Name, &s.Prefix, &s.ItemSource, &s.ItemType, &s.ShareWith, &s.Owner, &s.Initiator, &s.Ctime, &s.Mtime, &s.Expiration, &s.Type, &s.State); err != nil {
continue
}
shares = append(shares, convertToCS3OCMReceivedShare(&s, nil))
Expand Down Expand Up @@ -623,11 +623,11 @@ func (m *mgr) GetReceivedShare(ctx context.Context, user *userpb.User, ref *ocm.
}

func (m *mgr) getReceivedByID(ctx context.Context, user *userpb.User, id *ocm.ShareId) (*ocm.ReceivedShare, error) {
query := "SELECT id, name, fileid_prefix, item_source, share_with, owner, initiator, ctime, mtime, expiration, type, state FROM ocm_received_shares WHERE id=? AND share_with=?"
query := "SELECT id, name, fileid_prefix, item_source, item_type, share_with, owner, initiator, ctime, mtime, expiration, type, state FROM ocm_received_shares WHERE id=? AND share_with=?"
params := []any{id.OpaqueId, user.Id.OpaqueId}

var s dbReceivedShare
if err := m.db.QueryRowContext(ctx, query, params...).Scan(&s.ID, &s.Name, &s.Prefix, &s.ItemSource, &s.ShareWith, &s.Owner, &s.Initiator, &s.Ctime, &s.Mtime, &s.Expiration, &s.Type, &s.State); err != nil {
if err := m.db.QueryRowContext(ctx, query, params...).Scan(&s.ID, &s.Name, &s.Prefix, &s.ItemSource, &s.ItemType, &s.ShareWith, &s.Owner, &s.Initiator, &s.Ctime, &s.Mtime, &s.Expiration, &s.Type, &s.State); err != nil {
if err == sql.ErrNoRows {
return nil, share.ErrShareNotFound
}
Expand All @@ -643,11 +643,11 @@ func (m *mgr) getReceivedByID(ctx context.Context, user *userpb.User, id *ocm.Sh
}

func (m *mgr) getReceivedByKey(ctx context.Context, user *userpb.User, key *ocm.ShareKey) (*ocm.ReceivedShare, error) {
query := "SELECT id, name, fileid_prefix, item_source, share_with, owner, initiator, ctime, mtime, expiration, type, state FROM ocm_received_shares WHERE owner=? AND fileid_prefix=? AND item_source=? AND share_with=?"
query := "SELECT id, name, fileid_prefix, item_source, item_type, share_with, owner, initiator, ctime, mtime, expiration, type, state FROM ocm_received_shares WHERE owner=? AND fileid_prefix=? AND item_source=? AND share_with=?"
params := []any{formatUserID(key.Owner), key.ResourceId.StorageId, key.ResourceId.OpaqueId, key.Grantee.GetUserId().OpaqueId}

var s dbReceivedShare
if err := m.db.QueryRowContext(ctx, query, params...).Scan(&s.ID, &s.Name, &s.Prefix, &s.ItemSource, &s.ShareWith, &s.Owner, &s.Initiator, &s.Ctime, &s.Mtime, &s.Expiration, &s.Type, &s.State); err != nil {
if err := m.db.QueryRowContext(ctx, query, params...).Scan(&s.ID, &s.Name, &s.Prefix, &s.ItemSource, &s.ItemType, &s.ShareWith, &s.Owner, &s.Initiator, &s.Ctime, &s.Mtime, &s.Expiration, &s.Type, &s.State); err != nil {
if err == sql.ErrNoRows {
return nil, share.ErrShareNotFound
}
Expand Down
Loading

0 comments on commit 6a78ec2

Please sign in to comment.