2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
|
|
|
|
memdb "github.com/hashicorp/go-memdb"
|
2020-09-03 23:38:03 +00:00
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
2020-09-03 23:38:03 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2020-10-06 18:24:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ServiceIntentionLegacyIDIndex struct {
|
|
|
|
uuidFieldIndex memdb.UUIDFieldIndex // for helper code
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ServiceIntentionLegacyIDIndex) FromObject(obj interface{}) (bool, [][]byte, error) {
|
|
|
|
entry, ok := obj.(structs.ConfigEntry)
|
|
|
|
if !ok {
|
|
|
|
return false, nil, fmt.Errorf("object is not a ConfigEntry")
|
|
|
|
}
|
|
|
|
|
|
|
|
if entry.GetKind() != structs.ServiceIntentions {
|
|
|
|
return false, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ixnEntry, ok := entry.(*structs.ServiceIntentionsConfigEntry)
|
|
|
|
if !ok {
|
|
|
|
return false, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't pre-size this slice because it will only be populated
|
|
|
|
// for legacy data, which should reduce over time.
|
|
|
|
var vals [][]byte
|
|
|
|
for _, src := range ixnEntry.Sources {
|
|
|
|
if src.LegacyID != "" {
|
|
|
|
arg, err := s.FromArgs(src.LegacyID)
|
|
|
|
if err != nil {
|
|
|
|
return false, nil, err
|
|
|
|
}
|
|
|
|
vals = append(vals, arg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(vals) == 0 {
|
|
|
|
return false, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, vals, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ServiceIntentionLegacyIDIndex) FromArgs(args ...interface{}) ([]byte, error) {
|
|
|
|
arg, err := s.uuidFieldIndex.FromArgs(args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the null character as a terminator
|
|
|
|
b := make([]byte, 0, len(arg)+1)
|
|
|
|
b = append(b, arg...)
|
|
|
|
b = append(b, '\x00')
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ServiceIntentionLegacyIDIndex) PrefixFromArgs(args ...interface{}) ([]byte, error) {
|
|
|
|
val, err := s.FromArgs(args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Strip the null terminator, the rest is a prefix
|
|
|
|
n := len(val)
|
|
|
|
if n > 0 {
|
|
|
|
return val[:n-1], nil
|
|
|
|
}
|
|
|
|
return val, nil
|
|
|
|
}
|
|
|
|
|
2023-05-25 16:18:55 +00:00
|
|
|
type SamenessGroupMemberIndex struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compile-time assert that these interfaces hold to ensure that the
|
|
|
|
// methods correctly exist across the oss/ent split.
|
|
|
|
var _ memdb.Indexer = (*SamenessGroupMemberIndex)(nil)
|
|
|
|
var _ memdb.MultiIndexer = (*SamenessGroupMemberIndex)(nil)
|
|
|
|
|
|
|
|
func (s *SamenessGroupMemberIndex) FromObject(obj interface{}) (bool, [][]byte, error) {
|
|
|
|
entry, ok := obj.(structs.ConfigEntry)
|
|
|
|
if !ok {
|
|
|
|
return false, nil, fmt.Errorf("object is not a ConfigEntry")
|
|
|
|
}
|
|
|
|
|
|
|
|
sg, ok := entry.(*structs.SamenessGroupConfigEntry)
|
|
|
|
if !ok {
|
|
|
|
return false, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
vals := make([][]byte, 0)
|
|
|
|
for _, m := range sg.AllMembers() {
|
|
|
|
if m.Partition == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// add 1 for null separator after each string
|
|
|
|
buf := newIndexBuilder(len(m.Partition) + 1)
|
|
|
|
buf.String(m.Partition)
|
|
|
|
vals = append(vals, buf.Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(vals) == 0 {
|
|
|
|
return false, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, vals, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SamenessGroupMemberIndex) FromArgs(args ...interface{}) ([]byte, error) {
|
|
|
|
if len(args) != 1 {
|
|
|
|
return nil, fmt.Errorf("must provide only a single argument")
|
|
|
|
}
|
|
|
|
arg, ok := args[0].(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("argument must be a string: %#v", args[0])
|
|
|
|
}
|
|
|
|
buf := newIndexBuilder(len(arg) + 1)
|
|
|
|
buf.String(arg)
|
|
|
|
// Add the null character as a terminator
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type ServiceIntentionSourceSamenessGroupIndex struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compile-time assert that these interfaces hold to ensure that the
|
|
|
|
// methods correctly exist across the oss/ent split.
|
|
|
|
var _ memdb.Indexer = (*ServiceIntentionSourceSamenessGroupIndex)(nil)
|
|
|
|
var _ memdb.MultiIndexer = (*ServiceIntentionSourceSamenessGroupIndex)(nil)
|
|
|
|
|
|
|
|
func (s *ServiceIntentionSourceSamenessGroupIndex) FromObject(obj interface{}) (bool, [][]byte, error) {
|
|
|
|
entry, ok := obj.(structs.ConfigEntry)
|
|
|
|
if !ok {
|
|
|
|
return false, nil, fmt.Errorf("object is not a ConfigEntry")
|
|
|
|
}
|
|
|
|
|
|
|
|
ixnEntry, ok := entry.(*structs.ServiceIntentionsConfigEntry)
|
|
|
|
if !ok {
|
|
|
|
return false, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
vals := make([][]byte, 0, len(ixnEntry.Sources))
|
|
|
|
for _, src := range ixnEntry.Sources {
|
|
|
|
sg := src.SamenessGroup
|
|
|
|
if sg == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
sn := structs.ServiceName{
|
|
|
|
Name: src.Name,
|
|
|
|
EnterpriseMeta: acl.NewEnterpriseMetaWithPartition(ixnEntry.PartitionOrDefault(), src.NamespaceOrDefault()),
|
|
|
|
}.String()
|
|
|
|
|
|
|
|
// add 2 for null separator after each string
|
|
|
|
buf := newIndexBuilder(len(sg) + len(sn) + 2)
|
|
|
|
buf.String(sg)
|
|
|
|
buf.String(sn)
|
|
|
|
vals = append(vals, buf.Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(vals) == 0 {
|
|
|
|
return false, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, vals, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ServiceIntentionSourceSamenessGroupIndex) FromArgs(args ...interface{}) ([]byte, error) {
|
|
|
|
if len(args) != 1 {
|
|
|
|
return nil, fmt.Errorf("must provide only a single argument")
|
|
|
|
}
|
|
|
|
arg, ok := args[0].(structs.ServiceNameWithSamenessGroup)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("argument must be a structs.ServiceID: %#v", args[0])
|
|
|
|
}
|
|
|
|
// Intention queries cannot use a peered service as a source
|
|
|
|
sg := arg.SamenessGroup
|
|
|
|
sn := arg.ServiceName.String()
|
|
|
|
// add 2 for null separator after each string
|
|
|
|
buf := newIndexBuilder(len(sg) + len(sn) + 2)
|
|
|
|
buf.String(sg)
|
|
|
|
buf.String(sn)
|
|
|
|
// Add the null character as a terminator
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
type ServiceIntentionSourceIndex struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compile-time assert that these interfaces hold to ensure that the
|
|
|
|
// methods correctly exist across the oss/ent split.
|
|
|
|
var _ memdb.Indexer = (*ServiceIntentionSourceIndex)(nil)
|
|
|
|
var _ memdb.MultiIndexer = (*ServiceIntentionSourceIndex)(nil)
|
|
|
|
|
|
|
|
func (s *ServiceIntentionSourceIndex) FromObject(obj interface{}) (bool, [][]byte, error) {
|
|
|
|
entry, ok := obj.(structs.ConfigEntry)
|
|
|
|
if !ok {
|
|
|
|
return false, nil, fmt.Errorf("object is not a ConfigEntry")
|
|
|
|
}
|
|
|
|
|
|
|
|
if entry.GetKind() != structs.ServiceIntentions {
|
|
|
|
return false, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ixnEntry, ok := entry.(*structs.ServiceIntentionsConfigEntry)
|
|
|
|
if !ok {
|
|
|
|
return false, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
vals := make([][]byte, 0, len(ixnEntry.Sources))
|
|
|
|
for _, src := range ixnEntry.Sources {
|
2023-05-25 16:18:55 +00:00
|
|
|
if src.SamenessGroup != "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-06-07 23:26:23 +00:00
|
|
|
peer := src.Peer
|
|
|
|
if peer == "" {
|
|
|
|
peer = structs.LocalPeerKeyword
|
|
|
|
}
|
|
|
|
sn := src.SourceServiceName().String()
|
|
|
|
|
|
|
|
// add 2 for null separator after each string
|
|
|
|
buf := newIndexBuilder(len(peer) + len(sn) + 2)
|
|
|
|
buf.String(peer)
|
|
|
|
buf.String(sn)
|
|
|
|
vals = append(vals, buf.Bytes())
|
2020-10-06 18:24:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(vals) == 0 {
|
|
|
|
return false, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, vals, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ServiceIntentionSourceIndex) FromArgs(args ...interface{}) ([]byte, error) {
|
|
|
|
if len(args) != 1 {
|
|
|
|
return nil, fmt.Errorf("must provide only a single argument")
|
|
|
|
}
|
|
|
|
arg, ok := args[0].(structs.ServiceName)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("argument must be a structs.ServiceID: %#v", args[0])
|
|
|
|
}
|
2022-06-07 23:54:09 +00:00
|
|
|
// Intention queries cannot use a peered service as a source
|
|
|
|
peer := structs.LocalPeerKeyword
|
|
|
|
sn := arg.String()
|
|
|
|
// add 2 for null separator after each string
|
|
|
|
buf := newIndexBuilder(len(peer) + len(sn) + 2)
|
|
|
|
buf.String(peer)
|
|
|
|
buf.String(sn)
|
2020-10-06 18:24:05 +00:00
|
|
|
// Add the null character as a terminator
|
2022-06-07 23:54:09 +00:00
|
|
|
return buf.Bytes(), nil
|
2020-10-06 18:24:05 +00:00
|
|
|
}
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
func configIntentionsListTxn(tx ReadTxn, ws memdb.WatchSet, entMeta *acl.EnterpriseMeta) (uint64, structs.Intentions, bool, error) {
|
2020-10-06 18:24:05 +00:00
|
|
|
// unrolled part of configEntriesByKindTxn
|
|
|
|
|
2021-01-29 01:34:34 +00:00
|
|
|
idx := maxIndexTxn(tx, tableConfigEntries)
|
2020-10-06 18:24:05 +00:00
|
|
|
|
2021-10-13 14:47:12 +00:00
|
|
|
iter, err := getAllConfigEntriesByKindWithTxn(tx, structs.ServiceIntentions)
|
2020-10-06 18:24:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, nil, false, fmt.Errorf("failed config entry lookup: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ws.Add(iter.WatchCh())
|
|
|
|
|
|
|
|
results := configIntentionsConvertToList(iter, entMeta)
|
|
|
|
|
|
|
|
// Sort by precedence just because that's nicer and probably what most clients
|
|
|
|
// want for presentation.
|
|
|
|
sort.Sort(structs.IntentionPrecedenceSorter(results))
|
|
|
|
|
|
|
|
return idx, results, true, nil
|
|
|
|
}
|
|
|
|
|
2021-03-10 18:00:29 +00:00
|
|
|
func configIntentionGetTxn(tx ReadTxn, ws memdb.WatchSet, id string) (uint64, *structs.ServiceIntentionsConfigEntry, *structs.Intention, error) {
|
2021-01-29 01:34:34 +00:00
|
|
|
idx := maxIndexTxn(tx, tableConfigEntries)
|
2020-10-06 18:24:05 +00:00
|
|
|
if idx < 1 {
|
|
|
|
idx = 1
|
|
|
|
}
|
|
|
|
|
2021-04-15 17:26:53 +00:00
|
|
|
watchCh, existing, err := tx.FirstWatch(tableConfigEntries, indexIntentionLegacyID, id)
|
2020-10-06 18:24:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, nil, nil, fmt.Errorf("failed config entry lookup: %s", err)
|
|
|
|
}
|
|
|
|
ws.Add(watchCh)
|
|
|
|
if existing == nil {
|
|
|
|
return idx, nil, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
conf, ok := existing.(*structs.ServiceIntentionsConfigEntry)
|
|
|
|
if !ok {
|
|
|
|
return 0, nil, nil, fmt.Errorf("config entry is an invalid type: %T", conf)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, src := range conf.Sources {
|
|
|
|
if src.LegacyID == id {
|
|
|
|
return idx, conf, conf.ToIntention(src), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return idx, nil, nil, nil // Shouldn't happen.
|
|
|
|
}
|
|
|
|
|
2020-09-03 23:38:03 +00:00
|
|
|
func (s *Store) configIntentionGetExactTxn(tx ReadTxn, ws memdb.WatchSet, args *structs.IntentionQueryExact) (uint64, *structs.ServiceIntentionsConfigEntry, *structs.Intention, error) {
|
2020-10-06 18:24:05 +00:00
|
|
|
if err := args.Validate(); err != nil {
|
|
|
|
return 0, nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
idx, entry, err := getServiceIntentionsConfigEntryTxn(tx, ws, args.DestinationName, nil, args.DestinationEnterpriseMeta())
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, nil, err
|
|
|
|
} else if entry == nil {
|
|
|
|
return idx, nil, nil, nil
|
|
|
|
}
|
|
|
|
|
2022-06-16 17:27:31 +00:00
|
|
|
psn := structs.PeeredServiceName{
|
|
|
|
Peer: args.SourcePeer,
|
|
|
|
ServiceName: structs.NewServiceName(args.SourceName, args.SourceEnterpriseMeta()),
|
|
|
|
}
|
2020-10-06 18:24:05 +00:00
|
|
|
|
|
|
|
for _, src := range entry.Sources {
|
2022-06-16 17:27:31 +00:00
|
|
|
if psn.Peer == src.Peer && psn.ServiceName == src.SourceServiceName() {
|
2020-10-06 18:24:05 +00:00
|
|
|
return idx, entry, entry.ToIntention(src), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return idx, nil, nil, nil
|
|
|
|
}
|
|
|
|
|
2020-09-03 23:38:03 +00:00
|
|
|
func (s *Store) configIntentionMatchTxn(tx ReadTxn, ws memdb.WatchSet, args *structs.IntentionQueryMatch) (uint64, []structs.Intentions, error) {
|
2020-10-06 18:24:05 +00:00
|
|
|
maxIndex := uint64(1)
|
|
|
|
|
|
|
|
// Make all the calls and accumulate the results
|
|
|
|
results := make([]structs.Intentions, len(args.Entries))
|
|
|
|
for i, entry := range args.Entries {
|
|
|
|
// Note on performance: This is not the most optimal set of queries
|
|
|
|
// since we repeat some many times (such as */*). We can work on
|
|
|
|
// improving that in the future, the test cases shouldn't have to
|
|
|
|
// change for that.
|
|
|
|
|
2022-06-07 19:03:59 +00:00
|
|
|
index, ixns, err := configIntentionMatchOneTxn(tx, ws, entry, args.Type, structs.IntentionTargetService)
|
2020-10-06 18:24:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
if index > maxIndex {
|
|
|
|
maxIndex = index
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the result
|
|
|
|
results[i] = ixns
|
|
|
|
}
|
|
|
|
|
|
|
|
return maxIndex, results, nil
|
|
|
|
}
|
|
|
|
|
2020-09-03 23:38:03 +00:00
|
|
|
func configIntentionMatchOneTxn(
|
2022-06-07 19:03:59 +00:00
|
|
|
tx ReadTxn, ws memdb.WatchSet,
|
2020-10-06 18:24:05 +00:00
|
|
|
matchEntry structs.IntentionMatchEntry,
|
|
|
|
matchType structs.IntentionMatchType,
|
2022-06-07 19:03:59 +00:00
|
|
|
targetType structs.IntentionTargetType,
|
2020-10-06 18:24:05 +00:00
|
|
|
) (uint64, structs.Intentions, error) {
|
|
|
|
switch matchType {
|
2022-06-07 19:03:59 +00:00
|
|
|
// targetType is only relevant for Source matches as egress Destinations can only be Intention Destinations in the mesh
|
2020-10-06 18:24:05 +00:00
|
|
|
case structs.IntentionMatchSource:
|
2022-06-07 19:03:59 +00:00
|
|
|
return readSourceIntentionsFromConfigEntriesTxn(tx, ws, matchEntry.Name, matchEntry.GetEnterpriseMeta(), targetType)
|
2020-10-06 18:24:05 +00:00
|
|
|
case structs.IntentionMatchDestination:
|
2020-09-03 23:38:03 +00:00
|
|
|
return readDestinationIntentionsFromConfigEntriesTxn(tx, ws, matchEntry.Name, matchEntry.GetEnterpriseMeta())
|
2020-10-06 18:24:05 +00:00
|
|
|
default:
|
|
|
|
return 0, nil, fmt.Errorf("invalid intention match type: %s", matchType)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-07 19:03:59 +00:00
|
|
|
func readSourceIntentionsFromConfigEntriesTxn(
|
|
|
|
tx ReadTxn,
|
|
|
|
ws memdb.WatchSet,
|
|
|
|
serviceName string,
|
|
|
|
entMeta *acl.EnterpriseMeta,
|
|
|
|
targetType structs.IntentionTargetType,
|
|
|
|
) (uint64, structs.Intentions, error) {
|
2021-01-29 01:34:34 +00:00
|
|
|
idx := maxIndexTxn(tx, tableConfigEntries)
|
2020-10-06 18:24:05 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
results structs.Intentions
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
names := getIntentionPrecedenceMatchServiceNames(serviceName, entMeta)
|
|
|
|
for _, sn := range names {
|
2022-06-07 19:03:59 +00:00
|
|
|
results, err = readSourceIntentionsFromConfigEntriesForServiceTxn(tx, ws, sn.Name, &sn.EnterpriseMeta, results, targetType)
|
2020-10-06 18:24:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
2023-05-25 16:18:55 +00:00
|
|
|
|
|
|
|
results, err = readSourceSamenessIntentionsFromConfigEntriesForServiceTxn(tx, ws, sn.Name, &sn.EnterpriseMeta, results, targetType)
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
2020-10-06 18:24:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sort the results by precedence
|
|
|
|
sort.Sort(structs.IntentionPrecedenceSorter(results))
|
|
|
|
|
|
|
|
return idx, results, nil
|
|
|
|
}
|
|
|
|
|
2022-06-07 19:03:59 +00:00
|
|
|
func readSourceIntentionsFromConfigEntriesForServiceTxn(
|
|
|
|
tx ReadTxn,
|
|
|
|
ws memdb.WatchSet,
|
|
|
|
serviceName string,
|
2023-05-25 16:18:55 +00:00
|
|
|
sourceEntMeta *acl.EnterpriseMeta,
|
2022-06-07 19:03:59 +00:00
|
|
|
results structs.Intentions,
|
|
|
|
targetType structs.IntentionTargetType,
|
|
|
|
) (structs.Intentions, error) {
|
2023-05-25 16:18:55 +00:00
|
|
|
sn := structs.NewServiceName(serviceName, sourceEntMeta)
|
2021-04-15 17:26:53 +00:00
|
|
|
iter, err := tx.Get(tableConfigEntries, indexSource, sn)
|
2020-10-06 18:24:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed config entry lookup: %s", err)
|
|
|
|
}
|
|
|
|
ws.Add(iter.WatchCh())
|
|
|
|
|
|
|
|
for v := iter.Next(); v != nil; v = iter.Next() {
|
|
|
|
entry := v.(*structs.ServiceIntentionsConfigEntry)
|
2022-06-08 14:38:55 +00:00
|
|
|
entMeta := entry.DestinationServiceName().EnterpriseMeta
|
2023-05-25 16:18:55 +00:00
|
|
|
|
|
|
|
kind, err := serviceIntentionsToGatewayServiceKind(tx, entry.DestinationServiceName().Name, entMeta)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-06-08 14:38:55 +00:00
|
|
|
}
|
2023-05-25 16:18:55 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
for _, src := range entry.Sources {
|
|
|
|
if src.SourceServiceName() == sn {
|
2023-05-25 16:18:55 +00:00
|
|
|
canAdd, err := intentionMatches(targetType, kind, entry.HasWildcardDestination())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if canAdd {
|
|
|
|
results = append(results, entry.ToIntention(src))
|
2022-06-07 19:03:59 +00:00
|
|
|
}
|
2020-10-06 18:24:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
2023-05-25 16:18:55 +00:00
|
|
|
func serviceIntentionsToGatewayServiceKind(tx ReadTxn, serviceName string, entMeta acl.EnterpriseMeta) (structs.GatewayServiceKind, error) {
|
|
|
|
var err error
|
|
|
|
kind := structs.GatewayServiceKindService
|
|
|
|
|
|
|
|
// if we have a wildcard namespace or partition assume we are querying a service intention
|
|
|
|
// as destination intentions will never be queried as wildcard
|
|
|
|
if entMeta.NamespaceOrDefault() != acl.WildcardName && entMeta.PartitionOrDefault() != acl.WildcardName {
|
|
|
|
kind, err = GatewayServiceKind(tx, serviceName, &entMeta)
|
|
|
|
if err != nil {
|
|
|
|
return kind, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return kind, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func intentionMatches(targetType structs.IntentionTargetType, kind structs.GatewayServiceKind, wildcardDestination bool) (bool, error) {
|
|
|
|
var canAdd bool
|
|
|
|
switch targetType {
|
|
|
|
case structs.IntentionTargetService:
|
|
|
|
canAdd = kind == structs.GatewayServiceKindService || kind == structs.GatewayServiceKindUnknown
|
|
|
|
case structs.IntentionTargetDestination:
|
|
|
|
// wildcard is needed here to be able to consider destinations in the wildcard intentions
|
|
|
|
canAdd = kind == structs.GatewayServiceKindDestination || wildcardDestination
|
|
|
|
default:
|
|
|
|
return false, fmt.Errorf("invalid target type")
|
|
|
|
}
|
|
|
|
|
|
|
|
return canAdd, nil
|
|
|
|
}
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
func readDestinationIntentionsFromConfigEntriesTxn(tx ReadTxn, ws memdb.WatchSet, serviceName string, entMeta *acl.EnterpriseMeta) (uint64, structs.Intentions, error) {
|
2021-01-29 01:34:34 +00:00
|
|
|
idx := maxIndexTxn(tx, tableConfigEntries)
|
2020-10-06 18:24:05 +00:00
|
|
|
|
|
|
|
var results structs.Intentions
|
|
|
|
|
|
|
|
names := getIntentionPrecedenceMatchServiceNames(serviceName, entMeta)
|
|
|
|
for _, sn := range names {
|
|
|
|
_, entry, err := getServiceIntentionsConfigEntryTxn(tx, ws, sn.Name, nil, &sn.EnterpriseMeta)
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
} else if entry != nil {
|
|
|
|
results = append(results, entry.ToIntentions()...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Sort the results by precedence
|
|
|
|
sort.Sort(structs.IntentionPrecedenceSorter(results))
|
|
|
|
|
|
|
|
return idx, results, nil
|
|
|
|
}
|