2019-08-04 17:41:32 +00:00
|
|
|
package accesscontrol
|
|
|
|
|
|
|
|
import (
|
2020-02-08 20:03:57 +00:00
|
|
|
"context"
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
2024-08-28 08:06:21 +00:00
|
|
|
"hash"
|
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"
|
2024-08-28 08:06:21 +00:00
|
|
|
rbacv1 "k8s.io/api/rbac/v1"
|
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 {
|
|
|
|
get(string) *AccessSet
|
|
|
|
getRoleBindings(string) []*rbacv1.RoleBinding
|
|
|
|
getClusterRoleBindings(string) []*rbacv1.ClusterRoleBinding
|
|
|
|
}
|
|
|
|
|
|
|
|
type roleRevisions interface {
|
|
|
|
roleRevision(string, string) string
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
roles roleRevisions
|
|
|
|
cache accessStoreCache
|
|
|
|
concurrentAccessFor *singleflight.Group
|
2020-02-08 20:03:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type roleKey struct {
|
|
|
|
namespace string
|
|
|
|
name string
|
2019-08-04 17:41:32 +00:00
|
|
|
}
|
|
|
|
|
2020-02-08 20:03:57 +00:00
|
|
|
func NewAccessStore(ctx context.Context, cacheResults bool, rbac v1.Interface) *AccessStore {
|
|
|
|
as := &AccessStore{
|
2024-10-09 08:21:29 +00:00
|
|
|
usersPolicyRules: newPolicyRuleIndex(true, rbac),
|
|
|
|
groupsPolicyRules: newPolicyRuleIndex(false, rbac),
|
|
|
|
roles: newRoleRevision(ctx, 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-08 15:18:44 +00:00
|
|
|
if l.cache == nil {
|
|
|
|
return l.newAccessSet(user)
|
2020-02-08 20:03:57 +00:00
|
|
|
}
|
|
|
|
|
2024-10-08 15:18:44 +00:00
|
|
|
cacheKey := l.CacheKey(user)
|
|
|
|
|
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-09 08:21:29 +00:00
|
|
|
result := l.newAccessSet(user)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (l *AccessStore) newAccessSet(user user.Info) *AccessSet {
|
2024-08-28 08:06:21 +00:00
|
|
|
result := l.usersPolicyRules.get(user.GetName())
|
2019-08-04 17:41:32 +00:00
|
|
|
for _, group := range user.GetGroups() {
|
2024-08-28 08:06:21 +00:00
|
|
|
result.Merge(l.groupsPolicyRules.get(group))
|
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)
|
|
|
|
}
|
|
|
|
|
2020-02-08 20:03:57 +00:00
|
|
|
func (l *AccessStore) CacheKey(user user.Info) string {
|
2020-03-02 05:23:36 +00:00
|
|
|
d := sha256.New()
|
2020-02-08 20:03:57 +00:00
|
|
|
|
2020-03-02 05:23:36 +00:00
|
|
|
groupBase := user.GetGroups()
|
2024-07-18 07:54:14 +00:00
|
|
|
groups := make([]string, len(groupBase))
|
2020-03-02 05:23:36 +00:00
|
|
|
copy(groups, groupBase)
|
|
|
|
sort.Strings(groups)
|
2024-07-18 07:54:14 +00:00
|
|
|
|
2024-08-28 08:06:21 +00:00
|
|
|
l.addRolesToHash(d, user.GetName(), l.usersPolicyRules)
|
2024-07-18 07:54:14 +00:00
|
|
|
for _, group := range groups {
|
2024-08-28 08:06:21 +00:00
|
|
|
l.addRolesToHash(d, group, l.groupsPolicyRules)
|
2020-02-08 20:03:57 +00:00
|
|
|
}
|
2020-03-02 05:23:36 +00:00
|
|
|
|
2021-08-10 21:02:22 +00:00
|
|
|
return hex.EncodeToString(d.Sum(nil))
|
2020-02-08 20:03:57 +00:00
|
|
|
}
|
2024-08-28 08:06:21 +00:00
|
|
|
|
|
|
|
func (l *AccessStore) addRolesToHash(digest hash.Hash, subjectName string, rules policyRules) {
|
|
|
|
for _, crb := range rules.getClusterRoleBindings(subjectName) {
|
|
|
|
digest.Write([]byte(crb.RoleRef.Name))
|
|
|
|
digest.Write([]byte(l.roles.roleRevision("", crb.RoleRef.Name)))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, rb := range rules.getRoleBindings(subjectName) {
|
|
|
|
digest.Write([]byte(rb.RoleRef.Name))
|
|
|
|
if rb.Namespace != "" {
|
|
|
|
digest.Write([]byte(rb.Namespace))
|
|
|
|
}
|
|
|
|
digest.Write([]byte(l.roles.roleRevision(rb.Namespace, rb.RoleRef.Name)))
|
|
|
|
}
|
|
|
|
}
|