2014-11-18 16:03:36 +00:00
|
|
|
package tlsutil
|
2014-04-07 20:05:43 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
2014-05-26 17:58:57 +00:00
|
|
|
"crypto/x509"
|
2019-03-13 09:29:06 +00:00
|
|
|
"fmt"
|
2014-06-30 01:11:32 +00:00
|
|
|
"io"
|
|
|
|
"net"
|
2022-04-14 20:55:10 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
2021-10-04 17:22:30 +00:00
|
|
|
"path/filepath"
|
2014-04-07 20:05:43 +00:00
|
|
|
"testing"
|
2015-05-11 23:05:39 +00:00
|
|
|
|
2021-06-24 16:51:40 +00:00
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
"github.com/google/go-cmp/cmp/cmpopts"
|
2021-06-17 22:59:53 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
2015-05-11 23:05:39 +00:00
|
|
|
"github.com/hashicorp/yamux"
|
2018-12-17 17:56:18 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-06-17 22:59:53 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
2022-03-24 19:32:25 +00:00
|
|
|
"github.com/hashicorp/consul/types"
|
2014-04-07 20:05:43 +00:00
|
|
|
)
|
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
func TestConfigurator_IncomingConfig_Common(t *testing.T) {
|
|
|
|
// if this test is failing because of expired certificates
|
|
|
|
// use the procedure in test/CA-GENERATION.md
|
|
|
|
testCases := map[string]struct {
|
|
|
|
setupFn func(ProtocolConfig) Config
|
|
|
|
configFn func(*Configurator) *tls.Config
|
|
|
|
}{
|
|
|
|
"Internal RPC": {
|
|
|
|
func(lc ProtocolConfig) Config { return Config{InternalRPC: lc} },
|
|
|
|
func(c *Configurator) *tls.Config { return c.IncomingRPCConfig() },
|
|
|
|
},
|
|
|
|
"gRPC": {
|
|
|
|
func(lc ProtocolConfig) Config { return Config{GRPC: lc} },
|
|
|
|
func(c *Configurator) *tls.Config { return c.IncomingGRPCConfig() },
|
|
|
|
},
|
|
|
|
"HTTPS": {
|
|
|
|
func(lc ProtocolConfig) Config { return Config{HTTPS: lc} },
|
|
|
|
func(c *Configurator) *tls.Config { return c.IncomingHTTPSConfig() },
|
|
|
|
},
|
2020-03-09 20:59:02 +00:00
|
|
|
}
|
2016-11-08 02:15:26 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
for desc, tc := range testCases {
|
|
|
|
t.Run(desc, func(t *testing.T) {
|
|
|
|
t.Run("MinTLSVersion", func(t *testing.T) {
|
|
|
|
cfg := ProtocolConfig{
|
2022-03-24 19:32:25 +00:00
|
|
|
TLSMinVersion: "TLSv1_3",
|
2022-03-18 10:46:58 +00:00
|
|
|
CertFile: "../test/hostname/Alice.crt",
|
|
|
|
KeyFile: "../test/hostname/Alice.key",
|
|
|
|
}
|
|
|
|
c := makeConfigurator(t, tc.setupFn(cfg))
|
|
|
|
|
|
|
|
client, errc, _ := startTLSServer(tc.configFn(c))
|
|
|
|
if client == nil {
|
|
|
|
t.Fatalf("startTLSServer err: %v", <-errc)
|
|
|
|
}
|
|
|
|
|
|
|
|
tlsClient := tls.Client(client, &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
MaxVersion: tls.VersionTLS12,
|
|
|
|
})
|
|
|
|
|
|
|
|
err := tlsClient.Handshake()
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), "version not supported")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("CipherSuites", func(t *testing.T) {
|
|
|
|
cfg := ProtocolConfig{
|
2022-03-24 19:32:25 +00:00
|
|
|
CipherSuites: []types.TLSCipherSuite{types.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384},
|
2022-03-18 10:46:58 +00:00
|
|
|
CertFile: "../test/hostname/Alice.crt",
|
|
|
|
KeyFile: "../test/hostname/Alice.key",
|
|
|
|
}
|
|
|
|
c := makeConfigurator(t, tc.setupFn(cfg))
|
|
|
|
|
|
|
|
client, errc, _ := startTLSServer(tc.configFn(c))
|
|
|
|
if client == nil {
|
|
|
|
t.Fatalf("startTLSServer err: %v", <-errc)
|
|
|
|
}
|
|
|
|
|
|
|
|
tlsClient := tls.Client(client, &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
MaxVersion: tls.VersionTLS12, // TLS 1.3 cipher suites are not configurable.
|
|
|
|
})
|
|
|
|
require.NoError(t, tlsClient.Handshake())
|
|
|
|
|
|
|
|
cipherSuite := tlsClient.ConnectionState().CipherSuite
|
|
|
|
require.Equal(t, tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, cipherSuite)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("manually configured certificate is preferred over AutoTLS", func(t *testing.T) {
|
|
|
|
// Manually configure Alice's certifcate.
|
|
|
|
cfg := ProtocolConfig{
|
|
|
|
CertFile: "../test/hostname/Alice.crt",
|
|
|
|
KeyFile: "../test/hostname/Alice.key",
|
|
|
|
}
|
|
|
|
c := makeConfigurator(t, tc.setupFn(cfg))
|
|
|
|
|
|
|
|
// Set Bob's certificate via auto TLS.
|
|
|
|
bobCert := loadFile(t, "../test/hostname/Bob.crt")
|
|
|
|
bobKey := loadFile(t, "../test/hostname/Bob.key")
|
|
|
|
require.NoError(t, c.UpdateAutoTLSCert(bobCert, bobKey))
|
|
|
|
|
|
|
|
client, errc, _ := startTLSServer(tc.configFn(c))
|
|
|
|
if client == nil {
|
|
|
|
t.Fatalf("startTLSServer err: %v", <-errc)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform a handshake and check the server presented Alice's certificate.
|
|
|
|
tlsClient := tls.Client(client, &tls.Config{InsecureSkipVerify: true})
|
|
|
|
require.NoError(t, tlsClient.Handshake())
|
|
|
|
|
|
|
|
certificates := tlsClient.ConnectionState().PeerCertificates
|
|
|
|
require.NotEmpty(t, certificates)
|
|
|
|
require.Equal(t, "Alice", certificates[0].Subject.CommonName)
|
|
|
|
|
|
|
|
// Check the server side of the handshake succeded.
|
|
|
|
require.NoError(t, <-errc)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("AutoTLS certificate is presented if no certificate was configured manually", func(t *testing.T) {
|
|
|
|
// No manually configured certificate.
|
|
|
|
c := makeConfigurator(t, Config{})
|
|
|
|
|
|
|
|
// Set Bob's certificate via auto TLS.
|
|
|
|
bobCert := loadFile(t, "../test/hostname/Bob.crt")
|
|
|
|
bobKey := loadFile(t, "../test/hostname/Bob.key")
|
|
|
|
require.NoError(t, c.UpdateAutoTLSCert(bobCert, bobKey))
|
|
|
|
|
|
|
|
client, errc, _ := startTLSServer(tc.configFn(c))
|
|
|
|
if client == nil {
|
|
|
|
t.Fatalf("startTLSServer err: %v", <-errc)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform a handshake and check the server presented Bobs's certificate.
|
|
|
|
tlsClient := tls.Client(client, &tls.Config{InsecureSkipVerify: true})
|
|
|
|
require.NoError(t, tlsClient.Handshake())
|
|
|
|
|
|
|
|
certificates := tlsClient.ConnectionState().PeerCertificates
|
|
|
|
require.NotEmpty(t, certificates)
|
|
|
|
require.Equal(t, "Bob", certificates[0].Subject.CommonName)
|
|
|
|
|
|
|
|
// Check the server side of the handshake succeded.
|
|
|
|
require.NoError(t, <-errc)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("VerifyIncoming enabled - successful handshake", func(t *testing.T) {
|
|
|
|
cfg := ProtocolConfig{
|
|
|
|
CAFile: "../test/hostname/CertAuth.crt",
|
|
|
|
CertFile: "../test/hostname/Alice.crt",
|
|
|
|
KeyFile: "../test/hostname/Alice.key",
|
|
|
|
VerifyIncoming: true,
|
|
|
|
}
|
|
|
|
c := makeConfigurator(t, tc.setupFn(cfg))
|
|
|
|
|
|
|
|
client, errc, _ := startTLSServer(tc.configFn(c))
|
|
|
|
if client == nil {
|
|
|
|
t.Fatalf("startTLSServer err: %v", <-errc)
|
|
|
|
}
|
|
|
|
|
|
|
|
tlsClient := tls.Client(client, &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
GetClientCertificate: func(*tls.CertificateRequestInfo) (*tls.Certificate, error) {
|
|
|
|
cert, err := tls.LoadX509KeyPair("../test/hostname/Bob.crt", "../test/hostname/Bob.key")
|
|
|
|
return &cert, err
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, tlsClient.Handshake())
|
|
|
|
require.NoError(t, <-errc)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("VerifyIncoming enabled - client provides no certificate", func(t *testing.T) {
|
|
|
|
cfg := ProtocolConfig{
|
|
|
|
CAFile: "../test/hostname/CertAuth.crt",
|
|
|
|
CertFile: "../test/hostname/Alice.crt",
|
|
|
|
KeyFile: "../test/hostname/Alice.key",
|
|
|
|
VerifyIncoming: true,
|
|
|
|
}
|
|
|
|
c := makeConfigurator(t, tc.setupFn(cfg))
|
|
|
|
|
|
|
|
client, errc, _ := startTLSServer(tc.configFn(c))
|
|
|
|
if client == nil {
|
|
|
|
t.Fatalf("startTLSServer err: %v", <-errc)
|
|
|
|
}
|
|
|
|
|
|
|
|
tlsClient := tls.Client(client, &tls.Config{InsecureSkipVerify: true})
|
|
|
|
require.NoError(t, tlsClient.Handshake())
|
|
|
|
|
|
|
|
err := <-errc
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), "client didn't provide a certificate")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("VerifyIncoming enabled - client certificate signed by an unknown CA", func(t *testing.T) {
|
|
|
|
cfg := ProtocolConfig{
|
|
|
|
CAFile: "../test/ca/root.cer",
|
|
|
|
CertFile: "../test/hostname/Alice.crt",
|
|
|
|
KeyFile: "../test/hostname/Alice.key",
|
|
|
|
VerifyIncoming: true,
|
|
|
|
}
|
|
|
|
c := makeConfigurator(t, tc.setupFn(cfg))
|
|
|
|
|
|
|
|
client, errc, _ := startTLSServer(tc.configFn(c))
|
|
|
|
if client == nil {
|
|
|
|
t.Fatalf("startTLSServer err: %v", <-errc)
|
|
|
|
}
|
|
|
|
|
|
|
|
tlsClient := tls.Client(client, &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
GetClientCertificate: func(*tls.CertificateRequestInfo) (*tls.Certificate, error) {
|
|
|
|
cert, err := tls.LoadX509KeyPair("../test/hostname/Bob.crt", "../test/hostname/Bob.key")
|
|
|
|
return &cert, err
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, tlsClient.Handshake())
|
|
|
|
|
|
|
|
err := <-errc
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), "signed by unknown authority")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2015-05-11 23:05:39 +00:00
|
|
|
}
|
|
|
|
|
2022-09-09 19:01:47 +00:00
|
|
|
func TestConfigurator_IncomingGRPCConfig_Peering(t *testing.T) {
|
|
|
|
// Manually configure Alice's certificates
|
|
|
|
cfg := Config{
|
|
|
|
GRPC: ProtocolConfig{
|
|
|
|
CertFile: "../test/hostname/Alice.crt",
|
|
|
|
KeyFile: "../test/hostname/Alice.key",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
c := makeConfigurator(t, cfg)
|
|
|
|
|
|
|
|
// Set Bob's certificate via auto TLS.
|
|
|
|
bobCert := loadFile(t, "../test/hostname/Bob.crt")
|
|
|
|
bobKey := loadFile(t, "../test/hostname/Bob.key")
|
|
|
|
require.NoError(t, c.UpdateAutoTLSCert(bobCert, bobKey))
|
|
|
|
|
|
|
|
peeringServerName := "server.dc1.peering.1234"
|
|
|
|
c.UpdateAutoTLSPeeringServerName(peeringServerName)
|
|
|
|
|
|
|
|
testutil.RunStep(t, "with peering name", func(t *testing.T) {
|
|
|
|
client, errc, _ := startTLSServer(c.IncomingGRPCConfig())
|
|
|
|
if client == nil {
|
|
|
|
t.Fatalf("startTLSServer err: %v", <-errc)
|
|
|
|
}
|
|
|
|
tlsClient := tls.Client(client, &tls.Config{
|
|
|
|
// When the peering server name is provided the server should present
|
|
|
|
// the certificates configured via AutoTLS (Bob).
|
|
|
|
ServerName: peeringServerName,
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
})
|
|
|
|
require.NoError(t, tlsClient.Handshake())
|
|
|
|
|
|
|
|
certificates := tlsClient.ConnectionState().PeerCertificates
|
|
|
|
require.NotEmpty(t, certificates)
|
|
|
|
require.Equal(t, "Bob", certificates[0].Subject.CommonName)
|
|
|
|
|
|
|
|
// Check the server side of the handshake succeded.
|
|
|
|
require.NoError(t, <-errc)
|
|
|
|
})
|
|
|
|
|
|
|
|
testutil.RunStep(t, "without name", func(t *testing.T) {
|
|
|
|
client, errc, _ := startTLSServer(c.IncomingGRPCConfig())
|
|
|
|
if client == nil {
|
|
|
|
t.Fatalf("startTLSServer err: %v", <-errc)
|
|
|
|
}
|
|
|
|
|
|
|
|
tlsClient := tls.Client(client, &tls.Config{
|
|
|
|
// ServerName: peeringServerName,
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
})
|
|
|
|
require.NoError(t, tlsClient.Handshake())
|
|
|
|
|
|
|
|
certificates := tlsClient.ConnectionState().PeerCertificates
|
|
|
|
require.NotEmpty(t, certificates)
|
|
|
|
|
|
|
|
// Should default to presenting the manually configured certificates.
|
|
|
|
require.Equal(t, "Alice", certificates[0].Subject.CommonName)
|
|
|
|
|
|
|
|
// Check the server side of the handshake succeded.
|
|
|
|
require.NoError(t, <-errc)
|
|
|
|
})
|
|
|
|
}
|
2022-03-18 10:46:58 +00:00
|
|
|
func TestConfigurator_IncomingInsecureRPCConfig(t *testing.T) {
|
2021-11-01 15:40:16 +00:00
|
|
|
// if this test is failing because of expired certificates
|
|
|
|
// use the procedure in test/CA-GENERATION.md
|
2022-03-18 10:46:58 +00:00
|
|
|
cfg := Config{
|
|
|
|
InternalRPC: ProtocolConfig{
|
|
|
|
CAFile: "../test/hostname/CertAuth.crt",
|
|
|
|
CertFile: "../test/hostname/Alice.crt",
|
|
|
|
KeyFile: "../test/hostname/Alice.key",
|
|
|
|
VerifyIncoming: true,
|
|
|
|
},
|
2015-05-11 23:05:39 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
c := makeConfigurator(t, cfg)
|
|
|
|
|
|
|
|
client, errc, _ := startTLSServer(c.IncomingInsecureRPCConfig())
|
2019-03-13 09:29:06 +00:00
|
|
|
if client == nil {
|
|
|
|
t.Fatalf("startTLSServer err: %v", <-errc)
|
|
|
|
}
|
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
tlsClient := tls.Client(client, &tls.Config{InsecureSkipVerify: true})
|
|
|
|
require.NoError(t, tlsClient.Handshake())
|
2019-03-13 09:29:06 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
// Check the server side of the handshake succeded.
|
|
|
|
require.NoError(t, <-errc)
|
2019-03-13 09:29:06 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
func TestConfigurator_ALPNRPCConfig(t *testing.T) {
|
2021-11-01 15:40:16 +00:00
|
|
|
// if this test is failing because of expired certificates
|
|
|
|
// use the procedure in test/CA-GENERATION.md
|
2022-03-18 10:46:58 +00:00
|
|
|
t.Run("successful protocol negotiation", func(t *testing.T) {
|
|
|
|
serverCfg := makeConfigurator(t, Config{
|
|
|
|
InternalRPC: ProtocolConfig{
|
|
|
|
CAFile: "../test/hostname/CertAuth.crt",
|
|
|
|
CertFile: "../test/hostname/Bob.crt",
|
|
|
|
KeyFile: "../test/hostname/Bob.key",
|
|
|
|
},
|
|
|
|
})
|
2019-03-13 09:29:06 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
client, errc, _ := startTLSServer(serverCfg.IncomingALPNRPCConfig([]string{"some-protocol"}))
|
|
|
|
if client == nil {
|
|
|
|
t.Fatalf("startTLSServer err: %v", <-errc)
|
|
|
|
}
|
2015-05-11 23:05:39 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
clientCfg := makeConfigurator(t, Config{
|
|
|
|
InternalRPC: ProtocolConfig{
|
|
|
|
CAFile: "../test/hostname/CertAuth.crt",
|
|
|
|
CertFile: "../test/hostname/Alice.crt",
|
|
|
|
KeyFile: "../test/hostname/Alice.key",
|
|
|
|
},
|
|
|
|
Domain: "consul",
|
|
|
|
})
|
|
|
|
wrap := clientCfg.OutgoingALPNRPCWrapper()
|
2015-05-11 23:05:39 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
tlsClient, err := wrap("dc1", "bob", "some-protocol", client)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer tlsClient.Close()
|
2019-02-26 15:52:07 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
tlsConn := tlsClient.(*tls.Conn)
|
|
|
|
require.NoError(t, tlsConn.Handshake())
|
|
|
|
require.Equal(t, "some-protocol", tlsConn.ConnectionState().NegotiatedProtocol)
|
2015-05-11 23:05:39 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
// Check the server side of the handshake succeded.
|
|
|
|
require.NoError(t, <-errc)
|
|
|
|
})
|
2015-05-11 23:05:39 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
t.Run("protocol negotiation fails", func(t *testing.T) {
|
|
|
|
serverCfg := makeConfigurator(t, Config{
|
|
|
|
InternalRPC: ProtocolConfig{
|
|
|
|
CAFile: "../test/hostname/CertAuth.crt",
|
|
|
|
CertFile: "../test/hostname/Bob.crt",
|
|
|
|
KeyFile: "../test/hostname/Bob.key",
|
|
|
|
},
|
|
|
|
})
|
2015-05-11 23:05:39 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
client, errc, _ := startTLSServer(serverCfg.IncomingALPNRPCConfig([]string{"some-protocol"}))
|
|
|
|
if client == nil {
|
|
|
|
t.Fatalf("startTLSServer err: %v", <-errc)
|
|
|
|
}
|
2015-05-11 23:05:39 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
clientCfg := makeConfigurator(t, Config{
|
|
|
|
InternalRPC: ProtocolConfig{
|
|
|
|
CAFile: "../test/hostname/CertAuth.crt",
|
|
|
|
CertFile: "../test/hostname/Alice.crt",
|
|
|
|
KeyFile: "../test/hostname/Alice.key",
|
|
|
|
},
|
|
|
|
Domain: "consul",
|
|
|
|
})
|
|
|
|
wrap := clientCfg.OutgoingALPNRPCWrapper()
|
2015-05-11 23:05:39 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
_, err := wrap("dc1", "bob", "other-protocol", client)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Error(t, <-errc)
|
|
|
|
})
|
2019-02-26 15:52:07 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
t.Run("no node name in SAN", func(t *testing.T) {
|
|
|
|
// Note: Alice.crt has server.dc1.consul as its SAN (as apposed to alice.server.dc1.consul).
|
|
|
|
serverCfg := makeConfigurator(t, Config{
|
|
|
|
InternalRPC: ProtocolConfig{
|
|
|
|
CAFile: "../test/hostname/CertAuth.crt",
|
|
|
|
CertFile: "../test/hostname/Alice.crt",
|
|
|
|
KeyFile: "../test/hostname/Alice.key",
|
|
|
|
},
|
|
|
|
})
|
2015-05-11 23:05:39 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
client, errc, _ := startTLSServer(serverCfg.IncomingALPNRPCConfig([]string{"some-protocol"}))
|
|
|
|
if client == nil {
|
|
|
|
t.Fatalf("startTLSServer err: %v", <-errc)
|
|
|
|
}
|
2015-05-11 23:05:39 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
clientCfg := makeConfigurator(t, Config{
|
|
|
|
InternalRPC: ProtocolConfig{
|
|
|
|
CAFile: "../test/hostname/CertAuth.crt",
|
|
|
|
CertFile: "../test/hostname/Bob.crt",
|
|
|
|
KeyFile: "../test/hostname/Bob.key",
|
|
|
|
},
|
|
|
|
Domain: "consul",
|
|
|
|
})
|
|
|
|
wrap := clientCfg.OutgoingALPNRPCWrapper()
|
2015-05-11 23:05:39 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
_, err := wrap("dc1", "alice", "some-protocol", client)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Error(t, <-errc)
|
|
|
|
})
|
2015-05-11 23:05:39 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
t.Run("client certificate is always required", func(t *testing.T) {
|
|
|
|
cfg := Config{
|
|
|
|
InternalRPC: ProtocolConfig{
|
|
|
|
VerifyIncoming: false, // this setting is ignored
|
|
|
|
CAFile: "../test/hostname/CertAuth.crt",
|
|
|
|
CertFile: "../test/hostname/Alice.crt",
|
|
|
|
KeyFile: "../test/hostname/Alice.key",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
c := makeConfigurator(t, cfg)
|
2015-05-11 23:05:39 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
client, errc, _ := startTLSServer(c.IncomingALPNRPCConfig([]string{"some-protocol"}))
|
|
|
|
if client == nil {
|
|
|
|
t.Fatalf("startTLSServer err: %v", <-errc)
|
|
|
|
}
|
2019-02-26 15:52:07 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
tlsClient := tls.Client(client, &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
NextProtos: []string{"some-protocol"},
|
|
|
|
})
|
|
|
|
require.NoError(t, tlsClient.Handshake())
|
2015-05-11 23:05:39 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
err := <-errc
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), "client didn't provide a certificate")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("bad DC", func(t *testing.T) {
|
|
|
|
serverCfg := makeConfigurator(t, Config{
|
|
|
|
InternalRPC: ProtocolConfig{
|
|
|
|
CAFile: "../test/hostname/CertAuth.crt",
|
|
|
|
CertFile: "../test/hostname/Alice.crt",
|
|
|
|
KeyFile: "../test/hostname/Alice.key",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
client, errc, _ := startTLSServer(serverCfg.IncomingALPNRPCConfig([]string{"some-protocol"}))
|
|
|
|
if client == nil {
|
|
|
|
t.Fatalf("startTLSServer err: %v", <-errc)
|
|
|
|
}
|
|
|
|
|
|
|
|
clientCfg := makeConfigurator(t, Config{
|
|
|
|
InternalRPC: ProtocolConfig{
|
|
|
|
CAFile: "../test/hostname/CertAuth.crt",
|
|
|
|
CertFile: "../test/hostname/Bob.crt",
|
|
|
|
KeyFile: "../test/hostname/Bob.key",
|
|
|
|
},
|
|
|
|
Domain: "consul",
|
|
|
|
})
|
|
|
|
wrap := clientCfg.OutgoingALPNRPCWrapper()
|
|
|
|
|
|
|
|
_, err := wrap("dc2", "*", "some-protocol", client)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Error(t, <-errc)
|
|
|
|
})
|
2014-06-30 01:11:32 +00:00
|
|
|
}
|
|
|
|
|
2022-09-16 01:12:10 +00:00
|
|
|
func TestConfigurator_OutgoingRPC_ServerMode(t *testing.T) {
|
|
|
|
type testCase struct {
|
|
|
|
clientConfig Config
|
|
|
|
expectName string
|
|
|
|
}
|
|
|
|
|
|
|
|
run := func(t *testing.T, tc testCase) {
|
|
|
|
serverCfg := makeConfigurator(t, Config{
|
|
|
|
InternalRPC: ProtocolConfig{
|
|
|
|
CAFile: "../test/hostname/CertAuth.crt",
|
|
|
|
CertFile: "../test/hostname/Alice.crt",
|
|
|
|
KeyFile: "../test/hostname/Alice.key",
|
|
|
|
VerifyIncoming: true,
|
|
|
|
},
|
|
|
|
ServerMode: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
serverConn, errc, certc := startTLSServer(serverCfg.IncomingRPCConfig())
|
|
|
|
if serverConn == nil {
|
|
|
|
t.Fatalf("startTLSServer err: %v", <-errc)
|
|
|
|
}
|
|
|
|
|
|
|
|
clientCfg := makeConfigurator(t, tc.clientConfig)
|
|
|
|
|
|
|
|
bettyCert := loadFile(t, "../test/hostname/Betty.crt")
|
|
|
|
bettyKey := loadFile(t, "../test/hostname/Betty.key")
|
|
|
|
require.NoError(t, clientCfg.UpdateAutoTLSCert(bettyCert, bettyKey))
|
|
|
|
|
|
|
|
wrap := clientCfg.OutgoingRPCWrapper()
|
|
|
|
require.NotNil(t, wrap)
|
|
|
|
|
|
|
|
tlsClient, err := wrap("dc1", serverConn)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer tlsClient.Close()
|
|
|
|
|
|
|
|
err = tlsClient.(*tls.Conn).Handshake()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = <-errc
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
clientCerts := <-certc
|
|
|
|
require.NotEmpty(t, clientCerts)
|
|
|
|
|
|
|
|
require.Equal(t, tc.expectName, clientCerts[0].Subject.CommonName)
|
|
|
|
|
|
|
|
// Check the server side of the handshake succeeded.
|
|
|
|
require.NoError(t, <-errc)
|
|
|
|
}
|
|
|
|
|
|
|
|
tt := map[string]testCase{
|
|
|
|
"server with manual cert": {
|
|
|
|
clientConfig: Config{
|
|
|
|
InternalRPC: ProtocolConfig{
|
|
|
|
VerifyOutgoing: true,
|
|
|
|
CAFile: "../test/hostname/CertAuth.crt",
|
|
|
|
CertFile: "../test/hostname/Bob.crt",
|
|
|
|
KeyFile: "../test/hostname/Bob.key",
|
|
|
|
},
|
|
|
|
ServerMode: true,
|
|
|
|
},
|
|
|
|
// Even though an AutoTLS cert is configured, the server will prefer the manually configured cert.
|
|
|
|
expectName: "Bob",
|
|
|
|
},
|
|
|
|
"client with manual cert": {
|
|
|
|
clientConfig: Config{
|
|
|
|
InternalRPC: ProtocolConfig{
|
|
|
|
VerifyOutgoing: true,
|
|
|
|
CAFile: "../test/hostname/CertAuth.crt",
|
|
|
|
CertFile: "../test/hostname/Bob.crt",
|
|
|
|
KeyFile: "../test/hostname/Bob.key",
|
|
|
|
},
|
|
|
|
ServerMode: false,
|
|
|
|
},
|
|
|
|
expectName: "Betty",
|
|
|
|
},
|
|
|
|
"client with auto-TLS": {
|
|
|
|
clientConfig: Config{
|
|
|
|
ServerMode: false,
|
|
|
|
AutoTLS: true,
|
|
|
|
},
|
|
|
|
expectName: "Betty",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range tt {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
run(t, tc)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
func TestConfigurator_OutgoingInternalRPCWrapper(t *testing.T) {
|
2021-11-01 15:40:16 +00:00
|
|
|
// if this test is failing because of expired certificates
|
|
|
|
// use the procedure in test/CA-GENERATION.md
|
2022-03-18 10:46:58 +00:00
|
|
|
t.Run("AutoTLS", func(t *testing.T) {
|
|
|
|
serverCfg := makeConfigurator(t, Config{
|
|
|
|
InternalRPC: ProtocolConfig{
|
|
|
|
CAFile: "../test/hostname/CertAuth.crt",
|
|
|
|
CertFile: "../test/hostname/Alice.crt",
|
|
|
|
KeyFile: "../test/hostname/Alice.key",
|
|
|
|
VerifyIncoming: true,
|
|
|
|
},
|
|
|
|
})
|
2020-03-09 20:59:02 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
client, errc, _ := startTLSServer(serverCfg.IncomingRPCConfig())
|
|
|
|
if client == nil {
|
|
|
|
t.Fatalf("startTLSServer err: %v", <-errc)
|
|
|
|
}
|
2020-03-09 20:59:02 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
clientCfg := makeConfigurator(t, Config{
|
|
|
|
AutoTLS: true,
|
|
|
|
})
|
|
|
|
bobCert := loadFile(t, "../test/hostname/Bob.crt")
|
|
|
|
bobKey := loadFile(t, "../test/hostname/Bob.key")
|
|
|
|
require.NoError(t, clientCfg.UpdateAutoTLSCert(bobCert, bobKey))
|
2020-03-09 20:59:02 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
wrap := clientCfg.OutgoingRPCWrapper()
|
|
|
|
require.NotNil(t, wrap)
|
2020-03-09 20:59:02 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
tlsClient, err := wrap("dc1", client)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer tlsClient.Close()
|
2020-03-09 20:59:02 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
err = tlsClient.(*tls.Conn).Handshake()
|
|
|
|
require.NoError(t, err)
|
2020-03-09 20:59:02 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
err = <-errc
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
2020-03-09 20:59:02 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
t.Run("VerifyOutgoing and a manually configured certificate", func(t *testing.T) {
|
|
|
|
serverCfg := makeConfigurator(t, Config{
|
|
|
|
InternalRPC: ProtocolConfig{
|
|
|
|
CAFile: "../test/hostname/CertAuth.crt",
|
|
|
|
CertFile: "../test/hostname/Alice.crt",
|
|
|
|
KeyFile: "../test/hostname/Alice.key",
|
|
|
|
VerifyIncoming: true,
|
|
|
|
},
|
|
|
|
})
|
2020-03-09 20:59:02 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
client, errc, _ := startTLSServer(serverCfg.IncomingRPCConfig())
|
|
|
|
if client == nil {
|
|
|
|
t.Fatalf("startTLSServer err: %v", <-errc)
|
|
|
|
}
|
2020-03-09 20:59:02 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
clientCfg := makeConfigurator(t, Config{
|
|
|
|
InternalRPC: ProtocolConfig{
|
|
|
|
VerifyOutgoing: true,
|
|
|
|
CAFile: "../test/hostname/CertAuth.crt",
|
|
|
|
CertFile: "../test/hostname/Bob.crt",
|
|
|
|
KeyFile: "../test/hostname/Bob.key",
|
|
|
|
},
|
|
|
|
})
|
2020-03-09 20:59:02 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
wrap := clientCfg.OutgoingRPCWrapper()
|
|
|
|
require.NotNil(t, wrap)
|
2020-03-09 20:59:02 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
tlsClient, err := wrap("dc1", client)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer tlsClient.Close()
|
2020-03-09 20:59:02 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
err = tlsClient.(*tls.Conn).Handshake()
|
|
|
|
require.NoError(t, err)
|
2020-03-09 20:59:02 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
err = <-errc
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
2020-03-09 20:59:02 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
t.Run("outgoing TLS not enabled", func(t *testing.T) {
|
|
|
|
serverCfg := makeConfigurator(t, Config{
|
|
|
|
InternalRPC: ProtocolConfig{
|
|
|
|
CAFile: "../test/hostname/CertAuth.crt",
|
|
|
|
CertFile: "../test/hostname/Alice.crt",
|
|
|
|
KeyFile: "../test/hostname/Alice.key",
|
|
|
|
VerifyIncoming: true,
|
|
|
|
},
|
|
|
|
})
|
2020-03-09 20:59:02 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
client, errc, _ := startTLSServer(serverCfg.IncomingRPCConfig())
|
|
|
|
if client == nil {
|
|
|
|
t.Fatalf("startTLSServer err: %v", <-errc)
|
|
|
|
}
|
2020-03-09 20:59:02 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
clientCfg := makeConfigurator(t, Config{})
|
2020-03-09 20:59:02 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
wrap := clientCfg.OutgoingRPCWrapper()
|
|
|
|
require.NotNil(t, wrap)
|
2020-03-09 20:59:02 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
client, err := wrap("dc1", client)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer client.Close()
|
2020-03-09 20:59:02 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
_, isTLS := client.(*tls.Conn)
|
|
|
|
require.False(t, isTLS)
|
|
|
|
})
|
2020-03-09 20:59:02 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
t.Run("VerifyServerHostname = true", func(t *testing.T) {
|
|
|
|
serverCfg := makeConfigurator(t, Config{
|
|
|
|
InternalRPC: ProtocolConfig{
|
|
|
|
CAFile: "../test/client_certs/rootca.crt",
|
|
|
|
CertFile: "../test/client_certs/client.crt",
|
|
|
|
KeyFile: "../test/client_certs/client.key",
|
|
|
|
},
|
|
|
|
})
|
2020-03-09 20:59:02 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
client, errc, _ := startTLSServer(serverCfg.IncomingRPCConfig())
|
|
|
|
if client == nil {
|
|
|
|
t.Fatalf("startTLSServer err: %v", <-errc)
|
|
|
|
}
|
2020-03-09 20:59:02 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
clientCfg := makeConfigurator(t, Config{
|
|
|
|
InternalRPC: ProtocolConfig{
|
|
|
|
VerifyOutgoing: true,
|
|
|
|
VerifyServerHostname: true,
|
|
|
|
CAFile: "../test/client_certs/rootca.crt",
|
|
|
|
CertFile: "../test/client_certs/client.crt",
|
|
|
|
KeyFile: "../test/client_certs/client.key",
|
|
|
|
},
|
|
|
|
Domain: "consul",
|
|
|
|
})
|
2014-06-30 01:11:32 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
wrap := clientCfg.OutgoingRPCWrapper()
|
|
|
|
require.NotNil(t, wrap)
|
2014-06-30 01:11:32 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
tlsClient, err := wrap("dc1", client)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer tlsClient.Close()
|
2014-06-30 01:11:32 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
err = tlsClient.(*tls.Conn).Handshake()
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Regexp(t, `certificate is valid for ([a-z].+) not server.dc1.consul`, err.Error())
|
|
|
|
})
|
2019-02-26 15:52:07 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
t.Run("VerifyServerHostname = true and incorrect DC name", func(t *testing.T) {
|
|
|
|
serverCfg := makeConfigurator(t, Config{
|
|
|
|
InternalRPC: ProtocolConfig{
|
|
|
|
CAFile: "../test/client_certs/rootca.crt",
|
|
|
|
CertFile: "../test/client_certs/client.crt",
|
|
|
|
KeyFile: "../test/client_certs/client.key",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
client, errc, _ := startTLSServer(serverCfg.IncomingRPCConfig())
|
|
|
|
if client == nil {
|
|
|
|
t.Fatalf("startTLSServer err: %v", <-errc)
|
|
|
|
}
|
|
|
|
|
|
|
|
clientCfg := makeConfigurator(t, Config{
|
|
|
|
InternalRPC: ProtocolConfig{
|
|
|
|
VerifyServerHostname: true,
|
|
|
|
VerifyOutgoing: true,
|
|
|
|
CAFile: "../test/client_certs/rootca.crt",
|
|
|
|
CertFile: "../test/client_certs/client.crt",
|
|
|
|
KeyFile: "../test/client_certs/client.key",
|
|
|
|
},
|
|
|
|
Domain: "consul",
|
|
|
|
})
|
|
|
|
|
|
|
|
wrap := clientCfg.OutgoingRPCWrapper()
|
|
|
|
require.NotNil(t, wrap)
|
|
|
|
|
|
|
|
tlsClient, err := wrap("dc2", client)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer tlsClient.Close()
|
|
|
|
|
|
|
|
err = tlsClient.(*tls.Conn).Handshake()
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Regexp(t, `certificate is valid for ([a-z].+) not server.dc2.consul`, err.Error())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("VerifyServerHostname = false", func(t *testing.T) {
|
|
|
|
serverCfg := makeConfigurator(t, Config{
|
|
|
|
InternalRPC: ProtocolConfig{
|
|
|
|
CAFile: "../test/client_certs/rootca.crt",
|
|
|
|
CertFile: "../test/client_certs/client.crt",
|
|
|
|
KeyFile: "../test/client_certs/client.key",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
client, errc, _ := startTLSServer(serverCfg.IncomingRPCConfig())
|
|
|
|
if client == nil {
|
|
|
|
t.Fatalf("startTLSServer err: %v", <-errc)
|
|
|
|
}
|
|
|
|
|
|
|
|
clientCfg := makeConfigurator(t, Config{
|
|
|
|
InternalRPC: ProtocolConfig{
|
|
|
|
VerifyServerHostname: false,
|
|
|
|
VerifyOutgoing: true,
|
|
|
|
CAFile: "../test/client_certs/rootca.crt",
|
|
|
|
CertFile: "../test/client_certs/client.crt",
|
|
|
|
KeyFile: "../test/client_certs/client.key",
|
|
|
|
},
|
|
|
|
Domain: "other",
|
|
|
|
})
|
|
|
|
|
|
|
|
wrap := clientCfg.OutgoingRPCWrapper()
|
|
|
|
require.NotNil(t, wrap)
|
|
|
|
|
|
|
|
tlsClient, err := wrap("dc1", client)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer tlsClient.Close()
|
|
|
|
|
|
|
|
err = tlsClient.(*tls.Conn).Handshake()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Check the server side of the handshake succeded.
|
|
|
|
require.NoError(t, <-errc)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("AutoTLS certificate preferred over manually configured certificate", func(t *testing.T) {
|
|
|
|
serverCfg := makeConfigurator(t, Config{
|
|
|
|
InternalRPC: ProtocolConfig{
|
|
|
|
CAFile: "../test/hostname/CertAuth.crt",
|
|
|
|
CertFile: "../test/hostname/Alice.crt",
|
|
|
|
KeyFile: "../test/hostname/Alice.key",
|
|
|
|
VerifyIncoming: true,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
client, errc, certc := startTLSServer(serverCfg.IncomingRPCConfig())
|
|
|
|
if client == nil {
|
|
|
|
t.Fatalf("startTLSServer err: %v", <-errc)
|
|
|
|
}
|
|
|
|
|
|
|
|
clientCfg := makeConfigurator(t, Config{
|
|
|
|
InternalRPC: ProtocolConfig{
|
|
|
|
VerifyServerHostname: true,
|
|
|
|
VerifyOutgoing: true,
|
|
|
|
CAFile: "../test/hostname/CertAuth.crt",
|
|
|
|
CertFile: "../test/hostname/Bob.crt",
|
|
|
|
KeyFile: "../test/hostname/Bob.key",
|
|
|
|
},
|
|
|
|
Domain: "consul",
|
|
|
|
})
|
|
|
|
|
|
|
|
bettyCert := loadFile(t, "../test/hostname/Betty.crt")
|
|
|
|
bettyKey := loadFile(t, "../test/hostname/Betty.key")
|
|
|
|
require.NoError(t, clientCfg.UpdateAutoTLSCert(bettyCert, bettyKey))
|
|
|
|
|
|
|
|
wrap := clientCfg.OutgoingRPCWrapper()
|
|
|
|
require.NotNil(t, wrap)
|
|
|
|
|
|
|
|
tlsClient, err := wrap("dc1", client)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer tlsClient.Close()
|
|
|
|
|
|
|
|
err = tlsClient.(*tls.Conn).Handshake()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = <-errc
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
clientCerts := <-certc
|
|
|
|
require.NotEmpty(t, clientCerts)
|
|
|
|
require.Equal(t, "Betty", clientCerts[0].Subject.CommonName)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("manually configured certificate is presented if there's no AutoTLS certificate", func(t *testing.T) {
|
|
|
|
serverCfg := makeConfigurator(t, Config{
|
|
|
|
InternalRPC: ProtocolConfig{
|
|
|
|
CAFile: "../test/hostname/CertAuth.crt",
|
|
|
|
CertFile: "../test/hostname/Alice.crt",
|
|
|
|
KeyFile: "../test/hostname/Alice.key",
|
|
|
|
VerifyIncoming: true,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
client, errc, certc := startTLSServer(serverCfg.IncomingRPCConfig())
|
|
|
|
if client == nil {
|
|
|
|
t.Fatalf("startTLSServer err: %v", <-errc)
|
|
|
|
}
|
|
|
|
|
|
|
|
clientCfg := makeConfigurator(t, Config{
|
|
|
|
InternalRPC: ProtocolConfig{
|
|
|
|
VerifyServerHostname: true,
|
|
|
|
VerifyOutgoing: true,
|
|
|
|
CAFile: "../test/hostname/CertAuth.crt",
|
|
|
|
CertFile: "../test/hostname/Bob.crt",
|
|
|
|
KeyFile: "../test/hostname/Bob.key",
|
|
|
|
},
|
|
|
|
Domain: "consul",
|
|
|
|
})
|
|
|
|
|
|
|
|
wrap := clientCfg.OutgoingRPCWrapper()
|
|
|
|
require.NotNil(t, wrap)
|
|
|
|
|
|
|
|
tlsClient, err := wrap("dc1", client)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer tlsClient.Close()
|
|
|
|
|
|
|
|
err = tlsClient.(*tls.Conn).Handshake()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = <-errc
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
clientCerts := <-certc
|
|
|
|
require.NotEmpty(t, clientCerts)
|
|
|
|
require.Equal(t, "Bob", clientCerts[0].Subject.CommonName)
|
|
|
|
})
|
2014-06-30 01:11:32 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
func TestConfigurator_outgoingWrapperALPN_serverHasNoNodeNameInSAN(t *testing.T) {
|
|
|
|
// if this test is failing because of expired certificates
|
|
|
|
// use the procedure in test/CA-GENERATION.md
|
|
|
|
srvConfig := Config{
|
|
|
|
InternalRPC: ProtocolConfig{
|
|
|
|
CAFile: "../test/hostname/CertAuth.crt",
|
|
|
|
CertFile: "../test/hostname/Alice.crt",
|
|
|
|
KeyFile: "../test/hostname/Alice.key",
|
|
|
|
VerifyOutgoing: false, // doesn't matter
|
|
|
|
VerifyServerHostname: false, // doesn't matter
|
|
|
|
},
|
|
|
|
Domain: "consul",
|
2014-06-30 01:11:32 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
client, errc := startALPNRPCTLSServer(t, &srvConfig, []string{"foo", "bar"})
|
2014-06-30 01:11:32 +00:00
|
|
|
if client == nil {
|
|
|
|
t.Fatalf("startTLSServer err: %v", <-errc)
|
|
|
|
}
|
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
config := Config{
|
|
|
|
InternalRPC: ProtocolConfig{
|
|
|
|
CAFile: "../test/hostname/CertAuth.crt",
|
|
|
|
CertFile: "../test/hostname/Bob.crt",
|
|
|
|
KeyFile: "../test/hostname/Bob.key",
|
|
|
|
VerifyOutgoing: false, // doesn't matter
|
|
|
|
VerifyServerHostname: false, // doesn't matter
|
|
|
|
},
|
|
|
|
Domain: "consul",
|
2014-06-30 01:11:32 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
c, err := NewConfigurator(config, nil)
|
2019-02-26 15:52:07 +00:00
|
|
|
require.NoError(t, err)
|
2022-03-18 10:46:58 +00:00
|
|
|
wrap := c.OutgoingALPNRPCWrapper()
|
|
|
|
require.NotNil(t, wrap)
|
|
|
|
|
|
|
|
_, err = wrap("dc1", "bob", "foo", client)
|
2019-02-26 15:52:07 +00:00
|
|
|
require.Error(t, err)
|
2023-02-17 20:04:12 +00:00
|
|
|
_, ok := err.(*tls.CertificateVerificationError)
|
2022-03-18 10:46:58 +00:00
|
|
|
require.True(t, ok)
|
|
|
|
client.Close()
|
2014-06-30 01:11:32 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
<-errc
|
2017-02-01 20:52:04 +00:00
|
|
|
}
|
2017-04-27 08:29:39 +00:00
|
|
|
|
2021-06-21 15:51:05 +00:00
|
|
|
func TestLoadKeyPair(t *testing.T) {
|
2019-03-13 09:29:06 +00:00
|
|
|
type variant struct {
|
|
|
|
cert, key string
|
|
|
|
shoulderr bool
|
|
|
|
isnil bool
|
2019-02-26 15:52:07 +00:00
|
|
|
}
|
2019-03-13 09:29:06 +00:00
|
|
|
variants := []variant{
|
2019-11-12 18:40:32 +00:00
|
|
|
{"", "", false, true},
|
|
|
|
{"bogus", "", false, true},
|
|
|
|
{"", "bogus", false, true},
|
|
|
|
{"../test/key/ourdomain.cer", "", false, true},
|
|
|
|
{"", "../test/key/ourdomain.key", false, true},
|
|
|
|
{"bogus", "bogus", true, true},
|
2019-03-13 09:29:06 +00:00
|
|
|
{"../test/key/ourdomain.cer", "../test/key/ourdomain.key",
|
2019-11-12 18:40:32 +00:00
|
|
|
false, false},
|
2019-02-26 15:52:07 +00:00
|
|
|
}
|
2019-06-27 20:22:07 +00:00
|
|
|
for i, v := range variants {
|
2021-06-21 15:51:05 +00:00
|
|
|
t.Run(fmt.Sprintf("case %d", i), func(t *testing.T) {
|
|
|
|
cert, err := loadKeyPair(v.cert, v.key)
|
|
|
|
if v.shoulderr {
|
|
|
|
require.Error(t, err)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.isnil {
|
|
|
|
require.Nil(t, cert)
|
|
|
|
} else {
|
|
|
|
require.NotNil(t, cert)
|
|
|
|
}
|
|
|
|
})
|
2019-02-26 15:52:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
func TestConfig_SpecifyDC(t *testing.T) {
|
|
|
|
require.Nil(t, SpecificDC("", nil))
|
|
|
|
dcwrap := func(dc string, conn net.Conn) (net.Conn, error) { return nil, nil }
|
|
|
|
wrap := SpecificDC("", dcwrap)
|
|
|
|
require.NotNil(t, wrap)
|
|
|
|
conn, err := wrap(nil)
|
2019-02-26 15:52:07 +00:00
|
|
|
require.NoError(t, err)
|
2019-03-13 09:29:06 +00:00
|
|
|
require.Nil(t, conn)
|
2019-02-26 15:52:07 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
func TestConfigurator_Validation(t *testing.T) {
|
|
|
|
// if this test is failing because of expired certificates
|
|
|
|
// use the procedure in test/CA-GENERATION.md
|
|
|
|
const (
|
|
|
|
caFile = "../test/ca/root.cer"
|
|
|
|
caPath = "../test/ca_path"
|
|
|
|
certFile = "../test/key/ourdomain.cer"
|
|
|
|
keyFile = "../test/key/ourdomain.key"
|
|
|
|
)
|
|
|
|
|
|
|
|
t.Run("empty config", func(t *testing.T) {
|
|
|
|
_, err := NewConfigurator(Config{}, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, new(Configurator).Update(Config{}))
|
|
|
|
})
|
2019-03-13 09:29:06 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
t.Run("common fields", func(t *testing.T) {
|
|
|
|
type testCase struct {
|
|
|
|
config ProtocolConfig
|
|
|
|
isValid bool
|
|
|
|
}
|
2019-03-13 09:29:06 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
testCases := map[string]testCase{
|
|
|
|
"invalid CAFile": {
|
|
|
|
ProtocolConfig{CAFile: "bogus"},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
"invalid CAPath": {
|
|
|
|
ProtocolConfig{CAPath: "bogus"},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
"invalid CertFile": {
|
|
|
|
ProtocolConfig{
|
|
|
|
CertFile: "bogus",
|
|
|
|
KeyFile: keyFile,
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
"invalid KeyFile": {
|
|
|
|
ProtocolConfig{
|
|
|
|
CertFile: certFile,
|
|
|
|
KeyFile: "bogus",
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
"VerifyIncoming set but no CA": {
|
|
|
|
ProtocolConfig{
|
|
|
|
VerifyIncoming: true,
|
|
|
|
CAFile: "",
|
|
|
|
CAPath: "",
|
|
|
|
CertFile: certFile,
|
|
|
|
KeyFile: keyFile,
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
"VerifyIncoming set but no CertFile": {
|
|
|
|
ProtocolConfig{
|
|
|
|
VerifyIncoming: true,
|
|
|
|
CAFile: caFile,
|
|
|
|
CertFile: "",
|
|
|
|
KeyFile: keyFile,
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
"VerifyIncoming set but no KeyFile": {
|
|
|
|
ProtocolConfig{
|
|
|
|
VerifyIncoming: true,
|
|
|
|
CAFile: caFile,
|
|
|
|
CertFile: certFile,
|
|
|
|
KeyFile: "",
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
"VerifyIncoming + CAFile": {
|
|
|
|
ProtocolConfig{
|
|
|
|
VerifyIncoming: true,
|
|
|
|
CAFile: caFile,
|
|
|
|
CertFile: certFile,
|
|
|
|
KeyFile: keyFile,
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
"VerifyIncoming + CAPath": {
|
|
|
|
ProtocolConfig{
|
|
|
|
VerifyIncoming: true,
|
|
|
|
CAPath: caPath,
|
|
|
|
CertFile: certFile,
|
|
|
|
KeyFile: keyFile,
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
"VerifyIncoming + invalid CAFile": {
|
|
|
|
ProtocolConfig{
|
|
|
|
VerifyIncoming: true,
|
|
|
|
CAFile: "bogus",
|
|
|
|
CertFile: certFile,
|
|
|
|
KeyFile: keyFile,
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
"VerifyIncoming + invalid CAPath": {
|
|
|
|
ProtocolConfig{
|
|
|
|
VerifyIncoming: true,
|
|
|
|
CAPath: "bogus",
|
|
|
|
CertFile: certFile,
|
|
|
|
KeyFile: keyFile,
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
"VerifyOutgoing + CAFile": {
|
|
|
|
ProtocolConfig{VerifyOutgoing: true, CAFile: caFile},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
"VerifyOutgoing + CAPath": {
|
|
|
|
ProtocolConfig{VerifyOutgoing: true, CAPath: caPath},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
"VerifyOutgoing + CAFile + CAPath": {
|
|
|
|
ProtocolConfig{
|
|
|
|
VerifyOutgoing: true,
|
|
|
|
CAFile: caFile,
|
|
|
|
CAPath: caPath,
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
"VerifyOutgoing but no CA": {
|
|
|
|
ProtocolConfig{
|
|
|
|
VerifyOutgoing: true,
|
|
|
|
CAFile: "",
|
|
|
|
CAPath: "",
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
2019-03-13 09:29:06 +00:00
|
|
|
}
|
2022-03-18 10:46:58 +00:00
|
|
|
|
|
|
|
for desc, tc := range testCases {
|
|
|
|
for _, p := range []string{"internal", "grpc", "https"} {
|
|
|
|
info := fmt.Sprintf("%s => %s", p, desc)
|
|
|
|
|
|
|
|
var cfg Config
|
|
|
|
switch p {
|
|
|
|
case "internal":
|
|
|
|
cfg.InternalRPC = tc.config
|
|
|
|
case "grpc":
|
|
|
|
cfg.GRPC = tc.config
|
|
|
|
case "https":
|
|
|
|
cfg.HTTPS = tc.config
|
|
|
|
default:
|
|
|
|
t.Fatalf("unknown protocol: %s", p)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err1 := NewConfigurator(cfg, nil)
|
|
|
|
err2 := new(Configurator).Update(cfg)
|
|
|
|
|
|
|
|
if tc.isValid {
|
|
|
|
require.NoError(t, err1, info)
|
|
|
|
require.NoError(t, err2, info)
|
|
|
|
} else {
|
|
|
|
require.Error(t, err1, info)
|
|
|
|
require.Error(t, err2, info)
|
|
|
|
}
|
2019-03-13 09:29:06 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-18 10:46:58 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("VerifyIncoming + AutoTLS", func(t *testing.T) {
|
|
|
|
cfg := Config{
|
|
|
|
InternalRPC: ProtocolConfig{
|
|
|
|
VerifyIncoming: true,
|
|
|
|
CAFile: caFile,
|
|
|
|
},
|
|
|
|
GRPC: ProtocolConfig{
|
|
|
|
VerifyIncoming: true,
|
|
|
|
CAFile: caFile,
|
|
|
|
},
|
|
|
|
HTTPS: ProtocolConfig{
|
|
|
|
VerifyIncoming: true,
|
|
|
|
CAFile: caFile,
|
|
|
|
},
|
|
|
|
AutoTLS: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := NewConfigurator(cfg, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, new(Configurator).Update(cfg))
|
|
|
|
})
|
2019-02-27 09:14:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigurator_CommonTLSConfigServerNameNodeName(t *testing.T) {
|
|
|
|
type variant struct {
|
2019-03-13 09:29:06 +00:00
|
|
|
config Config
|
2019-02-27 09:14:59 +00:00
|
|
|
result string
|
|
|
|
}
|
|
|
|
variants := []variant{
|
2019-03-13 09:29:06 +00:00
|
|
|
{config: Config{NodeName: "node", ServerName: "server"},
|
2019-02-27 09:14:59 +00:00
|
|
|
result: "server"},
|
2019-03-13 09:29:06 +00:00
|
|
|
{config: Config{ServerName: "server"},
|
2019-02-27 09:14:59 +00:00
|
|
|
result: "server"},
|
2019-03-13 09:29:06 +00:00
|
|
|
{config: Config{NodeName: "node"},
|
2019-02-27 09:14:59 +00:00
|
|
|
result: "node"},
|
|
|
|
}
|
|
|
|
for _, v := range variants {
|
2019-03-13 09:29:06 +00:00
|
|
|
c, err := NewConfigurator(v.config, nil)
|
2019-02-27 09:14:59 +00:00
|
|
|
require.NoError(t, err)
|
2022-03-18 10:46:58 +00:00
|
|
|
tlsConf := c.internalRPCTLSConfig(false)
|
2019-03-05 20:35:43 +00:00
|
|
|
require.Empty(t, tlsConf.ServerName)
|
2019-02-26 15:52:07 +00:00
|
|
|
}
|
2019-02-27 09:14:59 +00:00
|
|
|
}
|
|
|
|
|
2020-09-08 10:16:16 +00:00
|
|
|
func TestConfigurator_LoadCAs(t *testing.T) {
|
2019-03-13 09:29:06 +00:00
|
|
|
type variant struct {
|
|
|
|
cafile, capath string
|
|
|
|
shouldErr bool
|
|
|
|
isNil bool
|
|
|
|
count int
|
2022-04-14 20:55:10 +00:00
|
|
|
expectedCaPool *x509.CertPool
|
2019-03-13 09:29:06 +00:00
|
|
|
}
|
|
|
|
variants := []variant{
|
2022-04-14 20:55:10 +00:00
|
|
|
{"", "", false, true, 0, nil},
|
|
|
|
{"bogus", "", true, true, 0, nil},
|
|
|
|
{"", "bogus", true, true, 0, nil},
|
|
|
|
{"", "../test/bin", true, true, 0, nil},
|
|
|
|
{"../test/ca/root.cer", "", false, false, 1, getExpectedCaPoolByFile(t)},
|
|
|
|
{"", "../test/ca_path", false, false, 2, getExpectedCaPoolByDir(t)},
|
|
|
|
{"../test/ca/root.cer", "../test/ca_path", false, false, 1, getExpectedCaPoolByFile(t)},
|
2019-03-13 09:29:06 +00:00
|
|
|
}
|
|
|
|
for i, v := range variants {
|
2020-09-08 10:16:16 +00:00
|
|
|
pems, err1 := LoadCAs(v.cafile, v.capath)
|
2021-08-26 22:04:32 +00:00
|
|
|
pool, err2 := newX509CertPool(pems)
|
2019-03-13 09:29:06 +00:00
|
|
|
info := fmt.Sprintf("case %d", i)
|
|
|
|
if v.shouldErr {
|
2019-06-27 20:22:07 +00:00
|
|
|
if err1 == nil && err2 == nil {
|
|
|
|
t.Fatal("An error is expected but got nil.")
|
|
|
|
}
|
2019-03-13 09:29:06 +00:00
|
|
|
} else {
|
2019-06-27 20:22:07 +00:00
|
|
|
require.NoError(t, err1, info)
|
|
|
|
require.NoError(t, err2, info)
|
2019-03-13 09:29:06 +00:00
|
|
|
}
|
|
|
|
if v.isNil {
|
2019-06-27 20:22:07 +00:00
|
|
|
require.Nil(t, pool, info)
|
2019-03-13 09:29:06 +00:00
|
|
|
} else {
|
2019-06-27 20:22:07 +00:00
|
|
|
require.NotEmpty(t, pems, info)
|
|
|
|
require.NotNil(t, pool, info)
|
2022-04-14 20:55:10 +00:00
|
|
|
assertDeepEqual(t, v.expectedCaPool, pool, cmpCertPool)
|
2019-06-27 20:22:07 +00:00
|
|
|
require.Len(t, pems, v.count, info)
|
2019-03-13 09:29:06 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-27 09:14:59 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
func TestConfigurator_InternalRPCMutualTLSCapable(t *testing.T) {
|
2021-11-01 15:40:16 +00:00
|
|
|
// if this test is failing because of expired certificates
|
|
|
|
// use the procedure in test/CA-GENERATION.md
|
2020-03-09 20:59:02 +00:00
|
|
|
t.Run("no ca", func(t *testing.T) {
|
|
|
|
config := Config{
|
|
|
|
Domain: "consul",
|
|
|
|
}
|
|
|
|
c, err := NewConfigurator(config, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-06-24 18:31:17 +00:00
|
|
|
require.False(t, c.MutualTLSCapable())
|
2020-03-09 20:59:02 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("ca and no keys", func(t *testing.T) {
|
|
|
|
config := Config{
|
2022-03-18 10:46:58 +00:00
|
|
|
InternalRPC: ProtocolConfig{
|
|
|
|
CAFile: "../test/hostname/CertAuth.crt",
|
|
|
|
},
|
2020-03-09 20:59:02 +00:00
|
|
|
Domain: "consul",
|
|
|
|
}
|
|
|
|
c, err := NewConfigurator(config, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-06-24 18:31:17 +00:00
|
|
|
require.False(t, c.MutualTLSCapable())
|
2020-03-09 20:59:02 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("ca and manual key", func(t *testing.T) {
|
|
|
|
config := Config{
|
2022-03-18 10:46:58 +00:00
|
|
|
InternalRPC: ProtocolConfig{
|
|
|
|
CAFile: "../test/hostname/CertAuth.crt",
|
|
|
|
CertFile: "../test/hostname/Bob.crt",
|
|
|
|
KeyFile: "../test/hostname/Bob.key",
|
|
|
|
},
|
|
|
|
Domain: "consul",
|
2020-03-09 20:59:02 +00:00
|
|
|
}
|
|
|
|
c, err := NewConfigurator(config, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-06-24 18:31:17 +00:00
|
|
|
require.True(t, c.MutualTLSCapable())
|
2020-03-09 20:59:02 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("autoencrypt ca and no autoencrypt keys", func(t *testing.T) {
|
|
|
|
config := Config{
|
|
|
|
Domain: "consul",
|
|
|
|
}
|
|
|
|
c, err := NewConfigurator(config, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
caPEM := loadFile(t, "../test/hostname/CertAuth.crt")
|
2020-07-30 14:15:12 +00:00
|
|
|
require.NoError(t, c.UpdateAutoTLSCA([]string{caPEM}))
|
2020-03-09 20:59:02 +00:00
|
|
|
|
2021-06-24 18:31:17 +00:00
|
|
|
require.False(t, c.MutualTLSCapable())
|
2020-03-09 20:59:02 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("autoencrypt ca and autoencrypt key", func(t *testing.T) {
|
|
|
|
config := Config{
|
|
|
|
Domain: "consul",
|
|
|
|
}
|
|
|
|
c, err := NewConfigurator(config, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
caPEM := loadFile(t, "../test/hostname/CertAuth.crt")
|
|
|
|
certPEM := loadFile(t, "../test/hostname/Bob.crt")
|
|
|
|
keyPEM := loadFile(t, "../test/hostname/Bob.key")
|
2020-07-30 14:15:12 +00:00
|
|
|
require.NoError(t, c.UpdateAutoTLSCA([]string{caPEM}))
|
|
|
|
require.NoError(t, c.UpdateAutoTLSCert(certPEM, keyPEM))
|
2020-03-09 20:59:02 +00:00
|
|
|
|
2021-06-24 18:31:17 +00:00
|
|
|
require.True(t, c.MutualTLSCapable())
|
2020-03-09 20:59:02 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-06-17 22:59:53 +00:00
|
|
|
func TestConfigurator_UpdateAutoTLSCA_DoesNotPanic(t *testing.T) {
|
|
|
|
config := Config{
|
|
|
|
Domain: "consul",
|
|
|
|
}
|
|
|
|
c, err := NewConfigurator(config, hclog.New(nil))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = c.UpdateAutoTLSCA([]string{"invalid pem"})
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
func TestConfigurator_VerifyIncomingRPC(t *testing.T) {
|
2022-03-18 10:46:58 +00:00
|
|
|
c := Configurator{base: &Config{}}
|
|
|
|
c.base.InternalRPC.VerifyIncoming = true
|
|
|
|
require.True(t, c.VerifyIncomingRPC())
|
2019-02-27 09:14:59 +00:00
|
|
|
}
|
|
|
|
|
2021-06-24 16:51:40 +00:00
|
|
|
func TestConfigurator_OutgoingTLSConfigForCheck(t *testing.T) {
|
|
|
|
type testCase struct {
|
|
|
|
name string
|
|
|
|
conf func() (*Configurator, error)
|
|
|
|
skipVerify bool
|
|
|
|
serverName string
|
|
|
|
expected *tls.Config
|
|
|
|
}
|
|
|
|
|
|
|
|
run := func(t *testing.T, tc testCase) {
|
|
|
|
configurator, err := tc.conf()
|
|
|
|
require.NoError(t, err)
|
|
|
|
c := configurator.OutgoingTLSConfigForCheck(tc.skipVerify, tc.serverName)
|
2022-03-18 10:46:58 +00:00
|
|
|
|
|
|
|
if diff := cmp.Diff(tc.expected, c, cmp.Options{
|
|
|
|
cmpopts.IgnoreFields(tls.Config{}, "GetCertificate", "GetClientCertificate"),
|
|
|
|
cmpopts.IgnoreUnexported(tls.Config{}),
|
|
|
|
}); diff != "" {
|
|
|
|
t.Fatalf("assertion failed: values are not equal\n--- expected\n+++ actual\n%v", diff)
|
|
|
|
}
|
2021-06-24 16:51:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []testCase{
|
2021-06-24 17:43:15 +00:00
|
|
|
{
|
|
|
|
name: "default tls",
|
|
|
|
conf: func() (*Configurator, error) {
|
|
|
|
return NewConfigurator(Config{}, nil)
|
|
|
|
},
|
|
|
|
expected: &tls.Config{},
|
|
|
|
},
|
2021-06-24 16:51:40 +00:00
|
|
|
{
|
|
|
|
name: "default tls, skip verify, no server name",
|
|
|
|
conf: func() (*Configurator, error) {
|
|
|
|
return NewConfigurator(Config{
|
2022-03-18 10:46:58 +00:00
|
|
|
InternalRPC: ProtocolConfig{
|
2022-03-24 19:32:25 +00:00
|
|
|
TLSMinVersion: types.TLSv1_2,
|
2022-03-18 10:46:58 +00:00
|
|
|
},
|
2021-06-24 16:51:40 +00:00
|
|
|
EnableAgentTLSForChecks: false,
|
|
|
|
}, nil)
|
|
|
|
},
|
|
|
|
skipVerify: true,
|
|
|
|
expected: &tls.Config{InsecureSkipVerify: true},
|
|
|
|
},
|
2021-06-24 17:36:58 +00:00
|
|
|
{
|
|
|
|
name: "default tls, skip verify, default server name",
|
|
|
|
conf: func() (*Configurator, error) {
|
|
|
|
return NewConfigurator(Config{
|
2022-03-18 10:46:58 +00:00
|
|
|
InternalRPC: ProtocolConfig{
|
2022-03-24 19:32:25 +00:00
|
|
|
TLSMinVersion: types.TLSv1_2,
|
2022-03-18 10:46:58 +00:00
|
|
|
},
|
2021-06-24 17:36:58 +00:00
|
|
|
EnableAgentTLSForChecks: false,
|
|
|
|
ServerName: "servername",
|
2021-06-24 17:43:15 +00:00
|
|
|
NodeName: "nodename",
|
2021-06-24 17:36:58 +00:00
|
|
|
}, nil)
|
|
|
|
},
|
|
|
|
skipVerify: true,
|
|
|
|
expected: &tls.Config{InsecureSkipVerify: true},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "default tls, skip verify, check server name",
|
|
|
|
conf: func() (*Configurator, error) {
|
|
|
|
return NewConfigurator(Config{
|
2022-03-18 10:46:58 +00:00
|
|
|
InternalRPC: ProtocolConfig{
|
2022-03-24 19:32:25 +00:00
|
|
|
TLSMinVersion: types.TLSv1_2,
|
2022-03-18 10:46:58 +00:00
|
|
|
},
|
2021-06-24 17:36:58 +00:00
|
|
|
EnableAgentTLSForChecks: false,
|
|
|
|
ServerName: "servername",
|
|
|
|
}, nil)
|
|
|
|
},
|
|
|
|
skipVerify: true,
|
|
|
|
serverName: "check-server-name",
|
|
|
|
expected: &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
ServerName: "check-server-name",
|
|
|
|
},
|
|
|
|
},
|
2021-06-24 16:51:40 +00:00
|
|
|
{
|
2021-06-24 17:43:15 +00:00
|
|
|
name: "agent tls, default server name",
|
2021-06-24 16:51:40 +00:00
|
|
|
conf: func() (*Configurator, error) {
|
|
|
|
return NewConfigurator(Config{
|
2022-03-18 10:46:58 +00:00
|
|
|
InternalRPC: ProtocolConfig{
|
2022-03-24 19:32:25 +00:00
|
|
|
TLSMinVersion: types.TLSv1_2,
|
2022-03-18 10:46:58 +00:00
|
|
|
},
|
2021-06-24 16:51:40 +00:00
|
|
|
EnableAgentTLSForChecks: true,
|
2021-06-24 17:43:15 +00:00
|
|
|
NodeName: "nodename",
|
2021-06-24 16:51:40 +00:00
|
|
|
ServerName: "servername",
|
|
|
|
}, nil)
|
|
|
|
},
|
2021-06-24 17:43:15 +00:00
|
|
|
expected: &tls.Config{
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
ServerName: "servername",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "agent tls, skip verify, node name for server name",
|
|
|
|
conf: func() (*Configurator, error) {
|
|
|
|
return NewConfigurator(Config{
|
2022-03-18 10:46:58 +00:00
|
|
|
InternalRPC: ProtocolConfig{
|
2022-03-24 19:32:25 +00:00
|
|
|
TLSMinVersion: types.TLSv1_2,
|
2022-03-18 10:46:58 +00:00
|
|
|
},
|
2021-06-24 17:43:15 +00:00
|
|
|
EnableAgentTLSForChecks: true,
|
|
|
|
NodeName: "nodename",
|
|
|
|
}, nil)
|
|
|
|
},
|
2021-06-24 16:51:40 +00:00
|
|
|
skipVerify: true,
|
|
|
|
expected: &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
MinVersion: tls.VersionTLS12,
|
2021-06-24 17:43:15 +00:00
|
|
|
ServerName: "nodename",
|
2021-06-24 16:51:40 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "agent tls, skip verify, with server name override",
|
|
|
|
conf: func() (*Configurator, error) {
|
|
|
|
return NewConfigurator(Config{
|
2022-03-18 10:46:58 +00:00
|
|
|
InternalRPC: ProtocolConfig{
|
2022-03-24 19:32:25 +00:00
|
|
|
TLSMinVersion: types.TLSv1_2,
|
2022-03-18 10:46:58 +00:00
|
|
|
},
|
2021-06-24 16:51:40 +00:00
|
|
|
EnableAgentTLSForChecks: true,
|
|
|
|
ServerName: "servername",
|
|
|
|
}, nil)
|
|
|
|
},
|
|
|
|
skipVerify: true,
|
|
|
|
serverName: "override",
|
|
|
|
expected: &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
ServerName: "override",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
run(t, tc)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
func TestConfigurator_ServerNameOrNodeName(t *testing.T) {
|
|
|
|
c := Configurator{base: &Config{}}
|
|
|
|
type variant struct {
|
|
|
|
server, node, expected string
|
|
|
|
}
|
|
|
|
variants := []variant{
|
|
|
|
{"", "", ""},
|
|
|
|
{"a", "", "a"},
|
|
|
|
{"", "b", "b"},
|
|
|
|
{"a", "b", "a"},
|
|
|
|
}
|
|
|
|
for _, v := range variants {
|
|
|
|
c.base.ServerName = v.server
|
|
|
|
c.base.NodeName = v.node
|
|
|
|
require.Equal(t, v.expected, c.serverNameOrNodeName())
|
|
|
|
}
|
2019-02-26 15:52:07 +00:00
|
|
|
}
|
2019-06-27 20:22:07 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
func TestConfigurator_InternalRPCVerifyServerHostname(t *testing.T) {
|
2021-06-21 16:20:39 +00:00
|
|
|
c := Configurator{base: &Config{}}
|
2019-06-27 20:22:07 +00:00
|
|
|
require.False(t, c.VerifyServerHostname())
|
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
c.base.InternalRPC.VerifyServerHostname = true
|
2020-07-30 14:15:12 +00:00
|
|
|
c.autoTLS.verifyServerHostname = false
|
2019-06-27 20:22:07 +00:00
|
|
|
require.True(t, c.VerifyServerHostname())
|
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
c.base.InternalRPC.VerifyServerHostname = false
|
2020-07-30 14:15:12 +00:00
|
|
|
c.autoTLS.verifyServerHostname = true
|
2019-06-27 20:22:07 +00:00
|
|
|
require.True(t, c.VerifyServerHostname())
|
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
c.base.InternalRPC.VerifyServerHostname = true
|
2020-07-30 14:15:12 +00:00
|
|
|
c.autoTLS.verifyServerHostname = true
|
2019-06-27 20:22:07 +00:00
|
|
|
require.True(t, c.VerifyServerHostname())
|
|
|
|
}
|
|
|
|
|
2021-06-21 19:19:34 +00:00
|
|
|
func TestConfigurator_AutoEncryptCert(t *testing.T) {
|
2021-06-21 16:20:39 +00:00
|
|
|
c := Configurator{base: &Config{}}
|
2021-06-21 19:19:34 +00:00
|
|
|
require.Nil(t, c.AutoEncryptCert())
|
2019-06-27 20:22:07 +00:00
|
|
|
|
|
|
|
cert, err := loadKeyPair("../test/key/something_expired.cer", "../test/key/something_expired.key")
|
|
|
|
require.NoError(t, err)
|
2020-07-30 14:15:12 +00:00
|
|
|
c.autoTLS.cert = cert
|
2021-06-21 19:19:34 +00:00
|
|
|
require.Equal(t, int64(1561561551), c.AutoEncryptCert().NotAfter.Unix())
|
2019-06-27 20:22:07 +00:00
|
|
|
|
|
|
|
cert, err = loadKeyPair("../test/key/ourdomain.cer", "../test/key/ourdomain.key")
|
|
|
|
require.NoError(t, err)
|
2020-07-30 14:15:12 +00:00
|
|
|
c.autoTLS.cert = cert
|
2022-11-01 15:51:13 +00:00
|
|
|
require.Equal(t, int64(4820915609), c.AutoEncryptCert().NotAfter.Unix())
|
2019-06-27 20:22:07 +00:00
|
|
|
}
|
2020-02-19 22:22:31 +00:00
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
func TestConfigurator_AuthorizeInternalRPCServerConn(t *testing.T) {
|
2021-10-04 17:22:30 +00:00
|
|
|
caPEM, caPK, err := GenerateCA(CAOpts{Days: 5, Domain: "consul"})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
dir := testutil.TempDir(t, "ca")
|
|
|
|
caPath := filepath.Join(dir, "ca.pem")
|
2022-11-10 16:26:01 +00:00
|
|
|
err = os.WriteFile(caPath, []byte(caPEM), 0600)
|
2021-10-04 17:22:30 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
// Cert and key are not used, but required to get past validation.
|
2021-10-08 16:08:56 +00:00
|
|
|
signer, err := ParseSigner(caPK)
|
|
|
|
require.NoError(t, err)
|
|
|
|
pub, pk, err := GenerateCert(CertOpts{
|
|
|
|
Signer: signer,
|
|
|
|
CA: caPEM,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
certFile := filepath.Join("cert.pem")
|
2022-11-10 16:26:01 +00:00
|
|
|
err = os.WriteFile(certFile, []byte(pub), 0600)
|
2021-10-08 16:08:56 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
keyFile := filepath.Join("cert.key")
|
2022-11-10 16:26:01 +00:00
|
|
|
err = os.WriteFile(keyFile, []byte(pk), 0600)
|
2021-10-08 16:08:56 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-10-04 17:22:30 +00:00
|
|
|
cfg := Config{
|
2022-03-18 10:46:58 +00:00
|
|
|
InternalRPC: ProtocolConfig{
|
|
|
|
VerifyServerHostname: true,
|
|
|
|
VerifyIncoming: true,
|
|
|
|
CAFile: caPath,
|
|
|
|
CertFile: certFile,
|
|
|
|
KeyFile: keyFile,
|
|
|
|
},
|
|
|
|
Domain: "consul",
|
2021-10-04 17:22:30 +00:00
|
|
|
}
|
2022-03-18 10:46:58 +00:00
|
|
|
c := makeConfigurator(t, cfg)
|
2021-10-04 17:22:30 +00:00
|
|
|
|
|
|
|
t.Run("wrong DNSName", func(t *testing.T) {
|
|
|
|
signer, err := ParseSigner(caPK)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
pem, _, err := GenerateCert(CertOpts{
|
|
|
|
Signer: signer,
|
|
|
|
CA: caPEM,
|
|
|
|
Name: "server.dc1.consul",
|
|
|
|
Days: 5,
|
|
|
|
DNSNames: []string{"this-name-is-wrong", "localhost"},
|
|
|
|
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
s := fakeTLSConn{
|
|
|
|
state: tls.ConnectionState{
|
|
|
|
VerifiedChains: [][]*x509.Certificate{certChain(t, pem, caPEM)},
|
|
|
|
PeerCertificates: certChain(t, pem, caPEM),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = c.AuthorizeServerConn("dc1", s)
|
|
|
|
testutil.RequireErrorContains(t, err, "is valid for this-name-is-wrong, localhost, not server.dc1.consul")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("wrong CA", func(t *testing.T) {
|
|
|
|
caPEM, caPK, err := GenerateCA(CAOpts{Days: 5, Domain: "consul"})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
dir := testutil.TempDir(t, "other")
|
|
|
|
caPath := filepath.Join(dir, "ca.pem")
|
2022-11-10 16:26:01 +00:00
|
|
|
err = os.WriteFile(caPath, []byte(caPEM), 0600)
|
2021-10-04 17:22:30 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
signer, err := ParseSigner(caPK)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
pem, _, err := GenerateCert(CertOpts{
|
|
|
|
Signer: signer,
|
|
|
|
CA: caPEM,
|
|
|
|
Name: "server.dc1.consul",
|
|
|
|
Days: 5,
|
|
|
|
DNSNames: []string{"server.dc1.consul", "localhost"},
|
|
|
|
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
s := fakeTLSConn{
|
|
|
|
state: tls.ConnectionState{
|
|
|
|
VerifiedChains: [][]*x509.Certificate{certChain(t, pem, caPEM)},
|
|
|
|
PeerCertificates: certChain(t, pem, caPEM),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = c.AuthorizeServerConn("dc1", s)
|
|
|
|
testutil.RequireErrorContains(t, err, "signed by unknown authority")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("missing ext key usage", func(t *testing.T) {
|
|
|
|
signer, err := ParseSigner(caPK)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
pem, _, err := GenerateCert(CertOpts{
|
|
|
|
Signer: signer,
|
|
|
|
CA: caPEM,
|
|
|
|
Name: "server.dc1.consul",
|
|
|
|
Days: 5,
|
|
|
|
DNSNames: []string{"server.dc1.consul", "localhost"},
|
|
|
|
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageEmailProtection},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
s := fakeTLSConn{
|
|
|
|
state: tls.ConnectionState{
|
|
|
|
VerifiedChains: [][]*x509.Certificate{certChain(t, pem, caPEM)},
|
|
|
|
PeerCertificates: certChain(t, pem, caPEM),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = c.AuthorizeServerConn("dc1", s)
|
|
|
|
testutil.RequireErrorContains(t, err, "certificate specifies an incompatible key usage")
|
|
|
|
})
|
2021-10-08 16:08:56 +00:00
|
|
|
|
|
|
|
t.Run("disabled by verify_incoming_rpc", func(t *testing.T) {
|
|
|
|
cfg := Config{
|
2022-03-18 10:46:58 +00:00
|
|
|
InternalRPC: ProtocolConfig{
|
|
|
|
VerifyServerHostname: true,
|
|
|
|
VerifyIncoming: false,
|
|
|
|
CAFile: caPath,
|
|
|
|
},
|
|
|
|
Domain: "consul",
|
2021-10-08 16:08:56 +00:00
|
|
|
}
|
|
|
|
c, err := NewConfigurator(cfg, hclog.New(nil))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
s := fakeTLSConn{}
|
|
|
|
err = c.AuthorizeServerConn("dc1", s)
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
2022-03-18 10:46:58 +00:00
|
|
|
}
|
|
|
|
|
2022-08-24 16:31:38 +00:00
|
|
|
func TestConfigurator_GRPCServerUseTLS(t *testing.T) {
|
2022-03-18 10:46:58 +00:00
|
|
|
t.Run("certificate manually configured", func(t *testing.T) {
|
|
|
|
c := makeConfigurator(t, Config{
|
|
|
|
GRPC: ProtocolConfig{
|
|
|
|
CertFile: "../test/hostname/Alice.crt",
|
|
|
|
KeyFile: "../test/hostname/Alice.key",
|
|
|
|
},
|
|
|
|
})
|
2022-08-24 16:31:38 +00:00
|
|
|
require.True(t, c.GRPCServerUseTLS())
|
2022-03-18 10:46:58 +00:00
|
|
|
})
|
|
|
|
|
2022-08-24 16:31:38 +00:00
|
|
|
t.Run("no certificate", func(t *testing.T) {
|
|
|
|
c := makeConfigurator(t, Config{})
|
|
|
|
require.False(t, c.GRPCServerUseTLS())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("AutoTLS (default)", func(t *testing.T) {
|
2022-03-18 10:46:58 +00:00
|
|
|
c := makeConfigurator(t, Config{})
|
|
|
|
|
|
|
|
bobCert := loadFile(t, "../test/hostname/Bob.crt")
|
|
|
|
bobKey := loadFile(t, "../test/hostname/Bob.key")
|
|
|
|
require.NoError(t, c.UpdateAutoTLSCert(bobCert, bobKey))
|
2022-08-24 16:31:38 +00:00
|
|
|
require.False(t, c.GRPCServerUseTLS())
|
|
|
|
})
|
2022-03-18 10:46:58 +00:00
|
|
|
|
2022-08-24 16:31:38 +00:00
|
|
|
t.Run("AutoTLS w/ UseAutoCert Disabled", func(t *testing.T) {
|
|
|
|
c := makeConfigurator(t, Config{
|
|
|
|
GRPC: ProtocolConfig{
|
|
|
|
UseAutoCert: false,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
bobCert := loadFile(t, "../test/hostname/Bob.crt")
|
|
|
|
bobKey := loadFile(t, "../test/hostname/Bob.key")
|
|
|
|
require.NoError(t, c.UpdateAutoTLSCert(bobCert, bobKey))
|
|
|
|
require.False(t, c.GRPCServerUseTLS())
|
2022-03-18 10:46:58 +00:00
|
|
|
})
|
2021-10-08 16:08:56 +00:00
|
|
|
|
2022-08-24 16:31:38 +00:00
|
|
|
t.Run("AutoTLS w/ UseAutoCert Enabled", func(t *testing.T) {
|
|
|
|
c := makeConfigurator(t, Config{
|
|
|
|
GRPC: ProtocolConfig{
|
|
|
|
UseAutoCert: true,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
bobCert := loadFile(t, "../test/hostname/Bob.crt")
|
|
|
|
bobKey := loadFile(t, "../test/hostname/Bob.key")
|
|
|
|
require.NoError(t, c.UpdateAutoTLSCert(bobCert, bobKey))
|
|
|
|
require.True(t, c.GRPCServerUseTLS())
|
2022-03-18 10:46:58 +00:00
|
|
|
})
|
2021-10-04 17:22:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type fakeTLSConn struct {
|
|
|
|
state tls.ConnectionState
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f fakeTLSConn) ConnectionState() tls.ConnectionState {
|
|
|
|
return f.state
|
|
|
|
}
|
|
|
|
|
|
|
|
func certChain(t *testing.T, certs ...string) []*x509.Certificate {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
result := make([]*x509.Certificate, 0, len(certs))
|
|
|
|
|
|
|
|
for i, c := range certs {
|
|
|
|
cert, err := parseCert(c)
|
|
|
|
require.NoError(t, err, "cert %d", i)
|
|
|
|
result = append(result, cert)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2022-03-18 10:46:58 +00:00
|
|
|
func startRPCTLSServer(t *testing.T, c *Configurator) (net.Conn, <-chan error) {
|
|
|
|
client, errc, _ := startTLSServer(c.IncomingRPCConfig())
|
|
|
|
return client, errc
|
|
|
|
}
|
|
|
|
|
|
|
|
func startALPNRPCTLSServer(t *testing.T, config *Config, alpnProtos []string) (net.Conn, <-chan error) {
|
|
|
|
cfg := makeConfigurator(t, *config).IncomingALPNRPCConfig(alpnProtos)
|
|
|
|
client, errc, _ := startTLSServer(cfg)
|
|
|
|
return client, errc
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeConfigurator(t *testing.T, config Config) *Configurator {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
c, err := NewConfigurator(config, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func startTLSServer(tlsConfigServer *tls.Config) (net.Conn, <-chan error, <-chan []*x509.Certificate) {
|
|
|
|
errc := make(chan error, 1)
|
|
|
|
certc := make(chan []*x509.Certificate, 1)
|
|
|
|
|
|
|
|
client, server := net.Pipe()
|
|
|
|
|
|
|
|
// Use yamux to buffer the reads, otherwise it's easy to deadlock
|
|
|
|
muxConf := yamux.DefaultConfig()
|
|
|
|
serverSession, _ := yamux.Server(server, muxConf)
|
|
|
|
clientSession, _ := yamux.Client(client, muxConf)
|
|
|
|
clientConn, _ := clientSession.Open()
|
|
|
|
serverConn, _ := serverSession.Accept()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
tlsServer := tls.Server(serverConn, tlsConfigServer)
|
|
|
|
if err := tlsServer.Handshake(); err != nil {
|
|
|
|
errc <- err
|
|
|
|
}
|
|
|
|
certc <- tlsServer.ConnectionState().PeerCertificates
|
|
|
|
close(errc)
|
|
|
|
|
|
|
|
// Because net.Pipe() is unbuffered, if both sides
|
|
|
|
// Close() simultaneously, we will deadlock as they
|
|
|
|
// both send an alert and then block. So we make the
|
|
|
|
// server read any data from the client until error or
|
|
|
|
// EOF, which will allow the client to Close(), and
|
|
|
|
// *then* we Close() the server.
|
2022-11-10 16:26:01 +00:00
|
|
|
io.Copy(io.Discard, tlsServer)
|
2022-03-18 10:46:58 +00:00
|
|
|
tlsServer.Close()
|
|
|
|
}()
|
|
|
|
return clientConn, errc, certc
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadFile(t *testing.T, path string) string {
|
|
|
|
t.Helper()
|
|
|
|
|
2022-11-10 16:26:01 +00:00
|
|
|
data, err := os.ReadFile(path)
|
2022-03-18 10:46:58 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
return string(data)
|
2020-02-19 22:22:31 +00:00
|
|
|
}
|
2022-04-14 20:55:10 +00:00
|
|
|
|
|
|
|
func getExpectedCaPoolByFile(t *testing.T) *x509.CertPool {
|
|
|
|
pool := x509.NewCertPool()
|
2022-11-10 16:26:01 +00:00
|
|
|
data, err := os.ReadFile("../test/ca/root.cer")
|
2022-04-14 20:55:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("could not open test file ../test/ca/root.cer for reading")
|
|
|
|
}
|
|
|
|
if !pool.AppendCertsFromPEM(data) {
|
|
|
|
t.Fatal("could not add test ca ../test/ca/root.cer to pool")
|
|
|
|
}
|
|
|
|
return pool
|
|
|
|
}
|
|
|
|
|
|
|
|
func getExpectedCaPoolByDir(t *testing.T) *x509.CertPool {
|
|
|
|
pool := x509.NewCertPool()
|
|
|
|
entries, err := os.ReadDir("../test/ca_path")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("could not open test dir ../test/ca_path for reading")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, entry := range entries {
|
|
|
|
filename := path.Join("../test/ca_path", entry.Name())
|
|
|
|
|
2022-11-10 16:26:01 +00:00
|
|
|
data, err := os.ReadFile(filename)
|
2022-04-14 20:55:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not open test file %s for reading", filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !pool.AppendCertsFromPEM(data) {
|
|
|
|
t.Fatalf("could not add test ca %s to pool", filename)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pool
|
|
|
|
}
|
|
|
|
|
|
|
|
// lazyCerts has a func field which can't be compared.
|
|
|
|
var cmpCertPool = cmp.Options{
|
|
|
|
cmpopts.IgnoreFields(x509.CertPool{}, "lazyCerts"),
|
|
|
|
cmp.AllowUnexported(x509.CertPool{}),
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertDeepEqual(t *testing.T, x, y interface{}, opts ...cmp.Option) {
|
|
|
|
t.Helper()
|
|
|
|
if diff := cmp.Diff(x, y, opts...); diff != "" {
|
|
|
|
t.Fatalf("assertion failed: values are not equal\n--- expected\n+++ actual\n%v", diff)
|
|
|
|
}
|
|
|
|
}
|