refactor: [OCISDEV-1516] share the vault recipient filter
- Move the VaultMode recipient filter to ocis-pkg/vault as FilterVaultModeUsers. The notifications and userlog services both use it. - Run the checks in parallel, with at most 10 at the same time, so that a large group does not block the event handler. - Add a tracing span with the number of checked, allowed and failed users. - In the notifications service, check the vault permission before the settings and user lookups, so that a member without the permission costs one call only. - Log a failed check with the user ID only, without the "event" field. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f7790d7be5
commit
b5cc5cc415
5 files changed
+136
-46
No files matched your search
@@ -12,8 +12,17 @@ import (
|
||||
"github.com/owncloud/reva/v2/pkg/permission"
|
||||
"github.com/owncloud/reva/v2/pkg/storagespace"
|
||||
"github.com/owncloud/reva/v2/pkg/utils"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
"golang.org/x/sync/errgroup"
|
||||
)
|
||||
|
||||
// maxConcurrentChecks bounds how many VaultMode checks run at the same time, so that a large
|
||||
// group does not send all its checks to the gateway at once.
|
||||
const maxConcurrentChecks = 10
|
||||
|
||||
const tracerName = "github.com/owncloud/ocis/v2/ocis-pkg/vault"
|
||||
|
||||
// IsVaultStorage reports whether the storage ID belongs to the vault storage provider.
|
||||
func IsVaultStorage(storageID string) bool {
|
||||
return storageID == utils.VaultStorageProviderID
|
||||
@@ -50,3 +59,45 @@ func HasVaultMode(ctx context.Context, gwc gateway.GatewayAPIClient, userID *use
|
||||
return false, fmt.Errorf("vault permission check returned status %q: %s", code, res.GetStatus().GetMessage())
|
||||
}
|
||||
}
|
||||
|
||||
// FilterVaultModeUsers returns the users from userIDs who hold the VaultMode permission, in the
|
||||
// same order. A group can contain users without the permission, because the gateway does not
|
||||
// check the group members. The checks run in parallel, with a bound. When a check fails, the
|
||||
// function calls onError for that user and leaves the user out, so that the caller fails closed.
|
||||
func FilterVaultModeUsers(ctx context.Context, gwc gateway.GatewayAPIClient, userIDs []*user.UserId, onError func(*user.UserId, error)) []*user.UserId {
|
||||
ctx, span := trace.SpanFromContext(ctx).TracerProvider().Tracer(tracerName).Start(ctx, "FilterVaultModeUsers")
|
||||
defer span.End()
|
||||
span.SetAttributes(attribute.Int("vault.users.checked", len(userIDs)))
|
||||
|
||||
allowed := make([]bool, len(userIDs))
|
||||
errs := make([]error, len(userIDs))
|
||||
var eg errgroup.Group
|
||||
eg.SetLimit(maxConcurrentChecks)
|
||||
for i, id := range userIDs {
|
||||
eg.Go(func() error {
|
||||
allowed[i], errs[i] = HasVaultMode(ctx, gwc, id)
|
||||
return nil
|
||||
})
|
||||
}
|
||||
_ = eg.Wait()
|
||||
|
||||
filtered := make([]*user.UserId, 0, len(userIDs))
|
||||
failed := 0
|
||||
for i, id := range userIDs {
|
||||
if errs[i] != nil {
|
||||
failed++
|
||||
if onError != nil {
|
||||
onError(id, errs[i])
|
||||
}
|
||||
continue
|
||||
}
|
||||
if allowed[i] {
|
||||
filtered = append(filtered, id)
|
||||
}
|
||||
}
|
||||
span.SetAttributes(
|
||||
attribute.Int("vault.users.allowed", len(filtered)),
|
||||
attribute.Int("vault.users.failed", failed),
|
||||
)
|
||||
return filtered
|
||||
}
|
||||
@@ -3,6 +3,8 @@ package vault
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
user "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
|
||||
@@ -12,6 +14,7 @@ import (
|
||||
"github.com/owncloud/reva/v2/pkg/utils"
|
||||
cs3mocks "github.com/owncloud/reva/v2/tests/cs3mocks/mocks"
|
||||
"github.com/stretchr/testify/mock"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
func TestIsVaultSpace(t *testing.T) {
|
||||
@@ -64,3 +67,44 @@ func TestHasVaultMode(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterVaultModeUsers(t *testing.T) {
|
||||
// alice and every "ok-" user hold the permission, bob does not, and the check for carol fails
|
||||
gwc := &cs3mocks.GatewayAPIClient{}
|
||||
gwc.On("CheckPermission", mock.Anything, mock.Anything).Return(func(_ context.Context, r *permissions.CheckPermissionRequest, _ ...grpc.CallOption) (*permissions.CheckPermissionResponse, error) {
|
||||
switch id := r.GetSubjectRef().GetUserId().GetOpaqueId(); {
|
||||
case id == "carol":
|
||||
return nil, errors.New("unavailable")
|
||||
case id == "bob":
|
||||
return &permissions.CheckPermissionResponse{Status: &rpc.Status{Code: rpc.Code_CODE_PERMISSION_DENIED}}, nil
|
||||
default:
|
||||
return &permissions.CheckPermissionResponse{Status: &rpc.Status{Code: rpc.Code_CODE_OK}}, nil
|
||||
}
|
||||
})
|
||||
|
||||
ids := []*user.UserId{{OpaqueId: "alice"}, {OpaqueId: "bob"}, {OpaqueId: "carol"}}
|
||||
for i := range 50 {
|
||||
ids = append(ids, &user.UserId{OpaqueId: fmt.Sprintf("ok-%02d", i)})
|
||||
}
|
||||
|
||||
var failed []string
|
||||
got := FilterVaultModeUsers(context.Background(), gwc, ids, func(id *user.UserId, err error) {
|
||||
failed = append(failed, id.GetOpaqueId())
|
||||
})
|
||||
|
||||
want := []string{"alice"}
|
||||
for i := range 50 {
|
||||
want = append(want, fmt.Sprintf("ok-%02d", i))
|
||||
}
|
||||
gotIDs := make([]string, 0, len(got))
|
||||
for _, id := range got {
|
||||
gotIDs = append(gotIDs, id.GetOpaqueId())
|
||||
}
|
||||
if !slices.Equal(gotIDs, want) {
|
||||
t.Errorf("FilterVaultModeUsers() = %v, want %v", gotIDs, want)
|
||||
}
|
||||
if !slices.Equal(failed, []string{"carol"}) {
|
||||
t.Errorf("onError called for %v, want [carol]", failed)
|
||||
}
|
||||
gwc.AssertNumberOfCalls(t, "CheckPermission", len(ids))
|
||||
}
|
||||
@@ -200,49 +200,28 @@ func (s eventsNotifier) send(ctx context.Context, emails []*channels.Message) {
|
||||
// ensureGranteeList resolves the grantee to the users who get the notification.
|
||||
// For a vault resource it keeps only the users who hold the VaultMode permission.
|
||||
func (s eventsNotifier) ensureGranteeList(ctx context.Context, executant, u *user.UserId, g *group.GroupId, inVault bool) []*user.User {
|
||||
granteeList, err := s.getGranteeList(ctx, executant, u, g)
|
||||
granteeList, err := s.getGranteeList(ctx, executant, u, g, inVault)
|
||||
if err != nil {
|
||||
s.logger.Error().Err(err).Str("event", "ensureGranteeList").Msg("Could not get grantee list")
|
||||
return nil
|
||||
}
|
||||
if inVault {
|
||||
granteeList = s.filterVaultGrantees(ctx, granteeList)
|
||||
}
|
||||
if len(granteeList) == 0 {
|
||||
} else if len(granteeList) == 0 {
|
||||
return nil
|
||||
}
|
||||
return granteeList
|
||||
}
|
||||
|
||||
// filterVaultGrantees removes the users who do not hold the VaultMode permission.
|
||||
// A group can contain such users, because the gateway does not check the group members.
|
||||
func (s eventsNotifier) filterVaultGrantees(ctx context.Context, users []*user.User) []*user.User {
|
||||
if len(users) == 0 {
|
||||
return users
|
||||
}
|
||||
gatewayClient, err := s.gatewaySelector.Next()
|
||||
if err != nil {
|
||||
s.logger.Error().Err(err).Str("event", "filterVaultGrantees").Msg("could not select next gateway client")
|
||||
return nil
|
||||
}
|
||||
|
||||
filtered := make([]*user.User, 0, len(users))
|
||||
for _, u := range users {
|
||||
allowed, err := vault.HasVaultMode(ctx, gatewayClient, u.GetId())
|
||||
if err != nil {
|
||||
s.logger.Error().Err(err).Str("event", "filterVaultGrantees").Str("userId", u.GetId().GetOpaqueId()).Msg("could not check vault permission, skipped")
|
||||
continue
|
||||
}
|
||||
if allowed {
|
||||
filtered = append(filtered, u)
|
||||
}
|
||||
}
|
||||
return filtered
|
||||
}
|
||||
|
||||
func (s eventsNotifier) getGranteeList(ctx context.Context, executant, u *user.UserId, g *group.GroupId) ([]*user.User, error) {
|
||||
func (s eventsNotifier) getGranteeList(ctx context.Context, executant, u *user.UserId, g *group.GroupId, inVault bool) ([]*user.User, error) {
|
||||
switch {
|
||||
case u != nil:
|
||||
if inVault {
|
||||
gatewayClient, err := s.gatewaySelector.Next()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(s.filterVaultModeUsers(ctx, gatewayClient, []*user.UserId{u})) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
}
|
||||
if s.disableEmails(ctx, u) {
|
||||
return nil, nil
|
||||
}
|
||||
@@ -269,12 +248,21 @@ func (s eventsNotifier) getGranteeList(ctx context.Context, executant, u *user.U
|
||||
return nil, errors.New("could not get group")
|
||||
}
|
||||
|
||||
userList := make([]*user.User, 0, len(res.GetGroup().GetMembers()))
|
||||
members := make([]*user.UserId, 0, len(res.GetGroup().GetMembers()))
|
||||
for _, userID := range res.GetGroup().GetMembers() {
|
||||
// don't add the executant
|
||||
if userID.GetOpaqueId() == executant.GetOpaqueId() {
|
||||
continue
|
||||
}
|
||||
members = append(members, userID)
|
||||
}
|
||||
// check the vault permission first, so that members without it cost no further calls
|
||||
if inVault {
|
||||
members = s.filterVaultModeUsers(ctx, gatewayClient, members)
|
||||
}
|
||||
|
||||
userList := make([]*user.User, 0, len(members))
|
||||
for _, userID := range members {
|
||||
// don't add users who opted out
|
||||
if s.disableEmails(ctx, userID) {
|
||||
continue
|
||||
@@ -295,6 +283,13 @@ func (s eventsNotifier) getGranteeList(ctx context.Context, executant, u *user.U
|
||||
}
|
||||
}
|
||||
|
||||
// filterVaultModeUsers keeps only the users who hold the VaultMode permission.
|
||||
func (s eventsNotifier) filterVaultModeUsers(ctx context.Context, gatewayClient gateway.GatewayAPIClient, userIDs []*user.UserId) []*user.UserId {
|
||||
return vault.FilterVaultModeUsers(ctx, gatewayClient, userIDs, func(id *user.UserId, err error) {
|
||||
s.logger.Error().Err(err).Str("userId", id.GetOpaqueId()).Msg("could not check the vault permission, user skipped")
|
||||
})
|
||||
}
|
||||
|
||||
func (s eventsNotifier) getUser(ctx context.Context, u *user.UserId) (*user.User, error) {
|
||||
if u == nil {
|
||||
return nil, errors.New("need at least one non-nil grantee")
|
||||
|
||||
@@ -248,19 +248,19 @@ func isVaultEvent(event events.Event) bool {
|
||||
}
|
||||
}
|
||||
|
||||
// filterVaultUsers removes the users who do not hold the VaultMode permission.
|
||||
// A group can contain such users, because the gateway does not check the group members.
|
||||
// filterVaultUsers keeps only the users who hold the VaultMode permission.
|
||||
func (ul *UserlogService) filterVaultUsers(ctx context.Context, gwc gateway.GatewayAPIClient, users []string) []string {
|
||||
filtered := make([]string, 0, len(users))
|
||||
ids := make([]*user.UserId, 0, len(users))
|
||||
for _, u := range users {
|
||||
allowed, err := vault.HasVaultMode(ctx, gwc, &user.UserId{OpaqueId: u})
|
||||
if err != nil {
|
||||
ul.log.Error().Err(err).Str("userId", u).Msg("could not check vault permission, skipped")
|
||||
continue
|
||||
}
|
||||
if allowed {
|
||||
filtered = append(filtered, u)
|
||||
}
|
||||
ids = append(ids, &user.UserId{OpaqueId: u})
|
||||
}
|
||||
allowed := vault.FilterVaultModeUsers(ctx, gwc, ids, func(id *user.UserId, err error) {
|
||||
ul.log.Error().Err(err).Str("userId", id.GetOpaqueId()).Msg("could not check the vault permission, user skipped")
|
||||
})
|
||||
|
||||
filtered := make([]string, 0, len(allowed))
|
||||
for _, id := range allowed {
|
||||
filtered = append(filtered, id.GetOpaqueId())
|
||||
}
|
||||
return filtered
|
||||
}
|
||||
|
||||
@@ -16,8 +16,8 @@ import (
|
||||
. "github.com/onsi/gomega"
|
||||
"github.com/owncloud/reva/v2/pkg/events"
|
||||
"github.com/owncloud/reva/v2/pkg/rgrpc/todo/pool"
|
||||
"github.com/owncloud/reva/v2/pkg/store"
|
||||
"github.com/owncloud/reva/v2/pkg/storagespace"
|
||||
"github.com/owncloud/reva/v2/pkg/store"
|
||||
"github.com/owncloud/reva/v2/pkg/utils"
|
||||
cs3mocks "github.com/owncloud/reva/v2/tests/cs3mocks/mocks"
|
||||
"github.com/stretchr/testify/mock"
|
||||
|
||||
Reference in new issue
Block a user