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
|
|
|
|
2018-03-20 17:36:05 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
2018-05-04 22:28:11 +00:00
|
|
|
"bytes"
|
2020-10-09 14:43:33 +00:00
|
|
|
"crypto/x509"
|
2022-04-14 20:55:10 +00:00
|
|
|
"encoding/pem"
|
2022-11-10 16:26:01 +00:00
|
|
|
"io"
|
2018-03-20 17:36:05 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
2018-09-10 15:58:53 +00:00
|
|
|
"github.com/hashicorp/consul/testrpc"
|
|
|
|
|
2018-07-25 19:26:27 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2021-07-05 23:10:23 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2018-03-21 17:20:35 +00:00
|
|
|
"github.com/hashicorp/consul/agent/connect"
|
2018-03-20 17:36:05 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestConnectCARoots_empty(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2018-03-20 17:36:05 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "connect { enabled = false }")
|
2018-03-20 17:36:05 +00:00
|
|
|
defer a.Shutdown()
|
2018-09-12 13:49:27 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2018-03-20 17:36:05 +00:00
|
|
|
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/connect/ca/roots", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
2018-07-25 19:26:27 +00:00
|
|
|
_, err := a.srv.ConnectCARoots(resp, req)
|
2021-07-01 00:48:29 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), "Connect must be enabled")
|
2018-03-20 17:36:05 +00:00
|
|
|
}
|
2018-03-21 17:10:53 +00:00
|
|
|
|
|
|
|
func TestConnectCARoots_list(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2018-03-21 17:10:53 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2018-03-21 17:10:53 +00:00
|
|
|
defer a.Shutdown()
|
2018-09-10 15:58:53 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2018-03-21 17:10:53 +00:00
|
|
|
|
2018-04-30 21:23:49 +00:00
|
|
|
// Set some CAs. Note that NewTestAgent already bootstraps one CA so this just
|
|
|
|
// adds a second and makes it active.
|
|
|
|
ca2 := connect.TestCAConfigSet(t, a, nil)
|
2018-03-21 17:10:53 +00:00
|
|
|
|
2018-03-21 17:20:35 +00:00
|
|
|
// List
|
2018-03-21 17:10:53 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/connect/ca/roots", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.ConnectCARoots(resp, req)
|
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
|
|
|
assert.NoError(t, err)
|
2018-03-21 17:10:53 +00:00
|
|
|
|
|
|
|
value := obj.(structs.IndexedCARoots)
|
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
|
|
|
assert.Equal(t, value.ActiveRootID, ca2.ID)
|
|
|
|
assert.Len(t, value.Roots, 2)
|
2018-03-21 17:20:35 +00:00
|
|
|
|
|
|
|
// We should never have the secret information
|
|
|
|
for _, r := range value.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
|
|
|
assert.Equal(t, "", r.SigningCert)
|
|
|
|
assert.Equal(t, "", r.SigningKey)
|
2018-03-21 17:20:35 +00:00
|
|
|
}
|
2018-03-21 17:10:53 +00:00
|
|
|
}
|
2018-05-04 22:28:11 +00:00
|
|
|
|
|
|
|
func TestConnectCAConfig(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2018-05-04 22:28:11 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2019-11-11 21:36:22 +00:00
|
|
|
tests := []struct {
|
2019-11-21 17:40:29 +00:00
|
|
|
name string
|
|
|
|
initialState string
|
|
|
|
body string
|
|
|
|
wantErr bool
|
|
|
|
wantCfg structs.CAConfiguration
|
2019-11-11 21:36:22 +00:00
|
|
|
}{
|
2018-06-22 16:48:41 +00:00
|
|
|
{
|
2019-11-11 21:36:22 +00:00
|
|
|
name: "basic",
|
|
|
|
body: `
|
|
|
|
{
|
|
|
|
"Provider": "consul",
|
|
|
|
"Config": {
|
|
|
|
"LeafCertTTL": "72h",
|
2020-02-10 23:05:49 +00:00
|
|
|
"IntermediateCertTTL": "288h"
|
2019-11-11 21:36:22 +00:00
|
|
|
}
|
|
|
|
}`,
|
|
|
|
wantErr: false,
|
|
|
|
wantCfg: structs.CAConfiguration{
|
|
|
|
Provider: "consul",
|
|
|
|
ClusterID: connect.TestClusterID,
|
|
|
|
Config: map[string]interface{}{
|
2020-02-10 23:05:49 +00:00
|
|
|
"LeafCertTTL": "72h",
|
|
|
|
"IntermediateCertTTL": "288h",
|
2019-11-11 21:36:22 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-01-17 22:27:13 +00:00
|
|
|
{
|
|
|
|
name: "basic with IntermediateCertTTL",
|
|
|
|
body: `
|
|
|
|
{
|
|
|
|
"Provider": "consul",
|
|
|
|
"Config": {
|
|
|
|
"LeafCertTTL": "72h",
|
2020-02-10 23:05:49 +00:00
|
|
|
"IntermediateCertTTL": "288h"
|
2020-01-17 22:27:13 +00:00
|
|
|
}
|
|
|
|
}`,
|
|
|
|
wantErr: false,
|
|
|
|
wantCfg: structs.CAConfiguration{
|
|
|
|
Provider: "consul",
|
|
|
|
ClusterID: connect.TestClusterID,
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"LeafCertTTL": "72h",
|
2020-02-10 23:05:49 +00:00
|
|
|
"IntermediateCertTTL": "288h",
|
2020-01-17 22:27:13 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-11-11 21:36:22 +00:00
|
|
|
{
|
|
|
|
name: "force without cross sign CamelCase",
|
|
|
|
body: `
|
|
|
|
{
|
|
|
|
"Provider": "consul",
|
|
|
|
"Config": {
|
|
|
|
"LeafCertTTL": "72h",
|
2020-02-10 23:05:49 +00:00
|
|
|
"IntermediateCertTTL": "288h"
|
2019-11-11 21:36:22 +00:00
|
|
|
},
|
|
|
|
"ForceWithoutCrossSigning": true
|
|
|
|
}`,
|
|
|
|
wantErr: false,
|
|
|
|
wantCfg: structs.CAConfiguration{
|
|
|
|
Provider: "consul",
|
|
|
|
ClusterID: connect.TestClusterID,
|
|
|
|
Config: map[string]interface{}{
|
2020-02-10 23:05:49 +00:00
|
|
|
"LeafCertTTL": "72h",
|
|
|
|
"IntermediateCertTTL": "288h",
|
2019-11-11 21:36:22 +00:00
|
|
|
},
|
|
|
|
ForceWithoutCrossSigning: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "force without cross sign snake_case",
|
|
|
|
// Note that config is still CamelCase. We don't currently support snake
|
|
|
|
// case config in the API only in config files for this. Arguably that's a
|
|
|
|
// bug but it's unrelated to the force options being tested here so we'll
|
|
|
|
// only test the new behaviour here rather than scope creep to refactoring
|
|
|
|
// all the CA config handling.
|
|
|
|
body: `
|
|
|
|
{
|
|
|
|
"provider": "consul",
|
|
|
|
"config": {
|
|
|
|
"LeafCertTTL": "72h",
|
2020-02-10 23:05:49 +00:00
|
|
|
"IntermediateCertTTL": "288h"
|
2019-11-11 21:36:22 +00:00
|
|
|
},
|
|
|
|
"force_without_cross_signing": true
|
|
|
|
}`,
|
|
|
|
wantErr: false,
|
|
|
|
wantCfg: structs.CAConfiguration{
|
|
|
|
Provider: "consul",
|
|
|
|
ClusterID: connect.TestClusterID,
|
|
|
|
Config: map[string]interface{}{
|
2020-02-10 23:05:49 +00:00
|
|
|
"LeafCertTTL": "72h",
|
|
|
|
"IntermediateCertTTL": "288h",
|
2019-11-11 21:36:22 +00:00
|
|
|
},
|
|
|
|
ForceWithoutCrossSigning: true,
|
|
|
|
},
|
|
|
|
},
|
2019-11-21 17:40:29 +00:00
|
|
|
{
|
|
|
|
name: "setting state fails",
|
|
|
|
body: `
|
|
|
|
{
|
|
|
|
"Provider": "consul",
|
|
|
|
"State": {
|
|
|
|
"foo": "bar"
|
|
|
|
}
|
|
|
|
}`,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "updating config with same state",
|
|
|
|
initialState: `foo = "bar"`,
|
|
|
|
body: `
|
|
|
|
{
|
|
|
|
"Provider": "consul",
|
|
|
|
"config": {
|
|
|
|
"LeafCertTTL": "72h",
|
2021-07-05 23:10:23 +00:00
|
|
|
"IntermediateCertTTL": "288h"
|
2019-11-21 17:40:29 +00:00
|
|
|
},
|
|
|
|
"State": {
|
|
|
|
"foo": "bar"
|
|
|
|
}
|
|
|
|
}`,
|
|
|
|
wantErr: false,
|
|
|
|
wantCfg: structs.CAConfiguration{
|
|
|
|
Provider: "consul",
|
|
|
|
ClusterID: connect.TestClusterID,
|
|
|
|
Config: map[string]interface{}{
|
2020-02-10 23:05:49 +00:00
|
|
|
"LeafCertTTL": "72h",
|
|
|
|
"IntermediateCertTTL": "288h",
|
2019-11-21 17:40:29 +00:00
|
|
|
},
|
|
|
|
State: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-05-04 22:28:11 +00:00
|
|
|
}
|
|
|
|
|
2019-11-11 21:36:22 +00:00
|
|
|
for _, tc := range tests {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2019-11-21 17:40:29 +00:00
|
|
|
hcl := ""
|
|
|
|
if tc.initialState != "" {
|
|
|
|
hcl = `
|
|
|
|
connect {
|
|
|
|
enabled = true
|
|
|
|
ca_provider = "consul"
|
|
|
|
ca_config {
|
2020-02-10 23:05:49 +00:00
|
|
|
intermediate_cert_ttl = "288h"
|
2019-11-21 17:40:29 +00:00
|
|
|
test_state {
|
|
|
|
` + tc.initialState + `
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
}
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, hcl)
|
2019-11-11 21:36:22 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
|
|
|
|
|
|
|
// Set the config.
|
|
|
|
{
|
|
|
|
body := bytes.NewBuffer([]byte(tc.body))
|
|
|
|
req, _ := http.NewRequest("PUT", "/v1/connect/ca/configuration", body)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
_, err := a.srv.ConnectCAConfiguration(resp, req)
|
|
|
|
if tc.wantErr {
|
2021-07-01 00:48:29 +00:00
|
|
|
require.Error(t, err)
|
2019-11-11 21:36:22 +00:00
|
|
|
return
|
|
|
|
}
|
2021-07-01 00:48:29 +00:00
|
|
|
require.NoError(t, err)
|
2019-11-11 21:36:22 +00:00
|
|
|
}
|
|
|
|
// The config should be updated now.
|
|
|
|
{
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/connect/ca/configuration", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.ConnectCAConfiguration(resp, req)
|
2021-07-01 00:48:29 +00:00
|
|
|
require.NoError(t, err)
|
2019-11-11 21:36:22 +00:00
|
|
|
|
|
|
|
got := obj.(structs.CAConfiguration)
|
|
|
|
// Reset Raft indexes to make it non flaky
|
|
|
|
got.CreateIndex = 0
|
|
|
|
got.ModifyIndex = 0
|
2021-07-01 00:48:29 +00:00
|
|
|
require.Equal(t, tc.wantCfg, got)
|
2019-11-11 21:36:22 +00:00
|
|
|
}
|
|
|
|
})
|
2018-05-04 22:28:11 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-09 14:43:33 +00:00
|
|
|
|
|
|
|
func TestConnectCARoots_PEMEncoding(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2020-10-09 14:43:33 +00:00
|
|
|
primary := NewTestAgent(t, "")
|
|
|
|
defer primary.Shutdown()
|
|
|
|
testrpc.WaitForActiveCARoot(t, primary.RPC, "dc1", nil)
|
|
|
|
|
|
|
|
secondary := NewTestAgent(t, `
|
|
|
|
primary_datacenter = "dc1"
|
|
|
|
datacenter = "dc2"
|
|
|
|
retry_join_wan = ["`+primary.Config.SerfBindAddrWAN.String()+`"]
|
|
|
|
`)
|
|
|
|
defer secondary.Shutdown()
|
|
|
|
testrpc.WaitForActiveCARoot(t, secondary.RPC, "dc2", nil)
|
|
|
|
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/connect/ca/roots?pem=true", nil)
|
|
|
|
recorder := httptest.NewRecorder()
|
|
|
|
obj, err := secondary.srv.ConnectCARoots(recorder, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Nil(t, obj, "Endpoint returned an object for serialization when it should have returned nil and written to the responses")
|
|
|
|
resp := recorder.Result()
|
|
|
|
require.Equal(t, resp.Header.Get("Content-Type"), "application/pem-certificate-chain")
|
|
|
|
|
2022-11-10 16:26:01 +00:00
|
|
|
data, err := io.ReadAll(resp.Body)
|
2020-10-09 14:43:33 +00:00
|
|
|
require.NoError(t, err)
|
2022-04-14 20:55:10 +00:00
|
|
|
|
2020-10-09 14:43:33 +00:00
|
|
|
// expecting the root cert from dc1 and an intermediate in dc2
|
2022-04-14 20:55:10 +00:00
|
|
|
block, rest := pem.Decode(data)
|
|
|
|
_, err = x509.ParseCertificate(block.Bytes)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
block, _ = pem.Decode(rest)
|
|
|
|
_, err = x509.ParseCertificate(block.Bytes)
|
|
|
|
require.NoError(t, err)
|
2020-10-09 14:43:33 +00:00
|
|
|
}
|