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

[v14] Fix DynamicIdentityFileCreds being incompatible with L7 Loadbalancers #36469

Merged
merged 4 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 2 additions & 5 deletions api/client/contextdialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,11 +364,8 @@ func newTLSRoutingWithConnUpgradeDialer(ssh ssh.ClientConfig, params connectPara
},
ALPNConnUpgradeRequired: IsALPNConnUpgradeRequired(ctx, params.addr, insecure),
GetClusterCAs: func(_ context.Context) (*x509.CertPool, error) {
tlsConfig, err := params.cfg.Credentials[0].TLSConfig()
if err != nil {
return nil, trace.Wrap(err)
}
return tlsConfig.RootCAs, nil
// Uses the Root CAs from the TLS Config of the Credentials.
return params.tlsConfig.RootCAs, nil
},
})
if err != nil {
Expand Down
9 changes: 7 additions & 2 deletions api/client/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,8 @@ func (d *DynamicIdentityFileCreds) Dialer(

// TLSConfig returns TLS configuration. Implementing the Credentials interface.
func (d *DynamicIdentityFileCreds) TLSConfig() (*tls.Config, error) {
d.mu.RLock()
defer d.mu.RUnlock()
// Build a "dynamic" tls.Config which can support a changing cert and root
// CA pool.
cfg := &tls.Config{
Expand All @@ -506,7 +508,10 @@ func (d *DynamicIdentityFileCreds) TLSConfig() (*tls.Config, error) {
},

// VerifyConnection is used instead of the static RootCAs field.
RootCAs: nil,
// However, there's some client code which relies on the static RootCAs
// field. So we set it to a copy of the current root CAs pool to support
// those - e.g ALPNDialerConfig.GetClusterCAs
RootCAs: d.tlsRootCAs.Clone(),
// InsecureSkipVerify is forced true to ensure that only our
// VerifyConnection callback is used to verify the server's presented
// certificate.
Expand All @@ -521,7 +526,7 @@ func (d *DynamicIdentityFileCreds) TLSConfig() (*tls.Config, error) {
opts := x509.VerifyOptions{
DNSName: state.ServerName,
Intermediates: x509.NewCertPool(),
Roots: d.tlsRootCAs,
Roots: d.tlsRootCAs.Clone(),
}
for _, cert := range state.PeerCertificates[1:] {
// Whilst we don't currently use intermediate certs at
Expand Down
7 changes: 7 additions & 0 deletions api/client/credentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,13 @@ func TestDynamicIdentityFileCreds(t *testing.T) {
require.NoError(t, err)
require.Equal(t, wantTLSCert, *gotTLSCert)

tlsCACertPEM, _ := pem.Decode(tlsCACert)
tlsCACertDER, err := x509.ParseCertificate(tlsCACertPEM.Bytes)
require.NoError(t, err)
wantCertPool := x509.NewCertPool()
wantCertPool.AddCert(tlsCACertDER)
require.True(t, wantCertPool.Equal(tlsConfig.RootCAs), "tlsconfig.RootCAs mismatch")

// Generate a new TLS certificate that contains the same private key as
// the original.
template := &x509.Certificate{
Expand Down