Skip to content
This repository was archived by the owner on May 17, 2024. It is now read-only.

Commit 8e5557f

Browse files
committed
Ensure authenticated group is added
When using impersonation users are not automatically added to the system:authenticated group. This change makes sure this happens Signed-off-by: Christian Simon <[email protected]>
1 parent 9440811 commit 8e5557f

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

pkg/proxy/proxy.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"time"
1212

1313
"k8s.io/apiserver/pkg/authentication/request/bearertoken"
14+
authuser "k8s.io/apiserver/pkg/authentication/user"
1415
"k8s.io/apiserver/pkg/server"
1516
"k8s.io/client-go/rest"
1617
"k8s.io/client-go/transport"
@@ -130,12 +131,27 @@ func (p *Proxy) RoundTrip(req *http.Request) (*http.Response, error) {
130131
return nil, errNoName
131132
}
132133

134+
// ensure group contains allauthenticated builtin
135+
found := false
136+
groups := user.GetGroups()
137+
for _, elem := range groups {
138+
if elem == authuser.AllAuthenticated {
139+
found = true
140+
break
141+
}
142+
}
143+
if !found {
144+
groups = append(groups, authuser.AllAuthenticated)
145+
}
146+
133147
// set impersonation header using authenticated user identity
148+
134149
conf := transport.ImpersonationConfig{
135150
UserName: user.GetName(),
136-
Groups: user.GetGroups(),
151+
Groups: groups,
137152
Extra: user.GetExtra(),
138153
}
154+
139155
rt := transport.NewImpersonatingRoundTripper(conf, p.clientTransport)
140156

141157
// push request through round trippers to the API server

pkg/proxy/proxy_test.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"errors"
77
"net/http"
88
"reflect"
9+
"sort"
910
"strconv"
1011
"strings"
1112
"testing"
@@ -14,6 +15,7 @@ import (
1415
"k8s.io/apiserver/pkg/authentication/authenticator"
1516
"k8s.io/apiserver/pkg/authentication/request/bearertoken"
1617
"k8s.io/apiserver/pkg/authentication/user"
18+
authuser "k8s.io/apiserver/pkg/authentication/user"
1719

1820
"github.com/jetstack/kube-oidc-proxy/pkg/mocks"
1921
)
@@ -71,9 +73,12 @@ func (f *fakeRT) RoundTrip(h *http.Request) (*http.Response, error) {
7173
f.expUser, h.Header.Get("Impersonate-User"))
7274
}
7375

74-
if !reflect.DeepEqual(h.Header["Impersonate-Group"], f.expGroup) {
75-
f.t.Errorf("client transport got unexpected group impersonation header, exp=%s got=%s",
76-
f.expGroup, h.Header.Get("Impersonate-Group"))
76+
if exp, act := sort.StringSlice(f.expGroup), sort.StringSlice(h.Header["Impersonate-Group"]); !reflect.DeepEqual(exp, act) {
77+
f.t.Errorf(
78+
"client transport got unexpected group impersonation header, exp=%#v got=%#v",
79+
exp,
80+
act,
81+
)
7782
}
7883

7984
for k, vv := range h.Header {
@@ -362,6 +367,7 @@ func Test_RoundTrip(t *testing.T) {
362367
}
363368
p.fakeToken.EXPECT().AuthenticateToken(gomock.Any(), "fake-token").Return(authResponse, true, nil)
364369
p.fakeRT.expUser = "a-user"
370+
p.fakeRT.expGroup = []string{authuser.AllAuthenticated}
365371
req.Header["Authorization"] = []string{"bearer fake-token"}
366372
_, err = p.RoundTrip(req)
367373
if err != nil {
@@ -371,7 +377,7 @@ func Test_RoundTrip(t *testing.T) {
371377
authResponse = &authenticator.Response{
372378
User: &user.DefaultInfo{
373379
Name: "a-user",
374-
Groups: []string{"a-group-a", "a-group-b"},
380+
Groups: []string{"a-group-a", "a-group-b", authuser.AllAuthenticated},
375381
Extra: map[string][]string{
376382
"foo": []string{"a", "b"},
377383
"bar": []string{"c", "d"},
@@ -381,7 +387,7 @@ func Test_RoundTrip(t *testing.T) {
381387
}
382388
p.fakeToken.EXPECT().AuthenticateToken(gomock.Any(), "fake-token").Return(authResponse, true, nil)
383389
p.fakeRT.expUser = "a-user"
384-
p.fakeRT.expGroup = []string{"a-group-a", "a-group-b"}
390+
p.fakeRT.expGroup = []string{"a-group-a", "a-group-b", authuser.AllAuthenticated}
385391
p.fakeRT.expExtra = map[string][]string{
386392
"Impersonate-Extra-Foo": []string{"a", "b"},
387393
"Impersonate-Extra-Bar": []string{"c", "d"},

0 commit comments

Comments
 (0)