2018-03-17 04:28:27 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2018-04-25 18:34:08 +00:00
|
|
|
"errors"
|
2018-03-19 21:36:17 +00:00
|
|
|
"fmt"
|
2018-04-21 01:46:02 +00:00
|
|
|
"reflect"
|
2018-06-20 11:37:36 +00:00
|
|
|
"strings"
|
2018-03-19 21:36:17 +00:00
|
|
|
|
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"
|
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"
|
|
|
|
)
|
|
|
|
|
2018-04-25 18:34:08 +00:00
|
|
|
var ErrConnectNotEnabled = errors.New("Connect must be enabled in order to use this endpoint")
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-04-09 04:58:31 +00:00
|
|
|
if done, err := s.srv.forward("ConnectCA.ConfigurationGet", args, args, reply); done {
|
|
|
|
return err
|
2018-03-21 19:42:42 +00:00
|
|
|
}
|
|
|
|
|
2018-04-09 04:58:31 +00:00
|
|
|
// This action requires operator read access.
|
|
|
|
rule, err := s.srv.resolveToken(args.Token)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-03-21 19:42:42 +00:00
|
|
|
}
|
2018-04-09 04:58:31 +00:00
|
|
|
if rule != nil && !rule.OperatorRead() {
|
|
|
|
return acl.ErrPermissionDenied
|
2018-03-21 19:42:42 +00:00
|
|
|
}
|
|
|
|
|
2018-04-09 04:58:31 +00:00
|
|
|
state := s.srv.fsm.State()
|
|
|
|
_, config, err := state.CAConfig()
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-04-09 04:58:31 +00:00
|
|
|
if done, err := s.srv.forward("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-04-09 04:58:31 +00:00
|
|
|
rule, err := s.srv.resolveToken(args.Token)
|
2018-03-21 19:42:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-09 04:58:31 +00:00
|
|
|
if rule != nil && !rule.OperatorWrite() {
|
|
|
|
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()
|
|
|
|
_, config, err := state.CAConfig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
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-05-04 23:01:54 +00:00
|
|
|
newActiveRoot, err := parseCARoot(newRootPEM, args.Config.Provider)
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
if root != nil && root.ID == newActiveRoot.ID {
|
|
|
|
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
|
|
|
|
2018-04-30 03:44:40 +00:00
|
|
|
s.srv.logger.Printf("[INFO] connect: 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.
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
// Have the old provider cross-sign the new intermediate
|
2018-06-20 11:37:36 +00:00
|
|
|
oldProvider, _ := s.srv.getCAProvider()
|
2018-05-09 04:30:18 +00:00
|
|
|
if oldProvider == nil {
|
|
|
|
return fmt.Errorf("internal error: CA provider is nil")
|
|
|
|
}
|
2018-06-19 23:46:18 +00:00
|
|
|
xcCert, err := oldProvider.CrossSignCA(newRoot)
|
2018-04-21 01:46:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-04-21 03:39:51 +00:00
|
|
|
}
|
|
|
|
|
2018-06-19 23:46:18 +00:00
|
|
|
// Add the cross signed cert to the new root's intermediates.
|
2018-04-24 18:50:31 +00:00
|
|
|
newActiveRoot.IntermediateCerts = []string{xcCert}
|
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
|
|
|
|
}
|
|
|
|
newRoots = append(newRoots, &newRoot)
|
|
|
|
}
|
|
|
|
newRoots = append(newRoots, newActiveRoot)
|
|
|
|
|
|
|
|
args.Op = structs.CAOpSetRootsAndConfig
|
|
|
|
args.Index = idx
|
|
|
|
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-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 {
|
|
|
|
s.srv.logger.Printf("[WARN] connect: failed to clean up old provider %q", config.Provider)
|
2018-04-21 01:46:02 +00:00
|
|
|
}
|
|
|
|
|
2018-04-24 18:50:31 +00:00
|
|
|
s.srv.logger.Printf("[INFO] connect: CA rotated to new root under provider %q", 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
|
|
|
|
if done, err := s.srv.forward("ConnectCA.Roots", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-05-08 13:23:44 +00:00
|
|
|
// Load the ClusterID to generate TrustDomain. We do this outside the loop
|
|
|
|
// since by definition this value should be immutable once set for lifetime of
|
|
|
|
// the cluster so we don't need to look it up more than once. We also don't
|
|
|
|
// have to worry about non-atomicity between the config fetch transaction and
|
|
|
|
// the CARoots transaction below since this field must remain immutable. Do
|
|
|
|
// not re-use this state/config for other logic that might care about changes
|
|
|
|
// of config during the blocking query below.
|
|
|
|
{
|
|
|
|
state := s.srv.fsm.State()
|
|
|
|
_, config, err := state.CAConfig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-05-09 16:15:29 +00:00
|
|
|
// Check CA is actually bootstrapped...
|
|
|
|
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-05-08 13:23:44 +00:00
|
|
|
}
|
|
|
|
|
2018-03-17 04:28:27 +00:00
|
|
|
return s.srv.blockingQuery(
|
|
|
|
&args.QueryOptions, &reply.QueryMeta,
|
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
|
|
|
index, roots, err := state.CARoots(ws)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
// such as keys and so on. We use a whitelist below to copy the
|
|
|
|
// 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-04-24 18:50:31 +00:00
|
|
|
ID: r.ID,
|
|
|
|
Name: r.Name,
|
2018-05-04 23:01:54 +00:00
|
|
|
SerialNumber: r.SerialNumber,
|
|
|
|
SigningKeyID: r.SigningKeyID,
|
|
|
|
NotBefore: r.NotBefore,
|
|
|
|
NotAfter: r.NotAfter,
|
2018-04-24 18:50:31 +00:00
|
|
|
RootCert: r.RootCert,
|
|
|
|
IntermediateCerts: r.IntermediateCerts,
|
|
|
|
RaftIndex: r.RaftIndex,
|
|
|
|
Active: r.Active,
|
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
|
|
|
|
}
|
|
|
|
|
2018-04-09 04:58:31 +00:00
|
|
|
if done, err := s.srv.forward("ConnectCA.Sign", args, args, reply); done {
|
|
|
|
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-05-09 13:25:48 +00:00
|
|
|
serviceID, ok := spiffeID.(*connect.SpiffeIDService)
|
2018-04-30 21:23:49 +00:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("SPIFFE ID in CSR must be a service ID")
|
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
}
|
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()
|
|
|
|
_, config, err := state.CAConfig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
signingID := connect.SpiffeIDSigningForCluster(config)
|
|
|
|
if !signingID.CanSign(serviceID) {
|
|
|
|
return fmt.Errorf("SPIFFE ID in CSR from a different trust domain: %s, "+
|
|
|
|
"we are %s", serviceID.Host, signingID.Host())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that the ACL token provided has permission to act as this service
|
|
|
|
rule, err := s.srv.resolveToken(args.Token)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if rule != nil && !rule.ServiceWrite(serviceID.Service, nil) {
|
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// All seems to be in order, actually sign it.
|
2018-04-24 23:31:42 +00:00
|
|
|
pem, err := provider.Sign(csr)
|
|
|
|
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-04-30 21:23:49 +00:00
|
|
|
// TODO(banks): when we implement IssuedCerts table we can use the insert to
|
|
|
|
// that as the raft index to return in response. Right now we can rely on only
|
|
|
|
// the built-in provider being supported and the implementation detail that we
|
|
|
|
// have to write a SerialIndex update to the provider config table for every
|
|
|
|
// cert issued so in all cases this index will be higher than any previous
|
2018-05-09 13:25:48 +00:00
|
|
|
// sign response. This has to be reloaded after the provider.Sign call to
|
|
|
|
// observe the index update.
|
|
|
|
state = s.srv.fsm.State()
|
|
|
|
modIdx, _, err := state.CAConfig()
|
2018-04-24 23:31:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-30 21:23:49 +00:00
|
|
|
|
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{
|
2018-04-24 23:31:42 +00:00
|
|
|
SerialNumber: connect.HexString(cert.SerialNumber.Bytes()),
|
|
|
|
CertPEM: pem,
|
2018-05-09 13:25:48 +00:00
|
|
|
Service: serviceID.Service,
|
2018-04-24 23:31:42 +00:00
|
|
|
ServiceURI: cert.URIs[0].String(),
|
|
|
|
ValidAfter: cert.NotBefore,
|
|
|
|
ValidBefore: cert.NotAfter,
|
2018-04-30 21:23:49 +00:00
|
|
|
RaftIndex: structs.RaftIndex{
|
|
|
|
ModifyIndex: modIdx,
|
|
|
|
CreateIndex: modIdx,
|
|
|
|
},
|
2018-04-24 23:31:42 +00:00
|
|
|
}
|
2018-03-20 04:00:01 +00:00
|
|
|
|
2018-03-19 21:36:17 +00:00
|
|
|
return nil
|
|
|
|
}
|