Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add grants to list-spaces #2498

Merged
merged 1 commit into from
Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog/unreleased/spaces-grants.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Enhancement: Include grants in list storage spaces response

Added the grants to the response of list storage spaces. This allows service clients to show who has access to a space.

https://github.com/cs3org/reva/pull/2498
26 changes: 25 additions & 1 deletion pkg/storage/utils/decomposedfs/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,7 @@ func (n *Node) ReadUserPermissions(ctx context.Context, u *userpb.User) (ap prov
func (n *Node) ListGrantees(ctx context.Context) (grantees []string, err error) {
var attrs []string
if attrs, err = xattr.List(n.InternalPath()); err != nil {
appctx.GetLogger(ctx).Error().Err(err).Interface("node", n).Msg("error listing attributes")
appctx.GetLogger(ctx).Error().Err(err).Str("node", n.ID).Msg("error listing attributes")
return nil, err
}
for i := range attrs {
Expand All @@ -903,6 +903,30 @@ func (n *Node) ReadGrant(ctx context.Context, grantee string) (g *provider.Grant
return e.Grant(), nil
}

// ListGrants lists all grants of the current node.
func (n *Node) ListGrants(ctx context.Context) ([]*provider.Grant, error) {
grantees, err := n.ListGrantees(ctx)
if err != nil {
return nil, err
}

grants := make([]*provider.Grant, 0, len(grantees))
for _, g := range grantees {
grant, err := n.ReadGrant(ctx, g)
if err != nil {
appctx.GetLogger(ctx).
Error().
Err(err).
Str("node", n.ID).
Str("grantee", g).
Msg("error reading grant")
continue
}
grants = append(grants, grant)
}
return grants, nil
}

// ReadBlobSizeAttr reads the blobsize from the xattrs
func ReadBlobSizeAttr(path string) (int64, error) {
attrBytes, err := xattr.Get(path, xattrs.BlobsizeAttr)
Expand Down
59 changes: 46 additions & 13 deletions pkg/storage/utils/decomposedfs/spaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package decomposedfs

import (
"context"
"encoding/json"
"fmt"
"math"
"os"
Expand Down Expand Up @@ -478,6 +479,19 @@ func (fs *Decomposedfs) createStorageSpace(ctx context.Context, spaceType, space
}

func (fs *Decomposedfs) storageSpaceFromNode(ctx context.Context, n *node.Node, spaceType, nodePath string, canListAllSpaces bool) (*provider.StorageSpace, error) {
user := ctxpkg.ContextMustGetUser(ctx)
if !canListAllSpaces {
ok, err := node.NewPermissions(fs.lu).HasPermission(ctx, n, func(p *provider.ResourcePermissions) bool {
return p.Stat
})
if err != nil || !ok {
return nil, errtypes.PermissionDenied(fmt.Sprintf("user %s is not allowed to Stat the space %s", user.Username, n.SpaceRoot.ID))
}
if n.SpaceRoot != nil && strings.Contains(n.SpaceRoot.ID, node.TrashIDDelimiter) {
return nil, errtypes.PermissionDenied(fmt.Sprintf("user %s is not allowed to list deleted spaces %s", user.Username, n.SpaceRoot.ID))
}
}

owner, err := n.Owner()
if err != nil {
return nil, err
Expand Down Expand Up @@ -506,7 +520,39 @@ func (fs *Decomposedfs) storageSpaceFromNode(ctx context.Context, n *node.Node,

spaceType = filepath.Base(filepath.Dir(matches[0]))

grants, err := n.ListGrants(ctx)
if err != nil {
return nil, err
}

m := make(map[string]*provider.ResourcePermissions, len(grants))
for _, g := range grants {
var id string
switch g.Grantee.Type {
case provider.GranteeType_GRANTEE_TYPE_GROUP:
id = g.Grantee.GetGroupId().OpaqueId
case provider.GranteeType_GRANTEE_TYPE_USER:
id = g.Grantee.GetUserId().OpaqueId
default:
continue
}

m[id] = g.Permissions
}
marshalled, err := json.Marshal(m)
if err != nil {
return nil, err
}

space := &provider.StorageSpace{
Opaque: &types.Opaque{
Map: map[string]*types.OpaqueEntry{
"grants": {
Decoder: "json",
Value: marshalled,
},
},
},
Id: &provider.StorageSpaceId{OpaqueId: n.SpaceRoot.ID},
Root: &provider.ResourceId{
StorageId: n.SpaceRoot.ID,
Expand All @@ -517,19 +563,6 @@ func (fs *Decomposedfs) storageSpaceFromNode(ctx context.Context, n *node.Node,
// Mtime is set either as node.tmtime or as fi.mtime below
}

user := ctxpkg.ContextMustGetUser(ctx)
if !canListAllSpaces {
ok, err := node.NewPermissions(fs.lu).HasPermission(ctx, n, func(p *provider.ResourcePermissions) bool {
return p.Stat
})
if err != nil || !ok {
return nil, errtypes.PermissionDenied(fmt.Sprintf("user %s is not allowed to Stat the space %+v", user.Username, space))
}
if strings.Contains(space.Root.OpaqueId, node.TrashIDDelimiter) {
return nil, errtypes.PermissionDenied(fmt.Sprintf("user %s is not allowed to list deleted spaces %+v", user.Username, space))
}
}

space.Owner = &userv1beta1.User{ // FIXME only return a UserID, not a full blown user object
Id: owner,
}
Expand Down