2020-11-12 01:05:04 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-06-23 20:32:59 +00:00
|
|
|
"crypto/x509"
|
2020-11-13 22:33:19 +00:00
|
|
|
"errors"
|
2020-11-12 01:05:04 +00:00
|
|
|
"fmt"
|
2021-06-23 20:32:59 +00:00
|
|
|
"net/url"
|
2020-11-12 01:05:04 +00:00
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2021-06-23 19:47:30 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
|
|
|
uuid "github.com/hashicorp/go-uuid"
|
2021-06-23 20:32:59 +00:00
|
|
|
"golang.org/x/time/rate"
|
|
|
|
|
2022-04-05 18:16:20 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
2021-06-23 20:32:59 +00:00
|
|
|
"github.com/hashicorp/consul/lib/semaphore"
|
2021-06-23 19:47:30 +00:00
|
|
|
|
2020-11-12 01:05:04 +00:00
|
|
|
"github.com/hashicorp/consul/agent/connect"
|
|
|
|
"github.com/hashicorp/consul/agent/connect/ca"
|
2020-11-20 04:08:06 +00:00
|
|
|
"github.com/hashicorp/consul/agent/consul/state"
|
2020-11-12 01:05:04 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2022-06-01 21:53:52 +00:00
|
|
|
"github.com/hashicorp/consul/lib"
|
2021-05-20 14:07:23 +00:00
|
|
|
"github.com/hashicorp/consul/lib/routine"
|
2020-11-12 01:05:04 +00:00
|
|
|
)
|
|
|
|
|
2021-01-15 18:20:27 +00:00
|
|
|
type caState string
|
2020-11-12 01:05:04 +00:00
|
|
|
|
|
|
|
const (
|
2021-01-15 18:20:27 +00:00
|
|
|
caStateUninitialized caState = "UNINITIALIZED"
|
2021-06-22 21:25:38 +00:00
|
|
|
caStateInitializing caState = "INITIALIZING"
|
|
|
|
caStateInitialized caState = "INITIALIZED"
|
|
|
|
caStateRenewIntermediate caState = "RENEWING"
|
|
|
|
caStateReconfig caState = "RECONFIGURING"
|
2020-11-12 01:05:04 +00:00
|
|
|
)
|
|
|
|
|
2020-11-20 04:08:06 +00:00
|
|
|
// caServerDelegate is an interface for server operations for facilitating
|
|
|
|
// easier testing.
|
|
|
|
type caServerDelegate interface {
|
2021-06-21 21:51:37 +00:00
|
|
|
ca.ConsulProviderStateDelegate
|
2021-11-12 00:03:52 +00:00
|
|
|
|
|
|
|
State() *state.Store
|
2020-11-20 04:08:06 +00:00
|
|
|
IsLeader() bool
|
2021-06-23 20:30:59 +00:00
|
|
|
ApplyCALeafRequest() (uint64, error)
|
2020-11-20 04:08:06 +00:00
|
|
|
|
|
|
|
forwardDC(method, dc string, args interface{}, reply interface{}) error
|
|
|
|
generateCASignRequest(csr string) *structs.CASignRequest
|
|
|
|
|
2021-11-05 20:37:10 +00:00
|
|
|
ServersSupportMultiDCConnectCA() error
|
2020-11-20 04:08:06 +00:00
|
|
|
}
|
|
|
|
|
2020-11-12 01:05:04 +00:00
|
|
|
// CAManager is a wrapper around CA operations such as updating roots, an intermediate
|
|
|
|
// or the configuration. All operations should go through the CAManager in order to
|
|
|
|
// avoid data races.
|
|
|
|
type CAManager struct {
|
2020-11-20 04:08:06 +00:00
|
|
|
delegate caServerDelegate
|
|
|
|
serverConf *Config
|
|
|
|
logger hclog.Logger
|
2021-06-23 20:30:59 +00:00
|
|
|
// rate limiter to use when signing leaf certificates
|
|
|
|
caLeafLimiter connectSignRateLimiter
|
2020-11-12 01:05:04 +00:00
|
|
|
|
2020-11-13 22:33:19 +00:00
|
|
|
providerLock sync.RWMutex
|
2020-11-12 01:05:04 +00:00
|
|
|
// provider is the current CA provider in use for Connect. This is
|
|
|
|
// only non-nil when we are the leader.
|
|
|
|
provider ca.Provider
|
|
|
|
// providerRoot is the CARoot that was stored along with the ca.Provider
|
|
|
|
// active. It's only updated in lock-step with the provider. This prevents
|
|
|
|
// races between state updates to active roots and the fetch of the provider
|
|
|
|
// instance.
|
|
|
|
providerRoot *structs.CARoot
|
|
|
|
|
2020-11-13 22:33:19 +00:00
|
|
|
// stateLock protects the internal state used for administrative CA tasks.
|
2021-10-18 15:44:47 +00:00
|
|
|
stateLock sync.Mutex
|
|
|
|
state caState
|
|
|
|
primaryRoots structs.IndexedCARoots // The most recently seen state of the root CAs from the primary datacenter.
|
2021-01-15 18:20:27 +00:00
|
|
|
|
2021-05-20 14:07:23 +00:00
|
|
|
leaderRoutineManager *routine.Manager
|
2021-06-21 21:51:37 +00:00
|
|
|
// providerShim is used to test CAManager with a fake provider.
|
|
|
|
providerShim ca.Provider
|
2021-07-13 16:15:06 +00:00
|
|
|
|
|
|
|
// shim time.Now for testing
|
|
|
|
timeNow func() time.Time
|
2020-11-12 01:05:04 +00:00
|
|
|
}
|
|
|
|
|
2020-11-20 04:08:06 +00:00
|
|
|
type caDelegateWithState struct {
|
|
|
|
*Server
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *caDelegateWithState) State() *state.Store {
|
|
|
|
return c.fsm.State()
|
|
|
|
}
|
|
|
|
|
2021-06-21 21:51:37 +00:00
|
|
|
func (c *caDelegateWithState) ApplyCARequest(req *structs.CARequest) (interface{}, error) {
|
|
|
|
return c.Server.raftApplyMsgpack(structs.ConnectCARequestType, req)
|
|
|
|
}
|
|
|
|
|
2021-06-23 20:30:59 +00:00
|
|
|
func (c *caDelegateWithState) ApplyCALeafRequest() (uint64, error) {
|
|
|
|
// TODO(banks): when we implement IssuedCerts table we can use the insert to
|
|
|
|
// that as the raft index to return in response.
|
|
|
|
//
|
|
|
|
// UPDATE(mkeeler): The original implementation relied on updating the CAConfig
|
|
|
|
// and using its index as the ModifyIndex for certs. This was buggy. The long
|
|
|
|
// term goal is still to insert some metadata into raft about the certificates
|
|
|
|
// and use that raft index for the ModifyIndex. This is a partial step in that
|
|
|
|
// direction except that we only are setting an index and not storing the
|
|
|
|
// metadata.
|
|
|
|
req := structs.CALeafRequest{
|
|
|
|
Op: structs.CALeafOpIncrementIndex,
|
|
|
|
Datacenter: c.Server.config.Datacenter,
|
|
|
|
}
|
|
|
|
resp, err := c.Server.raftApplyMsgpack(structs.ConnectCALeafRequestType|structs.IgnoreUnknownTypeFlag, &req)
|
2021-07-12 14:37:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2021-06-23 20:30:59 +00:00
|
|
|
|
|
|
|
modIdx, ok := resp.(uint64)
|
|
|
|
if !ok {
|
|
|
|
return 0, fmt.Errorf("Invalid response from updating the leaf cert index")
|
|
|
|
}
|
|
|
|
return modIdx, err
|
|
|
|
}
|
|
|
|
|
2021-06-21 23:22:01 +00:00
|
|
|
func (c *caDelegateWithState) generateCASignRequest(csr string) *structs.CASignRequest {
|
|
|
|
return &structs.CASignRequest{
|
|
|
|
Datacenter: c.Server.config.PrimaryDatacenter,
|
|
|
|
CSR: csr,
|
|
|
|
WriteRequest: structs.WriteRequest{Token: c.Server.tokens.ReplicationToken()},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-05 20:37:10 +00:00
|
|
|
func (c *caDelegateWithState) ServersSupportMultiDCConnectCA() error {
|
|
|
|
versionOk, primaryFound := ServersInDCMeetMinimumVersion(c.Server, c.Server.config.PrimaryDatacenter, minMultiDCConnectVersion)
|
|
|
|
if !primaryFound {
|
|
|
|
return fmt.Errorf("primary datacenter is unreachable")
|
|
|
|
}
|
|
|
|
if !versionOk {
|
|
|
|
return fmt.Errorf("all servers in the primary datacenter are not at the minimum version %v", minMultiDCConnectVersion)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-12 00:03:52 +00:00
|
|
|
func (c *caDelegateWithState) ProviderState(id string) (*structs.CAConsulProviderState, error) {
|
|
|
|
_, s, err := c.fsm.State().CAProviderState(id)
|
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
|
2021-05-20 14:07:23 +00:00
|
|
|
func NewCAManager(delegate caServerDelegate, leaderRoutineManager *routine.Manager, logger hclog.Logger, config *Config) *CAManager {
|
2020-11-12 01:05:04 +00:00
|
|
|
return &CAManager{
|
2021-01-15 18:20:27 +00:00
|
|
|
delegate: delegate,
|
|
|
|
logger: logger,
|
|
|
|
serverConf: config,
|
|
|
|
state: caStateUninitialized,
|
|
|
|
leaderRoutineManager: leaderRoutineManager,
|
2021-07-13 16:15:06 +00:00
|
|
|
timeNow: time.Now,
|
2020-11-12 01:05:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// setState attempts to update the CA state to the given state.
|
2021-01-15 18:20:27 +00:00
|
|
|
// Valid state transitions are:
|
|
|
|
//
|
2021-08-05 18:51:19 +00:00
|
|
|
// caStateInitialized -> <any state except caStateInitializing>
|
2021-01-15 18:20:27 +00:00
|
|
|
// caStateUninitialized -> caStateInitializing
|
|
|
|
// caStateUninitialized -> caStateReconfig
|
|
|
|
//
|
|
|
|
// Other state transitions may be forced if the validateState parameter is set to false.
|
|
|
|
// This will mainly be used in deferred functions which aim to set the final status based
|
|
|
|
// a successful/error return.
|
|
|
|
func (c *CAManager) setState(newState caState, validateState bool) (caState, error) {
|
2020-11-13 22:33:19 +00:00
|
|
|
c.stateLock.Lock()
|
|
|
|
defer c.stateLock.Unlock()
|
2020-11-12 01:05:04 +00:00
|
|
|
state := c.state
|
|
|
|
|
2021-01-15 18:20:27 +00:00
|
|
|
if !validateState ||
|
2021-08-05 18:51:19 +00:00
|
|
|
(state == caStateInitialized && newState != caStateInitializing) ||
|
2021-01-15 18:20:27 +00:00
|
|
|
(state == caStateUninitialized && newState == caStateInitializing) ||
|
|
|
|
(state == caStateUninitialized && newState == caStateReconfig) {
|
2020-11-12 01:05:04 +00:00
|
|
|
c.state = newState
|
|
|
|
} else {
|
2021-08-05 18:51:19 +00:00
|
|
|
return state, &caStateError{Current: state}
|
2020-11-12 01:05:04 +00:00
|
|
|
}
|
2021-01-15 18:20:27 +00:00
|
|
|
return state, nil
|
2020-11-12 01:05:04 +00:00
|
|
|
}
|
|
|
|
|
2021-08-05 18:51:19 +00:00
|
|
|
type caStateError struct {
|
|
|
|
Current caState
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *caStateError) Error() string {
|
|
|
|
return fmt.Sprintf("CA is already in state %q", e.Current)
|
|
|
|
}
|
|
|
|
|
2021-10-10 18:50:45 +00:00
|
|
|
// secondarySetPrimaryRoots updates the most recently seen roots from the primary.
|
2021-10-11 01:15:13 +00:00
|
|
|
func (c *CAManager) secondarySetPrimaryRoots(newRoots structs.IndexedCARoots) {
|
|
|
|
// TODO: this could be a different lock, as long as its the same lock in secondaryGetPrimaryRoots
|
2020-11-12 01:05:04 +00:00
|
|
|
c.stateLock.Lock()
|
2020-11-13 22:33:19 +00:00
|
|
|
defer c.stateLock.Unlock()
|
2021-10-11 01:15:13 +00:00
|
|
|
c.primaryRoots = newRoots
|
2020-11-13 22:33:19 +00:00
|
|
|
}
|
|
|
|
|
2021-11-25 21:15:09 +00:00
|
|
|
func (c *CAManager) secondaryGetActivePrimaryCARoot() (*structs.CARoot, error) {
|
2021-10-11 01:15:13 +00:00
|
|
|
// TODO: this could be a different lock, as long as its the same lock in secondarySetPrimaryRoots
|
2020-11-13 22:33:19 +00:00
|
|
|
c.stateLock.Lock()
|
2021-11-25 21:15:09 +00:00
|
|
|
primaryRoots := c.primaryRoots
|
|
|
|
c.stateLock.Unlock()
|
|
|
|
|
|
|
|
for _, root := range primaryRoots.Roots {
|
|
|
|
if root.ID == primaryRoots.ActiveRootID && root.Active {
|
|
|
|
return root, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("primary datacenter does not have an active root CA for Connect")
|
2020-11-12 01:05:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// initializeCAConfig is used to initialize the CA config if necessary
|
2020-11-17 23:35:33 +00:00
|
|
|
// when setting up the CA during establishLeadership. The state should be set to
|
|
|
|
// non-ready before calling this.
|
2020-11-12 01:05:04 +00:00
|
|
|
func (c *CAManager) initializeCAConfig() (*structs.CAConfiguration, error) {
|
2021-06-23 20:32:59 +00:00
|
|
|
st := c.delegate.State()
|
|
|
|
_, config, err := st.CAConfig(nil)
|
2020-11-12 01:05:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if config == nil {
|
2020-11-20 04:08:06 +00:00
|
|
|
config = c.serverConf.CAConfig
|
2021-11-05 22:08:44 +00:00
|
|
|
|
|
|
|
if c.serverConf.Datacenter == c.serverConf.PrimaryDatacenter && config.ClusterID == "" {
|
2020-11-12 01:05:04 +00:00
|
|
|
id, err := uuid.GenerateUUID()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
config.ClusterID = id
|
|
|
|
}
|
|
|
|
} else if _, ok := config.Config["IntermediateCertTTL"]; !ok {
|
|
|
|
dup := *config
|
|
|
|
copied := make(map[string]interface{})
|
|
|
|
for k, v := range dup.Config {
|
|
|
|
copied[k] = v
|
|
|
|
}
|
|
|
|
copied["IntermediateCertTTL"] = connect.DefaultIntermediateCertTTL.String()
|
|
|
|
dup.Config = copied
|
|
|
|
config = &dup
|
|
|
|
} else {
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
req := structs.CARequest{
|
|
|
|
Op: structs.CAOpSetConfig,
|
|
|
|
Config: config,
|
|
|
|
}
|
2021-11-26 22:57:55 +00:00
|
|
|
if _, err := c.delegate.ApplyCARequest(&req); err != nil {
|
2020-11-12 01:05:04 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
2022-01-05 23:21:04 +00:00
|
|
|
// newCARoot returns a filled-in structs.CARoot from a raw PEM value.
|
|
|
|
func newCARoot(pemValue, provider, clusterID string) (*structs.CARoot, error) {
|
|
|
|
primaryCert, err := connect.ParseCert(pemValue)
|
2020-11-12 01:05:04 +00:00
|
|
|
if err != nil {
|
2022-01-05 23:21:04 +00:00
|
|
|
return nil, err
|
2020-11-12 01:05:04 +00:00
|
|
|
}
|
2022-01-05 23:21:04 +00:00
|
|
|
keyType, keyBits, err := connect.KeyInfoFromCert(primaryCert)
|
2020-11-12 01:05:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error extracting root key info: %v", err)
|
|
|
|
}
|
|
|
|
return &structs.CARoot{
|
2022-01-05 23:21:04 +00:00
|
|
|
ID: connect.CalculateCertFingerprint(primaryCert.Raw),
|
2022-04-14 20:55:10 +00:00
|
|
|
Name: fmt.Sprintf("%s CA Primary Cert", providerPrettyName(provider)),
|
2022-01-05 23:21:04 +00:00
|
|
|
SerialNumber: primaryCert.SerialNumber.Uint64(),
|
|
|
|
SigningKeyID: connect.EncodeSigningKeyID(primaryCert.SubjectKeyId),
|
2020-11-12 01:05:04 +00:00
|
|
|
ExternalTrustDomain: clusterID,
|
2022-01-05 23:21:04 +00:00
|
|
|
NotBefore: primaryCert.NotBefore,
|
|
|
|
NotAfter: primaryCert.NotAfter,
|
2020-11-12 01:05:04 +00:00
|
|
|
RootCert: pemValue,
|
|
|
|
PrivateKeyType: keyType,
|
|
|
|
PrivateKeyBits: keyBits,
|
|
|
|
Active: true,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-11-17 23:35:33 +00:00
|
|
|
// getCAProvider returns the currently active instance of the CA Provider,
|
|
|
|
// as well as the active root.
|
2020-11-12 01:05:04 +00:00
|
|
|
func (c *CAManager) getCAProvider() (ca.Provider, *structs.CARoot) {
|
|
|
|
retries := 0
|
2021-06-21 23:35:38 +00:00
|
|
|
for {
|
2020-11-12 01:05:04 +00:00
|
|
|
c.providerLock.RLock()
|
2021-06-21 23:35:38 +00:00
|
|
|
result := c.provider
|
|
|
|
resultRoot := c.providerRoot
|
2020-11-12 01:05:04 +00:00
|
|
|
c.providerLock.RUnlock()
|
|
|
|
|
|
|
|
// In cases where an agent is started with managed proxies, we may ask
|
|
|
|
// for the provider before establishLeadership completes. If we're the
|
|
|
|
// leader, then wait and get the provider again
|
2020-11-20 04:08:06 +00:00
|
|
|
if result == nil && c.delegate.IsLeader() && retries < 10 {
|
2020-11-12 01:05:04 +00:00
|
|
|
retries++
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-06-21 23:35:38 +00:00
|
|
|
return result, resultRoot
|
2020-11-12 01:05:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// setCAProvider is being called while holding the stateLock
|
|
|
|
// which means it must never take that lock itself or call anything that does.
|
|
|
|
func (c *CAManager) setCAProvider(newProvider ca.Provider, root *structs.CARoot) {
|
|
|
|
c.providerLock.Lock()
|
|
|
|
c.provider = newProvider
|
|
|
|
c.providerRoot = root
|
|
|
|
c.providerLock.Unlock()
|
|
|
|
}
|
|
|
|
|
2021-05-20 14:07:23 +00:00
|
|
|
func (c *CAManager) Start(ctx context.Context) {
|
2021-01-15 18:20:27 +00:00
|
|
|
// Attempt to initialize the Connect CA now. This will
|
|
|
|
// happen during leader establishment and it would be great
|
|
|
|
// if the CA was ready to go once that process was finished.
|
2021-12-02 19:57:06 +00:00
|
|
|
if err := c.Initialize(); err != nil {
|
2021-01-15 18:20:27 +00:00
|
|
|
c.logger.Error("Failed to initialize Connect CA", "error", err)
|
|
|
|
|
|
|
|
// we failed to fully initialize the CA so we need to spawn a
|
|
|
|
// go routine to retry this process until it succeeds or we lose
|
|
|
|
// leadership and the go routine gets stopped.
|
2021-05-20 14:07:23 +00:00
|
|
|
c.leaderRoutineManager.Start(ctx, backgroundCAInitializationRoutineName, c.backgroundCAInitialization)
|
2021-01-15 18:20:27 +00:00
|
|
|
} else {
|
|
|
|
// We only start these if CA initialization was successful. If not the completion of the
|
|
|
|
// background CA initialization will start these routines.
|
2021-05-20 14:07:23 +00:00
|
|
|
c.startPostInitializeRoutines(ctx)
|
2021-01-15 18:20:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CAManager) Stop() {
|
|
|
|
c.leaderRoutineManager.Stop(secondaryCARootWatchRoutineName)
|
|
|
|
c.leaderRoutineManager.Stop(intermediateCertRenewWatchRoutineName)
|
|
|
|
c.leaderRoutineManager.Stop(backgroundCAInitializationRoutineName)
|
2021-06-21 21:20:54 +00:00
|
|
|
|
|
|
|
if provider, _ := c.getCAProvider(); provider != nil {
|
|
|
|
if needsStop, ok := provider.(ca.NeedsStop); ok {
|
|
|
|
needsStop.Stop()
|
|
|
|
}
|
|
|
|
}
|
2021-07-12 13:25:35 +00:00
|
|
|
|
2021-06-21 21:20:54 +00:00
|
|
|
c.setState(caStateUninitialized, false)
|
2021-07-12 13:25:35 +00:00
|
|
|
c.primaryRoots = structs.IndexedCARoots{}
|
|
|
|
c.setCAProvider(nil, nil)
|
2021-01-15 18:20:27 +00:00
|
|
|
}
|
|
|
|
|
2021-05-20 14:07:23 +00:00
|
|
|
func (c *CAManager) startPostInitializeRoutines(ctx context.Context) {
|
2021-01-15 18:20:27 +00:00
|
|
|
// Start the Connect secondary DC actions if enabled.
|
|
|
|
if c.serverConf.Datacenter != c.serverConf.PrimaryDatacenter {
|
2021-05-20 14:07:23 +00:00
|
|
|
c.leaderRoutineManager.Start(ctx, secondaryCARootWatchRoutineName, c.secondaryCARootWatch)
|
2021-01-15 18:20:27 +00:00
|
|
|
}
|
|
|
|
|
2021-12-08 17:21:52 +00:00
|
|
|
c.leaderRoutineManager.Start(ctx, intermediateCertRenewWatchRoutineName, c.runRenewIntermediate)
|
2021-01-15 18:20:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CAManager) backgroundCAInitialization(ctx context.Context) error {
|
2021-12-02 19:57:06 +00:00
|
|
|
retryLoopBackoffAbortOnSuccess(ctx, c.Initialize, func(err error) {
|
2021-01-15 18:20:27 +00:00
|
|
|
c.logger.Error("Failed to initialize Connect CA",
|
|
|
|
"routine", backgroundCAInitializationRoutineName,
|
|
|
|
"error", err,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
if err := ctx.Err(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.logger.Info("Successfully initialized the Connect CA")
|
|
|
|
|
2021-05-20 14:07:23 +00:00
|
|
|
c.startPostInitializeRoutines(ctx)
|
2021-01-15 18:20:27 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-02 19:57:06 +00:00
|
|
|
// Initialize sets up the CA provider when gaining leadership, either bootstrapping
|
2020-11-12 01:05:04 +00:00
|
|
|
// the CA if this is the primary DC or making a remote RPC for intermediate signing
|
|
|
|
// if this is a secondary DC.
|
2021-12-02 19:57:06 +00:00
|
|
|
func (c *CAManager) Initialize() (reterr error) {
|
2020-11-12 01:05:04 +00:00
|
|
|
// Bail if connect isn't enabled.
|
2020-11-20 04:08:06 +00:00
|
|
|
if !c.serverConf.ConnectEnabled {
|
2020-11-12 01:05:04 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-13 22:33:19 +00:00
|
|
|
// Update the state before doing anything else.
|
2021-08-05 18:51:19 +00:00
|
|
|
_, err := c.setState(caStateInitializing, true)
|
|
|
|
var errCaState *caStateError
|
|
|
|
switch {
|
|
|
|
case errors.As(err, &errCaState) && errCaState.Current == caStateInitialized:
|
2021-01-15 18:20:27 +00:00
|
|
|
return nil
|
2021-08-05 18:51:19 +00:00
|
|
|
case err != nil:
|
2020-11-12 01:05:04 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-01-15 18:20:27 +00:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
// Using named return values in deferred funcs isnt too common in our code
|
|
|
|
// but it is first class Go functionality. The error erturned from the
|
|
|
|
// main func will be available by its given name within deferred functions.
|
|
|
|
// See: https://blog.golang.org/defer-panic-and-recover
|
|
|
|
if reterr == nil {
|
|
|
|
c.setState(caStateInitialized, false)
|
|
|
|
} else {
|
|
|
|
c.setState(caStateUninitialized, false)
|
|
|
|
}
|
|
|
|
}()
|
2020-11-12 01:05:04 +00:00
|
|
|
|
|
|
|
// Initialize the provider based on the current config.
|
|
|
|
conf, err := c.initializeCAConfig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-06-21 21:51:37 +00:00
|
|
|
provider, err := c.newProvider(conf)
|
2020-11-12 01:05:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.setCAProvider(provider, nil)
|
|
|
|
|
2020-11-20 04:08:06 +00:00
|
|
|
if c.serverConf.PrimaryDatacenter == c.serverConf.Datacenter {
|
2021-10-10 18:50:45 +00:00
|
|
|
return c.primaryInitialize(provider, conf)
|
2020-11-17 23:35:33 +00:00
|
|
|
}
|
2021-10-10 18:50:45 +00:00
|
|
|
return c.secondaryInitialize(provider, conf)
|
|
|
|
}
|
2020-11-12 01:05:04 +00:00
|
|
|
|
2021-10-10 18:50:45 +00:00
|
|
|
func (c *CAManager) secondaryInitialize(provider ca.Provider, conf *structs.CAConfiguration) error {
|
2021-11-05 20:37:10 +00:00
|
|
|
if err := c.delegate.ServersSupportMultiDCConnectCA(); err != nil {
|
|
|
|
return fmt.Errorf("initialization will be deferred: %w", err)
|
2020-11-17 23:35:33 +00:00
|
|
|
}
|
2020-11-12 01:05:04 +00:00
|
|
|
|
2020-11-17 23:35:33 +00:00
|
|
|
// Get the root CA to see if we need to refresh our intermediate.
|
|
|
|
args := structs.DCSpecificRequest{
|
2020-11-20 04:08:06 +00:00
|
|
|
Datacenter: c.serverConf.PrimaryDatacenter,
|
2020-11-17 23:35:33 +00:00
|
|
|
}
|
|
|
|
var roots structs.IndexedCARoots
|
2020-11-20 04:08:06 +00:00
|
|
|
if err := c.delegate.forwardDC("ConnectCA.Roots", c.serverConf.PrimaryDatacenter, &args, &roots); err != nil {
|
2022-01-05 23:21:04 +00:00
|
|
|
return fmt.Errorf("failed to get CA roots from primary DC: %w", err)
|
2020-11-17 23:35:33 +00:00
|
|
|
}
|
2021-10-11 01:15:13 +00:00
|
|
|
c.secondarySetPrimaryRoots(roots)
|
2020-11-12 01:05:04 +00:00
|
|
|
|
2020-11-17 23:35:33 +00:00
|
|
|
// Configure the CA provider and initialize the intermediate certificate if necessary.
|
2021-10-10 18:50:45 +00:00
|
|
|
if err := c.secondaryInitializeProvider(provider, roots); err != nil {
|
2020-11-17 23:35:33 +00:00
|
|
|
return fmt.Errorf("error configuring provider: %v", err)
|
|
|
|
}
|
2021-10-10 18:50:45 +00:00
|
|
|
if err := c.secondaryInitializeIntermediateCA(provider, nil); err != nil {
|
2020-11-17 23:35:33 +00:00
|
|
|
return err
|
2020-11-12 01:05:04 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 23:35:33 +00:00
|
|
|
c.logger.Info("initialized secondary datacenter CA with provider", "provider", conf.Provider)
|
|
|
|
return nil
|
2020-11-12 01:05:04 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 21:51:37 +00:00
|
|
|
// createProvider returns a connect CA provider from the given config.
|
|
|
|
func (c *CAManager) newProvider(conf *structs.CAConfiguration) (ca.Provider, error) {
|
|
|
|
logger := c.logger.Named(conf.Provider)
|
|
|
|
switch conf.Provider {
|
|
|
|
case structs.ConsulCAProvider:
|
2021-07-12 18:04:34 +00:00
|
|
|
return ca.NewConsulProvider(c.delegate, logger), nil
|
2021-06-21 21:51:37 +00:00
|
|
|
case structs.VaultCAProvider:
|
|
|
|
return ca.NewVaultProvider(logger), nil
|
|
|
|
case structs.AWSCAProvider:
|
|
|
|
return ca.NewAWSProvider(logger), nil
|
|
|
|
default:
|
|
|
|
if c.providerShim != nil {
|
|
|
|
return c.providerShim, nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("unknown CA provider %q", conf.Provider)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-10 18:50:45 +00:00
|
|
|
// primaryInitialize runs the initialization logic for a root CA. It should only
|
2020-11-17 23:35:33 +00:00
|
|
|
// be called while the state lock is held by setting the state to non-ready.
|
2021-10-10 18:50:45 +00:00
|
|
|
func (c *CAManager) primaryInitialize(provider ca.Provider, conf *structs.CAConfiguration) error {
|
2020-11-12 01:05:04 +00:00
|
|
|
pCfg := ca.ProviderConfig{
|
|
|
|
ClusterID: conf.ClusterID,
|
2020-11-20 04:08:06 +00:00
|
|
|
Datacenter: c.serverConf.Datacenter,
|
2020-11-12 01:05:04 +00:00
|
|
|
IsPrimary: true,
|
|
|
|
RawConfig: conf.Config,
|
|
|
|
State: conf.State,
|
|
|
|
}
|
|
|
|
if err := provider.Configure(pCfg); err != nil {
|
|
|
|
return fmt.Errorf("error configuring provider: %v", err)
|
|
|
|
}
|
2021-12-01 20:39:20 +00:00
|
|
|
root, err := provider.GenerateRoot()
|
|
|
|
if err != nil {
|
2020-11-12 01:05:04 +00:00
|
|
|
return fmt.Errorf("error generating CA root certificate: %v", err)
|
|
|
|
}
|
|
|
|
|
2022-01-05 23:21:04 +00:00
|
|
|
rootCA, err := newCARoot(root.PEM, conf.Provider, conf.ClusterID)
|
2020-11-12 01:05:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-02-17 22:44:02 +00:00
|
|
|
// TODO: https://github.com/hashicorp/consul/issues/12386
|
2020-11-12 01:05:04 +00:00
|
|
|
interPEM, err := provider.GenerateIntermediate()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error generating intermediate cert: %v", err)
|
|
|
|
}
|
2021-02-08 19:18:51 +00:00
|
|
|
intermediateCert, err := connect.ParseCert(interPEM)
|
2020-11-12 01:05:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error getting intermediate cert: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the provider has state to persist and it's changed or new then update
|
|
|
|
// CAConfig.
|
|
|
|
pState, err := provider.State()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error getting provider state: %v", err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(conf.State, pState) {
|
|
|
|
// Update the CAConfig in raft to persist the provider state
|
|
|
|
conf.State = pState
|
|
|
|
req := structs.CARequest{
|
|
|
|
Op: structs.CAOpSetConfig,
|
|
|
|
Config: conf,
|
|
|
|
}
|
2021-06-21 23:27:30 +00:00
|
|
|
if _, err = c.delegate.ApplyCARequest(&req); err != nil {
|
2020-11-12 01:05:04 +00:00
|
|
|
return fmt.Errorf("error persisting provider state: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-26 00:12:49 +00:00
|
|
|
var rootUpdateRequired bool
|
|
|
|
|
2021-02-08 19:18:51 +00:00
|
|
|
// Versions prior to 1.9.3, 1.8.8, and 1.7.12 incorrectly used the primary
|
|
|
|
// rootCA's subjectKeyID here instead of the intermediate. For
|
|
|
|
// provider=consul this didn't matter since there are no intermediates in
|
|
|
|
// the primaryDC, but for vault it does matter.
|
|
|
|
expectedSigningKeyID := connect.EncodeSigningKeyID(intermediateCert.SubjectKeyId)
|
2021-11-26 00:12:49 +00:00
|
|
|
if rootCA.SigningKeyID != expectedSigningKeyID {
|
|
|
|
c.logger.Info("Correcting stored CARoot values",
|
|
|
|
"previous-signing-key", rootCA.SigningKeyID, "updated-signing-key", expectedSigningKeyID)
|
|
|
|
rootCA.SigningKeyID = expectedSigningKeyID
|
|
|
|
rootUpdateRequired = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the local leaf signing cert to the rootCA struct. This handles both
|
|
|
|
// upgrades of existing state, and new rootCA.
|
2021-12-01 00:26:36 +00:00
|
|
|
if c.getLeafSigningCertFromRoot(rootCA) != interPEM {
|
2021-11-26 00:12:49 +00:00
|
|
|
rootCA.IntermediateCerts = append(rootCA.IntermediateCerts, interPEM)
|
|
|
|
rootUpdateRequired = true
|
|
|
|
}
|
2021-02-08 19:18:51 +00:00
|
|
|
|
2020-11-12 01:05:04 +00:00
|
|
|
// Check if the CA root is already initialized and exit if it is,
|
|
|
|
// adding on any existing intermediate certs since they aren't directly
|
|
|
|
// tied to the provider.
|
|
|
|
// Every change to the CA after this initial bootstrapping should
|
|
|
|
// be done through the rotation process.
|
2020-11-20 04:08:06 +00:00
|
|
|
state := c.delegate.State()
|
2020-11-12 01:05:04 +00:00
|
|
|
_, activeRoot, err := state.CARootActive(nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-11-26 00:12:49 +00:00
|
|
|
if activeRoot != nil && !rootUpdateRequired {
|
2020-11-12 01:05:04 +00:00
|
|
|
// This state shouldn't be possible to get into because we update the root and
|
|
|
|
// CA config in the same FSM operation.
|
|
|
|
if activeRoot.ID != rootCA.ID {
|
|
|
|
return fmt.Errorf("stored CA root %q is not the active root (%s)", rootCA.ID, activeRoot.ID)
|
|
|
|
}
|
|
|
|
|
2021-11-26 00:12:49 +00:00
|
|
|
// TODO: why doesn't this c.setCAProvider(provider, activeRoot) ?
|
2020-11-12 01:05:04 +00:00
|
|
|
rootCA.IntermediateCerts = activeRoot.IntermediateCerts
|
|
|
|
c.setCAProvider(provider, rootCA)
|
|
|
|
|
2021-11-26 00:12:49 +00:00
|
|
|
c.logger.Info("initialized primary datacenter CA from existing CARoot with provider", "provider", conf.Provider)
|
2020-11-12 01:05:04 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the highest index
|
|
|
|
idx, _, err := state.CARoots(nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the root cert in raft
|
2021-11-26 22:57:55 +00:00
|
|
|
_, err = c.delegate.ApplyCARequest(&structs.CARequest{
|
2020-11-12 01:05:04 +00:00
|
|
|
Op: structs.CAOpSetRoots,
|
|
|
|
Index: idx,
|
|
|
|
Roots: []*structs.CARoot{rootCA},
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-11-26 22:57:55 +00:00
|
|
|
return fmt.Errorf("raft apply failed: %w", err)
|
2020-11-12 01:05:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c.setCAProvider(provider, rootCA)
|
|
|
|
|
|
|
|
c.logger.Info("initialized primary datacenter CA with provider", "provider", conf.Provider)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-01 00:26:36 +00:00
|
|
|
// getLeafSigningCertFromRoot returns the PEM encoded certificate that should be used to
|
|
|
|
// sign leaf certificates in the local datacenter. The SubjectKeyId of the
|
|
|
|
// returned cert should always match the SigningKeyID of the CARoot.
|
|
|
|
//
|
|
|
|
// TODO: fix the data model so that we don't need this complicated lookup to
|
|
|
|
// find the leaf signing cert. See github.com/hashicorp/consul/issues/11347.
|
|
|
|
func (c *CAManager) getLeafSigningCertFromRoot(root *structs.CARoot) string {
|
|
|
|
if !c.isIntermediateUsedToSignLeaf() {
|
|
|
|
return root.RootCert
|
|
|
|
}
|
|
|
|
if len(root.IntermediateCerts) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return root.IntermediateCerts[len(root.IntermediateCerts)-1]
|
|
|
|
}
|
|
|
|
|
2021-11-25 18:24:35 +00:00
|
|
|
// secondaryInitializeIntermediateCA generates a Certificate Signing Request (CSR)
|
|
|
|
// for the intermediate CA that is used to sign leaf certificates in the secondary.
|
|
|
|
// The CSR is signed by the primary DC and then persisted in the state store.
|
|
|
|
//
|
|
|
|
// This method should only be called while the state lock is held by setting the
|
|
|
|
// state to non-ready.
|
2021-10-10 18:50:45 +00:00
|
|
|
func (c *CAManager) secondaryInitializeIntermediateCA(provider ca.Provider, config *structs.CAConfiguration) error {
|
2020-11-12 01:05:04 +00:00
|
|
|
activeIntermediate, err := provider.ActiveIntermediate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-25 21:33:48 +00:00
|
|
|
_, activeRoot, err := c.delegate.State().CARootActive(nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var currentSigningKeyID string
|
|
|
|
if activeRoot != nil {
|
|
|
|
currentSigningKeyID = activeRoot.SigningKeyID
|
|
|
|
}
|
2020-11-12 01:05:04 +00:00
|
|
|
|
2021-11-25 21:33:48 +00:00
|
|
|
var expectedSigningKeyID string
|
|
|
|
if activeIntermediate != "" {
|
2020-11-12 01:05:04 +00:00
|
|
|
intermediateCert, err := connect.ParseCert(activeIntermediate)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error parsing active intermediate cert: %v", err)
|
|
|
|
}
|
|
|
|
expectedSigningKeyID = connect.EncodeSigningKeyID(intermediateCert.SubjectKeyId)
|
|
|
|
}
|
|
|
|
|
2021-11-25 21:15:09 +00:00
|
|
|
newActiveRoot, err := c.secondaryGetActivePrimaryCARoot()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-11-12 01:05:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get a signed intermediate from the primary DC if the provider
|
|
|
|
// hasn't been initialized yet or if the primary's root has changed.
|
2021-11-25 21:33:48 +00:00
|
|
|
needsNewIntermediate := activeIntermediate == ""
|
|
|
|
if activeRoot != nil && newActiveRoot.ID != activeRoot.ID {
|
2020-11-12 01:05:04 +00:00
|
|
|
needsNewIntermediate = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Also we take this opportunity to correct an incorrectly persisted SigningKeyID
|
|
|
|
// in secondary datacenters (see PR-6513).
|
|
|
|
if expectedSigningKeyID != "" && currentSigningKeyID != expectedSigningKeyID {
|
|
|
|
needsNewIntermediate = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if needsNewIntermediate {
|
2021-11-12 00:16:53 +00:00
|
|
|
if err := c.secondaryRequestNewSigningCert(provider, newActiveRoot); err != nil {
|
2020-11-12 01:05:04 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Discard the primary's representation since our local one is
|
|
|
|
// sufficiently up to date.
|
2021-11-25 21:33:48 +00:00
|
|
|
newActiveRoot = activeRoot
|
2020-11-12 01:05:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Determine whether a root update is needed, and persist the roots/config accordingly.
|
|
|
|
var newRoot *structs.CARoot
|
2021-11-25 23:09:23 +00:00
|
|
|
if activeRoot == nil || needsNewIntermediate {
|
2020-11-12 01:05:04 +00:00
|
|
|
newRoot = newActiveRoot
|
|
|
|
}
|
|
|
|
if err := c.persistNewRootAndConfig(provider, newRoot, config); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.setCAProvider(provider, newActiveRoot)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-17 23:35:33 +00:00
|
|
|
// persistNewRootAndConfig should only be called while the state lock is held
|
|
|
|
// by setting the state to non-ready.
|
2020-11-12 01:05:04 +00:00
|
|
|
// If newActiveRoot is non-nil, it will be appended to the current roots list.
|
|
|
|
// If config is non-nil, it will be used to overwrite the existing config.
|
|
|
|
func (c *CAManager) persistNewRootAndConfig(provider ca.Provider, newActiveRoot *structs.CARoot, config *structs.CAConfiguration) error {
|
2020-11-20 04:08:06 +00:00
|
|
|
state := c.delegate.State()
|
2020-11-12 01:05:04 +00:00
|
|
|
idx, oldRoots, err := state.CARoots(nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-20 23:54:44 +00:00
|
|
|
// Look up the existing CA config if a new one wasn't provided.
|
2020-11-12 01:05:04 +00:00
|
|
|
var newConf structs.CAConfiguration
|
|
|
|
_, storedConfig, err := state.CAConfig(nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if storedConfig == nil {
|
|
|
|
return fmt.Errorf("local CA not initialized yet")
|
|
|
|
}
|
2020-11-20 23:54:44 +00:00
|
|
|
// Exit early if the change is a no-op.
|
2022-02-09 16:46:42 +00:00
|
|
|
if !shouldPersistNewRootAndConfig(newActiveRoot, storedConfig, config) {
|
2020-11-20 23:54:44 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-12 01:05:04 +00:00
|
|
|
if config != nil {
|
|
|
|
newConf = *config
|
|
|
|
} else {
|
|
|
|
newConf = *storedConfig
|
|
|
|
}
|
2020-11-20 23:54:44 +00:00
|
|
|
|
|
|
|
// Update the trust domain for the config if there's a new root, or keep the old
|
|
|
|
// one if the root isn't being updated.
|
2020-11-12 01:05:04 +00:00
|
|
|
newConf.ModifyIndex = storedConfig.ModifyIndex
|
|
|
|
if newActiveRoot != nil {
|
|
|
|
newConf.ClusterID = newActiveRoot.ExternalTrustDomain
|
2020-11-20 23:54:44 +00:00
|
|
|
} else {
|
|
|
|
_, activeRoot, err := state.CARootActive(nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
newConf.ClusterID = activeRoot.ExternalTrustDomain
|
2020-11-12 01:05:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Persist any state the provider needs us to
|
|
|
|
newConf.State, err = provider.State()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error getting provider state: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there's a new active root, copy the root list and append it, updating
|
|
|
|
// the old root with the time it was rotated out.
|
|
|
|
var newRoots structs.CARoots
|
2020-11-20 23:54:44 +00:00
|
|
|
for _, r := range oldRoots {
|
|
|
|
newRoot := *r
|
2020-12-02 19:42:23 +00:00
|
|
|
if newRoot.Active && newActiveRoot != nil {
|
2020-11-20 23:54:44 +00:00
|
|
|
newRoot.Active = false
|
2021-07-13 16:15:06 +00:00
|
|
|
newRoot.RotatedOutAt = c.timeNow()
|
2020-11-20 23:54:44 +00:00
|
|
|
}
|
|
|
|
if newRoot.ExternalTrustDomain == "" {
|
|
|
|
newRoot.ExternalTrustDomain = newConf.ClusterID
|
2020-11-12 01:05:04 +00:00
|
|
|
}
|
2020-11-20 23:54:44 +00:00
|
|
|
newRoots = append(newRoots, &newRoot)
|
|
|
|
}
|
|
|
|
if newActiveRoot != nil {
|
2020-11-12 01:05:04 +00:00
|
|
|
newRoots = append(newRoots, newActiveRoot)
|
|
|
|
}
|
|
|
|
|
|
|
|
args := &structs.CARequest{
|
|
|
|
Op: structs.CAOpSetRootsAndConfig,
|
|
|
|
Index: idx,
|
|
|
|
Roots: newRoots,
|
|
|
|
Config: &newConf,
|
|
|
|
}
|
2021-06-21 23:27:30 +00:00
|
|
|
resp, err := c.delegate.ApplyCARequest(args)
|
2020-11-12 01:05:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if respOk, ok := resp.(bool); ok && !respOk {
|
|
|
|
return fmt.Errorf("could not atomically update roots and config")
|
|
|
|
}
|
|
|
|
|
|
|
|
c.logger.Info("updated root certificates from primary datacenter")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-09 16:46:42 +00:00
|
|
|
func shouldPersistNewRootAndConfig(newActiveRoot *structs.CARoot, oldConfig, newConfig *structs.CAConfiguration) bool {
|
|
|
|
if newActiveRoot != nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if newConfig == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return newConfig.Provider == oldConfig.Provider && reflect.DeepEqual(newConfig.Config, oldConfig.Config)
|
|
|
|
}
|
|
|
|
|
2021-01-15 18:20:27 +00:00
|
|
|
func (c *CAManager) UpdateConfiguration(args *structs.CARequest) (reterr error) {
|
2020-11-13 22:33:19 +00:00
|
|
|
// Attempt to update the state first.
|
2021-01-15 18:20:27 +00:00
|
|
|
oldState, err := c.setState(caStateReconfig, true)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
// Using named return values in deferred funcs isnt too common in our code
|
|
|
|
// but it is first class Go functionality. The error erturned from the
|
|
|
|
// main func will be available by its given name within deferred functions.
|
|
|
|
// See: https://blog.golang.org/defer-panic-and-recover
|
|
|
|
if reterr == nil {
|
|
|
|
c.setState(caStateInitialized, false)
|
|
|
|
} else {
|
|
|
|
c.setState(oldState, false)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-12-02 19:57:06 +00:00
|
|
|
// Attempt to initialize the config if we failed to do so in Initialize for some reason
|
2022-02-03 18:28:43 +00:00
|
|
|
prevConfig, err := c.initializeCAConfig()
|
2021-01-15 18:20:27 +00:00
|
|
|
if err != nil {
|
2020-11-13 22:33:19 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exit early if it's a no-op change
|
2020-11-20 04:08:06 +00:00
|
|
|
state := c.delegate.State()
|
2021-10-10 19:05:21 +00:00
|
|
|
_, config, err := state.CAConfig(nil)
|
2020-11-13 22:33:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't allow state changes. Either it needs to be empty or the same to allow
|
|
|
|
// read-modify-write loops that don't touch the State field.
|
|
|
|
if len(args.Config.State) > 0 &&
|
|
|
|
!reflect.DeepEqual(args.Config.State, config.State) {
|
|
|
|
return ErrStateReadOnly
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't allow users to change the ClusterID.
|
|
|
|
args.Config.ClusterID = config.ClusterID
|
|
|
|
if args.Config.Provider == config.Provider && reflect.DeepEqual(args.Config.Config, config.Config) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the provider hasn't changed, we need to load the current Provider state
|
|
|
|
// so it can decide if it needs to change resources or not based on the config
|
|
|
|
// change.
|
|
|
|
if args.Config.Provider == config.Provider {
|
|
|
|
// Note this is a shallow copy since the State method doc requires the
|
|
|
|
// provider return a map that will not be further modified and should not
|
|
|
|
// modify the one we pass to Configure.
|
|
|
|
args.Config.State = config.State
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new instance of the provider described by the config
|
|
|
|
// and get the current active root CA. This acts as a good validation
|
|
|
|
// of the config and makes sure the provider is functioning correctly
|
|
|
|
// before we commit any changes to Raft.
|
2021-06-21 21:51:37 +00:00
|
|
|
newProvider, err := c.newProvider(args.Config)
|
2020-11-13 22:33:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not initialize provider: %v", err)
|
|
|
|
}
|
|
|
|
pCfg := ca.ProviderConfig{
|
|
|
|
ClusterID: args.Config.ClusterID,
|
2020-11-20 04:08:06 +00:00
|
|
|
Datacenter: c.serverConf.Datacenter,
|
2020-11-13 22:33:19 +00:00
|
|
|
// This endpoint can be called in a secondary DC too so set this correctly.
|
2020-11-20 04:08:06 +00:00
|
|
|
IsPrimary: c.serverConf.Datacenter == c.serverConf.PrimaryDatacenter,
|
2020-11-13 22:33:19 +00:00
|
|
|
RawConfig: args.Config.Config,
|
|
|
|
State: args.Config.State,
|
|
|
|
}
|
2022-02-03 18:28:43 +00:00
|
|
|
|
|
|
|
if args.Config.Provider == config.Provider {
|
|
|
|
if validator, ok := newProvider.(ValidateConfigUpdater); ok {
|
|
|
|
if err := validator.ValidateConfigUpdate(prevConfig.Config, args.Config.Config); err != nil {
|
|
|
|
return fmt.Errorf("new configuration is incompatible with previous configuration: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-13 22:33:19 +00:00
|
|
|
if err := newProvider.Configure(pCfg); err != nil {
|
|
|
|
return fmt.Errorf("error configuring provider: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-10-10 19:05:21 +00:00
|
|
|
cleanupNewProvider := func() {
|
|
|
|
if err := newProvider.Cleanup(args.Config.Provider != config.Provider, args.Config.Config); err != nil {
|
|
|
|
c.logger.Warn("failed to clean up CA provider while handling startup failure", "provider", newProvider, "error", err)
|
2020-11-13 22:33:19 +00:00
|
|
|
}
|
2021-10-10 19:05:21 +00:00
|
|
|
}
|
2020-11-13 22:33:19 +00:00
|
|
|
|
|
|
|
// If this is a secondary, just check if the intermediate needs to be regenerated.
|
2020-11-20 04:08:06 +00:00
|
|
|
if c.serverConf.Datacenter != c.serverConf.PrimaryDatacenter {
|
2021-10-10 18:50:45 +00:00
|
|
|
if err := c.secondaryInitializeIntermediateCA(newProvider, args.Config); err != nil {
|
2021-10-10 19:05:21 +00:00
|
|
|
cleanupNewProvider()
|
2020-11-13 22:33:19 +00:00
|
|
|
return fmt.Errorf("Error updating secondary datacenter CA config: %v", err)
|
|
|
|
}
|
|
|
|
c.logger.Info("Secondary CA provider config updated")
|
|
|
|
return nil
|
|
|
|
}
|
2021-10-10 19:05:21 +00:00
|
|
|
if err := c.primaryUpdateRootCA(newProvider, args, config); err != nil {
|
|
|
|
cleanupNewProvider()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-11-13 22:33:19 +00:00
|
|
|
|
2022-02-03 18:28:43 +00:00
|
|
|
// ValidateConfigUpdater is an optional interface that may be implemented
|
|
|
|
// by a ca.Provider. If the provider implements this interface, the
|
|
|
|
// ValidateConfigurationUpdate will be called when a user attempts to change the
|
|
|
|
// CA configuration, and the provider type has not changed from the previous
|
|
|
|
// configuration.
|
|
|
|
type ValidateConfigUpdater interface {
|
|
|
|
// ValidateConfigUpdate should return an error if the next configuration is
|
|
|
|
// incompatible with the previous configuration.
|
|
|
|
//
|
|
|
|
// TODO: use better types after https://github.com/hashicorp/consul/issues/12238
|
|
|
|
ValidateConfigUpdate(previous, next map[string]interface{}) error
|
|
|
|
}
|
|
|
|
|
2021-10-10 19:05:21 +00:00
|
|
|
func (c *CAManager) primaryUpdateRootCA(newProvider ca.Provider, args *structs.CARequest, config *structs.CAConfiguration) error {
|
2021-12-01 20:39:20 +00:00
|
|
|
providerRoot, err := newProvider.GenerateRoot()
|
2020-11-13 22:33:19 +00:00
|
|
|
if err != nil {
|
2021-12-01 20:39:20 +00:00
|
|
|
return fmt.Errorf("error generating CA root certificate: %v", err)
|
2020-11-13 22:33:19 +00:00
|
|
|
}
|
|
|
|
|
2021-12-01 20:39:20 +00:00
|
|
|
newRootPEM := providerRoot.PEM
|
2022-01-05 23:21:04 +00:00
|
|
|
newActiveRoot, err := newCARoot(newRootPEM, args.Config.Provider, args.Config.ClusterID)
|
2020-11-13 22:33:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// See if the provider needs to persist any state along with the config
|
|
|
|
pState, err := newProvider.State()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error getting provider state: %v", err)
|
|
|
|
}
|
|
|
|
args.Config.State = pState
|
|
|
|
|
2021-10-10 19:05:21 +00:00
|
|
|
state := c.delegate.State()
|
2020-11-13 22:33:19 +00:00
|
|
|
// Compare the new provider's root CA ID to the current one. If they
|
|
|
|
// match, just update the existing provider with the new config.
|
|
|
|
// If they don't match, begin the root rotation process.
|
|
|
|
_, root, err := state.CARootActive(nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the root didn't change, just update the config and return.
|
|
|
|
if root != nil && root.ID == newActiveRoot.ID {
|
|
|
|
args.Op = structs.CAOpSetConfig
|
2021-11-26 22:57:55 +00:00
|
|
|
_, err := c.delegate.ApplyCARequest(args)
|
2020-11-13 22:33:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the config has been committed, update the local provider instance
|
|
|
|
c.setCAProvider(newProvider, newActiveRoot)
|
|
|
|
c.logger.Info("CA provider config updated")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-15 18:20:27 +00:00
|
|
|
// get the old CA provider to be used for Cross Signing and to clean it up at the end
|
|
|
|
// of the functi8on.
|
2020-11-13 22:33:19 +00:00
|
|
|
oldProvider, _ := c.getCAProvider()
|
|
|
|
if oldProvider == nil {
|
|
|
|
return fmt.Errorf("internal error: CA provider is nil")
|
|
|
|
}
|
|
|
|
|
2021-01-15 18:20:27 +00:00
|
|
|
// We only even think about cross signing if the current provider has a root cert
|
|
|
|
// In some cases such as having a bad CA configuration during startup the provider
|
|
|
|
// may not have been able to generate a cert. We then want to be able to prevent
|
|
|
|
// an attempt to cross sign the cert which will definitely fail.
|
|
|
|
if root != nil {
|
|
|
|
// If it's a config change that would trigger a rotation (different provider/root):
|
|
|
|
// 1. Get the root from the new provider.
|
|
|
|
// 2. Call CrossSignCA on the old provider to sign the new root with the old one to
|
|
|
|
// get a cross-signed certificate.
|
|
|
|
// 3. Take the active root for the new provider and append the intermediate from step 2
|
|
|
|
// to its list of intermediates.
|
2022-01-05 23:21:04 +00:00
|
|
|
// TODO: this cert is already parsed once in newCARoot, could we remove the second parse?
|
2021-01-15 18:20:27 +00:00
|
|
|
newRoot, err := connect.ParseCert(newRootPEM)
|
2020-11-13 22:33:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-15 18:20:27 +00:00
|
|
|
// At this point, we know the config change has triggered a root rotation,
|
|
|
|
// either by swapping the provider type or changing the provider's config
|
|
|
|
// to use a different root certificate.
|
|
|
|
|
|
|
|
// First up, check that the current provider actually supports
|
|
|
|
// cross-signing.
|
|
|
|
canXSign, err := oldProvider.SupportsCrossSigning()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("CA provider error: %s", err)
|
|
|
|
}
|
|
|
|
if !canXSign && !args.Config.ForceWithoutCrossSigning {
|
|
|
|
return errors.New("The current CA Provider does not support cross-signing. " +
|
|
|
|
"You can try again with ForceWithoutCrossSigningSet but this may cause " +
|
|
|
|
"disruption - see documentation for more.")
|
|
|
|
}
|
2021-01-29 21:38:11 +00:00
|
|
|
if args.Config.ForceWithoutCrossSigning {
|
|
|
|
c.logger.Warn("ForceWithoutCrossSigning set, CA reconfiguration skipping cross-signing")
|
2021-01-15 18:20:27 +00:00
|
|
|
}
|
|
|
|
|
2021-01-29 21:38:11 +00:00
|
|
|
// If ForceWithoutCrossSigning wasn't set, attempt to have the old CA generate a
|
|
|
|
// cross-signed intermediate.
|
|
|
|
if canXSign && !args.Config.ForceWithoutCrossSigning {
|
2021-01-15 18:20:27 +00:00
|
|
|
// Have the old provider cross-sign the new root
|
|
|
|
xcCert, err := oldProvider.CrossSignCA(newRoot)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the cross signed cert to the new CA's intermediates (to be attached
|
|
|
|
// to leaf certs).
|
|
|
|
newActiveRoot.IntermediateCerts = []string{xcCert}
|
|
|
|
}
|
2020-11-13 22:33:19 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 22:44:02 +00:00
|
|
|
// TODO: https://github.com/hashicorp/consul/issues/12386
|
2020-11-13 22:33:19 +00:00
|
|
|
intermediate, err := newProvider.GenerateIntermediate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if intermediate != newRootPEM {
|
2021-11-26 22:31:23 +00:00
|
|
|
if err := setLeafSigningCert(newActiveRoot, intermediate); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-13 22:33:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update the roots and CA config in the state store at the same time
|
|
|
|
idx, roots, err := state.CARoots(nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var newRoots structs.CARoots
|
|
|
|
for _, r := range roots {
|
|
|
|
newRoot := *r
|
|
|
|
if newRoot.Active {
|
|
|
|
newRoot.Active = false
|
2021-07-13 16:15:06 +00:00
|
|
|
newRoot.RotatedOutAt = c.timeNow()
|
2020-11-13 22:33:19 +00:00
|
|
|
}
|
|
|
|
newRoots = append(newRoots, &newRoot)
|
|
|
|
}
|
|
|
|
newRoots = append(newRoots, newActiveRoot)
|
|
|
|
|
|
|
|
args.Op = structs.CAOpSetRootsAndConfig
|
|
|
|
args.Index = idx
|
2021-10-10 19:05:21 +00:00
|
|
|
args.Config.ModifyIndex = config.ModifyIndex
|
2020-11-13 22:33:19 +00:00
|
|
|
args.Roots = newRoots
|
2021-06-21 23:27:30 +00:00
|
|
|
resp, err := c.delegate.ApplyCARequest(args)
|
2020-11-13 22:33:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if respOk, ok := resp.(bool); ok && !respOk {
|
|
|
|
return fmt.Errorf("could not atomically update roots and config")
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the config has been committed, update the local provider instance
|
|
|
|
// and call teardown on the old provider
|
|
|
|
c.setCAProvider(newProvider, newActiveRoot)
|
|
|
|
|
2021-01-15 18:20:27 +00:00
|
|
|
if err := oldProvider.Cleanup(args.Config.Provider != config.Provider, args.Config.Config); err != nil {
|
|
|
|
c.logger.Warn("failed to clean up old provider", "provider", config.Provider, "error", err)
|
2020-11-13 22:33:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c.logger.Info("CA rotated to new root under provider", "provider", args.Config.Provider)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-10 18:50:45 +00:00
|
|
|
// primaryRenewIntermediate regenerates the intermediate cert in the primary datacenter.
|
2020-11-12 01:05:04 +00:00
|
|
|
// This is only run for CAs that require an intermediary in the primary DC, such as Vault.
|
2021-06-23 21:14:28 +00:00
|
|
|
// It should only be called while the state lock is held by setting the state to non-ready.
|
2021-10-10 18:50:45 +00:00
|
|
|
func (c *CAManager) primaryRenewIntermediate(provider ca.Provider, newActiveRoot *structs.CARoot) error {
|
2020-11-12 01:05:04 +00:00
|
|
|
// Generate and sign an intermediate cert using the root CA.
|
|
|
|
intermediatePEM, err := provider.GenerateIntermediate()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error generating new intermediate cert: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-11-26 22:31:23 +00:00
|
|
|
if err := setLeafSigningCert(newActiveRoot, intermediatePEM); err != nil {
|
|
|
|
return err
|
2020-11-12 01:05:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c.logger.Info("generated new intermediate certificate for primary datacenter")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-12 00:16:53 +00:00
|
|
|
// secondaryRequestNewSigningCert creates a Certificate Signing Request, sends
|
|
|
|
// the request to the primary, and stores the received certificate in the
|
|
|
|
// provider.
|
|
|
|
// Should only be called while the state lock is held by setting the state to non-ready.
|
|
|
|
func (c *CAManager) secondaryRequestNewSigningCert(provider ca.Provider, newActiveRoot *structs.CARoot) error {
|
2020-11-12 01:05:04 +00:00
|
|
|
csr, err := provider.GenerateIntermediateCSR()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var intermediatePEM string
|
2020-11-20 04:08:06 +00:00
|
|
|
if err := c.delegate.forwardDC("ConnectCA.SignIntermediate", c.serverConf.PrimaryDatacenter, c.delegate.generateCASignRequest(csr), &intermediatePEM); err != nil {
|
2020-11-12 01:05:04 +00:00
|
|
|
// this is a failure in the primary and shouldn't be capable of erroring out our establishing leadership
|
|
|
|
c.logger.Warn("Primary datacenter refused to sign our intermediate CA certificate", "error", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := provider.SetIntermediate(intermediatePEM, newActiveRoot.RootCert); err != nil {
|
|
|
|
return fmt.Errorf("Failed to set the intermediate certificate with the CA provider: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-11-26 22:31:23 +00:00
|
|
|
if err := setLeafSigningCert(newActiveRoot, intermediatePEM); err != nil {
|
|
|
|
return err
|
2020-11-12 01:05:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c.logger.Info("received new intermediate certificate from primary datacenter")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-26 22:31:23 +00:00
|
|
|
// setLeafSigningCert updates the CARoot by appending the pem to the list of
|
|
|
|
// intermediate certificates, and setting the SigningKeyID to the encoded
|
|
|
|
// SubjectKeyId of the certificate.
|
|
|
|
func setLeafSigningCert(caRoot *structs.CARoot, pem string) error {
|
|
|
|
cert, err := connect.ParseCert(pem)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error parsing leaf signing cert: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-09-01 21:24:30 +00:00
|
|
|
if err := pruneExpiredIntermediates(caRoot); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-26 22:31:23 +00:00
|
|
|
caRoot.IntermediateCerts = append(caRoot.IntermediateCerts, pem)
|
|
|
|
caRoot.SigningKeyID = connect.EncodeSigningKeyID(cert.SubjectKeyId)
|
2022-09-01 21:24:30 +00:00
|
|
|
return nil
|
2022-08-31 18:20:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// pruneExpiredIntermediates removes expired intermediate certificates
|
|
|
|
// from the given CARoot.
|
|
|
|
func pruneExpiredIntermediates(caRoot *structs.CARoot) error {
|
|
|
|
var newIntermediates []string
|
|
|
|
now := time.Now()
|
2022-09-01 21:24:30 +00:00
|
|
|
for _, intermediatePEM := range caRoot.IntermediateCerts {
|
2022-08-31 18:20:29 +00:00
|
|
|
cert, err := connect.ParseCert(intermediatePEM)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error parsing leaf signing cert: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-09-01 21:24:30 +00:00
|
|
|
// Only keep the intermediate cert if it's still valid.
|
|
|
|
if cert.NotAfter.After(now) {
|
2022-08-31 18:20:29 +00:00
|
|
|
newIntermediates = append(newIntermediates, intermediatePEM)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
caRoot.IntermediateCerts = newIntermediates
|
2021-11-26 22:31:23 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-08 17:21:52 +00:00
|
|
|
// runRenewIntermediate periodically attempts to renew the intermediate cert.
|
|
|
|
func (c *CAManager) runRenewIntermediate(ctx context.Context) error {
|
2020-11-20 04:08:06 +00:00
|
|
|
isPrimary := c.serverConf.Datacenter == c.serverConf.PrimaryDatacenter
|
2020-11-12 01:05:04 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
|
|
|
case <-time.After(structs.IntermediateCertRenewInterval):
|
|
|
|
retryLoopBackoffAbortOnSuccess(ctx, func() error {
|
2020-11-20 04:08:06 +00:00
|
|
|
return c.RenewIntermediate(ctx, isPrimary)
|
2020-11-12 01:05:04 +00:00
|
|
|
}, func(err error) {
|
|
|
|
c.logger.Error("error renewing intermediate certs",
|
|
|
|
"routine", intermediateCertRenewWatchRoutineName,
|
|
|
|
"error", err,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-13 22:33:19 +00:00
|
|
|
// RenewIntermediate checks the intermediate cert for
|
|
|
|
// expiration. If more than half the time a cert is valid has passed,
|
|
|
|
// it will try to renew it.
|
2020-11-20 04:08:06 +00:00
|
|
|
func (c *CAManager) RenewIntermediate(ctx context.Context, isPrimary bool) error {
|
2020-11-13 22:33:19 +00:00
|
|
|
// Grab the 'lock' right away so the provider/config can't be changed out while we check
|
|
|
|
// the intermediate.
|
2021-01-15 18:20:27 +00:00
|
|
|
if _, err := c.setState(caStateRenewIntermediate, true); err != nil {
|
2020-11-13 22:33:19 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-01-15 18:20:27 +00:00
|
|
|
defer c.setState(caStateInitialized, false)
|
2020-11-13 22:33:19 +00:00
|
|
|
|
|
|
|
provider, _ := c.getCAProvider()
|
|
|
|
if provider == nil {
|
|
|
|
// this happens when leadership is being revoked and this go routine will be stopped
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// If this isn't the primary, make sure the CA has been initialized.
|
2021-10-18 15:44:47 +00:00
|
|
|
if !isPrimary && !c.secondaryHasProviderRoots() {
|
2020-11-13 22:33:19 +00:00
|
|
|
return fmt.Errorf("secondary CA is not yet configured.")
|
|
|
|
}
|
|
|
|
|
2020-11-20 04:08:06 +00:00
|
|
|
state := c.delegate.State()
|
2020-11-17 23:35:33 +00:00
|
|
|
_, root, err := state.CARootActive(nil)
|
2020-11-13 22:33:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-17 23:35:33 +00:00
|
|
|
activeRoot := root.Clone()
|
2020-11-13 22:33:19 +00:00
|
|
|
|
|
|
|
// If this is the primary, check if this is a provider that uses an intermediate cert. If
|
|
|
|
// it isn't, we don't need to check for a renewal.
|
2021-11-23 17:49:43 +00:00
|
|
|
if isPrimary && !primaryUsesIntermediate(provider) {
|
|
|
|
return nil
|
2020-11-13 22:33:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
activeIntermediate, err := provider.ActiveIntermediate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if activeIntermediate == "" {
|
|
|
|
return fmt.Errorf("datacenter doesn't have an active intermediate.")
|
|
|
|
}
|
|
|
|
|
|
|
|
intermediateCert, err := connect.ParseCert(activeIntermediate)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error parsing active intermediate cert: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-12-08 17:21:52 +00:00
|
|
|
if lessThanHalfTimePassed(c.timeNow(), intermediateCert.NotBefore, intermediateCert.NotAfter) {
|
2020-11-13 22:33:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enough time has passed, go ahead with getting a new intermediate.
|
2021-10-10 18:50:45 +00:00
|
|
|
renewalFunc := c.primaryRenewIntermediate
|
2020-11-13 22:33:19 +00:00
|
|
|
if !isPrimary {
|
2021-11-12 00:16:53 +00:00
|
|
|
renewalFunc = c.secondaryRequestNewSigningCert
|
2020-11-13 22:33:19 +00:00
|
|
|
}
|
2020-11-30 22:37:24 +00:00
|
|
|
errCh := make(chan error, 1)
|
2020-11-20 04:08:06 +00:00
|
|
|
go func() {
|
|
|
|
errCh <- renewalFunc(provider, activeRoot)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Wait for the renewal func to return or for the context to be canceled.
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
case err := <-errCh:
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-13 22:33:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.persistNewRootAndConfig(provider, activeRoot, nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.setCAProvider(provider, activeRoot)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-12 01:05:04 +00:00
|
|
|
// secondaryCARootWatch maintains a blocking query to the primary datacenter's
|
|
|
|
// ConnectCA.Roots endpoint to monitor when it needs to request a new signed
|
|
|
|
// intermediate certificate.
|
|
|
|
func (c *CAManager) secondaryCARootWatch(ctx context.Context) error {
|
|
|
|
args := structs.DCSpecificRequest{
|
2020-11-20 04:08:06 +00:00
|
|
|
Datacenter: c.serverConf.PrimaryDatacenter,
|
2020-11-12 01:05:04 +00:00
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
// the maximum time the primary roots watch query can block before returning
|
2020-11-20 04:08:06 +00:00
|
|
|
MaxQueryTime: c.serverConf.MaxQueryTime,
|
2020-11-12 01:05:04 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-11-20 04:08:06 +00:00
|
|
|
c.logger.Debug("starting Connect CA root replication from primary datacenter", "primary", c.serverConf.PrimaryDatacenter)
|
2020-11-12 01:05:04 +00:00
|
|
|
|
|
|
|
retryLoopBackoff(ctx, func() error {
|
|
|
|
var roots structs.IndexedCARoots
|
2020-11-20 04:08:06 +00:00
|
|
|
if err := c.delegate.forwardDC("ConnectCA.Roots", c.serverConf.PrimaryDatacenter, &args, &roots); err != nil {
|
2020-11-12 01:05:04 +00:00
|
|
|
return fmt.Errorf("Error retrieving the primary datacenter's roots: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-11-17 23:35:33 +00:00
|
|
|
// Return if the context has been canceled while waiting on the RPC.
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2020-11-13 22:33:19 +00:00
|
|
|
// Attempt to update the roots using the returned data.
|
2021-10-10 18:50:45 +00:00
|
|
|
if err := c.secondaryUpdateRoots(roots); err != nil {
|
2020-11-12 01:05:04 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
args.QueryOptions.MinQueryIndex = nextIndexVal(args.QueryOptions.MinQueryIndex, roots.QueryMeta.Index)
|
|
|
|
return nil
|
|
|
|
}, func(err error) {
|
|
|
|
c.logger.Error("CA root replication failed, will retry",
|
|
|
|
"routine", secondaryCARootWatchRoutineName,
|
|
|
|
"error", err,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-10 18:50:45 +00:00
|
|
|
// secondaryUpdateRoots updates the cached roots from the primary and regenerates the intermediate
|
2020-11-13 22:33:19 +00:00
|
|
|
// certificate if necessary.
|
2021-10-10 18:50:45 +00:00
|
|
|
func (c *CAManager) secondaryUpdateRoots(roots structs.IndexedCARoots) error {
|
2020-11-13 22:33:19 +00:00
|
|
|
// Update the state first to claim the 'lock'.
|
2021-01-15 18:20:27 +00:00
|
|
|
if _, err := c.setState(caStateReconfig, true); err != nil {
|
2020-11-13 22:33:19 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-01-15 18:20:27 +00:00
|
|
|
defer c.setState(caStateInitialized, false)
|
2020-11-13 22:33:19 +00:00
|
|
|
|
|
|
|
// Update the cached primary roots now that the lock is held.
|
2021-10-11 01:15:13 +00:00
|
|
|
c.secondarySetPrimaryRoots(roots)
|
2020-11-13 22:33:19 +00:00
|
|
|
|
|
|
|
provider, _ := c.getCAProvider()
|
|
|
|
if provider == nil {
|
|
|
|
// this happens when leadership is being revoked and this go routine will be stopped
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the secondary CA init routine to see if we need to request a new
|
|
|
|
// intermediate.
|
2021-10-18 15:44:47 +00:00
|
|
|
if c.secondaryHasProviderRoots() {
|
2021-10-10 18:50:45 +00:00
|
|
|
if err := c.secondaryInitializeIntermediateCA(provider, nil); err != nil {
|
2020-11-13 22:33:19 +00:00
|
|
|
return fmt.Errorf("Failed to initialize the secondary CA: %v", err)
|
|
|
|
}
|
2021-10-18 15:44:47 +00:00
|
|
|
return nil
|
2020-11-13 22:33:19 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 15:44:47 +00:00
|
|
|
// Attempt to initialize now that we have updated roots. This is an optimization
|
2021-12-02 19:57:06 +00:00
|
|
|
// so that we don't have to wait for the Initialize retry backoff if we were
|
2021-10-18 15:44:47 +00:00
|
|
|
// waiting on roots from the primary to be able to complete initialization.
|
|
|
|
if err := c.delegate.ServersSupportMultiDCConnectCA(); err != nil {
|
|
|
|
return fmt.Errorf("failed to initialize while updating primary roots: %w", err)
|
|
|
|
}
|
|
|
|
if err := c.secondaryInitializeProvider(provider, roots); err != nil {
|
|
|
|
return fmt.Errorf("Failed to initialize secondary CA provider: %v", err)
|
|
|
|
}
|
|
|
|
if err := c.secondaryInitializeIntermediateCA(provider, nil); err != nil {
|
|
|
|
return fmt.Errorf("Failed to initialize the secondary CA: %v", err)
|
|
|
|
}
|
2020-11-13 22:33:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-10 18:50:45 +00:00
|
|
|
// secondaryInitializeProvider configures the given provider for a secondary, non-root datacenter.
|
|
|
|
func (c *CAManager) secondaryInitializeProvider(provider ca.Provider, roots structs.IndexedCARoots) error {
|
2020-11-12 01:05:04 +00:00
|
|
|
if roots.TrustDomain == "" {
|
|
|
|
return fmt.Errorf("trust domain from primary datacenter is not initialized")
|
|
|
|
}
|
|
|
|
|
|
|
|
clusterID := strings.Split(roots.TrustDomain, ".")[0]
|
2020-11-20 04:08:06 +00:00
|
|
|
_, conf, err := c.delegate.State().CAConfig(nil)
|
2020-11-12 01:05:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
pCfg := ca.ProviderConfig{
|
|
|
|
ClusterID: clusterID,
|
2020-11-20 04:08:06 +00:00
|
|
|
Datacenter: c.serverConf.Datacenter,
|
2020-11-12 01:05:04 +00:00
|
|
|
IsPrimary: false,
|
|
|
|
RawConfig: conf.Config,
|
|
|
|
State: conf.State,
|
|
|
|
}
|
|
|
|
if err := provider.Configure(pCfg); err != nil {
|
|
|
|
return fmt.Errorf("error configuring provider: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-18 15:44:47 +00:00
|
|
|
// secondaryHasProviderRoots returns true after providerRoot has been set. This
|
|
|
|
// method is used to detect when the secondary has received the roots from the
|
|
|
|
// primary DC.
|
|
|
|
func (c *CAManager) secondaryHasProviderRoots() bool {
|
2021-10-11 01:15:13 +00:00
|
|
|
// TODO: this could potentially also use primaryRoots instead of providerRoot
|
2021-10-18 15:44:47 +00:00
|
|
|
c.providerLock.Lock()
|
|
|
|
defer c.providerLock.Unlock()
|
|
|
|
return c.providerRoot != nil
|
2020-11-12 01:05:04 +00:00
|
|
|
}
|
2021-06-23 20:32:59 +00:00
|
|
|
|
|
|
|
type connectSignRateLimiter struct {
|
|
|
|
// csrRateLimiter limits the rate of signing new certs if configured. Lazily
|
|
|
|
// initialized from current config to support dynamic changes.
|
|
|
|
// csrRateLimiterMu must be held while dereferencing the pointer or storing a
|
|
|
|
// new one, but methods can be called on the limiter object outside of the
|
|
|
|
// locked section. This is done only in the getCSRRateLimiterWithLimit method.
|
|
|
|
csrRateLimiter *rate.Limiter
|
|
|
|
csrRateLimiterMu sync.RWMutex
|
|
|
|
|
|
|
|
// csrConcurrencyLimiter is a dynamically resizable semaphore used to limit
|
|
|
|
// Sign RPC concurrency if configured. The zero value is usable as soon as
|
|
|
|
// SetSize is called which we do dynamically in the RPC handler to avoid
|
|
|
|
// having to hook elaborate synchronization mechanisms through the CA config
|
|
|
|
// endpoint and config reload etc.
|
|
|
|
csrConcurrencyLimiter semaphore.Dynamic
|
|
|
|
}
|
|
|
|
|
|
|
|
// getCSRRateLimiterWithLimit returns a rate.Limiter with the desired limit set.
|
|
|
|
// It uses the shared server-wide limiter unless the limit has been changed in
|
|
|
|
// config or the limiter has not been setup yet in which case it just-in-time
|
|
|
|
// configures the new limiter. We assume that limit changes are relatively rare
|
|
|
|
// and that all callers (there is currently only one) use the same config value
|
|
|
|
// as the limit. There might be some flapping if there are multiple concurrent
|
|
|
|
// requests in flight at the time the config changes where A sees the new value
|
|
|
|
// and updates, B sees the old but then gets this lock second and changes back.
|
|
|
|
// Eventually though and very soon (once all current RPCs are complete) we are
|
|
|
|
// guaranteed to have the correct limit set by the next RPC that comes in so I
|
|
|
|
// assume this is fine. If we observe strange behavior because of it, we could
|
|
|
|
// add hysteresis that prevents changes too soon after a previous change but
|
|
|
|
// that seems unnecessary for now.
|
|
|
|
func (l *connectSignRateLimiter) getCSRRateLimiterWithLimit(limit rate.Limit) *rate.Limiter {
|
|
|
|
l.csrRateLimiterMu.RLock()
|
|
|
|
lim := l.csrRateLimiter
|
|
|
|
l.csrRateLimiterMu.RUnlock()
|
|
|
|
|
|
|
|
// If there is a current limiter with the same limit, return it. This should
|
|
|
|
// be the common case.
|
|
|
|
if lim != nil && lim.Limit() == limit {
|
|
|
|
return lim
|
|
|
|
}
|
|
|
|
|
|
|
|
// Need to change limiter, get write lock
|
|
|
|
l.csrRateLimiterMu.Lock()
|
|
|
|
defer l.csrRateLimiterMu.Unlock()
|
|
|
|
// No limiter yet, or limit changed in CA config, reconfigure a new limiter.
|
|
|
|
// We use burst of 1 for a hard limit. Note that either bursting or waiting is
|
|
|
|
// necessary to get expected behavior in fact of random arrival times, but we
|
|
|
|
// don't need both and we use Wait with a small delay to smooth noise. See
|
|
|
|
// https://github.com/banks/sim-rate-limit-backoff/blob/master/README.md.
|
|
|
|
l.csrRateLimiter = rate.NewLimiter(limit, 1)
|
|
|
|
return l.csrRateLimiter
|
|
|
|
}
|
|
|
|
|
2022-04-05 18:16:20 +00:00
|
|
|
// AuthorizeAndSignCertificate signs a leaf certificate for the service or agent
|
|
|
|
// identified by the SPIFFE ID in the given CSR's SAN. It performs authorization
|
|
|
|
// using the given acl.Authorizer.
|
|
|
|
func (c *CAManager) AuthorizeAndSignCertificate(csr *x509.CertificateRequest, authz acl.Authorizer) (*structs.IssuedCert, error) {
|
|
|
|
// Parse the SPIFFE ID from the CSR SAN.
|
|
|
|
if len(csr.URIs) == 0 {
|
2022-04-14 13:26:14 +00:00
|
|
|
return nil, connect.InvalidCSRError("CSR SAN does not contain a SPIFFE ID")
|
2022-04-05 18:16:20 +00:00
|
|
|
}
|
|
|
|
spiffeID, err := connect.ParseCertURI(csr.URIs[0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform authorization.
|
|
|
|
var authzContext acl.AuthorizerContext
|
|
|
|
allow := authz.ToAllowAuthorizer()
|
|
|
|
switch v := spiffeID.(type) {
|
|
|
|
case *connect.SpiffeIDService:
|
|
|
|
v.GetEnterpriseMeta().FillAuthzContext(&authzContext)
|
|
|
|
if err := allow.ServiceWriteAllowed(v.Service, &authzContext); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that the DC in the service URI matches us. We might relax this
|
|
|
|
// requirement later but being restrictive for now is safer.
|
|
|
|
dc := c.serverConf.Datacenter
|
|
|
|
if v.Datacenter != dc {
|
2022-04-14 13:26:14 +00:00
|
|
|
return nil, connect.InvalidCSRError("SPIFFE ID in CSR from a different datacenter: %s, "+
|
2022-04-05 18:16:20 +00:00
|
|
|
"we are %s", v.Datacenter, dc)
|
|
|
|
}
|
|
|
|
case *connect.SpiffeIDAgent:
|
|
|
|
v.GetEnterpriseMeta().FillAuthzContext(&authzContext)
|
|
|
|
if err := allow.NodeWriteAllowed(v.Agent, &authzContext); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-06-15 19:36:18 +00:00
|
|
|
case *connect.SpiffeIDMeshGateway:
|
|
|
|
// TODO(peering): figure out what is appropriate here for ACLs
|
|
|
|
v.GetEnterpriseMeta().FillAuthzContext(&authzContext)
|
|
|
|
if err := allow.MeshWriteAllowed(&authzContext); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that the DC in the gateway URI matches us. We might relax this
|
|
|
|
// requirement later but being restrictive for now is safer.
|
|
|
|
dc := c.serverConf.Datacenter
|
|
|
|
if v.Datacenter != dc {
|
|
|
|
return nil, connect.InvalidCSRError("SPIFFE ID in CSR from a different datacenter: %s, "+
|
|
|
|
"we are %s", v.Datacenter, dc)
|
|
|
|
}
|
2022-09-06 23:58:13 +00:00
|
|
|
case *connect.SpiffeIDServer:
|
|
|
|
// The authorizer passed in should have unlimited permissions.
|
|
|
|
if err := allow.ACLWriteAllowed(&authzContext); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that the DC in the URI matches us.
|
|
|
|
// The request must have been issued by a local server.
|
|
|
|
dc := c.serverConf.Datacenter
|
|
|
|
if v.Datacenter != dc {
|
|
|
|
return nil, connect.InvalidCSRError("SPIFFE ID in CSR from a different datacenter: %s, "+
|
|
|
|
"we are %s", v.Datacenter, dc)
|
|
|
|
}
|
2022-04-05 18:16:20 +00:00
|
|
|
default:
|
2022-04-14 13:26:14 +00:00
|
|
|
return nil, connect.InvalidCSRError("SPIFFE ID in CSR must be a service or agent ID")
|
2022-04-05 18:16:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.SignCertificate(csr, spiffeID)
|
|
|
|
}
|
|
|
|
|
2021-06-23 20:32:59 +00:00
|
|
|
func (c *CAManager) SignCertificate(csr *x509.CertificateRequest, spiffeID connect.CertURI) (*structs.IssuedCert, error) {
|
|
|
|
provider, caRoot := c.getCAProvider()
|
|
|
|
if provider == nil {
|
|
|
|
return nil, fmt.Errorf("CA is uninitialized and unable to sign certificates yet: provider is nil")
|
|
|
|
} else if caRoot == nil {
|
|
|
|
return nil, fmt.Errorf("CA is uninitialized and unable to sign certificates yet: no root certificate")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that the CSR entity is in the cluster's trust domain
|
|
|
|
state := c.delegate.State()
|
|
|
|
_, config, err := state.CAConfig(nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-09-06 23:58:13 +00:00
|
|
|
|
2021-11-05 22:20:24 +00:00
|
|
|
signingID := connect.SpiffeIDSigningForCluster(config.ClusterID)
|
2021-06-23 20:32:59 +00:00
|
|
|
serviceID, isService := spiffeID.(*connect.SpiffeIDService)
|
|
|
|
agentID, isAgent := spiffeID.(*connect.SpiffeIDAgent)
|
2022-09-06 23:58:13 +00:00
|
|
|
serverID, isServer := spiffeID.(*connect.SpiffeIDServer)
|
2022-06-15 19:36:18 +00:00
|
|
|
mgwID, isMeshGateway := spiffeID.(*connect.SpiffeIDMeshGateway)
|
2021-06-23 20:32:59 +00:00
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
var entMeta acl.EnterpriseMeta
|
2022-06-15 19:36:18 +00:00
|
|
|
switch {
|
|
|
|
case isService:
|
2021-06-23 20:32:59 +00:00
|
|
|
if !signingID.CanSign(spiffeID) {
|
2022-04-14 13:26:14 +00:00
|
|
|
return nil, connect.InvalidCSRError("SPIFFE ID in CSR from a different trust domain: %s, "+
|
2021-06-23 20:32:59 +00:00
|
|
|
"we are %s", serviceID.Host, signingID.Host())
|
|
|
|
}
|
|
|
|
entMeta.Merge(serviceID.GetEnterpriseMeta())
|
2022-06-15 19:36:18 +00:00
|
|
|
|
|
|
|
case isMeshGateway:
|
|
|
|
if !signingID.CanSign(spiffeID) {
|
|
|
|
return nil, connect.InvalidCSRError("SPIFFE ID in CSR from a different trust domain: %s, "+
|
|
|
|
"we are %s", mgwID.Host, signingID.Host())
|
|
|
|
}
|
|
|
|
entMeta.Merge(mgwID.GetEnterpriseMeta())
|
|
|
|
|
2022-09-06 23:58:13 +00:00
|
|
|
case isServer:
|
|
|
|
if !signingID.CanSign(spiffeID) {
|
|
|
|
return nil, connect.InvalidCSRError("SPIFFE ID in CSR from a different trust domain: %s, "+
|
|
|
|
"we are %s", serverID.Host, signingID.Host())
|
|
|
|
}
|
|
|
|
entMeta.Normalize()
|
2022-06-15 19:36:18 +00:00
|
|
|
case isAgent:
|
2021-06-23 20:32:59 +00:00
|
|
|
// isAgent - if we support more ID types then this would need to be an else if
|
|
|
|
// here we are just automatically fixing the trust domain. For auto-encrypt and
|
|
|
|
// auto-config they make certificate requests before learning about the roots
|
|
|
|
// so they will have a dummy trust domain in the CSR.
|
|
|
|
trustDomain := signingID.Host()
|
|
|
|
if agentID.Host != trustDomain {
|
|
|
|
originalURI := agentID.URI()
|
|
|
|
|
|
|
|
agentID.Host = trustDomain
|
|
|
|
|
|
|
|
// recreate the URIs list
|
|
|
|
uris := make([]*url.URL, len(csr.URIs))
|
|
|
|
for i, uri := range csr.URIs {
|
|
|
|
if originalURI.String() == uri.String() {
|
|
|
|
uris[i] = agentID.URI()
|
|
|
|
} else {
|
|
|
|
uris[i] = uri
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
csr.URIs = uris
|
|
|
|
}
|
2021-10-14 14:32:45 +00:00
|
|
|
entMeta.Merge(agentID.GetEnterpriseMeta())
|
2022-06-15 19:36:18 +00:00
|
|
|
|
|
|
|
default:
|
2022-09-06 23:58:13 +00:00
|
|
|
return nil, connect.InvalidCSRError("SPIFFE ID in CSR must be a service, agent, server, or mesh gateway ID")
|
2021-06-23 20:32:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
commonCfg, err := config.GetCommonConfig()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if commonCfg.CSRMaxPerSecond > 0 {
|
|
|
|
lim := c.caLeafLimiter.getCSRRateLimiterWithLimit(rate.Limit(commonCfg.CSRMaxPerSecond))
|
|
|
|
// Wait up to the small threshold we allow for a token.
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), csrLimitWait)
|
|
|
|
defer cancel()
|
|
|
|
if lim.Wait(ctx) != nil {
|
|
|
|
return nil, ErrRateLimited
|
|
|
|
}
|
|
|
|
} else if commonCfg.CSRMaxConcurrent > 0 {
|
|
|
|
c.caLeafLimiter.csrConcurrencyLimiter.SetSize(int64(commonCfg.CSRMaxConcurrent))
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), csrLimitWait)
|
|
|
|
defer cancel()
|
|
|
|
if err := c.caLeafLimiter.csrConcurrencyLimiter.Acquire(ctx); err != nil {
|
|
|
|
return nil, ErrRateLimited
|
|
|
|
}
|
|
|
|
defer c.caLeafLimiter.csrConcurrencyLimiter.Release()
|
|
|
|
}
|
|
|
|
|
|
|
|
connect.HackSANExtensionForCSR(csr)
|
|
|
|
|
2021-07-13 16:15:06 +00:00
|
|
|
// Check if the root expired before using it to sign.
|
2021-11-23 19:15:08 +00:00
|
|
|
// TODO: we store NotBefore and NotAfter on this struct, so we could avoid
|
|
|
|
// parsing the cert here.
|
|
|
|
err = c.checkExpired(caRoot.RootCert)
|
2021-07-13 16:15:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("root expired: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-11-23 19:15:08 +00:00
|
|
|
if c.isIntermediateUsedToSignLeaf() && len(caRoot.IntermediateCerts) > 0 {
|
|
|
|
inter := caRoot.IntermediateCerts[len(caRoot.IntermediateCerts)-1]
|
|
|
|
if err := c.checkExpired(inter); err != nil {
|
|
|
|
return nil, fmt.Errorf("intermediate expired: %w", err)
|
|
|
|
}
|
2021-07-13 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
2021-06-23 20:32:59 +00:00
|
|
|
// All seems to be in order, actually sign it.
|
|
|
|
pem, err := provider.Sign(csr)
|
|
|
|
if err == ca.ErrRateLimited {
|
|
|
|
return nil, ErrRateLimited
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append any intermediates needed by this root.
|
|
|
|
for _, p := range caRoot.IntermediateCerts {
|
2022-06-01 21:53:52 +00:00
|
|
|
pem = pem + lib.EnsureTrailingNewline(p)
|
2021-06-23 20:32:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
modIdx, err := c.delegate.ApplyCALeafRequest()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cert, err := connect.ParseCert(pem)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the response
|
|
|
|
reply := structs.IssuedCert{
|
|
|
|
SerialNumber: connect.EncodeSerialNumber(cert.SerialNumber),
|
|
|
|
CertPEM: pem,
|
|
|
|
ValidAfter: cert.NotBefore,
|
|
|
|
ValidBefore: cert.NotAfter,
|
|
|
|
EnterpriseMeta: entMeta,
|
|
|
|
RaftIndex: structs.RaftIndex{
|
|
|
|
ModifyIndex: modIdx,
|
|
|
|
CreateIndex: modIdx,
|
|
|
|
},
|
|
|
|
}
|
2022-06-15 19:36:18 +00:00
|
|
|
|
|
|
|
switch {
|
|
|
|
case isService:
|
2021-06-23 20:32:59 +00:00
|
|
|
reply.Service = serviceID.Service
|
|
|
|
reply.ServiceURI = cert.URIs[0].String()
|
2022-06-15 19:36:18 +00:00
|
|
|
case isMeshGateway:
|
|
|
|
reply.Kind = structs.ServiceKindMeshGateway
|
|
|
|
reply.KindURI = cert.URIs[0].String()
|
|
|
|
case isAgent:
|
2021-06-23 20:32:59 +00:00
|
|
|
reply.Agent = agentID.Agent
|
|
|
|
reply.AgentURI = cert.URIs[0].String()
|
2022-09-06 23:58:13 +00:00
|
|
|
case isServer:
|
|
|
|
reply.ServerURI = cert.URIs[0].String()
|
2022-06-15 19:36:18 +00:00
|
|
|
default:
|
|
|
|
return nil, errors.New("not possible")
|
2021-06-23 20:32:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &reply, nil
|
|
|
|
}
|
2021-07-13 16:15:06 +00:00
|
|
|
|
2021-10-10 19:08:46 +00:00
|
|
|
func (c *CAManager) checkExpired(pem string) error {
|
2021-07-13 16:15:06 +00:00
|
|
|
cert, err := connect.ParseCert(pem)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-10-10 19:08:46 +00:00
|
|
|
if cert.NotAfter.Before(c.timeNow()) {
|
2021-07-13 16:15:06 +00:00
|
|
|
return fmt.Errorf("certificate expired, expiration date: %s ", cert.NotAfter.String())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-11-23 17:49:43 +00:00
|
|
|
|
|
|
|
func primaryUsesIntermediate(provider ca.Provider) bool {
|
|
|
|
_, ok := provider.(ca.PrimaryUsesIntermediate)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CAManager) isIntermediateUsedToSignLeaf() bool {
|
|
|
|
if c.serverConf.Datacenter != c.serverConf.PrimaryDatacenter {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
provider, _ := c.getCAProvider()
|
|
|
|
return primaryUsesIntermediate(provider)
|
2021-12-01 00:26:36 +00:00
|
|
|
}
|
2022-04-14 20:55:10 +00:00
|
|
|
|
|
|
|
func providerPrettyName(provider string) string {
|
|
|
|
switch provider {
|
|
|
|
case "consul":
|
|
|
|
return "Consul"
|
|
|
|
case "vault":
|
|
|
|
return "Vault"
|
|
|
|
case "aws-pca":
|
|
|
|
return "Aws-Pca"
|
|
|
|
case "provider-name":
|
|
|
|
return "Provider-Name"
|
|
|
|
default:
|
|
|
|
return provider
|
|
|
|
}
|
|
|
|
}
|