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 driveAlias #2623

Merged
merged 6 commits into from
Mar 15, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ build-reva: imports
go build -ldflags ${BUILD_FLAGS} -o ./cmd/reva/reva ./cmd/reva

test: off
go test -coverprofile coverage.out -race $$(go list ./... | grep -v /tests/integration)
go test -v -coverprofile coverage.out -race $$(go list ./... | grep -v /tests/integration)

test-integration: build-ci
cd tests/integration && go test -race ./...
Expand Down
5 changes: 5 additions & 0 deletions changelog/unreleased/space-aliases.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Enhancement: Add space aliases

Space aliases can be used to resolve spaceIDs in a client.

https://github.com/cs3org/reva/pull/2623
11 changes: 11 additions & 0 deletions pkg/storage/utils/decomposedfs/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ type Options struct {

// permissions service to use when checking permissions
PermissionsSVC string `mapstructure:"permissionssvc"`

PersonalSpaceAliasTemplate string `mapstructure:"personalspacealias_template"`
GeneralSpaceAliasTemplate string `mapstructure:"generalspacealias_template"`
}

// New returns a new Options instance for the given configuration
Expand All @@ -73,5 +76,13 @@ func New(m map[string]interface{}) (*Options, error) {
// c.DataDirectory should never end in / unless it is the root
o.Root = filepath.Clean(o.Root)

if o.PersonalSpaceAliasTemplate == "" {
o.PersonalSpaceAliasTemplate = "{{.SpaceType}}/{{.User.Username}}"
}

if o.GeneralSpaceAliasTemplate == "" {
o.GeneralSpaceAliasTemplate = "{{.SpaceType}}/{{.SpaceName | replace \" \" \"-\" | lower}}"
}

return o, nil
}
18 changes: 9 additions & 9 deletions pkg/storage/utils/decomposedfs/recycle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ var _ = Describe("Recycle", func() {
When("a user deletes files from the same space", func() {

BeforeEach(func() {
// in this scenario user "u-s-e-r-id" has this permissions:
registerPermissions(env.Permissions, "u-s-e-r-id", &provider.ResourcePermissions{
// in this scenario user "25b69780-5f39-43be-a7ac-a9b9e9fe4230" has this permissions:
registerPermissions(env.Permissions, "25b69780-5f39-43be-a7ac-a9b9e9fe4230", &provider.ResourcePermissions{
InitiateFileUpload: true,
Delete: true,
ListRecycle: true,
Expand Down Expand Up @@ -132,8 +132,8 @@ var _ = Describe("Recycle", func() {
Username: "anotherusername",
})

// in this scenario user "u-s-e-r-id" has this permissions:
registerPermissions(env.Permissions, "u-s-e-r-id", &provider.ResourcePermissions{
// in this scenario user "25b69780-5f39-43be-a7ac-a9b9e9fe4230" has this permissions:
registerPermissions(env.Permissions, "25b69780-5f39-43be-a7ac-a9b9e9fe4230", &provider.ResourcePermissions{
InitiateFileUpload: true,
Delete: true,
ListRecycle: true,
Expand Down Expand Up @@ -245,8 +245,8 @@ var _ = Describe("Recycle", func() {
Expect(err).ToNot(HaveOccurred())
Expect(projectID).ToNot(BeNil())

// in this scenario user "u-s-e-r-id" has this permissions:
registerPermissions(env.Permissions, "u-s-e-r-id", &provider.ResourcePermissions{
// in this scenario user "25b69780-5f39-43be-a7ac-a9b9e9fe4230" has this permissions:
registerPermissions(env.Permissions, "25b69780-5f39-43be-a7ac-a9b9e9fe4230", &provider.ResourcePermissions{
InitiateFileUpload: true,
Delete: true,
ListRecycle: true,
Expand Down Expand Up @@ -317,8 +317,8 @@ var _ = Describe("Recycle", func() {
Username: "readusername",
})

// in this scenario user "u-s-e-r-id" has this permissions:
registerPermissions(env.Permissions, "u-s-e-r-id", &provider.ResourcePermissions{
// in this scenario user "25b69780-5f39-43be-a7ac-a9b9e9fe4230" has this permissions:
registerPermissions(env.Permissions, "25b69780-5f39-43be-a7ac-a9b9e9fe4230", &provider.ResourcePermissions{
Delete: true,
ListRecycle: true,
PurgeRecycle: true,
Expand Down Expand Up @@ -403,7 +403,7 @@ var _ = Describe("Recycle", func() {
})

// in this scenario user "userid" has this permissions:
registerPermissions(env.Permissions, "u-s-e-r-id", &provider.ResourcePermissions{
registerPermissions(env.Permissions, "25b69780-5f39-43be-a7ac-a9b9e9fe4230", &provider.ResourcePermissions{
Delete: true,
ListRecycle: true,
PurgeRecycle: true,
Expand Down
64 changes: 34 additions & 30 deletions pkg/storage/utils/decomposedfs/spaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
"github.com/cs3org/reva/v2/pkg/storage/utils/decomposedfs/lookup"
"github.com/cs3org/reva/v2/pkg/storage/utils/decomposedfs/node"
"github.com/cs3org/reva/v2/pkg/storage/utils/decomposedfs/xattrs"
"github.com/cs3org/reva/v2/pkg/storage/utils/templates"
"github.com/cs3org/reva/v2/pkg/utils"
"github.com/cs3org/reva/v2/pkg/utils/resourceid"
"github.com/google/uuid"
Expand All @@ -62,22 +63,22 @@ func (fs *Decomposedfs) CreateStorageSpace(ctx context.Context, req *provider.Cr
// "everything is a resource" this is the unique ID for the Space resource.
spaceID := uuid.New().String()
// allow sending a space id
if req.Opaque != nil && req.Opaque.Map != nil {
if e, ok := req.Opaque.Map["spaceid"]; ok && e.Decoder == "plain" {
spaceID = string(e.Value)
}
if reqSpaceID := utils.ReadPlainFromOpaque(req.Opaque, "spaceid"); reqSpaceID != "" {
spaceID = reqSpaceID
}
// allow sending a space description
var description string
if req.Opaque != nil && req.Opaque.Map != nil {
if e, ok := req.Opaque.Map["description"]; ok && e.Decoder == "plain" {
description = string(e.Value)
}
description := utils.ReadPlainFromOpaque(req.Opaque, "description")
// allow sending a spaceAlias
alias := utils.ReadPlainFromOpaque(req.Opaque, "spaceAlias")
u := ctxpkg.ContextMustGetUser(ctx)
if alias == "" {
alias = templates.WithSpacePropertiesAndUser(u, req.Type, req.Name, fs.o.GeneralSpaceAliasTemplate)
}
// TODO enforce a uuid?
// TODO clarify if we want to enforce a single personal storage space or if we want to allow sending the spaceid
if req.Type == spaceTypePersonal {
spaceID = req.GetOwner().GetId().GetOpaqueId()
alias = templates.WithSpacePropertiesAndUser(u, req.Type, req.Name, fs.o.PersonalSpaceAliasTemplate)
}

root, err := node.ReadNode(ctx, fs.lu, spaceID, spaceID)
Expand Down Expand Up @@ -109,7 +110,7 @@ func (fs *Decomposedfs) CreateStorageSpace(ctx context.Context, req *provider.Cr
return nil, err
}

metadata := make(map[string]string, 3)
metadata := make(map[string]string, 6)

// always enable propagation on the storage space root
// mark the space root node as the end of propagation
Expand All @@ -129,6 +130,10 @@ func (fs *Decomposedfs) CreateStorageSpace(ctx context.Context, req *provider.Cr
metadata[xattrs.SpaceDescriptionAttr] = description
}

if alias != "" {
metadata[xattrs.SpaceAliasAttr] = alias
}

if err := xattrs.SetMultiple(root.InternalPath(), metadata); err != nil {
return nil, err
}
Expand Down Expand Up @@ -428,17 +433,20 @@ func (fs *Decomposedfs) UpdateStorageSpace(ctx context.Context, req *provider.Up
metadata[xattrs.SpaceDescriptionAttr] = string(description.Value)
hasDescription = true
}
if image, ok := space.Opaque.Map["image"]; ok {
imageID := resourceid.OwnCloudResourceIDUnwrap(string(image.Value))
if alias := utils.ReadPlainFromOpaque(space.Opaque, "spaceAlias"); alias != "" {
metadata[xattrs.SpaceAliasAttr] = alias
}
if image := utils.ReadPlainFromOpaque(space.Opaque, "image"); image != "" {
imageID := resourceid.OwnCloudResourceIDUnwrap(image)
if imageID == nil {
return &provider.UpdateStorageSpaceResponse{
Status: &v1beta11.Status{Code: v1beta11.Code_CODE_NOT_FOUND, Message: "decomposedFS: space image resource not found"},
}, nil
}
metadata[xattrs.SpaceImageAttr] = imageID.OpaqueId
}
if readme, ok := space.Opaque.Map["readme"]; ok {
readmeID := resourceid.OwnCloudResourceIDUnwrap(string(readme.Value))
if readme := utils.ReadPlainFromOpaque(space.Opaque, "readme"); readme != "" {
readmeID := resourceid.OwnCloudResourceIDUnwrap(readme)
if readmeID == nil {
return &provider.UpdateStorageSpaceResponse{
Status: &v1beta11.Status{Code: v1beta11.Code_CODE_NOT_FOUND, Message: "decomposedFS: space readme resource not found"},
Expand Down Expand Up @@ -634,10 +642,7 @@ func (fs *Decomposedfs) storageSpaceFromNode(ctx context.Context, n *node.Node,
}

if n.SpaceRoot.IsDisabled() {
space.Opaque.Map["trashed"] = &types.OpaqueEntry{
Decoder: "plain",
Value: []byte("trashed"),
}
space.Opaque = utils.AppendPlainToOpaque(space.Opaque, "trashed", "trashed")
}

if n.Owner() != nil && n.Owner().OpaqueId != "" {
Expand Down Expand Up @@ -699,24 +704,23 @@ func (fs *Decomposedfs) storageSpaceFromNode(ctx context.Context, n *node.Node,
}
spaceImage, ok := spaceAttributes[xattrs.SpaceImageAttr]
if ok {
space.Opaque.Map["image"] = &types.OpaqueEntry{
Decoder: "plain",
Value: []byte(resourceid.OwnCloudResourceIDWrap(&provider.ResourceId{StorageId: space.Root.StorageId, OpaqueId: spaceImage})),
}
space.Opaque = utils.AppendPlainToOpaque(space.Opaque, "image", resourceid.OwnCloudResourceIDWrap(
&provider.ResourceId{StorageId: space.Root.StorageId, OpaqueId: spaceImage},
))
}
spaceDescription, ok := spaceAttributes[xattrs.SpaceDescriptionAttr]
if ok {
space.Opaque.Map["description"] = &types.OpaqueEntry{
Decoder: "plain",
Value: []byte(spaceDescription),
}
space.Opaque = utils.AppendPlainToOpaque(space.Opaque, "description", spaceDescription)
}
spaceReadme, ok := spaceAttributes[xattrs.SpaceReadmeAttr]
if ok {
space.Opaque.Map["readme"] = &types.OpaqueEntry{
Decoder: "plain",
Value: []byte(resourceid.OwnCloudResourceIDWrap(&provider.ResourceId{StorageId: space.Root.StorageId, OpaqueId: spaceReadme})),
}
space.Opaque = utils.AppendPlainToOpaque(space.Opaque, "readme", resourceid.OwnCloudResourceIDWrap(
&provider.ResourceId{StorageId: space.Root.StorageId, OpaqueId: spaceReadme},
))
}
spaceAlias, ok := spaceAttributes[xattrs.SpaceAliasAttr]
if ok {
space.Opaque = utils.AppendPlainToOpaque(space.Opaque, "spaceAlias", spaceAlias)
}
return space, nil
}
Expand Down
70 changes: 70 additions & 0 deletions pkg/storage/utils/decomposedfs/spaces_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright 2018-2022 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 decomposedfs_test

import (
permissionsv1beta1 "github.com/cs3org/go-cs3apis/cs3/permissions/v1beta1"
rpcv1beta1 "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
helpers "github.com/cs3org/reva/v2/pkg/storage/utils/decomposedfs/testhelpers"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/stretchr/testify/mock"
)

var _ = Describe("Create Spaces", func() {
var (
env *helpers.TestEnv
)

BeforeEach(func() {
var err error
env, err = helpers.NewTestEnv()
Expect(err).ToNot(HaveOccurred())
env.PermissionsClient.On("CheckPermission", mock.Anything, mock.Anything, mock.Anything).Return(&permissionsv1beta1.CheckPermissionResponse{Status: &rpcv1beta1.Status{Code: rpcv1beta1.Code_CODE_OK}}, nil)
})

AfterEach(func() {
if env != nil {
env.Cleanup()
}
})

Context("during login", func() {
It("space is created", func() {
resp, err := env.Fs.ListStorageSpaces(env.Ctx, nil)
Expect(err).ToNot(HaveOccurred())
Expect(len(resp)).To(Equal(1))
Expect(string(resp[0].Opaque.GetMap()["spaceAlias"].Value)).To(Equal("personal/username"))
Expect(resp[0].Name).To(Equal("username"))
Expect(resp[0].SpaceType).To(Equal("personal"))
})
})
Context("when creating a space", func() {
It("project space is created", func() {
resp, err := env.Fs.CreateStorageSpace(env.Ctx, &provider.CreateStorageSpaceRequest{Name: "Mission to Mars", Type: "project"})
Expect(err).ToNot(HaveOccurred())
Expect(resp.Status.Code).To(Equal(rpcv1beta1.Code_CODE_OK))
Expect(resp.StorageSpace).ToNot(Equal(nil))
Expect(string(resp.StorageSpace.Opaque.Map["spaceAlias"].Value)).To(Equal("project/mission-to-mars"))
Expect(resp.StorageSpace.Name).To(Equal("Mission to Mars"))
Expect(resp.StorageSpace.SpaceType).To(Equal("project"))
})
})
})
2 changes: 1 addition & 1 deletion pkg/storage/utils/decomposedfs/testhelpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func NewTestEnv() (*TestEnv, error) {
owner := &userpb.User{
Id: &userpb.UserId{
Idp: "idp",
OpaqueId: "u-s-e-r-id",
OpaqueId: "25b69780-5f39-43be-a7ac-a9b9e9fe4230",
Type: userpb.UserType_USER_TYPE_PRIMARY,
},
Username: "username",
Expand Down
1 change: 1 addition & 0 deletions pkg/storage/utils/decomposedfs/xattrs/xattrs.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ const (
SpaceDescriptionAttr string = OcisPrefix + "space.description"
SpaceReadmeAttr string = OcisPrefix + "space.readme"
SpaceImageAttr string = OcisPrefix + "space.image"
SpaceAliasAttr string = OcisPrefix + "space.alias"

UserAcePrefix string = "u:"
GroupAcePrefix string = "g:"
Expand Down
40 changes: 40 additions & 0 deletions pkg/storage/utils/templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ type UserData struct {
Email EmailData
}

// SpaceData contains the templace placeholders for a space.
// For example {{.SpaceName}} {{.SpaceType}} or {{.User.Id.OpaqueId}}
type SpaceData struct {
*UserData
SpaceType string
SpaceName string
}

// EmailData contains mail data
// split into local and domain part.
// It is extracted from splitting the username by @.
Expand All @@ -69,8 +77,30 @@ func WithUser(u *userpb.User, tpl string) string {
return b.String()
}

// WithSpacePropertiesAndUser generates a layout based on user data and a space type.
func WithSpacePropertiesAndUser(u *userpb.User, spaceType string, spaceName string, tpl string) string {
tpl = clean(tpl)
sd := newSpaceData(u, spaceType, spaceName)
// compile given template tpl
t, err := template.New("tpl").Funcs(sprig.TxtFuncMap()).Parse(tpl)
if err != nil {
err := errors.Wrap(err, fmt.Sprintf("error parsing template: spaceanduser_template:%+v tpl:%s", sd, tpl))
panic(err)
}
b := bytes.Buffer{}
if err := t.Execute(&b, sd); err != nil {
err := errors.Wrap(err, fmt.Sprintf("error executing template: spaceanduser_template:%+v tpl:%s", sd, tpl))
panic(err)
}
return b.String()
}

func newUserData(u *userpb.User) *UserData {
usernameSplit := strings.Split(u.Username, "@")
if u.Mail != "" {
usernameSplit = strings.Split(u.Mail, "@")
}

if len(usernameSplit) == 1 {
usernameSplit = append(usernameSplit, "_unknown")
}
Expand All @@ -88,6 +118,16 @@ func newUserData(u *userpb.User) *UserData {
return ut
}

func newSpaceData(u *userpb.User, st string, n string) *SpaceData {
userData := newUserData(u)
sd := &SpaceData{
userData,
st,
n,
}
return sd
}

func clean(a string) string {
return path.Clean(a)
}
2 changes: 2 additions & 0 deletions tests/oc-integration-tests/drone/storage-users-ocis.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ root = "/drone/src/tmp/reva/data"
treetime_accounting = true
treesize_accounting = true
permissionssvc = "localhost:10000"
personalspacealias_template = "{{.SpaceType}}/{{.User.Username}}"
generalspacealias_template = "{{.SpaceType}}/{{.SpaceName | replace \" \" \"-\" | lower}}"

# we have a locally running dataprovider
[http]
Expand Down
2 changes: 2 additions & 0 deletions tests/oc-integration-tests/local/storage-users.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ root = "/var/tmp/reva/data"
treetime_accounting = true
treesize_accounting = true
permissionssvc = "localhost:10000"
personalspacealias_template = "{{.SpaceType}}/{{.User.Username}}"
generalspacealias_template = "{{.SpaceType}}/{{.SpaceName | replace \" \" \"-\" | lower}}"

# we have a locally running dataprovider
[http]
Expand Down