1
0
mirror of https://github.com/rancher/steve.git synced 2025-04-30 04:03:46 +00:00
steve/pkg/accesscontrol/access_set.go

123 lines
2.3 KiB
Go
Raw Normal View History

2019-08-04 17:41:32 +00:00
package accesscontrol
import (
2019-09-11 21:05:00 +00:00
"github.com/rancher/steve/pkg/attributes"
2020-01-31 05:37:59 +00:00
"github.com/rancher/steve/pkg/schemaserver/types"
2019-08-04 17:41:32 +00:00
"k8s.io/apimachinery/pkg/runtime/schema"
)
type AccessSet struct {
2020-01-31 05:37:59 +00:00
set map[key]resourceAccessSet
2019-08-04 17:41:32 +00:00
}
2020-01-31 05:37:59 +00:00
type resourceAccessSet map[Access]bool
2019-08-04 17:41:32 +00:00
type key struct {
verb string
gr schema.GroupResource
}
func (a *AccessSet) Merge(right *AccessSet) {
for k, accessMap := range right.set {
m, ok := a.set[k]
if !ok {
m = map[Access]bool{}
if a.set == nil {
2020-01-31 05:37:59 +00:00
a.set = map[key]resourceAccessSet{}
2019-08-04 17:41:32 +00:00
}
a.set[k] = m
}
for k, v := range accessMap {
m[k] = v
}
}
}
2020-01-31 05:37:59 +00:00
func (a AccessSet) AccessListFor(verb string, gr schema.GroupResource) (result AccessList) {
2019-08-04 17:41:32 +00:00
for _, v := range []string{all, verb} {
for _, g := range []string{all, gr.Group} {
for _, r := range []string{all, gr.Resource} {
for k := range a.set[key{
verb: v,
gr: schema.GroupResource{
Group: g,
Resource: r,
},
}] {
result = append(result, k)
}
}
}
}
return
}
func (a *AccessSet) Add(verb string, gr schema.GroupResource, access Access) {
if a.set == nil {
2020-01-31 05:37:59 +00:00
a.set = map[key]resourceAccessSet{}
2019-08-04 17:41:32 +00:00
}
k := key{verb: verb, gr: gr}
if m, ok := a.set[k]; ok {
m[access] = true
} else {
m = map[Access]bool{}
m[access] = true
a.set[k] = m
}
}
2020-01-31 05:37:59 +00:00
type AccessListByVerb map[string]AccessList
2019-08-04 17:41:32 +00:00
2020-01-31 05:37:59 +00:00
func (a AccessListByVerb) Grants(verb, namespace, name string) bool {
2019-08-04 17:41:32 +00:00
return a[verb].Grants(namespace, name)
}
2020-01-31 05:37:59 +00:00
func (a AccessListByVerb) AnyVerb(verb ...string) bool {
2019-08-04 17:41:32 +00:00
for _, v := range verb {
if len(a[v]) > 0 {
return true
}
}
return false
}
type AccessList []Access
func (a AccessList) Grants(namespace, name string) bool {
for _, a := range a {
if a.Grants(namespace, name) {
return true
}
}
return false
}
type Access struct {
Namespace string
ResourceName string
}
func (a Access) Grants(namespace, name string) bool {
return a.nsOK(namespace) && a.nameOK(name)
}
func (a Access) nsOK(namespace string) bool {
return a.Namespace == all || a.Namespace == namespace
}
func (a Access) nameOK(name string) bool {
return a.ResourceName == all || a.ResourceName == name
}
2020-01-31 05:37:59 +00:00
func GetAccessListMap(s *types.APISchema) AccessListByVerb {
2019-08-12 23:47:23 +00:00
if s == nil {
return nil
}
2020-01-31 05:37:59 +00:00
v, _ := attributes.Access(s).(AccessListByVerb)
2019-08-04 17:41:32 +00:00
return v
}