2018-03-17 04:28:27 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2019-01-22 17:19:36 +00:00
|
|
|
"context"
|
2018-04-25 18:34:08 +00:00
|
|
|
"errors"
|
2018-03-19 21:36:17 +00:00
|
|
|
"fmt"
|
2020-06-30 13:48:42 +00:00
|
|
|
"net/url"
|
2018-04-21 01:46:02 +00:00
|
|
|
"reflect"
|
2018-06-20 11:37:36 +00:00
|
|
|
"strings"
|
2018-06-21 22:42:28 +00:00
|
|
|
"time"
|
2018-03-19 21:36:17 +00:00
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
2019-01-22 17:19:36 +00:00
|
|
|
|
|
|
|
"golang.org/x/time/rate"
|
|
|
|
|
2018-04-09 04:58:31 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
2018-03-19 21:36:17 +00:00
|
|
|
"github.com/hashicorp/consul/agent/connect"
|
2019-11-18 14:22:19 +00:00
|
|
|
"github.com/hashicorp/consul/agent/connect/ca"
|
2018-03-17 04:28:27 +00:00
|
|
|
"github.com/hashicorp/consul/agent/consul/state"
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
|
|
|
"github.com/hashicorp/go-memdb"
|
|
|
|
)
|
|
|
|
|
2019-01-22 17:19:36 +00:00
|
|
|
var (
|
|
|
|
// Err strings. net/rpc doesn't have a way to transport typed/rich errors so
|
|
|
|
// we currently rely on sniffing the error string in a few cases where we need
|
|
|
|
// to change client behavior. These are the canonical error strings to use.
|
|
|
|
// Note though that client code can't use `err == consul.Err*` directly since
|
|
|
|
// the error returned by RPC will be a plain error.errorString created by
|
|
|
|
// net/rpc client so will not be the same _instance_ that this package
|
|
|
|
// variable points to. Clients need to compare using `err.Error() ==
|
|
|
|
// consul.ErrRateLimited.Error()` which is very sad. Short of replacing our
|
|
|
|
// RPC mechanism it's hard to know how to make that much better though.
|
2019-06-24 18:21:51 +00:00
|
|
|
ErrConnectNotEnabled = errors.New("Connect must be enabled in order to use this endpoint")
|
|
|
|
ErrRateLimited = errors.New("Rate limit reached, try again later")
|
|
|
|
ErrNotPrimaryDatacenter = errors.New("not the primary datacenter")
|
2019-11-21 17:40:29 +00:00
|
|
|
ErrStateReadOnly = errors.New("CA Provider State is read-only")
|
2019-01-22 17:19:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// csrLimitWait is the maximum time we'll wait for a slot when CSR concurrency
|
2019-03-06 17:13:28 +00:00
|
|
|
// limiting or rate limiting is occurring. It's intentionally short so small
|
2019-01-22 17:19:36 +00:00
|
|
|
// batches of requests can be accommodated when server has capacity (assuming
|
|
|
|
// signing one cert takes much less than this) but failing requests fast when
|
|
|
|
// a thundering herd comes along.
|
|
|
|
csrLimitWait = 500 * time.Millisecond
|
|
|
|
)
|
2018-04-25 18:34:08 +00:00
|
|
|
|
2018-03-17 04:28:27 +00:00
|
|
|
// ConnectCA manages the Connect CA.
|
|
|
|
type ConnectCA struct {
|
|
|
|
// srv is a pointer back to the server.
|
|
|
|
srv *Server
|
2019-01-22 17:19:36 +00:00
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
logger hclog.Logger
|
2018-03-17 04:28:27 +00:00
|
|
|
}
|
|
|
|
|
2018-04-09 04:58:31 +00:00
|
|
|
// ConfigurationGet returns the configuration for the CA.
|
|
|
|
func (s *ConnectCA) ConfigurationGet(
|
|
|
|
args *structs.DCSpecificRequest,
|
|
|
|
reply *structs.CAConfiguration) error {
|
2018-04-25 18:34:08 +00:00
|
|
|
// Exit early if Connect hasn't been enabled.
|
|
|
|
if !s.srv.config.ConnectEnabled {
|
|
|
|
return ErrConnectNotEnabled
|
|
|
|
}
|
|
|
|
|
2020-07-07 19:45:08 +00:00
|
|
|
if done, err := s.srv.ForwardRPC("ConnectCA.ConfigurationGet", args, args, reply); done {
|
2018-04-09 04:58:31 +00:00
|
|
|
return err
|
2018-03-21 19:42:42 +00:00
|
|
|
}
|
|
|
|
|
2018-04-09 04:58:31 +00:00
|
|
|
// This action requires operator read access.
|
2018-10-19 16:04:07 +00:00
|
|
|
rule, err := s.srv.ResolveToken(args.Token)
|
2018-04-09 04:58:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-03-21 19:42:42 +00:00
|
|
|
}
|
2019-10-15 20:58:50 +00:00
|
|
|
if rule != nil && rule.OperatorRead(nil) != acl.Allow {
|
2018-04-09 04:58:31 +00:00
|
|
|
return acl.ErrPermissionDenied
|
2018-03-21 19:42:42 +00:00
|
|
|
}
|
|
|
|
|
2018-04-09 04:58:31 +00:00
|
|
|
state := s.srv.fsm.State()
|
2019-08-19 18:03:03 +00:00
|
|
|
_, config, err := state.CAConfig(nil)
|
2018-04-09 04:58:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-03-21 19:42:42 +00:00
|
|
|
}
|
2018-04-09 04:58:31 +00:00
|
|
|
*reply = *config
|
2018-03-21 19:42:42 +00:00
|
|
|
|
2018-04-09 04:58:31 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-03-21 19:42:42 +00:00
|
|
|
|
2018-04-09 04:58:31 +00:00
|
|
|
// ConfigurationSet updates the configuration for the CA.
|
|
|
|
func (s *ConnectCA) ConfigurationSet(
|
|
|
|
args *structs.CARequest,
|
|
|
|
reply *interface{}) error {
|
2018-04-25 18:34:08 +00:00
|
|
|
// Exit early if Connect hasn't been enabled.
|
|
|
|
if !s.srv.config.ConnectEnabled {
|
|
|
|
return ErrConnectNotEnabled
|
|
|
|
}
|
|
|
|
|
2020-07-07 19:45:08 +00:00
|
|
|
if done, err := s.srv.ForwardRPC("ConnectCA.ConfigurationSet", args, args, reply); done {
|
2018-03-21 19:42:42 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-04-24 18:50:31 +00:00
|
|
|
// This action requires operator write access.
|
2018-10-19 16:04:07 +00:00
|
|
|
rule, err := s.srv.ResolveToken(args.Token)
|
2018-03-21 19:42:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-15 20:58:50 +00:00
|
|
|
if rule != nil && rule.OperatorWrite(nil) != acl.Allow {
|
2018-04-09 04:58:31 +00:00
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
2018-03-21 19:42:42 +00:00
|
|
|
|
2018-04-21 01:46:02 +00:00
|
|
|
// Exit early if it's a no-op change
|
|
|
|
state := s.srv.fsm.State()
|
2019-08-19 18:03:03 +00:00
|
|
|
confIdx, config, err := state.CAConfig(nil)
|
2018-04-21 01:46:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-11-07 06:46:06 +00:00
|
|
|
|
2019-11-21 17:40:29 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2018-11-07 06:46:06 +00:00
|
|
|
// Don't allow users to change the ClusterID.
|
2018-06-20 22:02:31 +00:00
|
|
|
args.Config.ClusterID = config.ClusterID
|
2018-04-21 01:46:02 +00:00
|
|
|
if args.Config.Provider == config.Provider && reflect.DeepEqual(args.Config.Config, config.Config) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-11 20:57:16 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2018-04-21 01:46:02 +00:00
|
|
|
// 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.
|
|
|
|
newProvider, err := s.srv.createCAProvider(args.Config)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not initialize provider: %v", err)
|
|
|
|
}
|
2019-11-18 14:22:19 +00:00
|
|
|
pCfg := ca.ProviderConfig{
|
|
|
|
ClusterID: args.Config.ClusterID,
|
|
|
|
Datacenter: s.srv.config.Datacenter,
|
|
|
|
// This endpoint can be called in a secondary DC too so set this correctly.
|
|
|
|
IsPrimary: s.srv.config.Datacenter == s.srv.config.PrimaryDatacenter,
|
|
|
|
RawConfig: args.Config.Config,
|
|
|
|
State: args.Config.State,
|
|
|
|
}
|
|
|
|
if err := newProvider.Configure(pCfg); err != nil {
|
2018-09-07 02:18:54 +00:00
|
|
|
return fmt.Errorf("error configuring provider: %v", err)
|
|
|
|
}
|
|
|
|
if err := newProvider.GenerateRoot(); err != nil {
|
|
|
|
return fmt.Errorf("error generating CA root certificate: %v", err)
|
|
|
|
}
|
2018-04-21 01:46:02 +00:00
|
|
|
|
2018-04-24 23:16:37 +00:00
|
|
|
newRootPEM, err := newProvider.ActiveRoot()
|
2018-04-21 01:46:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-15 16:17:48 +00:00
|
|
|
newActiveRoot, err := parseCARoot(newRootPEM, args.Config.Provider, args.Config.ClusterID)
|
2018-04-24 23:16:37 +00:00
|
|
|
if err != nil {
|
2018-05-04 23:01:54 +00:00
|
|
|
return err
|
2018-04-24 23:16:37 +00:00
|
|
|
}
|
|
|
|
|
2019-11-11 20:57:16 +00:00
|
|
|
// 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
|
|
|
|
|
2018-04-21 01:46:02 +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
|
|
|
|
}
|
|
|
|
|
2018-10-15 16:17:48 +00:00
|
|
|
// If the root didn't change or if this is a secondary DC, just update the
|
|
|
|
// config and return.
|
|
|
|
if (s.srv.config.Datacenter != s.srv.config.PrimaryDatacenter) ||
|
|
|
|
root != nil && root.ID == newActiveRoot.ID {
|
2018-04-21 01:46:02 +00:00
|
|
|
args.Op = structs.CAOpSetConfig
|
|
|
|
resp, err := s.srv.raftApply(structs.ConnectCARequestType, args)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if respErr, ok := resp.(error); ok {
|
|
|
|
return respErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the config has been committed, update the local provider instance
|
2018-06-20 11:37:36 +00:00
|
|
|
s.srv.setCAProvider(newProvider, newActiveRoot)
|
2018-04-21 01:46:02 +00:00
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
s.logger.Info("CA provider config updated")
|
2018-04-21 01:46:02 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point, we know the config change has trigged a root rotation,
|
|
|
|
// either by swapping the provider type or changing the provider's config
|
|
|
|
// to use a different root certificate.
|
|
|
|
|
2019-11-11 21:36:22 +00:00
|
|
|
// First up, sanity check that the current provider actually supports
|
|
|
|
// cross-signing.
|
|
|
|
oldProvider, _ := s.srv.getCAProvider()
|
|
|
|
if oldProvider == nil {
|
|
|
|
return fmt.Errorf("internal error: CA provider is nil")
|
|
|
|
}
|
|
|
|
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.")
|
|
|
|
}
|
|
|
|
if !canXSign && args.Config.ForceWithoutCrossSigning {
|
2020-01-28 23:50:41 +00:00
|
|
|
s.logger.Warn("current CA doesn't support cross signing but " +
|
2019-11-11 21:36:22 +00:00
|
|
|
"CA reconfiguration forced anyway with ForceWithoutCrossSigning")
|
|
|
|
}
|
|
|
|
|
2018-04-21 01:46:02 +00:00
|
|
|
// If it's a config change that would trigger a rotation (different provider/root):
|
2018-06-19 23:46:18 +00:00
|
|
|
// 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.
|
|
|
|
newRoot, err := connect.ParseCert(newRootPEM)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-24 23:16:37 +00:00
|
|
|
|
2019-11-11 21:36:22 +00:00
|
|
|
if canXSign {
|
|
|
|
// 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}
|
2018-04-21 03:39:51 +00:00
|
|
|
}
|
|
|
|
|
2018-06-19 23:46:18 +00:00
|
|
|
intermediate, err := newProvider.GenerateIntermediate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if intermediate != newRootPEM {
|
|
|
|
newActiveRoot.IntermediateCerts = append(newActiveRoot.IntermediateCerts, intermediate)
|
|
|
|
}
|
2018-04-21 01:46:02 +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
|
2018-07-06 23:05:25 +00:00
|
|
|
newRoot.RotatedOutAt = time.Now()
|
2018-04-21 01:46:02 +00:00
|
|
|
}
|
|
|
|
newRoots = append(newRoots, &newRoot)
|
|
|
|
}
|
|
|
|
newRoots = append(newRoots, newActiveRoot)
|
|
|
|
|
|
|
|
args.Op = structs.CAOpSetRootsAndConfig
|
|
|
|
args.Index = idx
|
2018-11-07 06:46:06 +00:00
|
|
|
args.Config.ModifyIndex = confIdx
|
2018-04-21 01:46:02 +00:00
|
|
|
args.Roots = newRoots
|
2018-04-09 04:58:31 +00:00
|
|
|
resp, err := s.srv.raftApply(structs.ConnectCARequestType, args)
|
2018-03-21 19:42:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if respErr, ok := resp.(error); ok {
|
|
|
|
return respErr
|
|
|
|
}
|
2018-11-07 06:46:06 +00:00
|
|
|
if respOk, ok := resp.(bool); ok && !respOk {
|
|
|
|
return fmt.Errorf("could not atomically update roots and config")
|
|
|
|
}
|
2018-03-21 19:42:42 +00:00
|
|
|
|
2018-04-21 01:46:02 +00:00
|
|
|
// If the config has been committed, update the local provider instance
|
|
|
|
// and call teardown on the old provider
|
2018-06-20 11:37:36 +00:00
|
|
|
s.srv.setCAProvider(newProvider, newActiveRoot)
|
2018-04-21 01:46:02 +00:00
|
|
|
|
2018-04-24 18:50:31 +00:00
|
|
|
if err := oldProvider.Cleanup(); err != nil {
|
2020-01-28 23:50:41 +00:00
|
|
|
s.logger.Warn("failed to clean up old provider", "provider", config.Provider)
|
2018-04-21 01:46:02 +00:00
|
|
|
}
|
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
s.logger.Info("CA rotated to new root under provider", "provider", args.Config.Provider)
|
2018-04-21 01:46:02 +00:00
|
|
|
|
2018-03-21 19:42:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-17 04:28:27 +00:00
|
|
|
// Roots returns the currently trusted root certificates.
|
|
|
|
func (s *ConnectCA) Roots(
|
|
|
|
args *structs.DCSpecificRequest,
|
|
|
|
reply *structs.IndexedCARoots) error {
|
|
|
|
// Forward if necessary
|
2020-07-07 19:45:08 +00:00
|
|
|
if done, err := s.srv.ForwardRPC("ConnectCA.Roots", args, args, reply); done {
|
2018-03-17 04:28:27 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-07-25 19:26:27 +00:00
|
|
|
// Exit early if Connect hasn't been enabled.
|
|
|
|
if !s.srv.config.ConnectEnabled {
|
|
|
|
return ErrConnectNotEnabled
|
|
|
|
}
|
|
|
|
|
2018-03-17 04:28:27 +00:00
|
|
|
return s.srv.blockingQuery(
|
|
|
|
&args.QueryOptions, &reply.QueryMeta,
|
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
2019-06-26 18:05:00 +00:00
|
|
|
index, roots, config, err := state.CARootsAndConfig(ws)
|
2018-03-17 04:28:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-06-26 18:05:00 +00:00
|
|
|
if config != nil {
|
|
|
|
// Build TrustDomain based on the ClusterID stored.
|
|
|
|
signingID := connect.SpiffeIDSigningForCluster(config)
|
|
|
|
if signingID == nil {
|
|
|
|
// If CA is bootstrapped at all then this should never happen but be
|
|
|
|
// defensive.
|
|
|
|
return errors.New("no cluster trust domain setup")
|
|
|
|
}
|
|
|
|
reply.TrustDomain = signingID.Host()
|
|
|
|
}
|
|
|
|
|
2018-03-17 04:28:27 +00:00
|
|
|
reply.Index, reply.Roots = index, roots
|
|
|
|
if reply.Roots == nil {
|
|
|
|
reply.Roots = make(structs.CARoots, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The API response must NEVER contain the secret information
|
2020-05-29 18:19:16 +00:00
|
|
|
// such as keys and so on. We use an allowlist below to copy the
|
2018-03-17 04:28:27 +00:00
|
|
|
// specific fields we want to expose.
|
|
|
|
for i, r := range reply.Roots {
|
|
|
|
// IMPORTANT: r must NEVER be modified, since it is a pointer
|
|
|
|
// directly to the structure in the memdb store.
|
|
|
|
|
|
|
|
reply.Roots[i] = &structs.CARoot{
|
2018-10-15 16:17:48 +00:00
|
|
|
ID: r.ID,
|
|
|
|
Name: r.Name,
|
|
|
|
SerialNumber: r.SerialNumber,
|
|
|
|
SigningKeyID: r.SigningKeyID,
|
|
|
|
ExternalTrustDomain: r.ExternalTrustDomain,
|
|
|
|
NotBefore: r.NotBefore,
|
|
|
|
NotAfter: r.NotAfter,
|
|
|
|
RootCert: r.RootCert,
|
|
|
|
IntermediateCerts: r.IntermediateCerts,
|
|
|
|
RaftIndex: r.RaftIndex,
|
|
|
|
Active: r.Active,
|
2019-07-30 21:47:39 +00:00
|
|
|
PrivateKeyType: r.PrivateKeyType,
|
|
|
|
PrivateKeyBits: r.PrivateKeyBits,
|
2018-03-20 17:36:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if r.Active {
|
|
|
|
reply.ActiveRootID = r.ID
|
2018-03-17 04:28:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
2018-03-19 21:36:17 +00:00
|
|
|
|
|
|
|
// Sign signs a certificate for a service.
|
|
|
|
func (s *ConnectCA) Sign(
|
|
|
|
args *structs.CASignRequest,
|
2018-03-20 04:00:01 +00:00
|
|
|
reply *structs.IssuedCert) error {
|
2018-04-25 18:34:08 +00:00
|
|
|
// Exit early if Connect hasn't been enabled.
|
|
|
|
if !s.srv.config.ConnectEnabled {
|
|
|
|
return ErrConnectNotEnabled
|
|
|
|
}
|
|
|
|
|
2020-07-07 19:45:08 +00:00
|
|
|
if done, err := s.srv.ForwardRPC("ConnectCA.Sign", args, args, reply); done {
|
2018-04-09 04:58:31 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-03-19 21:36:17 +00:00
|
|
|
// Parse the CSR
|
|
|
|
csr, err := connect.ParseCSR(args.CSR)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-04-30 21:23:49 +00:00
|
|
|
// Parse the SPIFFE ID
|
2018-05-09 13:25:48 +00:00
|
|
|
spiffeID, err := connect.ParseCertURI(csr.URIs[0])
|
2018-04-30 21:23:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-06-20 11:37:36 +00:00
|
|
|
provider, caRoot := s.srv.getCAProvider()
|
2018-05-09 04:30:18 +00:00
|
|
|
if provider == nil {
|
|
|
|
return fmt.Errorf("internal error: CA provider is nil")
|
2019-06-26 18:05:00 +00:00
|
|
|
} else if caRoot == nil {
|
|
|
|
return fmt.Errorf("internal error: CA root is nil")
|
2018-05-09 04:30:18 +00:00
|
|
|
}
|
2018-04-21 03:39:51 +00:00
|
|
|
|
2018-05-09 13:25:48 +00:00
|
|
|
// Verify that the CSR entity is in the cluster's trust domain
|
|
|
|
state := s.srv.fsm.State()
|
2019-08-19 18:03:03 +00:00
|
|
|
_, config, err := state.CAConfig(nil)
|
2018-05-09 13:25:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
signingID := connect.SpiffeIDSigningForCluster(config)
|
2019-06-27 20:22:07 +00:00
|
|
|
serviceID, isService := spiffeID.(*connect.SpiffeIDService)
|
|
|
|
agentID, isAgent := spiffeID.(*connect.SpiffeIDAgent)
|
|
|
|
if !isService && !isAgent {
|
|
|
|
return fmt.Errorf("SPIFFE ID in CSR must be a service or agent ID")
|
|
|
|
}
|
|
|
|
|
|
|
|
if isService {
|
|
|
|
if !signingID.CanSign(spiffeID) {
|
|
|
|
return fmt.Errorf("SPIFFE ID in CSR from a different trust domain: %s, "+
|
|
|
|
"we are %s", serviceID.Host, signingID.Host())
|
|
|
|
}
|
2020-06-30 13:48:42 +00:00
|
|
|
} else {
|
|
|
|
// 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
|
|
|
|
csr.Subject.CommonName = connect.AgentCN(agentID.Agent, 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
|
|
|
|
}
|
2018-05-09 13:25:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that the ACL token provided has permission to act as this service
|
2018-10-19 16:04:07 +00:00
|
|
|
rule, err := s.srv.ResolveToken(args.Token)
|
2018-05-09 13:25:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-10 15:40:44 +00:00
|
|
|
var authzContext acl.AuthorizerContext
|
|
|
|
var entMeta structs.EnterpriseMeta
|
2019-06-27 20:22:07 +00:00
|
|
|
if isService {
|
2020-02-10 15:40:44 +00:00
|
|
|
entMeta.Merge(serviceID.GetEnterpriseMeta())
|
|
|
|
entMeta.FillAuthzContext(&authzContext)
|
|
|
|
if rule != nil && rule.ServiceWrite(serviceID.Service, &authzContext) != acl.Allow {
|
2019-06-27 20:22:07 +00:00
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
2018-05-09 13:25:48 +00:00
|
|
|
|
2019-06-27 20:22:07 +00:00
|
|
|
// Verify that the DC in the service URI matches us. We might relax this
|
|
|
|
// requirement later but being restrictive for now is safer.
|
|
|
|
if serviceID.Datacenter != s.srv.config.Datacenter {
|
|
|
|
return fmt.Errorf("SPIFFE ID in CSR from a different datacenter: %s, "+
|
|
|
|
"we are %s", serviceID.Datacenter, s.srv.config.Datacenter)
|
|
|
|
}
|
|
|
|
} else if isAgent {
|
2020-02-10 15:40:44 +00:00
|
|
|
structs.DefaultEnterpriseMeta().FillAuthzContext(&authzContext)
|
|
|
|
if rule != nil && rule.NodeWrite(agentID.Agent, &authzContext) != acl.Allow {
|
2019-06-27 20:22:07 +00:00
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
2018-05-09 13:25:48 +00:00
|
|
|
}
|
|
|
|
|
2019-01-22 17:19:36 +00:00
|
|
|
commonCfg, err := config.GetCommonConfig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if commonCfg.CSRMaxPerSecond > 0 {
|
2020-06-29 19:52:47 +00:00
|
|
|
lim := s.srv.caLeafLimiter.getCSRRateLimiterWithLimit(rate.Limit(commonCfg.CSRMaxPerSecond))
|
2019-01-22 17:19:36 +00:00
|
|
|
// 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 ErrRateLimited
|
|
|
|
}
|
|
|
|
} else if commonCfg.CSRMaxConcurrent > 0 {
|
2020-06-29 19:52:47 +00:00
|
|
|
s.srv.caLeafLimiter.csrConcurrencyLimiter.SetSize(int64(commonCfg.CSRMaxConcurrent))
|
2019-01-22 17:19:36 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), csrLimitWait)
|
|
|
|
defer cancel()
|
2020-06-29 19:52:47 +00:00
|
|
|
if err := s.srv.caLeafLimiter.csrConcurrencyLimiter.Acquire(ctx); err != nil {
|
2019-01-22 17:19:36 +00:00
|
|
|
return ErrRateLimited
|
|
|
|
}
|
2020-06-29 19:52:47 +00:00
|
|
|
defer s.srv.caLeafLimiter.csrConcurrencyLimiter.Release()
|
2019-01-22 17:19:36 +00:00
|
|
|
}
|
|
|
|
|
2018-05-09 13:25:48 +00:00
|
|
|
// All seems to be in order, actually sign it.
|
2018-04-24 23:31:42 +00:00
|
|
|
pem, err := provider.Sign(csr)
|
2019-11-21 17:40:29 +00:00
|
|
|
if err == ca.ErrRateLimited {
|
|
|
|
return ErrRateLimited
|
|
|
|
}
|
2018-04-24 23:31:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-06-20 11:37:36 +00:00
|
|
|
// Append any intermediates needed by this root.
|
|
|
|
for _, p := range caRoot.IntermediateCerts {
|
|
|
|
pem = strings.TrimSpace(pem) + "\n" + p
|
|
|
|
}
|
|
|
|
|
2018-11-07 06:46:06 +00:00
|
|
|
// Append our local CA's intermediate if there is one.
|
|
|
|
inter, err := provider.ActiveIntermediate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
root, err := provider.ActiveRoot()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if inter != root {
|
|
|
|
pem = strings.TrimSpace(pem) + "\n" + inter
|
|
|
|
}
|
|
|
|
|
2018-04-30 21:23:49 +00:00
|
|
|
// TODO(banks): when we implement IssuedCerts table we can use the insert to
|
2019-01-11 21:04:57 +00:00
|
|
|
// 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: s.srv.config.Datacenter,
|
|
|
|
WriteRequest: structs.WriteRequest{Token: args.Token},
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := s.srv.raftApply(structs.ConnectCALeafRequestType|structs.IgnoreUnknownTypeFlag, &req)
|
2018-04-24 23:31:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-30 21:23:49 +00:00
|
|
|
|
2019-01-11 21:04:57 +00:00
|
|
|
modIdx, ok := resp.(uint64)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Invalid response from updating the leaf cert index")
|
|
|
|
}
|
|
|
|
|
2018-04-24 23:31:42 +00:00
|
|
|
cert, err := connect.ParseCert(pem)
|
2018-03-19 21:36:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-03-20 04:00:01 +00:00
|
|
|
// Set the response
|
2018-04-30 03:44:40 +00:00
|
|
|
*reply = structs.IssuedCert{
|
2020-02-10 15:40:44 +00:00
|
|
|
SerialNumber: connect.EncodeSerialNumber(cert.SerialNumber),
|
|
|
|
CertPEM: pem,
|
|
|
|
ValidAfter: cert.NotBefore,
|
|
|
|
ValidBefore: cert.NotAfter,
|
|
|
|
EnterpriseMeta: entMeta,
|
2018-04-30 21:23:49 +00:00
|
|
|
RaftIndex: structs.RaftIndex{
|
|
|
|
ModifyIndex: modIdx,
|
|
|
|
CreateIndex: modIdx,
|
|
|
|
},
|
2018-04-24 23:31:42 +00:00
|
|
|
}
|
2019-06-27 20:22:07 +00:00
|
|
|
if isService {
|
|
|
|
reply.Service = serviceID.Service
|
|
|
|
reply.ServiceURI = cert.URIs[0].String()
|
|
|
|
} else if isAgent {
|
|
|
|
reply.Agent = agentID.Agent
|
|
|
|
reply.AgentURI = cert.URIs[0].String()
|
|
|
|
}
|
2018-03-20 04:00:01 +00:00
|
|
|
|
2018-03-19 21:36:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
2019-06-24 18:21:51 +00:00
|
|
|
|
|
|
|
// SignIntermediate signs an intermediate certificate for a remote datacenter.
|
|
|
|
func (s *ConnectCA) SignIntermediate(
|
|
|
|
args *structs.CASignRequest,
|
|
|
|
reply *string) error {
|
|
|
|
// Exit early if Connect hasn't been enabled.
|
|
|
|
if !s.srv.config.ConnectEnabled {
|
|
|
|
return ErrConnectNotEnabled
|
|
|
|
}
|
|
|
|
|
2020-07-07 19:45:08 +00:00
|
|
|
if done, err := s.srv.ForwardRPC("ConnectCA.SignIntermediate", args, args, reply); done {
|
2019-06-24 18:21:51 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify we are allowed to serve this request
|
|
|
|
if s.srv.config.PrimaryDatacenter != s.srv.config.Datacenter {
|
|
|
|
return ErrNotPrimaryDatacenter
|
|
|
|
}
|
|
|
|
|
|
|
|
// This action requires operator write access.
|
|
|
|
rule, err := s.srv.ResolveToken(args.Token)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-15 20:58:50 +00:00
|
|
|
if rule != nil && rule.OperatorWrite(nil) != acl.Allow {
|
2019-06-24 18:21:51 +00:00
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
provider, _ := s.srv.getCAProvider()
|
|
|
|
if provider == nil {
|
|
|
|
return fmt.Errorf("internal error: CA provider is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
csr, err := connect.ParseCSR(args.CSR)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cert, err := provider.SignIntermediate(csr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*reply = cert
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|