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
|
|
|
|
)
|
|
|
|
|
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)
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func DatacenterSNI(dc string, trustDomain string) string {
|
|
|
|
return fmt.Sprintf("%s.internal.%s", dc, trustDomain)
|
|
|
|
}
|
|
|
|
|
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 == "" {
|
|
|
|
namespace = "default"
|
|
|
|
}
|
2021-09-01 14:35:39 +00:00
|
|
|
if partition == "" {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|