2024-01-17 23:46:18 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
|
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
2024-02-03 03:23:52 +00:00
|
|
|
"net"
|
2024-01-17 23:46:18 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/discovery"
|
|
|
|
)
|
|
|
|
|
|
|
|
// buildQueryFromDNSMessage returns a discovery.Query from a DNS message.
|
2024-02-03 03:23:52 +00:00
|
|
|
func buildQueryFromDNSMessage(req *dns.Msg, reqCtx Context, domain, altDomain string,
|
|
|
|
remoteAddress net.Addr) (*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)
|
|
|
|
|
2024-02-08 04:24:00 +00:00
|
|
|
switch {
|
|
|
|
case queryType == discovery.QueryTypeWorkload && req.Question[0].Qtype == dns.TypeSRV:
|
2024-02-02 23:29:38 +00:00
|
|
|
// Currently we do not support SRV records for workloads
|
|
|
|
return nil, errNotImplemented
|
2024-02-08 04:24:00 +00:00
|
|
|
case queryType == discovery.QueryTypeInvalid, name == "":
|
|
|
|
return nil, errInvalidQuestion
|
2024-02-02 23:29:38 +00:00
|
|
|
}
|
|
|
|
|
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,
|
2024-02-03 03:23:52 +00:00
|
|
|
SourceIP: getSourceIP(req, queryType, remoteAddress),
|
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) {
|
2024-02-06 14:53:39 +00:00
|
|
|
n := len(queryParts)
|
|
|
|
if n == 0 {
|
|
|
|
return "", ""
|
|
|
|
}
|
|
|
|
|
2024-01-29 22:33:45 +00:00
|
|
|
switch queryType {
|
|
|
|
case discovery.QueryTypeService:
|
|
|
|
// 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
|
|
|
|
}
|
2024-02-06 14:53:39 +00:00
|
|
|
return queryParts[n-1], ""
|
2024-02-12 19:27:25 +00:00
|
|
|
case discovery.QueryTypePreparedQuery:
|
|
|
|
name := ""
|
|
|
|
|
|
|
|
// If the first and last DNS query parts begin with _, this is an RFC 2782 style SRV lookup.
|
|
|
|
// This allows for prepared query names to include "." (for backwards compatibility).
|
|
|
|
// Otherwise, this is a standard prepared query lookup.
|
|
|
|
if n >= 2 && strings.HasPrefix(queryParts[0], "_") && strings.HasPrefix(queryParts[n-1], "_") {
|
|
|
|
// The last DNS query part is the protocol field (ignored).
|
|
|
|
// All prior parts are the prepared query name or ID.
|
|
|
|
name = strings.Join(queryParts[:n-1], ".")
|
|
|
|
|
|
|
|
// Strip leading underscore
|
|
|
|
name = name[1:]
|
|
|
|
} else {
|
|
|
|
// Allow a "." in the query name, just join all the parts.
|
|
|
|
name = strings.Join(queryParts, ".")
|
|
|
|
}
|
|
|
|
return name, ""
|
2024-01-29 22:33:45 +00:00
|
|
|
}
|
2024-02-06 14:53:39 +00:00
|
|
|
return queryParts[n-1], ""
|
2024-01-29 22:33:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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,
|
2024-02-09 02:56:04 +00:00
|
|
|
Datacenter: reqCtx.DefaultDatacenter,
|
2024-02-02 23:29:38 +00:00
|
|
|
}, 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,
|
2024-02-09 02:56:04 +00:00
|
|
|
Datacenter: getEffectiveDatacenter(labels, reqCtx.DefaultDatacenter),
|
2024-02-02 23:29:38 +00:00
|
|
|
}, nil
|
2024-01-29 22:33:45 +00:00
|
|
|
}
|
|
|
|
|
2024-02-09 02:56:04 +00:00
|
|
|
// getEffectiveDatacenter returns the effective datacenter from the parsed labels.
|
|
|
|
func getEffectiveDatacenter(labels *parsedLabels, defaultDC string) string {
|
|
|
|
switch {
|
|
|
|
case labels.Datacenter != "":
|
|
|
|
return labels.Datacenter
|
|
|
|
case labels.PeerOrDatacenter != "" && labels.Peer != labels.PeerOrDatacenter:
|
|
|
|
return labels.PeerOrDatacenter
|
|
|
|
}
|
|
|
|
return defaultDC
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2024-02-03 03:23:52 +00:00
|
|
|
|
|
|
|
// getSourceIP returns the source IP from the dns request.
|
|
|
|
func getSourceIP(req *dns.Msg, queryType discovery.QueryType, remoteAddr net.Addr) (sourceIP net.IP) {
|
|
|
|
if queryType == discovery.QueryTypePreparedQuery {
|
|
|
|
subnet := ednsSubnetForRequest(req)
|
|
|
|
|
|
|
|
if subnet != nil {
|
|
|
|
sourceIP = subnet.Address
|
|
|
|
} else {
|
|
|
|
switch v := remoteAddr.(type) {
|
|
|
|
case *net.UDPAddr:
|
|
|
|
sourceIP = v.IP
|
|
|
|
case *net.TCPAddr:
|
|
|
|
sourceIP = v.IP
|
|
|
|
case *net.IPAddr:
|
|
|
|
sourceIP = v.IP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sourceIP
|
|
|
|
}
|