consul/internal/resource/bound_refs.go
R.B. Boyer 6742340878
mesh: add ComputedImplicitDestinations resource for future use (#20547)
Creates a new controller to create ComputedImplicitDestinations resources by 
composing ComputedRoutes, Services, and ComputedTrafficPermissions to 
infer all ParentRef services that could possibly send some portion of traffic to a 
Service that has at least one accessible Workload Identity. A followup PR will 
rewire the sidecar controller to make use of this new resource.

As this is a performance optimization, rather than a security feature the following 
aspects of traffic permissions have been ignored:

- DENY rules
- port rules (all ports are allowed)

Also:

- Add some v2 TestController machinery to help test complex dependency mappers.
2024-02-09 15:42:10 -06:00

66 lines
1.2 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package resource
import (
"sort"
"github.com/hashicorp/consul/proto-public/pbresource"
)
type sectionRefKey struct {
ReferenceKey
Section string
}
type BoundReferenceCollector struct {
refs map[sectionRefKey]*pbresource.Reference
}
func NewBoundReferenceCollector() *BoundReferenceCollector {
return &BoundReferenceCollector{
refs: make(map[sectionRefKey]*pbresource.Reference),
}
}
func (c *BoundReferenceCollector) List() []*pbresource.Reference {
if len(c.refs) == 0 {
return nil
}
out := make([]*pbresource.Reference, 0, len(c.refs))
for _, ref := range c.refs {
out = append(out, ref)
}
sort.Slice(out, func(i, j int) bool {
return LessReference(out[i], out[j])
})
return out
}
func (c *BoundReferenceCollector) AddRefOrID(ref ReferenceOrID) {
if c == nil {
return
}
c.AddRef(ReferenceFromReferenceOrID(ref))
}
func (c *BoundReferenceCollector) AddRef(ref *pbresource.Reference) {
if c == nil {
return
}
srk := sectionRefKey{
ReferenceKey: NewReferenceKey(ref),
Section: ref.Section,
}
if _, ok := c.refs[srk]; ok {
return
}
c.refs[srk] = ref
}