2019-08-04 17:41:32 +00:00
|
|
|
package accesscontrol
|
|
|
|
|
|
|
|
import (
|
2020-02-08 20:03:57 +00:00
|
|
|
"context"
|
2024-10-28 08:35:59 +00:00
|
|
|
"slices"
|
2020-02-08 20:03:57 +00:00
|
|
|
"sort"
|
|
|
|
"time"
|
|
|
|
|
2024-06-04 18:52:48 +00:00
|
|
|
v1 "github.com/rancher/wrangler/v3/pkg/generated/controllers/rbac/v1"
|
2024-10-09 08:21:29 +00:00
|
|
|
"golang.org/x/sync/singleflight"
|
2020-02-08 20:03:57 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/cache"
|
2019-08-04 17:41:32 +00:00
|
|
|
"k8s.io/apiserver/pkg/authentication/user"
|
|
|
|
)
|
|
|
|
|
2022-10-20 04:16:01 +00:00
|
|
|
//go:generate mockgen --build_flags=--mod=mod -package fake -destination fake/AccessSetLookup.go "github.com/rancher/steve/pkg/accesscontrol" AccessSetLookup
|
|
|
|
|
2020-02-04 21:11:21 +00:00
|
|
|
type AccessSetLookup interface {
|
|
|
|
AccessFor(user user.Info) *AccessSet
|
2022-08-26 14:27:57 +00:00
|
|
|
PurgeUserData(id string)
|
2020-02-04 21:11:21 +00:00
|
|
|
}
|
|
|
|
|
2024-08-28 08:06:21 +00:00
|
|
|
type policyRules interface {
|
2024-10-28 08:35:59 +00:00
|
|
|
getRoleRefs(subjectName string) subjectGrants
|
2024-08-28 08:06:21 +00:00
|
|
|
}
|
|
|
|
|
2024-10-08 15:18:44 +00:00
|
|
|
// accessStoreCache is a subset of the methods implemented by LRUExpireCache
|
|
|
|
type accessStoreCache interface {
|
|
|
|
Add(key interface{}, value interface{}, ttl time.Duration)
|
|
|
|
Get(key interface{}) (interface{}, bool)
|
|
|
|
Remove(key interface{})
|
|
|
|
}
|
|
|
|
|
2019-08-04 17:41:32 +00:00
|
|
|
type AccessStore struct {
|
2024-10-09 08:21:29 +00:00
|
|
|
usersPolicyRules policyRules
|
|
|
|
groupsPolicyRules policyRules
|
|
|
|
cache accessStoreCache
|
|
|
|
concurrentAccessFor *singleflight.Group
|
2020-02-08 20:03:57 +00:00
|
|
|
}
|
|
|
|
|
2024-10-28 08:35:59 +00:00
|
|
|
func NewAccessStore(_ context.Context, cacheResults bool, rbac v1.Interface) *AccessStore {
|
2020-02-08 20:03:57 +00:00
|
|
|
as := &AccessStore{
|
2024-10-09 08:21:29 +00:00
|
|
|
usersPolicyRules: newPolicyRuleIndex(true, rbac),
|
|
|
|
groupsPolicyRules: newPolicyRuleIndex(false, rbac),
|
|
|
|
concurrentAccessFor: new(singleflight.Group),
|
2019-08-04 17:41:32 +00:00
|
|
|
}
|
2020-02-08 20:03:57 +00:00
|
|
|
if cacheResults {
|
2020-03-02 05:23:36 +00:00
|
|
|
as.cache = cache.NewLRUExpireCache(50)
|
2020-02-08 20:03:57 +00:00
|
|
|
}
|
|
|
|
return as
|
|
|
|
}
|
|
|
|
|
2019-08-04 17:41:32 +00:00
|
|
|
func (l *AccessStore) AccessFor(user user.Info) *AccessSet {
|
2024-10-28 08:35:59 +00:00
|
|
|
info := l.userGrantsFor(user)
|
2024-10-08 15:18:44 +00:00
|
|
|
if l.cache == nil {
|
2024-10-28 08:35:59 +00:00
|
|
|
return l.newAccessSet(info)
|
2020-02-08 20:03:57 +00:00
|
|
|
}
|
|
|
|
|
2024-10-28 08:35:59 +00:00
|
|
|
cacheKey := info.hash()
|
2024-10-08 15:18:44 +00:00
|
|
|
|
2024-10-09 08:21:29 +00:00
|
|
|
res, _, _ := l.concurrentAccessFor.Do(cacheKey, func() (interface{}, error) {
|
|
|
|
if val, ok := l.cache.Get(cacheKey); ok {
|
|
|
|
as, _ := val.(*AccessSet)
|
|
|
|
return as, nil
|
|
|
|
}
|
2024-10-08 15:18:44 +00:00
|
|
|
|
2024-10-28 08:35:59 +00:00
|
|
|
result := l.newAccessSet(info)
|
2024-10-09 08:21:29 +00:00
|
|
|
result.ID = cacheKey
|
|
|
|
l.cache.Add(cacheKey, result, 24*time.Hour)
|
2024-10-08 15:18:44 +00:00
|
|
|
|
2024-10-09 08:21:29 +00:00
|
|
|
return result, nil
|
|
|
|
})
|
|
|
|
return res.(*AccessSet)
|
2024-10-08 15:18:44 +00:00
|
|
|
}
|
|
|
|
|
2024-10-28 08:35:59 +00:00
|
|
|
func (l *AccessStore) newAccessSet(info userGrants) *AccessSet {
|
|
|
|
result := info.user.toAccessSet()
|
|
|
|
for _, group := range info.groups {
|
|
|
|
result.Merge(group.toAccessSet())
|
2019-08-04 17:41:32 +00:00
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
2020-02-08 20:03:57 +00:00
|
|
|
|
2022-08-26 14:27:57 +00:00
|
|
|
func (l *AccessStore) PurgeUserData(id string) {
|
|
|
|
l.cache.Remove(id)
|
|
|
|
}
|
|
|
|
|
2024-10-28 08:35:59 +00:00
|
|
|
// userGrantsFor retrieves all the access information for a user
|
|
|
|
func (l *AccessStore) userGrantsFor(user user.Info) userGrants {
|
|
|
|
var res userGrants
|
2020-02-08 20:03:57 +00:00
|
|
|
|
2024-10-28 08:35:59 +00:00
|
|
|
groups := slices.Clone(user.GetGroups())
|
2020-03-02 05:23:36 +00:00
|
|
|
sort.Strings(groups)
|
2024-07-18 07:54:14 +00:00
|
|
|
|
2024-10-28 08:35:59 +00:00
|
|
|
res.user = l.usersPolicyRules.getRoleRefs(user.GetName())
|
2024-07-18 07:54:14 +00:00
|
|
|
for _, group := range groups {
|
2024-10-28 08:35:59 +00:00
|
|
|
res.groups = append(res.groups, l.groupsPolicyRules.getRoleRefs(group))
|
2024-08-28 08:06:21 +00:00
|
|
|
}
|
|
|
|
|
2024-10-28 08:35:59 +00:00
|
|
|
return res
|
2024-08-28 08:06:21 +00:00
|
|
|
}
|