File tree Expand file tree Collapse file tree 2 files changed +27
-0
lines changed
pkg/registry/rbac/validation Expand file tree Collapse file tree 2 files changed +27
-0
lines changed Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ package validation
22
33import (
44 "context"
5+ "fmt"
56 "strings"
67
78 "github.com/kcp-dev/logicalcluster/v3"
@@ -69,6 +70,20 @@ func withWarrants(appliesToUser appliesToUserFuncCtx) appliesToUserFuncCtx {
6970 return true
7071 }
7172
73+ if IsServiceAccount (u ) {
74+ if cluster := genericapirequest .ClusterFrom (ctx ); cluster != nil && cluster .Name != "" {
75+ nsNameSuffix := strings .TrimPrefix (u .GetName (), "system:serviceaccount:" )
76+ rewritten := & user.DefaultInfo {
77+ Name : fmt .Sprintf ("system:kcp:serviceaccount:%s:%s" , cluster .Name , nsNameSuffix ),
78+ Groups : []string {user .AllAuthenticated },
79+ Extra : u .GetExtra (),
80+ }
81+ if appliesToUser (ctx , rewritten , bindingSubject , namespace ) {
82+ return true
83+ }
84+ }
85+ }
86+
7287 for _ , v := range u .GetExtra ()[WarrantExtraKey ] {
7388 var w Warrant
7489 if err := json .Unmarshal ([]byte (v ), & w ); err != nil {
Original file line number Diff line number Diff line change @@ -358,6 +358,18 @@ func TestAppliesToUserWithWarrantsAndScopes(t *testing.T) {
358358 sub : rbacv1.Subject {Kind : "ServiceAccount" , Namespace : "ns" , Name : "sa" },
359359 want : false ,
360360 },
361+ {
362+ name : "service account with cluster as global kcp service account" ,
363+ user : & user.DefaultInfo {Name : "system:serviceaccount:ns:sa" , Extra : map [string ][]string {"authentication.kubernetes.io/cluster-name" : {"this" }}},
364+ sub : rbacv1.Subject {Kind : "User" , Name : "system:kcp:serviceaccount:this:ns:sa" },
365+ want : true ,
366+ },
367+ {
368+ name : "service account with scope as global kcp service account" ,
369+ user : & user.DefaultInfo {Name : "system:serviceaccount:ns:sa" , Extra : map [string ][]string {"authentication.kcp.io/scopes" : {"cluster:this" }}},
370+ sub : rbacv1.Subject {Kind : "User" , Name : "system:kcp:serviceaccount:this:ns:sa" },
371+ want : true ,
372+ },
361373 }
362374 for _ , tt := range tests {
363375 t .Run (tt .name , func (t * testing.T ) {
You can’t perform that action at this time.
0 commit comments