Skip to content

Commit 8e798ba

Browse files
authored
Merge pull request #3267 from sttts/sttts-impersonation-guard-complete
🐛 impersonation: add missing privileged groups
2 parents 8b594fe + 5d7fc07 commit 8e798ba

File tree

2 files changed

+16
-31
lines changed

2 files changed

+16
-31
lines changed

pkg/server/filters/impersonation.go

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,12 @@ var (
4646
// specialGroups specify groups with special meaning kcp. Lower privilege (= lower number)
4747
// cannot impersonate higher privilege levels.
4848
specialGroups = map[string]privilege{
49-
authorizationbootstrap.SystemMastersGroup: superPrivileged,
50-
authorizationbootstrap.SystemKcpAdminGroup: priviledged,
51-
user.AllAuthenticated: authenticated,
49+
authorizationbootstrap.SystemMastersGroup: superPrivileged,
50+
authorizationbootstrap.SystemLogicalClusterAdmin: priviledged,
51+
authorizationbootstrap.SystemExternalLogicalClusterAdmin: priviledged,
52+
authorizationbootstrap.SystemKcpWorkspaceBootstrapper: priviledged,
53+
authorizationbootstrap.SystemKcpAdminGroup: priviledged,
54+
user.AllAuthenticated: authenticated,
5255
}
5356
)
5457

@@ -177,33 +180,20 @@ func WithImpersonationScoping(handler http.Handler) http.Handler {
177180
})
178181
}
179182

180-
// maxUserPrivilege returns the highest privilege level found among the user's groups.
181-
func maxUserPrivilege(userGroups []string) privilege {
182-
max := unprivileged
183-
for _, g := range userGroups {
184-
if p, found := specialGroups[g]; found && p > max {
185-
max = p
183+
// validImpersonation checks if a user can impersonate all requested groups.
184+
func validImpersonation(existingGroups, requestedGroups []string) bool {
185+
for _, g := range existingGroups {
186+
if g == authorizationbootstrap.SystemMastersGroup {
187+
return true
186188
}
187189
}
188-
return max
189-
}
190190

191-
// validImpersonation checks if a user can impersonate all requested groups.
192-
func validImpersonation(userGroups, requestedGroups []string) bool {
193-
userMax := maxUserPrivilege(userGroups)
194-
195-
// Case 1: User is requesting to impersonate a group with higher privilege.
191+
existing := sets.New(existingGroups...)
196192
for _, g := range requestedGroups {
197-
if userMax < specialGroups[g] {
198-
return false
193+
if specialGroups[g] != unprivileged && !existing.Has(g) {
194+
return false // only impersonate non-unprivileged groups the user already has.
199195
}
200196
}
201-
// Case 2: User is requesting to impersonate a `system:authenticated` group without having the group itself and not being privileged.
202-
// This is very much academic, as all users reaching this point will have the `system:authenticated` group or be privileged.
203-
if sets.New(requestedGroups...).Has(user.AllAuthenticated) &&
204-
!(sets.New(userGroups...).HasAny(user.AllAuthenticated, authorizationbootstrap.SystemMastersGroup, authorizationbootstrap.SystemKcpAdminGroup)) {
205-
return false
206-
}
207197

208198
return true
209199
}

pkg/server/filters/impersonation_test.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,9 @@ import (
3333
authorizationbootstrap "github.com/kcp-dev/kcp/pkg/authorization/bootstrap"
3434
)
3535

36-
func TestCheckImpersonation(t *testing.T) {
36+
func TestValidImpersonation(t *testing.T) {
3737
var systemUserGroup = "system:user:group"
3838
nonExistingGroup := "non-existing-group"
39-
specialGroups = map[string]privilege{
40-
authorizationbootstrap.SystemMastersGroup: superPrivileged,
41-
authorizationbootstrap.SystemKcpAdminGroup: priviledged,
42-
systemUserGroup: unprivileged,
43-
}
4439

4540
tests := []struct {
4641
name string
@@ -79,7 +74,7 @@ func TestCheckImpersonation(t *testing.T) {
7974
expectedResult: false,
8075
},
8176
{
82-
name: "system:autenticated is beyond unprivileged",
77+
name: "system:authenticated is beyond unprivileged",
8378
userGroups: []string{systemUserGroup},
8479
requestedGroups: []string{user.AllAuthenticated},
8580
expectedResult: false,

0 commit comments

Comments
 (0)