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
|
|
|
|
2019-08-19 18:03:03 +00:00
|
|
|
package connect
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-09-01 14:35:39 +00:00
|
|
|
"strings"
|
2019-08-19 18:03:03 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
|
|
|
)
|
|
|
|
|
2021-09-01 14:35:39 +00:00
|
|
|
const (
|
|
|
|
internal = "internal"
|
|
|
|
version = "v1"
|
|
|
|
internalVersion = internal + "-" + version
|
2022-05-25 17:37:44 +00:00
|
|
|
external = "external"
|
2021-09-01 14:35:39 +00:00
|
|
|
)
|
|
|
|
|
2019-08-19 18:03:03 +00:00
|
|
|
func UpstreamSNI(u *structs.Upstream, subset string, dc string, trustDomain string) string {
|
|
|
|
if u.Datacenter != "" {
|
|
|
|
dc = u.Datacenter
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.DestinationType == structs.UpstreamDestTypePreparedQuery {
|
|
|
|
return QuerySNI(u.DestinationName, dc, trustDomain)
|
|
|
|
}
|
2022-04-29 22:12:51 +00:00
|
|
|
// TODO(peering): account for peer here?
|
2021-09-01 14:35:39 +00:00
|
|
|
return ServiceSNI(u.DestinationName, subset, u.DestinationNamespace, u.DestinationPartition, dc, trustDomain)
|
2019-08-19 18:03:03 +00:00
|
|
|
}
|
|
|
|
|
2021-10-24 15:16:28 +00:00
|
|
|
func GatewaySNI(dc string, partition, trustDomain string) string {
|
|
|
|
if partition == "" {
|
2023-08-22 14:46:03 +00:00
|
|
|
// TODO(partitions) Make default available in CE as a constant for uses like this one
|
2021-10-24 15:16:28 +00:00
|
|
|
partition = "default"
|
|
|
|
}
|
|
|
|
|
|
|
|
switch partition {
|
|
|
|
case "default":
|
|
|
|
return dotJoin(dc, internal, trustDomain)
|
|
|
|
default:
|
|
|
|
return dotJoin(partition, dc, internalVersion, trustDomain)
|
|
|
|
}
|
2019-08-19 18:03:03 +00:00
|
|
|
}
|
|
|
|
|
2021-09-01 14:35:39 +00:00
|
|
|
func ServiceSNI(service string, subset string, namespace string, partition string, datacenter string, trustDomain string) string {
|
2019-08-19 18:03:03 +00:00
|
|
|
if namespace == "" {
|
2021-10-26 21:42:30 +00:00
|
|
|
namespace = structs.IntentionDefaultNamespace
|
2019-08-19 18:03:03 +00:00
|
|
|
}
|
2021-09-01 14:35:39 +00:00
|
|
|
if partition == "" {
|
2023-08-22 14:46:03 +00:00
|
|
|
// TODO(partitions) Make default available in CE as a constant for uses like this one
|
2021-09-01 14:35:39 +00:00
|
|
|
partition = "default"
|
|
|
|
}
|
2019-08-19 18:03:03 +00:00
|
|
|
|
2021-09-01 14:35:39 +00:00
|
|
|
switch partition {
|
|
|
|
case "default":
|
|
|
|
if subset == "" {
|
|
|
|
return dotJoin(service, namespace, datacenter, internal, trustDomain)
|
|
|
|
} else {
|
|
|
|
return dotJoin(subset, service, namespace, datacenter, internal, trustDomain)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
if subset == "" {
|
|
|
|
return dotJoin(service, namespace, partition, datacenter, internalVersion, trustDomain)
|
|
|
|
} else {
|
|
|
|
return dotJoin(subset, service, namespace, partition, datacenter, internalVersion, trustDomain)
|
|
|
|
}
|
2019-08-19 18:03:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-13 19:18:05 +00:00
|
|
|
func dotSplitLast(s string, n int) string {
|
|
|
|
tokens := strings.SplitN(s, ".", n)
|
|
|
|
if len(tokens) != n {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return tokens[n-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
func TrustDomainForTarget(target structs.DiscoveryTarget) string {
|
|
|
|
if target.External {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
switch target.Partition {
|
|
|
|
case "default":
|
|
|
|
if target.ServiceSubset == "" {
|
|
|
|
// service, namespace, datacenter, internal, trustDomain
|
|
|
|
return dotSplitLast(target.SNI, 5)
|
|
|
|
} else {
|
|
|
|
// subset, service, namespace, datacenter, internal, trustDomain
|
|
|
|
return dotSplitLast(target.SNI, 6)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
if target.ServiceSubset == "" {
|
|
|
|
// service, namespace, partition, datacenter, internalVersion, trustDomain
|
|
|
|
return dotSplitLast(target.SNI, 6)
|
|
|
|
} else {
|
|
|
|
// subset, service, namespace, partition, datacenter, internalVersion, trustDomain
|
|
|
|
return dotSplitLast(target.SNI, 7)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-25 17:37:44 +00:00
|
|
|
func PeeredServiceSNI(service, namespace, partition, peerName, trustDomain string) string {
|
|
|
|
if peerName == "" {
|
|
|
|
panic("peer name is a requirement for this function and does not make sense without it")
|
|
|
|
}
|
|
|
|
if namespace == "" {
|
|
|
|
namespace = structs.IntentionDefaultNamespace
|
|
|
|
}
|
|
|
|
if partition == "" {
|
2023-08-22 14:46:03 +00:00
|
|
|
// TODO(partitions) Make default available in CE as a constant for uses like this one
|
2022-05-25 17:37:44 +00:00
|
|
|
partition = "default"
|
|
|
|
}
|
|
|
|
|
|
|
|
return dotJoin(service, namespace, partition, peerName, external, trustDomain)
|
|
|
|
}
|
|
|
|
|
2021-09-01 14:35:39 +00:00
|
|
|
func dotJoin(parts ...string) string {
|
|
|
|
return strings.Join(parts, ".")
|
|
|
|
}
|
|
|
|
|
2019-08-19 18:03:03 +00:00
|
|
|
func QuerySNI(service string, datacenter string, trustDomain string) string {
|
|
|
|
return fmt.Sprintf("%s.default.%s.query.%s", service, datacenter, trustDomain)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TargetSNI(target *structs.DiscoveryTarget, trustDomain string) string {
|
2021-09-01 14:35:39 +00:00
|
|
|
return ServiceSNI(target.Service, target.ServiceSubset, target.Namespace, target.Partition, target.Datacenter, trustDomain)
|
2019-08-19 18:03:03 +00:00
|
|
|
}
|