2023-03-28 22:48:58 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
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"
|
|
|
|
"net/http"
|
2022-04-14 20:55:10 +00:00
|
|
|
"reflect"
|
|
|
|
"sort"
|
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"
|
2020-11-11 18:15:33 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-06-11 16:53:14 +00:00
|
|
|
|
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"
|
2019-03-27 12:54:56 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil/retry"
|
2019-04-25 16:26:33 +00:00
|
|
|
"github.com/hashicorp/consul/testrpc"
|
2018-03-29 15:25:11 +00:00
|
|
|
)
|
|
|
|
|
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) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2018-03-29 15:25:11 +00:00
|
|
|
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) {
|
|
|
|
|
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()
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.NoError(t, err)
|
2018-03-29 15:25:11 +00:00
|
|
|
}()
|
2018-04-20 21:26:00 +00:00
|
|
|
<-testSvr.Listening
|
2020-11-11 18:15:33 +00:00
|
|
|
defer testSvr.Close()
|
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 == "" {
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.IsType(t, &tls.Conn{}, conn)
|
2018-03-29 15:25:11 +00:00
|
|
|
} else {
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), tt.wantErr)
|
2018-03-29 15:25:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2018-04-03 18:10:59 +00:00
|
|
|
|
|
|
|
func TestService_ServerTLSConfig(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2020-06-10 20:47:35 +00:00
|
|
|
a := agent.StartTestAgent(t, agent.TestAgent{Name: "007", Overrides: `
|
|
|
|
connect {
|
|
|
|
test_ca_leaf_root_change_spread = "1ns"
|
|
|
|
}
|
|
|
|
`})
|
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
|
|
|
|
|
2019-08-09 19:19:30 +00:00
|
|
|
// Register a local agent service
|
2018-04-26 13:01:20 +00:00
|
|
|
reg := &api.AgentServiceRegistration{
|
|
|
|
Name: "web",
|
|
|
|
Port: 8080,
|
|
|
|
}
|
|
|
|
err := agent.ServiceRegister(reg)
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.NoError(t, err)
|
2018-04-26 13:01:20 +00:00
|
|
|
|
|
|
|
// Now we should be able to create a service that will eventually get it's TLS
|
|
|
|
// all by itself!
|
|
|
|
service, err := NewService("web", client)
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.NoError(t, err)
|
2018-04-26 13:01:20 +00:00
|
|
|
|
|
|
|
// 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.
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.NotNil(t, tlsCfg.GetCertificate)
|
2018-04-26 13:01:20 +00:00
|
|
|
leaf, err := tlsCfg.GetCertificate(&tls.ClientHelloInfo{})
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.NoError(t, err)
|
2018-04-26 13:01:20 +00:00
|
|
|
cert, err := x509.ParseCertificate(leaf.Certificate[0])
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, cert.URIs, 1)
|
|
|
|
require.True(t, strings.HasSuffix(cert.URIs[0].String(), "/svc/web"))
|
2018-04-26 13:01:20 +00:00
|
|
|
|
|
|
|
// Verify it as a client would
|
|
|
|
err = clientSideVerifier(tlsCfg, leaf.Certificate)
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.NoError(t, err)
|
2018-04-26 13:01:20 +00:00
|
|
|
|
|
|
|
// 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.
|
2022-04-14 20:55:10 +00:00
|
|
|
oldRootSubjects := getSubjects(tlsCfg.RootCAs)
|
2019-09-23 17:52:35 +00:00
|
|
|
oldLeafSerial := cert.SerialNumber
|
|
|
|
oldLeafKeyID := cert.SubjectKeyId
|
2018-04-26 13:01:20 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
updatedCfg := service.ServerTLSConfig()
|
|
|
|
|
|
|
|
// Wait until roots are different
|
2022-04-14 20:55:10 +00:00
|
|
|
rootSubjects := getSubjects(updatedCfg.RootCAs)
|
|
|
|
if oldRootSubjects == rootSubjects {
|
2018-04-26 13:01:20 +00:00
|
|
|
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)
|
|
|
|
|
2019-09-23 17:52:35 +00:00
|
|
|
if oldLeafSerial.Cmp(cert.SerialNumber) == 0 {
|
2018-06-07 09:17:44 +00:00
|
|
|
r.Fatalf("leaf certificate should have changed, got serial %s",
|
2019-09-23 17:52:35 +00:00
|
|
|
connect.EncodeSerialNumber(oldLeafSerial))
|
2018-06-07 09:17:44 +00:00
|
|
|
}
|
2019-09-23 17:52:35 +00:00
|
|
|
if bytes.Equal(oldLeafKeyID, cert.SubjectKeyId) {
|
2018-04-26 13:01:20 +00:00
|
|
|
r.Fatalf("leaf should have a different key, got matching SubjectKeyID = %s",
|
2019-09-23 17:52:35 +00:00
|
|
|
connect.HexString(oldLeafKeyID))
|
2018-04-26 13:01:20 +00:00
|
|
|
}
|
|
|
|
})
|
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.
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
//require.Equal(t,"https://backend.service.consul:443", addr)
|
2018-04-03 18:10:59 +00:00
|
|
|
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()
|
|
|
|
|
2022-11-10 16:26:01 +00:00
|
|
|
bodyBytes, err := io.ReadAll(resp.Body)
|
2018-04-03 18:10:59 +00:00
|
|
|
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)
|
|
|
|
}
|
2022-04-14 20:55:10 +00:00
|
|
|
|
|
|
|
func getSubjects(cp *x509.CertPool) string {
|
|
|
|
subjectsIter := reflect.ValueOf(cp).Elem().FieldByName("byName").MapRange()
|
|
|
|
subjects := []string{}
|
|
|
|
for subjectsIter.Next() {
|
|
|
|
k := subjectsIter.Key()
|
|
|
|
subjects = append(subjects, k.String())
|
|
|
|
}
|
|
|
|
sort.Strings(subjects)
|
|
|
|
subjectList := strings.Join(subjects, ",")
|
|
|
|
return subjectList
|
|
|
|
}
|