359 lines
10 KiB
Groff
359 lines
10 KiB
Groff
.\" Generated by Mmark Markdown Processer - mmark.miek.nl
|
|
.TH "COREDNS-TEMPLATE" 7 "March 2021" "CoreDNS" "CoreDNS Plugins"
|
|
|
|
.SH "NAME"
|
|
.PP
|
|
\fItemplate\fP - allows for dynamic responses based on the incoming query.
|
|
|
|
.SH "DESCRIPTION"
|
|
.PP
|
|
The \fItemplate\fP plugin allows you to dynamically respond to queries by just writing a (Go) template.
|
|
|
|
.SH "SYNTAX"
|
|
.PP
|
|
.RS
|
|
|
|
.nf
|
|
template CLASS TYPE [ZONE...] {
|
|
match REGEX...
|
|
answer RR
|
|
additional RR
|
|
authority RR
|
|
rcode CODE
|
|
fallthrough [ZONE...]
|
|
}
|
|
|
|
.fi
|
|
.RE
|
|
|
|
.IP \(bu 4
|
|
\fBCLASS\fP the query class (usually IN or ANY).
|
|
.IP \(bu 4
|
|
\fBTYPE\fP the query type (A, PTR, ... can be ANY to match all types).
|
|
.IP \(bu 4
|
|
\fBZONE\fP the zone scope(s) for this template. Defaults to the server zones.
|
|
.IP \(bu 4
|
|
\fBREGEX\fP Go regexp
|
|
\[la]https://golang.org/pkg/regexp/\[ra] that are matched against the incoming question name. Specifying no regex matches everything (default: \fB\fC.*\fR). First matching regex wins.
|
|
.IP \(bu 4
|
|
\fB\fCanswer|additional|authority\fR \fBRR\fP A RFC 1035
|
|
\[la]https://tools.ietf.org/html/rfc1035#section-5\[ra] style resource record fragment
|
|
built by a Go template
|
|
\[la]https://golang.org/pkg/text/template/\[ra] that contains the reply.
|
|
.IP \(bu 4
|
|
\fB\fCrcode\fR \fBCODE\fP A response code (\fB\fCNXDOMAIN, SERVFAIL, ...\fR). The default is \fB\fCSUCCESS\fR.
|
|
.IP \(bu 4
|
|
\fB\fCfallthrough\fR Continue with the next plugin if the zone matched but no regex matched.
|
|
If specific zones are listed (for example \fB\fCin-addr.arpa\fR and \fB\fCip6.arpa\fR), then only queries for
|
|
those zones will be subject to fallthrough.
|
|
|
|
|
|
.PP
|
|
At least one \fB\fCanswer\fR or \fB\fCrcode\fR directive is needed (e.g. \fB\fCrcode NXDOMAIN\fR).
|
|
|
|
.PP
|
|
Also see
|
|
\[la]#also-see\[ra] contains an additional reading list.
|
|
|
|
.SH "TEMPLATES"
|
|
.PP
|
|
Each resource record is a full-featured Go template
|
|
\[la]https://golang.org/pkg/text/template/\[ra] with the following predefined data
|
|
|
|
.IP \(bu 4
|
|
\fB\fC.Zone\fR the matched zone string (e.g. \fB\fCexample.\fR).
|
|
.IP \(bu 4
|
|
\fB\fC.Name\fR the query name, as a string (lowercased).
|
|
.IP \(bu 4
|
|
\fB\fC.Class\fR the query class (usually \fB\fCIN\fR).
|
|
.IP \(bu 4
|
|
\fB\fC.Type\fR the RR type requested (e.g. \fB\fCPTR\fR).
|
|
.IP \(bu 4
|
|
\fB\fC.Match\fR an array of all matches. \fB\fCindex .Match 0\fR refers to the whole match.
|
|
.IP \(bu 4
|
|
\fB\fC.Group\fR a map of the named capture groups.
|
|
.IP \(bu 4
|
|
\fB\fC.Message\fR the complete incoming DNS message.
|
|
.IP \(bu 4
|
|
\fB\fC.Question\fR the matched question section.
|
|
.IP \(bu 4
|
|
\fB\fC.Remote\fR client’s IP address
|
|
.IP \(bu 4
|
|
\fB\fC.Meta\fR a function that takes a metadata name and returns the value, if the
|
|
metadata plugin is enabled. For example, \fB\fC.Meta "kubernetes/client-namespace"\fR
|
|
|
|
|
|
.PP
|
|
The output of the template must be a RFC 1035
|
|
\[la]https://tools.ietf.org/html/rfc1035\[ra] style resource record (commonly referred to as a "zone file").
|
|
|
|
.PP
|
|
\fBWARNING\fP there is a syntactical problem with Go templates and CoreDNS config files. Expressions
|
|
like \fB\fC{{$var}}\fR will be interpreted as a reference to an environment variable by CoreDNS (and
|
|
Caddy) while \fB\fC{{ $var }}\fR will work. See Bugs
|
|
\[la]#bugs\[ra] and corefile(5).
|
|
|
|
.SH "METRICS"
|
|
.PP
|
|
If monitoring is enabled (via the \fIprometheus\fP plugin) then the following metrics are exported:
|
|
|
|
.IP \(bu 4
|
|
\fB\fCcoredns_template_matches_total{server, regex}\fR the total number of matched requests by regex.
|
|
.IP \(bu 4
|
|
\fB\fCcoredns_template_template_failures_total{server, regex,section,template}\fR the number of times the Go templating failed. Regex, section and template label values can be used to map the error back to the config file.
|
|
.IP \(bu 4
|
|
\fB\fCcoredns_template_rr_failures_total{server, regex,section,template}\fR the number of times the templated resource record was invalid and could not be parsed. Regex, section and template label values can be used to map the error back to the config file.
|
|
|
|
|
|
.PP
|
|
Both failure cases indicate a problem with the template configuration. The \fB\fCserver\fR label indicates
|
|
the server incrementing the metric, see the \fImetrics\fP plugin for details.
|
|
|
|
.SH "EXAMPLES"
|
|
.SS "RESOLVE EVERYTHING TO NXDOMAIN"
|
|
.PP
|
|
The most simplistic template is
|
|
|
|
.PP
|
|
.RS
|
|
|
|
.nf
|
|
\&. {
|
|
template ANY ANY {
|
|
rcode NXDOMAIN
|
|
}
|
|
}
|
|
|
|
.fi
|
|
.RE
|
|
|
|
.IP 1\. 4
|
|
This template uses the default zone (\fB\fC.\fR or all queries)
|
|
.IP 2\. 4
|
|
All queries will be answered (no \fB\fCfallthrough\fR)
|
|
.IP 3\. 4
|
|
The answer is always NXDOMAIN
|
|
|
|
|
|
.SS "RESOLVE .INVALID AS NXDOMAIN"
|
|
.PP
|
|
The \fB\fC.invalid\fR domain is a reserved TLD (see RFC 2606 Reserved Top Level DNS Names
|
|
\[la]https://tools.ietf.org/html/rfc2606#section-2\[ra]) to indicate invalid domains.
|
|
|
|
.PP
|
|
.RS
|
|
|
|
.nf
|
|
\&. {
|
|
forward . 8.8.8.8
|
|
|
|
template ANY ANY invalid {
|
|
rcode NXDOMAIN
|
|
authority "invalid. 60 {{ .Class }} SOA ns.invalid. hostmaster.invalid. (1 60 60 60 60)"
|
|
}
|
|
}
|
|
|
|
.fi
|
|
.RE
|
|
|
|
.IP 1\. 4
|
|
A query to .invalid will result in NXDOMAIN (rcode)
|
|
.IP 2\. 4
|
|
A dummy SOA record is sent to hand out a TTL of 60s for caching purposes
|
|
.IP 3\. 4
|
|
Querying \fB\fC.invalid\fR in the \fB\fCCH\fR class will also cause a NXDOMAIN/SOA response
|
|
.IP 4\. 4
|
|
The default regex is \fB\fC.*\fR
|
|
|
|
|
|
.SS "BLOCK INVALID SEARCH DOMAIN COMPLETIONS"
|
|
.PP
|
|
Imagine you run \fB\fCexample.com\fR with a datacenter \fB\fCdc1.example.com\fR. The datacenter domain
|
|
is part of the DNS search domain.
|
|
However \fB\fCsomething.example.com.dc1.example.com\fR would indicate a fully qualified
|
|
domain name (\fB\fCsomething.example.com\fR) that inadvertently has the default domain or search
|
|
path (\fB\fCdc1.example.com\fR) added.
|
|
|
|
.PP
|
|
.RS
|
|
|
|
.nf
|
|
\&. {
|
|
forward . 8.8.8.8
|
|
|
|
template IN ANY example.com.dc1.example.com {
|
|
rcode NXDOMAIN
|
|
authority "{{ .Zone }} 60 IN SOA ns.example.com hostmaster.example.com (1 60 60 60 60)"
|
|
}
|
|
}
|
|
|
|
.fi
|
|
.RE
|
|
|
|
.PP
|
|
A more verbose regex based equivalent would be
|
|
|
|
.PP
|
|
.RS
|
|
|
|
.nf
|
|
\&. {
|
|
forward . 8.8.8.8
|
|
|
|
template IN ANY example.com {
|
|
match "example\\.com\\.(dc1\\.example\\.com\\.)$"
|
|
rcode NXDOMAIN
|
|
authority "{{ index .Match 1 }} 60 IN SOA ns.{{ index .Match 1 }} hostmaster.{{ index .Match 1 }} (1 60 60 60 60)"
|
|
fallthrough
|
|
}
|
|
}
|
|
|
|
.fi
|
|
.RE
|
|
|
|
.PP
|
|
The regex-based version can do more complex matching/templating while zone-based templating is easier to read and use.
|
|
|
|
.SS "RESOLVE A/PTR FOR .EXAMPLE"
|
|
.PP
|
|
.RS
|
|
|
|
.nf
|
|
\&. {
|
|
forward . 8.8.8.8
|
|
|
|
# ip\-a\-b\-c\-d.example A a.b.c.d
|
|
|
|
template IN A example {
|
|
match (^|[.])ip\-(?P<a>[0\-9]*)\-(?P<b>[0\-9]*)\-(?P<c>[0\-9]*)\-(?P<d>[0\-9]*)[.]example[.]$
|
|
answer "{{ .Name }} 60 IN A {{ .Group.a }}.{{ .Group.b }}.{{ .Group.c }}.{{ .Group.d }}"
|
|
fallthrough
|
|
}
|
|
|
|
# d.c.b.a.in\-addr.arpa PTR ip\-a\-b\-c\-d.example
|
|
|
|
template IN PTR in\-addr.arpa {
|
|
match ^(?P<d>[0\-9]*)[.](?P<c>[0\-9]*)[.](?P<b>[0\-9]*)[.](?P<a>[0\-9]*)[.]in\-addr[.]arpa[.]$
|
|
answer "{{ .Name }} 60 IN PTR ip\-{{ .Group.a }}\-{{ .Group.b }}\-{{ .Group.c }}\-{{ .Group.d }}.example."
|
|
}
|
|
}
|
|
|
|
.fi
|
|
.RE
|
|
|
|
.PP
|
|
An IPv4 address consists of 4 bytes, \fB\fCa.b.c.d\fR. Named groups make it less error-prone to reverse the
|
|
IP address in the PTR case. Try to use named groups to explain what your regex and template are doing.
|
|
|
|
.PP
|
|
Note that the A record is actually a wildcard: any subdomain of the IP address will resolve to the IP address.
|
|
|
|
.PP
|
|
Having templates to map certain PTR/A pairs is a common pattern.
|
|
|
|
.PP
|
|
Fallthrough is needed for mixed domains where only some responses are templated.
|
|
|
|
.SS "RESOLVE MULTIPLE IP PATTERNS"
|
|
.PP
|
|
.RS
|
|
|
|
.nf
|
|
\&. {
|
|
forward . 8.8.8.8
|
|
|
|
template IN A example {
|
|
match "^ip\-(?P<a>10)\-(?P<b>[0\-9]*)\-(?P<c>[0\-9]*)\-(?P<d>[0\-9]*)[.]dc[.]example[.]$"
|
|
match "^(?P<a>[0\-9]*)[.](?P<b>[0\-9]*)[.](?P<c>[0\-9]*)[.](?P<d>[0\-9]*)[.]ext[.]example[.]$"
|
|
answer "{{ .Name }} 60 IN A {{ .Group.a}}.{{ .Group.b }}.{{ .Group.c }}.{{ .Group.d }}"
|
|
fallthrough
|
|
}
|
|
}
|
|
|
|
.fi
|
|
.RE
|
|
|
|
.PP
|
|
Named capture groups can be used to template one response for multiple patterns.
|
|
|
|
.SS "RESOLVE A AND MX RECORDS FOR IP TEMPLATES IN .EXAMPLE"
|
|
.PP
|
|
.RS
|
|
|
|
.nf
|
|
\&. {
|
|
forward . 8.8.8.8
|
|
|
|
template IN A example {
|
|
match ^ip\-10\-(?P<b>[0\-9]*)\-(?P<c>[0\-9]*)\-(?P<d>[0\-9]*)[.]example[.]$
|
|
answer "{{ .Name }} 60 IN A 10.{{ .Group.b }}.{{ .Group.c }}.{{ .Group.d }}"
|
|
fallthrough
|
|
}
|
|
template IN MX example {
|
|
match ^ip\-10\-(?P<b>[0\-9]*)\-(?P<c>[0\-9]*)\-(?P<d>[0\-9]*)[.]example[.]$
|
|
answer "{{ .Name }} 60 IN MX 10 {{ .Name }}"
|
|
additional "{{ .Name }} 60 IN A 10.{{ .Group.b }}.{{ .Group.c }}.{{ .Group.d }}"
|
|
fallthrough
|
|
}
|
|
}
|
|
|
|
.fi
|
|
.RE
|
|
|
|
.SS "ADDING AUTHORITATIVE NAMESERVERS TO THE RESPONSE"
|
|
.PP
|
|
.RS
|
|
|
|
.nf
|
|
\&. {
|
|
forward . 8.8.8.8
|
|
|
|
template IN A example {
|
|
match ^ip\-10\-(?P<b>[0\-9]*)\-(?P<c>[0\-9]*)\-(?P<d>[0\-9]*)[.]example[.]$
|
|
answer "{{ .Name }} 60 IN A 10.{{ .Group.b }}.{{ .Group.c }}.{{ .Group.d }}"
|
|
authority "example. 60 IN NS ns0.example."
|
|
authority "example. 60 IN NS ns1.example."
|
|
additional "ns0.example. 60 IN A 203.0.113.8"
|
|
additional "ns1.example. 60 IN A 198.51.100.8"
|
|
fallthrough
|
|
}
|
|
template IN MX example {
|
|
match ^ip\-10\-(?P<b>[0\-9]*)\-(?P<c>[0\-9]*)\-(?P<d>[0\-9]*)[.]example[.]$
|
|
answer "{{ .Name }} 60 IN MX 10 {{ .Name }}"
|
|
additional "{{ .Name }} 60 IN A 10.{{ .Group.b }}.{{ .Group.c }}.{{ .Group.d }}"
|
|
authority "example. 60 IN NS ns0.example."
|
|
authority "example. 60 IN NS ns1.example."
|
|
additional "ns0.example. 60 IN A 203.0.113.8"
|
|
additional "ns1.example. 60 IN A 198.51.100.8"
|
|
fallthrough
|
|
}
|
|
}
|
|
|
|
.fi
|
|
.RE
|
|
|
|
.SH "ALSO SEE"
|
|
.IP \(bu 4
|
|
Go regexp
|
|
\[la]https://golang.org/pkg/regexp/\[ra] for details about the regex implementation
|
|
.IP \(bu 4
|
|
RE2 syntax reference
|
|
\[la]https://github.com/google/re2/wiki/Syntax\[ra] for details about the regex syntax
|
|
.IP \(bu 4
|
|
RFC 1034
|
|
\[la]https://tools.ietf.org/html/rfc1034#section-3.6.1\[ra] and RFC 1035
|
|
\[la]https://tools.ietf.org/html/rfc1035#section-5\[ra] for the resource record format
|
|
.IP \(bu 4
|
|
Go template
|
|
\[la]https://golang.org/pkg/text/template/\[ra] for the template language reference
|
|
|
|
|
|
.SH "BUGS"
|
|
.PP
|
|
CoreDNS supports caddyfile environment variables
|
|
\[la]https://caddyserver.com/docs/caddyfile#env\[ra]
|
|
with notion of \fB\fC{$ENV_VAR}\fR. This parser feature will break Go template variables
|
|
\[la]https://golang.org/pkg/text/template/#hdr-Variables\[ra] notations like\fB\fC{{$variable}}\fR.
|
|
The equivalent notation \fB\fC{{ $variable }}\fR will work.
|
|
Try to avoid Go template variables in the context of this plugin.
|
|
|