2018-03-17 04:20:54 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-07-22 14:32:27 +00:00
|
|
|
|
2018-03-17 04:20:54 +00:00
|
|
|
"github.com/hashicorp/go-memdb"
|
2021-07-22 14:32:27 +00:00
|
|
|
"github.com/pkg/errors"
|
2021-01-29 01:53:21 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2018-03-17 04:20:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-01-29 01:53:21 +00:00
|
|
|
tableConnectCABuiltin = "connect-ca-builtin"
|
|
|
|
tableConnectCABuiltinSerial = "connect-ca-builtin-serial"
|
|
|
|
tableConnectCAConfig = "connect-ca-config"
|
|
|
|
tableConnectCARoots = "connect-ca-roots"
|
|
|
|
tableConnectCALeafCerts = "connect-ca-leaf-certs"
|
2018-03-17 04:20:54 +00:00
|
|
|
)
|
|
|
|
|
2018-04-24 18:50:31 +00:00
|
|
|
// caBuiltinProviderTableSchema returns a new table schema used for storing
|
|
|
|
// the built-in CA provider's state for connect. This is only used by
|
|
|
|
// the internal Consul CA provider.
|
|
|
|
func caBuiltinProviderTableSchema() *memdb.TableSchema {
|
|
|
|
return &memdb.TableSchema{
|
2021-01-29 01:53:21 +00:00
|
|
|
Name: tableConnectCABuiltin,
|
2018-04-24 18:50:31 +00:00
|
|
|
Indexes: map[string]*memdb.IndexSchema{
|
2020-06-16 17:19:31 +00:00
|
|
|
"id": {
|
2018-04-24 18:50:31 +00:00
|
|
|
Name: "id",
|
|
|
|
AllowMissing: false,
|
|
|
|
Unique: true,
|
|
|
|
Indexer: &memdb.StringFieldIndex{
|
|
|
|
Field: "ID",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-07 00:13:22 +00:00
|
|
|
// caConfigTableSchema returns a new table schema used for storing
|
|
|
|
// the CA config for Connect.
|
|
|
|
func caConfigTableSchema() *memdb.TableSchema {
|
|
|
|
return &memdb.TableSchema{
|
2021-01-29 01:53:21 +00:00
|
|
|
Name: tableConnectCAConfig,
|
2018-04-07 00:13:22 +00:00
|
|
|
Indexes: map[string]*memdb.IndexSchema{
|
2018-04-24 18:50:31 +00:00
|
|
|
// This table only stores one row, so this just ignores the ID field
|
|
|
|
// and always overwrites the same config object.
|
2020-06-16 17:19:31 +00:00
|
|
|
"id": {
|
2018-04-07 00:13:22 +00:00
|
|
|
Name: "id",
|
|
|
|
AllowMissing: true,
|
|
|
|
Unique: true,
|
|
|
|
Indexer: &memdb.ConditionalIndex{
|
|
|
|
Conditional: func(obj interface{}) (bool, error) { return true, nil },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-17 04:20:54 +00:00
|
|
|
// caRootTableSchema returns a new table schema used for storing
|
|
|
|
// CA roots for Connect.
|
|
|
|
func caRootTableSchema() *memdb.TableSchema {
|
|
|
|
return &memdb.TableSchema{
|
2021-01-29 01:53:21 +00:00
|
|
|
Name: tableConnectCARoots,
|
2018-03-17 04:20:54 +00:00
|
|
|
Indexes: map[string]*memdb.IndexSchema{
|
2020-06-16 17:19:31 +00:00
|
|
|
"id": {
|
2018-03-17 04:20:54 +00:00
|
|
|
Name: "id",
|
|
|
|
AllowMissing: false,
|
|
|
|
Unique: true,
|
2018-04-24 23:16:37 +00:00
|
|
|
Indexer: &memdb.StringFieldIndex{
|
2018-03-17 04:20:54 +00:00
|
|
|
Field: "ID",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-07 00:13:22 +00:00
|
|
|
// CAConfig is used to pull the CA config from the snapshot.
|
|
|
|
func (s *Snapshot) CAConfig() (*structs.CAConfiguration, error) {
|
2021-01-29 01:53:21 +00:00
|
|
|
c, err := s.tx.First(tableConnectCAConfig, "id")
|
2018-04-07 00:13:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
config, ok := c.(*structs.CAConfiguration)
|
|
|
|
if !ok {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CAConfig is used when restoring from a snapshot.
|
|
|
|
func (s *Restore) CAConfig(config *structs.CAConfiguration) error {
|
2018-12-06 02:27:20 +00:00
|
|
|
// Don't restore a blank CA config
|
|
|
|
// https://github.com/hashicorp/consul/issues/4954
|
|
|
|
if config.Provider == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-29 01:53:21 +00:00
|
|
|
if err := s.tx.Insert(tableConnectCAConfig, config); err != nil {
|
2018-04-07 00:13:22 +00:00
|
|
|
return fmt.Errorf("failed restoring CA config: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-21 01:46:02 +00:00
|
|
|
// CAConfig is used to get the current CA configuration.
|
2019-08-19 18:03:03 +00:00
|
|
|
func (s *Store) CAConfig(ws memdb.WatchSet) (uint64, *structs.CAConfiguration, error) {
|
2018-04-07 00:13:22 +00:00
|
|
|
tx := s.db.Txn(false)
|
|
|
|
defer tx.Abort()
|
|
|
|
|
2020-07-10 00:56:43 +00:00
|
|
|
return caConfigTxn(tx, ws)
|
2019-06-26 18:05:00 +00:00
|
|
|
}
|
|
|
|
|
2020-09-03 23:38:03 +00:00
|
|
|
func caConfigTxn(tx ReadTxn, ws memdb.WatchSet) (uint64, *structs.CAConfiguration, error) {
|
2018-04-21 01:46:02 +00:00
|
|
|
// Get the CA config
|
2021-01-29 01:53:21 +00:00
|
|
|
ch, c, err := tx.FirstWatch(tableConnectCAConfig, "id")
|
2018-04-07 00:13:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, nil, fmt.Errorf("failed CA config lookup: %s", err)
|
|
|
|
}
|
2019-06-18 00:52:01 +00:00
|
|
|
|
2019-06-26 18:05:00 +00:00
|
|
|
ws.Add(ch)
|
2018-04-07 00:13:22 +00:00
|
|
|
|
|
|
|
config, ok := c.(*structs.CAConfiguration)
|
|
|
|
if !ok {
|
|
|
|
return 0, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return config.ModifyIndex, config, nil
|
|
|
|
}
|
|
|
|
|
2018-04-21 01:46:02 +00:00
|
|
|
// CASetConfig is used to set the current CA configuration.
|
2018-04-07 00:13:22 +00:00
|
|
|
func (s *Store) CASetConfig(idx uint64, config *structs.CAConfiguration) error {
|
2020-03-19 13:11:20 +00:00
|
|
|
tx := s.db.WriteTxn(idx)
|
2018-04-07 00:13:22 +00:00
|
|
|
defer tx.Abort()
|
|
|
|
|
2018-04-20 08:30:34 +00:00
|
|
|
if err := s.caSetConfigTxn(idx, tx, config); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-07 00:13:22 +00:00
|
|
|
|
2020-06-02 20:34:56 +00:00
|
|
|
return tx.Commit()
|
2018-04-07 00:13:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CACheckAndSetConfig is used to try updating the CA configuration with a
|
|
|
|
// given Raft index. If the CAS index specified is not equal to the last observed index
|
2021-07-22 14:32:27 +00:00
|
|
|
// for the config, then the call will return an error,
|
2018-04-07 00:13:22 +00:00
|
|
|
func (s *Store) CACheckAndSetConfig(idx, cidx uint64, config *structs.CAConfiguration) (bool, error) {
|
2020-03-19 13:11:20 +00:00
|
|
|
tx := s.db.WriteTxn(idx)
|
2018-04-07 00:13:22 +00:00
|
|
|
defer tx.Abort()
|
|
|
|
|
|
|
|
// Check for an existing config
|
2021-01-29 01:53:21 +00:00
|
|
|
existing, err := tx.First(tableConnectCAConfig, "id")
|
2018-04-07 00:13:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("failed CA config lookup: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the existing index does not match the provided CAS
|
|
|
|
// index arg, then we shouldn't update anything and can safely
|
|
|
|
// return early here.
|
|
|
|
e, ok := existing.(*structs.CAConfiguration)
|
2019-06-26 18:05:00 +00:00
|
|
|
if (ok && e.ModifyIndex != cidx) || (!ok && cidx != 0) {
|
2021-07-22 14:32:27 +00:00
|
|
|
return false, errors.Errorf("ModifyIndex did not match existing")
|
2018-04-07 00:13:22 +00:00
|
|
|
}
|
|
|
|
|
2018-04-20 08:30:34 +00:00
|
|
|
if err := s.caSetConfigTxn(idx, tx, config); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2018-04-07 00:13:22 +00:00
|
|
|
|
2020-06-02 20:34:56 +00:00
|
|
|
err = tx.Commit()
|
|
|
|
return err == nil, err
|
2018-04-07 00:13:22 +00:00
|
|
|
}
|
|
|
|
|
2021-03-08 22:41:10 +00:00
|
|
|
func (s *Store) caSetConfigTxn(idx uint64, tx WriteTxn, config *structs.CAConfiguration) error {
|
2018-04-07 00:13:22 +00:00
|
|
|
// Check for an existing config
|
2021-01-29 01:53:21 +00:00
|
|
|
prev, err := tx.First(tableConnectCAConfig, "id")
|
2018-04-07 00:13:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed CA config lookup: %s", err)
|
|
|
|
}
|
2018-04-20 08:30:34 +00:00
|
|
|
// Set the indexes, prevent the cluster ID from changing.
|
|
|
|
if prev != nil {
|
|
|
|
existing := prev.(*structs.CAConfiguration)
|
|
|
|
config.CreateIndex = existing.CreateIndex
|
2018-11-07 06:46:06 +00:00
|
|
|
if config.ClusterID == "" {
|
|
|
|
config.ClusterID = existing.ClusterID
|
|
|
|
}
|
2018-04-07 00:13:22 +00:00
|
|
|
} else {
|
|
|
|
config.CreateIndex = idx
|
|
|
|
}
|
|
|
|
config.ModifyIndex = idx
|
|
|
|
|
2021-01-29 01:53:21 +00:00
|
|
|
if err := tx.Insert(tableConnectCAConfig, config); err != nil {
|
2018-04-07 00:13:22 +00:00
|
|
|
return fmt.Errorf("failed updating CA config: %s", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-21 18:33:19 +00:00
|
|
|
// CARoots is used to pull all the CA roots for the snapshot.
|
|
|
|
func (s *Snapshot) CARoots() (structs.CARoots, error) {
|
2021-01-29 01:53:21 +00:00
|
|
|
ixns, err := s.tx.Get(tableConnectCARoots, "id")
|
2018-03-21 18:33:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var ret structs.CARoots
|
|
|
|
for wrapped := ixns.Next(); wrapped != nil; wrapped = ixns.Next() {
|
|
|
|
ret = append(ret, wrapped.(*structs.CARoot))
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CARoots is used when restoring from a snapshot.
|
|
|
|
func (s *Restore) CARoot(r *structs.CARoot) error {
|
|
|
|
// Insert
|
2021-01-29 01:53:21 +00:00
|
|
|
if err := s.tx.Insert(tableConnectCARoots, r); err != nil {
|
2018-03-21 18:33:19 +00:00
|
|
|
return fmt.Errorf("failed restoring CA root: %s", err)
|
|
|
|
}
|
2021-01-29 01:53:21 +00:00
|
|
|
if err := indexUpdateMaxTxn(s.tx, r.ModifyIndex, tableConnectCARoots); err != nil {
|
2018-03-21 18:33:19 +00:00
|
|
|
return fmt.Errorf("failed updating index: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-17 04:20:54 +00:00
|
|
|
// CARoots returns the list of all CA roots.
|
|
|
|
func (s *Store) CARoots(ws memdb.WatchSet) (uint64, structs.CARoots, error) {
|
|
|
|
tx := s.db.Txn(false)
|
|
|
|
defer tx.Abort()
|
|
|
|
|
2020-07-10 00:56:43 +00:00
|
|
|
return caRootsTxn(tx, ws)
|
2019-06-26 18:05:00 +00:00
|
|
|
}
|
|
|
|
|
2020-09-03 23:38:03 +00:00
|
|
|
func caRootsTxn(tx ReadTxn, ws memdb.WatchSet) (uint64, structs.CARoots, error) {
|
2018-03-17 04:20:54 +00:00
|
|
|
// Get the index
|
2021-01-29 01:53:21 +00:00
|
|
|
idx := maxIndexTxn(tx, tableConnectCARoots)
|
2018-03-17 04:20:54 +00:00
|
|
|
|
|
|
|
// Get all
|
2021-01-29 01:53:21 +00:00
|
|
|
iter, err := tx.Get(tableConnectCARoots, "id")
|
2018-03-17 04:20:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, nil, fmt.Errorf("failed CA root lookup: %s", err)
|
|
|
|
}
|
|
|
|
ws.Add(iter.WatchCh())
|
|
|
|
|
|
|
|
var results structs.CARoots
|
|
|
|
for v := iter.Next(); v != nil; v = iter.Next() {
|
|
|
|
results = append(results, v.(*structs.CARoot))
|
|
|
|
}
|
|
|
|
return idx, results, nil
|
|
|
|
}
|
|
|
|
|
2018-03-20 03:29:14 +00:00
|
|
|
// CARootActive returns the currently active CARoot.
|
|
|
|
func (s *Store) CARootActive(ws memdb.WatchSet) (uint64, *structs.CARoot, error) {
|
|
|
|
// Get all the roots since there should never be that many and just
|
|
|
|
// do the filtering in this method.
|
|
|
|
idx, roots, err := s.CARoots(ws)
|
2021-11-28 19:11:03 +00:00
|
|
|
return idx, roots.Active(), err
|
2018-03-20 03:29:14 +00:00
|
|
|
}
|
|
|
|
|
2018-03-21 17:10:53 +00:00
|
|
|
// CARootSetCAS sets the current CA root state using a check-and-set operation.
|
|
|
|
// On success, this will replace the previous set of CARoots completely with
|
|
|
|
// the given set of roots.
|
2018-03-17 04:20:54 +00:00
|
|
|
//
|
2018-03-21 17:10:53 +00:00
|
|
|
// The first boolean result returns whether the transaction succeeded or not.
|
|
|
|
func (s *Store) CARootSetCAS(idx, cidx uint64, rs []*structs.CARoot) (bool, error) {
|
2020-03-19 13:11:20 +00:00
|
|
|
tx := s.db.WriteTxn(idx)
|
2018-03-17 04:20:54 +00:00
|
|
|
defer tx.Abort()
|
|
|
|
|
2018-03-27 03:38:39 +00:00
|
|
|
// There must be exactly one active CA root.
|
|
|
|
activeCount := 0
|
|
|
|
for _, r := range rs {
|
|
|
|
if r.Active {
|
|
|
|
activeCount++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if activeCount != 1 {
|
|
|
|
return false, fmt.Errorf("there must be exactly one active CA")
|
|
|
|
}
|
|
|
|
|
2018-03-21 17:10:53 +00:00
|
|
|
// Get the current max index
|
2021-01-29 01:53:21 +00:00
|
|
|
if midx := maxIndexTxn(tx, tableConnectCARoots); midx != cidx {
|
2018-03-21 17:10:53 +00:00
|
|
|
return false, nil
|
2018-03-17 04:20:54 +00:00
|
|
|
}
|
|
|
|
|
2018-03-21 17:10:53 +00:00
|
|
|
// Go through and find any existing matching CAs so we can preserve and
|
|
|
|
// update their Create/ModifyIndex values.
|
|
|
|
for _, r := range rs {
|
|
|
|
if r.ID == "" {
|
|
|
|
return false, ErrMissingCARootID
|
|
|
|
}
|
|
|
|
|
2021-01-29 01:53:21 +00:00
|
|
|
existing, err := tx.First(tableConnectCARoots, "id", r.ID)
|
2018-03-21 17:10:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("failed CA root lookup: %s", err)
|
|
|
|
}
|
2018-03-17 04:20:54 +00:00
|
|
|
|
2018-03-21 17:10:53 +00:00
|
|
|
if existing != nil {
|
|
|
|
r.CreateIndex = existing.(*structs.CARoot).CreateIndex
|
|
|
|
} else {
|
|
|
|
r.CreateIndex = idx
|
|
|
|
}
|
|
|
|
r.ModifyIndex = idx
|
2018-03-17 04:20:54 +00:00
|
|
|
}
|
|
|
|
|
2018-03-21 17:10:53 +00:00
|
|
|
// Delete all
|
2021-01-29 01:53:21 +00:00
|
|
|
_, err := tx.DeleteAll(tableConnectCARoots, "id")
|
2018-03-17 04:20:54 +00:00
|
|
|
if err != nil {
|
2018-03-21 17:10:53 +00:00
|
|
|
return false, err
|
2018-03-17 04:20:54 +00:00
|
|
|
}
|
|
|
|
|
2018-03-21 17:10:53 +00:00
|
|
|
// Insert all
|
|
|
|
for _, r := range rs {
|
2021-01-29 01:53:21 +00:00
|
|
|
if err := tx.Insert(tableConnectCARoots, r); err != nil {
|
2018-03-21 17:10:53 +00:00
|
|
|
return false, err
|
|
|
|
}
|
2018-03-17 04:20:54 +00:00
|
|
|
}
|
2018-03-21 17:10:53 +00:00
|
|
|
|
|
|
|
// Update the index
|
2021-03-08 18:02:41 +00:00
|
|
|
if err := tx.Insert(tableIndex, &IndexEntry{tableConnectCARoots, idx}); err != nil {
|
2018-03-21 17:10:53 +00:00
|
|
|
return false, fmt.Errorf("failed updating index: %s", err)
|
2018-03-17 04:20:54 +00:00
|
|
|
}
|
|
|
|
|
2020-06-02 20:34:56 +00:00
|
|
|
err = tx.Commit()
|
|
|
|
return err == nil, err
|
2018-03-17 04:20:54 +00:00
|
|
|
}
|
2018-04-20 08:30:34 +00:00
|
|
|
|
2018-04-27 06:28:27 +00:00
|
|
|
// CAProviderState is used to pull the built-in provider states from the snapshot.
|
|
|
|
func (s *Snapshot) CAProviderState() ([]*structs.CAConsulProviderState, error) {
|
2021-01-29 01:53:21 +00:00
|
|
|
ixns, err := s.tx.Get(tableConnectCABuiltin, "id")
|
2018-04-20 08:30:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-04-27 06:28:27 +00:00
|
|
|
var ret []*structs.CAConsulProviderState
|
|
|
|
for wrapped := ixns.Next(); wrapped != nil; wrapped = ixns.Next() {
|
|
|
|
ret = append(ret, wrapped.(*structs.CAConsulProviderState))
|
2018-04-20 08:30:34 +00:00
|
|
|
}
|
|
|
|
|
2018-04-27 06:28:27 +00:00
|
|
|
return ret, nil
|
2018-04-20 08:30:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CAProviderState is used when restoring from a snapshot.
|
|
|
|
func (s *Restore) CAProviderState(state *structs.CAConsulProviderState) error {
|
2021-01-29 01:53:21 +00:00
|
|
|
if err := s.tx.Insert(tableConnectCABuiltin, state); err != nil {
|
2018-04-20 08:30:34 +00:00
|
|
|
return fmt.Errorf("failed restoring built-in CA state: %s", err)
|
|
|
|
}
|
2021-01-29 01:53:21 +00:00
|
|
|
if err := indexUpdateMaxTxn(s.tx, state.ModifyIndex, tableConnectCABuiltin); err != nil {
|
2018-04-27 06:28:27 +00:00
|
|
|
return fmt.Errorf("failed updating index: %s", err)
|
|
|
|
}
|
2018-04-20 08:30:34 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-21 01:46:02 +00:00
|
|
|
// CAProviderState is used to get the Consul CA provider state for the given ID.
|
|
|
|
func (s *Store) CAProviderState(id string) (uint64, *structs.CAConsulProviderState, error) {
|
2018-04-20 08:30:34 +00:00
|
|
|
tx := s.db.Txn(false)
|
|
|
|
defer tx.Abort()
|
|
|
|
|
2018-04-21 01:46:02 +00:00
|
|
|
// Get the index
|
2021-01-29 01:53:21 +00:00
|
|
|
idx := maxIndexTxn(tx, tableConnectCABuiltin)
|
2018-04-21 01:46:02 +00:00
|
|
|
|
|
|
|
// Get the provider config
|
2021-01-29 01:53:21 +00:00
|
|
|
c, err := tx.First(tableConnectCABuiltin, "id", id)
|
2018-04-20 08:30:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, nil, fmt.Errorf("failed built-in CA state lookup: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
state, ok := c.(*structs.CAConsulProviderState)
|
|
|
|
if !ok {
|
|
|
|
return 0, nil, nil
|
|
|
|
}
|
|
|
|
|
2018-04-21 01:46:02 +00:00
|
|
|
return idx, state, nil
|
|
|
|
}
|
|
|
|
|
2018-04-20 08:30:34 +00:00
|
|
|
// CASetProviderState is used to set the current built-in CA provider state.
|
|
|
|
func (s *Store) CASetProviderState(idx uint64, state *structs.CAConsulProviderState) (bool, error) {
|
2020-03-19 13:11:20 +00:00
|
|
|
tx := s.db.WriteTxn(idx)
|
2018-04-20 08:30:34 +00:00
|
|
|
defer tx.Abort()
|
|
|
|
|
|
|
|
// Check for an existing config
|
2021-01-29 01:53:21 +00:00
|
|
|
existing, err := tx.First(tableConnectCABuiltin, "id", state.ID)
|
2018-04-20 08:30:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("failed built-in CA state lookup: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the indexes.
|
|
|
|
if existing != nil {
|
2018-04-21 01:46:02 +00:00
|
|
|
state.CreateIndex = existing.(*structs.CAConsulProviderState).CreateIndex
|
2018-04-20 08:30:34 +00:00
|
|
|
} else {
|
|
|
|
state.CreateIndex = idx
|
|
|
|
}
|
|
|
|
state.ModifyIndex = idx
|
|
|
|
|
2021-01-29 01:53:21 +00:00
|
|
|
if err := tx.Insert(tableConnectCABuiltin, state); err != nil {
|
2018-04-20 08:30:34 +00:00
|
|
|
return false, fmt.Errorf("failed updating built-in CA state: %s", err)
|
|
|
|
}
|
2018-04-21 01:46:02 +00:00
|
|
|
|
|
|
|
// Update the index
|
2021-03-08 18:02:41 +00:00
|
|
|
if err := tx.Insert(tableIndex, &IndexEntry{tableConnectCABuiltin, idx}); err != nil {
|
2018-04-21 01:46:02 +00:00
|
|
|
return false, fmt.Errorf("failed updating index: %s", err)
|
|
|
|
}
|
|
|
|
|
2020-06-02 20:34:56 +00:00
|
|
|
err = tx.Commit()
|
|
|
|
return err == nil, err
|
2018-04-20 08:30:34 +00:00
|
|
|
}
|
2018-04-21 01:46:02 +00:00
|
|
|
|
2018-04-27 06:28:27 +00:00
|
|
|
// CADeleteProviderState is used to remove the built-in Consul CA provider
|
|
|
|
// state for the given ID.
|
2020-03-19 13:11:20 +00:00
|
|
|
func (s *Store) CADeleteProviderState(idx uint64, id string) error {
|
|
|
|
tx := s.db.WriteTxn(idx)
|
2018-04-21 01:46:02 +00:00
|
|
|
defer tx.Abort()
|
|
|
|
|
|
|
|
// Check for an existing config
|
2021-01-29 01:53:21 +00:00
|
|
|
existing, err := tx.First(tableConnectCABuiltin, "id", id)
|
2018-04-21 01:46:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed built-in CA state lookup: %s", err)
|
|
|
|
}
|
|
|
|
if existing == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
providerState := existing.(*structs.CAConsulProviderState)
|
|
|
|
|
|
|
|
// Do the delete and update the index
|
2021-01-29 01:53:21 +00:00
|
|
|
if err := tx.Delete(tableConnectCABuiltin, providerState); err != nil {
|
2018-04-21 01:46:02 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-03-08 18:02:41 +00:00
|
|
|
if err := tx.Insert(tableIndex, &IndexEntry{tableConnectCABuiltin, idx}); err != nil {
|
2018-04-21 01:46:02 +00:00
|
|
|
return fmt.Errorf("failed updating index: %s", err)
|
|
|
|
}
|
|
|
|
|
2020-06-02 20:34:56 +00:00
|
|
|
return tx.Commit()
|
2018-04-21 01:46:02 +00:00
|
|
|
}
|
2019-01-11 21:04:57 +00:00
|
|
|
|
2020-03-19 13:11:20 +00:00
|
|
|
func (s *Store) CALeafSetIndex(idx uint64, index uint64) error {
|
|
|
|
tx := s.db.WriteTxn(idx)
|
2019-01-11 21:04:57 +00:00
|
|
|
defer tx.Abort()
|
|
|
|
|
2021-01-29 01:53:21 +00:00
|
|
|
return indexUpdateMaxTxn(tx, index, tableConnectCALeafCerts)
|
2019-01-11 21:04:57 +00:00
|
|
|
}
|
2019-06-26 18:05:00 +00:00
|
|
|
|
|
|
|
func (s *Store) CARootsAndConfig(ws memdb.WatchSet) (uint64, structs.CARoots, *structs.CAConfiguration, error) {
|
|
|
|
tx := s.db.Txn(false)
|
|
|
|
defer tx.Abort()
|
2019-06-18 00:52:01 +00:00
|
|
|
|
2020-07-10 00:56:43 +00:00
|
|
|
confIdx, config, err := caConfigTxn(tx, ws)
|
2019-06-26 18:05:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, nil, nil, fmt.Errorf("failed CA config lookup: %v", err)
|
|
|
|
}
|
2019-06-18 00:52:01 +00:00
|
|
|
|
2020-07-10 00:56:43 +00:00
|
|
|
rootsIdx, roots, err := caRootsTxn(tx, ws)
|
2019-06-26 18:05:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, nil, nil, fmt.Errorf("failed CA roots lookup: %v", err)
|
|
|
|
}
|
2019-06-18 00:52:01 +00:00
|
|
|
|
2019-06-26 18:05:00 +00:00
|
|
|
idx := rootsIdx
|
|
|
|
if confIdx > idx {
|
|
|
|
idx = confIdx
|
|
|
|
}
|
2019-06-18 00:52:01 +00:00
|
|
|
|
2019-06-26 18:05:00 +00:00
|
|
|
return idx, roots, config, nil
|
|
|
|
}
|
2020-01-09 15:32:19 +00:00
|
|
|
|
2020-03-19 13:11:20 +00:00
|
|
|
func (s *Store) CAIncrementProviderSerialNumber(idx uint64) (uint64, error) {
|
|
|
|
tx := s.db.WriteTxn(idx)
|
2020-01-09 15:32:19 +00:00
|
|
|
defer tx.Abort()
|
|
|
|
|
2021-03-08 18:02:41 +00:00
|
|
|
existing, err := tx.First(tableIndex, "id", tableConnectCABuiltinSerial)
|
2020-01-09 15:32:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("failed built-in CA serial number lookup: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var last uint64
|
|
|
|
if existing != nil {
|
|
|
|
last = existing.(*IndexEntry).Value
|
|
|
|
} else {
|
|
|
|
// Serials used to be based on the raft indexes in the provider table,
|
|
|
|
// so bootstrap off of that.
|
2021-01-29 01:53:21 +00:00
|
|
|
last = maxIndexTxn(tx, tableConnectCABuiltin)
|
2020-01-09 15:32:19 +00:00
|
|
|
}
|
|
|
|
next := last + 1
|
|
|
|
|
2021-03-08 18:02:41 +00:00
|
|
|
if err := tx.Insert(tableIndex, &IndexEntry{tableConnectCABuiltinSerial, next}); err != nil {
|
2020-01-09 15:32:19 +00:00
|
|
|
return 0, fmt.Errorf("failed updating index: %s", err)
|
|
|
|
}
|
|
|
|
|
2020-06-02 20:34:56 +00:00
|
|
|
err = tx.Commit()
|
|
|
|
return next, err
|
2020-01-09 15:32:19 +00:00
|
|
|
}
|