2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 18:39:22 +00:00
|
|
|
|
2022-01-20 16:12:04 +00:00
|
|
|
package proxycfg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
2022-01-20 16:12:04 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
|
|
|
)
|
|
|
|
|
2022-07-13 16:14:57 +00:00
|
|
|
type PeerName = string
|
|
|
|
|
2022-01-20 16:12:04 +00:00
|
|
|
type UpstreamID struct {
|
|
|
|
Type string
|
|
|
|
Name string
|
|
|
|
Datacenter string
|
2022-04-29 22:12:51 +00:00
|
|
|
// If Peer is not empty, Namespace refers to the remote
|
|
|
|
// peer namespace and Partition refers to the local partition
|
|
|
|
Peer string
|
2022-04-05 21:10:06 +00:00
|
|
|
acl.EnterpriseMeta
|
2022-01-20 16:12:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewUpstreamID(u *structs.Upstream) UpstreamID {
|
|
|
|
id := UpstreamID{
|
|
|
|
Type: u.DestinationType,
|
|
|
|
Name: u.DestinationName,
|
|
|
|
Datacenter: u.Datacenter,
|
2022-04-05 21:10:06 +00:00
|
|
|
EnterpriseMeta: acl.NewEnterpriseMetaWithPartition(
|
2022-01-20 16:12:04 +00:00
|
|
|
u.DestinationPartition,
|
|
|
|
u.DestinationNamespace,
|
|
|
|
),
|
2022-04-29 22:12:51 +00:00
|
|
|
Peer: u.DestinationPeer,
|
2022-01-20 16:12:04 +00:00
|
|
|
}
|
|
|
|
id.normalize()
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
2022-07-13 16:14:57 +00:00
|
|
|
func NewUpstreamIDFromPeeredServiceName(psn structs.PeeredServiceName) UpstreamID {
|
|
|
|
id := UpstreamID{
|
|
|
|
Name: psn.ServiceName.Name,
|
|
|
|
EnterpriseMeta: psn.ServiceName.EnterpriseMeta,
|
|
|
|
Peer: psn.Peer,
|
|
|
|
}
|
|
|
|
id.normalize()
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
2022-01-20 16:12:04 +00:00
|
|
|
func NewUpstreamIDFromServiceName(sn structs.ServiceName) UpstreamID {
|
|
|
|
id := UpstreamID{
|
|
|
|
Name: sn.Name,
|
|
|
|
EnterpriseMeta: sn.EnterpriseMeta,
|
|
|
|
}
|
|
|
|
id.normalize()
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
2022-04-29 22:12:51 +00:00
|
|
|
// TODO(peering): confirm we don't need peername here
|
2022-01-20 16:12:04 +00:00
|
|
|
func NewUpstreamIDFromServiceID(sid structs.ServiceID) UpstreamID {
|
|
|
|
id := UpstreamID{
|
|
|
|
Name: sid.ID,
|
|
|
|
EnterpriseMeta: sid.EnterpriseMeta,
|
|
|
|
}
|
|
|
|
id.normalize()
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
2022-01-28 03:52:26 +00:00
|
|
|
func NewUpstreamIDFromTargetID(tid string) UpstreamID {
|
2022-08-23 13:13:43 +00:00
|
|
|
var id UpstreamID
|
|
|
|
split := strings.Split(tid, ".")
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case split[len(split)-2] == "external":
|
|
|
|
id = UpstreamID{
|
|
|
|
Name: split[0],
|
|
|
|
EnterpriseMeta: acl.NewEnterpriseMetaWithPartition(split[2], split[1]),
|
|
|
|
Peer: split[4],
|
|
|
|
}
|
|
|
|
case len(split) == 5:
|
|
|
|
// Drop the leading subset if one is present in the target ID.
|
|
|
|
split = split[1:]
|
|
|
|
fallthrough
|
|
|
|
default:
|
|
|
|
id = UpstreamID{
|
|
|
|
Name: split[0],
|
|
|
|
EnterpriseMeta: acl.NewEnterpriseMetaWithPartition(split[2], split[1]),
|
|
|
|
Datacenter: split[3],
|
|
|
|
}
|
2022-01-28 03:52:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
id.normalize()
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
2022-01-20 16:12:04 +00:00
|
|
|
func (u *UpstreamID) normalize() {
|
|
|
|
if u.Type == structs.UpstreamDestTypeService {
|
|
|
|
u.Type = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
u.EnterpriseMeta.Normalize()
|
|
|
|
}
|
|
|
|
|
|
|
|
// String encodes the UpstreamID into a string for use in agent cache keys.
|
|
|
|
// You can decode it back again using UpstreamIDFromString.
|
|
|
|
func (u UpstreamID) String() string {
|
2022-04-29 22:12:51 +00:00
|
|
|
return UpstreamIDString(u.Type, u.Datacenter, u.Name, &u.EnterpriseMeta, u.Peer)
|
2022-01-20 16:12:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u UpstreamID) GoString() string {
|
|
|
|
return u.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func UpstreamIDFromString(input string) UpstreamID {
|
2022-04-29 22:12:51 +00:00
|
|
|
typ, dc, name, entMeta, peerName := ParseUpstreamIDString(input)
|
2022-01-20 16:12:04 +00:00
|
|
|
id := UpstreamID{
|
|
|
|
Type: typ,
|
|
|
|
Datacenter: dc,
|
|
|
|
Name: name,
|
|
|
|
EnterpriseMeta: *entMeta,
|
2022-04-29 22:12:51 +00:00
|
|
|
Peer: peerName,
|
2022-01-20 16:12:04 +00:00
|
|
|
}
|
|
|
|
id.normalize()
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
|
|
|
const upstreamTypePreparedQueryPrefix = structs.UpstreamDestTypePreparedQuery + ":"
|
|
|
|
|
2022-04-29 22:12:51 +00:00
|
|
|
func ParseUpstreamIDString(input string) (typ, dc, name string, meta *acl.EnterpriseMeta, peerName string) {
|
2022-01-20 16:12:04 +00:00
|
|
|
if strings.HasPrefix(input, upstreamTypePreparedQueryPrefix) {
|
|
|
|
typ = structs.UpstreamDestTypePreparedQuery
|
|
|
|
input = strings.TrimPrefix(input, upstreamTypePreparedQueryPrefix)
|
|
|
|
}
|
|
|
|
|
2022-04-29 22:12:51 +00:00
|
|
|
before, after, found := strings.Cut(input, "?")
|
|
|
|
input = before
|
|
|
|
if found {
|
|
|
|
if _, peerVal, ok := strings.Cut(after, "peer="); ok {
|
|
|
|
peerName = peerVal
|
|
|
|
} else if _, dcVal, ok2 := strings.Cut(after, "dc="); ok2 {
|
|
|
|
dc = dcVal
|
|
|
|
}
|
2022-01-20 16:12:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
name, meta = parseInnerUpstreamIDString(input)
|
|
|
|
|
2022-04-29 22:12:51 +00:00
|
|
|
return typ, dc, name, meta, peerName
|
2022-01-20 16:12:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// EnvoyID returns a string representation that uniquely identifies the
|
|
|
|
// upstream in a canonical but human readable way.
|
|
|
|
//
|
|
|
|
// This should be used for any situation where we generate identifiers in Envoy
|
|
|
|
// xDS structures for this upstream.
|
|
|
|
//
|
2023-08-22 14:46:03 +00:00
|
|
|
// This will ensure that generated identifiers for the same thing in CE and
|
2022-01-20 16:12:04 +00:00
|
|
|
// Enterprise render the same and omit default namespaces and partitions.
|
|
|
|
func (u UpstreamID) EnvoyID() string {
|
|
|
|
name := u.enterpriseIdentifierPrefix() + u.Name
|
|
|
|
typ := u.Type
|
|
|
|
|
2022-04-29 22:12:51 +00:00
|
|
|
if u.Peer != "" {
|
|
|
|
name += "?peer=" + u.Peer
|
|
|
|
} else if u.Datacenter != "" {
|
2022-01-20 16:12:04 +00:00
|
|
|
name += "?dc=" + u.Datacenter
|
|
|
|
}
|
|
|
|
|
|
|
|
// Service is default type so never prefix it. This is more readable and long
|
|
|
|
// term it is the only type that matters so we can drop the prefix and have
|
|
|
|
// nicer naming in metrics etc.
|
|
|
|
if typ == "" || typ == structs.UpstreamDestTypeService {
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
return typ + ":" + name
|
|
|
|
}
|
|
|
|
|
|
|
|
func UpstreamsToMap(us structs.Upstreams) map[UpstreamID]*structs.Upstream {
|
|
|
|
upstreamMap := make(map[UpstreamID]*structs.Upstream)
|
|
|
|
|
|
|
|
for i := range us {
|
|
|
|
u := us[i]
|
|
|
|
upstreamMap[NewUpstreamID(&u)] = &u
|
|
|
|
}
|
|
|
|
return upstreamMap
|
|
|
|
}
|
2023-01-18 19:43:53 +00:00
|
|
|
|
|
|
|
func NewWildcardUID(entMeta *acl.EnterpriseMeta) UpstreamID {
|
|
|
|
wildcardSID := structs.NewServiceID(
|
|
|
|
structs.WildcardSpecifier,
|
|
|
|
entMeta.WithWildcardNamespace(),
|
|
|
|
)
|
|
|
|
return NewUpstreamIDFromServiceID(wildcardSID)
|
|
|
|
}
|