1
0
mirror of https://github.com/StackExchange/dnscontrol.git synced 2024-05-11 05:55:12 +00:00

Switch to Go 1.13 error wrapping (#604)

* Replaced errors.Wrap with fmt.Errorf (#589)

* Find:    errors\.Wrap\(([^,]+),\s+(["`][^"`]*)(["`])\)
  Replace: fmt.Errorf($2: %w$3, $1)

* Replaced errors.Wrapf with fmt.Errorf (#589)

* Find:    errors\.Wrapf\(([^,]+),\s+(["`][^"`]*)(["`])\)
  Replace: fmt.Errorf($2: %w$3, $1)
* Find:    errors\.Wrapf\(([^,]+),\s+(["`][^"`]*)(["`])(,[^)]+)\)
* Replace: fmt.Errorf($2: %w$3$4, $1)

* Replaced errors.Errorf with fmt.Errorf (#589)

* Find:    errors\.Errorf
  Replace: fmt.Errorf

* Cleaned up remaining imports

* Cleanup

* Regenerate provider support matrix

This was broken by #533 ... and it's now the third time this has been missed.
This commit is contained in:
Patrick Gaskin
2020-01-28 11:06:56 -05:00
committed by Tom Limoncelli
parent cae35a2c8f
commit 825ba2d081
64 changed files with 328 additions and 379 deletions

View File

@@ -7,12 +7,12 @@ import (
"path/filepath"
"strings"
"github.com/robertkrimen/otto" // load underscore js into vm by default
_ "github.com/robertkrimen/otto/underscore" // required by otto
"github.com/StackExchange/dnscontrol/v2/models"
"github.com/StackExchange/dnscontrol/v2/pkg/printer"
"github.com/StackExchange/dnscontrol/v2/pkg/transform"
"github.com/pkg/errors"
"github.com/robertkrimen/otto" // load underscore js into vm by default
_ "github.com/robertkrimen/otto/underscore" // required by otto
)
// currentDirectory is the current directory as used by require().
@@ -27,7 +27,7 @@ var currentDirectory string
func ExecuteJavascript(file string, devMode bool) (*models.DNSConfig, error) {
script, err := ioutil.ReadFile(file)
if err != nil {
return nil, errors.Errorf("Reading js file %s: %s", file, err)
return nil, fmt.Errorf("Reading js file %s: %s", file, err)
}
// Record the directory path leading up to this file.

View File

@@ -1,10 +1,9 @@
package normalize
import (
"fmt"
"strings"
"github.com/pkg/errors"
"github.com/StackExchange/dnscontrol/v2/models"
"github.com/StackExchange/dnscontrol/v2/pkg/spflib"
)
@@ -43,7 +42,7 @@ func flattenSPFs(cfg *models.DNSConfig) []error {
// now split if needed
if split, ok := txt.Metadata["split"]; ok {
if !strings.Contains(split, "%d") {
errs = append(errs, Warning{errors.Errorf("Split format `%s` in `%s` is not proper format (should have %%d in it)", split, txt.GetLabelFQDN())})
errs = append(errs, Warning{fmt.Errorf("Split format `%s` in `%s` is not proper format (should have %%d in it)", split, txt.GetLabelFQDN())})
continue
}
recs := rec.TXTSplit(split + "." + domain.Name)
@@ -65,7 +64,7 @@ func flattenSPFs(cfg *models.DNSConfig) []error {
}
// check if cache is stale
for _, e := range cache.ResolveErrors() {
errs = append(errs, Warning{errors.Errorf("problem resolving SPF record: %s", e)})
errs = append(errs, Warning{fmt.Errorf("problem resolving SPF record: %s", e)})
}
if len(cache.ResolveErrors()) == 0 {
changed := cache.ChangedRecords()
@@ -73,7 +72,7 @@ func flattenSPFs(cfg *models.DNSConfig) []error {
if err := cache.Save("spfcache.updated.json"); err != nil {
errs = append(errs, err)
} else {
errs = append(errs, Warning{errors.Errorf("%d spf record lookups are out of date with cache (%s).\nWrote changes to spfcache.updated.json. Please rename and commit:\n $ mv spfcache.updated.json spfcache.json\n $ git commit -m'Update spfcache.json' spfcache.json", len(changed), strings.Join(changed, ","))})
errs = append(errs, Warning{fmt.Errorf("%d spf record lookups are out of date with cache (%s).\nWrote changes to spfcache.updated.json. Please rename and commit:\n $ mv spfcache.updated.json spfcache.json\n $ git commit -m'Update spfcache.json' spfcache.json", len(changed), strings.Join(changed, ","))})
}
}
}

View File

@@ -10,13 +10,12 @@ import (
"github.com/StackExchange/dnscontrol/v2/providers"
"github.com/miekg/dns"
"github.com/miekg/dns/dnsutil"
"github.com/pkg/errors"
)
// Returns false if target does not validate.
func checkIPv4(label string) error {
if net.ParseIP(label).To4() == nil {
return errors.Errorf("WARNING: target (%v) is not an IPv4 address", label)
return fmt.Errorf("WARNING: target (%v) is not an IPv4 address", label)
}
return nil
}
@@ -24,7 +23,7 @@ func checkIPv4(label string) error {
// Returns false if target does not validate.
func checkIPv6(label string) error {
if net.ParseIP(label).To16() == nil {
return errors.Errorf("WARNING: target (%v) is not an IPv6 address", label)
return fmt.Errorf("WARNING: target (%v) is not an IPv6 address", label)
}
return nil
}
@@ -35,14 +34,14 @@ func checkTarget(target string) error {
return nil
}
if len(target) < 1 {
return errors.Errorf("empty target")
return fmt.Errorf("empty target")
}
if strings.ContainsAny(target, `'" +,|!£$%&/()=?^*ç°§;:<>[]()@`) {
return errors.Errorf("target (%v) includes invalid char", target)
return fmt.Errorf("target (%v) includes invalid char", target)
}
// If it containts a ".", it must end in a ".".
if strings.ContainsRune(target, '.') && target[len(target)-1] != '.' {
return errors.Errorf("target (%v) must end with a (.) [https://stackexchange.github.io/dnscontrol/why-the-dot]", target)
return fmt.Errorf("target (%v) must end with a (.) [https://stackexchange.github.io/dnscontrol/why-the-dot]", target)
}
return nil
}
@@ -69,11 +68,11 @@ func validateRecordTypes(rec *models.RecordConfig, domain string, pTypes []strin
if !ok {
cType := providers.GetCustomRecordType(rec.Type)
if cType == nil {
return errors.Errorf("Unsupported record type (%v) domain=%v name=%v", rec.Type, domain, rec.GetLabel())
return fmt.Errorf("Unsupported record type (%v) domain=%v name=%v", rec.Type, domain, rec.GetLabel())
}
for _, providerType := range pTypes {
if providerType != cType.Provider {
return errors.Errorf("Custom record type %s is not compatible with provider type %s", rec.Type, providerType)
return fmt.Errorf("Custom record type %s is not compatible with provider type %s", rec.Type, providerType)
}
}
// it is ok. Lets replace the type with real type and add metadata to say we checked it
@@ -105,14 +104,14 @@ func checkLabel(label string, rType string, domain string, meta map[string]strin
return nil
}
if len(label) < 1 {
return errors.Errorf("empty %s label in %s", rType, domain)
return fmt.Errorf("empty %s label in %s", rType, domain)
}
if label[len(label)-1] == '.' {
return errors.Errorf("label %s.%s ends with a (.)", label, domain)
return fmt.Errorf("label %s.%s ends with a (.)", label, domain)
}
if strings.HasSuffix(label, domain) {
if m := meta["skip_fqdn_check"]; m != "true" {
return errors.Errorf(`label %s ends with domain name %s. Record names should not be fully qualified. Add {skip_fqdn_check:"true"} to this record if you really want to make %s.%s`, label, domain, label, domain)
return fmt.Errorf(`label %s ends with domain name %s. Record names should not be fully qualified. Add {skip_fqdn_check:"true"} to this record if you really want to make %s.%s`, label, domain, label, domain)
}
}
@@ -133,7 +132,7 @@ func checkLabel(label string, rType string, domain string, meta map[string]strin
}
// Otherwise, warn.
if strings.ContainsRune(label, '_') {
return Warning{errors.Errorf("label %s.%s contains an underscore", label, domain)}
return Warning{fmt.Errorf("label %s.%s contains an underscore", label, domain)}
}
return nil
@@ -145,7 +144,7 @@ func checkTargets(rec *models.RecordConfig, domain string) (errs []error) {
target := rec.GetTargetField()
check := func(e error) {
if e != nil {
err := errors.Errorf("In %s %s.%s: %s", rec.Type, rec.GetLabel(), domain, e.Error())
err := fmt.Errorf("In %s %s.%s: %s", rec.Type, rec.GetLabel(), domain, e.Error())
if _, ok := e.(Warning); ok {
err = Warning{err}
}
@@ -160,14 +159,14 @@ func checkTargets(rec *models.RecordConfig, domain string) (errs []error) {
case "CNAME":
check(checkTarget(target))
if label == "@" {
check(errors.Errorf("cannot create CNAME record for bare domain"))
check(fmt.Errorf("cannot create CNAME record for bare domain"))
}
case "MX":
check(checkTarget(target))
case "NS":
check(checkTarget(target))
if label == "@" {
check(errors.Errorf("cannot create NS record for bare domain. Use NAMESERVER instead"))
check(fmt.Errorf("cannot create NS record for bare domain. Use NAMESERVER instead"))
}
case "PTR":
check(checkTarget(target))
@@ -183,7 +182,7 @@ func checkTargets(rec *models.RecordConfig, domain string) (errs []error) {
// it is a valid custom type. We perform no validation on target
return
}
errs = append(errs, errors.Errorf("checkTargets: Unimplemented record type (%v) domain=%v name=%v",
errs = append(errs, fmt.Errorf("checkTargets: Unimplemented record type (%v) domain=%v name=%v",
rec.Type, domain, rec.GetLabel()))
}
return
@@ -223,7 +222,7 @@ func importTransform(srcDomain, dstDomain *models.DomainConfig, transforms []tra
case "A":
trs, err := transform.TransformIPToList(net.ParseIP(rec.GetTargetField()), transforms)
if err != nil {
return errors.Errorf("import_transform: TransformIP(%v, %v) returned err=%s", rec.GetTargetField(), transforms, err)
return fmt.Errorf("import_transform: TransformIP(%v, %v) returned err=%s", rec.GetTargetField(), transforms, err)
}
for _, tr := range trs {
r := newRec()
@@ -238,7 +237,7 @@ func importTransform(srcDomain, dstDomain *models.DomainConfig, transforms []tra
// Not imported.
continue
default:
return errors.Errorf("import_transform: Unimplemented record type %v (%v)",
return fmt.Errorf("import_transform: Unimplemented record type %v (%v)",
rec.Type, rec.GetLabel())
}
}
@@ -270,7 +269,7 @@ func NormalizeAndValidateConfig(config *models.DNSConfig) (errs []error) {
pType := provider.ProviderType
// If NO_PURGE is in use, make sure this *isn't* a provider that *doesn't* support NO_PURGE.
if domain.KeepUnknown && providers.ProviderHasCapability(pType, providers.CantUseNOPURGE) {
errs = append(errs, errors.Errorf("%s uses NO_PURGE which is not supported by %s(%s)", domain.Name, provider.Name, pType))
errs = append(errs, fmt.Errorf("%s uses NO_PURGE which is not supported by %s(%s)", domain.Name, provider.Name, pType))
}
// Record if any providers do not support TXTMulti:
@@ -319,26 +318,26 @@ func NormalizeAndValidateConfig(config *models.DNSConfig) (errs []error) {
rec.SetLabel(name, domain.Name)
} else if rec.Type == "CAA" {
if rec.CaaTag != "issue" && rec.CaaTag != "issuewild" && rec.CaaTag != "iodef" {
errs = append(errs, errors.Errorf("CAA tag %s is invalid", rec.CaaTag))
errs = append(errs, fmt.Errorf("CAA tag %s is invalid", rec.CaaTag))
}
} else if rec.Type == "TLSA" {
if rec.TlsaUsage < 0 || rec.TlsaUsage > 3 {
errs = append(errs, errors.Errorf("TLSA Usage %d is invalid in record %s (domain %s)",
errs = append(errs, fmt.Errorf("TLSA Usage %d is invalid in record %s (domain %s)",
rec.TlsaUsage, rec.GetLabel(), domain.Name))
}
if rec.TlsaSelector < 0 || rec.TlsaSelector > 1 {
errs = append(errs, errors.Errorf("TLSA Selector %d is invalid in record %s (domain %s)",
errs = append(errs, fmt.Errorf("TLSA Selector %d is invalid in record %s (domain %s)",
rec.TlsaSelector, rec.GetLabel(), domain.Name))
}
if rec.TlsaMatchingType < 0 || rec.TlsaMatchingType > 2 {
errs = append(errs, errors.Errorf("TLSA MatchingType %d is invalid in record %s (domain %s)",
errs = append(errs, fmt.Errorf("TLSA MatchingType %d is invalid in record %s (domain %s)",
rec.TlsaMatchingType, rec.GetLabel(), domain.Name))
}
} else if rec.Type == "TXT" && len(txtMultiDissenters) != 0 && len(rec.TxtStrings) > 1 {
// There are providers that don't support TXTMulti yet there is
// a TXT record with multiple strings:
errs = append(errs,
errors.Errorf("TXT records with multiple strings (label %v domain: %v) not supported by %s",
fmt.Errorf("TXT records with multiple strings (label %v domain: %v) not supported by %s",
rec.GetLabel(), domain.Name, strings.Join(txtMultiDissenters, ",")))
}
@@ -405,14 +404,14 @@ func checkCNAMEs(dc *models.DomainConfig) (errs []error) {
for _, r := range dc.Records {
if r.Type == "CNAME" {
if cnames[r.GetLabel()] {
errs = append(errs, errors.Errorf("Cannot have multiple CNAMEs with same name: %s", r.GetLabelFQDN()))
errs = append(errs, fmt.Errorf("Cannot have multiple CNAMEs with same name: %s", r.GetLabelFQDN()))
}
cnames[r.GetLabel()] = true
}
}
for _, r := range dc.Records {
if cnames[r.GetLabel()] && r.Type != "CNAME" {
errs = append(errs, errors.Errorf("Cannot have CNAME and %s record with same name: %s", r.Type, r.GetLabelFQDN()))
errs = append(errs, fmt.Errorf("Cannot have CNAME and %s record with same name: %s", r.Type, r.GetLabelFQDN()))
}
}
return
@@ -423,7 +422,7 @@ func checkDuplicates(records []*models.RecordConfig) (errs []error) {
for _, r := range records {
diffable := fmt.Sprintf("%s %s %s", r.GetLabelFQDN(), r.Type, r.ToDiffable())
if seen[diffable] != nil {
errs = append(errs, errors.Errorf("Exact duplicate record found: %s", diffable))
errs = append(errs, fmt.Errorf("Exact duplicate record found: %s", diffable))
}
seen[diffable] = r
}
@@ -454,7 +453,7 @@ func checkProviderCapabilities(dc *models.DomainConfig) error {
}
for _, provider := range dc.DNSProviderInstances {
if !providers.ProviderHasCapability(provider.ProviderType, ty.cap) {
return errors.Errorf("Domain %s uses %s records, but DNS provider type %s does not support them", dc.Name, ty.rType, provider.ProviderType)
return fmt.Errorf("Domain %s uses %s records, but DNS provider type %s does not support them", dc.Name, ty.rType, provider.ProviderType)
}
}
}

View File

@@ -1,14 +1,10 @@
package spflib
import (
"fmt"
"strings"
"bytes"
"fmt"
"io"
"github.com/pkg/errors"
"strings"
)
// SPFRecord stores the parts of an SPF record.
@@ -48,7 +44,7 @@ var qualifiers = map[byte]bool{
// Parse parses a raw SPF record.
func Parse(text string, dnsres Resolver) (*SPFRecord, error) {
if !strings.HasPrefix(text, "v=spf1 ") {
return nil, errors.Errorf("Not an spf record")
return nil, fmt.Errorf("Not an spf record")
}
parts := strings.Split(text, " ")
rec := &SPFRecord{}
@@ -79,7 +75,7 @@ func Parse(text string, dnsres Resolver) (*SPFRecord, error) {
// pi + 2: because pi starts at 0 when it iterates starting on parts[1],
// and because len(parts) is one bigger than the highest index.
if (pi + 2) != len(parts) {
return nil, errors.Errorf("%s must be last item", part)
return nil, fmt.Errorf("%s must be last item", part)
}
p.IncludeDomain = strings.TrimPrefix(part, "redirect=")
} else {
@@ -93,13 +89,13 @@ func Parse(text string, dnsres Resolver) (*SPFRecord, error) {
}
p.IncludeRecord, err = Parse(subRecord, dnsres)
if err != nil {
return nil, errors.Errorf("In included spf: %s", err)
return nil, fmt.Errorf("In included spf: %s", err)
}
}
} else if strings.HasPrefix(part, "exists:") || strings.HasPrefix(part, "ptr:") {
p.IsLookup = true
} else {
return nil, errors.Errorf("Unsupported spf part %s", part)
return nil, fmt.Errorf("Unsupported spf part %s", part)
}
}

View File

@@ -2,12 +2,11 @@ package spflib
import (
"encoding/json"
"fmt"
"io/ioutil"
"net"
"os"
"strings"
"github.com/pkg/errors"
)
// Resolver looks up spf txt records associated with a FQDN.
@@ -28,13 +27,13 @@ func (l LiveResolver) GetSPF(name string) (string, error) {
for _, v := range vals {
if strings.HasPrefix(v, "v=spf1") {
if spf != "" {
return "", errors.Errorf("%s has multiple SPF records", name)
return "", fmt.Errorf("%s has multiple SPF records", name)
}
spf = v
}
}
if spf == "" {
return "", errors.Errorf("%s has no SPF record", name)
return "", fmt.Errorf("%s has no SPF record", name)
}
return spf, nil
}

View File

@@ -4,8 +4,6 @@ import (
"fmt"
"net"
"strings"
"github.com/pkg/errors"
)
// ReverseDomainName turns a CIDR block into a reversed (in-addr) name.
@@ -20,13 +18,13 @@ func ReverseDomainName(cidr string) (string, error) {
}
base = strings.TrimRight(base, ".")
if !a.Equal(c.IP) {
return "", errors.Errorf("CIDR %v has 1 bits beyond the mask", cidr)
return "", fmt.Errorf("CIDR %v has 1 bits beyond the mask", cidr)
}
bits, total := c.Mask.Size()
var toTrim int
if bits == 0 {
return "", errors.Errorf("Cannot use /0 in reverse cidr")
return "", fmt.Errorf("Cannot use /0 in reverse cidr")
}
// Handle IPv4 "Classless in-addr.arpa delegation" RFC2317:
@@ -41,16 +39,16 @@ func ReverseDomainName(cidr string) (string, error) {
// Handle IPv4 Class-full and IPv6:
if total == 32 {
if bits%8 != 0 {
return "", errors.Errorf("IPv4 mask must be multiple of 8 bits")
return "", fmt.Errorf("IPv4 mask must be multiple of 8 bits")
}
toTrim = (total - bits) / 8
} else if total == 128 {
if bits%4 != 0 {
return "", errors.Errorf("IPv6 mask must be multiple of 4 bits")
return "", fmt.Errorf("IPv6 mask must be multiple of 4 bits")
}
toTrim = (total - bits) / 4
} else {
return "", errors.Errorf("Address is not IPv4 or IPv6: %v", cidr)
return "", fmt.Errorf("Address is not IPv4 or IPv6: %v", cidr)
}
parts := strings.SplitN(base, ".", toTrim+1)

View File

@@ -6,8 +6,6 @@ import (
"regexp"
"strconv"
"strings"
"github.com/pkg/errors"
)
// PtrNameMagic implements the PTR magic.
@@ -22,7 +20,7 @@ func PtrNameMagic(name, domain string) (string, error) {
if strings.HasSuffix(name, "."+domain+".") {
return strings.TrimSuffix(name, "."+domain+"."), nil
}
return name, errors.Errorf("PTR record %v in wrong domain (%v)", name, domain)
return name, fmt.Errorf("PTR record %v in wrong domain (%v)", name, domain)
}
// If the domain is .arpa, we do magic.
@@ -57,7 +55,7 @@ func ipv4magic(name, domain string) (string, error) {
return strings.SplitN(rev, ".", 2)[0], nil
}
return "", errors.Errorf("PTR record %v in wrong IPv4 domain (%v)", name, domain)
return "", fmt.Errorf("PTR record %v in wrong IPv4 domain (%v)", name, domain)
}
var isRfc2317Format1 = regexp.MustCompile(`(\d{1,3})/(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.in-addr\.arpa$`)
@@ -112,7 +110,7 @@ func ipv6magic(name, domain string) (string, error) {
return name, err
}
if !strings.HasSuffix(rev, "."+domain) {
err = errors.Errorf("PTR record %v in wrong IPv6 domain (%v)", name, domain)
err = fmt.Errorf("PTR record %v in wrong IPv6 domain (%v)", name, domain)
}
return strings.TrimSuffix(rev, "."+domain), err
}

View File

@@ -1,10 +1,9 @@
package transform
import (
"fmt"
"net"
"strings"
"github.com/pkg/errors"
)
// IpConversion describes an IP conversion.
@@ -17,7 +16,7 @@ type IpConversion struct {
func ipToUint(i net.IP) (uint32, error) {
parts := i.To4()
if parts == nil || len(parts) != 4 {
return 0, errors.Errorf("%s is not an ipv4 address", parts.String())
return 0, fmt.Errorf("%s is not an ipv4 address", parts.String())
}
r := uint32(parts[0])<<24 | uint32(parts[1])<<16 | uint32(parts[2])<<8 | uint32(parts[3])
return r, nil
@@ -39,7 +38,7 @@ func DecodeTransformTable(transforms string) ([]IpConversion, error) {
for ri, row := range rows {
items := strings.Split(row, "~")
if len(items) != 4 {
return nil, errors.Errorf("transform_table rows should have 4 elements. (%v) found in row (%v) of %#v", len(items), ri, transforms)
return nil, fmt.Errorf("transform_table rows should have 4 elements. (%v) found in row (%v) of %#v", len(items), ri, transforms)
}
for i, item := range items {
items[i] = strings.TrimSpace(item)
@@ -58,7 +57,7 @@ func DecodeTransformTable(transforms string) ([]IpConversion, error) {
}
addr := net.ParseIP(ip)
if addr == nil {
return nil, errors.Errorf("%s is not a valid ip address", ip)
return nil, fmt.Errorf("%s is not a valid ip address", ip)
}
ips = append(ips, addr)
}
@@ -75,10 +74,10 @@ func DecodeTransformTable(transforms string) ([]IpConversion, error) {
low, _ := ipToUint(con.Low)
high, _ := ipToUint(con.High)
if low > high {
return nil, errors.Errorf("transform_table Low should be less than High. row (%v) %v>%v (%v)", ri, con.Low, con.High, transforms)
return nil, fmt.Errorf("transform_table Low should be less than High. row (%v) %v>%v (%v)", ri, con.Low, con.High, transforms)
}
if len(con.NewBases) > 0 && len(con.NewIPs) > 0 {
return nil, errors.Errorf("transform_table_rows should only specify one of NewBases or NewIPs, Not both")
return nil, fmt.Errorf("transform_table_rows should only specify one of NewBases or NewIPs, Not both")
}
result = append(result, con)
}
@@ -93,7 +92,7 @@ func TransformIP(address net.IP, transforms []IpConversion) (net.IP, error) {
return nil, err
}
if len(ips) != 1 {
return nil, errors.Errorf("Expect exactly one ip for TransformIP result. Got: %s", ips)
return nil, fmt.Errorf("Expect exactly one ip for TransformIP result. Got: %s", ips)
}
return ips[0], err
}