Skip to content

Commit 2f5dde7

Browse files
committed
Search client auth with and without port
1 parent c7c89f8 commit 2f5dde7

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

staging/src/k8s.io/apiserver/pkg/util/webhook/authentication.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,23 @@ func (c *defaultAuthenticationInfoResolver) clientConfig(target string) (*rest.C
136136
}
137137
}
138138

139+
// If target included the default https port (443), search again without the port
140+
if target, port, err := net.SplitHostPort(target); err == nil && port == "443" {
141+
// exact match without port
142+
if authConfig, ok := c.kubeconfig.AuthInfos[target]; ok {
143+
return restConfigFromKubeconfig(authConfig)
144+
}
145+
146+
// star prefixed match without port
147+
serverSteps := strings.Split(target, ".")
148+
for i := 1; i < len(serverSteps); i++ {
149+
nickName := "*." + strings.Join(serverSteps[i:], ".")
150+
if authConfig, ok := c.kubeconfig.AuthInfos[nickName]; ok {
151+
return restConfigFromKubeconfig(authConfig)
152+
}
153+
}
154+
}
155+
139156
// if we're trying to hit the kube-apiserver and there wasn't an explicit config, use the in-cluster config
140157
if target == "kubernetes.default.svc" {
141158
// if we can find an in-cluster-config use that. If we can't, fall through.

staging/src/k8s.io/apiserver/pkg/util/webhook/authentication_test.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,90 @@ func TestAuthenticationDetection(t *testing.T) {
109109
},
110110
expected: rest.Config{BearerToken: "first"},
111111
},
112+
{
113+
name: "exact match with default https port",
114+
serverName: "one.two.three.com:443",
115+
kubeconfig: clientcmdapi.Config{
116+
AuthInfos: map[string]*clientcmdapi.AuthInfo{
117+
"one.two.three.com:443": {Token: "exact"},
118+
"*.two.three.com": {Token: "first"},
119+
"*.three.com": {Token: "second"},
120+
"*.com": {Token: "third"},
121+
"*": {Token: "fallback"},
122+
},
123+
},
124+
expected: rest.Config{BearerToken: "exact"},
125+
},
126+
{
127+
name: "wildcard match with default https port",
128+
serverName: "one.two.three.com:443",
129+
kubeconfig: clientcmdapi.Config{
130+
AuthInfos: map[string]*clientcmdapi.AuthInfo{
131+
"*.two.three.com:443": {Token: "first-with-port"},
132+
"*.two.three.com": {Token: "first"},
133+
"*.three.com": {Token: "second"},
134+
"*.com": {Token: "third"},
135+
"*": {Token: "fallback"},
136+
},
137+
},
138+
expected: rest.Config{BearerToken: "first-with-port"},
139+
},
140+
{
141+
name: "wildcard match without default https port",
142+
serverName: "one.two.three.com:443",
143+
kubeconfig: clientcmdapi.Config{
144+
AuthInfos: map[string]*clientcmdapi.AuthInfo{
145+
"*.two.three.com": {Token: "first"},
146+
"*.three.com": {Token: "second"},
147+
"*.com": {Token: "third"},
148+
"*": {Token: "fallback"},
149+
},
150+
},
151+
expected: rest.Config{BearerToken: "first"},
152+
},
153+
{
154+
name: "exact match with non-default https port",
155+
serverName: "one.two.three.com:8443",
156+
kubeconfig: clientcmdapi.Config{
157+
AuthInfos: map[string]*clientcmdapi.AuthInfo{
158+
"one.two.three.com:8443": {Token: "exact"},
159+
"*.two.three.com": {Token: "first"},
160+
"*.three.com": {Token: "second"},
161+
"*.com": {Token: "third"},
162+
"*": {Token: "fallback"},
163+
},
164+
},
165+
expected: rest.Config{BearerToken: "exact"},
166+
},
167+
{
168+
name: "wildcard match with non-default https port",
169+
serverName: "one.two.three.com:8443",
170+
kubeconfig: clientcmdapi.Config{
171+
AuthInfos: map[string]*clientcmdapi.AuthInfo{
172+
"*.two.three.com:8443": {Token: "first-with-port"},
173+
"one.two.three.com": {Token: "first-without-port"},
174+
"*.two.three.com": {Token: "first"},
175+
"*.three.com": {Token: "second"},
176+
"*.com": {Token: "third"},
177+
"*": {Token: "fallback"},
178+
},
179+
},
180+
expected: rest.Config{BearerToken: "first-with-port"},
181+
},
182+
{
183+
name: "wildcard match without non-default https port",
184+
serverName: "one.two.three.com:8443",
185+
kubeconfig: clientcmdapi.Config{
186+
AuthInfos: map[string]*clientcmdapi.AuthInfo{
187+
"one.two.three.com": {Token: "first-without-port"},
188+
"*.two.three.com": {Token: "first"},
189+
"*.three.com": {Token: "second"},
190+
"*.com": {Token: "third"},
191+
"*": {Token: "fallback"},
192+
},
193+
},
194+
expected: rest.Config{BearerToken: "fallback"},
195+
},
112196
}
113197

114198
for _, tc := range tests {

0 commit comments

Comments
 (0)