Skip to content
This repository has been archived by the owner on Jul 16, 2024. It is now read-only.

Commit

Permalink
Update OCM shares to last version of CS3APIs (cs3org#3646)
Browse files Browse the repository at this point in the history
* rewritten new share endpoint according to new ocm specs

* add go validator dependency

* add new share to ocm client

* defined ocm repository

* rewrote ocm core

* rewrote ocm share provider

* removed commented code

* fixed app provider with new cs3apis

* rewrote ocmshareprovider

* fix commands

* add share type

* add token

* add expiration time to ocm shares

* refactor

* do not set ctime and mtime of share in json driver

* set ctime and mtime of ocm share on creation

* pass user obj

* removed old files

* refactored nextcloud ocm share driver

* removed unused variable

* add commands to get info about ocm share

* generate crypto secure random string

* fixes in json pkg

* fix linting

* add randutil pkg

* fixes

* expose recipient display name

* fixes in json parsing

* fix function name

* add helper functions for tests

* fix response when creating new ocm share

* fix errors in ocm share provider (still internal error)

* improved spawn revad daemons for testing. it's possible now define files with initial content and empty folders used by config files

* defined integration tests for ocm shares

* fixes in other tests

* use fork for go cs3 bindings

* fix typo

* fix linter

* add changelog

* fix linter x2

* return correct errors according to specs

* add other tests and bugfixes

* fix linter

* fix cmd for creating ocm shares
  • Loading branch information
gmgigi96 authored Feb 15, 2023
1 parent c178db1 commit 94ced01
Show file tree
Hide file tree
Showing 51 changed files with 3,550 additions and 2,238 deletions.
4 changes: 4 additions & 0 deletions changelog/unreleased/ocm-shares.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Enhancement: Update OCM shares to last version of CS3APIs

https://github.com/cs3org/reva/pull/3646
https://github.com/cs3org/cs3apis/pull/199
2 changes: 1 addition & 1 deletion cmd/reva/arguments.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (c *Completer) ocmShareReceivedArgumentCompleter() []prompt.Suggest {
}

for _, r := range info {
suggests = append(suggests, prompt.Suggest{Text: r.Share.Id.OpaqueId})
suggests = append(suggests, prompt.Suggest{Text: r.Id.OpaqueId})
}
}

