2018-03-29 15:25:11 +00:00
|
|
|
package connect
|
|
|
|
|
|
|
|
import (
|
2018-04-26 13:01:20 +00:00
|
|
|
"bytes"
|
2018-03-29 15:25:11 +00:00
|
|
|
"context"
|
2018-04-03 18:10:59 +00:00
|
|
|
"crypto/tls"
|
2018-04-26 13:01:20 +00:00
|
|
|
"crypto/x509"
|
2018-03-29 15:25:11 +00:00
|
|
|
"fmt"
|
2018-04-03 18:10:59 +00:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2018-04-26 13:01:20 +00:00
|
|
|
"strings"
|
2018-03-29 15:25:11 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2018-06-11 16:53:14 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2018-04-26 13:01:20 +00:00
|
|
|
"github.com/hashicorp/consul/agent"
|
2018-03-29 15:25:11 +00:00
|
|
|
"github.com/hashicorp/consul/agent/connect"
|
2018-04-26 13:01:20 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
2018-09-04 11:31:51 +00:00
|
|
|
"github.com/hashicorp/consul/testrpc"
|
2018-04-03 18:10:59 +00:00
|
|
|
"github.com/hashicorp/consul/testutil/retry"
|
2018-03-29 15:25:11 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2018-04-03 18:10:59 +00:00
|
|
|
// Assert io.Closer implementation
|
|
|
|
var _ io.Closer = new(Service)
|
|
|
|
|
2018-06-11 16:53:14 +00:00
|
|
|
func TestService_Name(t *testing.T) {
|
|
|
|
ca := connect.TestCA(t, nil)
|
|
|
|
s := TestService(t, "web", ca)
|
|
|
|
assert.Equal(t, "web", s.Name())
|
|
|
|
}
|
|
|
|
|
2018-03-29 15:25:11 +00:00
|
|
|
func TestService_Dial(t *testing.T) {
|
|
|
|
ca := connect.TestCA(t, nil)
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
accept bool
|
|
|
|
handshake bool
|
|
|
|
presentService string
|
|
|
|
wantErr string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "working",
|
|
|
|
accept: true,
|
|
|
|
handshake: true,
|
|
|
|
presentService: "db",
|
|
|
|
wantErr: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "tcp connect fail",
|
|
|
|
accept: false,
|
|
|
|
handshake: false,
|
|
|
|
presentService: "db",
|
|
|
|
wantErr: "connection refused",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "handshake timeout",
|
|
|
|
accept: true,
|
|
|
|
handshake: false,
|
|
|
|
presentService: "db",
|
|
|
|
wantErr: "i/o timeout",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "bad cert",
|
|
|
|
accept: true,
|
|
|
|
handshake: true,
|
|
|
|
presentService: "web",
|
|
|
|
wantErr: "peer certificate mismatch",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
|
2018-04-03 18:10:59 +00:00
|
|
|
s := TestService(t, "web", ca)
|
2018-03-29 15:25:11 +00:00
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(),
|
|
|
|
100*time.Millisecond)
|
|
|
|
defer cancel()
|
|
|
|
|
2018-04-03 18:10:59 +00:00
|
|
|
testSvr := NewTestServer(t, tt.presentService, ca)
|
|
|
|
testSvr.TimeoutHandshake = !tt.handshake
|
2018-03-29 15:25:11 +00:00
|
|
|
|
|
|
|
if tt.accept {
|
|
|
|
go func() {
|
2018-04-03 18:10:59 +00:00
|
|
|
err := testSvr.Serve()
|
2018-04-20 21:26:00 +00:00
|
|
|
require.NoError(err)
|
2018-03-29 15:25:11 +00:00
|
|
|
}()
|
2018-04-03 18:10:59 +00:00
|
|
|
defer testSvr.Close()
|
2018-04-20 21:26:00 +00:00
|
|
|
<-testSvr.Listening
|
2018-03-29 15:25:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Always expect to be connecting to a "DB"
|
|
|
|
resolver := &StaticResolver{
|
2018-04-03 18:10:59 +00:00
|
|
|
Addr: testSvr.Addr,
|
2018-03-29 15:25:11 +00:00
|
|
|
CertURI: connect.TestSpiffeIDService(t, "db"),
|
|
|
|
}
|
|
|
|
|
|
|
|
// All test runs should complete in under 500ms due to the timeout about.
|
|
|
|
// Don't wait for whole test run to get stuck.
|
|
|
|
testTimeout := 500 * time.Millisecond
|
|
|
|
testTimer := time.AfterFunc(testTimeout, func() {
|
|
|
|
panic(fmt.Sprintf("test timed out after %s", testTimeout))
|
|
|
|
})
|
|
|
|
|
|
|
|
conn, err := s.Dial(ctx, resolver)
|
|
|
|
testTimer.Stop()
|
|
|
|
|
|
|
|
if tt.wantErr == "" {
|
2018-04-20 21:26:00 +00:00
|
|
|
require.NoError(err)
|
2018-04-03 18:10:59 +00:00
|
|
|
require.IsType(&tls.Conn{}, conn)
|
2018-03-29 15:25:11 +00:00
|
|
|
} else {
|
2018-04-20 21:26:00 +00:00
|
|
|
require.Error(err)
|
2018-03-29 15:25:11 +00:00
|
|
|
require.Contains(err.Error(), tt.wantErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2018-04-03 18:10:59 +00:00
|
|
|
|
|
|
|
func TestService_ServerTLSConfig(t *testing.T) {
|
2018-04-26 13:01:20 +00:00
|
|
|
require := require.New(t)
|
|
|
|
|
2019-02-14 15:59:14 +00:00
|
|
|
a := agent.NewTestAgent(t, "007", "")
|
2018-04-26 13:01:20 +00:00
|
|
|
defer a.Shutdown()
|
2018-09-04 11:31:51 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2018-04-26 13:01:20 +00:00
|
|
|
client := a.Client()
|
|
|
|
agent := client.Agent()
|
|
|
|
|
|
|
|
// NewTestAgent setup a CA already by default
|
|
|
|
|
|
|
|
// Register a local agent service with a managed proxy
|
|
|
|
reg := &api.AgentServiceRegistration{
|
|
|
|
Name: "web",
|
|
|
|
Port: 8080,
|
|
|
|
}
|
|
|
|
err := agent.ServiceRegister(reg)
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
// Now we should be able to create a service that will eventually get it's TLS
|
|
|
|
// all by itself!
|
|
|
|
service, err := NewService("web", client)
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
// Wait for it to be ready
|
|
|
|
select {
|
|
|
|
case <-service.ReadyWait():
|
|
|
|
// continue with test case below
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
t.Fatalf("timeout waiting for Service.ReadyWait after 1s")
|
|
|
|
}
|
|
|
|
|
|
|
|
tlsCfg := service.ServerTLSConfig()
|
|
|
|
|
|
|
|
// Sanity check it has a leaf with the right ServiceID and that validates with
|
|
|
|
// the given roots.
|
|
|
|
require.NotNil(tlsCfg.GetCertificate)
|
|
|
|
leaf, err := tlsCfg.GetCertificate(&tls.ClientHelloInfo{})
|
|
|
|
require.NoError(err)
|
|
|
|
cert, err := x509.ParseCertificate(leaf.Certificate[0])
|
|
|
|
require.NoError(err)
|
|
|
|
require.Len(cert.URIs, 1)
|
|
|
|
require.True(strings.HasSuffix(cert.URIs[0].String(), "/svc/web"))
|
|
|
|
|
|
|
|
// Verify it as a client would
|
|
|
|
err = clientSideVerifier(tlsCfg, leaf.Certificate)
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
// Now test that rotating the root updates
|
|
|
|
{
|
|
|
|
// Setup a new generated CA
|
|
|
|
connect.TestCAConfigSet(t, a, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// After some time, both root and leaves should be different but both should
|
|
|
|
// still be correct.
|
|
|
|
oldRootSubjects := bytes.Join(tlsCfg.RootCAs.Subjects(), []byte(", "))
|
2018-06-07 09:17:44 +00:00
|
|
|
oldLeafSerial := connect.HexString(cert.SerialNumber.Bytes())
|
2018-04-26 13:01:20 +00:00
|
|
|
oldLeafKeyID := connect.HexString(cert.SubjectKeyId)
|
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
updatedCfg := service.ServerTLSConfig()
|
|
|
|
|
|
|
|
// Wait until roots are different
|
|
|
|
rootSubjects := bytes.Join(updatedCfg.RootCAs.Subjects(), []byte(", "))
|
|
|
|
if bytes.Equal(oldRootSubjects, rootSubjects) {
|
|
|
|
r.Fatalf("root certificates should have changed, got %s",
|
|
|
|
rootSubjects)
|
|
|
|
}
|
|
|
|
|
|
|
|
leaf, err := updatedCfg.GetCertificate(&tls.ClientHelloInfo{})
|
|
|
|
r.Check(err)
|
|
|
|
cert, err := x509.ParseCertificate(leaf.Certificate[0])
|
|
|
|
r.Check(err)
|
|
|
|
|
2018-06-07 09:17:44 +00:00
|
|
|
if oldLeafSerial == connect.HexString(cert.SerialNumber.Bytes()) {
|
|
|
|
r.Fatalf("leaf certificate should have changed, got serial %s",
|
|
|
|
oldLeafSerial)
|
|
|
|
}
|
2018-04-26 13:01:20 +00:00
|
|
|
if oldLeafKeyID == connect.HexString(cert.SubjectKeyId) {
|
|
|
|
r.Fatalf("leaf should have a different key, got matching SubjectKeyID = %s",
|
|
|
|
oldLeafKeyID)
|
|
|
|
}
|
|
|
|
})
|
2018-04-03 18:10:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestService_HTTPClient(t *testing.T) {
|
|
|
|
ca := connect.TestCA(t, nil)
|
|
|
|
|
|
|
|
s := TestService(t, "web", ca)
|
|
|
|
|
|
|
|
// Run a test HTTP server
|
|
|
|
testSvr := NewTestServer(t, "backend", ca)
|
|
|
|
defer testSvr.Close()
|
|
|
|
go func() {
|
|
|
|
err := testSvr.ServeHTTPS(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Write([]byte("Hello, I am Backend"))
|
|
|
|
}))
|
2018-04-20 21:26:00 +00:00
|
|
|
require.NoError(t, err)
|
2018-04-03 18:10:59 +00:00
|
|
|
}()
|
2018-04-20 21:26:00 +00:00
|
|
|
<-testSvr.Listening
|
2018-04-03 18:10:59 +00:00
|
|
|
|
|
|
|
// Still get connection refused some times so retry on those
|
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
// Hook the service resolver to avoid needing full agent setup.
|
|
|
|
s.httpResolverFromAddr = func(addr string) (Resolver, error) {
|
|
|
|
// Require in this goroutine seems to block causing a timeout on the Get.
|
|
|
|
//require.Equal("https://backend.service.consul:443", addr)
|
|
|
|
return &StaticResolver{
|
|
|
|
Addr: testSvr.Addr,
|
|
|
|
CertURI: connect.TestSpiffeIDService(t, "backend"),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
client := s.HTTPClient()
|
|
|
|
client.Timeout = 1 * time.Second
|
|
|
|
|
|
|
|
resp, err := client.Get("https://backend.service.consul/foo")
|
|
|
|
r.Check(err)
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
bodyBytes, err := ioutil.ReadAll(resp.Body)
|
|
|
|
r.Check(err)
|
|
|
|
|
|
|
|
got := string(bodyBytes)
|
|
|
|
want := "Hello, I am Backend"
|
|
|
|
if got != want {
|
|
|
|
r.Fatalf("got %s, want %s", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2018-07-13 21:38:35 +00:00
|
|
|
|
|
|
|
func TestService_HasDefaultHTTPResolverFromAddr(t *testing.T) {
|
|
|
|
|
|
|
|
client, err := api.NewClient(api.DefaultConfig())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
s, err := NewService("foo", client)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Sanity check this is actually set in constructor since we always override
|
|
|
|
// it in tests. Full tests of the resolver func are in resolver_test.go
|
|
|
|
require.NotNil(t, s.httpResolverFromAddr)
|
|
|
|
|
|
|
|
fn := s.httpResolverFromAddr
|
|
|
|
|
|
|
|
expected := &ConsulResolver{
|
|
|
|
Client: client,
|
|
|
|
Namespace: "default",
|
|
|
|
Name: "foo",
|
|
|
|
Type: ConsulResolverTypeService,
|
|
|
|
}
|
|
|
|
got, err := fn("foo.service.consul")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, expected, got)
|
|
|
|
}
|