Skip to content
Open
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
16 changes: 16 additions & 0 deletions internal/http/services/owncloud/ocdav/propfind/propfind.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ import (

const (
tracerName = "ocdav"

// keys in the opaque of a version's ResourceInfo, set by the versions handler
OpaqueVersionAuthorUsername = "version-author-username"
OpaqueVersionAuthorDisplayName = "version-author-displayname"
Comment on lines +68 to +70

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes more sense for these constants to be part of the "pkg/utils" package.

From my point of view, either follow what is already done and use the string directly, or move all those opaque strings (including "owner-display-name", "downloadURL", etc) as constants in the "pkg/utils" package.
I'm fine to keep using the string directly and leave it as a technical debt for a followup PR if the amount of changes needed is too big.

)

// these keys are used to lookup in ArbitraryMetadata, generated prop names are lowercased
Expand Down Expand Up @@ -1519,6 +1523,18 @@ func mdToPropResponse(ctx context.Context, pf *XML, md *provider.ResourceInfo, p
} else {
appendToNotFound(prop.NotFound("oc:" + pf.Prop[i].Local))
}
case "meta-version-edited-by": // the user who uploaded a file version
if v := utils.ReadPlainFromOpaque(md.Opaque, OpaqueVersionAuthorUsername); v != "" {
appendToOK(prop.Escaped("oc:meta-version-edited-by", v))
} else {
appendToNotFound(prop.NotFound("oc:meta-version-edited-by"))
}
case "meta-version-edited-by-name": // display name of the above
if v := utils.ReadPlainFromOpaque(md.Opaque, OpaqueVersionAuthorDisplayName); v != "" {
appendToOK(prop.Escaped("oc:meta-version-edited-by-name", v))
} else {
appendToNotFound(prop.NotFound("oc:meta-version-edited-by-name"))
}
case "owner-display-name": // phoenix only
if md.Owner != nil {
if net.IsCurrentUserOwnerOrManager(ctx, md.Owner, md) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package propfind_test

import (
"context"
"strings"

provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/owncloud/reva/v2/internal/http/services/owncloud/ocdav/net"
"github.com/owncloud/reva/v2/internal/http/services/owncloud/ocdav/propfind"
"github.com/owncloud/reva/v2/pkg/utils"
)

var _ = Describe("version author properties", func() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better move these tests inside the "propfind_test.go" file.
There is no "version_author.go" file inside the package, so this test file will throw people off thinking there is a missing file. In addition, the target of the tests is in the "propfind.go" file, so it make sense for these tests to be in the "propfind_test.go" file.

body := `<?xml version="1.0"?>
<d:propfind xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns"><d:prop>
<oc:meta-version-edited-by /><oc:meta-version-edited-by-name />
</d:prop></d:propfind>`

Comment on lines +16 to +20

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be moved to a BeforeEach section. At the moment, there is a risk that a test overwrites the variable, and that will cause a mess.

var _ = Describe(......
    var body string

    BeforeEach(func() {
        body = "<?xml version......."
    })

    It(......)
)

This way we can ensure that the tests will receive the same data.

respond := func(md *provider.ResourceInfo) string {
pf, _, err := propfind.ReadPropfind(strings.NewReader(body))
Expect(err).ToNot(HaveOccurred())
ctx := context.WithValue(context.Background(), net.CtxKeyBaseURI, "/remote.php/dav/meta/id/versions")
b, err := propfind.MultistatusResponse(ctx, &pf, []*provider.ResourceInfo{md}, "", "", nil, false)
Expect(err).ToNot(HaveOccurred())
return string(b)
}
version := func(o interface{}) *provider.ResourceInfo {
md := &provider.ResourceInfo{Type: provider.ResourceType_RESOURCE_TYPE_FILE, Path: "v/key"}
if o != nil {
md.Opaque = utils.AppendPlainToOpaque(nil, propfind.OpaqueVersionAuthorUsername, "carol")
md.Opaque = utils.AppendPlainToOpaque(md.Opaque, propfind.OpaqueVersionAuthorDisplayName, "Carol King")
}
return md
}
Comment on lines +21 to +36

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to move these functions inside the tests. Yes, there will be some code duplication, but the test should be easier and more straightforward to read.
Basically, the test should be: prepare data, do the thing, check the result.


It("reports username and display name of the author", func() {
out := respond(version(true))
Expect(out).To(ContainSubstring("<oc:meta-version-edited-by>carol</oc:meta-version-edited-by>"))
Expect(out).To(ContainSubstring("<oc:meta-version-edited-by-name>Carol King</oc:meta-version-edited-by-name>"))
})

It("answers 404 for the properties of a version without a recorded author", func() {
out := respond(version(nil))
Expect(out).To(ContainSubstring("HTTP/1.1 404 Not Found"))
Expect(out).ToNot(ContainSubstring("carol"))
})
})
3 changes: 3 additions & 0 deletions internal/http/services/owncloud/ocdav/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/owncloud/reva/v2/pkg/storagespace"
"github.com/owncloud/reva/v2/pkg/utils"

userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
types "github.com/cs3org/go-cs3apis/cs3/types/v1beta1"
Expand Down Expand Up @@ -173,8 +174,10 @@ func (h *VersionsHandler) doListVersions(w http.ResponseWriter, r *http.Request,
Type: provider.ResourceType_RESOURCE_TYPE_CONTAINER,
})

