Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v10] Fix Kube impersonation header overwrite when dealing with remote clusters #22247

Merged
merged 3 commits into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/kube/proxy/forwarder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1549,7 +1549,7 @@ func setupImpersonationHeaders(log log.FieldLogger, ctx authContext, headers htt

// Make sure to overwrite the exiting headers, instead of appending to
// them.
headers[ImpersonateGroupHeader] = nil
headers.Del(ImpersonateGroupHeader)
for _, group := range impersonateGroups {
headers.Add(ImpersonateGroupHeader, group)
}
Expand Down
48 changes: 48 additions & 0 deletions lib/kube/proxy/forwarder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,19 @@ func (s ForwarderSuite) TestSetupImpersonationHeaders(c *check.C) {
},
wantErr: true,
},
{
desc: "empty impersonated group header ignored",
kubeUsers: []string{"kube-user-a"},
kubeGroups: []string{},
inHeaders: http.Header{
"Host": []string{"example.com"},
},
wantHeaders: http.Header{
"Host": []string{"example.com"},
ImpersonateUserHeader: []string{"kube-user-a"},
},
wantErr: false,
},
}
for _, tt := range tests {
c.Log(tt.desc)
Expand Down Expand Up @@ -1483,3 +1496,38 @@ func TestForwarder_clientCreds_cache(t *testing.T) {
})
}
}

func Test_copyImpersonationHeaders(t *testing.T) {
tests := []struct {
name string
inHeaders http.Header
wantHeaders http.Header
}{
{
name: "copy impersonation headers",
inHeaders: http.Header{
"Host": []string{"example.com"},
ImpersonateUserHeader: []string{"user-a"},
ImpersonateGroupHeader: []string{"kube-group-b"},
},
wantHeaders: http.Header{
ImpersonateUserHeader: []string{"user-a"},
ImpersonateGroupHeader: []string{"kube-group-b"},
},
},
{
name: "don't introduce empty impersonation headers",
inHeaders: http.Header{
"Host": []string{"example.com"},
},
wantHeaders: http.Header{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dst := http.Header{}
copyImpersonationHeaders(dst, tt.inHeaders)
require.Equal(t, tt.wantHeaders, dst)
})
}
}
4 changes: 2 additions & 2 deletions lib/kube/proxy/roundtrip.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ func (s *SpdyRoundTripper) dial(url *url.URL) (net.Conn, error) {
// copyImpersonationHeaders copies the impersonation headers from the source
// request to the destination request.
func copyImpersonationHeaders(dst, src http.Header) {
dst[ImpersonateUserHeader] = nil
dst[ImpersonateGroupHeader] = nil
dst.Del(ImpersonateUserHeader)
dst.Del(ImpersonateGroupHeader)

for _, v := range src.Values(ImpersonateUserHeader) {
dst.Add(ImpersonateUserHeader, v)
Expand Down