mirror of
https://github.com/minio/minio.git
synced 2025-02-10 05:08:10 -05:00
fix: groups lookup performance issue with users with lots of groups (#20740)
fixes https://github.com/minio/minio/issues/20717
This commit is contained in:
parent
d56ef8dbe1
commit
7b3eb9f7f8
@ -26,6 +26,7 @@ import (
|
|||||||
"path"
|
"path"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
jsoniter "github.com/json-iterator/go"
|
jsoniter "github.com/json-iterator/go"
|
||||||
@ -37,6 +38,7 @@ import (
|
|||||||
"github.com/minio/minio/internal/jwt"
|
"github.com/minio/minio/internal/jwt"
|
||||||
"github.com/minio/pkg/v3/env"
|
"github.com/minio/pkg/v3/env"
|
||||||
"github.com/minio/pkg/v3/policy"
|
"github.com/minio/pkg/v3/policy"
|
||||||
|
"github.com/minio/pkg/v3/sync/errgroup"
|
||||||
"github.com/puzpuzpuz/xsync/v3"
|
"github.com/puzpuzpuz/xsync/v3"
|
||||||
"golang.org/x/sync/singleflight"
|
"golang.org/x/sync/singleflight"
|
||||||
)
|
)
|
||||||
@ -357,6 +359,68 @@ func (c *iamCache) removeGroupFromMembershipsMap(group string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *iamCache) policyDBGetGroups(store *IAMStoreSys, userPolicyPresent bool, groups ...string) ([]string, error) {
|
||||||
|
var policies []string
|
||||||
|
for _, group := range groups {
|
||||||
|
if store.getUsersSysType() == MinIOUsersSysType {
|
||||||
|
g, ok := c.iamGroupsMap[group]
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Group is disabled, so we return no policy - this
|
||||||
|
// ensures the request is denied.
|
||||||
|
if g.Status == statusDisabled {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
policy, ok := c.iamGroupPolicyMap.Load(group)
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
policies = append(policies, policy.toSlice()...)
|
||||||
|
}
|
||||||
|
|
||||||
|
found := len(policies) > 0
|
||||||
|
if found {
|
||||||
|
return policies, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if userPolicyPresent {
|
||||||
|
// if user mapping present and no group policies found
|
||||||
|
// rely on user policy for access, instead of fallback.
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var mu sync.Mutex
|
||||||
|
|
||||||
|
// no mappings found, fallback for all groups.
|
||||||
|
g := errgroup.WithNErrs(len(groups)).WithConcurrency(10) // load like 10 groups at a time.
|
||||||
|
|
||||||
|
for index := range groups {
|
||||||
|
index := index
|
||||||
|
g.Go(func() error {
|
||||||
|
err := store.loadMappedPolicy(context.TODO(), groups[index], regUser, true, c.iamGroupPolicyMap)
|
||||||
|
if err != nil && !errors.Is(err, errNoSuchPolicy) {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if errors.Is(err, errNoSuchPolicy) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
policy, _ := c.iamGroupPolicyMap.Load(groups[index])
|
||||||
|
mu.Lock()
|
||||||
|
policies = append(policies, policy.toSlice()...)
|
||||||
|
mu.Unlock()
|
||||||
|
return nil
|
||||||
|
}, index)
|
||||||
|
}
|
||||||
|
|
||||||
|
err := errors.Join(g.Wait()...)
|
||||||
|
return policies, err
|
||||||
|
}
|
||||||
|
|
||||||
// policyDBGet - lower-level helper; does not take locks.
|
// policyDBGet - lower-level helper; does not take locks.
|
||||||
//
|
//
|
||||||
// If a group is passed, it returns policies associated with the group.
|
// If a group is passed, it returns policies associated with the group.
|
||||||
@ -677,6 +741,7 @@ type IAMStoreSys struct {
|
|||||||
IAMStorageAPI
|
IAMStorageAPI
|
||||||
|
|
||||||
group *singleflight.Group
|
group *singleflight.Group
|
||||||
|
policy *singleflight.Group
|
||||||
}
|
}
|
||||||
|
|
||||||
// HasWatcher - returns if the storage system has a watcher.
|
// HasWatcher - returns if the storage system has a watcher.
|
||||||
@ -757,21 +822,32 @@ func (store *IAMStoreSys) PolicyDBGet(name string, groups ...string) ([]string,
|
|||||||
cache := store.rlock()
|
cache := store.rlock()
|
||||||
defer store.runlock()
|
defer store.runlock()
|
||||||
|
|
||||||
|
getPolicies := func() ([]string, error) {
|
||||||
policies, _, err := cache.policyDBGet(store, name, false, false)
|
policies, _, err := cache.policyDBGet(store, name, false, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
userPolicyPresent := len(policies) > 0
|
userPolicyPresent := len(policies) > 0
|
||||||
for _, group := range groups {
|
|
||||||
ps, _, err := cache.policyDBGet(store, group, true, userPolicyPresent)
|
groupPolicies, err := cache.policyDBGetGroups(store, userPolicyPresent, groups...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
policies = append(policies, ps...)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
policies = append(policies, groupPolicies...)
|
||||||
return policies, nil
|
return policies, nil
|
||||||
|
}
|
||||||
|
if store.policy != nil {
|
||||||
|
val, err, _ := store.policy.Do(name, func() (interface{}, error) {
|
||||||
|
return getPolicies()
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return val.([]string), nil
|
||||||
|
}
|
||||||
|
return getPolicies()
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddUsersToGroup - adds users to group, creating the group if needed.
|
// AddUsersToGroup - adds users to group, creating the group if needed.
|
||||||
|
@ -178,13 +178,18 @@ func (sys *IAMSys) initStore(objAPI ObjectLayer, etcdClient *etcd.Client) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if etcdClient == nil {
|
if etcdClient == nil {
|
||||||
var group *singleflight.Group
|
var (
|
||||||
|
group *singleflight.Group
|
||||||
|
policy *singleflight.Group
|
||||||
|
)
|
||||||
if env.Get("_MINIO_IAM_SINGLE_FLIGHT", config.EnableOn) == config.EnableOn {
|
if env.Get("_MINIO_IAM_SINGLE_FLIGHT", config.EnableOn) == config.EnableOn {
|
||||||
group = &singleflight.Group{}
|
group = &singleflight.Group{}
|
||||||
|
policy = &singleflight.Group{}
|
||||||
}
|
}
|
||||||
sys.store = &IAMStoreSys{
|
sys.store = &IAMStoreSys{
|
||||||
IAMStorageAPI: newIAMObjectStore(objAPI, sys.usersSysType),
|
IAMStorageAPI: newIAMObjectStore(objAPI, sys.usersSysType),
|
||||||
group: group,
|
group: group,
|
||||||
|
policy: policy,
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
sys.store = &IAMStoreSys{IAMStorageAPI: newIAMEtcdStore(etcdClient, sys.usersSysType)}
|
sys.store = &IAMStoreSys{IAMStorageAPI: newIAMEtcdStore(etcdClient, sys.usersSysType)}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user