Skip to content

Commit

Permalink
Merge pull request #4129 from kobergj/AutoAcceptShares
Browse files Browse the repository at this point in the history
Auto Accept Shares with service accounts
  • Loading branch information
kobergj authored Sep 4, 2023
2 parents bf1bb01 + edd41af commit 2ebd3e9
Show file tree
Hide file tree
Showing 14 changed files with 84 additions and 54 deletions.
5 changes: 5 additions & 0 deletions changelog/unreleased/auto-accept-shares.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Enhancement: Auto-Accept Shares through ServiceAccounts

Auto accept shares with service accounts

https://github.com/cs3org/reva/pull/4129
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,9 @@ func (s *service) UpdateReceivedShare(ctx context.Context, req *collaboration.Up
}, nil
}

share, err := s.sm.UpdateReceivedShare(ctx, req.Share, req.UpdateMask)
var uid userpb.UserId
_ = utils.ReadJSONFromOpaque(req.Opaque, "userid", &uid)
share, err := s.sm.UpdateReceivedShare(ctx, req.Share, req.UpdateMask, &uid)
if err != nil {
return &collaboration.UpdateReceivedShareResponse{
Status: status.NewInternal(ctx, "error updating received share"),
Expand Down
3 changes: 2 additions & 1 deletion pkg/cbox/share/sql/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"strings"
"time"

userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
collaboration "github.com/cs3org/go-cs3apis/cs3/sharing/collaboration/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
typespb "github.com/cs3org/go-cs3apis/cs3/types/v1beta1"
Expand Down Expand Up @@ -453,7 +454,7 @@ func (m *mgr) GetReceivedShare(ctx context.Context, ref *collaboration.ShareRefe

}

func (m *mgr) UpdateReceivedShare(ctx context.Context, share *collaboration.ReceivedShare, fieldMask *field_mask.FieldMask) (*collaboration.ReceivedShare, error) {
func (m *mgr) UpdateReceivedShare(ctx context.Context, share *collaboration.ReceivedShare, fieldMask *field_mask.FieldMask, _ *userpb.UserId) (*collaboration.ReceivedShare, error) {
user := ctxpkg.ContextMustGetUser(ctx)

rs, err := m.GetReceivedShare(ctx, &collaboration.ShareReference{Spec: &collaboration.ShareReference_Id{Id: share.Share.Id}})
Expand Down
11 changes: 8 additions & 3 deletions pkg/share/manager/cs3/cs3.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ func (m *Manager) GetShare(ctx context.Context, ref *collaboration.ShareReferenc

// check if we are the owner or the grantee
user := ctxpkg.ContextMustGetUser(ctx)
if share.IsCreatedByUser(s, user) || share.IsGrantedToUser(s, user) {
if user.GetId().GetType() == userpb.UserType_USER_TYPE_SERVICE || share.IsCreatedByUser(s, user) || share.IsGrantedToUser(s, user) {
return s, nil
}

Expand Down Expand Up @@ -617,7 +617,7 @@ func (m *Manager) GetReceivedShare(ctx context.Context, ref *collaboration.Share
}

// UpdateReceivedShare updates the received share with share state.
func (m *Manager) UpdateReceivedShare(ctx context.Context, rshare *collaboration.ReceivedShare, fieldMask *field_mask.FieldMask) (*collaboration.ReceivedShare, error) {
func (m *Manager) UpdateReceivedShare(ctx context.Context, rshare *collaboration.ReceivedShare, fieldMask *field_mask.FieldMask, forUser *userpb.UserId) (*collaboration.ReceivedShare, error) {
if err := m.initialize(); err != nil {
return nil, err
}
Expand All @@ -643,7 +643,12 @@ func (m *Manager) UpdateReceivedShare(ctx context.Context, rshare *collaboration
}
}

err = m.persistReceivedShare(ctx, user.Id, rs)
uid := user.GetId()
if user.GetId().GetType() == userpb.UserType_USER_TYPE_SERVICE {
uid = forUser
}

err = m.persistReceivedShare(ctx, uid, rs)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/share/manager/cs3/cs3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ var _ = Describe("Manager", func() {
rs.MountPoint.Path = "newPath/"

rrs, err := m.UpdateReceivedShare(granteeCtx,
rs, &fieldmaskpb.FieldMask{Paths: []string{"state", "mount_point"}})
rs, &fieldmaskpb.FieldMask{Paths: []string{"state", "mount_point"}}, nil)
Expect(err).ToNot(HaveOccurred())
Expect(rrs).ToNot(BeNil())
Expect(rrs.Share.ResourceId).ToNot(BeNil())
Expand All @@ -630,7 +630,7 @@ var _ = Describe("Manager", func() {
rs.MountPoint.Path = "newPath/"

rrs, err := m.UpdateReceivedShare(granteeCtx,
rs, &fieldmaskpb.FieldMask{Paths: []string{"mount_point"}})
rs, &fieldmaskpb.FieldMask{Paths: []string{"mount_point"}}, nil)
Expect(err).ToNot(HaveOccurred())
Expect(rrs).ToNot(BeNil())
Expect(rrs.Share.ResourceId).ToNot(BeNil())
Expand Down
22 changes: 13 additions & 9 deletions pkg/share/manager/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,13 +542,13 @@ func (m *mgr) getReceived(ctx context.Context, ref *collaboration.ShareReference
return nil, err
}
user := ctxpkg.ContextMustGetUser(ctx)
if !share.IsGrantedToUser(s, user) {
if user.GetId().GetType() != userv1beta1.UserType_USER_TYPE_SERVICE && !share.IsGrantedToUser(s, user) {
return nil, errtypes.NotFound(ref.String())
}
return m.convert(user.Id, s), nil
}

func (m *mgr) UpdateReceivedShare(ctx context.Context, receivedShare *collaboration.ReceivedShare, fieldMask *field_mask.FieldMask) (*collaboration.ReceivedShare, error) {
func (m *mgr) UpdateReceivedShare(ctx context.Context, receivedShare *collaboration.ReceivedShare, fieldMask *field_mask.FieldMask, forUser *userv1beta1.UserId) (*collaboration.ReceivedShare, error) {
rs, err := m.getReceived(ctx, &collaboration.ShareReference{Spec: &collaboration.ShareReference_Id{Id: receivedShare.Share.Id}})
if err != nil {
return nil, err
Expand All @@ -568,27 +568,31 @@ func (m *mgr) UpdateReceivedShare(ctx context.Context, receivedShare *collaborat
}
}

user := ctxpkg.ContextMustGetUser(ctx)
u := ctxpkg.ContextMustGetUser(ctx)
uid := u.GetId().String()
if u.GetId().GetType() == userv1beta1.UserType_USER_TYPE_SERVICE {
uid = forUser.String()
}
// Persist state
if v, ok := m.model.State[user.Id.String()]; ok {
if v, ok := m.model.State[uid]; ok {
v[rs.Share.Id.String()] = rs.State
m.model.State[user.Id.String()] = v
m.model.State[uid] = v
} else {
a := map[string]collaboration.ShareState{
rs.Share.Id.String(): rs.State,
}
m.model.State[user.Id.String()] = a
m.model.State[uid] = a
}

// Persist mount point
if v, ok := m.model.MountPoint[user.Id.String()]; ok {
if v, ok := m.model.MountPoint[uid]; ok {
v[rs.Share.Id.String()] = rs.MountPoint
m.model.MountPoint[user.Id.String()] = v
m.model.MountPoint[uid] = v
} else {
a := map[string]*provider.Reference{
rs.Share.Id.String(): rs.MountPoint,
}
m.model.MountPoint[user.Id.String()] = a
m.model.MountPoint[uid] = a
}

if err := m.model.Save(); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/share/manager/json/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ var _ = Describe("Json", func() {
rs.MountPoint = &providerv1beta1.Reference{Path: "newPath/"}

_, err = m.UpdateReceivedShare(granteeCtx,
rs, &fieldmaskpb.FieldMask{Paths: []string{"state", "mount_point"}})
rs, &fieldmaskpb.FieldMask{Paths: []string{"state", "mount_point"}}, nil)
Expect(err).ToNot(HaveOccurred())
})

Expand Down
13 changes: 8 additions & 5 deletions pkg/share/manager/jsoncs3/jsoncs3.go
Original file line number Diff line number Diff line change
Expand Up @@ -955,7 +955,7 @@ func (m *Manager) getReceived(ctx context.Context, ref *collaboration.ShareRefer
return nil, err
}
user := ctxpkg.ContextMustGetUser(ctx)
if !share.IsGrantedToUser(s, user) {
if user.GetId().GetType() != userv1beta1.UserType_USER_TYPE_SERVICE && !share.IsGrantedToUser(s, user) {
return nil, errtypes.NotFound(ref.String())
}
if share.IsExpired(s) {
Expand All @@ -978,7 +978,7 @@ func (m *Manager) getReceived(ctx context.Context, ref *collaboration.ShareRefer
}

// UpdateReceivedShare updates the received share with share state.
func (m *Manager) UpdateReceivedShare(ctx context.Context, receivedShare *collaboration.ReceivedShare, fieldMask *field_mask.FieldMask) (*collaboration.ReceivedShare, error) {
func (m *Manager) UpdateReceivedShare(ctx context.Context, receivedShare *collaboration.ReceivedShare, fieldMask *field_mask.FieldMask, forUser *userv1beta1.UserId) (*collaboration.ReceivedShare, error) {
ctx, span := appctx.GetTracerProvider(ctx).Tracer(tracerName).Start(ctx, "UpdateReceivedShare")
defer span.End()

Expand All @@ -1003,10 +1003,13 @@ func (m *Manager) UpdateReceivedShare(ctx context.Context, receivedShare *collab
}

// write back
u := ctxpkg.ContextMustGetUser(ctx)
uid := u.GetId().GetOpaqueId()
if u.GetId().GetType() == userv1beta1.UserType_USER_TYPE_SERVICE {
uid = forUser.GetOpaqueId()
}

userID := ctxpkg.ContextMustGetUser(ctx)

err = m.UserReceivedStates.Add(ctx, userID.GetId().GetOpaqueId(), rs.Share.ResourceId.StorageId+shareid.IDDelimiter+rs.Share.ResourceId.SpaceId, rs)
err = m.UserReceivedStates.Add(ctx, uid, rs.Share.ResourceId.StorageId+shareid.IDDelimiter+rs.Share.ResourceId.SpaceId, rs)
if err != nil {
return nil, err
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/share/manager/jsoncs3/jsoncs3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ var _ = Describe("Jsoncs3", func() {
Expect(err).ToNot(HaveOccurred())

rs.State = collaboration.ShareState_SHARE_STATE_ACCEPTED
_, err = m.UpdateReceivedShare(granteeCtx, rs, &fieldmaskpb.FieldMask{Paths: []string{"state"}})
_, err = m.UpdateReceivedShare(granteeCtx, rs, &fieldmaskpb.FieldMask{Paths: []string{"state"}}, nil)
Expect(err).ToNot(HaveOccurred())

received, err := m.ListReceivedShares(granteeCtx, []*collaboration.Filter{})
Expand Down Expand Up @@ -921,7 +921,7 @@ var _ = Describe("Jsoncs3", func() {
Expect(rs.State).To(Equal(collaboration.ShareState_SHARE_STATE_PENDING))

rs.State = collaboration.ShareState_SHARE_STATE_ACCEPTED
rs, err = m.UpdateReceivedShare(granteeCtx, rs, &fieldmaskpb.FieldMask{Paths: []string{"state"}})
rs, err = m.UpdateReceivedShare(granteeCtx, rs, &fieldmaskpb.FieldMask{Paths: []string{"state"}}, nil)
Expect(err).ToNot(HaveOccurred())
Expect(rs.State).To(Equal(collaboration.ShareState_SHARE_STATE_ACCEPTED))

Expand All @@ -946,7 +946,7 @@ var _ = Describe("Jsoncs3", func() {
rs.MountPoint = &providerv1beta1.Reference{
Path: "newMP",
}
rs, err = m.UpdateReceivedShare(granteeCtx, rs, &fieldmaskpb.FieldMask{Paths: []string{"mount_point"}})
rs, err = m.UpdateReceivedShare(granteeCtx, rs, &fieldmaskpb.FieldMask{Paths: []string{"mount_point"}}, nil)
Expect(err).ToNot(HaveOccurred())
Expect(rs.MountPoint.Path).To(Equal("newMP"))

Expand All @@ -967,7 +967,7 @@ var _ = Describe("Jsoncs3", func() {
})
Expect(err).ToNot(HaveOccurred())

_, err = m.UpdateReceivedShare(granteeCtx, rs, &fieldmaskpb.FieldMask{Paths: []string{"invalid"}})
_, err = m.UpdateReceivedShare(granteeCtx, rs, &fieldmaskpb.FieldMask{Paths: []string{"invalid"}}, nil)
Expect(err).To(HaveOccurred())
})

Expand All @@ -992,7 +992,7 @@ var _ = Describe("Jsoncs3", func() {
Expect(rs.State).To(Equal(collaboration.ShareState_SHARE_STATE_PENDING))

rs.State = collaboration.ShareState_SHARE_STATE_ACCEPTED
rs, err = m.UpdateReceivedShare(granteeCtx, rs, &fieldmaskpb.FieldMask{Paths: []string{"state"}})
rs, err = m.UpdateReceivedShare(granteeCtx, rs, &fieldmaskpb.FieldMask{Paths: []string{"state"}}, nil)
Expect(err).ToNot(HaveOccurred())
Expect(rs.State).To(Equal(collaboration.ShareState_SHARE_STATE_ACCEPTED))

Expand All @@ -1015,7 +1015,7 @@ var _ = Describe("Jsoncs3", func() {
Expect(rs.State).To(Equal(collaboration.ShareState_SHARE_STATE_PENDING))

rs.State = collaboration.ShareState_SHARE_STATE_ACCEPTED
rs, err = m.UpdateReceivedShare(granteeCtx, rs, &fieldmaskpb.FieldMask{Paths: []string{"state"}})
rs, err = m.UpdateReceivedShare(granteeCtx, rs, &fieldmaskpb.FieldMask{Paths: []string{"state"}}, nil)
Expect(err).ToNot(HaveOccurred())
Expect(rs.State).To(Equal(collaboration.ShareState_SHARE_STATE_ACCEPTED))

Expand Down
24 changes: 15 additions & 9 deletions pkg/share/manager/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/cs3org/reva/v2/pkg/share"
"google.golang.org/genproto/protobuf/field_mask"

userv1beta1 "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
collaboration "github.com/cs3org/go-cs3apis/cs3/sharing/collaboration/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
typespb "github.com/cs3org/go-cs3apis/cs3/types/v1beta1"
Expand Down Expand Up @@ -334,7 +335,7 @@ func (m *manager) getReceived(ctx context.Context, ref *collaboration.ShareRefer
user := ctxpkg.ContextMustGetUser(ctx)
for _, s := range m.shares {
if sharesEqual(ref, s) {
if share.IsGrantedToUser(s, user) {
if user.GetId().GetType() == userv1beta1.UserType_USER_TYPE_SERVICE || share.IsGrantedToUser(s, user) {
rs := m.convert(ctx, s)
return rs, nil
}
Expand All @@ -343,13 +344,12 @@ func (m *manager) getReceived(ctx context.Context, ref *collaboration.ShareRefer
return nil, errtypes.NotFound(ref.String())
}

func (m *manager) UpdateReceivedShare(ctx context.Context, receivedShare *collaboration.ReceivedShare, fieldMask *field_mask.FieldMask) (*collaboration.ReceivedShare, error) {
func (m *manager) UpdateReceivedShare(ctx context.Context, receivedShare *collaboration.ReceivedShare, fieldMask *field_mask.FieldMask, forUser *userv1beta1.UserId) (*collaboration.ReceivedShare, error) {
rs, err := m.getReceived(ctx, &collaboration.ShareReference{Spec: &collaboration.ShareReference_Id{Id: receivedShare.Share.Id}})
if err != nil {
return nil, err
}

user := ctxpkg.ContextMustGetUser(ctx)
m.lock.Lock()
defer m.lock.Unlock()

Expand All @@ -364,25 +364,31 @@ func (m *manager) UpdateReceivedShare(ctx context.Context, receivedShare *collab
}
}

u := ctxpkg.ContextMustGetUser(ctx)
uid := u.GetId().String()
if u.GetId().GetType() == userv1beta1.UserType_USER_TYPE_SERVICE {
uid = forUser.String()
}

// Persist state
if v, ok := m.shareState[user.Id.String()]; ok {
if v, ok := m.shareState[uid]; ok {
v[rs.Share.Id] = rs.State
m.shareState[user.Id.String()] = v
m.shareState[uid] = v
} else {
a := map[*collaboration.ShareId]collaboration.ShareState{
rs.Share.Id: rs.State,
}
m.shareState[user.Id.String()] = a
m.shareState[uid] = a
}
// Persist mount point
if v, ok := m.shareMountPoint[user.Id.String()]; ok {
if v, ok := m.shareMountPoint[uid]; ok {
v[rs.Share.Id] = rs.MountPoint
m.shareMountPoint[user.Id.String()] = v
m.shareMountPoint[uid] = v
} else {
a := map[*collaboration.ShareId]*provider.Reference{
rs.Share.Id: rs.MountPoint,
}
m.shareMountPoint[user.Id.String()] = a
m.shareMountPoint[uid] = a
}

return rs, nil
Expand Down
4 changes: 3 additions & 1 deletion pkg/share/manager/owncloudsql/owncloudsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"strings"
"time"

userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
collaboration "github.com/cs3org/go-cs3apis/cs3/sharing/collaboration/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
typespb "github.com/cs3org/go-cs3apis/cs3/types/v1beta1"
Expand Down Expand Up @@ -422,7 +423,8 @@ func (m *mgr) GetReceivedShare(ctx context.Context, ref *collaboration.ShareRefe

}

func (m *mgr) UpdateReceivedShare(ctx context.Context, receivedShare *collaboration.ReceivedShare, fieldMask *field_mask.FieldMask) (*collaboration.ReceivedShare, error) {
func (m *mgr) UpdateReceivedShare(ctx context.Context, receivedShare *collaboration.ReceivedShare, fieldMask *field_mask.FieldMask, _ *userpb.UserId) (*collaboration.ReceivedShare, error) {
// TODO: How to inject the uid when a UserId is set? override it in the ctx? Add parameter to GetReceivedShare?
rs, err := m.GetReceivedShare(ctx, &collaboration.ShareReference{Spec: &collaboration.ShareReference_Id{Id: receivedShare.Share.Id}})
if err != nil {
return nil, err
Expand Down
12 changes: 6 additions & 6 deletions pkg/share/manager/owncloudsql/owncloudsql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ var _ = Describe("SQL manager", func() {
Expect(share.State).To(Equal(collaboration.ShareState_SHARE_STATE_ACCEPTED))

share.State = collaboration.ShareState_SHARE_STATE_REJECTED
_, err = mgr.UpdateReceivedShare(ctx, share, &fieldmaskpb.FieldMask{Paths: []string{"foo"}})
_, err = mgr.UpdateReceivedShare(ctx, share, &fieldmaskpb.FieldMask{Paths: []string{"foo"}}, nil)
Expect(err).To(HaveOccurred())
})

Expand All @@ -386,12 +386,12 @@ var _ = Describe("SQL manager", func() {

share.State = collaboration.ShareState_SHARE_STATE_REJECTED

share, err = mgr.UpdateReceivedShare(ctx, share, &fieldmaskpb.FieldMask{Paths: []string{"mount_point"}})
share, err = mgr.UpdateReceivedShare(ctx, share, &fieldmaskpb.FieldMask{Paths: []string{"mount_point"}}, nil)
Expect(err).ToNot(HaveOccurred())
Expect(share.State).To(Equal(collaboration.ShareState_SHARE_STATE_ACCEPTED))

share.State = collaboration.ShareState_SHARE_STATE_REJECTED
share, err = mgr.UpdateReceivedShare(ctx, share, &fieldmaskpb.FieldMask{Paths: []string{"state"}})
share, err = mgr.UpdateReceivedShare(ctx, share, &fieldmaskpb.FieldMask{Paths: []string{"state"}}, nil)
Expect(err).ToNot(HaveOccurred())
Expect(share.State).To(Equal(collaboration.ShareState_SHARE_STATE_REJECTED))

Expand All @@ -411,12 +411,12 @@ var _ = Describe("SQL manager", func() {

share.MountPoint = &provider.Reference{Path: "foo"}

share, err = mgr.UpdateReceivedShare(ctx, share, &fieldmaskpb.FieldMask{Paths: []string{"state"}})
share, err = mgr.UpdateReceivedShare(ctx, share, &fieldmaskpb.FieldMask{Paths: []string{"state"}}, nil)
Expect(err).ToNot(HaveOccurred())
Expect(share.MountPoint.Path).To(Equal("shared"))

share.MountPoint = &provider.Reference{Path: "foo"}
share, err = mgr.UpdateReceivedShare(ctx, share, &fieldmaskpb.FieldMask{Paths: []string{"mount_point"}})
share, err = mgr.UpdateReceivedShare(ctx, share, &fieldmaskpb.FieldMask{Paths: []string{"mount_point"}}, nil)
Expect(err).ToNot(HaveOccurred())
Expect(share.MountPoint.Path).To(Equal("foo"))

Expand Down Expand Up @@ -465,7 +465,7 @@ var _ = Describe("SQL manager", func() {
share.MountPoint = &provider.Reference{Path: "foo"}

By("overriding the child share information for the current user")
share, err = mgr.UpdateReceivedShare(ctx, share, &fieldmaskpb.FieldMask{Paths: []string{"mount_point"}})
share, err = mgr.UpdateReceivedShare(ctx, share, &fieldmaskpb.FieldMask{Paths: []string{"mount_point"}}, nil)
Expect(err).ToNot(HaveOccurred())
Expect(share.MountPoint.Path).To(Equal("foo"))

Expand Down
Loading

0 comments on commit 2ebd3e9

Please sign in to comment.