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
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
2022-06-21 18:04:08 +00:00
|
|
|
"errors"
|
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
|
|
|
"fmt"
|
2022-06-27 19:37:18 +00:00
|
|
|
"sort"
|
2022-05-23 23:57:42 +00:00
|
|
|
"strings"
|
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
|
|
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
"github.com/hashicorp/go-memdb"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/acl"
|
2022-07-12 16:03:41 +00:00
|
|
|
"github.com/hashicorp/consul/agent/configentry"
|
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
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2022-05-19 19:21:44 +00:00
|
|
|
"github.com/hashicorp/consul/lib/maps"
|
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
|
|
|
"github.com/hashicorp/consul/proto/pbpeering"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
tablePeering = "peering"
|
|
|
|
tablePeeringTrustBundles = "peering-trust-bundles"
|
2022-08-01 14:33:18 +00:00
|
|
|
tablePeeringSecrets = "peering-secrets"
|
|
|
|
tablePeeringSecretUUIDs = "peering-secret-uuids"
|
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 peeringTableSchema() *memdb.TableSchema {
|
|
|
|
return &memdb.TableSchema{
|
|
|
|
Name: tablePeering,
|
|
|
|
Indexes: map[string]*memdb.IndexSchema{
|
|
|
|
indexID: {
|
|
|
|
Name: indexID,
|
|
|
|
AllowMissing: false,
|
|
|
|
Unique: true,
|
2022-06-23 15:07:19 +00:00
|
|
|
Indexer: indexerSingle[string, *pbpeering.Peering]{
|
|
|
|
readIndex: indexFromUUIDString,
|
|
|
|
writeIndex: indexIDFromPeering,
|
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
|
|
|
},
|
|
|
|
},
|
|
|
|
indexName: {
|
|
|
|
Name: indexName,
|
|
|
|
AllowMissing: false,
|
|
|
|
Unique: true,
|
2022-06-23 15:07:19 +00:00
|
|
|
Indexer: indexerSingleWithPrefix[Query, *pbpeering.Peering, any]{
|
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
|
|
|
readIndex: indexPeeringFromQuery,
|
|
|
|
writeIndex: indexFromPeering,
|
|
|
|
prefixIndex: prefixIndexFromQueryNoNamespace,
|
|
|
|
},
|
|
|
|
},
|
2022-06-08 22:53:32 +00:00
|
|
|
indexDeleted: {
|
|
|
|
Name: indexDeleted,
|
|
|
|
AllowMissing: false,
|
|
|
|
Unique: false,
|
2022-06-23 15:07:19 +00:00
|
|
|
Indexer: indexerSingle[BoolQuery, *pbpeering.Peering]{
|
2022-06-08 22:53:32 +00:00
|
|
|
readIndex: indexDeletedFromBoolQuery,
|
|
|
|
writeIndex: indexDeletedFromPeering,
|
|
|
|
},
|
|
|
|
},
|
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 peeringTrustBundlesTableSchema() *memdb.TableSchema {
|
|
|
|
return &memdb.TableSchema{
|
|
|
|
Name: tablePeeringTrustBundles,
|
|
|
|
Indexes: map[string]*memdb.IndexSchema{
|
|
|
|
indexID: {
|
|
|
|
Name: indexID,
|
|
|
|
AllowMissing: false,
|
|
|
|
Unique: true,
|
2022-06-23 15:07:19 +00:00
|
|
|
Indexer: indexerSingleWithPrefix[Query, *pbpeering.PeeringTrustBundle, any]{
|
2022-06-15 19:36:18 +00:00
|
|
|
readIndex: indexPeeringFromQuery, // same as peering table since we'll use the query.Value
|
|
|
|
writeIndex: indexFromPeeringTrustBundle,
|
|
|
|
prefixIndex: prefixIndexFromQueryNoNamespace,
|
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
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-01 14:33:18 +00:00
|
|
|
func peeringSecretsTableSchema() *memdb.TableSchema {
|
|
|
|
return &memdb.TableSchema{
|
|
|
|
Name: tablePeeringSecrets,
|
|
|
|
Indexes: map[string]*memdb.IndexSchema{
|
|
|
|
indexID: {
|
|
|
|
Name: indexID,
|
|
|
|
AllowMissing: false,
|
|
|
|
Unique: true,
|
|
|
|
Indexer: indexerSingle[string, *pbpeering.PeeringSecrets]{
|
|
|
|
readIndex: indexFromUUIDString,
|
|
|
|
writeIndex: indexIDFromPeeringSecret,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func peeringSecretUUIDsTableSchema() *memdb.TableSchema {
|
|
|
|
return &memdb.TableSchema{
|
|
|
|
Name: tablePeeringSecretUUIDs,
|
|
|
|
Indexes: map[string]*memdb.IndexSchema{
|
|
|
|
indexID: {
|
|
|
|
Name: indexID,
|
|
|
|
AllowMissing: false,
|
|
|
|
Unique: true,
|
|
|
|
Indexer: indexerSingle[string, string]{
|
|
|
|
readIndex: indexFromUUIDString,
|
|
|
|
writeIndex: indexFromUUIDString,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func indexIDFromPeeringSecret(p *pbpeering.PeeringSecrets) ([]byte, error) {
|
|
|
|
if p.PeerID == "" {
|
|
|
|
return nil, errMissingValueForIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
uuid, err := uuidStringToBytes(p.PeerID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var b indexBuilder
|
|
|
|
b.Raw(uuid)
|
|
|
|
return b.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2022-06-23 15:07:19 +00:00
|
|
|
func indexIDFromPeering(p *pbpeering.Peering) ([]byte, error) {
|
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
|
|
|
if p.ID == "" {
|
|
|
|
return nil, errMissingValueForIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
uuid, err := uuidStringToBytes(p.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var b indexBuilder
|
|
|
|
b.Raw(uuid)
|
|
|
|
return b.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2022-06-23 15:07:19 +00:00
|
|
|
func indexDeletedFromPeering(p *pbpeering.Peering) ([]byte, error) {
|
2022-06-08 22:53:32 +00:00
|
|
|
var b indexBuilder
|
|
|
|
b.Bool(!p.IsActive())
|
|
|
|
return b.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2022-08-01 14:33:18 +00:00
|
|
|
func (s *Store) PeeringSecretsRead(ws memdb.WatchSet, peerID string) (*pbpeering.PeeringSecrets, error) {
|
|
|
|
tx := s.db.ReadTxn()
|
|
|
|
defer tx.Abort()
|
|
|
|
|
|
|
|
secret, err := peeringSecretsReadByPeerIDTxn(tx, ws, peerID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if secret == nil {
|
|
|
|
// TODO (peering) Return the tables index so caller can watch it for changes if the secret doesn't exist.
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return secret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func peeringSecretsReadByPeerIDTxn(tx ReadTxn, ws memdb.WatchSet, id string) (*pbpeering.PeeringSecrets, error) {
|
|
|
|
watchCh, secretRaw, err := tx.FirstWatch(tablePeeringSecrets, indexID, id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed peering secret lookup: %w", err)
|
|
|
|
}
|
|
|
|
ws.Add(watchCh)
|
|
|
|
|
|
|
|
secret, ok := secretRaw.(*pbpeering.PeeringSecrets)
|
|
|
|
if secretRaw != nil && !ok {
|
|
|
|
return nil, fmt.Errorf("invalid type %T", secret)
|
|
|
|
}
|
|
|
|
return secret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) PeeringSecretsWrite(idx uint64, secret *pbpeering.PeeringSecrets) error {
|
|
|
|
tx := s.db.WriteTxn(idx)
|
|
|
|
defer tx.Abort()
|
|
|
|
|
|
|
|
if err := s.peeringSecretsWriteTxn(tx, secret); err != nil {
|
|
|
|
return fmt.Errorf("failed to write peering secret: %w", err)
|
|
|
|
}
|
|
|
|
return tx.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) peeringSecretsWriteTxn(tx WriteTxn, secret *pbpeering.PeeringSecrets) error {
|
|
|
|
if secret == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err := secret.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
peering, err := peeringReadByIDTxn(tx, nil, secret.PeerID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to read peering by id: %w", err)
|
|
|
|
}
|
|
|
|
if peering == nil {
|
|
|
|
return fmt.Errorf("unknown peering %q for secret", secret.PeerID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the peering came from a peering token no validation is done for the given secrets.
|
|
|
|
// Dialing peers do not need to validate uniqueness because the secrets were generated elsewhere.
|
|
|
|
if peering.ShouldDial() {
|
|
|
|
if err := tx.Insert(tablePeeringSecrets, secret); err != nil {
|
|
|
|
return fmt.Errorf("failed inserting peering: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the peering token was generated locally, validate that the newly introduced UUID is still unique.
|
|
|
|
// RPC handlers validate that generated IDs are available, but availability cannot be guaranteed until the state store operation.
|
|
|
|
var newSecretID string
|
|
|
|
switch {
|
|
|
|
// Establishment secrets are written when generating peering tokens, and no other secret IDs are included.
|
|
|
|
case secret.GetEstablishment() != nil:
|
|
|
|
newSecretID = secret.GetEstablishment().SecretID
|
|
|
|
// Stream secrets can be written as:
|
|
|
|
// - A new PendingSecretID from the ExchangeSecret RPC
|
|
|
|
// - An ActiveSecretID when promoting a pending secret on first use
|
|
|
|
case secret.GetStream() != nil:
|
|
|
|
if pending := secret.GetStream().GetPendingSecretID(); pending != "" {
|
|
|
|
newSecretID = pending
|
|
|
|
}
|
|
|
|
|
|
|
|
// We do not need to check the long-lived Stream.ActiveSecretID for uniqueness because:
|
|
|
|
// - In the cluster that generated it the secret is always introduced as a PendingSecretID, then promoted to ActiveSecretID.
|
|
|
|
// This means that the promoted secret is already known to be unique.
|
|
|
|
}
|
|
|
|
|
|
|
|
if newSecretID != "" {
|
|
|
|
valid, err := validateProposedPeeringSecretUUIDTxn(tx, newSecretID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to check peering secret ID: %w", err)
|
|
|
|
}
|
|
|
|
if !valid {
|
|
|
|
return fmt.Errorf("peering secret is already in use, retry the operation")
|
|
|
|
}
|
|
|
|
err = tx.Insert(tablePeeringSecretUUIDs, newSecretID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to write secret UUID: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
existing, err := peeringSecretsReadByPeerIDTxn(tx, nil, secret.PeerID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var toDelete []string
|
|
|
|
if existing != nil {
|
|
|
|
// Merge in existing stream secrets when persisting a new establishment secret.
|
|
|
|
// This is to avoid invalidating stream secrets when a new peering token
|
|
|
|
// is generated.
|
|
|
|
//
|
|
|
|
// We purposely DO NOT do the reverse of inheriting an existing establishment secret.
|
|
|
|
// When exchanging establishment secrets for stream secrets, we invalidate the
|
|
|
|
// establishment secret by deleting it.
|
|
|
|
if secret.GetEstablishment() != nil && secret.GetStream() == nil && existing.GetStream() != nil {
|
|
|
|
secret.Stream = existing.Stream
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect any overwritten UUIDs for deletion.
|
|
|
|
//
|
|
|
|
// Old establishment secret ID are always cleaned up when they don't match.
|
|
|
|
// They will either be replaced by a new one or deleted in the secret exchange RPC.
|
|
|
|
existingEstablishment := existing.GetEstablishment().GetSecretID()
|
|
|
|
if existingEstablishment != "" && existingEstablishment != secret.GetEstablishment().GetSecretID() {
|
|
|
|
toDelete = append(toDelete, existingEstablishment)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Old active secret IDs are always cleaned up when they don't match.
|
|
|
|
// They are only ever replaced when promoting a pending secret ID.
|
|
|
|
existingActive := existing.GetStream().GetActiveSecretID()
|
|
|
|
if existingActive != "" && existingActive != secret.GetStream().GetActiveSecretID() {
|
|
|
|
toDelete = append(toDelete, existingActive)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pending secrets can change in three ways:
|
|
|
|
// - Generating a new pending secret: Nothing to delete here since there's no old pending secret being replaced.
|
|
|
|
// - Re-establishing a peering, and re-generating a pending secret: should delete the old one if both are non-empty.
|
|
|
|
// - Promoting a pending secret: Nothing to delete here since the pending secret is now active and still in use.
|
|
|
|
existingPending := existing.GetStream().GetPendingSecretID()
|
|
|
|
newPending := secret.GetStream().GetPendingSecretID()
|
|
|
|
if existingPending != "" &&
|
|
|
|
// The value of newPending indicates whether a peering is being generated/re-established (not empty)
|
|
|
|
// or whether a pending secret is being promoted (empty).
|
|
|
|
newPending != "" &&
|
|
|
|
newPending != existingPending {
|
|
|
|
toDelete = append(toDelete, existingPending)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, id := range toDelete {
|
|
|
|
if err := tx.Delete(tablePeeringSecretUUIDs, id); err != nil {
|
|
|
|
return fmt.Errorf("failed to free UUID: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tx.Insert(tablePeeringSecrets, secret); err != nil {
|
|
|
|
return fmt.Errorf("failed inserting peering: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) PeeringSecretsDelete(idx uint64, peerID string) error {
|
|
|
|
tx := s.db.WriteTxn(idx)
|
|
|
|
defer tx.Abort()
|
|
|
|
|
|
|
|
if err := peeringSecretsDeleteTxn(tx, peerID); err != nil {
|
|
|
|
return fmt.Errorf("failed to write peering secret: %w", err)
|
|
|
|
}
|
|
|
|
return tx.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
func peeringSecretsDeleteTxn(tx WriteTxn, peerID string) error {
|
|
|
|
secretRaw, err := tx.First(tablePeeringSecrets, indexID, peerID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to fetch secret for peering: %w", err)
|
|
|
|
}
|
|
|
|
if secretRaw == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err := tx.Delete(tablePeeringSecrets, secretRaw); err != nil {
|
|
|
|
return fmt.Errorf("failed to delete secret for peering: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
secrets, ok := secretRaw.(*pbpeering.PeeringSecrets)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid type %T", secretRaw)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Also clean up the UUID tracking table.
|
|
|
|
var toDelete []string
|
|
|
|
if establishment := secrets.GetEstablishment().GetSecretID(); establishment != "" {
|
|
|
|
toDelete = append(toDelete, establishment)
|
|
|
|
}
|
|
|
|
if pending := secrets.GetStream().GetPendingSecretID(); pending != "" {
|
|
|
|
toDelete = append(toDelete, pending)
|
|
|
|
}
|
|
|
|
if active := secrets.GetStream().GetActiveSecretID(); active != "" {
|
|
|
|
toDelete = append(toDelete, active)
|
|
|
|
}
|
|
|
|
for _, id := range toDelete {
|
|
|
|
if err := tx.Delete(tablePeeringSecretUUIDs, id); err != nil {
|
|
|
|
return fmt.Errorf("failed to free UUID: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) ValidateProposedPeeringSecretUUID(id string) (bool, error) {
|
|
|
|
tx := s.db.ReadTxn()
|
|
|
|
defer tx.Abort()
|
|
|
|
|
|
|
|
return validateProposedPeeringSecretUUIDTxn(tx, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// validateProposedPeeringSecretUUIDTxn is used to test whether a candidate secretID can be used as a peering secret.
|
|
|
|
// Returns true if the given secret is not in use.
|
|
|
|
func validateProposedPeeringSecretUUIDTxn(tx ReadTxn, secretID string) (bool, error) {
|
|
|
|
secretRaw, err := tx.First(tablePeeringSecretUUIDs, indexID, secretID)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("failed peering secret lookup: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
secret, ok := secretRaw.(string)
|
|
|
|
if secretRaw != nil && !ok {
|
|
|
|
return false, fmt.Errorf("invalid type %T", secret)
|
|
|
|
}
|
|
|
|
return secret == "", nil
|
|
|
|
}
|
|
|
|
|
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 (s *Store) PeeringReadByID(ws memdb.WatchSet, id string) (uint64, *pbpeering.Peering, error) {
|
|
|
|
tx := s.db.ReadTxn()
|
|
|
|
defer tx.Abort()
|
|
|
|
|
2022-05-06 19:35:31 +00:00
|
|
|
peering, err := peeringReadByIDTxn(tx, ws, id)
|
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
|
|
|
if err != nil {
|
|
|
|
return 0, nil, fmt.Errorf("failed to read peering by id: %w", err)
|
|
|
|
}
|
|
|
|
if peering == nil {
|
|
|
|
// Return the tables index so caller can watch it for changes if the peering doesn't exist
|
|
|
|
return maxIndexWatchTxn(tx, ws, tablePeering), nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return peering.ModifyIndex, peering, nil
|
|
|
|
}
|
|
|
|
|
2022-05-06 19:35:31 +00:00
|
|
|
func peeringReadByIDTxn(tx ReadTxn, ws memdb.WatchSet, id string) (*pbpeering.Peering, error) {
|
|
|
|
watchCh, peeringRaw, err := tx.FirstWatch(tablePeering, indexID, id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed peering lookup: %w", err)
|
|
|
|
}
|
|
|
|
ws.Add(watchCh)
|
|
|
|
|
|
|
|
peering, ok := peeringRaw.(*pbpeering.Peering)
|
|
|
|
if peeringRaw != nil && !ok {
|
|
|
|
return nil, fmt.Errorf("invalid type %T", peering)
|
|
|
|
}
|
|
|
|
return peering, nil
|
|
|
|
}
|
|
|
|
|
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 (s *Store) PeeringRead(ws memdb.WatchSet, q Query) (uint64, *pbpeering.Peering, error) {
|
|
|
|
tx := s.db.ReadTxn()
|
|
|
|
defer tx.Abort()
|
|
|
|
|
2022-05-06 19:35:31 +00:00
|
|
|
return peeringReadTxn(tx, ws, q)
|
|
|
|
}
|
|
|
|
|
|
|
|
func peeringReadTxn(tx ReadTxn, ws memdb.WatchSet, q Query) (uint64, *pbpeering.Peering, error) {
|
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
|
|
|
watchCh, peeringRaw, err := tx.FirstWatch(tablePeering, indexName, q)
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, fmt.Errorf("failed peering lookup: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
peering, ok := peeringRaw.(*pbpeering.Peering)
|
2022-05-06 19:35:31 +00:00
|
|
|
if peeringRaw != nil && !ok {
|
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
|
|
|
return 0, nil, fmt.Errorf("invalid type %T", peering)
|
|
|
|
}
|
|
|
|
ws.Add(watchCh)
|
|
|
|
|
|
|
|
if peering == nil {
|
|
|
|
// Return the tables index so caller can watch it for changes if the peering doesn't exist
|
|
|
|
return maxIndexWatchTxn(tx, ws, partitionedIndexEntryName(tablePeering, q.PartitionOrDefault())), nil, nil
|
|
|
|
}
|
|
|
|
|
2022-05-06 19:35:31 +00:00
|
|
|
return peering.ModifyIndex, peering, nil
|
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 (s *Store) PeeringList(ws memdb.WatchSet, entMeta acl.EnterpriseMeta) (uint64, []*pbpeering.Peering, error) {
|
|
|
|
tx := s.db.ReadTxn()
|
|
|
|
defer tx.Abort()
|
2022-07-12 16:03:41 +00:00
|
|
|
return peeringListTxn(ws, tx, entMeta)
|
2022-05-19 19:21:44 +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
|
|
|
|
2022-07-12 16:03:41 +00:00
|
|
|
func peeringListTxn(ws memdb.WatchSet, tx ReadTxn, entMeta acl.EnterpriseMeta) (uint64, []*pbpeering.Peering, error) {
|
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
|
|
|
var (
|
|
|
|
iter memdb.ResultIterator
|
|
|
|
err error
|
|
|
|
idx uint64
|
|
|
|
)
|
|
|
|
if entMeta.PartitionOrDefault() == structs.WildcardSpecifier {
|
|
|
|
iter, err = tx.Get(tablePeering, indexID)
|
|
|
|
idx = maxIndexWatchTxn(tx, ws, tablePeering)
|
|
|
|
} else {
|
|
|
|
iter, err = tx.Get(tablePeering, indexName+"_prefix", entMeta)
|
|
|
|
idx = maxIndexWatchTxn(tx, ws, partitionedIndexEntryName(tablePeering, entMeta.PartitionOrDefault()))
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, fmt.Errorf("failed peering lookup: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var result []*pbpeering.Peering
|
|
|
|
for entry := iter.Next(); entry != nil; entry = iter.Next() {
|
|
|
|
result = append(result, entry.(*pbpeering.Peering))
|
|
|
|
}
|
|
|
|
|
|
|
|
return idx, result, nil
|
|
|
|
}
|
|
|
|
|
2022-08-01 14:33:18 +00:00
|
|
|
func (s *Store) PeeringWrite(idx uint64, req *pbpeering.PeeringWriteRequest) error {
|
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
|
|
|
tx := s.db.WriteTxn(idx)
|
|
|
|
defer tx.Abort()
|
|
|
|
|
2022-06-21 18:04:08 +00:00
|
|
|
// Check that the ID and Name are set.
|
2022-08-01 14:33:18 +00:00
|
|
|
if req.Peering.ID == "" {
|
2022-06-21 18:04:08 +00:00
|
|
|
return errors.New("Missing Peering ID")
|
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
|
|
|
}
|
2022-08-01 14:33:18 +00:00
|
|
|
if req.Peering.Name == "" {
|
2022-06-21 18:04:08 +00:00
|
|
|
return errors.New("Missing Peering Name")
|
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
|
|
|
}
|
|
|
|
|
2022-08-01 14:33:18 +00:00
|
|
|
// Ensure the name is unique (cannot conflict with another peering with a different ID).
|
2022-06-21 18:04:08 +00:00
|
|
|
_, existing, err := peeringReadTxn(tx, nil, Query{
|
2022-08-01 14:33:18 +00:00
|
|
|
Value: req.Peering.Name,
|
|
|
|
EnterpriseMeta: *structs.NodeEnterpriseMetaInPartition(req.Peering.Partition),
|
2022-06-21 18:04:08 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
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
|
|
|
}
|
|
|
|
|
|
|
|
if existing != nil {
|
2022-08-01 14:33:18 +00:00
|
|
|
if req.Peering.ID != existing.ID {
|
|
|
|
return fmt.Errorf("A peering already exists with the name %q and a different ID %q", req.Peering.Name, existing.ID)
|
2022-06-21 18:04:08 +00:00
|
|
|
}
|
2022-08-01 14:33:18 +00:00
|
|
|
// Prevent modifications to Peering marked for deletion.
|
2022-06-08 22:53:32 +00:00
|
|
|
if !existing.IsActive() {
|
|
|
|
return fmt.Errorf("cannot write to peering that is marked for deletion")
|
|
|
|
}
|
|
|
|
|
2022-08-01 14:33:18 +00:00
|
|
|
if req.Peering.State == pbpeering.PeeringState_UNDEFINED {
|
|
|
|
req.Peering.State = existing.State
|
2022-07-25 18:37:56 +00:00
|
|
|
}
|
|
|
|
// TODO(peering): Confirm behavior when /peering/token is called more than once.
|
|
|
|
// We may need to avoid clobbering existing values.
|
2022-08-01 14:33:18 +00:00
|
|
|
req.Peering.ImportedServiceCount = existing.ImportedServiceCount
|
|
|
|
req.Peering.ExportedServiceCount = existing.ExportedServiceCount
|
|
|
|
req.Peering.CreateIndex = existing.CreateIndex
|
|
|
|
req.Peering.ModifyIndex = idx
|
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
|
|
|
} else {
|
2022-08-01 14:33:18 +00:00
|
|
|
idMatch, err := peeringReadByIDTxn(tx, nil, req.Peering.ID)
|
2022-06-21 18:04:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if idMatch != nil {
|
2022-08-01 14:33:18 +00:00
|
|
|
return fmt.Errorf("A peering already exists with the ID %q and a different name %q", req.Peering.Name, existing.ID)
|
2022-06-21 18:04:08 +00:00
|
|
|
}
|
|
|
|
|
2022-08-01 14:33:18 +00:00
|
|
|
if !req.Peering.IsActive() {
|
2022-06-08 22:53:32 +00:00
|
|
|
return fmt.Errorf("cannot create a new peering marked for deletion")
|
|
|
|
}
|
2022-08-01 14:33:18 +00:00
|
|
|
if req.Peering.State == 0 {
|
|
|
|
req.Peering.State = pbpeering.PeeringState_PENDING
|
2022-07-04 14:47:58 +00:00
|
|
|
}
|
2022-08-01 14:33:18 +00:00
|
|
|
req.Peering.CreateIndex = idx
|
|
|
|
req.Peering.ModifyIndex = idx
|
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
|
|
|
}
|
|
|
|
|
2022-08-01 14:33:18 +00:00
|
|
|
// Ensure associated secrets are cleaned up when a peering is marked for deletion.
|
|
|
|
if req.Peering.State == pbpeering.PeeringState_DELETING {
|
|
|
|
if err := peeringSecretsDeleteTxn(tx, req.Peering.ID); err != nil {
|
|
|
|
return fmt.Errorf("failed to delete peering secrets: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Peerings are inserted before the associated StreamSecret because writing secrets
|
|
|
|
// depends on the peering existing.
|
|
|
|
if err := tx.Insert(tablePeering, req.Peering); err != nil {
|
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
|
|
|
return fmt.Errorf("failed inserting peering: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-08-01 14:33:18 +00:00
|
|
|
// Write any secrets generated with the peering.
|
|
|
|
err = s.peeringSecretsWriteTxn(tx, req.GetSecret())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to write peering establishment secret: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := updatePeeringTableIndexes(tx, idx, req.Peering.PartitionOrDefault()); err != nil {
|
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
|
|
|
return err
|
|
|
|
}
|
|
|
|
return tx.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) PeeringDelete(idx uint64, q Query) error {
|
|
|
|
tx := s.db.WriteTxn(idx)
|
|
|
|
defer tx.Abort()
|
|
|
|
|
|
|
|
existing, err := tx.First(tablePeering, indexName, q)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed peering lookup: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if existing == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-08 22:53:32 +00:00
|
|
|
if existing.(*pbpeering.Peering).IsActive() {
|
|
|
|
return fmt.Errorf("cannot delete a peering without first marking for deletion")
|
|
|
|
}
|
|
|
|
|
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
|
|
|
if err := tx.Delete(tablePeering, existing); err != nil {
|
|
|
|
return fmt.Errorf("failed deleting peering: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := updatePeeringTableIndexes(tx, idx, q.PartitionOrDefault()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return tx.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) PeeringTerminateByID(idx uint64, id string) error {
|
|
|
|
tx := s.db.WriteTxn(idx)
|
|
|
|
defer tx.Abort()
|
|
|
|
|
2022-05-06 19:35:31 +00:00
|
|
|
existing, err := peeringReadByIDTxn(tx, nil, id)
|
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
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to read peering %q: %w", id, err)
|
|
|
|
}
|
|
|
|
if existing == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
c := proto.Clone(existing)
|
|
|
|
clone, ok := c.(*pbpeering.Peering)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid type %T, expected *pbpeering.Peering", existing)
|
|
|
|
}
|
|
|
|
|
|
|
|
clone.State = pbpeering.PeeringState_TERMINATED
|
|
|
|
clone.ModifyIndex = idx
|
|
|
|
|
|
|
|
if err := tx.Insert(tablePeering, clone); err != nil {
|
|
|
|
return fmt.Errorf("failed inserting peering: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := updatePeeringTableIndexes(tx, idx, clone.PartitionOrDefault()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return tx.Commit()
|
|
|
|
}
|
|
|
|
|
2022-05-19 19:21:44 +00:00
|
|
|
// ExportedServicesForPeer returns the list of typical and proxy services
|
|
|
|
// exported to a peer.
|
|
|
|
//
|
|
|
|
// TODO(peering): What to do about terminating gateways? Sometimes terminating
|
|
|
|
// gateways are the appropriate destination to dial for an upstream mesh
|
|
|
|
// service. However, that information is handled by observing the terminating
|
|
|
|
// gateway's config entry, which we wouldn't want to replicate. How would
|
|
|
|
// client peers know to route through terminating gateways when they're not
|
|
|
|
// dialing through a remote mesh gateway?
|
2022-06-27 19:37:18 +00:00
|
|
|
func (s *Store) ExportedServicesForPeer(ws memdb.WatchSet, peerID string, dc string) (uint64, *structs.ExportedServiceList, error) {
|
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
|
|
|
tx := s.db.ReadTxn()
|
|
|
|
defer tx.Abort()
|
|
|
|
|
2022-05-06 19:35:31 +00:00
|
|
|
peering, err := peeringReadByIDTxn(tx, ws, peerID)
|
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
|
|
|
if err != nil {
|
|
|
|
return 0, nil, fmt.Errorf("failed to read peering: %w", err)
|
|
|
|
}
|
|
|
|
if peering == nil {
|
2022-05-19 19:21:44 +00:00
|
|
|
return 0, &structs.ExportedServiceList{}, nil
|
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
|
|
|
}
|
|
|
|
|
2022-07-12 16:03:41 +00:00
|
|
|
return exportedServicesForPeerTxn(ws, tx, peering, dc)
|
2022-05-19 19:21:44 +00:00
|
|
|
}
|
|
|
|
|
2022-06-06 19:20:41 +00:00
|
|
|
func (s *Store) ExportedServicesForAllPeersByName(ws memdb.WatchSet, entMeta acl.EnterpriseMeta) (uint64, map[string]structs.ServiceList, error) {
|
|
|
|
tx := s.db.ReadTxn()
|
|
|
|
defer tx.Abort()
|
|
|
|
|
2022-07-12 16:03:41 +00:00
|
|
|
maxIdx, peerings, err := peeringListTxn(ws, tx, entMeta)
|
2022-06-06 19:20:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, nil, fmt.Errorf("failed to list peerings: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
out := make(map[string]structs.ServiceList)
|
|
|
|
for _, peering := range peerings {
|
2022-07-12 16:03:41 +00:00
|
|
|
idx, list, err := exportedServicesForPeerTxn(ws, tx, peering, "")
|
2022-06-06 19:20:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, nil, fmt.Errorf("failed to list exported services for peer %q: %w", peering.ID, err)
|
|
|
|
}
|
|
|
|
if idx > maxIdx {
|
|
|
|
maxIdx = idx
|
|
|
|
}
|
|
|
|
m := list.ListAllDiscoveryChains()
|
|
|
|
if len(m) > 0 {
|
2022-08-01 14:33:18 +00:00
|
|
|
sns := maps.SliceOfKeys(m)
|
2022-07-27 15:06:20 +00:00
|
|
|
sort.Sort(structs.ServiceList(sns))
|
|
|
|
out[peering.Name] = sns
|
2022-06-06 19:20:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return maxIdx, out, nil
|
|
|
|
}
|
|
|
|
|
2022-06-27 19:37:18 +00:00
|
|
|
// exportedServicesForPeerTxn will find all services that are exported to a
|
|
|
|
// specific peering, and optionally include information about discovery chain
|
|
|
|
// reachable targets for these exported services if the "dc" parameter is
|
|
|
|
// specified.
|
2022-07-12 16:03:41 +00:00
|
|
|
func exportedServicesForPeerTxn(
|
|
|
|
ws memdb.WatchSet,
|
|
|
|
tx ReadTxn,
|
|
|
|
peering *pbpeering.Peering,
|
|
|
|
dc string,
|
|
|
|
) (uint64, *structs.ExportedServiceList, error) {
|
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
|
|
|
maxIdx := peering.ModifyIndex
|
|
|
|
|
|
|
|
entMeta := structs.NodeEnterpriseMetaInPartition(peering.Partition)
|
2022-07-12 16:03:41 +00:00
|
|
|
idx, conf, err := getExportedServicesConfigEntryTxn(tx, ws, nil, entMeta)
|
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
|
|
|
if err != nil {
|
|
|
|
return 0, nil, fmt.Errorf("failed to fetch exported-services config entry: %w", err)
|
|
|
|
}
|
|
|
|
if idx > maxIdx {
|
|
|
|
maxIdx = idx
|
|
|
|
}
|
2022-07-12 16:03:41 +00:00
|
|
|
if conf == nil {
|
2022-05-19 19:21:44 +00:00
|
|
|
return maxIdx, &structs.ExportedServiceList{}, nil
|
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
|
|
|
}
|
2022-05-19 19:21:44 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
normalSet = make(map[structs.ServiceName]struct{})
|
|
|
|
discoSet = make(map[structs.ServiceName]struct{})
|
|
|
|
)
|
|
|
|
|
|
|
|
// At least one of the following should be true for a name for it to
|
|
|
|
// replicate:
|
|
|
|
//
|
|
|
|
// - are a discovery chain by definition (service-router, service-splitter, service-resolver)
|
|
|
|
// - have an explicit sidecar kind=connect-proxy
|
|
|
|
// - use connect native mode
|
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
|
|
|
|
|
|
|
for _, svc := range conf.Services {
|
|
|
|
svcMeta := acl.NewEnterpriseMetaWithPartition(entMeta.PartitionOrDefault(), svc.Namespace)
|
|
|
|
|
|
|
|
sawPeer := false
|
|
|
|
for _, consumer := range svc.Consumers {
|
|
|
|
name := structs.NewServiceName(svc.Name, &svcMeta)
|
|
|
|
|
2022-05-19 19:21:44 +00:00
|
|
|
if _, ok := normalSet[name]; ok {
|
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
|
|
|
// Service was covered by a wildcard that was already accounted for
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if consumer.PeerName != peering.Name {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
sawPeer = true
|
|
|
|
|
|
|
|
if svc.Name != structs.WildcardSpecifier {
|
2022-05-19 19:21:44 +00:00
|
|
|
normalSet[name] = 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the target peer is a consumer, and all services in the namespace are exported, query those service names.
|
|
|
|
if sawPeer && svc.Name == structs.WildcardSpecifier {
|
2022-05-19 19:21:44 +00:00
|
|
|
idx, typicalServices, err := serviceNamesOfKindTxn(tx, ws, structs.ServiceKindTypical, svcMeta)
|
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
|
|
|
if err != nil {
|
2022-05-19 19:21:44 +00:00
|
|
|
return 0, nil, fmt.Errorf("failed to get typical service names: %w", err)
|
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
|
|
|
}
|
|
|
|
if idx > maxIdx {
|
|
|
|
maxIdx = idx
|
|
|
|
}
|
|
|
|
for _, s := range typicalServices {
|
2022-05-19 19:21:44 +00:00
|
|
|
normalSet[s.Service] = 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
|
|
|
}
|
|
|
|
|
2022-05-19 19:21:44 +00:00
|
|
|
// list all config entries of kind service-resolver, service-router, service-splitter?
|
2022-07-12 16:03:41 +00:00
|
|
|
idx, discoChains, err := listDiscoveryChainNamesTxn(tx, ws, nil, svcMeta)
|
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
|
|
|
if err != nil {
|
2022-05-19 19:21:44 +00:00
|
|
|
return 0, nil, fmt.Errorf("failed to get discovery chain names: %w", err)
|
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
|
|
|
}
|
|
|
|
if idx > maxIdx {
|
|
|
|
maxIdx = idx
|
|
|
|
}
|
2022-05-19 19:21:44 +00:00
|
|
|
for _, sn := range discoChains {
|
|
|
|
discoSet[sn] = 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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-19 19:21:44 +00:00
|
|
|
normal := maps.SliceOfKeys(normalSet)
|
|
|
|
disco := maps.SliceOfKeys(discoSet)
|
|
|
|
|
2022-06-27 19:37:18 +00:00
|
|
|
chainInfo := make(map[structs.ServiceName]structs.ExportedDiscoveryChainInfo)
|
|
|
|
populateChainInfo := func(svc structs.ServiceName) error {
|
|
|
|
if _, ok := chainInfo[svc]; ok {
|
2022-05-25 17:37:44 +00:00
|
|
|
return nil // already processed
|
|
|
|
}
|
|
|
|
|
2022-06-27 19:37:18 +00:00
|
|
|
var info structs.ExportedDiscoveryChainInfo
|
|
|
|
|
2022-05-25 17:37:44 +00:00
|
|
|
idx, protocol, err := protocolForService(tx, ws, svc)
|
|
|
|
if err != nil {
|
2022-06-27 19:37:18 +00:00
|
|
|
return fmt.Errorf("failed to get protocol for service %q: %w", svc, err)
|
2022-05-25 17:37:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if idx > maxIdx {
|
|
|
|
maxIdx = idx
|
|
|
|
}
|
2022-06-27 19:37:18 +00:00
|
|
|
info.Protocol = protocol
|
|
|
|
|
|
|
|
if dc != "" && !structs.IsProtocolHTTPLike(protocol) {
|
|
|
|
// We only need to populate the targets for replication purposes for L4 protocols, which
|
|
|
|
// do not ultimately get intercepted by the mesh gateways.
|
2022-07-12 16:03:41 +00:00
|
|
|
idx, targets, err := discoveryChainOriginalTargetsTxn(tx, ws, dc, svc.Name, &svc.EnterpriseMeta)
|
2022-06-27 19:37:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to get discovery chain targets for service %q: %w", svc, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if idx > maxIdx {
|
|
|
|
maxIdx = idx
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(targets, func(i, j int) bool {
|
|
|
|
return targets[i].ID < targets[j].ID
|
|
|
|
})
|
|
|
|
|
|
|
|
info.TCPTargets = targets
|
|
|
|
}
|
2022-05-25 17:37:44 +00:00
|
|
|
|
2022-06-27 19:37:18 +00:00
|
|
|
chainInfo[svc] = info
|
2022-05-25 17:37:44 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-06-27 19:37:18 +00:00
|
|
|
|
2022-05-25 17:37:44 +00:00
|
|
|
for _, svc := range normal {
|
2022-06-27 19:37:18 +00:00
|
|
|
if err := populateChainInfo(svc); err != nil {
|
2022-05-25 17:37:44 +00:00
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, svc := range disco {
|
2022-06-27 19:37:18 +00:00
|
|
|
if err := populateChainInfo(svc); err != nil {
|
2022-05-25 17:37:44 +00:00
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-27 19:37:18 +00:00
|
|
|
structs.ServiceList(normal).Sort()
|
|
|
|
|
2022-05-25 17:37:44 +00:00
|
|
|
list := &structs.ExportedServiceList{
|
2022-06-27 19:37:18 +00:00
|
|
|
Services: normal,
|
|
|
|
DiscoChains: chainInfo,
|
2022-05-25 17:37:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return maxIdx, list, nil
|
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
|
|
|
}
|
|
|
|
|
2022-07-12 16:03:41 +00:00
|
|
|
func listAllExportedServices(
|
|
|
|
ws memdb.WatchSet,
|
|
|
|
tx ReadTxn,
|
|
|
|
overrides map[configentry.KindName]structs.ConfigEntry,
|
|
|
|
entMeta acl.EnterpriseMeta,
|
|
|
|
) (uint64, map[structs.ServiceName]struct{}, error) {
|
|
|
|
idx, export, err := getExportedServicesConfigEntryTxn(tx, ws, overrides, &entMeta)
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
found := make(map[structs.ServiceName]struct{})
|
|
|
|
if export == nil {
|
|
|
|
return idx, found, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
_, services, err := listServicesExportedToAnyPeerByConfigEntry(ws, tx, export, overrides)
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
for _, svc := range services {
|
|
|
|
found[svc] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return idx, found, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func listServicesExportedToAnyPeerByConfigEntry(
|
|
|
|
ws memdb.WatchSet,
|
|
|
|
tx ReadTxn,
|
|
|
|
conf *structs.ExportedServicesConfigEntry,
|
|
|
|
overrides map[configentry.KindName]structs.ConfigEntry,
|
|
|
|
) (uint64, []structs.ServiceName, error) {
|
|
|
|
var (
|
|
|
|
entMeta = conf.GetEnterpriseMeta()
|
|
|
|
found = make(map[structs.ServiceName]struct{})
|
|
|
|
maxIdx uint64
|
|
|
|
)
|
|
|
|
|
|
|
|
for _, svc := range conf.Services {
|
|
|
|
svcMeta := acl.NewEnterpriseMetaWithPartition(entMeta.PartitionOrDefault(), svc.Namespace)
|
|
|
|
|
|
|
|
sawPeer := false
|
|
|
|
for _, consumer := range svc.Consumers {
|
|
|
|
if consumer.PeerName == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
sawPeer = true
|
|
|
|
|
|
|
|
sn := structs.NewServiceName(svc.Name, &svcMeta)
|
|
|
|
if _, ok := found[sn]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if svc.Name != structs.WildcardSpecifier {
|
|
|
|
found[sn] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if sawPeer && svc.Name == structs.WildcardSpecifier {
|
|
|
|
idx, discoChains, err := listDiscoveryChainNamesTxn(tx, ws, overrides, svcMeta)
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, fmt.Errorf("failed to get discovery chain names: %w", err)
|
|
|
|
}
|
|
|
|
if idx > maxIdx {
|
|
|
|
maxIdx = idx
|
|
|
|
}
|
|
|
|
for _, sn := range discoChains {
|
|
|
|
found[sn] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foundKeys := maps.SliceOfKeys(found)
|
|
|
|
|
|
|
|
structs.ServiceList(foundKeys).Sort()
|
|
|
|
|
|
|
|
return maxIdx, foundKeys, nil
|
|
|
|
}
|
|
|
|
|
2022-05-06 19:35:31 +00:00
|
|
|
// PeeringsForService returns the list of peerings that are associated with the service name provided in the query.
|
|
|
|
// This is used to configure connect proxies for a given service. The result is generated by querying for exported
|
|
|
|
// service config entries and filtering for those that match the given service.
|
2022-05-23 16:16:39 +00:00
|
|
|
//
|
2022-05-06 19:35:31 +00:00
|
|
|
// TODO(peering): this implementation does all of the work on read to materialize this list of peerings, we should explore
|
|
|
|
// writing to a separate index that has service peerings prepared ahead of time should this become a performance bottleneck.
|
|
|
|
func (s *Store) PeeringsForService(ws memdb.WatchSet, serviceName string, entMeta acl.EnterpriseMeta) (uint64, []*pbpeering.Peering, error) {
|
|
|
|
tx := s.db.ReadTxn()
|
|
|
|
defer tx.Abort()
|
|
|
|
|
2022-05-23 23:57:42 +00:00
|
|
|
return peeringsForServiceTxn(tx, ws, serviceName, entMeta)
|
|
|
|
}
|
2022-05-23 16:16:39 +00:00
|
|
|
|
2022-05-23 23:57:42 +00:00
|
|
|
func peeringsForServiceTxn(tx ReadTxn, ws memdb.WatchSet, serviceName string, entMeta acl.EnterpriseMeta) (uint64, []*pbpeering.Peering, error) {
|
2022-05-23 16:16:39 +00:00
|
|
|
// Return the idx of the config entry so the caller can watch for changes.
|
2022-05-23 23:57:42 +00:00
|
|
|
maxIdx, peerNames, err := peersForServiceTxn(tx, ws, serviceName, &entMeta)
|
2022-05-06 19:35:31 +00:00
|
|
|
if err != nil {
|
2022-05-23 16:16:39 +00:00
|
|
|
return 0, nil, fmt.Errorf("failed to read peers for service name %q: %w", serviceName, err)
|
2022-05-06 19:35:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var peerings []*pbpeering.Peering
|
|
|
|
|
2022-05-23 16:16:39 +00:00
|
|
|
// Lookup and return the peering corresponding to each name.
|
|
|
|
for _, name := range peerNames {
|
2022-05-06 19:35:31 +00:00
|
|
|
readQuery := Query{
|
2022-05-23 16:16:39 +00:00
|
|
|
Value: name,
|
|
|
|
EnterpriseMeta: *structs.NodeEnterpriseMetaInPartition(entMeta.PartitionOrDefault()),
|
2022-05-06 19:35:31 +00:00
|
|
|
}
|
2022-05-23 23:57:42 +00:00
|
|
|
idx, peering, err := peeringReadTxn(tx, ws, readQuery)
|
2022-05-06 19:35:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, nil, fmt.Errorf("failed to read peering: %w", err)
|
|
|
|
}
|
2022-05-23 23:57:42 +00:00
|
|
|
if idx > maxIdx {
|
|
|
|
maxIdx = idx
|
|
|
|
}
|
2022-06-08 22:53:32 +00:00
|
|
|
if peering == nil || !peering.IsActive() {
|
2022-05-06 19:35:31 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
peerings = append(peerings, peering)
|
|
|
|
}
|
2022-05-23 23:57:42 +00:00
|
|
|
return maxIdx, peerings, nil
|
|
|
|
}
|
|
|
|
|
2022-06-27 19:37:18 +00:00
|
|
|
// TrustBundleListByService returns the trust bundles for all peers that the
|
|
|
|
// given service is exported to, via a discovery chain target.
|
|
|
|
func (s *Store) TrustBundleListByService(ws memdb.WatchSet, service, dc string, entMeta acl.EnterpriseMeta) (uint64, []*pbpeering.PeeringTrustBundle, error) {
|
2022-05-23 23:57:42 +00:00
|
|
|
tx := s.db.ReadTxn()
|
|
|
|
defer tx.Abort()
|
|
|
|
|
2022-06-27 19:37:18 +00:00
|
|
|
realSvc := structs.NewServiceName(service, &entMeta)
|
|
|
|
|
|
|
|
maxIdx, chainNames, err := s.discoveryChainSourcesTxn(tx, ws, dc, realSvc)
|
2022-05-23 23:57:42 +00:00
|
|
|
if err != nil {
|
2022-06-27 19:37:18 +00:00
|
|
|
return 0, nil, fmt.Errorf("failed to list all discovery chains referring to %q: %w", realSvc, err)
|
2022-05-23 23:57:42 +00:00
|
|
|
}
|
|
|
|
|
2022-06-27 19:37:18 +00:00
|
|
|
peerNames := make(map[string]struct{})
|
|
|
|
for _, chainSvc := range chainNames {
|
|
|
|
idx, peers, err := peeringsForServiceTxn(tx, ws, chainSvc.Name, chainSvc.EnterpriseMeta)
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, fmt.Errorf("failed to get peers for service %s: %v", chainSvc, err)
|
|
|
|
}
|
|
|
|
if idx > maxIdx {
|
|
|
|
maxIdx = idx
|
|
|
|
}
|
|
|
|
for _, peer := range peers {
|
|
|
|
peerNames[peer.Name] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
peerNamesSlice := maps.SliceOfKeys(peerNames)
|
|
|
|
sort.Strings(peerNamesSlice)
|
|
|
|
|
2022-05-23 23:57:42 +00:00
|
|
|
var resp []*pbpeering.PeeringTrustBundle
|
2022-06-27 19:37:18 +00:00
|
|
|
for _, peerName := range peerNamesSlice {
|
2022-05-23 23:57:42 +00:00
|
|
|
pq := Query{
|
2022-06-27 19:37:18 +00:00
|
|
|
Value: strings.ToLower(peerName),
|
2022-05-23 23:57:42 +00:00
|
|
|
EnterpriseMeta: *structs.NodeEnterpriseMetaInPartition(entMeta.PartitionOrDefault()),
|
|
|
|
}
|
|
|
|
idx, trustBundle, err := peeringTrustBundleReadTxn(tx, ws, pq)
|
|
|
|
if err != nil {
|
2022-06-27 19:37:18 +00:00
|
|
|
return 0, nil, fmt.Errorf("failed to read trust bundle for peer %s: %v", peerName, err)
|
2022-05-23 23:57:42 +00:00
|
|
|
}
|
|
|
|
if idx > maxIdx {
|
|
|
|
maxIdx = idx
|
|
|
|
}
|
|
|
|
if trustBundle != nil {
|
|
|
|
resp = append(resp, trustBundle)
|
|
|
|
}
|
|
|
|
}
|
2022-06-27 19:37:18 +00:00
|
|
|
|
2022-05-23 23:57:42 +00:00
|
|
|
return maxIdx, resp, nil
|
2022-05-06 19:35:31 +00:00
|
|
|
}
|
|
|
|
|
2022-06-15 19:36:18 +00:00
|
|
|
// PeeringTrustBundleList returns the peering trust bundles for all peers.
|
|
|
|
func (s *Store) PeeringTrustBundleList(ws memdb.WatchSet, entMeta acl.EnterpriseMeta) (uint64, []*pbpeering.PeeringTrustBundle, error) {
|
|
|
|
tx := s.db.ReadTxn()
|
|
|
|
defer tx.Abort()
|
|
|
|
|
|
|
|
return peeringTrustBundleListTxn(tx, ws, entMeta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func peeringTrustBundleListTxn(tx ReadTxn, ws memdb.WatchSet, entMeta acl.EnterpriseMeta) (uint64, []*pbpeering.PeeringTrustBundle, error) {
|
|
|
|
iter, err := tx.Get(tablePeeringTrustBundles, indexID+"_prefix", entMeta)
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, fmt.Errorf("failed peering trust bundle lookup: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
idx := maxIndexWatchTxn(tx, ws, partitionedIndexEntryName(tablePeeringTrustBundles, entMeta.PartitionOrDefault()))
|
|
|
|
|
|
|
|
var result []*pbpeering.PeeringTrustBundle
|
|
|
|
for entry := iter.Next(); entry != nil; entry = iter.Next() {
|
|
|
|
result = append(result, entry.(*pbpeering.PeeringTrustBundle))
|
|
|
|
}
|
|
|
|
|
|
|
|
return idx, result, nil
|
|
|
|
}
|
|
|
|
|
2022-05-06 19:35:31 +00:00
|
|
|
// PeeringTrustBundleRead returns the peering trust bundle for the peer name given as the query value.
|
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 (s *Store) PeeringTrustBundleRead(ws memdb.WatchSet, q Query) (uint64, *pbpeering.PeeringTrustBundle, error) {
|
|
|
|
tx := s.db.ReadTxn()
|
|
|
|
defer tx.Abort()
|
|
|
|
|
2022-05-23 23:57:42 +00:00
|
|
|
return peeringTrustBundleReadTxn(tx, ws, q)
|
|
|
|
}
|
|
|
|
|
|
|
|
func peeringTrustBundleReadTxn(tx ReadTxn, ws memdb.WatchSet, q Query) (uint64, *pbpeering.PeeringTrustBundle, error) {
|
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
|
|
|
watchCh, ptbRaw, err := tx.FirstWatch(tablePeeringTrustBundles, indexID, q)
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, fmt.Errorf("failed peering trust bundle lookup: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ptb, ok := ptbRaw.(*pbpeering.PeeringTrustBundle)
|
2022-05-06 19:35:31 +00:00
|
|
|
if ptbRaw != nil && !ok {
|
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
|
|
|
return 0, nil, fmt.Errorf("invalid type %T", ptb)
|
|
|
|
}
|
|
|
|
ws.Add(watchCh)
|
|
|
|
|
|
|
|
if ptb == nil {
|
|
|
|
// Return the tables index so caller can watch it for changes if the trust bundle doesn't exist
|
|
|
|
return maxIndexWatchTxn(tx, ws, partitionedIndexEntryName(tablePeeringTrustBundles, q.PartitionOrDefault())), nil, nil
|
|
|
|
}
|
|
|
|
return ptb.ModifyIndex, ptb, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PeeringTrustBundleWrite writes ptb to the state store. If there is an existing trust bundle with the given peer name,
|
|
|
|
// it will be overwritten.
|
|
|
|
func (s *Store) PeeringTrustBundleWrite(idx uint64, ptb *pbpeering.PeeringTrustBundle) error {
|
|
|
|
tx := s.db.WriteTxn(idx)
|
|
|
|
defer tx.Abort()
|
|
|
|
|
|
|
|
q := Query{
|
|
|
|
Value: ptb.PeerName,
|
|
|
|
EnterpriseMeta: *structs.NodeEnterpriseMetaInPartition(ptb.Partition),
|
|
|
|
}
|
|
|
|
existingRaw, err := tx.First(tablePeeringTrustBundles, indexID, q)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed peering trust bundle lookup: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
existing, ok := existingRaw.(*pbpeering.PeeringTrustBundle)
|
|
|
|
if existingRaw != nil && !ok {
|
|
|
|
return fmt.Errorf("invalid type %T", existingRaw)
|
|
|
|
}
|
|
|
|
|
|
|
|
if existing != nil {
|
|
|
|
ptb.CreateIndex = existing.CreateIndex
|
|
|
|
|
|
|
|
} else {
|
|
|
|
ptb.CreateIndex = idx
|
|
|
|
}
|
|
|
|
|
|
|
|
ptb.ModifyIndex = idx
|
|
|
|
|
|
|
|
if err := tx.Insert(tablePeeringTrustBundles, ptb); err != nil {
|
|
|
|
return fmt.Errorf("failed inserting peering trust bundle: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := updatePeeringTrustBundlesTableIndexes(tx, idx, ptb.PartitionOrDefault()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return tx.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) PeeringTrustBundleDelete(idx uint64, q Query) error {
|
|
|
|
tx := s.db.WriteTxn(idx)
|
|
|
|
defer tx.Abort()
|
|
|
|
|
|
|
|
existing, err := tx.First(tablePeeringTrustBundles, indexID, q)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed peering trust bundle lookup: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if existing == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tx.Delete(tablePeeringTrustBundles, existing); err != nil {
|
|
|
|
return fmt.Errorf("failed deleting peering trust bundle: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := updatePeeringTrustBundlesTableIndexes(tx, idx, q.PartitionOrDefault()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return tx.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Snapshot) Peerings() (memdb.ResultIterator, error) {
|
|
|
|
return s.tx.Get(tablePeering, indexName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Snapshot) PeeringTrustBundles() (memdb.ResultIterator, error) {
|
|
|
|
return s.tx.Get(tablePeeringTrustBundles, indexID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Restore) Peering(p *pbpeering.Peering) error {
|
|
|
|
if err := r.tx.Insert(tablePeering, p); err != nil {
|
|
|
|
return fmt.Errorf("failed restoring peering: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := updatePeeringTableIndexes(r.tx, p.ModifyIndex, p.PartitionOrDefault()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Restore) PeeringTrustBundle(ptb *pbpeering.PeeringTrustBundle) error {
|
|
|
|
if err := r.tx.Insert(tablePeeringTrustBundles, ptb); err != nil {
|
|
|
|
return fmt.Errorf("failed restoring peering trust bundle: %w", err)
|
|
|
|
}
|
|
|
|
if err := updatePeeringTrustBundlesTableIndexes(r.tx, ptb.ModifyIndex, ptb.PartitionOrDefault()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-05-06 19:35:31 +00:00
|
|
|
|
2022-05-23 16:16:39 +00:00
|
|
|
// peersForServiceTxn returns the names of all peers that a service is exported to.
|
|
|
|
func peersForServiceTxn(
|
2022-05-06 19:35:31 +00:00
|
|
|
tx ReadTxn,
|
|
|
|
ws memdb.WatchSet,
|
|
|
|
serviceName string,
|
|
|
|
entMeta *acl.EnterpriseMeta,
|
2022-05-23 16:16:39 +00:00
|
|
|
) (uint64, []string, error) {
|
|
|
|
// Exported service config entries are scoped to partitions so they are in the default namespace.
|
|
|
|
partitionMeta := structs.DefaultEnterpriseMetaInPartition(entMeta.PartitionOrDefault())
|
2022-05-06 19:35:31 +00:00
|
|
|
|
2022-05-23 16:16:39 +00:00
|
|
|
idx, rawEntry, err := configEntryTxn(tx, ws, structs.ExportedServices, partitionMeta.PartitionOrDefault(), partitionMeta)
|
2022-05-06 19:35:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
2022-05-23 16:16:39 +00:00
|
|
|
if rawEntry == nil {
|
|
|
|
return idx, nil, err
|
|
|
|
}
|
2022-05-06 19:35:31 +00:00
|
|
|
|
2022-05-23 16:16:39 +00:00
|
|
|
entry, ok := rawEntry.(*structs.ExportedServicesConfigEntry)
|
|
|
|
if !ok {
|
|
|
|
return 0, nil, fmt.Errorf("unexpected type %T for pbpeering.Peering index", rawEntry)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
wildcardNamespaceIdx = -1
|
|
|
|
wildcardServiceIdx = -1
|
|
|
|
exactMatchIdx = -1
|
|
|
|
)
|
|
|
|
|
|
|
|
// Ensure the metadata is defaulted since we make assertions against potentially empty values below.
|
|
|
|
// In OSS this is a no-op.
|
|
|
|
if entMeta == nil {
|
|
|
|
entMeta = acl.DefaultEnterpriseMeta()
|
|
|
|
}
|
|
|
|
entMeta.Normalize()
|
|
|
|
|
|
|
|
// Services can be exported via wildcards or by their exact name:
|
|
|
|
// Namespace: *, Service: *
|
|
|
|
// Namespace: Exact, Service: *
|
|
|
|
// Namespace: Exact, Service: Exact
|
|
|
|
for i, service := range entry.Services {
|
|
|
|
switch {
|
|
|
|
case service.Namespace == structs.WildcardSpecifier:
|
|
|
|
wildcardNamespaceIdx = i
|
|
|
|
|
2022-05-23 23:57:42 +00:00
|
|
|
case service.Name == structs.WildcardSpecifier && acl.EqualNamespaces(service.Namespace, entMeta.NamespaceOrDefault()):
|
2022-05-23 16:16:39 +00:00
|
|
|
wildcardServiceIdx = i
|
|
|
|
|
2022-05-23 23:57:42 +00:00
|
|
|
case service.Name == serviceName && acl.EqualNamespaces(service.Namespace, entMeta.NamespaceOrDefault()):
|
2022-05-23 16:16:39 +00:00
|
|
|
exactMatchIdx = i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var results []string
|
|
|
|
|
|
|
|
// Prefer the exact match over the wildcard match. This matches how we handle intention precedence.
|
|
|
|
var targetIdx int
|
|
|
|
switch {
|
|
|
|
case exactMatchIdx >= 0:
|
|
|
|
targetIdx = exactMatchIdx
|
|
|
|
|
|
|
|
case wildcardServiceIdx >= 0:
|
|
|
|
targetIdx = wildcardServiceIdx
|
|
|
|
|
|
|
|
case wildcardNamespaceIdx >= 0:
|
|
|
|
targetIdx = wildcardNamespaceIdx
|
|
|
|
|
|
|
|
default:
|
|
|
|
return idx, results, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range entry.Services[targetIdx].Consumers {
|
|
|
|
if c.PeerName != "" {
|
|
|
|
results = append(results, c.PeerName)
|
2022-05-06 19:35:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return idx, results, nil
|
|
|
|
}
|
2022-06-08 22:53:32 +00:00
|
|
|
|
|
|
|
func (s *Store) PeeringListDeleted(ws memdb.WatchSet) (uint64, []*pbpeering.Peering, error) {
|
|
|
|
tx := s.db.ReadTxn()
|
|
|
|
defer tx.Abort()
|
|
|
|
|
|
|
|
return peeringListDeletedTxn(tx, ws)
|
|
|
|
}
|
|
|
|
|
|
|
|
func peeringListDeletedTxn(tx ReadTxn, ws memdb.WatchSet) (uint64, []*pbpeering.Peering, error) {
|
|
|
|
iter, err := tx.Get(tablePeering, indexDeleted, BoolQuery{Value: true})
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, fmt.Errorf("failed peering lookup: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Instead of watching iter.WatchCh() we only need to watch the index entry for the peering table
|
|
|
|
// This is sufficient to pick up any changes to peerings.
|
|
|
|
idx := maxIndexWatchTxn(tx, ws, tablePeering)
|
|
|
|
|
|
|
|
var result []*pbpeering.Peering
|
|
|
|
for t := iter.Next(); t != nil; t = iter.Next() {
|
|
|
|
result = append(result, t.(*pbpeering.Peering))
|
|
|
|
}
|
|
|
|
|
|
|
|
return idx, result, nil
|
|
|
|
}
|