2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 18:39:22 +00:00
|
|
|
|
2022-07-13 15:33:48 +00:00
|
|
|
package internal
|
2020-09-14 17:16:13 +00:00
|
|
|
|
|
|
|
import (
|
2020-10-14 20:47:16 +00:00
|
|
|
"crypto/tls"
|
2020-09-11 18:15:02 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
2020-10-14 20:47:16 +00:00
|
|
|
"sync/atomic"
|
2020-09-11 18:15:02 +00:00
|
|
|
"testing"
|
2020-09-14 17:16:13 +00:00
|
|
|
"time"
|
|
|
|
|
2022-03-22 12:40:24 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
2020-09-11 18:15:02 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"golang.org/x/sync/errgroup"
|
2020-09-08 21:31:47 +00:00
|
|
|
"google.golang.org/grpc"
|
2020-10-14 17:21:51 +00:00
|
|
|
|
2022-12-13 20:09:55 +00:00
|
|
|
"github.com/hashicorp/consul/agent/consul/rate"
|
2022-10-11 22:00:32 +00:00
|
|
|
"github.com/hashicorp/consul/agent/grpc-middleware/testutil/testservice"
|
2020-10-14 17:21:51 +00:00
|
|
|
"github.com/hashicorp/consul/agent/metadata"
|
|
|
|
"github.com/hashicorp/consul/agent/pool"
|
2020-10-14 20:47:16 +00:00
|
|
|
"github.com/hashicorp/consul/tlsutil"
|
2020-09-14 17:16:13 +00:00
|
|
|
)
|
|
|
|
|
2020-09-11 18:15:02 +00:00
|
|
|
type testServer struct {
|
|
|
|
addr net.Addr
|
|
|
|
name string
|
|
|
|
dc string
|
|
|
|
shutdown func()
|
2020-10-14 20:47:16 +00:00
|
|
|
rpc *fakeRPCListener
|
2020-09-11 18:15:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s testServer) Metadata() *metadata.Server {
|
2020-10-14 20:47:16 +00:00
|
|
|
return &metadata.Server{
|
|
|
|
ID: s.name,
|
2021-08-24 21:28:44 +00:00
|
|
|
Name: s.name + "." + s.dc,
|
|
|
|
ShortName: s.name,
|
2020-10-14 20:47:16 +00:00
|
|
|
Datacenter: s.dc,
|
|
|
|
Addr: s.addr,
|
|
|
|
UseTLS: s.rpc.tlsConf != nil,
|
|
|
|
}
|
2020-09-11 18:15:02 +00:00
|
|
|
}
|
|
|
|
|
2021-10-16 15:59:32 +00:00
|
|
|
func newSimpleTestServer(t *testing.T, name, dc string, tlsConf *tlsutil.Configurator) testServer {
|
2024-01-12 16:54:07 +00:00
|
|
|
return newTestServer(t, hclog.Default(), name, dc, tlsConf, func(server grpc.ServiceRegistrar) {
|
2022-10-11 22:00:32 +00:00
|
|
|
testservice.RegisterSimpleServer(server, &testservice.Simple{Name: name, DC: dc})
|
2020-09-08 21:31:47 +00:00
|
|
|
})
|
2021-08-22 18:06:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// newPanicTestServer sets up a simple server with handlers that panic.
|
2021-10-16 15:59:32 +00:00
|
|
|
func newPanicTestServer(t *testing.T, logger hclog.Logger, name, dc string, tlsConf *tlsutil.Configurator) testServer {
|
2024-01-12 16:54:07 +00:00
|
|
|
return newTestServer(t, logger, name, dc, tlsConf, func(server grpc.ServiceRegistrar) {
|
2022-10-11 22:00:32 +00:00
|
|
|
testservice.RegisterSimpleServer(server, &testservice.SimplePanic{Name: name, DC: dc})
|
2021-08-22 18:06:26 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-01-12 16:54:07 +00:00
|
|
|
func newTestServer(t *testing.T, logger hclog.Logger, name, dc string, tlsConf *tlsutil.Configurator, register func(server grpc.ServiceRegistrar)) testServer {
|
2021-08-22 18:06:26 +00:00
|
|
|
addr := &net.IPAddr{IP: net.ParseIP("127.0.0.1")}
|
2024-01-12 16:54:07 +00:00
|
|
|
handler := NewHandler(logger, addr, nil, rate.NullRequestLimitsHandler())
|
|
|
|
register(handler)
|
2020-09-11 18:15:02 +00:00
|
|
|
|
2021-11-29 17:19:43 +00:00
|
|
|
lis, err := net.Listen("tcp", "127.0.0.1:0")
|
2020-09-11 18:15:02 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-08-24 21:28:44 +00:00
|
|
|
rpc := &fakeRPCListener{t: t, handler: handler, tlsConf: tlsConf}
|
2020-09-11 18:15:02 +00:00
|
|
|
|
|
|
|
g := errgroup.Group{}
|
|
|
|
g.Go(func() error {
|
2020-10-14 17:21:51 +00:00
|
|
|
if err := rpc.listen(lis); err != nil {
|
|
|
|
return fmt.Errorf("fake rpc listen error: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
2020-09-11 18:15:02 +00:00
|
|
|
})
|
|
|
|
g.Go(func() error {
|
2020-10-14 17:21:51 +00:00
|
|
|
if err := handler.Run(); err != nil {
|
|
|
|
return fmt.Errorf("grpc server error: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
2020-09-11 18:15:02 +00:00
|
|
|
})
|
|
|
|
return testServer{
|
|
|
|
addr: lis.Addr(),
|
|
|
|
name: name,
|
|
|
|
dc: dc,
|
2020-10-14 20:47:16 +00:00
|
|
|
rpc: rpc,
|
2020-09-11 18:15:02 +00:00
|
|
|
shutdown: func() {
|
2020-10-14 17:21:51 +00:00
|
|
|
rpc.shutdown = true
|
2020-09-11 18:15:02 +00:00
|
|
|
if err := lis.Close(); err != nil {
|
|
|
|
t.Logf("listener closed with error: %v", err)
|
|
|
|
}
|
|
|
|
if err := handler.Shutdown(); err != nil {
|
|
|
|
t.Logf("grpc server shutdown: %v", err)
|
|
|
|
}
|
|
|
|
if err := g.Wait(); err != nil {
|
2020-10-14 17:21:51 +00:00
|
|
|
t.Log(err)
|
2020-09-11 18:15:02 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// fakeRPCListener mimics agent/consul.Server.listen to handle the RPCType byte.
|
|
|
|
// In the future we should be able to refactor Server and extract this RPC
|
|
|
|
// handling logic so that we don't need to use a fake.
|
|
|
|
// For now, since this logic is in agent/consul, we can't easily use Server.listen
|
|
|
|
// so we fake it.
|
|
|
|
type fakeRPCListener struct {
|
2021-08-24 21:28:44 +00:00
|
|
|
t *testing.T
|
|
|
|
handler *Handler
|
|
|
|
shutdown bool
|
|
|
|
tlsConf *tlsutil.Configurator
|
|
|
|
tlsConnEstablished int32
|
|
|
|
alpnConnEstablished int32
|
2020-09-11 18:15:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fakeRPCListener) listen(listener net.Listener) error {
|
|
|
|
for {
|
|
|
|
conn, err := listener.Accept()
|
|
|
|
if err != nil {
|
2020-10-14 17:21:51 +00:00
|
|
|
if f.shutdown {
|
|
|
|
return nil
|
|
|
|
}
|
2020-09-11 18:15:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
go f.handleConn(conn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fakeRPCListener) handleConn(conn net.Conn) {
|
2021-08-24 21:28:44 +00:00
|
|
|
if f.tlsConf != nil && f.tlsConf.MutualTLSCapable() {
|
|
|
|
// See if actually this is native TLS multiplexed onto the old
|
|
|
|
// "type-byte" system.
|
|
|
|
|
|
|
|
peekedConn, nativeTLS, err := pool.PeekForTLS(conn)
|
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
|
|
|
fmt.Printf("ERROR: failed to read first byte: %v\n", err)
|
|
|
|
}
|
|
|
|
conn.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if nativeTLS {
|
|
|
|
f.handleNativeTLSConn(peekedConn)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
conn = peekedConn
|
|
|
|
}
|
|
|
|
|
2020-09-11 18:15:02 +00:00
|
|
|
buf := make([]byte, 1)
|
|
|
|
|
|
|
|
if _, err := conn.Read(buf); err != nil {
|
|
|
|
if err != io.EOF {
|
|
|
|
fmt.Println("ERROR", err.Error())
|
|
|
|
}
|
|
|
|
conn.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
typ := pool.RPCType(buf[0])
|
|
|
|
|
2020-10-14 20:47:16 +00:00
|
|
|
switch typ {
|
|
|
|
|
|
|
|
case pool.RPCGRPC:
|
2020-09-11 18:15:02 +00:00
|
|
|
f.handler.Handle(conn)
|
|
|
|
return
|
2020-10-14 20:47:16 +00:00
|
|
|
|
|
|
|
case pool.RPCTLS:
|
|
|
|
// occasionally we see a test client connecting to an rpc listener that
|
|
|
|
// was created as part of another test, despite none of the tests running
|
|
|
|
// in parallel.
|
|
|
|
// Maybe some strange grpc behaviour? I'm not sure.
|
|
|
|
if f.tlsConf == nil {
|
|
|
|
fmt.Println("ERROR: tls is not configured")
|
|
|
|
conn.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
atomic.AddInt32(&f.tlsConnEstablished, 1)
|
|
|
|
conn = tls.Server(conn, f.tlsConf.IncomingRPCConfig())
|
|
|
|
f.handleConn(conn)
|
|
|
|
|
|
|
|
default:
|
|
|
|
fmt.Println("ERROR: unexpected byte", typ)
|
|
|
|
conn.Close()
|
2020-09-11 18:15:02 +00:00
|
|
|
}
|
2020-10-14 20:47:16 +00:00
|
|
|
}
|
2021-08-24 21:28:44 +00:00
|
|
|
|
|
|
|
func (f *fakeRPCListener) handleNativeTLSConn(conn net.Conn) {
|
|
|
|
tlscfg := f.tlsConf.IncomingALPNRPCConfig(pool.RPCNextProtos)
|
|
|
|
tlsConn := tls.Server(conn, tlscfg)
|
|
|
|
|
|
|
|
// Force the handshake to conclude.
|
|
|
|
if err := tlsConn.Handshake(); err != nil {
|
|
|
|
fmt.Printf("ERROR: TLS handshake failed: %v", err)
|
|
|
|
conn.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
conn.SetReadDeadline(time.Time{})
|
|
|
|
|
|
|
|
var (
|
|
|
|
cs = tlsConn.ConnectionState()
|
|
|
|
nextProto = cs.NegotiatedProtocol
|
|
|
|
)
|
|
|
|
|
|
|
|
switch nextProto {
|
|
|
|
case pool.ALPN_RPCGRPC:
|
|
|
|
atomic.AddInt32(&f.alpnConnEstablished, 1)
|
|
|
|
f.handler.Handle(tlsConn)
|
|
|
|
|
|
|
|
default:
|
|
|
|
fmt.Printf("ERROR: discarding RPC for unknown negotiated protocol %q\n", nextProto)
|
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
}
|