2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2015-08-22 00:23:01 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-09-23 15:06:23 +00:00
|
|
|
"strings"
|
2015-08-22 00:23:01 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/go-memdb"
|
|
|
|
)
|
|
|
|
|
2021-01-30 00:24:41 +00:00
|
|
|
// newDBSchema creates and returns the memdb schema for the Store.
|
|
|
|
func newDBSchema() *memdb.DBSchema {
|
|
|
|
db := &memdb.DBSchema{Tables: make(map[string]*memdb.TableSchema)}
|
2015-08-22 00:23:01 +00:00
|
|
|
|
2021-01-30 00:24:41 +00:00
|
|
|
addTableSchemas(db,
|
|
|
|
authMethodsTableSchema,
|
|
|
|
autopilotConfigTableSchema,
|
|
|
|
bindingRulesTableSchema,
|
|
|
|
caBuiltinProviderTableSchema,
|
|
|
|
caConfigTableSchema,
|
|
|
|
caRootTableSchema,
|
|
|
|
checksTableSchema,
|
|
|
|
configTableSchema,
|
|
|
|
coordinatesTableSchema,
|
|
|
|
federationStateTableSchema,
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
freeVirtualIPTableSchema,
|
2021-01-30 00:24:41 +00:00
|
|
|
gatewayServicesTableSchema,
|
|
|
|
indexTableSchema,
|
|
|
|
intentionsTableSchema,
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
kindServiceNameTableSchema,
|
2021-01-30 00:24:41 +00:00
|
|
|
kvsTableSchema,
|
|
|
|
meshTopologyTableSchema,
|
|
|
|
nodesTableSchema,
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
peeringTableSchema,
|
|
|
|
peeringTrustBundlesTableSchema,
|
2022-08-01 14:33:18 +00:00
|
|
|
peeringSecretsTableSchema,
|
|
|
|
peeringSecretUUIDsTableSchema,
|
2021-01-30 00:24:41 +00:00
|
|
|
policiesTableSchema,
|
|
|
|
preparedQueriesTableSchema,
|
|
|
|
rolesTableSchema,
|
|
|
|
servicesTableSchema,
|
2021-12-02 23:42:47 +00:00
|
|
|
serviceVirtualIPTableSchema,
|
2021-01-30 00:24:41 +00:00
|
|
|
sessionChecksTableSchema,
|
|
|
|
sessionsTableSchema,
|
|
|
|
systemMetadataTableSchema,
|
|
|
|
tokensTableSchema,
|
|
|
|
tombstonesTableSchema,
|
|
|
|
usageTableSchema,
|
|
|
|
)
|
|
|
|
withEnterpriseSchema(db)
|
|
|
|
return db
|
2017-11-29 01:03:34 +00:00
|
|
|
}
|
|
|
|
|
2021-01-30 00:24:41 +00:00
|
|
|
func addTableSchemas(db *memdb.DBSchema, schemas ...func() *memdb.TableSchema) {
|
2015-08-22 00:23:01 +00:00
|
|
|
for _, fn := range schemas {
|
|
|
|
schema := fn()
|
|
|
|
if _, ok := db.Tables[schema.Name]; ok {
|
|
|
|
panic(fmt.Sprintf("duplicate table name: %s", schema.Name))
|
|
|
|
}
|
|
|
|
db.Tables[schema.Name] = schema
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-05 22:45:27 +00:00
|
|
|
// IndexEntry keeps a record of the last index of a table or entity within a table.
|
|
|
|
type IndexEntry struct {
|
|
|
|
Key string
|
|
|
|
Value uint64
|
|
|
|
}
|
|
|
|
|
2022-06-13 14:22:46 +00:00
|
|
|
const (
|
|
|
|
tableIndex = "index"
|
|
|
|
indexDeleted = "deleted"
|
|
|
|
)
|
2021-02-05 22:53:08 +00:00
|
|
|
|
2021-02-05 22:45:27 +00:00
|
|
|
// indexTableSchema returns a new table schema used for tracking various the
|
|
|
|
// latest raft index for a table or entities within a table.
|
|
|
|
//
|
|
|
|
// The index table is necessary for tables that do not use tombstones. If the latest
|
|
|
|
// items in the table are deleted, the max index of a table would appear to go
|
|
|
|
// backwards. With the index table we can keep track of the latest update to a
|
|
|
|
// table, even when that update is a delete of the most recent item.
|
2015-08-22 00:23:01 +00:00
|
|
|
func indexTableSchema() *memdb.TableSchema {
|
|
|
|
return &memdb.TableSchema{
|
2021-02-05 22:53:08 +00:00
|
|
|
Name: tableIndex,
|
2015-08-22 00:23:01 +00:00
|
|
|
Indexes: map[string]*memdb.IndexSchema{
|
2021-02-05 22:53:08 +00:00
|
|
|
indexID: {
|
|
|
|
Name: indexID,
|
2015-08-22 00:23:01 +00:00
|
|
|
AllowMissing: false,
|
|
|
|
Unique: true,
|
2022-06-23 15:07:19 +00:00
|
|
|
Indexer: indexerSingle[string, *IndexEntry]{
|
2021-09-23 15:06:23 +00:00
|
|
|
readIndex: indexFromString,
|
|
|
|
writeIndex: indexNameFromIndexEntry,
|
2015-08-22 00:23:01 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2021-09-23 15:06:23 +00:00
|
|
|
|
2022-06-23 15:07:19 +00:00
|
|
|
func indexNameFromIndexEntry(e *IndexEntry) ([]byte, error) {
|
|
|
|
if e.Key == "" {
|
2021-09-23 15:06:23 +00:00
|
|
|
return nil, errMissingValueForIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
var b indexBuilder
|
2022-06-23 15:07:19 +00:00
|
|
|
b.String(strings.ToLower(e.Key))
|
2021-09-23 15:06:23 +00:00
|
|
|
return b.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2022-06-23 15:07:19 +00:00
|
|
|
func indexFromString(s string) ([]byte, error) {
|
2021-09-23 15:06:23 +00:00
|
|
|
var b indexBuilder
|
2022-06-23 15:07:19 +00:00
|
|
|
b.String(strings.ToLower(s))
|
2021-09-23 15:06:23 +00:00
|
|
|
return b.Bytes(), nil
|
|
|
|
}
|
2022-06-08 22:53:32 +00:00
|
|
|
|
2022-06-23 15:07:19 +00:00
|
|
|
func indexDeletedFromBoolQuery(q BoolQuery) ([]byte, error) {
|
2022-06-08 22:53:32 +00:00
|
|
|
var b indexBuilder
|
|
|
|
b.Bool(q.Value)
|
|
|
|
return b.Bytes(), nil
|
|
|
|
}
|
2022-06-23 15:07:19 +00:00
|
|
|
|
|
|
|
type enterpriseIndexable interface {
|
|
|
|
partitionIndexable
|
|
|
|
namespaceIndexable
|
|
|
|
}
|
|
|
|
|
|
|
|
type partitionIndexable interface {
|
|
|
|
PartitionOrDefault() string
|
|
|
|
}
|
|
|
|
|
|
|
|
type namespaceIndexable interface {
|
|
|
|
NamespaceOrDefault() string
|
|
|
|
}
|