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
|
|
|
|
2021-11-16 18:04:01 +00:00
|
|
|
//go:build !consulent
|
2019-12-10 02:26:41 +00:00
|
|
|
|
|
|
|
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"
|
2024-05-14 13:32:49 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2019-12-10 02:26:41 +00:00
|
|
|
)
|
|
|
|
|
2024-01-17 23:46:18 +00:00
|
|
|
// NOTE: these functions have also been copied to agent/dns package for dns v2.
|
|
|
|
// If you change these functions, please also change the ones in agent/dns as well.
|
|
|
|
// These v1 versions will soon be deprecated.
|
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-04-14 16:24:46 +00:00
|
|
|
type querySameness struct{}
|
|
|
|
|
2023-08-22 14:46:03 +00:00
|
|
|
// parseSamenessGroupLocality wraps parseLocality in CE
|
2024-05-14 13:32:49 +00:00
|
|
|
func (d *DNSServer) parseSamenessGroupLocality(cfg *dnsConfig, labels []string, errfnc func() error) (queryLocality, error) {
|
2023-04-14 16:24:46 +00:00
|
|
|
locality, ok := d.parseLocality(labels, cfg)
|
|
|
|
if !ok {
|
2024-05-14 13:32:49 +00:00
|
|
|
return queryLocality{}, errfnc()
|
2023-04-14 16:24:46 +00:00
|
|
|
}
|
2024-05-14 13:32:49 +00:00
|
|
|
return locality, nil
|
2023-04-14 16:24:46 +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
|
|
|
|
2024-05-14 13:32:49 +00:00
|
|
|
func nodeCanonicalDNSName(node *structs.Node, respDomain string) string {
|
|
|
|
if node.PeerName != "" {
|
2022-11-29 18:23:18 +00:00
|
|
|
// 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",
|
2024-05-14 13:32:49 +00:00
|
|
|
node.Node,
|
|
|
|
node.PeerName,
|
2022-11-29 18:23:18 +00:00
|
|
|
respDomain)
|
|
|
|
}
|
|
|
|
// Return a simpler format for non-peering nodes.
|
2024-05-14 13:32:49 +00:00
|
|
|
return fmt.Sprintf("%s.node.%s.%s", node.Node, node.Datacenter, respDomain)
|
2022-11-29 18:23:18 +00:00
|
|
|
}
|