authors := &versionAuthors{client: client, users: map[string]*userpb.User{}}
for i := range versions {
vi := &provider.ResourceInfo{
Opaque: authors.opaqueFor(ctx, versions[i].GetOpaque()),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to just copy the information here. The opaque should contain just the user ID of the author; we only want to resolve his displayname if needed. This seems a wrong place to get a displayname when we don't know if it has been requested.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In addition, I'm not sure if we need a cache.
As said, the displayname should be fetched by the propfind package, around https://github.com/owncloud/reva/pull/816/changes#diff-fa4130be45a2f0a202cc47c8f7ab33722e36ceccb5ba67e881b772e077a59251R1526 . That happens only if it's explicitly requested. I think we can also limit it to propfinds of depth 0, or at least check what is the expected use case (likely we only want to show the author of a specific file, as part of the file details)

// TODO(jfd) we cannot access version content, this will be a problem when trying to fetch version thumbnails
// Opaque
Type: provider.ResourceType_RESOURCE_TYPE_FILE,
Expand Down
50 changes: 50 additions & 0 deletions internal/http/services/owncloud/ocdav/versions_author.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package ocdav

import (
"context"

gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"
userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
typespb "github.com/cs3org/go-cs3apis/cs3/types/v1beta1"

"github.com/owncloud/reva/v2/internal/http/services/owncloud/ocdav/propfind"
"github.com/owncloud/reva/v2/pkg/appctx"
"github.com/owncloud/reva/v2/pkg/storage"
"github.com/owncloud/reva/v2/pkg/utils"
)

// versionAuthors resolves the author of file versions to a name for the
// duration of one request, asking the user provider once per distinct user.
type versionAuthors struct {
client gateway.GatewayAPIClient
users map[string]*userpb.User
}

// opaqueFor returns the opaque to put on a version's ResourceInfo, or nil when
// the version has no recorded author. A user that can no longer be resolved
// (e.g. deleted) leaves the properties unset rather than failing the listing.
func (a *versionAuthors) opaqueFor(ctx context.Context, v *typespb.Opaque) *typespb.Opaque {
id := &userpb.UserId{}
if !utils.ExistsInOpaque(v, storage.RevisionAuthorOpaqueKey) ||
utils.ReadJSONFromOpaque(v, storage.RevisionAuthorOpaqueKey, id) != nil {
return nil
}
key := id.GetIdp() + "!" + id.GetOpaqueId()
u, ok := a.users[key]
if !ok {
res, err := a.client.GetUser(ctx, &userpb.GetUserRequest{UserId: id})
switch {
case err != nil:
appctx.GetLogger(ctx).Error().Err(err).Msg("could not resolve version author")
case res.GetStatus().GetCode() == rpc.Code_CODE_OK:
u = res.GetUser()
}
a.users[key] = u // also caches a miss
}
if u == nil {
return nil
}
o := utils.AppendPlainToOpaque(nil, propfind.OpaqueVersionAuthorUsername, u.GetUsername())
return utils.AppendPlainToOpaque(o, propfind.OpaqueVersionAuthorDisplayName, u.GetDisplayName())
}
40 changes: 40 additions & 0 deletions internal/http/services/owncloud/ocdav/versions_author_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package ocdav

import (
"context"
"testing"

userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
typespb "github.com/cs3org/go-cs3apis/cs3/types/v1beta1"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"

"github.com/owncloud/reva/v2/internal/http/services/owncloud/ocdav/propfind"
"github.com/owncloud/reva/v2/pkg/rgrpc/status"
"github.com/owncloud/reva/v2/pkg/storage"
"github.com/owncloud/reva/v2/pkg/utils"
"github.com/owncloud/reva/v2/tests/cs3mocks/mocks"
)

func TestVersionAuthorsResolveOncePerUserAndTolerateMisses(t *testing.T) {
ctx := context.Background()
gw := mocks.NewGatewayAPIClient(t)
gw.On("GetUser", mock.Anything, mock.MatchedBy(func(r *userpb.GetUserRequest) bool { return r.UserId.OpaqueId == "carol" })).
Return(&userpb.GetUserResponse{Status: status.NewOK(ctx), User: &userpb.User{Username: "carol", DisplayName: "Carol King"}}, nil).Once()
gw.On("GetUser", mock.Anything, mock.MatchedBy(func(r *userpb.GetUserRequest) bool { return r.UserId.OpaqueId == "gone" })).
Return(&userpb.GetUserResponse{Status: &rpc.Status{Code: rpc.Code_CODE_NOT_FOUND}}, nil).Once()

a := &versionAuthors{client: gw, users: map[string]*userpb.User{}}
mk := func(id string) *typespb.Opaque {
return utils.AppendJSONToOpaque(nil, storage.RevisionAuthorOpaqueKey, &userpb.UserId{OpaqueId: id, Idp: "idp"})
}

for i := 0; i < 2; i++ { // second round must be served from the per-request cache
o := a.opaqueFor(ctx, mk("carol"))
require.Equal(t, "carol", utils.ReadPlainFromOpaque(o, propfind.OpaqueVersionAuthorUsername))
require.Equal(t, "Carol King", utils.ReadPlainFromOpaque(o, propfind.OpaqueVersionAuthorDisplayName))
require.Nil(t, a.opaqueFor(ctx, mk("gone")), "an unresolvable author must not fail the listing")
}
require.Nil(t, a.opaqueFor(ctx, nil), "a version without an author has no opaque")
}
4 changes: 4 additions & 0 deletions pkg/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ type UploadChecksums struct {
Adler32 []byte
}

// RevisionAuthorOpaqueKey is the key in FileVersion.Opaque that holds the json
// encoded userpb.UserId of the user who uploaded the content of a revision.
const RevisionAuthorOpaqueKey = "author"

type UploadInfo struct {
NodeExisted bool // true when the target node existed before the upload started
Size int64
Expand Down
82 changes: 82 additions & 0 deletions pkg/storage/utils/decomposedfs/author.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package decomposedfs

import (
"context"

userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
typespb "github.com/cs3org/go-cs3apis/cs3/types/v1beta1"

"github.com/owncloud/reva/v2/pkg/appctx"
ctxpkg "github.com/owncloud/reva/v2/pkg/ctx"
"github.com/owncloud/reva/v2/pkg/storage"
"github.com/owncloud/reva/v2/pkg/storage/utils/decomposedfs/metadata/prefixes"
"github.com/owncloud/reva/v2/pkg/storage/utils/decomposedfs/node"
"github.com/owncloud/reva/v2/pkg/utils"
)

var authorAttrs = []string{prefixes.AuthorIDAttr, prefixes.AuthorIDPAttr, prefixes.AuthorTypeAttr}

func isAuthorAttr(name string) bool {
for _, a := range authorAttrs {
if a == name {
return true
}
}
return false
}

// setAuthor records the user in the context as author of the content being written.
// Without a user nothing is written, so no author is claimed.
func setAuthor(ctx context.Context, attrs node.Attributes) {
u, ok := ctxpkg.ContextGetUser(ctx)
if !ok || u.GetId().GetOpaqueId() == "" {
return
}
attrs.SetString(prefixes.AuthorIDAttr, u.GetId().GetOpaqueId())
attrs.SetString(prefixes.AuthorIDPAttr, u.GetId().GetIdp())
attrs.SetString(prefixes.AuthorTypeAttr, utils.UserTypeToString(u.GetId().GetType()))
}

// revisionAuthorOpaque reads the author of a revision. It returns nil for a
// revision without one, e.g. one created before authors were recorded.
func (fs *Decomposedfs) revisionAuthorOpaque(ctx context.Context, revisionPath string) *typespb.Opaque {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not against this function, but there are some things that itches...

  • The name is barely descriptive.
  • The functionality seems too specific, for just one use case in one place. If it's going to be difficult to reuse, why not move this function closer to the code that is going to use it?
  • Returning an Opaque hurts the reusability of the function. Is it something this function should do, or it can be delegated to the caller function?

attrs, err := fs.lu.MetadataBackend().All(ctx, revisionPath)
if err != nil {
appctx.GetLogger(ctx).Debug().Err(err).Str("revision", revisionPath).Msg("could not read revision author")
return nil
}
id := string(attrs[prefixes.AuthorIDAttr])
if id == "" {
return nil
}
return utils.AppendJSONToOpaque(nil, storage.RevisionAuthorOpaqueKey, &userpb.UserId{

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The RevisionAuthorOpaqueKey constant should be in the "pkg/utils" package.

OpaqueId: id,
Idp: string(attrs[prefixes.AuthorIDPAttr]),
Type: utils.UserTypeMap(string(attrs[prefixes.AuthorTypeAttr])),
})
}

// dropStaleAuthor removes the author attributes from target that the source does not have.
func (fs *Decomposedfs) dropStaleAuthor(ctx context.Context, sourcePath, targetPath string) error {
src, err := fs.lu.MetadataBackend().All(ctx, sourcePath)
if err != nil {
return err
}
dst, err := fs.lu.MetadataBackend().All(ctx, targetPath)
if err != nil {
return err
}
for _, a := range authorAttrs {
if _, ok := src[a]; ok {
continue
}
// removing an attribute that is not there fails on the xattr backend
if _, ok := dst[a]; !ok {
continue
}
if err := fs.lu.MetadataBackend().Remove(ctx, targetPath, a, false); err != nil {
return err
}
}
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ const (
OwnerIDAttr string = OcisPrefix + "owner.id"
OwnerIDPAttr string = OcisPrefix + "owner.idp"
OwnerTypeAttr string = OcisPrefix + "owner.type"

// the user who uploaded the current content of the node, copied to a revision
// when a later upload archives the content. Modelled on owner.*
AuthorIDAttr string = OcisPrefix + "author.id"
AuthorIDPAttr string = OcisPrefix + "author.idp"
AuthorTypeAttr string = OcisPrefix + "author.type"
// the base name of the node
// updated when the file is renamed or moved
NameAttr string = OcisPrefix + "name"
Expand Down
Loading