1
0
mirror of https://github.com/rancher/steve.git synced 2025-07-05 19:16:38 +00:00
steve/pkg/accesscontrol/access_store.go

116 lines
2.7 KiB
Go
Raw Normal View History

2019-08-04 17:41:32 +00:00
package accesscontrol
import (
2020-02-08 20:03:57 +00:00
"context"
"crypto/sha256"
"encoding/hex"
"hash"
2020-02-08 20:03:57 +00:00
"sort"
"time"
v1 "github.com/rancher/wrangler/v3/pkg/generated/controllers/rbac/v1"
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"
)
//go:generate mockgen --build_flags=--mod=mod -package fake -destination fake/AccessSetLookup.go "github.com/rancher/steve/pkg/accesscontrol" AccessSetLookup
type AccessSetLookup interface {
AccessFor(user user.Info) *AccessSet
PurgeUserData(id string)
}
type policyRules interface {
get(string) *AccessSet
getRoleBindings(string) []*rbacv1.RoleBinding
getClusterRoleBindings(string) []*rbacv1.ClusterRoleBinding
}
type roleRevisions interface {
roleRevision(string, string) string
}
2019-08-04 17:41:32 +00:00
type AccessStore struct {
usersPolicyRules policyRules
groupsPolicyRules policyRules
roles roleRevisions
cache *cache.LRUExpireCache
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{
usersPolicyRules: newPolicyRuleIndex(true, rbac),
groupsPolicyRules: newPolicyRuleIndex(false, rbac),
roles: newRoleRevision(ctx, rbac),
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 {
2020-02-08 20:03:57 +00:00
var cacheKey string
if l.cache != nil {
cacheKey = l.CacheKey(user)
val, ok := l.cache.Get(cacheKey)
if ok {
as, _ := val.(*AccessSet)
return as
}
}
result := l.usersPolicyRules.get(user.GetName())
2019-08-04 17:41:32 +00:00
for _, group := range user.GetGroups() {
result.Merge(l.groupsPolicyRules.get(group))
2019-08-04 17:41:32 +00:00
}
2020-02-08 20:03:57 +00:00
if l.cache != nil {
result.ID = cacheKey
l.cache.Add(cacheKey, result, 24*time.Hour)
}
2019-08-04 17:41:32 +00:00
return result
}
2020-02-08 20:03: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()
groups := make([]string, len(groupBase))
2020-03-02 05:23:36 +00:00
copy(groups, groupBase)
sort.Strings(groups)
l.addRolesToHash(d, user.GetName(), l.usersPolicyRules)
for _, group := range groups {
l.addRolesToHash(d, group, l.groupsPolicyRules)
2020-02-08 20:03:57 +00:00
}
2020-03-02 05:23:36 +00:00
return hex.EncodeToString(d.Sum(nil))
2020-02-08 20:03:57 +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)))
}
}