aboutsummaryrefslogtreecommitdiff
path: root/plugin/normalize.go
diff options
context:
space:
mode:
authorGravatar Chris O'Haver <cohaver@infoblox.com> 2021-05-27 07:26:14 -0400
committerGravatar GitHub <noreply@github.com> 2021-05-27 13:26:14 +0200
commitd8a0d97df27244c8f958dea4cb8bbb89121e355d (patch)
tree9d2b733dcc213463cfee98d331d7a92e7689dc01 /plugin/normalize.go
parentb56f2efe54bcfafa614f7b5f1acb876d8d80c11b (diff)
downloadcoredns-d8a0d97df27244c8f958dea4cb8bbb89121e355d.tar.gz
coredns-d8a0d97df27244c8f958dea4cb8bbb89121e355d.tar.zst
coredns-d8a0d97df27244c8f958dea4cb8bbb89121e355d.zip
deprecate Normalize and MustNormalize (#4648)
* deprecate normalize and mustnormalize Signed-off-by: Chris O'Haver <cohaver@infoblox.com> * add runtime warning Signed-off-by: Chris O'Haver <cohaver@infoblox.com> * elaborate runtime warning Signed-off-by: Chris O'Haver <cohaver@infoblox.com> * include caller info Signed-off-by: Chris O'Haver <cohaver@infoblox.com>
Diffstat (limited to 'plugin/normalize.go')
-rw-r--r--plugin/normalize.go56
1 files changed, 53 insertions, 3 deletions
diff --git a/plugin/normalize.go b/plugin/normalize.go
index 10a60a806..b46966b74 100644
--- a/plugin/normalize.go
+++ b/plugin/normalize.go
@@ -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"
@@ -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)
@@ -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.
}