vitess.io/vitess@v0.16.2/go/vt/vtadmin/rbac/rule.go (about) 1 /* 2 Copyright 2021 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package rbac 18 19 import ( 20 "fmt" 21 22 "k8s.io/apimachinery/pkg/util/sets" 23 ) 24 25 // Rule is a single rule governing access to a particular resource. 26 type Rule struct { 27 clusters sets.Set[string] 28 actions sets.Set[string] 29 subjects sets.Set[string] 30 } 31 32 // Allows returns true if the actor is allowed to take the specified action in 33 // the specified cluster. 34 // 35 // A nil actor signifies the unauthenticated state, and is only allowed access 36 // if the rule contains the wildcard ("*") subject. 37 func (r *Rule) Allows(clusterID string, action Action, actor *Actor) bool { 38 if r.clusters.HasAny("*", clusterID) { 39 if r.actions.HasAny("*", string(action)) { 40 if r.subjects.Has("*") { 41 return true 42 } 43 44 if actor == nil { 45 return false 46 } 47 48 if r.subjects.Has(fmt.Sprintf("user:%s", actor.Name)) { 49 return true 50 } 51 52 for _, role := range actor.Roles { 53 if r.subjects.Has(fmt.Sprintf("role:%s", role)) { 54 return true 55 } 56 } 57 } 58 } 59 60 return false 61 }