Align plugin/template usage and syntax with other plugins (#1360)

* Align plugin/template usage and syntax with other plugins

* Use new fallthrough logic in plugin/template

* Use zone name normalization for plugin/template

* Test fallthrough parsing in plugin/template

* Rework scoping of match checks

Most matches are not plugin global but per template. The plugin does only a
very rough check while detailed checks are done per-template.

Per template checks include:
- Zones
- Class/Type
- Regex
- Fallthrough

* Remove trailing `.` from fully qualified domain names

* Register template metrics with zone/class/type instead of regex

* Remove trailing fqdn dot from multiple testcases
This commit is contained in:
Rene Treffer
2018-01-09 22:30:58 +01:00
committed by Miek Gieben
parent a7590897fb
commit 0091e1c9dc
6 changed files with 412 additions and 134 deletions

View File

@@ -19,74 +19,86 @@ func init() {
}
func setupTemplate(c *caddy.Controller) error {
templates, err := templateParse(c)
handler, err := templateParse(c)
if err != nil {
return plugin.Error("template", err)
}
c.OnStartup(OnStartupMetrics)
if err := setupMetrics(c); err != nil {
return plugin.Error("template", err)
}
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
return Handler{Next: next, Templates: templates}
handler.Next = next
return handler
})
return nil
}
func templateParse(c *caddy.Controller) (templates []template, err error) {
templates = make([]template, 0)
func templateParse(c *caddy.Controller) (handler Handler, err error) {
handler.Templates = make([]template, 0)
for c.Next() {
t := template{}
if !c.NextArg() {
return nil, c.ArgErr()
}
if !c.NextArg() {
return handler, c.ArgErr()
}
class, ok := dns.StringToClass[c.Val()]
if !ok {
return nil, c.Errf("invalid query class %s", c.Val())
return handler, c.Errf("invalid query class %s", c.Val())
}
t.class = class
if !c.NextArg() {
return nil, c.ArgErr()
return handler, c.ArgErr()
}
queryType, ok := dns.StringToType[c.Val()]
qtype, ok := dns.StringToType[c.Val()]
if !ok {
return nil, c.Errf("invalid RR type %s", c.Val())
return handler, c.Errf("invalid RR class %s", c.Val())
}
t.qtype = queryType
zones := c.RemainingArgs()
if len(zones) == 0 {
zones = make([]string, len(c.ServerBlockKeys))
copy(zones, c.ServerBlockKeys)
}
for i, str := range zones {
zones[i] = plugin.Host(str).Normalize()
}
handler.Zones = append(handler.Zones, zones...)
t := template{qclass: class, qtype: qtype, zones: zones}
t.regex = make([]*regexp.Regexp, 0)
templatePrefix := ""
for _, regex := range c.RemainingArgs() {
r, err := regexp.Compile(regex)
if err != nil {
return nil, c.Errf("could not parse regex: %s, %v", regex, err)
}
templatePrefix = templatePrefix + regex + " "
t.regex = append(t.regex, r)
}
if len(t.regex) == 0 {
t.regex = append(t.regex, regexp.MustCompile(".*"))
templatePrefix = ".* "
}
t.answer = make([]*gotmpl.Template, 0)
for c.NextBlock() {
switch c.Val() {
case "match":
args := c.RemainingArgs()
if len(args) == 0 {
return handler, c.ArgErr()
}
for _, regex := range args {
r, err := regexp.Compile(regex)
if err != nil {
return handler, c.Errf("could not parse regex: %s, %v", regex, err)
}
templatePrefix = templatePrefix + regex + " "
t.regex = append(t.regex, r)
}
case "answer":
args := c.RemainingArgs()
if len(args) == 0 {
return nil, c.ArgErr()
return handler, c.ArgErr()
}
for _, answer := range args {
tmpl, err := gotmpl.New("answer").Parse(answer)
if err != nil {
return nil, c.Errf("could not compile template: %s, %v", c.Val(), err)
return handler, c.Errf("could not compile template: %s, %v", c.Val(), err)
}
t.answer = append(t.answer, tmpl)
}
@@ -94,12 +106,12 @@ func templateParse(c *caddy.Controller) (templates []template, err error) {
case "additional":
args := c.RemainingArgs()
if len(args) == 0 {
return nil, c.ArgErr()
return handler, c.ArgErr()
}
for _, additional := range args {
tmpl, err := gotmpl.New("additional").Parse(additional)
if err != nil {
return nil, c.Errf("could not compile template: %s, %v\n", c.Val(), err)
return handler, c.Errf("could not compile template: %s, %v\n", c.Val(), err)
}
t.additional = append(t.additional, tmpl)
}
@@ -107,37 +119,49 @@ func templateParse(c *caddy.Controller) (templates []template, err error) {
case "authority":
args := c.RemainingArgs()
if len(args) == 0 {
return nil, c.ArgErr()
return handler, c.ArgErr()
}
for _, authority := range args {
tmpl, err := gotmpl.New("authority").Parse(authority)
if err != nil {
return nil, c.Errf("could not compile template: %s, %v\n", c.Val(), err)
return handler, c.Errf("could not compile template: %s, %v\n", c.Val(), err)
}
t.authority = append(t.authority, tmpl)
}
case "rcode":
if !c.NextArg() {
return nil, c.ArgErr()
return handler, c.ArgErr()
}
rcode, ok := dns.StringToRcode[c.Val()]
if !ok {
return nil, c.Errf("unknown rcode %s", c.Val())
return handler, c.Errf("unknown rcode %s", c.Val())
}
t.rcode = rcode
case "fallthrough":
args := c.RemainingArgs()
if len(args) > 0 {
t.fthrough.SetZonesFromArgs(c.RemainingArgs())
} else {
t.fthrough.SetZonesFromArgs(zones)
}
default:
return nil, c.ArgErr()
return handler, c.ArgErr()
}
}
if len(t.answer) == 0 && len(t.additional) == 0 && t.rcode == dns.RcodeSuccess {
return nil, c.Errf("no answer section for template %s %sfound", t.qtype, templatePrefix)
if len(t.regex) == 0 {
t.regex = append(t.regex, regexp.MustCompile(".*"))
}
templates = append(templates, t)
if len(t.answer) == 0 && len(t.additional) == 0 && t.rcode == dns.RcodeSuccess {
return handler, c.Errf("no answer section for template found: %v", handler)
}
handler.Templates = append(handler.Templates, t)
}
return templates, nil
return
}