Remove the word middleware (#1067)

* Rename middleware to plugin

first pass; mostly used 'sed', few spots where I manually changed
text.

This still builds a coredns binary.

* fmt error

* Rename AddMiddleware to AddPlugin

* Readd AddMiddleware to remain backwards compat
This commit is contained in:
Miek Gieben
2017-09-14 09:36:06 +01:00
committed by GitHub
parent b984aa4559
commit d8714e64e4
354 changed files with 974 additions and 969 deletions

View File

@@ -0,0 +1,54 @@
# secondary
*secondary* enables serving a zone retrieved from a primary server.
## Syntax
~~~
secondary [ZONES...]
~~~
* **ZONES** zones it should be authoritative for. If empty, the zones from the configuration block
are used. Note that without a remote address to *get* the zone from, the above is not that useful.
A working syntax would be:
~~~
secondary [zones...] {
transfer from ADDRESS
transfer to ADDRESS
upstream ADDRESS...
}
~~~
* `transfer from` specifies from which address to fetch the zone. It can be specified multiple times;
if one does not work, another will be tried.
* `transfer to` can be enabled to allow this secondary zone to be transferred again.
* `upstream` defines upstream resolvers to be used resolve external names found (think CNAMEs)
pointing to external names. This is only really useful when CoreDNS is configured as a proxy, for
normal authoritative serving you don't need *or* want to use this. **ADDRESS** can be an IP
address, and IP:port or a string pointing to a file that is structured as /etc/resolv.conf.
## Examples
Transfer `example.org` from 10.0.1.1, and if that fails try 10.1.2.1.
~~~ corefile
example.org {
secondary {
transfer from 10.0.1.1
transfer from 10.1.2.1
}
}
~~~
Or re-export the retrieved zone to other secondaries.
~~~ corefile
. {
secondary example.net {
transfer from 10.1.2.1
transfer to *
}
}
~~~

View File

@@ -0,0 +1,10 @@
// Package secondary implements a secondary plugin.
package secondary
import "github.com/coredns/coredns/plugin/file"
// Secondary implements a secondary plugin that allows CoreDNS to retrieve (via AXFR)
// zone information from a primary server.
type Secondary struct {
file.File
}

108
plugin/secondary/setup.go Normal file
View File

@@ -0,0 +1,108 @@
package secondary
import (
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/file"
"github.com/coredns/coredns/plugin/pkg/dnsutil"
"github.com/coredns/coredns/plugin/proxy"
"github.com/mholt/caddy"
)
func init() {
caddy.RegisterPlugin("secondary", caddy.Plugin{
ServerType: "dns",
Action: setup,
})
}
func setup(c *caddy.Controller) error {
zones, err := secondaryParse(c)
if err != nil {
return plugin.Error("secondary", err)
}
// Add startup functions to retrieve the zone and keep it up to date.
for _, n := range zones.Names {
z := zones.Z[n]
if len(z.TransferFrom) > 0 {
c.OnStartup(func() error {
z.StartupOnce.Do(func() {
z.TransferIn()
go func() {
z.Update()
}()
})
return nil
})
}
}
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
return Secondary{file.File{Next: next, Zones: zones}}
})
return nil
}
func secondaryParse(c *caddy.Controller) (file.Zones, error) {
z := make(map[string]*file.Zone)
names := []string{}
origins := []string{}
prxy := proxy.Proxy{}
for c.Next() {
if c.Val() == "secondary" {
// secondary [origin]
origins = make([]string, len(c.ServerBlockKeys))
copy(origins, c.ServerBlockKeys)
args := c.RemainingArgs()
if len(args) > 0 {
origins = args
}
for i := range origins {
origins[i] = plugin.Host(origins[i]).Normalize()
z[origins[i]] = file.NewZone(origins[i], "stdin")
names = append(names, origins[i])
}
for c.NextBlock() {
t, f := []string{}, []string{}
var e error
switch c.Val() {
case "transfer":
t, f, e = file.TransferParse(c, true)
if e != nil {
return file.Zones{}, e
}
case "upstream":
args := c.RemainingArgs()
if len(args) == 0 {
return file.Zones{}, c.ArgErr()
}
ups, err := dnsutil.ParseHostPortOrFile(args...)
if err != nil {
return file.Zones{}, err
}
prxy = proxy.NewLookup(ups)
default:
return file.Zones{}, c.Errf("unknown property '%s'", c.Val())
}
for _, origin := range origins {
if t != nil {
z[origin].TransferTo = append(z[origin].TransferTo, t...)
}
if f != nil {
z[origin].TransferFrom = append(z[origin].TransferFrom, f...)
}
z[origin].Proxy = prxy
}
}
}
}
return file.Zones{Z: z, Names: names}, nil
}

View File

@@ -0,0 +1,65 @@
package secondary
import (
"testing"
"github.com/mholt/caddy"
)
func TestSecondaryParse(t *testing.T) {
tests := []struct {
inputFileRules string
shouldErr bool
transferFrom string
zones []string
}{
{
`secondary`,
false, // TODO(miek): should actually be true, because without transfer lines this does not make sense
"",
nil,
},
{
`secondary {
transfer from 127.0.0.1
transfer to 127.0.0.1
}`,
false,
"127.0.0.1:53",
nil,
},
{
`secondary example.org {
transfer from 127.0.0.1
transfer to 127.0.0.1
}`,
false,
"127.0.0.1:53",
[]string{"example.org."},
},
}
for i, test := range tests {
c := caddy.NewTestController("dns", test.inputFileRules)
s, err := secondaryParse(c)
if err == nil && test.shouldErr {
t.Fatalf("Test %d expected errors, but got no error", i)
} else if err != nil && !test.shouldErr {
t.Fatalf("Test %d expected no errors, but got '%v'", i, err)
}
for i, name := range test.zones {
if x := s.Names[i]; x != name {
t.Fatalf("Test %d zone names don't match expected %q, but got %q", i, name, x)
}
}
// This is only set *iff* we have a zone (i.e. not in all tests above)
for _, v := range s.Z {
if x := v.TransferFrom[0]; x != test.transferFrom {
t.Fatalf("Test %d transform from names don't match expected %q, but got %q", i, test.transferFrom, x)
}
}
}
}