Skip to content

Commit dc03406

Browse files
stttsgman0
authored andcommitted
UPSTREAM: <squash>: authz: add warrants to default rule resolver: global service account
Signed-off-by: Dr. Stefan Schimanski <[email protected]>
1 parent b6ac17f commit dc03406

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

pkg/registry/rbac/validation/kcp.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package validation
22

33
import (
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 {

pkg/registry/rbac/validation/kcp_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff 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) {

0 commit comments

Comments
 (0)