mirror of
https://github.com/kubernetes/client-go.git
synced 2025-12-25 22:22:28 +00:00
This change updates the transport.Config .Dial and .TLS.GetCert fields to use a struct wrapper. This indirection via a pointer allows the functions to be compared and thus makes them valid to use as map keys. This change is then leveraged by the existing global exec auth and TLS config caches to return the same authenticator and TLS config even when distinct but identical rest configs were used to create distinct clientsets. Signed-off-by: Monis Khan <mok@microsoft.com> Kubernetes-commit: e3bffcd28922b24e54cc89c4356c2dc16e778e67
189 lines
5.3 KiB
Go
189 lines
5.3 KiB
Go
/*
|
|
Copyright 2015 The Kubernetes Authors.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package transport
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"net"
|
|
"net/http"
|
|
"net/url"
|
|
"testing"
|
|
)
|
|
|
|
func TestTLSConfigKey(t *testing.T) {
|
|
// Make sure config fields that don't affect the tls config don't affect the cache key
|
|
identicalConfigurations := map[string]*Config{
|
|
"empty": {},
|
|
"basic": {Username: "bob", Password: "password"},
|
|
"bearer": {BearerToken: "token"},
|
|
"user agent": {UserAgent: "useragent"},
|
|
"transport": {Transport: http.DefaultTransport},
|
|
"wrap transport": {WrapTransport: func(http.RoundTripper) http.RoundTripper { return nil }},
|
|
}
|
|
for nameA, valueA := range identicalConfigurations {
|
|
for nameB, valueB := range identicalConfigurations {
|
|
keyA, canCache, err := tlsConfigKey(valueA)
|
|
if err != nil {
|
|
t.Errorf("Unexpected error for %q: %v", nameA, err)
|
|
continue
|
|
}
|
|
if !canCache {
|
|
t.Errorf("Unexpected canCache=false")
|
|
continue
|
|
}
|
|
keyB, canCache, err := tlsConfigKey(valueB)
|
|
if err != nil {
|
|
t.Errorf("Unexpected error for %q: %v", nameB, err)
|
|
continue
|
|
}
|
|
if !canCache {
|
|
t.Errorf("Unexpected canCache=false")
|
|
continue
|
|
}
|
|
if keyA != keyB {
|
|
t.Errorf("Expected identical cache keys for %q and %q, got:\n\t%s\n\t%s", nameA, nameB, keyA, keyB)
|
|
continue
|
|
}
|
|
if keyA != (tlsCacheKey{}) {
|
|
t.Errorf("Expected empty cache keys for %q and %q, got:\n\t%s\n\t%s", nameA, nameB, keyA, keyB)
|
|
continue
|
|
}
|
|
}
|
|
}
|
|
|
|
// Make sure config fields that affect the tls config affect the cache key
|
|
dialer := net.Dialer{}
|
|
getCert := func() (*tls.Certificate, error) { return nil, nil }
|
|
getCertHolder := &GetCertHolder{GetCert: getCert}
|
|
uniqueConfigurations := map[string]*Config{
|
|
"proxy": {Proxy: func(request *http.Request) (*url.URL, error) { return nil, nil }},
|
|
"no tls": {},
|
|
"dialer": {Dial: dialer.DialContext},
|
|
"dialer2": {Dial: func(ctx context.Context, network, address string) (net.Conn, error) { return nil, nil }},
|
|
"dialer3": {Dial: dialer.DialContext, DialHolder: &DialHolder{Dial: dialer.DialContext}},
|
|
"dialer4": {Dial: func(ctx context.Context, network, address string) (net.Conn, error) { return nil, nil }, DialHolder: &DialHolder{Dial: func(ctx context.Context, network, address string) (net.Conn, error) { return nil, nil }}},
|
|
"insecure": {TLS: TLSConfig{Insecure: true}},
|
|
"cadata 1": {TLS: TLSConfig{CAData: []byte{1}}},
|
|
"cadata 2": {TLS: TLSConfig{CAData: []byte{2}}},
|
|
"cert 1, key 1": {
|
|
TLS: TLSConfig{
|
|
CertData: []byte{1},
|
|
KeyData: []byte{1},
|
|
},
|
|
},
|
|
"cert 1, key 1, servername 1": {
|
|
TLS: TLSConfig{
|
|
CertData: []byte{1},
|
|
KeyData: []byte{1},
|
|
ServerName: "1",
|
|
},
|
|
},
|
|
"cert 1, key 1, servername 2": {
|
|
TLS: TLSConfig{
|
|
CertData: []byte{1},
|
|
KeyData: []byte{1},
|
|
ServerName: "2",
|
|
},
|
|
},
|
|
"cert 1, key 2": {
|
|
TLS: TLSConfig{
|
|
CertData: []byte{1},
|
|
KeyData: []byte{2},
|
|
},
|
|
},
|
|
"cert 2, key 1": {
|
|
TLS: TLSConfig{
|
|
CertData: []byte{2},
|
|
KeyData: []byte{1},
|
|
},
|
|
},
|
|
"cert 2, key 2": {
|
|
TLS: TLSConfig{
|
|
CertData: []byte{2},
|
|
KeyData: []byte{2},
|
|
},
|
|
},
|
|
"cadata 1, cert 1, key 1": {
|
|
TLS: TLSConfig{
|
|
CAData: []byte{1},
|
|
CertData: []byte{1},
|
|
KeyData: []byte{1},
|
|
},
|
|
},
|
|
"getCert1": {
|
|
TLS: TLSConfig{
|
|
KeyData: []byte{1},
|
|
GetCert: getCert,
|
|
},
|
|
},
|
|
"getCert2": {
|
|
TLS: TLSConfig{
|
|
KeyData: []byte{1},
|
|
GetCert: func() (*tls.Certificate, error) { return nil, nil },
|
|
},
|
|
},
|
|
"getCert3": {
|
|
TLS: TLSConfig{
|
|
KeyData: []byte{1},
|
|
GetCert: getCert,
|
|
GetCertHolder: getCertHolder,
|
|
},
|
|
},
|
|
"getCert1, key 2": {
|
|
TLS: TLSConfig{
|
|
KeyData: []byte{2},
|
|
GetCert: getCert,
|
|
},
|
|
},
|
|
"http2, http1.1": {TLS: TLSConfig{NextProtos: []string{"h2", "http/1.1"}}},
|
|
"http1.1-only": {TLS: TLSConfig{NextProtos: []string{"http/1.1"}}},
|
|
}
|
|
for nameA, valueA := range uniqueConfigurations {
|
|
for nameB, valueB := range uniqueConfigurations {
|
|
keyA, canCacheA, err := tlsConfigKey(valueA)
|
|
if err != nil {
|
|
t.Errorf("Unexpected error for %q: %v", nameA, err)
|
|
continue
|
|
}
|
|
keyB, canCacheB, err := tlsConfigKey(valueB)
|
|
if err != nil {
|
|
t.Errorf("Unexpected error for %q: %v", nameB, err)
|
|
continue
|
|
}
|
|
|
|
// Make sure we get the same key on the same config
|
|
if nameA == nameB {
|
|
if keyA != keyB {
|
|
t.Errorf("Expected identical cache keys for %q and %q, got:\n\t%s\n\t%s", nameA, nameB, keyA, keyB)
|
|
}
|
|
if canCacheA != canCacheB {
|
|
t.Errorf("Expected identical canCache %q and %q, got:\n\t%v\n\t%v", nameA, nameB, canCacheA, canCacheB)
|
|
}
|
|
continue
|
|
}
|
|
|
|
if canCacheA && canCacheB {
|
|
if keyA == keyB {
|
|
t.Errorf("Expected unique cache keys for %q and %q, got:\n\t%s\n\t%s", nameA, nameB, keyA, keyB)
|
|
continue
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|