2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2021-03-19 23:35:16 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
2021-03-19 23:35:16 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Query is a type used to query any single value index that may include an
|
|
|
|
// enterprise identifier.
|
|
|
|
type Query struct {
|
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
|
|
|
Value string
|
|
|
|
PeerName string
|
2022-04-05 21:10:06 +00:00
|
|
|
acl.EnterpriseMeta
|
2021-03-19 23:35:16 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
func (q Query) PeerOrEmpty() string {
|
|
|
|
return q.PeerName
|
|
|
|
}
|
|
|
|
|
2021-11-08 14:35:56 +00:00
|
|
|
func (q Query) IDValue() string {
|
|
|
|
return q.Value
|
|
|
|
}
|
|
|
|
|
2021-03-29 18:07:36 +00:00
|
|
|
// NamespaceOrDefault exists because structs.EnterpriseMeta uses a pointer
|
|
|
|
// receiver for this method. Remove once that is fixed.
|
|
|
|
func (q Query) NamespaceOrDefault() string {
|
|
|
|
return q.EnterpriseMeta.NamespaceOrDefault()
|
|
|
|
}
|
|
|
|
|
2021-07-22 18:58:08 +00:00
|
|
|
// PartitionOrDefault exists because structs.EnterpriseMeta uses a pointer
|
|
|
|
// receiver for this method. Remove once that is fixed.
|
|
|
|
func (q Query) PartitionOrDefault() string {
|
|
|
|
return q.EnterpriseMeta.PartitionOrDefault()
|
|
|
|
}
|
|
|
|
|
2021-11-24 14:10:38 +00:00
|
|
|
type MultiQuery struct {
|
|
|
|
Value []string
|
2022-04-05 21:10:06 +00:00
|
|
|
acl.EnterpriseMeta
|
2021-11-24 14:10:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (q MultiQuery) IDValue() []string {
|
|
|
|
return q.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
// NamespaceOrDefault exists because structs.EnterpriseMeta uses a pointer
|
|
|
|
// receiver for this method. Remove once that is fixed.
|
|
|
|
func (q MultiQuery) NamespaceOrDefault() string {
|
|
|
|
return q.EnterpriseMeta.NamespaceOrDefault()
|
|
|
|
}
|
|
|
|
|
|
|
|
// PartitionOrDefault exists because structs.EnterpriseMeta uses a pointer
|
|
|
|
// receiver for this method. Remove once that is fixed.
|
|
|
|
func (q MultiQuery) PartitionOrDefault() string {
|
|
|
|
return q.EnterpriseMeta.PartitionOrDefault()
|
|
|
|
}
|
|
|
|
|
2021-03-29 18:14:20 +00:00
|
|
|
// indexFromQuery builds an index key where Query.Value is lowercase, and is
|
|
|
|
// a required value.
|
2022-06-23 15:07:19 +00:00
|
|
|
func indexFromQuery(q Query) ([]byte, error) {
|
2021-03-29 18:14:20 +00:00
|
|
|
var b indexBuilder
|
|
|
|
b.String(strings.ToLower(q.Value))
|
|
|
|
return b.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2021-08-17 18:29:39 +00:00
|
|
|
func indexFromServiceNameAsString(arg interface{}) ([]byte, error) {
|
|
|
|
sn, ok := arg.(structs.ServiceName)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unexpected type %T for ServiceName index", arg)
|
|
|
|
}
|
|
|
|
|
|
|
|
var b indexBuilder
|
|
|
|
b.String(strings.ToLower(sn.String()))
|
|
|
|
return b.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2021-03-19 23:35:16 +00:00
|
|
|
func uuidStringToBytes(uuid string) ([]byte, error) {
|
2021-08-19 21:17:59 +00:00
|
|
|
// Verify the length
|
|
|
|
if l := len(uuid); l != 36 {
|
2021-03-19 23:35:16 +00:00
|
|
|
return nil, fmt.Errorf("UUID must be 36 characters")
|
|
|
|
}
|
2021-08-19 21:17:59 +00:00
|
|
|
return parseUUIDString(uuid)
|
|
|
|
}
|
|
|
|
|
|
|
|
func variableLengthUUIDStringToBytes(uuid string) ([]byte, error) {
|
|
|
|
// Verify the length
|
|
|
|
if l := len(uuid); l > 36 {
|
|
|
|
return nil, fmt.Errorf("Invalid UUID length. UUID have 36 characters; got %d", l)
|
|
|
|
}
|
|
|
|
return parseUUIDString(uuid)
|
|
|
|
}
|
2021-03-19 23:35:16 +00:00
|
|
|
|
2021-08-19 21:17:59 +00:00
|
|
|
// parseUUIDString is a modified version of memdb.UUIDFieldIndex.parseString.
|
|
|
|
// Callers should verify the length.
|
|
|
|
func parseUUIDString(uuid string) ([]byte, error) {
|
2021-03-19 23:35:16 +00:00
|
|
|
hyphens := strings.Count(uuid, "-")
|
|
|
|
if hyphens > 4 {
|
|
|
|
return nil, fmt.Errorf(`UUID should have maximum of 4 "-"; got %d`, hyphens)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The sanitized length is the length of the original string without the "-".
|
|
|
|
sanitized := strings.Replace(uuid, "-", "", -1)
|
|
|
|
sanitizedLength := len(sanitized)
|
|
|
|
if sanitizedLength%2 != 0 {
|
|
|
|
return nil, fmt.Errorf("UUID (without hyphens) must be even length")
|
|
|
|
}
|
|
|
|
|
|
|
|
dec, err := hex.DecodeString(sanitized)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid UUID: %w", err)
|
|
|
|
}
|
|
|
|
return dec, nil
|
|
|
|
}
|
2021-03-19 03:07:30 +00:00
|
|
|
|
|
|
|
// BoolQuery is a type used to query a boolean condition that may include an
|
|
|
|
// enterprise identifier.
|
|
|
|
type BoolQuery struct {
|
|
|
|
Value bool
|
2022-04-05 21:10:06 +00:00
|
|
|
acl.EnterpriseMeta
|
2021-03-19 03:07:30 +00:00
|
|
|
}
|
2021-08-19 21:17:59 +00:00
|
|
|
|
2021-09-13 15:53:00 +00:00
|
|
|
// NamespaceOrDefault exists because structs.EnterpriseMeta uses a pointer
|
|
|
|
// receiver for this method. Remove once that is fixed.
|
|
|
|
func (q BoolQuery) NamespaceOrDefault() string {
|
|
|
|
return q.EnterpriseMeta.NamespaceOrDefault()
|
|
|
|
}
|
|
|
|
|
|
|
|
// PartitionOrDefault exists because structs.EnterpriseMeta uses a pointer
|
|
|
|
// receiver for this method. Remove once that is fixed.
|
|
|
|
func (q BoolQuery) PartitionOrDefault() string {
|
|
|
|
return q.EnterpriseMeta.PartitionOrDefault()
|
|
|
|
}
|
|
|
|
|
2021-08-19 21:17:59 +00:00
|
|
|
// KeyValueQuery is a type used to query for both a key and a value that may
|
|
|
|
// include an enterprise identifier.
|
|
|
|
type KeyValueQuery struct {
|
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
|
|
|
Key string
|
|
|
|
Value string
|
|
|
|
PeerName string
|
2022-04-05 21:10:06 +00:00
|
|
|
acl.EnterpriseMeta
|
2021-08-19 21:17:59 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
func (q KeyValueQuery) PeerOrEmpty() string {
|
|
|
|
return q.PeerName
|
|
|
|
}
|
|
|
|
|
2021-08-19 21:17:59 +00:00
|
|
|
// NamespaceOrDefault exists because structs.EnterpriseMeta uses a pointer
|
|
|
|
// receiver for this method. Remove once that is fixed.
|
|
|
|
func (q KeyValueQuery) NamespaceOrDefault() string {
|
|
|
|
return q.EnterpriseMeta.NamespaceOrDefault()
|
|
|
|
}
|
|
|
|
|
|
|
|
// PartitionOrDefault exists because structs.EnterpriseMeta uses a pointer
|
|
|
|
// receiver for this method. Remove once that is fixed.
|
|
|
|
func (q KeyValueQuery) PartitionOrDefault() string {
|
|
|
|
return q.EnterpriseMeta.PartitionOrDefault()
|
|
|
|
}
|
|
|
|
|
2022-06-23 15:07:19 +00:00
|
|
|
func indexFromKeyValueQuery(q KeyValueQuery) ([]byte, error) {
|
2021-08-19 21:17:59 +00:00
|
|
|
// NOTE: this is case-sensitive!
|
|
|
|
|
|
|
|
var b indexBuilder
|
|
|
|
b.String(q.Key)
|
|
|
|
b.String(q.Value)
|
|
|
|
return b.Bytes(), nil
|
|
|
|
}
|
2021-09-10 20:56:56 +00:00
|
|
|
|
|
|
|
type AuthMethodQuery struct {
|
|
|
|
Value string
|
2022-04-05 21:10:06 +00:00
|
|
|
AuthMethodEntMeta acl.EnterpriseMeta
|
|
|
|
acl.EnterpriseMeta
|
2021-09-10 20:56:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NamespaceOrDefault exists because structs.EnterpriseMeta uses a pointer
|
|
|
|
// receiver for this method. Remove once that is fixed.
|
|
|
|
func (q AuthMethodQuery) NamespaceOrDefault() string {
|
|
|
|
return q.EnterpriseMeta.NamespaceOrDefault()
|
|
|
|
}
|
|
|
|
|
|
|
|
// PartitionOrDefault exists because structs.EnterpriseMeta uses a pointer
|
|
|
|
// receiver for this method. Remove once that is fixed.
|
|
|
|
func (q AuthMethodQuery) PartitionOrDefault() string {
|
|
|
|
return q.EnterpriseMeta.PartitionOrDefault()
|
|
|
|
}
|