Skip to content

Commit

Permalink
deprecate Normalize and MustNormalize (coredns#4648)
Browse files Browse the repository at this point in the history
* deprecate normalize and mustnormalize

Signed-off-by: Chris O'Haver <[email protected]>

* add runtime warning

Signed-off-by: Chris O'Haver <[email protected]>

* elaborate runtime warning

Signed-off-by: Chris O'Haver <[email protected]>

* include caller info

Signed-off-by: Chris O'Haver <[email protected]>
  • Loading branch information
chrisohaver authored May 27, 2021
1 parent b56f2ef commit d8a0d97
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 11 deletions.
4 changes: 2 additions & 2 deletions plugin/forward/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func parseStanza(c *caddy.Controller) (*Forward, error) {
return f, c.ArgErr()
}
origFrom := f.from
f.from = plugin.Host(f.from).Normalize()[0] // there can only be one here, won't work with non-octet reverse
f.from = plugin.Host(f.from).NormalizeExact()[0] // there can only be one here, won't work with non-octet reverse

if len(f.from) > 1 {
log.Warningf("Unsupported CIDR notation: '%s' expands to multiple zones. Using only '%s'.", origFrom, f.from)
Expand Down Expand Up @@ -156,7 +156,7 @@ func parseBlock(c *caddy.Controller, f *Forward) error {
return c.ArgErr()
}
for i := 0; i < len(ignore); i++ {
f.ignored = append(f.ignored, plugin.Host(ignore[i]).Normalize()...)
f.ignored = append(f.ignored, plugin.Host(ignore[i]).NormalizeExact()...)
}
case "max_fails":
if !c.NextArg() {
Expand Down
4 changes: 2 additions & 2 deletions plugin/grpc/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func parseStanza(c *caddy.Controller) (*GRPC, error) {
if !c.Args(&g.from) {
return g, c.ArgErr()
}
g.from = plugin.Host(g.from).Normalize()[0] // only the first is used.
g.from = plugin.Host(g.from).NormalizeExact()[0] // only the first is used.

to := c.RemainingArgs()
if len(to) == 0 {
Expand Down Expand Up @@ -100,7 +100,7 @@ func parseBlock(c *caddy.Controller, g *GRPC) error {
return c.ArgErr()
}
for i := 0; i < len(ignore); i++ {
g.ignored = append(g.ignored, plugin.Host(ignore[i]).Normalize()...)
g.ignored = append(g.ignored, plugin.Host(ignore[i]).NormalizeExact()...)
}
case "tls":
args := c.RemainingArgs()
Expand Down
2 changes: 1 addition & 1 deletion plugin/loop/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func parse(c *caddy.Controller) (*Loop, error) {
}

if len(c.ServerBlockKeys) > 0 {
zones = plugin.Host(c.ServerBlockKeys[0]).Normalize()
zones = plugin.Host(c.ServerBlockKeys[0]).NormalizeExact()
}
}
return New(zones[0]), nil
Expand Down
56 changes: 53 additions & 3 deletions plugin/normalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package plugin
import (
"fmt"
"net"
"runtime"
"strconv"
"strings"

"github.com/coredns/coredns/plugin/pkg/cidr"
"github.com/coredns/coredns/plugin/pkg/log"
"github.com/coredns/coredns/plugin/pkg/parse"

"github.com/miekg/dns"
Expand Down Expand Up @@ -63,8 +65,56 @@ type (

// Normalize will return the host portion of host, stripping
// of any port or transport. The host will also be fully qualified and lowercased.
// An empty string is returned on failure
// Deprecated: use OriginsFromArgsOrServerBlock or NormalizeExact
func (h Host) Normalize() string {
var caller string
if _, file, line, ok := runtime.Caller(1); ok {
caller = fmt.Sprintf("(%v line %d) ", file, line)
}
log.Warning("An external plugin " + caller + "is using the deprecated function Normalize. " +
"This will be removed in a future versions of CoreDNS. The plugin should be updated to use " +
"OriginsFromArgsOrServerBlock or NormalizeExact instead.")

s := string(h)
_, s = parse.Transport(s)

// The error can be ignored here, because this function is called after the corefile has already been vetted.
hosts, _, err := SplitHostPort(s)
if err != nil {
return ""
}
return Name(hosts[0]).Normalize()
}

// MustNormalize will return the host portion of host, stripping
// of any port or transport. The host will also be fully qualified and lowercased.
// An error is returned on error
// Deprecated: use OriginsFromArgsOrServerBlock or NormalizeExact
func (h Host) MustNormalize() (string, error) {
var caller string
if _, file, line, ok := runtime.Caller(1); ok {
caller = fmt.Sprintf("(%v line %d) ", file, line)
}
log.Warning("An external plugin " + caller + "is using the deprecated function MustNormalize. " +
"This will be removed in a future versions of CoreDNS. The plugin should be updated to use " +
"OriginsFromArgsOrServerBlock or NormalizeExact instead.")

s := string(h)
_, s = parse.Transport(s)

// The error can be ignored here, because this function is called after the corefile has already been vetted.
hosts, _, err := SplitHostPort(s)
if err != nil {
return "", err
}
return Name(hosts[0]).Normalize(), nil
}

// NormalizeExact will return the host portion of host, stripping
// of any port or transport. The host will also be fully qualified and lowercased.
// An empty slice is returned on failure
func (h Host) Normalize() []string {
func (h Host) NormalizeExact() []string {
// The error can be ignored here, because this function should only be called after the corefile has already been vetted.
s := string(h)
_, s = parse.Transport(s)
Expand Down Expand Up @@ -126,13 +176,13 @@ func OriginsFromArgsOrServerBlock(args, serverblock []string) []string {
s := make([]string, len(serverblock))
copy(s, serverblock)
for i := range s {
s[i] = Host(s[i]).Normalize()[0] // expansion of these already happened in dnsserver/registrer.go
s[i] = Host(s[i]).NormalizeExact()[0] // expansion of these already happened in dnsserver/register.go
}
return s
}
s := []string{}
for i := range args {
sx := Host(args[i]).Normalize()
sx := Host(args[i]).NormalizeExact()
if len(sx) == 0 {
continue // silently ignores errors.
}
Expand Down
4 changes: 2 additions & 2 deletions plugin/normalize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TestNameNormalize(t *testing.T) {
}
}

func TestHostNormalize(t *testing.T) {
func TestHostNormalizeExact(t *testing.T) {
tests := []struct {
in string
out []string
Expand All @@ -85,7 +85,7 @@ func TestHostNormalize(t *testing.T) {
}

for i := range tests {
actual := Host(tests[i].in).Normalize()
actual := Host(tests[i].in).NormalizeExact()
expected := tests[i].out
sort.Strings(expected)
for j := range expected {
Expand Down
2 changes: 1 addition & 1 deletion plugin/pkg/fall/fall.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (f F) Through(qname string) bool {
func (f *F) setZones(zones []string) {
z := []string{}
for i := range zones {
z = append(z, plugin.Host(zones[i]).Normalize()...)
z = append(z, plugin.Host(zones[i]).NormalizeExact()...)
}
f.Zones = z
}
Expand Down

0 comments on commit d8a0d97

Please sign in to comment.