2024-01-17 23:46:18 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
|
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/discovery"
|
|
|
|
)
|
|
|
|
|
|
|
|
// buildQueryFromDNSMessage returns a discovery.Query from a DNS message.
|
2024-02-02 23:29:38 +00:00
|
|
|
func buildQueryFromDNSMessage(req *dns.Msg, reqCtx Context, domain, altDomain string) (*discovery.Query, error) {
|
2024-01-17 23:46:18 +00:00
|
|
|
queryType, queryParts, querySuffixes := getQueryTypePartsAndSuffixesFromDNSMessage(req, domain, altDomain)
|
|
|
|
|
2024-02-02 23:29:38 +00:00
|
|
|
queryTenancy, err := getQueryTenancy(reqCtx, queryType, querySuffixes)
|
2024-01-29 22:33:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2024-01-17 23:46:18 +00:00
|
|
|
}
|
|
|
|
|
2024-01-29 22:33:45 +00:00
|
|
|
name, tag := getQueryNameAndTagFromParts(queryType, queryParts)
|
2024-01-17 23:46:18 +00:00
|
|
|
|
2024-02-02 23:29:38 +00:00
|
|
|
portName := parsePort(queryParts)
|
|
|
|
|
|
|
|
if queryType == discovery.QueryTypeWorkload && req.Question[0].Qtype == dns.TypeSRV {
|
|
|
|
// Currently we do not support SRV records for workloads
|
|
|
|
return nil, errNotImplemented
|
|
|
|
}
|
|
|
|
|
2024-01-17 23:46:18 +00:00
|
|
|
return &discovery.Query{
|
|
|
|
QueryType: queryType,
|
|
|
|
QueryPayload: discovery.QueryPayload{
|
2024-02-02 23:29:38 +00:00
|
|
|
Name: name,
|
|
|
|
Tenancy: queryTenancy,
|
|
|
|
Tag: tag,
|
|
|
|
PortName: portName,
|
|
|
|
//RemoteAddr: nil, // TODO (v2-dns): Prepared Queries for V1 Catalog
|
2024-01-17 23:46:18 +00:00
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-01-29 22:33:45 +00:00
|
|
|
// getQueryNameAndTagFromParts returns the query name and tag from the query parts that are taken from the original dns question.
|
|
|
|
func getQueryNameAndTagFromParts(queryType discovery.QueryType, queryParts []string) (string, string) {
|
|
|
|
switch queryType {
|
|
|
|
case discovery.QueryTypeService:
|
|
|
|
n := len(queryParts)
|
|
|
|
// Support RFC 2782 style syntax
|
|
|
|
if n == 2 && strings.HasPrefix(queryParts[1], "_") && strings.HasPrefix(queryParts[0], "_") {
|
|
|
|
// Grab the tag since we make nuke it if it's tcp
|
|
|
|
tag := queryParts[1][1:]
|
|
|
|
|
|
|
|
// Treat _name._tcp.service.consul as a default, no need to filter on that tag
|
|
|
|
if tag == "tcp" {
|
|
|
|
tag = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
name := queryParts[0][1:]
|
|
|
|
// _name._tag.service.consul
|
|
|
|
return name, tag
|
|
|
|
}
|
|
|
|
return queryParts[len(queryParts)-1], ""
|
|
|
|
}
|
|
|
|
return queryParts[len(queryParts)-1], ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// getQueryTenancy returns a discovery.QueryTenancy from a DNS message.
|
2024-02-02 23:29:38 +00:00
|
|
|
func getQueryTenancy(reqCtx Context, queryType discovery.QueryType, querySuffixes []string) (discovery.QueryTenancy, error) {
|
|
|
|
labels, ok := parseLabels(querySuffixes)
|
|
|
|
if !ok {
|
|
|
|
return discovery.QueryTenancy{}, errNameNotFound
|
2024-01-29 22:33:45 +00:00
|
|
|
}
|
|
|
|
|
2024-02-02 23:29:38 +00:00
|
|
|
// If we don't have an explicit partition in the request, try the first fallback
|
|
|
|
// which was supplied in the request context. The agent's partition will be used as the last fallback
|
|
|
|
// later in the query processor.
|
|
|
|
if labels.Partition == "" {
|
|
|
|
labels.Partition = reqCtx.DefaultPartition
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have a sameness group, we can return early without further data massage.
|
|
|
|
if labels.SamenessGroup != "" {
|
|
|
|
return discovery.QueryTenancy{
|
|
|
|
Namespace: labels.Namespace,
|
|
|
|
Partition: labels.Partition,
|
|
|
|
SamenessGroup: labels.SamenessGroup,
|
|
|
|
}, nil
|
2024-01-29 22:33:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if queryType == discovery.QueryTypeVirtual {
|
2024-02-02 23:29:38 +00:00
|
|
|
if labels.Peer == "" {
|
2024-01-29 22:33:45 +00:00
|
|
|
// If the peer name was not explicitly defined, fall back to the ambiguously-parsed version.
|
2024-02-02 23:29:38 +00:00
|
|
|
labels.Peer = labels.PeerOrDatacenter
|
2024-01-29 22:33:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-02 23:29:38 +00:00
|
|
|
return discovery.QueryTenancy{
|
|
|
|
Namespace: labels.Namespace,
|
|
|
|
Partition: labels.Partition,
|
|
|
|
Peer: labels.Peer,
|
|
|
|
Datacenter: labels.Datacenter,
|
|
|
|
}, nil
|
2024-01-29 22:33:45 +00:00
|
|
|
}
|
|
|
|
|
2024-01-17 23:46:18 +00:00
|
|
|
// getQueryTypePartsAndSuffixesFromDNSMessage returns the query type, the parts, and suffixes of the query name.
|
|
|
|
func getQueryTypePartsAndSuffixesFromDNSMessage(req *dns.Msg, domain, altDomain string) (queryType discovery.QueryType, parts []string, suffixes []string) {
|
|
|
|
// Get the QName without the domain suffix
|
2024-02-02 23:29:38 +00:00
|
|
|
// TODO (v2-dns): we will also need to handle the "failover" and "no-failover" suffixes here.
|
|
|
|
// They come AFTER the domain. See `stripSuffix` in router.go
|
2024-01-17 23:46:18 +00:00
|
|
|
qName := trimDomainFromQuestionName(req.Question[0].Name, domain, altDomain)
|
|
|
|
|
|
|
|
// Split into the label parts
|
|
|
|
labels := dns.SplitDomainName(qName)
|
|
|
|
|
|
|
|
done := false
|
|
|
|
for i := len(labels) - 1; i >= 0 && !done; i-- {
|
|
|
|
queryType = getQueryTypeFromLabels(labels[i])
|
|
|
|
switch queryType {
|
2024-02-02 23:29:38 +00:00
|
|
|
case discovery.QueryTypeService, discovery.QueryTypeWorkload,
|
2024-01-17 23:46:18 +00:00
|
|
|
discovery.QueryTypeConnect, discovery.QueryTypeVirtual, discovery.QueryTypeIngress,
|
|
|
|
discovery.QueryTypeNode, discovery.QueryTypePreparedQuery:
|
|
|
|
parts = labels[:i]
|
|
|
|
suffixes = labels[i+1:]
|
|
|
|
done = true
|
2024-01-29 22:33:45 +00:00
|
|
|
case discovery.QueryTypeInvalid:
|
|
|
|
fallthrough
|
2024-01-17 23:46:18 +00:00
|
|
|
default:
|
|
|
|
// If this is a SRV query the "service" label is optional, we add it back to use the
|
|
|
|
// existing code-path.
|
|
|
|
if req.Question[0].Qtype == dns.TypeSRV && strings.HasPrefix(labels[i], "_") {
|
2024-01-29 22:33:45 +00:00
|
|
|
queryType = discovery.QueryTypeService
|
2024-01-17 23:46:18 +00:00
|
|
|
parts = labels[:i+1]
|
|
|
|
suffixes = labels[i+1:]
|
|
|
|
done = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return queryType, parts, suffixes
|
|
|
|
}
|
|
|
|
|
|
|
|
// trimDomainFromQuestionName returns the question name without the domain suffix.
|
|
|
|
func trimDomainFromQuestionName(questionName, domain, altDomain string) string {
|
2024-02-02 23:29:38 +00:00
|
|
|
qName := dns.CanonicalName(questionName)
|
2024-01-17 23:46:18 +00:00
|
|
|
longer := domain
|
|
|
|
shorter := altDomain
|
|
|
|
|
|
|
|
if len(shorter) > len(longer) {
|
|
|
|
longer, shorter = shorter, longer
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasSuffix(qName, "."+strings.TrimLeft(longer, ".")) {
|
|
|
|
return strings.TrimSuffix(qName, longer)
|
|
|
|
}
|
|
|
|
return strings.TrimSuffix(qName, shorter)
|
|
|
|
}
|
|
|
|
|
|
|
|
// getQueryTypeFromLabels returns the query type from the labels.
|
|
|
|
func getQueryTypeFromLabels(label string) discovery.QueryType {
|
|
|
|
switch label {
|
|
|
|
case "service":
|
|
|
|
return discovery.QueryTypeService
|
|
|
|
case "connect":
|
|
|
|
return discovery.QueryTypeConnect
|
|
|
|
case "virtual":
|
|
|
|
return discovery.QueryTypeVirtual
|
|
|
|
case "ingress":
|
|
|
|
return discovery.QueryTypeIngress
|
|
|
|
case "node":
|
|
|
|
return discovery.QueryTypeNode
|
|
|
|
case "query":
|
|
|
|
return discovery.QueryTypePreparedQuery
|
|
|
|
case "workload":
|
|
|
|
return discovery.QueryTypeWorkload
|
|
|
|
default:
|
|
|
|
return discovery.QueryTypeInvalid
|
|
|
|
}
|
|
|
|
}
|