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 (
|
2018-04-05 10:45:53 +00:00
|
|
|
"crypto/tls"
|
2018-03-29 15:25:11 +00:00
|
|
|
"crypto/x509"
|
2018-04-05 10:45:53 +00:00
|
|
|
"encoding/pem"
|
2018-03-21 22:35:00 +00:00
|
|
|
"testing"
|
|
|
|
|
2021-04-13 17:25:45 +00:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2022-04-14 20:55:10 +00:00
|
|
|
"github.com/google/go-cmp/cmp/cmpopts"
|
2021-04-13 17:25:45 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-09-04 11:31:51 +00:00
|
|
|
|
2018-04-05 10:45:53 +00:00
|
|
|
"github.com/hashicorp/consul/agent"
|
2018-03-29 15:25:11 +00:00
|
|
|
"github.com/hashicorp/consul/agent/connect"
|
2018-04-05 10:45:53 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
2023-02-17 21:14:46 +00:00
|
|
|
"github.com/hashicorp/consul/proto/private/prototest"
|
2021-04-13 17:25:45 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
|
|
|
"github.com/hashicorp/consul/testrpc"
|
2018-03-21 22:35:00 +00:00
|
|
|
)
|
|
|
|
|
2018-03-29 15:25:11 +00:00
|
|
|
func Test_verifyServerCertMatchesURI(t *testing.T) {
|
|
|
|
ca1 := connect.TestCA(t, nil)
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
certs []*x509.Certificate
|
|
|
|
expected connect.CertURI
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "simple match",
|
|
|
|
certs: TestPeerCertificates(t, "web", ca1),
|
|
|
|
expected: connect.TestSpiffeIDService(t, "web"),
|
|
|
|
wantErr: false,
|
|
|
|
},
|
2018-11-12 20:20:12 +00:00
|
|
|
{
|
|
|
|
// Could happen during migration of secondary DC to multi-DC. Trust domain
|
|
|
|
// validity is enforced with x509 name constraints where needed.
|
|
|
|
name: "different trust-domain allowed",
|
|
|
|
certs: TestPeerCertificates(t, "web", ca1),
|
|
|
|
expected: connect.TestSpiffeIDServiceWithHost(t, "web", "other.consul"),
|
|
|
|
wantErr: false,
|
|
|
|
},
|
2018-03-29 15:25:11 +00:00
|
|
|
{
|
|
|
|
name: "mismatch",
|
|
|
|
certs: TestPeerCertificates(t, "web", ca1),
|
|
|
|
expected: connect.TestSpiffeIDService(t, "db"),
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no certs",
|
|
|
|
certs: []*x509.Certificate{},
|
|
|
|
expected: connect.TestSpiffeIDService(t, "db"),
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "nil certs",
|
|
|
|
certs: nil,
|
|
|
|
expected: connect.TestSpiffeIDService(t, "db"),
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
err := verifyServerCertMatchesURI(tt.certs, tt.expected)
|
|
|
|
if tt.wantErr {
|
|
|
|
require.NotNil(t, err)
|
|
|
|
} else {
|
|
|
|
require.Nil(t, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2018-03-21 22:35:00 +00:00
|
|
|
}
|
2018-04-05 10:45:53 +00:00
|
|
|
|
|
|
|
func testCertPEMBlock(t *testing.T, pemValue string) []byte {
|
|
|
|
t.Helper()
|
|
|
|
// The _ result below is not an error but the remaining PEM bytes.
|
|
|
|
block, _ := pem.Decode([]byte(pemValue))
|
|
|
|
require.NotNil(t, block)
|
|
|
|
require.Equal(t, "CERTIFICATE", block.Type)
|
|
|
|
return block.Bytes
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientSideVerifier(t *testing.T) {
|
|
|
|
ca1 := connect.TestCA(t, nil)
|
|
|
|
ca2 := connect.TestCA(t, ca1)
|
|
|
|
|
|
|
|
webCA1PEM, _ := connect.TestLeaf(t, "web", ca1)
|
|
|
|
webCA2PEM, _ := connect.TestLeaf(t, "web", ca2)
|
|
|
|
|
|
|
|
webCA1 := testCertPEMBlock(t, webCA1PEM)
|
|
|
|
xcCA2 := testCertPEMBlock(t, ca2.SigningCert)
|
|
|
|
webCA2 := testCertPEMBlock(t, webCA2PEM)
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
tlsCfg *tls.Config
|
|
|
|
rawCerts [][]byte
|
|
|
|
wantErr string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "ok service ca1",
|
|
|
|
tlsCfg: TestTLSConfig(t, "web", ca1),
|
|
|
|
rawCerts: [][]byte{webCA1},
|
|
|
|
wantErr: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "untrusted CA",
|
|
|
|
tlsCfg: TestTLSConfig(t, "web", ca2), // only trust ca2
|
|
|
|
rawCerts: [][]byte{webCA1}, // present ca1
|
|
|
|
wantErr: "unknown authority",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "cross signed intermediate",
|
|
|
|
tlsCfg: TestTLSConfig(t, "web", ca1), // only trust ca1
|
|
|
|
rawCerts: [][]byte{webCA2, xcCA2}, // present ca2 signed cert, and xc
|
|
|
|
wantErr: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "cross signed without intermediate",
|
|
|
|
tlsCfg: TestTLSConfig(t, "web", ca1), // only trust ca1
|
|
|
|
rawCerts: [][]byte{webCA2}, // present ca2 signed cert only
|
|
|
|
wantErr: "unknown authority",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
err := clientSideVerifier(tt.tlsCfg, tt.rawCerts)
|
|
|
|
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.Nil(t, err)
|
2018-04-05 10:45:53 +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.NotNil(t, err)
|
|
|
|
require.Contains(t, err.Error(), tt.wantErr)
|
2018-04-05 10:45:53 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServerSideVerifier(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2018-04-05 10:45:53 +00:00
|
|
|
ca1 := connect.TestCA(t, nil)
|
|
|
|
ca2 := connect.TestCA(t, ca1)
|
|
|
|
|
|
|
|
webCA1PEM, _ := connect.TestLeaf(t, "web", ca1)
|
|
|
|
webCA2PEM, _ := connect.TestLeaf(t, "web", ca2)
|
|
|
|
|
|
|
|
apiCA1PEM, _ := connect.TestLeaf(t, "api", ca1)
|
|
|
|
apiCA2PEM, _ := connect.TestLeaf(t, "api", ca2)
|
|
|
|
|
|
|
|
webCA1 := testCertPEMBlock(t, webCA1PEM)
|
|
|
|
xcCA2 := testCertPEMBlock(t, ca2.SigningCert)
|
|
|
|
webCA2 := testCertPEMBlock(t, webCA2PEM)
|
|
|
|
|
|
|
|
apiCA1 := testCertPEMBlock(t, apiCA1PEM)
|
|
|
|
apiCA2 := testCertPEMBlock(t, apiCA2PEM)
|
|
|
|
|
|
|
|
// Setup a local test agent to query
|
2020-03-31 20:12:33 +00:00
|
|
|
agent := agent.StartTestAgent(t, agent.TestAgent{Name: "test-consul"})
|
2018-04-05 10:45:53 +00:00
|
|
|
defer agent.Shutdown()
|
2018-09-04 11:31:51 +00:00
|
|
|
testrpc.WaitForTestAgent(t, agent.RPC, "dc1")
|
2018-04-05 10:45:53 +00:00
|
|
|
|
|
|
|
cfg := api.DefaultConfig()
|
|
|
|
cfg.Address = agent.HTTPAddr()
|
|
|
|
client, err := api.NewClient(cfg)
|
2018-05-10 16:04:33 +00:00
|
|
|
require.NoError(t, err)
|
2018-04-05 10:45:53 +00:00
|
|
|
|
|
|
|
// Setup intentions to validate against. We actually default to allow so first
|
|
|
|
// setup a blanket deny rule for db, then only allow web.
|
|
|
|
connect := client.Connect()
|
|
|
|
ixn := &api.Intention{
|
|
|
|
SourceNS: "default",
|
|
|
|
SourceName: "*",
|
|
|
|
DestinationNS: "default",
|
|
|
|
DestinationName: "db",
|
|
|
|
Action: api.IntentionActionDeny,
|
|
|
|
SourceType: api.IntentionSourceConsul,
|
|
|
|
Meta: map[string]string{},
|
|
|
|
}
|
2020-10-06 18:24:05 +00:00
|
|
|
//nolint:staticcheck
|
2018-04-05 10:45:53 +00:00
|
|
|
id, _, err := connect.IntentionCreate(ixn, nil)
|
2018-05-10 16:04:33 +00:00
|
|
|
require.NoError(t, err)
|
2018-04-05 10:45:53 +00:00
|
|
|
require.NotEmpty(t, id)
|
|
|
|
|
|
|
|
ixn = &api.Intention{
|
|
|
|
SourceNS: "default",
|
|
|
|
SourceName: "web",
|
|
|
|
DestinationNS: "default",
|
|
|
|
DestinationName: "db",
|
|
|
|
Action: api.IntentionActionAllow,
|
|
|
|
SourceType: api.IntentionSourceConsul,
|
|
|
|
Meta: map[string]string{},
|
|
|
|
}
|
2020-10-06 18:24:05 +00:00
|
|
|
//nolint:staticcheck
|
2018-04-05 10:45:53 +00:00
|
|
|
id, _, err = connect.IntentionCreate(ixn, nil)
|
2018-05-10 16:04:33 +00:00
|
|
|
require.NoError(t, err)
|
2018-04-05 10:45:53 +00:00
|
|
|
require.NotEmpty(t, id)
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
service string
|
|
|
|
tlsCfg *tls.Config
|
|
|
|
rawCerts [][]byte
|
|
|
|
wantErr string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "ok service ca1, allow",
|
|
|
|
service: "db",
|
|
|
|
tlsCfg: TestTLSConfig(t, "db", ca1),
|
|
|
|
rawCerts: [][]byte{webCA1},
|
|
|
|
wantErr: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "untrusted CA",
|
|
|
|
service: "db",
|
|
|
|
tlsCfg: TestTLSConfig(t, "db", ca2), // only trust ca2
|
|
|
|
rawCerts: [][]byte{webCA1}, // present ca1
|
|
|
|
wantErr: "unknown authority",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "cross signed intermediate, allow",
|
|
|
|
service: "db",
|
|
|
|
tlsCfg: TestTLSConfig(t, "db", ca1), // only trust ca1
|
|
|
|
rawCerts: [][]byte{webCA2, xcCA2}, // present ca2 signed cert, and xc
|
|
|
|
wantErr: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "cross signed without intermediate",
|
|
|
|
service: "db",
|
|
|
|
tlsCfg: TestTLSConfig(t, "db", ca1), // only trust ca1
|
|
|
|
rawCerts: [][]byte{webCA2}, // present ca2 signed cert only
|
|
|
|
wantErr: "unknown authority",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ok service ca1, deny",
|
|
|
|
service: "db",
|
|
|
|
tlsCfg: TestTLSConfig(t, "db", ca1),
|
|
|
|
rawCerts: [][]byte{apiCA1},
|
|
|
|
wantErr: "denied",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "cross signed intermediate, deny",
|
|
|
|
service: "db",
|
|
|
|
tlsCfg: TestTLSConfig(t, "db", ca1), // only trust ca1
|
|
|
|
rawCerts: [][]byte{apiCA2, xcCA2}, // present ca2 signed cert, and xc
|
|
|
|
wantErr: "denied",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2020-01-28 23:50:41 +00:00
|
|
|
v := newServerSideVerifier(testutil.Logger(t), client, tt.service)
|
2018-04-05 10:45:53 +00:00
|
|
|
err := v(tt.tlsCfg, tt.rawCerts)
|
|
|
|
if tt.wantErr == "" {
|
|
|
|
require.Nil(t, err)
|
|
|
|
} else {
|
|
|
|
require.NotNil(t, err)
|
|
|
|
require.Contains(t, err.Error(), tt.wantErr)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2018-04-05 19:30:19 +00:00
|
|
|
|
|
|
|
// requireEqualTLSConfig compares tlsConfig fields we care about. Equal and even
|
|
|
|
// cmp.Diff fail on tls.Config due to unexported fields in each. expectLeaf
|
|
|
|
// allows expecting a leaf cert different from the one in expect
|
|
|
|
func requireEqualTLSConfig(t *testing.T, expect, got *tls.Config) {
|
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, expect.RootCAs, got.RootCAs)
|
2022-03-30 16:51:56 +00:00
|
|
|
prototest.AssertDeepEqual(t, expect.ClientCAs, got.ClientCAs, cmpCertPool)
|
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, expect.InsecureSkipVerify, got.InsecureSkipVerify)
|
|
|
|
require.Equal(t, expect.MinVersion, got.MinVersion)
|
|
|
|
require.Equal(t, expect.CipherSuites, got.CipherSuites)
|
|
|
|
require.NotNil(t, got.GetCertificate)
|
|
|
|
require.NotNil(t, got.GetClientCertificate)
|
|
|
|
require.NotNil(t, got.GetConfigForClient)
|
|
|
|
require.Contains(t, got.NextProtos, "h2")
|
2018-04-05 19:30:19 +00:00
|
|
|
|
|
|
|
var expectLeaf *tls.Certificate
|
|
|
|
var err error
|
|
|
|
if expect.GetCertificate != nil {
|
|
|
|
expectLeaf, err = expect.GetCertificate(nil)
|
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.Nil(t, err)
|
2018-04-05 19:30:19 +00:00
|
|
|
} else if len(expect.Certificates) > 0 {
|
|
|
|
expectLeaf = &expect.Certificates[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
gotLeaf, err := got.GetCertificate(nil)
|
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.Nil(t, err)
|
|
|
|
require.Equal(t, expectLeaf, gotLeaf)
|
2018-04-05 19:30:19 +00:00
|
|
|
|
|
|
|
gotLeaf, err = got.GetClientCertificate(nil)
|
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.Nil(t, err)
|
|
|
|
require.Equal(t, expectLeaf, gotLeaf)
|
2018-04-05 19:30:19 +00:00
|
|
|
}
|
|
|
|
|
2021-05-03 16:12:12 +00:00
|
|
|
// cmpCertPool is a custom comparison for x509.CertPool, because CertPool.lazyCerts
|
|
|
|
// has a func field which can't be compared.
|
2022-04-14 20:55:10 +00:00
|
|
|
// lazyCerts has a func field which can't be compared.
|
|
|
|
var cmpCertPool = cmp.Options{
|
|
|
|
cmpopts.IgnoreFields(x509.CertPool{}, "lazyCerts"),
|
|
|
|
cmp.AllowUnexported(x509.CertPool{}),
|
|
|
|
}
|
2021-04-13 17:25:45 +00:00
|
|
|
|
2018-04-05 19:30:19 +00:00
|
|
|
// requireCorrectVerifier invokes got.VerifyPeerCertificate and expects the
|
|
|
|
// tls.Config arg to be returned on the provided channel. This ensures the
|
|
|
|
// correct verifier func was attached to got.
|
|
|
|
//
|
|
|
|
// It then ensures that the tls.Config passed to the verifierFunc was actually
|
|
|
|
// the same as the expected current value.
|
|
|
|
func requireCorrectVerifier(t *testing.T, expect, got *tls.Config,
|
|
|
|
ch chan *tls.Config) {
|
|
|
|
|
|
|
|
err := got.VerifyPeerCertificate(nil, nil)
|
|
|
|
require.Nil(t, err)
|
|
|
|
verifierCfg := <-ch
|
|
|
|
// The tls.Cfg passed to verifyFunc should be the expected (current) value.
|
|
|
|
requireEqualTLSConfig(t, expect, verifierCfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDynamicTLSConfig(t *testing.T) {
|
|
|
|
|
|
|
|
ca1 := connect.TestCA(t, nil)
|
|
|
|
ca2 := connect.TestCA(t, nil)
|
|
|
|
baseCfg := TestTLSConfig(t, "web", ca1)
|
|
|
|
newCfg := TestTLSConfig(t, "web", ca2)
|
|
|
|
|
2018-09-07 14:58:06 +00:00
|
|
|
c := newDynamicTLSConfig(baseCfg, nil)
|
2018-04-05 19:30:19 +00:00
|
|
|
|
|
|
|
// Should set them from the base config
|
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, c.Leaf(), &baseCfg.Certificates[0])
|
|
|
|
require.Equal(t, c.Roots(), baseCfg.RootCAs)
|
2018-04-05 19:30:19 +00:00
|
|
|
|
|
|
|
// Create verifiers we can assert are set and run correctly.
|
|
|
|
v1Ch := make(chan *tls.Config, 1)
|
|
|
|
v2Ch := make(chan *tls.Config, 1)
|
|
|
|
v3Ch := make(chan *tls.Config, 1)
|
|
|
|
verify1 := func(cfg *tls.Config, rawCerts [][]byte) error {
|
|
|
|
v1Ch <- cfg
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
verify2 := func(cfg *tls.Config, rawCerts [][]byte) error {
|
|
|
|
v2Ch <- cfg
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
verify3 := func(cfg *tls.Config, rawCerts [][]byte) error {
|
|
|
|
v3Ch <- cfg
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// The dynamic config should be the one we loaded (with some different hooks)
|
|
|
|
gotBefore := c.Get(verify1)
|
|
|
|
requireEqualTLSConfig(t, baseCfg, gotBefore)
|
|
|
|
requireCorrectVerifier(t, baseCfg, gotBefore, v1Ch)
|
|
|
|
|
|
|
|
// Now change the roots as if we just loaded new roots from Consul
|
|
|
|
err := c.SetRoots(newCfg.RootCAs)
|
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.Nil(t, err)
|
2018-04-05 19:30:19 +00:00
|
|
|
|
|
|
|
// The dynamic config should have the new roots, but old leaf
|
|
|
|
gotAfter := c.Get(verify2)
|
|
|
|
expect := newCfg.Clone()
|
|
|
|
expect.GetCertificate = func(_ *tls.ClientHelloInfo) (*tls.Certificate, error) {
|
|
|
|
return &baseCfg.Certificates[0], nil
|
|
|
|
}
|
|
|
|
requireEqualTLSConfig(t, expect, gotAfter)
|
|
|
|
requireCorrectVerifier(t, expect, gotAfter, v2Ch)
|
|
|
|
|
|
|
|
// The old config fetched before should still call it's own verify func, but
|
|
|
|
// that verifier should be passed the new config (expect).
|
|
|
|
requireCorrectVerifier(t, expect, gotBefore, v1Ch)
|
|
|
|
|
|
|
|
// Now change the leaf
|
|
|
|
err = c.SetLeaf(&newCfg.Certificates[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.Nil(t, err)
|
2018-04-05 19:30:19 +00:00
|
|
|
|
|
|
|
// The dynamic config should have the new roots, AND new leaf
|
|
|
|
gotAfterLeaf := c.Get(verify3)
|
|
|
|
requireEqualTLSConfig(t, newCfg, gotAfterLeaf)
|
|
|
|
requireCorrectVerifier(t, newCfg, gotAfterLeaf, v3Ch)
|
|
|
|
|
|
|
|
// Both older configs should still call their own verify funcs, but those
|
|
|
|
// verifiers should be passed the new config.
|
|
|
|
requireCorrectVerifier(t, newCfg, gotBefore, v1Ch)
|
|
|
|
requireCorrectVerifier(t, newCfg, gotAfter, v2Ch)
|
|
|
|
}
|
2018-04-26 13:01:20 +00:00
|
|
|
|
|
|
|
func TestDynamicTLSConfig_Ready(t *testing.T) {
|
|
|
|
|
|
|
|
ca1 := connect.TestCA(t, nil)
|
|
|
|
baseCfg := TestTLSConfig(t, "web", ca1)
|
|
|
|
|
2018-09-07 14:58:06 +00:00
|
|
|
c := newDynamicTLSConfig(defaultTLSConfig(), nil)
|
2018-04-26 13:01:20 +00:00
|
|
|
readyCh := c.ReadyWait()
|
|
|
|
assertBlocked(t, readyCh)
|
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.False(t, c.Ready(), "no roots or leaf, should not be ready")
|
2018-04-26 13:01:20 +00:00
|
|
|
|
|
|
|
err := c.SetLeaf(&baseCfg.Certificates[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)
|
2018-04-26 13:01:20 +00:00
|
|
|
assertBlocked(t, readyCh)
|
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.False(t, c.Ready(), "no roots, should not be ready")
|
2018-04-26 13:01:20 +00:00
|
|
|
|
|
|
|
err = c.SetRoots(baseCfg.RootCAs)
|
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
|
|
|
assertNotBlocked(t, readyCh)
|
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.True(t, c.Ready(), "should be ready")
|
2018-09-07 14:58:06 +00:00
|
|
|
|
|
|
|
ca2 := connect.TestCA(t, nil)
|
|
|
|
ca2cfg := TestTLSConfig(t, "web", ca2)
|
|
|
|
|
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, c.SetRoots(ca2cfg.RootCAs))
|
2018-09-07 14:58:06 +00:00
|
|
|
assertNotBlocked(t, readyCh)
|
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.False(t, c.Ready(), "invalid leaf, should not be ready")
|
2018-09-07 14:58:06 +00:00
|
|
|
|
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, c.SetRoots(baseCfg.RootCAs))
|
2018-09-07 14:58:06 +00:00
|
|
|
assertNotBlocked(t, readyCh)
|
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.True(t, c.Ready(), "should be ready")
|
2018-04-26 13:01:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func assertBlocked(t *testing.T, ch <-chan struct{}) {
|
|
|
|
t.Helper()
|
|
|
|
select {
|
|
|
|
case <-ch:
|
|
|
|
t.Fatalf("want blocked chan")
|
|
|
|
default:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertNotBlocked(t *testing.T, ch <-chan struct{}) {
|
|
|
|
t.Helper()
|
|
|
|
select {
|
|
|
|
case <-ch:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
t.Fatalf("want unblocked chan but it blocked")
|
|
|
|
}
|
|
|
|
}
|