diff options
author | 2018-02-14 14:19:32 -0500 | |
---|---|---|
committer | 2018-02-14 20:19:32 +0100 | |
commit | 76455c6a0deb812db4a6a091cdf305ef4960c5b7 (patch) | |
tree | 8250653a6d6b8bb304f8e9d7f4ccd23f8ff32263 /plugin | |
parent | a0834b1dd50ea6d46b0e28c66868dec03422bdd5 (diff) | |
download | coredns-76455c6a0deb812db4a6a091cdf305ef4960c5b7.tar.gz coredns-76455c6a0deb812db4a6a091cdf305ef4960c5b7.tar.zst coredns-76455c6a0deb812db4a6a091cdf305ef4960c5b7.zip |
Plugin/BIND - extend the syntax to allow multiple addresses (#1512)
* Extend bind to allow multiple addresses. UTs added. Changes the log for server starting, adding address when available
* update readme for bind
* fixes after review
* minor fix on readme
* accept multiple BIND directives in blocserver, consolidate the addresses
* fixes after review - format logging server address, variable names
Diffstat (limited to 'plugin')
-rw-r--r-- | plugin/bind/README.md | 33 | ||||
-rw-r--r-- | plugin/bind/bind_test.go | 50 | ||||
-rw-r--r-- | plugin/bind/setup.go | 18 | ||||
-rw-r--r-- | plugin/trace/setup.go | 2 |
4 files changed, 75 insertions, 28 deletions
diff --git a/plugin/bind/README.md b/plugin/bind/README.md index 989a65b28..a33a6c8da 100644 --- a/plugin/bind/README.md +++ b/plugin/bind/README.md @@ -6,23 +6,46 @@ ## Description -Normally, the listener binds to the wildcard host. However, you may force the listener to bind to -another IP instead. This directive accepts only an address, not a port. +Normally, the listener binds to the wildcard host. However, you may want the listener to bind to +another IP instead. + +If several addresses are provided, a listener will be open on each of the IP provided. + +Each address has to be an IP of one of the interfaces of the host. ## Syntax ~~~ txt -bind ADDRESS +bind ADDRESS ... ~~~ -**ADDRESS** is the IP address to bind to. +**ADDRESS** is an IP address to bind to. +When several addresses are provided a listener will be opened on each of the addresses. ## Examples To make your socket accessible only to that machine, bind to IP 127.0.0.1 (localhost): -~~~ +~~~ corefile . { bind 127.0.0.1 } ~~~ + +To allow processing DNS requests only local host on both IPv4 and IPv6 stacks, use the syntax: + +~~~ corefile +. { + bind 127.0.0.1 ::1 +} +~~~ + +If the configuration comes up with several *bind* directives, all addresses are consolidated together: +The following sample is equivalent to the preceding: + +~~~ corefile +. { + bind 127.0.0.1 + bind ::1 +} +~~~ diff --git a/plugin/bind/bind_test.go b/plugin/bind/bind_test.go index 11556f0bd..9b1dc54aa 100644 --- a/plugin/bind/bind_test.go +++ b/plugin/bind/bind_test.go @@ -9,22 +9,38 @@ import ( ) func TestSetupBind(t *testing.T) { - c := caddy.NewTestController("dns", `bind 1.2.3.4`) - err := setupBind(c) - if err != nil { - t.Fatalf("Expected no errors, but got: %v", err) - } - - cfg := dnsserver.GetConfig(c) - if got, want := cfg.ListenHost, "1.2.3.4"; got != want { - t.Errorf("Expected the config's ListenHost to be %s, was %s", want, got) - } -} - -func TestBindAddress(t *testing.T) { - c := caddy.NewTestController("dns", `bind 1.2.3.bla`) - err := setupBind(c) - if err == nil { - t.Fatalf("Expected errors, but got none") + for i, test := range []struct { + config string + expected []string + failing bool + }{ + {`bind 1.2.3.4`, []string{"1.2.3.4"}, false}, + {`bind`, nil, true}, + {`bind 1.2.3.invalid`, nil, true}, + {`bind 1.2.3.4 ::5`, []string{"1.2.3.4", "::5"}, false}, + {`bind ::1 1.2.3.4 ::5 127.9.9.0`, []string{"::1", "1.2.3.4", "::5", "127.9.9.0"}, false}, + {`bind ::1 1.2.3.4 ::5 127.9.9.0 noone`, nil, true}, + } { + c := caddy.NewTestController("dns", test.config) + err := setupBind(c) + if err != nil { + if !test.failing { + t.Fatalf("test %d, expected no errors, but got: %v", i, err) + } + continue + } + if test.failing { + t.Fatalf("test %d, expected to failed but did not, returned values", i) + } + cfg := dnsserver.GetConfig(c) + if len(cfg.ListenHosts) != len(test.expected) { + t.Errorf("test %d : expected the config's ListenHosts size to be %d, was %d", i, len(test.expected), len(cfg.ListenHosts)) + continue + } + for i, v := range test.expected { + if got, want := cfg.ListenHosts[i], v; got != want { + t.Errorf("test %d : expected the config's ListenHost to be %s, was %s", i, want, got) + } + } } } diff --git a/plugin/bind/setup.go b/plugin/bind/setup.go index 796377841..a57e8ffce 100644 --- a/plugin/bind/setup.go +++ b/plugin/bind/setup.go @@ -12,13 +12,21 @@ import ( func setupBind(c *caddy.Controller) error { config := dnsserver.GetConfig(c) + + // addresses will be consolidated over all BIND directives available in that BlocServer + all := []string{} for c.Next() { - if !c.Args(&config.ListenHost) { - return plugin.Error("bind", c.ArgErr()) + addrs := c.RemainingArgs() + if len(addrs) == 0 { + return plugin.Error("bind", fmt.Errorf("at least one address is expected")) } + for _, addr := range addrs { + if net.ParseIP(addr) == nil { + return plugin.Error("bind", fmt.Errorf("not a valid IP address: %s", addr)) + } + } + all = append(all, addrs...) } - if net.ParseIP(config.ListenHost) == nil { - return plugin.Error("bind", fmt.Errorf("not a valid IP address: %s", config.ListenHost)) - } + config.ListenHosts = all return nil } diff --git a/plugin/trace/setup.go b/plugin/trace/setup.go index 5c6e473c3..2eb93c3a2 100644 --- a/plugin/trace/setup.go +++ b/plugin/trace/setup.go @@ -41,7 +41,7 @@ func traceParse(c *caddy.Controller) (*trace, error) { ) cfg := dnsserver.GetConfig(c) - tr.ServiceEndpoint = cfg.ListenHost + ":" + cfg.Port + tr.ServiceEndpoint = cfg.HostAddresses() for c.Next() { // trace var err error args := c.RemainingArgs() |