2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2021-11-16 18:04:01 +00:00
|
|
|
//go:build !consulent
|
2019-12-10 02:26:41 +00:00
|
|
|
// +build !consulent
|
|
|
|
|
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
2019-12-10 02:26:41 +00:00
|
|
|
"github.com/hashicorp/consul/agent/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
type enterpriseDNSConfig struct{}
|
|
|
|
|
|
|
|
func getEnterpriseDNSConfig(conf *config.RuntimeConfig) enterpriseDNSConfig {
|
|
|
|
return enterpriseDNSConfig{}
|
|
|
|
}
|
|
|
|
|
2022-07-06 16:30:04 +00:00
|
|
|
// parseLocality can parse peer name or datacenter from a DNS query's labels.
|
|
|
|
// Peer name is parsed from the same query part that datacenter is, so given this ambiguity
|
|
|
|
// we parse a "peerOrDatacenter". The caller or RPC handler are responsible for disambiguating.
|
|
|
|
func (d *DNSServer) parseLocality(labels []string, cfg *dnsConfig) (queryLocality, bool) {
|
2022-11-29 18:23:18 +00:00
|
|
|
locality := queryLocality{
|
|
|
|
EnterpriseMeta: d.defaultEnterpriseMeta,
|
|
|
|
}
|
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
switch len(labels) {
|
2022-11-29 18:23:18 +00:00
|
|
|
case 2, 4:
|
|
|
|
// Support the following formats:
|
|
|
|
// - [.<datacenter>.dc]
|
|
|
|
// - [.<peer>.peer]
|
|
|
|
for i := 0; i < len(labels); i += 2 {
|
|
|
|
switch labels[i+1] {
|
|
|
|
case "dc":
|
|
|
|
locality.datacenter = labels[i]
|
|
|
|
case "peer":
|
|
|
|
locality.peer = labels[i]
|
|
|
|
default:
|
|
|
|
return queryLocality{}, false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Return error when both datacenter and peer are specified.
|
|
|
|
if locality.datacenter != "" && locality.peer != "" {
|
|
|
|
return queryLocality{}, false
|
|
|
|
}
|
|
|
|
return locality, true
|
2019-12-10 02:26:41 +00:00
|
|
|
case 1:
|
2022-07-06 16:30:04 +00:00
|
|
|
return queryLocality{peerOrDatacenter: labels[0]}, true
|
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
case 0:
|
2022-07-06 16:30:04 +00:00
|
|
|
return queryLocality{}, true
|
2019-12-10 02:26:41 +00:00
|
|
|
}
|
2022-07-06 16:30:04 +00:00
|
|
|
|
|
|
|
return queryLocality{}, false
|
2019-12-10 02:26:41 +00:00
|
|
|
}
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
func serviceCanonicalDNSName(name, kind, datacenter, domain string, _ *acl.EnterpriseMeta) string {
|
2020-06-22 19:14:12 +00:00
|
|
|
return fmt.Sprintf("%s.%s.%s.%s", name, kind, datacenter, domain)
|
2019-12-10 02:26:41 +00:00
|
|
|
}
|
2022-11-29 18:23:18 +00:00
|
|
|
|
|
|
|
func nodeCanonicalDNSName(lookup serviceLookup, nodeName, respDomain string) string {
|
|
|
|
if lookup.PeerName != "" {
|
|
|
|
// We must return a more-specific DNS name for peering so
|
|
|
|
// that there is no ambiguity with lookups.
|
|
|
|
return fmt.Sprintf("%s.node.%s.peer.%s",
|
|
|
|
nodeName,
|
|
|
|
lookup.PeerName,
|
|
|
|
respDomain)
|
|
|
|
}
|
|
|
|
// Return a simpler format for non-peering nodes.
|
|
|
|
return fmt.Sprintf("%s.node.%s.%s", nodeName, lookup.Datacenter, respDomain)
|
|
|
|
}
|