2023-03-28 22:48:58 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2018-05-19 17:47:30 +00:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-03-26 11:34:47 +00:00
|
|
|
"crypto/tls"
|
2018-05-19 17:47:30 +00:00
|
|
|
"net"
|
2021-06-24 20:02:34 +00:00
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"strconv"
|
2018-05-19 17:47:30 +00:00
|
|
|
"testing"
|
2021-06-24 20:02:34 +00:00
|
|
|
"time"
|
2018-05-19 17:47:30 +00:00
|
|
|
|
2020-11-11 18:15:33 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-08-27 15:45:07 +00:00
|
|
|
|
2018-05-19 17:47:30 +00:00
|
|
|
"github.com/hashicorp/consul/agent"
|
|
|
|
agConnect "github.com/hashicorp/consul/agent/connect"
|
|
|
|
"github.com/hashicorp/consul/api"
|
|
|
|
"github.com/hashicorp/consul/connect"
|
2019-03-27 12:54:56 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/freeport"
|
2020-01-28 23:50:41 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
2019-03-27 12:54:56 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil/retry"
|
2020-11-11 18:15:33 +00:00
|
|
|
"github.com/hashicorp/consul/testrpc"
|
2018-05-19 17:47:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestProxy_public(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2021-11-27 20:27:59 +00:00
|
|
|
ports := freeport.GetN(t, 2)
|
2018-05-19 17:47:30 +00:00
|
|
|
|
2020-03-31 19:59:56 +00:00
|
|
|
a := agent.NewTestAgent(t, "")
|
2018-05-19 17:47:30 +00:00
|
|
|
defer a.Shutdown()
|
2018-09-04 11:31:51 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2018-05-19 17:47:30 +00:00
|
|
|
client := a.Client()
|
|
|
|
|
|
|
|
// Register the service so we can get a leaf cert
|
|
|
|
_, err := client.Catalog().Register(&api.CatalogRegistration{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "local",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &api.AgentService{
|
|
|
|
Service: "echo",
|
|
|
|
},
|
|
|
|
}, nil)
|
2021-06-24 20:02:34 +00:00
|
|
|
require.NoError(t, err)
|
2018-05-19 17:47:30 +00:00
|
|
|
|
|
|
|
// Start the backend service that is being proxied
|
|
|
|
testApp := NewTestTCPServer(t)
|
|
|
|
defer testApp.Close()
|
|
|
|
|
2021-06-24 20:02:34 +00:00
|
|
|
upstreams := []UpstreamConfig{
|
|
|
|
{
|
|
|
|
DestinationName: "just-a-port",
|
|
|
|
LocalBindPort: ports[1],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var unixSocket string
|
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
tempDir := testutil.TempDir(t, "consul")
|
|
|
|
unixSocket = filepath.Join(tempDir, "test.sock")
|
|
|
|
|
|
|
|
upstreams = append(upstreams, UpstreamConfig{
|
|
|
|
DestinationName: "just-a-unix-domain-socket",
|
|
|
|
LocalBindSocketPath: unixSocket,
|
|
|
|
LocalBindSocketMode: "0600",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-05-19 17:47:30 +00:00
|
|
|
// Start the proxy
|
|
|
|
p, err := New(client, NewStaticConfigWatcher(&Config{
|
|
|
|
ProxiedServiceName: "echo",
|
|
|
|
PublicListener: PublicListenerConfig{
|
|
|
|
BindAddress: "127.0.0.1",
|
|
|
|
BindPort: ports[0],
|
|
|
|
LocalServiceAddress: testApp.Addr().String(),
|
|
|
|
},
|
2021-06-24 20:02:34 +00:00
|
|
|
Upstreams: upstreams,
|
2020-01-28 23:50:41 +00:00
|
|
|
}), testutil.Logger(t))
|
2021-06-24 20:02:34 +00:00
|
|
|
require.NoError(t, err)
|
2018-05-19 17:47:30 +00:00
|
|
|
defer p.Close()
|
|
|
|
go p.Serve()
|
|
|
|
|
2021-03-26 11:34:47 +00:00
|
|
|
// We create this client with an explicit ServerNextProtos here which will use `h2`
|
|
|
|
// if the proxy supports it. This is so we can verify below that the proxy _doesn't_
|
|
|
|
// advertise `h2` support as it's only a L4 proxy.
|
|
|
|
svc, err := connect.NewServiceWithConfig("echo", connect.Config{Client: client, ServerNextProtos: []string{"h2"}})
|
2021-06-24 20:02:34 +00:00
|
|
|
require.NoError(t, err)
|
2021-03-26 11:34:47 +00:00
|
|
|
|
2018-05-19 17:47:30 +00:00
|
|
|
// Create a test connection to the proxy. We retry here a few times
|
|
|
|
// since this is dependent on the agent actually starting up and setting
|
|
|
|
// up the CA.
|
|
|
|
var conn net.Conn
|
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
conn, err = svc.Dial(context.Background(), &connect.StaticResolver{
|
|
|
|
Addr: TestLocalAddr(ports[0]),
|
|
|
|
CertURI: agConnect.TestSpiffeIDService(t, "echo"),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
r.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-03-26 11:34:47 +00:00
|
|
|
// Verify that we did not select h2 via ALPN since the proxy is layer 4 only
|
|
|
|
tlsConn := conn.(*tls.Conn)
|
2021-06-24 20:02:34 +00:00
|
|
|
require.Equal(t, "", tlsConn.ConnectionState().NegotiatedProtocol)
|
2021-03-26 11:34:47 +00:00
|
|
|
|
2018-05-19 17:47:30 +00:00
|
|
|
// Connection works, test it is the right one
|
|
|
|
TestEchoConn(t, conn, "")
|
2021-06-24 20:02:34 +00:00
|
|
|
|
|
|
|
t.Run("verify port upstream is configured", func(t *testing.T) {
|
|
|
|
// Verify that it is listening by doing a simple TCP dial.
|
|
|
|
addr := net.JoinHostPort("127.0.0.1", strconv.Itoa(ports[1]))
|
|
|
|
conn, err := net.DialTimeout("tcp", addr, 100*time.Millisecond)
|
|
|
|
require.NoError(t, err)
|
|
|
|
_ = conn.Close()
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("verify unix domain socket upstream will never work", func(t *testing.T) {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the socket was not created
|
|
|
|
require.NoFileExists(t, unixSocket)
|
|
|
|
})
|
2018-05-19 17:47:30 +00:00
|
|
|
}
|