Expand Down
2 changes: 2 additions & 0 deletions cmd/reva/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ var (
ocmShareListCommand(),
ocmShareRemoveCommand(),
ocmShareUpdateCommand(),
ocmShareGetCommand(),
ocmShareListReceivedCommand(),
ocmShareUpdateReceivedCommand(),
ocmShareGetReceivedCommand(),
openInAppCommand(),
preferencesCommand(),
publicShareCreateCommand(),
Expand Down
97 changes: 57 additions & 40 deletions cmd/reva/ocm-share-create.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ package main
import (
"io"
"os"
"strconv"
"time"

appprovider "github.com/cs3org/go-cs3apis/cs3/app/provider/v1beta1"
userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
invitepb "github.com/cs3org/go-cs3apis/cs3/ocm/invite/v1beta1"
ocmprovider "github.com/cs3org/go-cs3apis/cs3/ocm/provider/v1beta1"
rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
ocm "github.com/cs3org/go-cs3apis/cs3/sharing/ocm/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
types "github.com/cs3org/go-cs3apis/cs3/types/v1beta1"
"github.com/cs3org/reva/internal/http/services/owncloud/ocs/conversions"
ocmshare "github.com/cs3org/reva/pkg/ocm/share"
"github.com/cs3org/reva/pkg/utils"
"github.com/jedib0t/go-pretty/table"
"github.com/pkg/errors"
Expand All @@ -45,10 +45,15 @@ func ocmShareCreateCommand() *command {
grantee := cmd.String("grantee", "", "the grantee")
idp := cmd.String("idp", "", "the idp of the grantee, default to same idp as the user triggering the action")
userType := cmd.String("user-type", "primary", "the type of user account, defaults to primary")
rol := cmd.String("rol", "viewer", "the permission for the share (viewer or editor)")

webdav := cmd.Bool("webdav", false, "create a share with webdav access")
webapp := cmd.Bool("webapp", false, "create a share for app access")
transfer := cmd.Bool("transfer", false, "create a share for a data transfer")

rol := cmd.String("rol", "viewer", "the permission for the share (viewer or editor) / applies to webdav and webapp")

cmd.ResetFlags = func() {
*grantType, *grantee, *idp, *rol, *userType = "user", "", "", "viewer", "primary"
*grantType, *grantee, *idp, *rol, *userType, *webdav, *webapp, *transfer = "user", "", "", "viewer", "primary", false, false, false
}

cmd.Action = func(w ...io.Writer) error {
Expand Down Expand Up @@ -101,40 +106,22 @@ func ocmShareCreateCommand() *command {
return formatError(res.Status)
}

perm, pint, err := getOCMSharePerm(*rol)
gt := getGrantType(*grantType)
am, err := getAccessMethods(*webdav, *webapp, *transfer, *rol)
if err != nil {
return err
}

gt := getGrantType(*grantType)
grant := &ocm.ShareGrant{
Permissions: perm,
shareRequest := &ocm.CreateOCMShareRequest{
ResourceId: res.Info.Id,
Grantee: &provider.Grantee{
Type: gt,
// For now, we only support user shares.
// TODO (ishank011): To be updated once this is decided.
Id: &provider.Grantee_UserId{UserId: u},
},
}

opaqueObj := &types.Opaque{
Map: map[string]*types.OpaqueEntry{
"permissions": {
Decoder: "plain",
Value: []byte(strconv.Itoa(pint)),
},
"name": {
Decoder: "plain",
Value: []byte(res.Info.Path),
},
Id: &provider.Grantee_UserId{UserId: remoteUserRes.RemoteUser.Id},
},
}

shareRequest := &ocm.CreateOCMShareRequest{
Opaque: opaqueObj,
ResourceId: res.Info.Id,
Grant: grant,
RecipientMeshProvider: providerInfo.ProviderInfo,
AccessMethods: am,
}

shareRes, err := client.CreateOCMShare(ctx, shareRequest)
Expand All @@ -148,11 +135,12 @@ func ocmShareCreateCommand() *command {

t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"#", "Owner.Idp", "Owner.OpaqueId", "ResourceId", "Permissions", "Type", "Grantee.Idp", "Grantee.OpaqueId", "Created", "Updated"})
t.AppendHeader(table.Row{"#", "Owner.Idp", "Owner.OpaqueId", "ResourceId", "Type", "Grantee.Idp", "Grantee.OpaqueId", "Created", "Updated"})
// TODO (gdelmont): expose protocols info

s := shareRes.Share
t.AppendRows([]table.Row{
{s.Id.OpaqueId, s.Owner.Idp, s.Owner.OpaqueId, s.ResourceId.String(), s.Permissions.String(),
{s.Id.OpaqueId, s.Owner.Idp, s.Owner.OpaqueId, s.ResourceId.String(),
s.Grantee.Type.String(), s.Grantee.GetUserId().Idp, s.Grantee.GetUserId().OpaqueId,
time.Unix(int64(s.Ctime.Seconds), 0), time.Unix(int64(s.Mtime.Seconds), 0)},
})
Expand All @@ -163,15 +151,44 @@ func ocmShareCreateCommand() *command {
return cmd
}

func getOCMSharePerm(p string) (*ocm.SharePermissions, int, error) {
if p == viewerPermission {
return &ocm.SharePermissions{
Permissions: conversions.NewViewerRole().CS3ResourcePermissions(),
}, 1, nil
} else if p == editorPermission {
return &ocm.SharePermissions{
Permissions: conversions.NewEditorRole().CS3ResourcePermissions(),
}, 15, nil
func getAccessMethods(webdav, webapp, transfer bool, rol string) ([]*ocm.AccessMethod, error) {
var m []*ocm.AccessMethod
if webdav {
perm, err := getOCMSharePerm(rol)
if err != nil {
return nil, err
}
m = append(m, ocmshare.NewWebDavAccessMethod(perm))
}
if webapp {
v, err := getOCMViewMode(rol)
if err != nil {
return nil, err
}
m = append(m, ocmshare.NewWebappAccessMethod(v))
}
if transfer {
m = append(m, ocmshare.NewTransferAccessMethod())
}
return m, nil
}

func getOCMSharePerm(p string) (*provider.ResourcePermissions, error) {
switch p {
case viewerPermission:
return conversions.NewViewerRole().CS3ResourcePermissions(), nil
case editorPermission:
return conversions.NewEditorRole().CS3ResourcePermissions(), nil
}
return nil, errors.New("invalid rol: " + p)
}

func getOCMViewMode(p string) (appprovider.ViewMode, error) {
switch p {
case viewerPermission:
return appprovider.ViewMode_VIEW_MODE_READ_ONLY, nil
case editorPermission:
return appprovider.ViewMode_VIEW_MODE_READ_WRITE, nil
}
return nil, 0, errors.New("invalid rol: " + p)
return 0, errors.New("invalid rol: " + p)
}
74 changes: 74 additions & 0 deletions cmd/reva/ocm-share-get-received.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright 2018-2023 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 main

import (
"errors"
"fmt"
"io"

rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
ocm "github.com/cs3org/go-cs3apis/cs3/sharing/ocm/v1beta1"
"github.com/cs3org/reva/pkg/utils"
)

func ocmShareGetReceivedCommand() *command {
cmd := newCommand("ocm-share-get-received")
cmd.Description = func() string { return "get the info of the OCM share you have received" }
cmd.Usage = func() string { return "Usage: ocm-share-get-received" }
cmd.Action = func(w ...io.Writer) error {
if cmd.NArg() < 1 {
return errors.New("Invalid arguments: " + cmd.Usage())
}

ctx := getAuthContext()
client, err := getClient()
if err != nil {
return err
}

id := cmd.Arg(0)

shareRes, err := client.GetReceivedOCMShare(ctx, &ocm.GetReceivedOCMShareRequest{
Ref: &ocm.ShareReference{
Spec: &ocm.ShareReference_Id{
Id: &ocm.ShareId{
OpaqueId: id,
},
},
},
})
if err != nil {
return err
}
if shareRes.Status.Code != rpc.Code_CODE_OK {
return formatError(shareRes.Status)
}

d, err := utils.MarshalProtoV1ToJSON(shareRes.Share)
if err != nil {
return err
}

fmt.Println(string(d))

return nil
}
return cmd
}
74 changes: 74 additions & 0 deletions cmd/reva/ocm-share-get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright 2018-2023 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 main

import (
"errors"
"fmt"
"io"

rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
ocm "github.com/cs3org/go-cs3apis/cs3/sharing/ocm/v1beta1"
"github.com/cs3org/reva/pkg/utils"
)

func ocmShareGetCommand() *command {
cmd := newCommand("ocm-share-get")
cmd.Description = func() string { return "get the info of the OCM share" }
cmd.Usage = func() string { return "Usage: ocm-share-get" }
cmd.Action = func(w ...io.Writer) error {
if cmd.NArg() < 1 {
return errors.New("Invalid arguments: " + cmd.Usage())
}

ctx := getAuthContext()
client, err := getClient()
if err != nil {
return err
}

id := cmd.Arg(0)

shareRes, err := client.GetOCMShare(ctx, &ocm.GetOCMShareRequest{
Ref: &ocm.ShareReference{
Spec: &ocm.ShareReference_Id{
Id: &ocm.ShareId{
OpaqueId: id,
},
},
},
})
if err != nil {
return err
}
if shareRes.Status.Code != rpc.Code_CODE_OK {
return formatError(shareRes.Status)
}

d, err := utils.MarshalProtoV1ToJSON(shareRes.Share)
if err != nil {
return err
}

fmt.Println(string(d))

return nil
}
return cmd
}
10 changes: 5 additions & 5 deletions cmd/reva/ocm-share-list-received.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ func ocmShareListReceivedCommand() *command {
if len(w) == 0 {
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"#", "Owner.Idp", "Owner.OpaqueId", "ResourceId", "Permissions", "Type",
t.AppendHeader(table.Row{"#", "Owner.Idp", "Owner.OpaqueId", "ResourceId", "Type",
"Grantee.Idp", "Grantee.OpaqueId", "Created", "Updated", "State", "ShareType"})
for _, s := range shareRes.Shares {
t.AppendRows([]table.Row{
{s.Share.Id.OpaqueId, s.Share.Owner.Idp, s.Share.Owner.OpaqueId, s.Share.ResourceId.String(),
s.Share.Permissions.String(), s.Share.Grantee.Type.String(), s.Share.Grantee.GetUserId().Idp,
s.Share.Grantee.GetUserId().OpaqueId, time.Unix(int64(s.Share.Ctime.Seconds), 0),
time.Unix(int64(s.Share.Mtime.Seconds), 0), s.State.String(), s.Share.ShareType.String()},
{s.Id.OpaqueId, s.Owner.Idp, s.Owner.OpaqueId, s.ResourceId.String(),
s.Grantee.Type.String(), s.Grantee.GetUserId().Idp,
s.Grantee.GetUserId().OpaqueId, time.Unix(int64(s.Ctime.Seconds), 0),
time.Unix(int64(s.Mtime.Seconds), 0), s.State.String(), s.ShareType.String()},
})
}
t.Render()
Expand Down
4 changes: 2 additions & 2 deletions cmd/reva/ocm-share-list.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ func ocmShareListCommand() *command {
if len(w) == 0 {
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"#", "Owner.Idp", "Owner.OpaqueId", "ResourceId", "Permissions", "Type",
t.AppendHeader(table.Row{"#", "Owner.Idp", "Owner.OpaqueId", "ResourceId", "Type",
"ShareType", "Grantee.Idp", "Grantee.OpaqueId", "Created", "Updated"})

for _, s := range shareRes.Shares {
t.AppendRows([]table.Row{
{s.Id.OpaqueId, s.Owner.Idp, s.Owner.OpaqueId, s.ResourceId.String(), s.Permissions.String(),
{s.Id.OpaqueId, s.Owner.Idp, s.Owner.OpaqueId, s.ResourceId.String(),
s.Grantee.Type.String(), s.ShareType.String(), s.Grantee.GetUserId().Idp, s.Grantee.GetUserId().OpaqueId,
time.Unix(int64(s.Ctime.Seconds), 0), time.Unix(int64(s.Mtime.Seconds), 0)},
})
Expand Down
6 changes: 4 additions & 2 deletions cmd/reva/ocm-share-update.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func ocmShareUpdateCommand() *command {
return err
}

perm, _, err := getOCMSharePerm(*rol)
perm, err := getOCMSharePerm(*rol)
if err != nil {
return err
}
Expand All @@ -69,7 +69,9 @@ func ocmShareUpdateCommand() *command {
},
Field: &ocm.UpdateOCMShareRequest_UpdateField{
Field: &ocm.UpdateOCMShareRequest_UpdateField_Permissions{
Permissions: perm,
Permissions: &ocm.SharePermissions{
Permissions: perm,
},
},
},
}
Expand Down
Loading

0 comments on commit 94ced01

Please sign in to comment.