2020-11-20 04:08:06 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2021-07-13 16:15:06 +00:00
|
|
|
"bytes"
|
2020-11-20 04:08:06 +00:00
|
|
|
"context"
|
2021-07-13 16:15:06 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
2020-11-20 04:08:06 +00:00
|
|
|
"crypto/x509"
|
2021-07-13 16:15:06 +00:00
|
|
|
"crypto/x509/pkix"
|
|
|
|
"encoding/pem"
|
2020-11-20 04:08:06 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-07-13 16:15:06 +00:00
|
|
|
"math/big"
|
2021-12-07 00:05:35 +00:00
|
|
|
"net/rpc"
|
2020-11-20 04:08:06 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-12-07 00:05:35 +00:00
|
|
|
msgpackrpc "github.com/hashicorp/net-rpc-msgpackrpc"
|
2021-06-22 21:25:38 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-06-21 23:22:01 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2020-11-20 04:08:06 +00:00
|
|
|
"github.com/hashicorp/consul/agent/connect"
|
|
|
|
ca "github.com/hashicorp/consul/agent/connect/ca"
|
2021-12-02 22:10:25 +00:00
|
|
|
"github.com/hashicorp/consul/agent/consul/fsm"
|
2020-11-20 04:08:06 +00:00
|
|
|
"github.com/hashicorp/consul/agent/consul/state"
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2021-06-21 23:22:01 +00:00
|
|
|
"github.com/hashicorp/consul/agent/token"
|
2020-11-20 04:08:06 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
2021-12-02 19:57:06 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil/retry"
|
2021-12-02 22:10:25 +00:00
|
|
|
"github.com/hashicorp/consul/testrpc"
|
2020-11-20 04:08:06 +00:00
|
|
|
)
|
|
|
|
|
2020-11-30 22:37:24 +00:00
|
|
|
// TODO(kyhavlov): replace with t.Deadline()
|
|
|
|
const CATestTimeout = 7 * time.Second
|
|
|
|
|
2021-12-07 00:05:35 +00:00
|
|
|
func TestCAManager_Initialize_Vault_Secondary_SharedVault(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
ca.SkipIfVaultNotPresent(t)
|
|
|
|
|
|
|
|
vault := ca.NewTestVaultServer(t)
|
|
|
|
|
|
|
|
_, serverDC1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.CAConfig = &structs.CAConfiguration{
|
|
|
|
Provider: "vault",
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"Address": vault.Addr,
|
|
|
|
"Token": vault.RootToken,
|
|
|
|
"RootPKIPath": "pki-root/",
|
|
|
|
"IntermediatePKIPath": "pki-primary/",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
runStep(t, "check primary DC", func(t *testing.T) {
|
|
|
|
testrpc.WaitForTestAgent(t, serverDC1.RPC, "dc1")
|
|
|
|
|
|
|
|
codec := rpcClient(t, serverDC1)
|
|
|
|
roots := structs.IndexedCARoots{}
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, "ConnectCA.Roots", &structs.DCSpecificRequest{}, &roots)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, roots.Roots, 1)
|
|
|
|
|
|
|
|
leafPEM := getLeafCert(t, codec, roots.TrustDomain, "dc1")
|
|
|
|
verifyLeafCert(t, roots.Roots[0], leafPEM)
|
|
|
|
})
|
|
|
|
|
|
|
|
runStep(t, "start secondary DC", func(t *testing.T) {
|
|
|
|
_, serverDC2 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.Datacenter = "dc2"
|
|
|
|
c.PrimaryDatacenter = "dc1"
|
|
|
|
c.CAConfig = &structs.CAConfiguration{
|
|
|
|
Provider: "vault",
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"Address": vault.Addr,
|
|
|
|
"Token": vault.RootToken,
|
|
|
|
"RootPKIPath": "pki-root/",
|
|
|
|
"IntermediatePKIPath": "pki-secondary/",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
})
|
|
|
|
defer serverDC2.Shutdown()
|
|
|
|
joinWAN(t, serverDC2, serverDC1)
|
|
|
|
testrpc.WaitForActiveCARoot(t, serverDC2.RPC, "dc2", nil)
|
|
|
|
|
|
|
|
codec := rpcClient(t, serverDC2)
|
|
|
|
roots := structs.IndexedCARoots{}
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, "ConnectCA.Roots", &structs.DCSpecificRequest{}, &roots)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, roots.Roots, 1)
|
|
|
|
|
|
|
|
leafPEM := getLeafCert(t, codec, roots.TrustDomain, "dc2")
|
|
|
|
verifyLeafCert(t, roots.Roots[0], leafPEM)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func verifyLeafCert(t *testing.T, root *structs.CARoot, leafCertPEM string) {
|
|
|
|
t.Helper()
|
|
|
|
leaf, intermediates, err := connect.ParseLeafCerts(leafCertPEM)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
pool := x509.NewCertPool()
|
|
|
|
ok := pool.AppendCertsFromPEM([]byte(root.RootCert))
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Failed to add root CA PEM to cert pool")
|
|
|
|
}
|
|
|
|
|
|
|
|
// verify with intermediates from leaf CertPEM
|
|
|
|
_, err = leaf.Verify(x509.VerifyOptions{
|
|
|
|
Roots: pool,
|
|
|
|
Intermediates: intermediates,
|
|
|
|
KeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
|
|
|
|
})
|
|
|
|
require.NoError(t, err, "failed to verify using intermediates from leaf cert PEM")
|
|
|
|
|
|
|
|
// verify with intermediates from the CARoot
|
|
|
|
intermediates = x509.NewCertPool()
|
|
|
|
for _, intermediate := range root.IntermediateCerts {
|
|
|
|
c, err := connect.ParseCert(intermediate)
|
|
|
|
require.NoError(t, err)
|
|
|
|
intermediates.AddCert(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = leaf.Verify(x509.VerifyOptions{
|
|
|
|
Roots: pool,
|
|
|
|
Intermediates: intermediates,
|
|
|
|
KeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
|
|
|
|
})
|
|
|
|
require.NoError(t, err, "failed to verify using intermediates from CARoot list")
|
|
|
|
}
|
|
|
|
|
2020-11-20 04:08:06 +00:00
|
|
|
type mockCAServerDelegate struct {
|
|
|
|
t *testing.T
|
|
|
|
config *Config
|
|
|
|
store *state.Store
|
|
|
|
primaryRoot *structs.CARoot
|
|
|
|
callbackCh chan string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMockCAServerDelegate(t *testing.T, config *Config) *mockCAServerDelegate {
|
|
|
|
delegate := &mockCAServerDelegate{
|
|
|
|
t: t,
|
|
|
|
config: config,
|
|
|
|
store: state.NewStateStore(nil),
|
|
|
|
primaryRoot: connect.TestCAWithTTL(t, nil, 1*time.Second),
|
|
|
|
callbackCh: make(chan string, 0),
|
|
|
|
}
|
|
|
|
delegate.store.CASetConfig(1, testCAConfig())
|
|
|
|
|
|
|
|
return delegate
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockCAServerDelegate) State() *state.Store {
|
|
|
|
return m.store
|
|
|
|
}
|
|
|
|
|
2021-11-12 00:03:52 +00:00
|
|
|
func (m *mockCAServerDelegate) ProviderState(id string) (*structs.CAConsulProviderState, error) {
|
|
|
|
_, s, err := m.store.CAProviderState(id)
|
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
|
2020-11-20 04:08:06 +00:00
|
|
|
func (m *mockCAServerDelegate) IsLeader() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-11-05 20:37:10 +00:00
|
|
|
func (m *mockCAServerDelegate) ServersSupportMultiDCConnectCA() error {
|
|
|
|
return nil
|
2020-11-20 04:08:06 +00:00
|
|
|
}
|
|
|
|
|
2021-06-23 20:30:59 +00:00
|
|
|
func (m *mockCAServerDelegate) ApplyCALeafRequest() (uint64, error) {
|
|
|
|
return 3, nil
|
|
|
|
}
|
|
|
|
|
2021-06-22 21:25:38 +00:00
|
|
|
// ApplyCARequest mirrors FSM.applyConnectCAOperation because that functionality
|
|
|
|
// is not exported.
|
2020-11-20 04:08:06 +00:00
|
|
|
func (m *mockCAServerDelegate) ApplyCARequest(req *structs.CARequest) (interface{}, error) {
|
2021-06-22 21:25:38 +00:00
|
|
|
idx, _, err := m.store.CAConfig(nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
m.callbackCh <- fmt.Sprintf("raftApply/ConnectCA")
|
|
|
|
|
2021-12-02 22:10:25 +00:00
|
|
|
result := fsm.ApplyConnectCAOperationFromRequest(m.store, req, idx+1)
|
|
|
|
if err, ok := result.(error); ok && err != nil {
|
|
|
|
return nil, err
|
2021-06-22 21:25:38 +00:00
|
|
|
}
|
2021-12-02 22:10:25 +00:00
|
|
|
return result, nil
|
2020-11-20 04:08:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockCAServerDelegate) forwardDC(method, dc string, args interface{}, reply interface{}) error {
|
|
|
|
switch method {
|
|
|
|
case "ConnectCA.Roots":
|
|
|
|
roots := reply.(*structs.IndexedCARoots)
|
|
|
|
roots.TrustDomain = connect.TestClusterID
|
|
|
|
roots.Roots = []*structs.CARoot{m.primaryRoot}
|
|
|
|
roots.ActiveRootID = m.primaryRoot.ID
|
|
|
|
case "ConnectCA.SignIntermediate":
|
|
|
|
r := reply.(*string)
|
|
|
|
*r = m.primaryRoot.RootCert
|
2020-11-30 22:37:24 +00:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("received call to unsupported method %q", method)
|
2020-11-20 04:08:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m.callbackCh <- fmt.Sprintf("forwardDC/%s", method)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockCAServerDelegate) generateCASignRequest(csr string) *structs.CASignRequest {
|
|
|
|
return &structs.CASignRequest{
|
|
|
|
Datacenter: m.config.PrimaryDatacenter,
|
|
|
|
CSR: csr,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// mockCAProvider mocks an empty provider implementation with a channel in order to coordinate
|
|
|
|
// waiting for certain methods to be called.
|
|
|
|
type mockCAProvider struct {
|
2021-07-13 16:15:06 +00:00
|
|
|
callbackCh chan string
|
|
|
|
rootPEM string
|
|
|
|
intermediatePem string
|
2020-11-20 04:08:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockCAProvider) Configure(cfg ca.ProviderConfig) error { return nil }
|
|
|
|
func (m *mockCAProvider) State() (map[string]string, error) { return nil, nil }
|
|
|
|
func (m *mockCAProvider) GenerateRoot() error { return nil }
|
2021-07-01 00:48:29 +00:00
|
|
|
func (m *mockCAProvider) ActiveRoot() (string, error) {
|
|
|
|
return m.rootPEM, nil
|
|
|
|
}
|
2020-11-20 04:08:06 +00:00
|
|
|
func (m *mockCAProvider) GenerateIntermediateCSR() (string, error) {
|
|
|
|
m.callbackCh <- "provider/GenerateIntermediateCSR"
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
func (m *mockCAProvider) SetIntermediate(intermediatePEM, rootPEM string) error {
|
|
|
|
m.callbackCh <- "provider/SetIntermediate"
|
|
|
|
return nil
|
|
|
|
}
|
2021-07-01 00:48:29 +00:00
|
|
|
func (m *mockCAProvider) ActiveIntermediate() (string, error) {
|
2021-07-13 16:15:06 +00:00
|
|
|
if m.intermediatePem == "" {
|
|
|
|
return m.rootPEM, nil
|
|
|
|
}
|
|
|
|
return m.intermediatePem, nil
|
2021-07-01 00:48:29 +00:00
|
|
|
}
|
2020-11-20 04:08:06 +00:00
|
|
|
func (m *mockCAProvider) GenerateIntermediate() (string, error) { return "", nil }
|
|
|
|
func (m *mockCAProvider) Sign(*x509.CertificateRequest) (string, error) { return "", nil }
|
|
|
|
func (m *mockCAProvider) SignIntermediate(*x509.CertificateRequest) (string, error) { return "", nil }
|
|
|
|
func (m *mockCAProvider) CrossSignCA(*x509.Certificate) (string, error) { return "", nil }
|
|
|
|
func (m *mockCAProvider) SupportsCrossSigning() (bool, error) { return false, nil }
|
2021-01-15 18:20:27 +00:00
|
|
|
func (m *mockCAProvider) Cleanup(_ bool, _ map[string]interface{}) error { return nil }
|
2020-11-20 04:08:06 +00:00
|
|
|
|
|
|
|
func waitForCh(t *testing.T, ch chan string, expected string) {
|
2021-06-22 21:25:38 +00:00
|
|
|
t.Helper()
|
2020-11-20 04:08:06 +00:00
|
|
|
select {
|
|
|
|
case op := <-ch:
|
|
|
|
if op != expected {
|
|
|
|
t.Fatalf("got unexpected op %q, wanted %q", op, expected)
|
|
|
|
}
|
2020-11-30 22:37:24 +00:00
|
|
|
case <-time.After(CATestTimeout):
|
2020-11-20 04:08:06 +00:00
|
|
|
t.Fatalf("never got op %q", expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func waitForEmptyCh(t *testing.T, ch chan string) {
|
|
|
|
select {
|
|
|
|
case op := <-ch:
|
|
|
|
t.Fatalf("got unexpected op %q", op)
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCAConfig() *structs.CAConfiguration {
|
|
|
|
return &structs.CAConfiguration{
|
|
|
|
ClusterID: connect.TestClusterID,
|
|
|
|
Provider: "mock",
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"LeafCertTTL": "72h",
|
|
|
|
"IntermediateCertTTL": "2160h",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// initTestManager initializes a CAManager with a mockCAServerDelegate, consuming
|
|
|
|
// the ops that come through the channels and returning when initialization has finished.
|
|
|
|
func initTestManager(t *testing.T, manager *CAManager, delegate *mockCAServerDelegate) {
|
2021-06-22 21:25:38 +00:00
|
|
|
t.Helper()
|
2020-11-20 04:08:06 +00:00
|
|
|
initCh := make(chan struct{})
|
|
|
|
go func() {
|
2021-12-02 19:57:06 +00:00
|
|
|
require.NoError(t, manager.Initialize())
|
2020-11-20 04:08:06 +00:00
|
|
|
close(initCh)
|
|
|
|
}()
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
select {
|
|
|
|
case <-delegate.callbackCh:
|
2020-11-30 22:37:24 +00:00
|
|
|
case <-time.After(CATestTimeout):
|
2020-11-20 04:08:06 +00:00
|
|
|
t.Fatal("failed waiting for initialization events")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-initCh:
|
2020-11-30 22:37:24 +00:00
|
|
|
case <-time.After(CATestTimeout):
|
2020-11-20 04:08:06 +00:00
|
|
|
t.Fatal("failed waiting for initialization")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCAManager_Initialize(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
|
2020-11-20 04:08:06 +00:00
|
|
|
conf := DefaultConfig()
|
|
|
|
conf.ConnectEnabled = true
|
|
|
|
conf.PrimaryDatacenter = "dc1"
|
|
|
|
conf.Datacenter = "dc2"
|
|
|
|
delegate := NewMockCAServerDelegate(t, conf)
|
2021-01-15 18:20:27 +00:00
|
|
|
manager := NewCAManager(delegate, nil, testutil.Logger(t), conf)
|
2021-06-22 21:25:38 +00:00
|
|
|
manager.providerShim = &mockCAProvider{
|
|
|
|
callbackCh: delegate.callbackCh,
|
|
|
|
rootPEM: delegate.primaryRoot.RootCert,
|
|
|
|
}
|
2020-11-20 04:08:06 +00:00
|
|
|
|
2021-12-02 19:57:06 +00:00
|
|
|
// Call Initialize and then confirm the RPCs and provider calls
|
2020-11-20 04:08:06 +00:00
|
|
|
// happen in the expected order.
|
2021-06-22 21:25:38 +00:00
|
|
|
require.Equal(t, caStateUninitialized, manager.state)
|
2020-11-30 22:37:24 +00:00
|
|
|
errCh := make(chan error)
|
2020-11-20 04:08:06 +00:00
|
|
|
go func() {
|
2021-12-02 19:57:06 +00:00
|
|
|
err := manager.Initialize()
|
2021-06-22 21:25:38 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
errCh <- err
|
2020-11-20 04:08:06 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
waitForCh(t, delegate.callbackCh, "forwardDC/ConnectCA.Roots")
|
2021-01-15 18:20:27 +00:00
|
|
|
require.EqualValues(t, caStateInitializing, manager.state)
|
2020-11-20 04:08:06 +00:00
|
|
|
waitForCh(t, delegate.callbackCh, "provider/GenerateIntermediateCSR")
|
|
|
|
waitForCh(t, delegate.callbackCh, "forwardDC/ConnectCA.SignIntermediate")
|
|
|
|
waitForCh(t, delegate.callbackCh, "provider/SetIntermediate")
|
|
|
|
waitForCh(t, delegate.callbackCh, "raftApply/ConnectCA")
|
|
|
|
waitForEmptyCh(t, delegate.callbackCh)
|
2020-11-30 22:37:24 +00:00
|
|
|
|
2021-12-02 19:57:06 +00:00
|
|
|
// Make sure the Initialize call returned successfully.
|
2020-11-30 22:37:24 +00:00
|
|
|
select {
|
|
|
|
case err := <-errCh:
|
|
|
|
require.NoError(t, err)
|
|
|
|
case <-time.After(CATestTimeout):
|
|
|
|
t.Fatal("never got result from errCh")
|
|
|
|
}
|
|
|
|
|
2021-06-22 21:25:38 +00:00
|
|
|
require.Equal(t, caStateInitialized, manager.state)
|
2020-11-20 04:08:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCAManager_UpdateConfigWhileRenewIntermediate(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
|
2020-11-20 04:08:06 +00:00
|
|
|
// No parallel execution because we change globals
|
2021-12-08 17:21:52 +00:00
|
|
|
patchIntermediateCertRenewInterval(t)
|
2020-11-20 04:08:06 +00:00
|
|
|
|
|
|
|
conf := DefaultConfig()
|
|
|
|
conf.ConnectEnabled = true
|
|
|
|
conf.PrimaryDatacenter = "dc1"
|
|
|
|
conf.Datacenter = "dc2"
|
|
|
|
delegate := NewMockCAServerDelegate(t, conf)
|
2021-01-15 18:20:27 +00:00
|
|
|
manager := NewCAManager(delegate, nil, testutil.Logger(t), conf)
|
2021-06-22 21:25:38 +00:00
|
|
|
manager.providerShim = &mockCAProvider{
|
|
|
|
callbackCh: delegate.callbackCh,
|
|
|
|
rootPEM: delegate.primaryRoot.RootCert,
|
|
|
|
}
|
2020-11-20 04:08:06 +00:00
|
|
|
initTestManager(t, manager, delegate)
|
|
|
|
|
2021-07-13 16:15:06 +00:00
|
|
|
// Simulate Wait half the TTL for the cert to need renewing.
|
|
|
|
manager.timeNow = func() time.Time {
|
|
|
|
return time.Now().Add(500 * time.Millisecond)
|
|
|
|
}
|
2020-11-20 04:08:06 +00:00
|
|
|
|
|
|
|
// Call RenewIntermediate and then confirm the RPCs and provider calls
|
|
|
|
// happen in the expected order.
|
2020-11-30 22:37:24 +00:00
|
|
|
errCh := make(chan error)
|
2020-11-20 04:08:06 +00:00
|
|
|
go func() {
|
2020-11-30 22:37:24 +00:00
|
|
|
errCh <- manager.RenewIntermediate(context.TODO(), false)
|
2020-11-20 04:08:06 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
waitForCh(t, delegate.callbackCh, "provider/GenerateIntermediateCSR")
|
|
|
|
|
|
|
|
// Call UpdateConfiguration while RenewIntermediate is still in-flight to
|
|
|
|
// make sure we get an error about the state being occupied.
|
|
|
|
go func() {
|
2021-01-15 18:20:27 +00:00
|
|
|
require.EqualValues(t, caStateRenewIntermediate, manager.state)
|
2020-11-20 04:08:06 +00:00
|
|
|
require.Error(t, errors.New("already in state"), manager.UpdateConfiguration(&structs.CARequest{}))
|
|
|
|
}()
|
|
|
|
|
|
|
|
waitForCh(t, delegate.callbackCh, "forwardDC/ConnectCA.SignIntermediate")
|
|
|
|
waitForCh(t, delegate.callbackCh, "provider/SetIntermediate")
|
|
|
|
waitForCh(t, delegate.callbackCh, "raftApply/ConnectCA")
|
|
|
|
waitForEmptyCh(t, delegate.callbackCh)
|
2020-11-30 22:37:24 +00:00
|
|
|
|
|
|
|
// Make sure the RenewIntermediate call returned successfully.
|
|
|
|
select {
|
|
|
|
case err := <-errCh:
|
|
|
|
require.NoError(t, err)
|
|
|
|
case <-time.After(CATestTimeout):
|
|
|
|
t.Fatal("never got result from errCh")
|
|
|
|
}
|
|
|
|
|
2021-01-15 18:20:27 +00:00
|
|
|
require.EqualValues(t, caStateInitialized, manager.state)
|
2020-11-20 04:08:06 +00:00
|
|
|
}
|
2021-06-21 23:22:01 +00:00
|
|
|
|
2021-07-13 16:15:06 +00:00
|
|
|
func TestCAManager_SignLeafWithExpiredCert(t *testing.T) {
|
2021-08-09 20:29:21 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
2021-07-13 16:15:06 +00:00
|
|
|
|
|
|
|
args := []struct {
|
|
|
|
testName string
|
|
|
|
notBeforeRoot time.Time
|
|
|
|
notAfterRoot time.Time
|
|
|
|
notBeforeIntermediate time.Time
|
|
|
|
notAfterIntermediate time.Time
|
|
|
|
isError bool
|
|
|
|
errorMsg string
|
|
|
|
}{
|
|
|
|
{"intermediate valid", time.Now().AddDate(0, 0, -1), time.Now().AddDate(0, 0, 2), time.Now().AddDate(0, 0, -1), time.Now().AddDate(0, 0, 2), false, ""},
|
|
|
|
{"intermediate expired", time.Now().AddDate(0, 0, -1), time.Now().AddDate(0, 0, 2), time.Now().AddDate(-2, 0, 0), time.Now().AddDate(0, 0, -1), true, "intermediate expired: certificate expired, expiration date"},
|
|
|
|
{"root expired", time.Now().AddDate(-2, 0, 0), time.Now().AddDate(0, 0, -1), time.Now().AddDate(0, 0, -1), time.Now().AddDate(0, 0, 2), true, "root expired: certificate expired, expiration date"},
|
|
|
|
// a cert that is not yet valid is ok, assume it will be valid soon enough
|
|
|
|
{"intermediate in the future", time.Now().AddDate(0, 0, -1), time.Now().AddDate(0, 0, 2), time.Now().AddDate(0, 0, 1), time.Now().AddDate(0, 0, 2), false, ""},
|
|
|
|
{"root in the future", time.Now().AddDate(0, 0, 1), time.Now().AddDate(0, 0, 2), time.Now().AddDate(0, 0, -1), time.Now().AddDate(0, 0, 2), false, ""},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, arg := range args {
|
|
|
|
|
|
|
|
t.Run(arg.testName, func(t *testing.T) {
|
|
|
|
// No parallel execution because we change globals
|
|
|
|
// Set the interval and drift buffer low for renewing the cert.
|
|
|
|
origInterval := structs.IntermediateCertRenewInterval
|
|
|
|
origDriftBuffer := ca.CertificateTimeDriftBuffer
|
|
|
|
defer func() {
|
|
|
|
structs.IntermediateCertRenewInterval = origInterval
|
|
|
|
ca.CertificateTimeDriftBuffer = origDriftBuffer
|
|
|
|
}()
|
|
|
|
structs.IntermediateCertRenewInterval = time.Millisecond
|
|
|
|
ca.CertificateTimeDriftBuffer = 0
|
|
|
|
|
|
|
|
conf := DefaultConfig()
|
|
|
|
conf.ConnectEnabled = true
|
|
|
|
conf.PrimaryDatacenter = "dc1"
|
|
|
|
conf.Datacenter = "dc2"
|
|
|
|
delegate := NewMockCAServerDelegate(t, conf)
|
|
|
|
manager := NewCAManager(delegate, nil, testutil.Logger(t), conf)
|
|
|
|
|
|
|
|
err, rootPEM := generatePem(arg.notBeforeRoot, arg.notAfterRoot)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err, intermediatePEM := generatePem(arg.notBeforeIntermediate, arg.notAfterIntermediate)
|
|
|
|
require.NoError(t, err)
|
|
|
|
manager.providerShim = &mockCAProvider{
|
|
|
|
callbackCh: delegate.callbackCh,
|
|
|
|
rootPEM: rootPEM,
|
|
|
|
intermediatePem: intermediatePEM,
|
|
|
|
}
|
|
|
|
initTestManager(t, manager, delegate)
|
|
|
|
|
|
|
|
// Simulate Wait half the TTL for the cert to need renewing.
|
|
|
|
manager.timeNow = func() time.Time {
|
|
|
|
return time.Now().Add(500 * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call RenewIntermediate and then confirm the RPCs and provider calls
|
|
|
|
// happen in the expected order.
|
|
|
|
|
|
|
|
_, err = manager.SignCertificate(&x509.CertificateRequest{}, &connect.SpiffeIDAgent{})
|
|
|
|
|
|
|
|
if arg.isError {
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), arg.errorMsg)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func generatePem(notBefore time.Time, notAfter time.Time) (error, string) {
|
|
|
|
ca := &x509.Certificate{
|
|
|
|
SerialNumber: big.NewInt(2019),
|
|
|
|
Subject: pkix.Name{
|
|
|
|
Organization: []string{"Company, INC."},
|
|
|
|
Country: []string{"US"},
|
|
|
|
Province: []string{""},
|
|
|
|
Locality: []string{"San Francisco"},
|
|
|
|
StreetAddress: []string{"Golden Gate Bridge"},
|
|
|
|
PostalCode: []string{"94016"},
|
|
|
|
},
|
|
|
|
NotBefore: notBefore,
|
|
|
|
NotAfter: notAfter,
|
|
|
|
IsCA: true,
|
|
|
|
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
|
|
|
|
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
|
|
|
|
BasicConstraintsValid: true,
|
|
|
|
}
|
|
|
|
caPrivKey, err := rsa.GenerateKey(rand.Reader, 4096)
|
|
|
|
if err != nil {
|
|
|
|
return err, ""
|
|
|
|
}
|
|
|
|
caBytes, err := x509.CreateCertificate(rand.Reader, ca, ca, &caPrivKey.PublicKey, caPrivKey)
|
|
|
|
if err != nil {
|
|
|
|
return err, ""
|
|
|
|
}
|
|
|
|
caPEM := new(bytes.Buffer)
|
|
|
|
pem.Encode(caPEM, &pem.Block{
|
|
|
|
Type: "CERTIFICATE",
|
|
|
|
Bytes: caBytes,
|
|
|
|
})
|
|
|
|
|
|
|
|
caPrivKeyPEM := new(bytes.Buffer)
|
|
|
|
pem.Encode(caPrivKeyPEM, &pem.Block{
|
|
|
|
Type: "RSA PRIVATE KEY",
|
|
|
|
Bytes: x509.MarshalPKCS1PrivateKey(caPrivKey),
|
|
|
|
})
|
|
|
|
return err, caPEM.String()
|
|
|
|
}
|
|
|
|
|
2021-06-21 23:22:01 +00:00
|
|
|
func TestCADelegateWithState_GenerateCASignRequest(t *testing.T) {
|
|
|
|
s := Server{config: &Config{PrimaryDatacenter: "east"}, tokens: new(token.Store)}
|
|
|
|
d := &caDelegateWithState{Server: &s}
|
|
|
|
req := d.generateCASignRequest("A")
|
|
|
|
require.Equal(t, "east", req.RequestDatacenter())
|
|
|
|
}
|
2021-12-02 19:57:06 +00:00
|
|
|
|
|
|
|
func TestCAManager_Initialize_Logging(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Parallel()
|
|
|
|
_, conf1 := testServerConfig(t)
|
|
|
|
|
|
|
|
// Setup dummy logger to catch output
|
|
|
|
var buf bytes.Buffer
|
|
|
|
logger := testutil.LoggerWithOutput(t, &buf)
|
|
|
|
|
|
|
|
deps := newDefaultDeps(t, conf1)
|
|
|
|
deps.Logger = logger
|
|
|
|
|
|
|
|
s1, err := NewServer(conf1, deps)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
|
|
|
|
|
|
|
// Wait til CA root is setup
|
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
var out structs.IndexedCARoots
|
|
|
|
r.Check(s1.RPC("ConnectCA.Roots", structs.DCSpecificRequest{
|
|
|
|
Datacenter: conf1.Datacenter,
|
|
|
|
}, &out))
|
|
|
|
})
|
|
|
|
|
|
|
|
require.Contains(t, buf.String(), "consul CA provider configured")
|
|
|
|
}
|
2021-11-26 22:31:23 +00:00
|
|
|
|
|
|
|
func TestCAManager_UpdateConfiguration_Vault_Primary(t *testing.T) {
|
|
|
|
ca.SkipIfVaultNotPresent(t)
|
|
|
|
vault := ca.NewTestVaultServer(t)
|
|
|
|
|
|
|
|
_, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.PrimaryDatacenter = "dc1"
|
|
|
|
c.CAConfig = &structs.CAConfiguration{
|
|
|
|
Provider: "vault",
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"Address": vault.Addr,
|
|
|
|
"Token": vault.RootToken,
|
|
|
|
"RootPKIPath": "pki-root/",
|
|
|
|
"IntermediatePKIPath": "pki-intermediate/",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
})
|
|
|
|
defer func() {
|
|
|
|
s1.Shutdown()
|
|
|
|
s1.leaderRoutineManager.Wait()
|
|
|
|
}()
|
|
|
|
|
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
|
|
|
|
|
|
|
_, origRoot, err := s1.fsm.State().CARootActive(nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, origRoot.IntermediateCerts, 1)
|
|
|
|
|
|
|
|
cert, err := connect.ParseCert(s1.caManager.getLeafSigningCertFromRoot(origRoot))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, connect.HexString(cert.SubjectKeyId), origRoot.SigningKeyID)
|
|
|
|
|
|
|
|
err = s1.caManager.UpdateConfiguration(&structs.CARequest{
|
|
|
|
Config: &structs.CAConfiguration{
|
|
|
|
Provider: "vault",
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"Address": vault.Addr,
|
|
|
|
"Token": vault.RootToken,
|
|
|
|
"RootPKIPath": "pki-root-2/",
|
|
|
|
"IntermediatePKIPath": "pki-intermediate-2/",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, newRoot, err := s1.fsm.State().CARootActive(nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, newRoot.IntermediateCerts, 2,
|
|
|
|
"expected one cross-sign cert and one local leaf sign cert")
|
|
|
|
require.NotEqual(t, origRoot.ID, newRoot.ID)
|
|
|
|
|
|
|
|
cert, err = connect.ParseCert(s1.caManager.getLeafSigningCertFromRoot(newRoot))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, connect.HexString(cert.SubjectKeyId), newRoot.SigningKeyID)
|
|
|
|
}
|
2021-12-07 00:05:35 +00:00
|
|
|
|
|
|
|
func getLeafCert(t *testing.T, codec rpc.ClientCodec, trustDomain string, dc string) string {
|
|
|
|
pk, _, err := connect.GeneratePrivateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
spiffeID := &connect.SpiffeIDService{
|
|
|
|
Host: trustDomain,
|
|
|
|
Service: "srv1",
|
|
|
|
Datacenter: dc,
|
|
|
|
}
|
|
|
|
csr, err := connect.CreateCSR(spiffeID, pk, nil, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
req := structs.CASignRequest{CSR: csr}
|
|
|
|
cert := structs.IssuedCert{}
|
|
|
|
err = msgpackrpc.CallWithCodec(codec, "ConnectCA.Sign", &req, &cert)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return cert.CertPEM
|
|
|
|
}
|