2023-03-28 22:48:58 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 22:48:58 +00:00
|
|
|
|
2018-03-21 22:35:00 +00:00
|
|
|
package connect
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"fmt"
|
2018-03-29 15:25:11 +00:00
|
|
|
"io"
|
2018-04-03 18:10:59 +00:00
|
|
|
"log"
|
2018-03-29 15:25:11 +00:00
|
|
|
"net"
|
2018-04-03 18:10:59 +00:00
|
|
|
"net/http"
|
2018-03-29 15:25:11 +00:00
|
|
|
"sync/atomic"
|
2018-03-21 22:35:00 +00:00
|
|
|
|
2020-11-11 18:15:33 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
|
|
|
testing "github.com/mitchellh/go-testing-interface"
|
|
|
|
|
2018-03-29 15:25:11 +00:00
|
|
|
"github.com/hashicorp/consul/agent/connect"
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2019-03-27 12:54:56 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/freeport"
|
2018-03-21 22:35:00 +00:00
|
|
|
)
|
|
|
|
|
2018-04-03 18:10:59 +00:00
|
|
|
// TestService returns a Service instance based on a static TLS Config.
|
|
|
|
func TestService(t testing.T, service string, ca *structs.CARoot) *Service {
|
|
|
|
t.Helper()
|
|
|
|
|
2018-04-26 13:01:20 +00:00
|
|
|
// Don't need to talk to client since we are setting TLSConfig locally
|
2020-01-28 23:50:41 +00:00
|
|
|
logger := hclog.New(&hclog.LoggerOptions{})
|
2018-04-26 13:01:20 +00:00
|
|
|
svc, err := NewDevServiceWithTLSConfig(service,
|
2020-01-28 23:50:41 +00:00
|
|
|
logger, TestTLSConfig(t, service, ca))
|
2018-04-03 18:10:59 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return svc
|
2018-03-29 15:25:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestTLSConfig returns a *tls.Config suitable for use during tests.
|
|
|
|
func TestTLSConfig(t testing.T, service string, ca *structs.CARoot) *tls.Config {
|
|
|
|
t.Helper()
|
|
|
|
|
2018-04-05 19:30:19 +00:00
|
|
|
cfg := defaultTLSConfig()
|
2018-03-29 15:25:11 +00:00
|
|
|
cfg.Certificates = []tls.Certificate{TestSvcKeyPair(t, service, ca)}
|
|
|
|
cfg.RootCAs = TestCAPool(t, ca)
|
|
|
|
cfg.ClientCAs = TestCAPool(t, ca)
|
|
|
|
return cfg
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestCAPool returns an *x509.CertPool containing the passed CA's root(s)
|
|
|
|
func TestCAPool(t testing.T, cas ...*structs.CARoot) *x509.CertPool {
|
|
|
|
t.Helper()
|
|
|
|
pool := x509.NewCertPool()
|
|
|
|
for _, ca := range cas {
|
|
|
|
pool.AppendCertsFromPEM([]byte(ca.RootCert))
|
|
|
|
}
|
|
|
|
return pool
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestSvcKeyPair returns an tls.Certificate containing both cert and private
|
|
|
|
// key for a given service under a given CA from the testdata dir.
|
|
|
|
func TestSvcKeyPair(t testing.T, service string, ca *structs.CARoot) tls.Certificate {
|
|
|
|
t.Helper()
|
|
|
|
certPEM, keyPEM := connect.TestLeaf(t, service, ca)
|
|
|
|
cert, err := tls.X509KeyPair([]byte(certPEM), []byte(keyPEM))
|
2018-04-03 18:10:59 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-03-29 15:25:11 +00:00
|
|
|
return cert
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestPeerCertificates returns a []*x509.Certificate as you'd get from
|
|
|
|
// tls.Conn.ConnectionState().PeerCertificates including the named certificate.
|
|
|
|
func TestPeerCertificates(t testing.T, service string, ca *structs.CARoot) []*x509.Certificate {
|
|
|
|
t.Helper()
|
|
|
|
certPEM, _ := connect.TestLeaf(t, service, ca)
|
|
|
|
cert, err := connect.ParseCert(certPEM)
|
2018-04-03 18:10:59 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-03-29 15:25:11 +00:00
|
|
|
return []*x509.Certificate{cert}
|
|
|
|
}
|
|
|
|
|
2018-04-03 18:10:59 +00:00
|
|
|
// TestServer runs a service listener that can be used to test clients. It's
|
2019-03-06 17:13:28 +00:00
|
|
|
// behavior can be controlled by the struct members.
|
2018-04-03 18:10:59 +00:00
|
|
|
type TestServer struct {
|
2018-03-29 15:25:11 +00:00
|
|
|
// The service name to serve.
|
|
|
|
Service string
|
|
|
|
// The (test) CA to use for generating certs.
|
|
|
|
CA *structs.CARoot
|
|
|
|
// TimeoutHandshake controls whether the listening server will complete a TLS
|
|
|
|
// handshake quickly enough.
|
|
|
|
TimeoutHandshake bool
|
|
|
|
// TLSCfg is the tls.Config that will be used. By default it's set up from the
|
|
|
|
// service and ca set.
|
|
|
|
TLSCfg *tls.Config
|
|
|
|
// Addr is the listen address. It is set to a random free port on `localhost`
|
|
|
|
// by default.
|
|
|
|
Addr string
|
2018-04-20 21:26:00 +00:00
|
|
|
// Listening is closed when the listener is run.
|
|
|
|
Listening chan struct{}
|
2018-03-29 15:25:11 +00:00
|
|
|
|
2021-11-27 20:27:59 +00:00
|
|
|
l net.Listener
|
|
|
|
stopFlag int32
|
|
|
|
stopChan chan struct{}
|
2018-03-29 15:25:11 +00:00
|
|
|
}
|
|
|
|
|
2018-04-03 18:10:59 +00:00
|
|
|
// NewTestServer returns a TestServer. It should be closed when test is
|
2018-03-29 15:25:11 +00:00
|
|
|
// complete.
|
2018-04-03 18:10:59 +00:00
|
|
|
func NewTestServer(t testing.T, service string, ca *structs.CARoot) *TestServer {
|
|
|
|
return &TestServer{
|
2021-11-27 20:27:59 +00:00
|
|
|
Service: service,
|
|
|
|
CA: ca,
|
|
|
|
stopChan: make(chan struct{}),
|
|
|
|
TLSCfg: TestTLSConfig(t, service, ca),
|
2021-11-30 22:32:41 +00:00
|
|
|
Addr: fmt.Sprintf("127.0.0.1:%d", freeport.GetOne(t)),
|
2021-11-27 20:27:59 +00:00
|
|
|
Listening: make(chan struct{}),
|
2018-03-29 15:25:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-03 18:10:59 +00:00
|
|
|
// Serve runs a tcp echo server and blocks until it is closed or errors. If
|
|
|
|
// TimeoutHandshake is set it won't start TLS handshake on new connections.
|
|
|
|
func (s *TestServer) Serve() error {
|
2018-03-29 15:25:11 +00:00
|
|
|
// Just accept TCP conn but so we can control timing of accept/handshake
|
|
|
|
l, err := net.Listen("tcp", s.Addr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.l = l
|
2020-11-11 18:15:33 +00:00
|
|
|
close(s.Listening)
|
2018-04-03 18:10:59 +00:00
|
|
|
log.Printf("test connect service listening on %s", s.Addr)
|
2018-03-29 15:25:11 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
conn, err := s.l.Accept()
|
|
|
|
if err != nil {
|
|
|
|
if atomic.LoadInt32(&s.stopFlag) == 1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-04-03 18:10:59 +00:00
|
|
|
// Ignore the conn if we are not actively handshaking
|
2018-03-29 15:25:11 +00:00
|
|
|
if !s.TimeoutHandshake {
|
|
|
|
// Upgrade conn to TLS
|
|
|
|
conn = tls.Server(conn, s.TLSCfg)
|
|
|
|
|
|
|
|
// Run an echo service
|
2018-04-03 18:10:59 +00:00
|
|
|
log.Printf("test connect service accepted conn from %s, "+
|
|
|
|
" running echo service", conn.RemoteAddr())
|
2018-03-29 15:25:11 +00:00
|
|
|
go io.Copy(conn, conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close this conn when we stop
|
|
|
|
go func(c net.Conn) {
|
|
|
|
<-s.stopChan
|
|
|
|
c.Close()
|
|
|
|
}(conn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-03 18:10:59 +00:00
|
|
|
// ServeHTTPS runs an HTTPS server with the given config. It invokes the passed
|
|
|
|
// Handler for all requests.
|
|
|
|
func (s *TestServer) ServeHTTPS(h http.Handler) error {
|
|
|
|
srv := http.Server{
|
|
|
|
Addr: s.Addr,
|
|
|
|
TLSConfig: s.TLSCfg,
|
|
|
|
Handler: h,
|
|
|
|
}
|
|
|
|
log.Printf("starting test connect HTTPS server on %s", s.Addr)
|
2018-04-20 21:26:00 +00:00
|
|
|
|
|
|
|
// Use our own listener so we can signal when it's ready.
|
|
|
|
l, err := net.Listen("tcp", s.Addr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
close(s.Listening)
|
|
|
|
s.l = l
|
|
|
|
log.Printf("test connect service listening on %s", s.Addr)
|
|
|
|
|
|
|
|
err = srv.ServeTLS(l, "", "")
|
|
|
|
if atomic.LoadInt32(&s.stopFlag) == 1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
2018-04-03 18:10:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close stops a TestServer
|
|
|
|
func (s *TestServer) Close() error {
|
2018-03-29 15:25:11 +00:00
|
|
|
old := atomic.SwapInt32(&s.stopFlag, 1)
|
|
|
|
if old == 0 {
|
|
|
|
if s.l != nil {
|
|
|
|
s.l.Close()
|
|
|
|
}
|
|
|
|
close(s.stopChan)
|
2018-03-21 22:35:00 +00:00
|
|
|
}
|
2018-04-03 18:10:59 +00:00
|
|
|
return nil
|
2018-03-21 22:35:00 +00:00
|
|
|
}
|