mirror of
https://github.com/vmware-tanzu/pinniped.git
synced 2026-09-05 23:57:12 +00:00
Force the use of secure TLS config
This change updates the TLS config used by all pinniped components. There are no configuration knobs associated with this change. Thus this change tightens our static defaults. There are four TLS config levels: 1. Secure (TLS 1.3 only) 2. Default (TLS 1.2+ best ciphers that are well supported) 3. Default LDAP (TLS 1.2+ with less good ciphers) 4. Legacy (currently unused, TLS 1.2+ with all non-broken ciphers) Highlights per component: 1. pinniped CLI - uses "secure" config against KAS - uses "default" for all other connections 2. concierge - uses "secure" config as an aggregated API server - uses "default" config as a impersonation proxy API server - uses "secure" config against KAS - uses "default" config for JWT authenticater (mostly, see code) - no changes to webhook authenticater (see code) 3. supervisor - uses "default" config as a server - uses "secure" config against KAS - uses "default" config against OIDC IDPs - uses "default LDAP" config against LDAP IDPs Signed-off-by: Monis Khan <mok@vmware.com>
This commit is contained in:
@@ -18,7 +18,6 @@
|
||||
package fakekubeapi
|
||||
|
||||
import (
|
||||
"encoding/pem"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"mime"
|
||||
@@ -37,7 +36,9 @@ import (
|
||||
|
||||
pinnipedconciergeclientsetscheme "go.pinniped.dev/generated/latest/client/concierge/clientset/versioned/scheme"
|
||||
pinnipedsupervisorclientsetscheme "go.pinniped.dev/generated/latest/client/supervisor/clientset/versioned/scheme"
|
||||
"go.pinniped.dev/internal/crypto/ptls"
|
||||
"go.pinniped.dev/internal/httputil/httperr"
|
||||
"go.pinniped.dev/internal/testutil/tlsserver"
|
||||
)
|
||||
|
||||
// Start starts an httptest.Server (with TLS) that pretends to be a Kube API server.
|
||||
@@ -54,7 +55,9 @@ func Start(t *testing.T, resources map[string]runtime.Object) (*httptest.Server,
|
||||
resources = make(map[string]runtime.Object)
|
||||
}
|
||||
|
||||
server := httptest.NewTLSServer(httperr.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (err error) {
|
||||
server := tlsserver.TLSTestServer(t, httperr.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
|
||||
tlsserver.AssertTLS(t, r, ptls.Secure)
|
||||
|
||||
obj, err := decodeObj(r)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -74,11 +77,11 @@ func Start(t *testing.T, resources map[string]runtime.Object) (*httptest.Server,
|
||||
}
|
||||
|
||||
return nil
|
||||
}))
|
||||
}), tlsserver.RecordTLSHello)
|
||||
restConfig := &restclient.Config{
|
||||
Host: server.URL,
|
||||
TLSClientConfig: restclient.TLSClientConfig{
|
||||
CAData: pem.EncodeToMemory(&pem.Block{Bytes: server.Certificate().Raw, Type: "CERTIFICATE"}),
|
||||
CAData: tlsserver.TLSTestServerCA(server),
|
||||
},
|
||||
}
|
||||
return server, restConfig
|
||||
|
||||
@@ -5,39 +5,36 @@ package testutil
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"encoding/pem"
|
||||
"errors"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"go.pinniped.dev/internal/crypto/ptls"
|
||||
"go.pinniped.dev/internal/testutil/tlsserver"
|
||||
)
|
||||
|
||||
// TLSTestServer starts a test server listening on a local port using a test CA. It returns the PEM CA bundle and the
|
||||
// URL of the listening server. The lifetime of the server is bound to the provided *testing.T.
|
||||
func TLSTestServer(t *testing.T, handler http.HandlerFunc) (caBundlePEM string, url string) {
|
||||
func TLSTestServer(t *testing.T, handler http.HandlerFunc) (caBundlePEM, url string) {
|
||||
t.Helper()
|
||||
server := httptest.NewTLSServer(handler)
|
||||
t.Cleanup(server.Close)
|
||||
|
||||
caBundle := string(pem.EncodeToMemory(&pem.Block{
|
||||
Type: "CERTIFICATE",
|
||||
Bytes: server.TLS.Certificates[0].Certificate[0],
|
||||
}))
|
||||
return caBundle, server.URL
|
||||
server := tlsserver.TLSTestServer(t, handler, nil)
|
||||
|
||||
return string(tlsserver.TLSTestServerCA(server)), server.URL
|
||||
}
|
||||
|
||||
func TLSTestServerWithCert(t *testing.T, handler http.HandlerFunc, certificate *tls.Certificate) (url string) {
|
||||
t.Helper()
|
||||
|
||||
c := ptls.Default(nil) // mimic API server config
|
||||
c.Certificates = []tls.Certificate{*certificate}
|
||||
|
||||
server := http.Server{
|
||||
TLSConfig: &tls.Config{
|
||||
Certificates: []tls.Certificate{*certificate},
|
||||
MinVersion: tls.VersionTLS12,
|
||||
},
|
||||
Handler: handler,
|
||||
TLSConfig: c,
|
||||
Handler: handler,
|
||||
}
|
||||
|
||||
l, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
// Copyright 2021 the Pinniped contributors. All Rights Reserved.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package tlsserver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"encoding/pem"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"reflect"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"k8s.io/apimachinery/pkg/util/httpstream"
|
||||
|
||||
"go.pinniped.dev/internal/crypto/ptls"
|
||||
)
|
||||
|
||||
type ctxKey int
|
||||
|
||||
const (
|
||||
mapKey ctxKey = iota + 1
|
||||
helloKey
|
||||
)
|
||||
|
||||
func TLSTestServer(t *testing.T, handler http.Handler, f func(*httptest.Server)) *httptest.Server {
|
||||
t.Helper()
|
||||
|
||||
server := httptest.NewUnstartedServer(handler)
|
||||
server.TLS = ptls.Default(nil) // mimic API server config
|
||||
if f != nil {
|
||||
f(server)
|
||||
}
|
||||
server.StartTLS()
|
||||
t.Cleanup(server.Close)
|
||||
return server
|
||||
}
|
||||
|
||||
func TLSTestServerCA(server *httptest.Server) []byte {
|
||||
return pem.EncodeToMemory(&pem.Block{
|
||||
Type: "CERTIFICATE",
|
||||
Bytes: server.Certificate().Raw,
|
||||
})
|
||||
}
|
||||
|
||||
func RecordTLSHello(server *httptest.Server) {
|
||||
server.Config.ConnContext = func(ctx context.Context, _ net.Conn) context.Context {
|
||||
return context.WithValue(ctx, mapKey, &sync.Map{})
|
||||
}
|
||||
|
||||
server.TLS.GetConfigForClient = func(info *tls.ClientHelloInfo) (*tls.Config, error) {
|
||||
m, ok := getCtxMap(info.Context())
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("could not find ctx map")
|
||||
}
|
||||
if actual, loaded := m.LoadOrStore(helloKey, info); loaded && !reflect.DeepEqual(info, actual) {
|
||||
return nil, fmt.Errorf("different client hello seen")
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
}
|
||||
|
||||
func AssertTLS(t *testing.T, r *http.Request, tlsConfigFunc ptls.ConfigFunc) {
|
||||
t.Helper()
|
||||
|
||||
m, ok := getCtxMap(r.Context())
|
||||
require.True(t, ok)
|
||||
|
||||
h, ok := m.Load(helloKey)
|
||||
require.True(t, ok)
|
||||
|
||||
info, ok := h.(*tls.ClientHelloInfo)
|
||||
require.True(t, ok)
|
||||
|
||||
tlsConfig := tlsConfigFunc(nil)
|
||||
|
||||
supportedVersions := []uint16{tlsConfig.MinVersion}
|
||||
ciphers := tlsConfig.CipherSuites
|
||||
|
||||
if secureTLSConfig := ptls.Secure(nil); tlsConfig.MinVersion != secureTLSConfig.MinVersion {
|
||||
supportedVersions = append([]uint16{secureTLSConfig.MinVersion}, supportedVersions...)
|
||||
ciphers = append(ciphers, secureTLSConfig.CipherSuites...)
|
||||
}
|
||||
|
||||
protos := tlsConfig.NextProtos
|
||||
if httpstream.IsUpgradeRequest(r) {
|
||||
protos = tlsConfig.NextProtos[1:]
|
||||
}
|
||||
|
||||
// use assert instead of require to not break the http.Handler with a panic
|
||||
ok1 := assert.Equal(t, supportedVersions, info.SupportedVersions)
|
||||
ok2 := assert.Equal(t, ciphers, info.CipherSuites)
|
||||
ok3 := assert.Equal(t, protos, info.SupportedProtos)
|
||||
|
||||
if all := ok1 && ok2 && ok3; !all {
|
||||
t.Errorf("insecure TLS detected for %q %q %q upgrade=%v supportedVersions=%v ciphers=%v protos=%v",
|
||||
r.Proto, r.Method, r.URL.String(), httpstream.IsUpgradeRequest(r), ok1, ok2, ok3)
|
||||
}
|
||||
}
|
||||
|
||||
func getCtxMap(ctx context.Context) (*sync.Map, bool) {
|
||||
m, ok := ctx.Value(mapKey).(*sync.Map)
|
||||
return m, ok
|
||||
}
|
||||
Reference in New Issue
Block a user