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"
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
2017-04-27 08:29:39 +00:00
|
|
|
"reflect"
|
|
|
|
"strings"
|
2014-04-07 20:05:43 +00:00
|
|
|
"testing"
|
2015-05-11 23:05:39 +00:00
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
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"
|
2014-04-07 20:05:43 +00:00
|
|
|
)
|
|
|
|
|
2020-03-09 20:59:02 +00:00
|
|
|
func startRPCTLSServer(config *Config) (net.Conn, chan error) {
|
|
|
|
return startTLSServer(config, nil, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func startALPNRPCTLSServer(config *Config, alpnProtos []string) (net.Conn, chan error) {
|
|
|
|
return startTLSServer(config, alpnProtos, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func startTLSServer(config *Config, alpnProtos []string, doAlpnVariant bool) (net.Conn, chan error) {
|
2014-06-30 01:11:32 +00:00
|
|
|
errc := make(chan error, 1)
|
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
c, err := NewConfigurator(*config, nil)
|
2014-06-30 01:11:32 +00:00
|
|
|
if err != nil {
|
|
|
|
errc <- err
|
|
|
|
return nil, errc
|
|
|
|
}
|
2020-03-09 20:59:02 +00:00
|
|
|
var tlsConfigServer *tls.Config
|
|
|
|
if doAlpnVariant {
|
|
|
|
tlsConfigServer = c.IncomingALPNRPCConfig(alpnProtos)
|
|
|
|
} else {
|
|
|
|
tlsConfigServer = c.IncomingRPCConfig()
|
|
|
|
}
|
2014-06-30 01:11:32 +00:00
|
|
|
client, server := net.Pipe()
|
2015-05-11 23:05:39 +00:00
|
|
|
|
|
|
|
// 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()
|
|
|
|
|
2014-06-30 01:11:32 +00:00
|
|
|
go func() {
|
2015-05-11 23:05:39 +00:00
|
|
|
tlsServer := tls.Server(serverConn, tlsConfigServer)
|
2014-06-30 01:11:32 +00:00
|
|
|
if err := tlsServer.Handshake(); err != nil {
|
|
|
|
errc <- err
|
|
|
|
}
|
|
|
|
close(errc)
|
2016-11-08 02:15:26 +00:00
|
|
|
|
2014-06-30 01:11:32 +00:00
|
|
|
// 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.
|
|
|
|
io.Copy(ioutil.Discard, tlsServer)
|
|
|
|
tlsServer.Close()
|
|
|
|
}()
|
2015-05-11 23:05:39 +00:00
|
|
|
return clientConn, errc
|
|
|
|
}
|
|
|
|
|
2019-02-26 15:52:07 +00:00
|
|
|
func TestConfigurator_outgoingWrapper_OK(t *testing.T) {
|
2019-03-13 09:29:06 +00:00
|
|
|
config := Config{
|
2015-05-11 23:05:39 +00:00
|
|
|
CAFile: "../test/hostname/CertAuth.crt",
|
|
|
|
CertFile: "../test/hostname/Alice.crt",
|
|
|
|
KeyFile: "../test/hostname/Alice.key",
|
|
|
|
VerifyServerHostname: true,
|
2018-12-06 21:51:49 +00:00
|
|
|
VerifyOutgoing: true,
|
2015-05-11 23:05:39 +00:00
|
|
|
Domain: "consul",
|
|
|
|
}
|
|
|
|
|
2020-03-09 20:59:02 +00:00
|
|
|
client, errc := startRPCTLSServer(&config)
|
2019-03-13 09:29:06 +00:00
|
|
|
if client == nil {
|
|
|
|
t.Fatalf("startTLSServer err: %v", <-errc)
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := NewConfigurator(config, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
wrap := c.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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigurator_outgoingWrapper_noverify_OK(t *testing.T) {
|
|
|
|
config := Config{
|
2020-04-30 20:12:17 +00:00
|
|
|
VerifyOutgoing: true,
|
|
|
|
CAFile: "../test/hostname/CertAuth.crt",
|
|
|
|
CertFile: "../test/hostname/Alice.crt",
|
|
|
|
KeyFile: "../test/hostname/Alice.key",
|
|
|
|
Domain: "consul",
|
2019-03-13 09:29:06 +00:00
|
|
|
}
|
|
|
|
|
2020-03-09 20:59:02 +00:00
|
|
|
client, errc := startRPCTLSServer(&config)
|
2015-05-11 23:05:39 +00:00
|
|
|
if client == nil {
|
|
|
|
t.Fatalf("startTLSServer err: %v", <-errc)
|
|
|
|
}
|
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
c, err := NewConfigurator(config, nil)
|
2019-02-26 15:52:07 +00:00
|
|
|
require.NoError(t, err)
|
2019-03-13 09:29:06 +00:00
|
|
|
wrap := c.OutgoingRPCWrapper()
|
|
|
|
require.NotNil(t, wrap)
|
2015-05-11 23:05:39 +00:00
|
|
|
|
|
|
|
tlsClient, err := wrap("dc1", client)
|
2019-02-26 15:52:07 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2015-05-11 23:05:39 +00:00
|
|
|
defer tlsClient.Close()
|
2019-02-26 15:52:07 +00:00
|
|
|
err = tlsClient.(*tls.Conn).Handshake()
|
|
|
|
require.NoError(t, err)
|
2015-05-11 23:05:39 +00:00
|
|
|
|
|
|
|
err = <-errc
|
2019-02-26 15:52:07 +00:00
|
|
|
require.NoError(t, err)
|
2015-05-11 23:05:39 +00:00
|
|
|
}
|
|
|
|
|
2019-02-26 15:52:07 +00:00
|
|
|
func TestConfigurator_outgoingWrapper_BadDC(t *testing.T) {
|
2019-03-13 09:29:06 +00:00
|
|
|
config := Config{
|
2015-05-11 23:05:39 +00:00
|
|
|
CAFile: "../test/hostname/CertAuth.crt",
|
|
|
|
CertFile: "../test/hostname/Alice.crt",
|
|
|
|
KeyFile: "../test/hostname/Alice.key",
|
|
|
|
VerifyServerHostname: true,
|
2018-12-06 21:51:49 +00:00
|
|
|
VerifyOutgoing: true,
|
2015-05-11 23:05:39 +00:00
|
|
|
Domain: "consul",
|
|
|
|
}
|
|
|
|
|
2020-03-09 20:59:02 +00:00
|
|
|
client, errc := startRPCTLSServer(&config)
|
2015-05-11 23:05:39 +00:00
|
|
|
if client == nil {
|
|
|
|
t.Fatalf("startTLSServer err: %v", <-errc)
|
|
|
|
}
|
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
c, err := NewConfigurator(config, nil)
|
2019-02-26 15:52:07 +00:00
|
|
|
require.NoError(t, err)
|
2019-03-13 09:29:06 +00:00
|
|
|
wrap := c.OutgoingRPCWrapper()
|
2015-05-11 23:05:39 +00:00
|
|
|
|
|
|
|
tlsClient, err := wrap("dc2", client)
|
2019-02-26 15:52:07 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2015-05-11 23:05:39 +00:00
|
|
|
err = tlsClient.(*tls.Conn).Handshake()
|
2019-02-26 15:52:07 +00:00
|
|
|
_, ok := err.(x509.HostnameError)
|
|
|
|
require.True(t, ok)
|
2016-11-08 02:15:26 +00:00
|
|
|
tlsClient.Close()
|
2015-05-11 23:05:39 +00:00
|
|
|
|
|
|
|
<-errc
|
|
|
|
}
|
|
|
|
|
2019-02-26 15:52:07 +00:00
|
|
|
func TestConfigurator_outgoingWrapper_BadCert(t *testing.T) {
|
2019-03-13 09:29:06 +00:00
|
|
|
config := Config{
|
2015-05-11 23:05:39 +00:00
|
|
|
CAFile: "../test/ca/root.cer",
|
|
|
|
CertFile: "../test/key/ourdomain.cer",
|
|
|
|
KeyFile: "../test/key/ourdomain.key",
|
|
|
|
VerifyServerHostname: true,
|
2018-12-06 21:51:49 +00:00
|
|
|
VerifyOutgoing: true,
|
2015-05-11 23:05:39 +00:00
|
|
|
Domain: "consul",
|
|
|
|
}
|
|
|
|
|
2020-03-09 20:59:02 +00:00
|
|
|
client, errc := startRPCTLSServer(&config)
|
2015-05-11 23:05:39 +00:00
|
|
|
if client == nil {
|
|
|
|
t.Fatalf("startTLSServer err: %v", <-errc)
|
|
|
|
}
|
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
c, err := NewConfigurator(config, nil)
|
2019-02-26 15:52:07 +00:00
|
|
|
require.NoError(t, err)
|
2019-03-13 09:29:06 +00:00
|
|
|
wrap := c.OutgoingRPCWrapper()
|
2015-05-11 23:05:39 +00:00
|
|
|
|
|
|
|
tlsClient, err := wrap("dc1", client)
|
2019-02-26 15:52:07 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2015-05-11 23:05:39 +00:00
|
|
|
err = tlsClient.(*tls.Conn).Handshake()
|
|
|
|
if _, ok := err.(x509.HostnameError); !ok {
|
|
|
|
t.Fatalf("should get hostname err: %v", err)
|
|
|
|
}
|
2016-11-08 02:15:26 +00:00
|
|
|
tlsClient.Close()
|
2015-05-11 23:05:39 +00:00
|
|
|
|
|
|
|
<-errc
|
2014-06-30 01:11:32 +00:00
|
|
|
}
|
|
|
|
|
2020-03-09 20:59:02 +00:00
|
|
|
func TestConfigurator_outgoingWrapperALPN_OK(t *testing.T) {
|
|
|
|
config := Config{
|
|
|
|
CAFile: "../test/hostname/CertAuth.crt",
|
|
|
|
CertFile: "../test/hostname/Bob.crt",
|
|
|
|
KeyFile: "../test/hostname/Bob.key",
|
|
|
|
VerifyServerHostname: false, // doesn't matter
|
|
|
|
VerifyOutgoing: false, // doesn't matter
|
|
|
|
Domain: "consul",
|
|
|
|
}
|
|
|
|
|
|
|
|
client, errc := startALPNRPCTLSServer(&config, []string{"foo", "bar"})
|
|
|
|
if client == nil {
|
|
|
|
t.Fatalf("startTLSServer err: %v", <-errc)
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := NewConfigurator(config, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
wrap := c.OutgoingALPNRPCWrapper()
|
|
|
|
require.NotNil(t, wrap)
|
|
|
|
|
|
|
|
tlsClient, err := wrap("dc1", "bob", "foo", client)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer tlsClient.Close()
|
|
|
|
|
|
|
|
tlsConn := tlsClient.(*tls.Conn)
|
|
|
|
cs := tlsConn.ConnectionState()
|
|
|
|
require.Equal(t, "foo", cs.NegotiatedProtocol)
|
|
|
|
require.True(t, cs.NegotiatedProtocolIsMutual)
|
|
|
|
|
|
|
|
err = <-errc
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigurator_outgoingWrapperALPN_serverHasNoNodeNameInSAN(t *testing.T) {
|
|
|
|
srvConfig := Config{
|
|
|
|
CAFile: "../test/hostname/CertAuth.crt",
|
|
|
|
CertFile: "../test/hostname/Alice.crt",
|
|
|
|
KeyFile: "../test/hostname/Alice.key",
|
|
|
|
VerifyServerHostname: false, // doesn't matter
|
|
|
|
VerifyOutgoing: false, // doesn't matter
|
|
|
|
Domain: "consul",
|
|
|
|
}
|
|
|
|
|
|
|
|
client, errc := startALPNRPCTLSServer(&srvConfig, []string{"foo", "bar"})
|
|
|
|
if client == nil {
|
|
|
|
t.Fatalf("startTLSServer err: %v", <-errc)
|
|
|
|
}
|
|
|
|
|
|
|
|
config := Config{
|
|
|
|
CAFile: "../test/hostname/CertAuth.crt",
|
|
|
|
CertFile: "../test/hostname/Bob.crt",
|
|
|
|
KeyFile: "../test/hostname/Bob.key",
|
|
|
|
VerifyServerHostname: false, // doesn't matter
|
|
|
|
VerifyOutgoing: false, // doesn't matter
|
|
|
|
Domain: "consul",
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := NewConfigurator(config, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
wrap := c.OutgoingALPNRPCWrapper()
|
|
|
|
require.NotNil(t, wrap)
|
|
|
|
|
|
|
|
_, err = wrap("dc1", "bob", "foo", client)
|
|
|
|
require.Error(t, err)
|
|
|
|
_, ok := err.(x509.HostnameError)
|
|
|
|
require.True(t, ok)
|
|
|
|
client.Close()
|
|
|
|
|
|
|
|
<-errc
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigurator_outgoingWrapperALPN_BadDC(t *testing.T) {
|
|
|
|
config := Config{
|
|
|
|
CAFile: "../test/hostname/CertAuth.crt",
|
|
|
|
CertFile: "../test/hostname/Bob.crt",
|
|
|
|
KeyFile: "../test/hostname/Bob.key",
|
|
|
|
VerifyServerHostname: false, // doesn't matter
|
|
|
|
VerifyOutgoing: false, // doesn't matter
|
|
|
|
Domain: "consul",
|
|
|
|
}
|
|
|
|
|
|
|
|
client, errc := startALPNRPCTLSServer(&config, []string{"foo", "bar"})
|
|
|
|
if client == nil {
|
|
|
|
t.Fatalf("startTLSServer err: %v", <-errc)
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := NewConfigurator(config, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
wrap := c.OutgoingALPNRPCWrapper()
|
|
|
|
|
|
|
|
_, err = wrap("dc2", "bob", "foo", client)
|
|
|
|
require.Error(t, err)
|
|
|
|
_, ok := err.(x509.HostnameError)
|
|
|
|
require.True(t, ok)
|
|
|
|
client.Close()
|
|
|
|
|
|
|
|
<-errc
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigurator_outgoingWrapperALPN_BadCert(t *testing.T) {
|
|
|
|
config := Config{
|
|
|
|
CAFile: "../test/ca/root.cer",
|
|
|
|
CertFile: "../test/key/ourdomain.cer",
|
|
|
|
KeyFile: "../test/key/ourdomain.key",
|
|
|
|
VerifyServerHostname: false, // doesn't matter
|
|
|
|
VerifyOutgoing: false, // doesn't matter
|
|
|
|
Domain: "consul",
|
|
|
|
}
|
|
|
|
|
|
|
|
client, errc := startALPNRPCTLSServer(&config, []string{"foo", "bar"})
|
|
|
|
if client == nil {
|
|
|
|
t.Fatalf("startTLSServer err: %v", <-errc)
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := NewConfigurator(config, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
wrap := c.OutgoingALPNRPCWrapper()
|
|
|
|
|
|
|
|
_, err = wrap("dc1", "bob", "foo", client)
|
|
|
|
require.Error(t, err)
|
|
|
|
_, ok := err.(x509.HostnameError)
|
|
|
|
require.True(t, ok)
|
|
|
|
client.Close()
|
|
|
|
|
|
|
|
<-errc
|
|
|
|
}
|
|
|
|
|
2019-02-26 15:52:07 +00:00
|
|
|
func TestConfigurator_wrapTLS_OK(t *testing.T) {
|
2019-03-13 09:29:06 +00:00
|
|
|
config := Config{
|
2014-06-30 01:11:32 +00:00
|
|
|
CAFile: "../test/ca/root.cer",
|
|
|
|
CertFile: "../test/key/ourdomain.cer",
|
|
|
|
KeyFile: "../test/key/ourdomain.key",
|
|
|
|
VerifyOutgoing: true,
|
|
|
|
}
|
|
|
|
|
2020-03-09 20:59:02 +00:00
|
|
|
client, errc := startRPCTLSServer(&config)
|
2014-06-30 01:11:32 +00:00
|
|
|
if client == nil {
|
|
|
|
t.Fatalf("startTLSServer err: %v", <-errc)
|
|
|
|
}
|
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
c, err := NewConfigurator(config, nil)
|
2019-02-26 15:52:07 +00:00
|
|
|
require.NoError(t, err)
|
2014-06-30 01:11:32 +00:00
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
tlsClient, err := c.wrapTLSClient("dc1", client)
|
2019-02-26 15:52:07 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
tlsClient.Close()
|
2014-06-30 01:11:32 +00:00
|
|
|
err = <-errc
|
2019-02-26 15:52:07 +00:00
|
|
|
require.NoError(t, err)
|
2014-06-30 01:11:32 +00:00
|
|
|
}
|
|
|
|
|
2019-02-26 15:52:07 +00:00
|
|
|
func TestConfigurator_wrapTLS_BadCert(t *testing.T) {
|
2014-06-30 01:11:32 +00:00
|
|
|
serverConfig := &Config{
|
|
|
|
CertFile: "../test/key/ssl-cert-snakeoil.pem",
|
|
|
|
KeyFile: "../test/key/ssl-cert-snakeoil.key",
|
|
|
|
}
|
|
|
|
|
2020-03-09 20:59:02 +00:00
|
|
|
client, errc := startRPCTLSServer(serverConfig)
|
2014-06-30 01:11:32 +00:00
|
|
|
if client == nil {
|
|
|
|
t.Fatalf("startTLSServer err: %v", <-errc)
|
|
|
|
}
|
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
clientConfig := Config{
|
2014-06-30 01:11:32 +00:00
|
|
|
CAFile: "../test/ca/root.cer",
|
|
|
|
VerifyOutgoing: true,
|
|
|
|
}
|
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
c, err := NewConfigurator(clientConfig, nil)
|
2019-02-26 15:52:07 +00:00
|
|
|
require.NoError(t, err)
|
2019-03-13 09:29:06 +00:00
|
|
|
tlsClient, err := c.wrapTLSClient("dc1", client)
|
2019-02-26 15:52:07 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Nil(t, tlsClient)
|
2014-06-30 01:11:32 +00:00
|
|
|
|
|
|
|
err = <-errc
|
2019-02-26 15:52:07 +00:00
|
|
|
require.NoError(t, err)
|
2017-02-01 20:52:04 +00:00
|
|
|
}
|
2017-04-27 08:29:39 +00:00
|
|
|
|
|
|
|
func TestConfig_ParseCiphers(t *testing.T) {
|
|
|
|
testOk := strings.Join([]string{
|
2020-03-10 20:44:26 +00:00
|
|
|
"TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA",
|
|
|
|
"TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256",
|
2017-04-27 08:29:39 +00:00
|
|
|
"TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256",
|
2020-03-10 20:44:26 +00:00
|
|
|
"TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA",
|
2017-04-27 08:29:39 +00:00
|
|
|
"TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384",
|
|
|
|
"TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA",
|
2020-03-10 20:44:26 +00:00
|
|
|
"TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256",
|
|
|
|
"TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
|
2017-04-27 08:29:39 +00:00
|
|
|
"TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA",
|
2020-03-10 20:44:26 +00:00
|
|
|
"TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384",
|
2017-04-27 08:29:39 +00:00
|
|
|
}, ",")
|
|
|
|
ciphers := []uint16{
|
2020-03-10 20:44:26 +00:00
|
|
|
tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
|
|
|
|
tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,
|
2017-04-27 08:29:39 +00:00
|
|
|
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
|
2020-03-10 20:44:26 +00:00
|
|
|
tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
|
2017-04-27 08:29:39 +00:00
|
|
|
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
|
|
|
|
tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
|
2020-03-10 20:44:26 +00:00
|
|
|
tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
|
|
|
|
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
|
2017-04-27 08:29:39 +00:00
|
|
|
tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
|
2020-03-10 20:44:26 +00:00
|
|
|
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
|
2017-04-27 08:29:39 +00:00
|
|
|
}
|
|
|
|
v, err := ParseCiphers(testOk)
|
2019-03-13 09:29:06 +00:00
|
|
|
require.NoError(t, err)
|
2017-04-27 08:29:39 +00:00
|
|
|
if got, want := v, ciphers; !reflect.DeepEqual(got, want) {
|
|
|
|
t.Fatalf("got ciphers %#v want %#v", got, want)
|
|
|
|
}
|
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
_, err = ParseCiphers("TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,cipherX")
|
|
|
|
require.Error(t, err)
|
2019-02-26 15:52:07 +00:00
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
v, err = ParseCiphers("")
|
2019-02-26 15:52:07 +00:00
|
|
|
require.NoError(t, err)
|
2019-03-13 09:29:06 +00:00
|
|
|
require.Equal(t, []uint16{}, v)
|
2019-02-26 15:52:07 +00:00
|
|
|
}
|
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
func TestConfigurator_loadKeyPair(t *testing.T) {
|
|
|
|
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 {
|
|
|
|
info := fmt.Sprintf("case %d", i)
|
2019-03-13 09:29:06 +00:00
|
|
|
cert1, err1 := loadKeyPair(v.cert, v.key)
|
|
|
|
config := &Config{CertFile: v.cert, KeyFile: v.key}
|
|
|
|
cert2, err2 := config.KeyPair()
|
|
|
|
if v.shoulderr {
|
2019-06-27 20:22:07 +00:00
|
|
|
require.Error(t, err1, info)
|
|
|
|
require.Error(t, err2, info)
|
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, cert1, info)
|
|
|
|
require.Nil(t, cert2, info)
|
2019-03-13 09:29:06 +00:00
|
|
|
} else {
|
2019-06-27 20:22:07 +00:00
|
|
|
require.NotNil(t, cert1, info)
|
|
|
|
require.NotNil(t, cert2, info)
|
2019-03-13 09:29:06 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
func TestConfigurator_NewConfigurator(t *testing.T) {
|
2020-01-28 23:50:41 +00:00
|
|
|
logger := testutil.Logger(t)
|
2019-03-13 09:29:06 +00:00
|
|
|
c, err := NewConfigurator(Config{}, logger)
|
2019-02-27 09:14:59 +00:00
|
|
|
require.NoError(t, err)
|
2019-03-13 09:29:06 +00:00
|
|
|
require.NotNil(t, c)
|
2019-02-27 09:14:59 +00:00
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
c, err = NewConfigurator(Config{VerifyOutgoing: true}, nil)
|
2019-02-27 09:14:59 +00:00
|
|
|
require.Error(t, err)
|
2019-03-13 09:29:06 +00:00
|
|
|
require.Nil(t, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigurator_ErrorPropagation(t *testing.T) {
|
|
|
|
type variant struct {
|
|
|
|
config Config
|
|
|
|
shouldErr bool
|
|
|
|
excludeCheck bool
|
|
|
|
}
|
|
|
|
cafile := "../test/ca/root.cer"
|
|
|
|
capath := "../test/ca_path"
|
|
|
|
certfile := "../test/key/ourdomain.cer"
|
|
|
|
keyfile := "../test/key/ourdomain.key"
|
|
|
|
variants := []variant{
|
2019-06-27 20:22:07 +00:00
|
|
|
{Config{}, false, false}, // 1
|
|
|
|
{Config{TLSMinVersion: "tls9"}, true, false}, // 1
|
|
|
|
{Config{TLSMinVersion: ""}, false, false}, // 2
|
|
|
|
{Config{VerifyOutgoing: true, CAFile: "", CAPath: ""}, true, false}, // 6
|
|
|
|
{Config{VerifyOutgoing: false, CAFile: "", CAPath: ""}, false, false}, // 7
|
2019-03-13 09:29:06 +00:00
|
|
|
{Config{VerifyOutgoing: false, CAFile: cafile, CAPath: ""},
|
2019-06-27 20:22:07 +00:00
|
|
|
false, false}, // 8
|
2019-03-13 09:29:06 +00:00
|
|
|
{Config{VerifyOutgoing: false, CAFile: "", CAPath: capath},
|
2019-06-27 20:22:07 +00:00
|
|
|
false, false}, // 9
|
2019-03-13 09:29:06 +00:00
|
|
|
{Config{VerifyOutgoing: false, CAFile: cafile, CAPath: capath},
|
2019-06-27 20:22:07 +00:00
|
|
|
false, false}, // 10
|
2019-03-13 09:29:06 +00:00
|
|
|
{Config{VerifyOutgoing: true, CAFile: cafile, CAPath: ""},
|
2019-06-27 20:22:07 +00:00
|
|
|
false, false}, // 11
|
2019-03-13 09:29:06 +00:00
|
|
|
{Config{VerifyOutgoing: true, CAFile: "", CAPath: capath},
|
2019-06-27 20:22:07 +00:00
|
|
|
false, false}, // 12
|
2019-03-13 09:29:06 +00:00
|
|
|
{Config{VerifyOutgoing: true, CAFile: cafile, CAPath: capath},
|
2019-06-27 20:22:07 +00:00
|
|
|
false, false}, // 13
|
|
|
|
{Config{VerifyIncoming: true, CAFile: "", CAPath: ""}, true, false}, // 14
|
2019-03-13 09:29:06 +00:00
|
|
|
{Config{VerifyIncomingRPC: true, CAFile: "", CAPath: ""},
|
2019-06-27 20:22:07 +00:00
|
|
|
true, false}, // 15
|
2019-03-13 09:29:06 +00:00
|
|
|
{Config{VerifyIncomingHTTPS: true, CAFile: "", CAPath: ""},
|
2019-06-27 20:22:07 +00:00
|
|
|
true, false}, // 16
|
|
|
|
{Config{VerifyIncoming: true, CAFile: cafile, CAPath: ""}, true, false}, // 17
|
|
|
|
{Config{VerifyIncoming: true, CAFile: "", CAPath: capath}, true, false}, // 18
|
2019-03-13 09:29:06 +00:00
|
|
|
{Config{VerifyIncoming: true, CAFile: "", CAPath: capath,
|
2019-06-27 20:22:07 +00:00
|
|
|
CertFile: certfile, KeyFile: keyfile}, false, false}, // 19
|
|
|
|
{Config{CertFile: "bogus", KeyFile: "bogus"}, true, true}, // 20
|
|
|
|
{Config{CAFile: "bogus"}, true, true}, // 21
|
|
|
|
{Config{CAPath: "bogus"}, true, true}, // 22
|
2019-03-13 09:29:06 +00:00
|
|
|
}
|
2020-02-19 22:22:31 +00:00
|
|
|
for _, v := range tlsVersions() {
|
|
|
|
variants = append(variants, variant{Config{TLSMinVersion: v}, false, false})
|
|
|
|
}
|
2019-03-13 09:29:06 +00:00
|
|
|
|
2019-06-27 20:22:07 +00:00
|
|
|
c := Configurator{autoEncrypt: &autoEncrypt{}, manual: &manual{}}
|
2019-03-13 09:29:06 +00:00
|
|
|
for i, v := range variants {
|
2019-06-27 20:22:07 +00:00
|
|
|
info := fmt.Sprintf("case %d, config: %+v", i, v.config)
|
2019-03-13 09:29:06 +00:00
|
|
|
_, err1 := NewConfigurator(v.config, nil)
|
|
|
|
err2 := c.Update(v.config)
|
|
|
|
|
|
|
|
var err3 error
|
|
|
|
if !v.excludeCheck {
|
|
|
|
cert, err := v.config.KeyPair()
|
|
|
|
require.NoError(t, err, info)
|
2019-06-27 20:22:07 +00:00
|
|
|
pems, err := loadCAs(v.config.CAFile, v.config.CAPath)
|
2019-03-13 09:29:06 +00:00
|
|
|
require.NoError(t, err, info)
|
2019-06-27 20:22:07 +00:00
|
|
|
pool, err := pool(pems)
|
|
|
|
require.NoError(t, err, info)
|
|
|
|
err3 = c.check(v.config, pool, cert)
|
2019-03-13 09:29:06 +00:00
|
|
|
}
|
|
|
|
if v.shouldErr {
|
|
|
|
require.Error(t, err1, info)
|
|
|
|
require.Error(t, err2, info)
|
|
|
|
if !v.excludeCheck {
|
|
|
|
require.Error(t, err3, info)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err1, info)
|
|
|
|
require.NoError(t, err2, info)
|
|
|
|
if !v.excludeCheck {
|
|
|
|
require.NoError(t, err3, info)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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)
|
2019-03-13 09:29:06 +00:00
|
|
|
tlsConf := c.commonTLSConfig(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
|
|
|
}
|
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
func TestConfigurator_loadCAs(t *testing.T) {
|
|
|
|
type variant struct {
|
|
|
|
cafile, capath string
|
|
|
|
shouldErr bool
|
|
|
|
isNil bool
|
|
|
|
count int
|
|
|
|
}
|
|
|
|
variants := []variant{
|
|
|
|
{"", "", false, true, 0},
|
|
|
|
{"bogus", "", true, true, 0},
|
|
|
|
{"", "bogus", true, true, 0},
|
|
|
|
{"", "../test/bin", true, true, 0},
|
|
|
|
{"../test/ca/root.cer", "", false, false, 1},
|
|
|
|
{"", "../test/ca_path", false, false, 2},
|
|
|
|
{"../test/ca/root.cer", "../test/ca_path", false, false, 1},
|
|
|
|
}
|
|
|
|
for i, v := range variants {
|
2019-06-27 20:22:07 +00:00
|
|
|
pems, err1 := loadCAs(v.cafile, v.capath)
|
|
|
|
pool, err2 := pool(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)
|
|
|
|
require.Len(t, pool.Subjects(), v.count, info)
|
|
|
|
require.Len(t, pems, v.count, info)
|
2019-03-13 09:29:06 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-27 09:14:59 +00:00
|
|
|
}
|
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
func TestConfigurator_CommonTLSConfigInsecureSkipVerify(t *testing.T) {
|
|
|
|
c, err := NewConfigurator(Config{}, nil)
|
2019-02-27 09:14:59 +00:00
|
|
|
require.NoError(t, err)
|
2019-03-13 09:29:06 +00:00
|
|
|
tlsConf := c.commonTLSConfig(false)
|
|
|
|
require.True(t, tlsConf.InsecureSkipVerify)
|
2019-02-27 09:14:59 +00:00
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
require.NoError(t, c.Update(Config{VerifyServerHostname: false}))
|
|
|
|
tlsConf = c.commonTLSConfig(false)
|
|
|
|
require.True(t, tlsConf.InsecureSkipVerify)
|
2019-02-27 09:14:59 +00:00
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
require.NoError(t, c.Update(Config{VerifyServerHostname: true}))
|
|
|
|
tlsConf = c.commonTLSConfig(false)
|
|
|
|
require.False(t, tlsConf.InsecureSkipVerify)
|
2019-02-27 09:14:59 +00:00
|
|
|
}
|
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
func TestConfigurator_CommonTLSConfigPreferServerCipherSuites(t *testing.T) {
|
|
|
|
c, err := NewConfigurator(Config{}, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
tlsConf := c.commonTLSConfig(false)
|
|
|
|
require.False(t, tlsConf.PreferServerCipherSuites)
|
2019-02-27 09:14:59 +00:00
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
require.NoError(t, c.Update(Config{PreferServerCipherSuites: false}))
|
|
|
|
tlsConf = c.commonTLSConfig(false)
|
|
|
|
require.False(t, tlsConf.PreferServerCipherSuites)
|
2019-02-27 09:14:59 +00:00
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
require.NoError(t, c.Update(Config{PreferServerCipherSuites: true}))
|
|
|
|
tlsConf = c.commonTLSConfig(false)
|
|
|
|
require.True(t, tlsConf.PreferServerCipherSuites)
|
2019-02-27 09:14:59 +00:00
|
|
|
}
|
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
func TestConfigurator_CommonTLSConfigCipherSuites(t *testing.T) {
|
|
|
|
c, err := NewConfigurator(Config{}, nil)
|
2019-02-27 09:14:59 +00:00
|
|
|
require.NoError(t, err)
|
2019-03-13 09:29:06 +00:00
|
|
|
tlsConf := c.commonTLSConfig(false)
|
|
|
|
require.Empty(t, tlsConf.CipherSuites)
|
2019-02-27 09:14:59 +00:00
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
conf := Config{CipherSuites: []uint16{
|
|
|
|
tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305}}
|
|
|
|
require.NoError(t, c.Update(conf))
|
|
|
|
tlsConf = c.commonTLSConfig(false)
|
|
|
|
require.Equal(t, conf.CipherSuites, tlsConf.CipherSuites)
|
|
|
|
}
|
2019-02-27 09:14:59 +00:00
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
func TestConfigurator_CommonTLSConfigGetClientCertificate(t *testing.T) {
|
|
|
|
c, err := NewConfigurator(Config{}, nil)
|
|
|
|
require.NoError(t, err)
|
2019-02-27 09:14:59 +00:00
|
|
|
|
2019-11-12 18:40:32 +00:00
|
|
|
cert, err := c.commonTLSConfig(false).GetClientCertificate(nil)
|
2019-02-27 09:14:59 +00:00
|
|
|
require.NoError(t, err)
|
2019-11-12 18:40:32 +00:00
|
|
|
require.Nil(t, cert)
|
2019-02-27 09:14:59 +00:00
|
|
|
|
2019-11-12 18:40:32 +00:00
|
|
|
c1, err := loadKeyPair("../test/key/something_expired.cer", "../test/key/something_expired.key")
|
|
|
|
require.NoError(t, err)
|
|
|
|
c.manual.cert = c1
|
|
|
|
cert, err = c.commonTLSConfig(false).GetClientCertificate(nil)
|
2019-02-27 09:14:59 +00:00
|
|
|
require.NoError(t, err)
|
2019-06-27 20:22:07 +00:00
|
|
|
require.Equal(t, c.manual.cert, cert)
|
2019-02-27 09:14:59 +00:00
|
|
|
|
2019-11-12 18:40:32 +00:00
|
|
|
c2, err := loadKeyPair("../test/key/ourdomain.cer", "../test/key/ourdomain.key")
|
|
|
|
require.NoError(t, err)
|
|
|
|
c.autoEncrypt.cert = c2
|
2019-03-13 09:29:06 +00:00
|
|
|
cert, err = c.commonTLSConfig(false).GetClientCertificate(nil)
|
2019-02-27 09:14:59 +00:00
|
|
|
require.NoError(t, err)
|
2019-11-12 18:40:32 +00:00
|
|
|
require.Equal(t, c.autoEncrypt.cert, cert)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigurator_CommonTLSConfigGetCertificate(t *testing.T) {
|
|
|
|
c, err := NewConfigurator(Config{}, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
cert, err := c.commonTLSConfig(false).GetCertificate(nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Nil(t, cert)
|
|
|
|
|
|
|
|
// Setting a certificate as the auto-encrypt cert will return it as the regular server certificate
|
|
|
|
c1, err := loadKeyPair("../test/key/something_expired.cer", "../test/key/something_expired.key")
|
|
|
|
require.NoError(t, err)
|
|
|
|
c.autoEncrypt.cert = c1
|
|
|
|
cert, err = c.commonTLSConfig(false).GetCertificate(nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, c.autoEncrypt.cert, cert)
|
|
|
|
|
|
|
|
// Setting a different certificate as a manual cert will override the auto-encrypt cert and instead return the manual cert
|
|
|
|
c2, err := loadKeyPair("../test/key/ourdomain.cer", "../test/key/ourdomain.key")
|
|
|
|
require.NoError(t, err)
|
|
|
|
c.manual.cert = c2
|
|
|
|
cert, err = c.commonTLSConfig(false).GetCertificate(nil)
|
|
|
|
require.NoError(t, err)
|
2019-06-27 20:22:07 +00:00
|
|
|
require.Equal(t, c.manual.cert, cert)
|
2019-02-27 09:14:59 +00:00
|
|
|
}
|
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
func TestConfigurator_CommonTLSConfigCAs(t *testing.T) {
|
|
|
|
c, err := NewConfigurator(Config{}, nil)
|
2019-02-27 09:14:59 +00:00
|
|
|
require.NoError(t, err)
|
2019-03-13 09:29:06 +00:00
|
|
|
require.Nil(t, c.commonTLSConfig(false).ClientCAs)
|
|
|
|
require.Nil(t, c.commonTLSConfig(false).RootCAs)
|
2019-02-27 09:14:59 +00:00
|
|
|
|
2019-06-27 20:22:07 +00:00
|
|
|
c.caPool = &x509.CertPool{}
|
|
|
|
require.Equal(t, c.caPool, c.commonTLSConfig(false).ClientCAs)
|
|
|
|
require.Equal(t, c.caPool, c.commonTLSConfig(false).RootCAs)
|
2019-03-13 09:29:06 +00:00
|
|
|
}
|
2019-02-27 09:14:59 +00:00
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
func TestConfigurator_CommonTLSConfigTLSMinVersion(t *testing.T) {
|
|
|
|
c, err := NewConfigurator(Config{TLSMinVersion: ""}, nil)
|
2019-02-27 09:14:59 +00:00
|
|
|
require.NoError(t, err)
|
2019-03-13 09:29:06 +00:00
|
|
|
require.Equal(t, c.commonTLSConfig(false).MinVersion, TLSLookup["tls10"])
|
2019-03-05 20:35:43 +00:00
|
|
|
|
2020-02-19 22:22:31 +00:00
|
|
|
for _, version := range tlsVersions() {
|
2019-03-13 09:29:06 +00:00
|
|
|
require.NoError(t, c.Update(Config{TLSMinVersion: version}))
|
|
|
|
require.Equal(t, c.commonTLSConfig(false).MinVersion,
|
|
|
|
TLSLookup[version])
|
|
|
|
}
|
2019-03-05 20:35:43 +00:00
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
require.Error(t, c.Update(Config{TLSMinVersion: "tlsBOGUS"}))
|
2019-02-27 09:14:59 +00:00
|
|
|
}
|
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
func TestConfigurator_CommonTLSConfigVerifyIncoming(t *testing.T) {
|
2019-06-27 20:22:07 +00:00
|
|
|
c := Configurator{base: &Config{}, autoEncrypt: &autoEncrypt{}}
|
2019-03-13 09:29:06 +00:00
|
|
|
type variant struct {
|
2019-06-27 20:22:07 +00:00
|
|
|
verify bool
|
|
|
|
expected tls.ClientAuthType
|
2019-03-13 09:29:06 +00:00
|
|
|
}
|
|
|
|
variants := []variant{
|
2019-06-27 20:22:07 +00:00
|
|
|
{true, tls.RequireAndVerifyClientCert},
|
|
|
|
{false, tls.NoClientCert},
|
2019-03-13 09:29:06 +00:00
|
|
|
}
|
|
|
|
for _, v := range variants {
|
2019-06-27 20:22:07 +00:00
|
|
|
require.Equal(t, v.expected, c.commonTLSConfig(v.verify).ClientAuth)
|
2019-03-13 09:29:06 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-27 09:14:59 +00:00
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
func TestConfigurator_OutgoingRPCTLSDisabled(t *testing.T) {
|
2019-06-27 20:22:07 +00:00
|
|
|
c := Configurator{base: &Config{}, autoEncrypt: &autoEncrypt{}}
|
2019-03-13 09:29:06 +00:00
|
|
|
type variant struct {
|
2019-06-27 20:22:07 +00:00
|
|
|
verify bool
|
|
|
|
autoEncryptTLS bool
|
|
|
|
pool *x509.CertPool
|
|
|
|
expected bool
|
2019-03-13 09:29:06 +00:00
|
|
|
}
|
|
|
|
variants := []variant{
|
2019-06-27 20:22:07 +00:00
|
|
|
{false, false, nil, true},
|
|
|
|
{true, false, nil, false},
|
|
|
|
{false, true, nil, false},
|
|
|
|
{true, true, nil, false},
|
|
|
|
|
2020-04-30 20:12:17 +00:00
|
|
|
// {false, false, &x509.CertPool{}, false},
|
2019-06-27 20:22:07 +00:00
|
|
|
{true, false, &x509.CertPool{}, false},
|
|
|
|
{false, true, &x509.CertPool{}, false},
|
|
|
|
{true, true, &x509.CertPool{}, false},
|
2019-03-13 09:29:06 +00:00
|
|
|
}
|
|
|
|
for i, v := range variants {
|
|
|
|
info := fmt.Sprintf("case %d", i)
|
2019-06-27 20:22:07 +00:00
|
|
|
c.caPool = v.pool
|
2019-03-13 09:29:06 +00:00
|
|
|
c.base.VerifyOutgoing = v.verify
|
2019-06-27 20:22:07 +00:00
|
|
|
c.base.AutoEncryptTLS = v.autoEncryptTLS
|
2019-03-13 09:29:06 +00:00
|
|
|
require.Equal(t, v.expected, c.outgoingRPCTLSDisabled(), info)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-09 20:59:02 +00:00
|
|
|
func TestConfigurator_MutualTLSCapable(t *testing.T) {
|
|
|
|
t.Run("no ca", func(t *testing.T) {
|
|
|
|
config := Config{
|
|
|
|
Domain: "consul",
|
|
|
|
}
|
|
|
|
c, err := NewConfigurator(config, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.False(t, c.mutualTLSCapable())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("ca and no keys", func(t *testing.T) {
|
|
|
|
config := Config{
|
|
|
|
CAFile: "../test/hostname/CertAuth.crt",
|
|
|
|
Domain: "consul",
|
|
|
|
}
|
|
|
|
c, err := NewConfigurator(config, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.False(t, c.mutualTLSCapable())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("ca and manual key", func(t *testing.T) {
|
|
|
|
config := Config{
|
|
|
|
CAFile: "../test/hostname/CertAuth.crt",
|
|
|
|
CertFile: "../test/hostname/Bob.crt",
|
|
|
|
KeyFile: "../test/hostname/Bob.key",
|
|
|
|
Domain: "consul",
|
|
|
|
}
|
|
|
|
c, err := NewConfigurator(config, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.True(t, c.mutualTLSCapable())
|
|
|
|
})
|
|
|
|
|
|
|
|
loadFile := func(t *testing.T, path string) string {
|
|
|
|
data, err := ioutil.ReadFile(path)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return string(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
require.NoError(t, c.UpdateAutoEncryptCA([]string{caPEM}))
|
|
|
|
|
|
|
|
require.False(t, c.mutualTLSCapable())
|
|
|
|
})
|
|
|
|
|
|
|
|
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")
|
|
|
|
require.NoError(t, c.UpdateAutoEncryptCA([]string{caPEM}))
|
|
|
|
require.NoError(t, c.UpdateAutoEncryptCert(certPEM, keyPEM))
|
|
|
|
|
|
|
|
require.True(t, c.mutualTLSCapable())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
func TestConfigurator_VerifyIncomingRPC(t *testing.T) {
|
|
|
|
c := Configurator{base: &Config{
|
|
|
|
VerifyIncomingRPC: true,
|
|
|
|
}}
|
|
|
|
verify := c.verifyIncomingRPC()
|
|
|
|
require.Equal(t, c.base.VerifyIncomingRPC, verify)
|
|
|
|
}
|
2019-02-27 09:14:59 +00:00
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
func TestConfigurator_VerifyIncomingHTTPS(t *testing.T) {
|
|
|
|
c := Configurator{base: &Config{
|
|
|
|
VerifyIncomingHTTPS: true,
|
|
|
|
}}
|
|
|
|
verify := c.verifyIncomingHTTPS()
|
|
|
|
require.Equal(t, c.base.VerifyIncomingHTTPS, verify)
|
2019-02-27 09:14:59 +00:00
|
|
|
}
|
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
func TestConfigurator_EnableAgentTLSForChecks(t *testing.T) {
|
|
|
|
c := Configurator{base: &Config{
|
|
|
|
EnableAgentTLSForChecks: true,
|
|
|
|
}}
|
|
|
|
enabled := c.enableAgentTLSForChecks()
|
|
|
|
require.Equal(t, c.base.EnableAgentTLSForChecks, enabled)
|
|
|
|
}
|
2019-02-27 09:14:59 +00:00
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
func TestConfigurator_IncomingRPCConfig(t *testing.T) {
|
|
|
|
c, err := NewConfigurator(Config{
|
|
|
|
VerifyIncomingRPC: true,
|
|
|
|
CAFile: "../test/ca/root.cer",
|
|
|
|
CertFile: "../test/key/ourdomain.cer",
|
|
|
|
KeyFile: "../test/key/ourdomain.key",
|
|
|
|
}, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
tlsConf := c.IncomingRPCConfig()
|
2019-02-27 09:14:59 +00:00
|
|
|
require.Equal(t, tls.RequireAndVerifyClientCert, tlsConf.ClientAuth)
|
2020-03-09 20:59:02 +00:00
|
|
|
require.Empty(t, tlsConf.NextProtos)
|
|
|
|
require.Empty(t, tlsConf.ServerName)
|
|
|
|
|
|
|
|
require.NotNil(t, tlsConf.GetConfigForClient)
|
|
|
|
tlsConf, err = tlsConf.GetConfigForClient(nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, tls.RequireAndVerifyClientCert, tlsConf.ClientAuth)
|
|
|
|
require.Empty(t, tlsConf.NextProtos)
|
|
|
|
require.Empty(t, tlsConf.ServerName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigurator_IncomingALPNRPCConfig(t *testing.T) {
|
|
|
|
c, err := NewConfigurator(Config{
|
|
|
|
VerifyIncomingRPC: false, // ignored, assumed true
|
|
|
|
CAFile: "../test/ca/root.cer",
|
|
|
|
CertFile: "../test/key/ourdomain.cer",
|
|
|
|
KeyFile: "../test/key/ourdomain.key",
|
|
|
|
}, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
tlsConf := c.IncomingALPNRPCConfig([]string{"foo/1", "bar/2"})
|
|
|
|
require.Equal(t, tls.RequireAndVerifyClientCert, tlsConf.ClientAuth)
|
|
|
|
require.False(t, tlsConf.InsecureSkipVerify)
|
|
|
|
require.Equal(t, []string{"foo/1", "bar/2"}, tlsConf.NextProtos)
|
|
|
|
require.Empty(t, tlsConf.ServerName)
|
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
require.NotNil(t, tlsConf.GetConfigForClient)
|
|
|
|
tlsConf, err = tlsConf.GetConfigForClient(nil)
|
2019-02-27 09:14:59 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, tls.RequireAndVerifyClientCert, tlsConf.ClientAuth)
|
2020-03-09 20:59:02 +00:00
|
|
|
require.False(t, tlsConf.InsecureSkipVerify)
|
|
|
|
require.Equal(t, []string{"foo/1", "bar/2"}, tlsConf.NextProtos)
|
|
|
|
require.Empty(t, tlsConf.ServerName)
|
2019-02-27 09:14:59 +00:00
|
|
|
}
|
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
func TestConfigurator_IncomingHTTPSConfig(t *testing.T) {
|
2019-06-27 20:22:07 +00:00
|
|
|
c := Configurator{base: &Config{}, autoEncrypt: &autoEncrypt{}}
|
2019-03-13 09:29:06 +00:00
|
|
|
require.Equal(t, []string{"h2", "http/1.1"}, c.IncomingHTTPSConfig().NextProtos)
|
2019-02-27 09:14:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigurator_OutgoingTLSConfigForChecks(t *testing.T) {
|
2019-03-13 09:29:06 +00:00
|
|
|
c := Configurator{base: &Config{
|
|
|
|
TLSMinVersion: "tls12",
|
|
|
|
EnableAgentTLSForChecks: false,
|
2019-06-27 20:22:07 +00:00
|
|
|
}, autoEncrypt: &autoEncrypt{}}
|
2019-03-13 09:29:06 +00:00
|
|
|
tlsConf := c.OutgoingTLSConfigForCheck(true)
|
|
|
|
require.Equal(t, true, tlsConf.InsecureSkipVerify)
|
|
|
|
require.Equal(t, uint16(0), tlsConf.MinVersion)
|
|
|
|
|
|
|
|
c.base.EnableAgentTLSForChecks = true
|
|
|
|
c.base.ServerName = "servername"
|
|
|
|
tlsConf = c.OutgoingTLSConfigForCheck(true)
|
|
|
|
require.Equal(t, true, tlsConf.InsecureSkipVerify)
|
|
|
|
require.Equal(t, TLSLookup[c.base.TLSMinVersion], tlsConf.MinVersion)
|
|
|
|
require.Equal(t, c.base.ServerName, tlsConf.ServerName)
|
|
|
|
}
|
2019-02-27 09:14:59 +00:00
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
func TestConfigurator_OutgoingRPCConfig(t *testing.T) {
|
2020-03-09 20:59:02 +00:00
|
|
|
c := &Configurator{base: &Config{}, autoEncrypt: &autoEncrypt{}}
|
2019-03-13 09:29:06 +00:00
|
|
|
require.Nil(t, c.OutgoingRPCConfig())
|
2020-03-09 20:59:02 +00:00
|
|
|
|
|
|
|
c, err := NewConfigurator(Config{
|
|
|
|
VerifyOutgoing: true,
|
|
|
|
CAFile: "../test/ca/root.cer",
|
|
|
|
}, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
tlsConf := c.OutgoingRPCConfig()
|
|
|
|
require.NotNil(t, tlsConf)
|
|
|
|
require.Equal(t, tls.NoClientCert, tlsConf.ClientAuth)
|
|
|
|
require.True(t, tlsConf.InsecureSkipVerify)
|
|
|
|
require.Empty(t, tlsConf.NextProtos)
|
|
|
|
require.Empty(t, tlsConf.ServerName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigurator_OutgoingALPNRPCConfig(t *testing.T) {
|
|
|
|
c := &Configurator{base: &Config{}, autoEncrypt: &autoEncrypt{}}
|
|
|
|
require.Nil(t, c.OutgoingALPNRPCConfig())
|
|
|
|
|
|
|
|
c, err := NewConfigurator(Config{
|
|
|
|
VerifyOutgoing: false, // ignored, assumed true
|
|
|
|
CAFile: "../test/ca/root.cer",
|
|
|
|
CertFile: "../test/key/ourdomain.cer",
|
|
|
|
KeyFile: "../test/key/ourdomain.key",
|
|
|
|
}, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
tlsConf := c.OutgoingALPNRPCConfig()
|
|
|
|
require.NotNil(t, tlsConf)
|
|
|
|
require.Equal(t, tls.RequireAndVerifyClientCert, tlsConf.ClientAuth)
|
|
|
|
require.False(t, tlsConf.InsecureSkipVerify)
|
|
|
|
require.Empty(t, tlsConf.NextProtos)
|
|
|
|
require.Empty(t, tlsConf.ServerName)
|
2019-03-13 09:29:06 +00:00
|
|
|
}
|
2019-02-27 09:14:59 +00:00
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
func TestConfigurator_OutgoingRPCWrapper(t *testing.T) {
|
2020-03-09 20:59:02 +00:00
|
|
|
c := &Configurator{base: &Config{}, autoEncrypt: &autoEncrypt{}}
|
2020-04-30 20:12:17 +00:00
|
|
|
wrapper := c.OutgoingRPCWrapper()
|
|
|
|
require.NotNil(t, wrapper)
|
|
|
|
conn := &net.TCPConn{}
|
|
|
|
cWrap, err := wrapper("", conn)
|
|
|
|
require.Equal(t, conn, cWrap)
|
2020-03-09 20:59:02 +00:00
|
|
|
|
2020-04-30 20:12:17 +00:00
|
|
|
c, err = NewConfigurator(Config{
|
2020-03-09 20:59:02 +00:00
|
|
|
VerifyOutgoing: true,
|
|
|
|
CAFile: "../test/ca/root.cer",
|
|
|
|
}, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-04-30 20:12:17 +00:00
|
|
|
wrapper = c.OutgoingRPCWrapper()
|
|
|
|
require.NotNil(t, wrapper)
|
|
|
|
cWrap, err = wrapper("", conn)
|
|
|
|
require.NotEqual(t, conn, cWrap)
|
2020-03-09 20:59:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigurator_OutgoingALPNRPCWrapper(t *testing.T) {
|
|
|
|
c := &Configurator{base: &Config{}, autoEncrypt: &autoEncrypt{}}
|
2020-04-30 20:12:17 +00:00
|
|
|
wrapper := c.OutgoingRPCWrapper()
|
|
|
|
require.NotNil(t, wrapper)
|
|
|
|
conn := &net.TCPConn{}
|
|
|
|
cWrap, err := wrapper("", conn)
|
|
|
|
require.Equal(t, conn, cWrap)
|
2020-03-09 20:59:02 +00:00
|
|
|
|
2020-04-30 20:12:17 +00:00
|
|
|
c, err = NewConfigurator(Config{
|
|
|
|
VerifyOutgoing: true,
|
2020-03-09 20:59:02 +00:00
|
|
|
CAFile: "../test/ca/root.cer",
|
|
|
|
}, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-04-30 20:12:17 +00:00
|
|
|
wrapper = c.OutgoingRPCWrapper()
|
|
|
|
require.NotNil(t, wrapper)
|
|
|
|
cWrap, err = wrapper("", conn)
|
|
|
|
require.NotEqual(t, conn, cWrap)
|
2019-03-13 09:29:06 +00:00
|
|
|
}
|
2019-02-27 09:14:59 +00:00
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
func TestConfigurator_UpdateChecks(t *testing.T) {
|
|
|
|
c, err := NewConfigurator(Config{}, nil)
|
2019-02-27 09:14:59 +00:00
|
|
|
require.NoError(t, err)
|
2019-03-13 09:29:06 +00:00
|
|
|
require.NoError(t, c.Update(Config{}))
|
|
|
|
require.Error(t, c.Update(Config{VerifyOutgoing: true}))
|
|
|
|
require.Error(t, c.Update(Config{VerifyIncoming: true,
|
|
|
|
CAFile: "../test/ca/root.cer"}))
|
|
|
|
require.False(t, c.base.VerifyIncoming)
|
|
|
|
require.False(t, c.base.VerifyOutgoing)
|
|
|
|
require.Equal(t, c.version, 2)
|
|
|
|
}
|
2019-02-27 09:14:59 +00:00
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
func TestConfigurator_UpdateSetsStuff(t *testing.T) {
|
|
|
|
c, err := NewConfigurator(Config{}, nil)
|
2019-02-27 09:14:59 +00:00
|
|
|
require.NoError(t, err)
|
2019-06-27 20:22:07 +00:00
|
|
|
require.Nil(t, c.caPool)
|
2019-11-12 18:40:32 +00:00
|
|
|
require.Nil(t, c.manual.cert)
|
2019-03-13 09:29:06 +00:00
|
|
|
require.Equal(t, c.base, &Config{})
|
|
|
|
require.Equal(t, 1, c.version)
|
2019-03-05 20:35:43 +00:00
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
require.Error(t, c.Update(Config{VerifyOutgoing: true}))
|
|
|
|
require.Equal(t, c.version, 1)
|
2019-03-05 20:35:43 +00:00
|
|
|
|
2019-03-13 09:29:06 +00:00
|
|
|
config := Config{
|
|
|
|
CAFile: "../test/ca/root.cer",
|
|
|
|
CertFile: "../test/key/ourdomain.cer",
|
|
|
|
KeyFile: "../test/key/ourdomain.key",
|
|
|
|
}
|
|
|
|
require.NoError(t, c.Update(config))
|
2019-06-27 20:22:07 +00:00
|
|
|
require.NotNil(t, c.caPool)
|
|
|
|
require.Len(t, c.caPool.Subjects(), 1)
|
|
|
|
require.NotNil(t, c.manual.cert)
|
2019-03-13 09:29:06 +00:00
|
|
|
require.Equal(t, c.base, &config)
|
|
|
|
require.Equal(t, 2, c.version)
|
|
|
|
}
|
2019-03-05 20:35:43 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
func TestConfigurator_VerifyOutgoing(t *testing.T) {
|
|
|
|
c := Configurator{base: &Config{}, autoEncrypt: &autoEncrypt{}}
|
|
|
|
type variant struct {
|
|
|
|
verify bool
|
|
|
|
autoEncryptTLS bool
|
|
|
|
pool *x509.CertPool
|
|
|
|
expected bool
|
|
|
|
}
|
|
|
|
variants := []variant{
|
|
|
|
{false, false, nil, false},
|
|
|
|
{true, false, nil, true},
|
|
|
|
{false, true, nil, false},
|
|
|
|
{true, true, nil, true},
|
|
|
|
|
|
|
|
{false, false, &x509.CertPool{}, false},
|
|
|
|
{true, false, &x509.CertPool{}, true},
|
|
|
|
{false, true, &x509.CertPool{}, true},
|
|
|
|
{true, true, &x509.CertPool{}, true},
|
|
|
|
}
|
|
|
|
for i, v := range variants {
|
|
|
|
info := fmt.Sprintf("case %d", i)
|
|
|
|
c.caPool = v.pool
|
|
|
|
c.base.VerifyOutgoing = v.verify
|
|
|
|
c.base.AutoEncryptTLS = v.autoEncryptTLS
|
|
|
|
require.Equal(t, v.expected, c.verifyOutgoing(), info)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigurator_Domain(t *testing.T) {
|
|
|
|
c := Configurator{base: &Config{Domain: "something"}}
|
|
|
|
require.Equal(t, "something", c.domain())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigurator_VerifyServerHostname(t *testing.T) {
|
|
|
|
c := Configurator{base: &Config{}, autoEncrypt: &autoEncrypt{}}
|
|
|
|
require.False(t, c.VerifyServerHostname())
|
|
|
|
|
|
|
|
c.base.VerifyServerHostname = true
|
|
|
|
c.autoEncrypt.verifyServerHostname = false
|
|
|
|
require.True(t, c.VerifyServerHostname())
|
|
|
|
|
|
|
|
c.base.VerifyServerHostname = false
|
|
|
|
c.autoEncrypt.verifyServerHostname = true
|
|
|
|
require.True(t, c.VerifyServerHostname())
|
|
|
|
|
|
|
|
c.base.VerifyServerHostname = true
|
|
|
|
c.autoEncrypt.verifyServerHostname = true
|
|
|
|
require.True(t, c.VerifyServerHostname())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigurator_AutoEncrytCertExpired(t *testing.T) {
|
|
|
|
c := Configurator{base: &Config{}, autoEncrypt: &autoEncrypt{}}
|
|
|
|
require.True(t, c.AutoEncryptCertExpired())
|
|
|
|
|
|
|
|
cert, err := loadKeyPair("../test/key/something_expired.cer", "../test/key/something_expired.key")
|
|
|
|
require.NoError(t, err)
|
|
|
|
c.autoEncrypt.cert = cert
|
|
|
|
require.True(t, c.AutoEncryptCertExpired())
|
|
|
|
|
|
|
|
cert, err = loadKeyPair("../test/key/ourdomain.cer", "../test/key/ourdomain.key")
|
|
|
|
require.NoError(t, err)
|
|
|
|
c.autoEncrypt.cert = cert
|
|
|
|
require.False(t, c.AutoEncryptCertExpired())
|
|
|
|
}
|
2020-02-19 22:22:31 +00:00
|
|
|
|
|
|
|
func TestConfig_tlsVersions(t *testing.T) {
|
|
|
|
require.Equal(t, []string{"tls10", "tls11", "tls12", "tls13"}, tlsVersions())
|
|
|
|
require.Equal(t, strings.Join(tlsVersions(), ", "), TLSVersions)
|
|
|
|
}
|