2019-01-13 16:54:49 +00:00
|
|
|
// Package upstream abstracts a upstream lookups so that plugins can handle them in an unified way.
|
2018-02-14 15:11:26 -05:00
|
|
|
package upstream
|
|
|
|
|
|
|
|
|
|
import (
|
2019-03-26 14:37:30 +00:00
|
|
|
"context"
|
2026-10-25 04:45:58 +02:00
|
|
|
"errors"
|
2019-01-13 16:54:49 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
2018-02-14 15:11:26 -05:00
|
|
|
"github.com/coredns/coredns/core/dnsserver"
|
|
|
|
|
"github.com/coredns/coredns/plugin/pkg/nonwriter"
|
|
|
|
|
"github.com/coredns/coredns/request"
|
2020-01-30 09:19:26 +00:00
|
|
|
|
|
|
|
|
"github.com/miekg/dns"
|
2018-02-14 15:11:26 -05:00
|
|
|
)
|
|
|
|
|
|
2019-01-13 16:54:49 +00:00
|
|
|
// Upstream is used to resolve CNAME or other external targets via CoreDNS itself.
|
|
|
|
|
type Upstream struct{}
|
2018-02-14 15:11:26 -05:00
|
|
|
|
2019-02-21 09:20:48 +07:00
|
|
|
// New creates a new Upstream to resolve names using the coredns process.
|
2019-01-13 16:54:49 +00:00
|
|
|
func New() *Upstream { return &Upstream{} }
|
2018-02-14 15:11:26 -05:00
|
|
|
|
2026-10-25 04:45:58 +02:00
|
|
|
// maxDepth is the number of nested self-lookups one client query may cause.
|
|
|
|
|
// Each Lookup re-enters the server's plugin chain, and a plugin that answers
|
|
|
|
|
// with another CNAME re-enters it again. Backends serve data that other
|
|
|
|
|
// parties write (a Kubernetes tenant's Service and EndpointSlice, an etcd
|
|
|
|
|
// record), so a target that leads back to itself must stop here instead of
|
|
|
|
|
// growing the Go stack until the process dies. This matches the generic
|
|
|
|
|
// backend's CNAME-chain limit.
|
|
|
|
|
const maxDepth = 10
|
|
|
|
|
|
|
|
|
|
// errMaxDepth is returned when a lookup would nest deeper than maxDepth.
|
|
|
|
|
var errMaxDepth = errors.New("upstream lookup depth exceeded")
|
|
|
|
|
|
|
|
|
|
// depthKey is deliberately separate from dnsserver.LoopKey. The file and
|
|
|
|
|
// rewrite plugins already maintain LoopKey around their own CNAME processing;
|
|
|
|
|
// sharing it here would count each re-entry twice and shorten valid chains.
|
|
|
|
|
type depthKey struct{}
|
|
|
|
|
|
2021-08-27 05:23:05 -07:00
|
|
|
// Lookup routes lookups to our selves to make it follow the plugin chain *again*, but with a (possibly) new query. As
|
|
|
|
|
// we are doing the query against ourselves again, there is no actual new hop, as such RFC 6891 does not apply and we
|
|
|
|
|
// need the EDNS0 option present in the *original* query to be present here too.
|
2026-10-25 04:45:58 +02:00
|
|
|
//
|
|
|
|
|
// The nesting depth travels with the context so chains that alternate between
|
|
|
|
|
// plugins are bounded as one. Lookup returns errMaxDepth once maxDepth nested
|
|
|
|
|
// lookups are in progress for the client query.
|
2019-03-26 14:37:30 +00:00
|
|
|
func (u *Upstream) Lookup(ctx context.Context, state request.Request, name string, typ uint16) (*dns.Msg, error) {
|
|
|
|
|
server, ok := ctx.Value(dnsserver.Key{}).(*dnsserver.Server)
|
2019-01-13 16:54:49 +00:00
|
|
|
if !ok {
|
|
|
|
|
return nil, fmt.Errorf("no full server is running")
|
|
|
|
|
}
|
2026-10-25 04:45:58 +02:00
|
|
|
depth, _ := ctx.Value(depthKey{}).(int)
|
|
|
|
|
if depth >= maxDepth {
|
|
|
|
|
return nil, errMaxDepth
|
|
|
|
|
}
|
|
|
|
|
ctx = context.WithValue(ctx, depthKey{}, depth+1)
|
|
|
|
|
|
2021-08-26 09:39:44 -07:00
|
|
|
req := state.NewWithQuestion(name, typ)
|
2018-06-12 14:54:37 +01:00
|
|
|
|
2019-01-13 16:54:49 +00:00
|
|
|
nw := nonwriter.New(state.W)
|
2021-08-26 09:39:44 -07:00
|
|
|
server.ServeDNS(ctx, nw, req.Req)
|
2018-06-12 14:54:37 +01:00
|
|
|
|
2019-01-13 16:54:49 +00:00
|
|
|
return nw.Msg, nil
|
2018-02-14 15:11:26 -05:00
|
|
|
}
|