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

Switch from fmt.Error* to errors.Error* (#317)

This commit is contained in:
Tom Limoncelli
2018-02-05 16:17:20 -05:00
committed by GitHub
parent 65f8fb63f0
commit 4b1dc82c9b
31 changed files with 162 additions and 140 deletions

View File

@@ -1,10 +1,10 @@
package normalize
import (
"fmt"
"strings"
"github.com/miekg/dns/dnsutil"
"github.com/pkg/errors"
"github.com/StackExchange/dnscontrol/models"
"github.com/StackExchange/dnscontrol/pkg/spflib"
@@ -40,7 +40,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{fmt.Errorf("Split format `%s` in `%s` is not proper format (should have %%d in it)", split, txt.NameFQDN)})
errs = append(errs, Warning{errors.Errorf("Split format `%s` in `%s` is not proper format (should have %%d in it)", split, txt.NameFQDN)})
continue
}
recs := rec.TXTSplit(split + "." + domain.Name)
@@ -63,7 +63,7 @@ func flattenSPFs(cfg *models.DNSConfig) []error {
}
// check if cache is stale
for _, e := range cache.ResolveErrors() {
errs = append(errs, Warning{fmt.Errorf("problem resolving SPF record: %s", e)})
errs = append(errs, Warning{errors.Errorf("problem resolving SPF record: %s", e)})
}
if len(cache.ResolveErrors()) == 0 {
changed := cache.ChangedRecords()
@@ -71,7 +71,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{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 spfcache.json", len(changed), strings.Join(changed, ","))})
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 spfcache.json", len(changed), strings.Join(changed, ","))})
}
}
}

View File

@@ -1,7 +1,6 @@
package normalize
import (
"fmt"
"net"
"strings"
@@ -16,7 +15,7 @@ import (
// Returns false if target does not validate.
func checkIPv4(label string) error {
if net.ParseIP(label).To4() == nil {
return fmt.Errorf("WARNING: target (%v) is not an IPv4 address", label)
return errors.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 fmt.Errorf("WARNING: target (%v) is not an IPv6 address", label)
return errors.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 fmt.Errorf("empty target")
return errors.Errorf("empty target")
}
if strings.ContainsAny(target, `'" +,|!£$%&/()=?^*ç°§;:<>[]()@`) {
return errors.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 fmt.Errorf("target (%v) must end with a (.) [https://stackexchange.github.io/dnscontrol/why-the-dot]", target)
return errors.Errorf("target (%v) must end with a (.) [https://stackexchange.github.io/dnscontrol/why-the-dot]", target)
}
return nil
}
@@ -67,11 +66,11 @@ func validateRecordTypes(rec *models.RecordConfig, domain string, pTypes []strin
if !ok {
cType := providers.GetCustomRecordType(rec.Type)
if cType == nil {
return fmt.Errorf("Unsupported record type (%v) domain=%v name=%v", rec.Type, domain, rec.Name)
return errors.Errorf("Unsupported record type (%v) domain=%v name=%v", rec.Type, domain, rec.Name)
}
for _, providerType := range pTypes {
if providerType != cType.Provider {
return fmt.Errorf("Custom record type %s is not compatible with provider type %s", rec.Type, providerType)
return errors.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
@@ -95,14 +94,14 @@ func checkLabel(label string, rType string, domain string, meta map[string]strin
return nil
}
if len(label) < 1 {
return fmt.Errorf("empty %s label in %s", rType, domain)
return errors.Errorf("empty %s label in %s", rType, domain)
}
if label[len(label)-1] == '.' {
return fmt.Errorf("label %s.%s ends with a (.)", label, domain)
return errors.Errorf("label %s.%s ends with a (.)", label, domain)
}
if strings.HasSuffix(label, domain) {
if m := meta["skip_fqdn_check"]; m != "true" {
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)
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)
}
}
// check for underscores last
@@ -118,7 +117,7 @@ func checkLabel(label string, rType string, domain string, meta map[string]strin
}
// underscores are warnings
if strings.ContainsRune(label, '_') {
return Warning{fmt.Errorf("label %s.%s contains an underscore", label, domain)}
return Warning{errors.Errorf("label %s.%s contains an underscore", label, domain)}
}
return nil
@@ -130,7 +129,7 @@ func checkTargets(rec *models.RecordConfig, domain string) (errs []error) {
target := rec.Target
check := func(e error) {
if e != nil {
err := fmt.Errorf("In %s %s.%s: %s", rec.Type, rec.Name, domain, e.Error())
err := errors.Errorf("In %s %s.%s: %s", rec.Type, rec.Name, domain, e.Error())
if _, ok := e.(Warning); ok {
err = Warning{err}
}
@@ -145,14 +144,14 @@ func checkTargets(rec *models.RecordConfig, domain string) (errs []error) {
case "CNAME":
check(checkTarget(target))
if label == "@" {
check(fmt.Errorf("cannot create CNAME record for bare domain"))
check(errors.Errorf("cannot create CNAME record for bare domain"))
}
case "MX":
check(checkTarget(target))
case "NS":
check(checkTarget(target))
if label == "@" {
check(fmt.Errorf("cannot create NS record for bare domain. Use NAMESERVER instead"))
check(errors.Errorf("cannot create NS record for bare domain. Use NAMESERVER instead"))
}
case "PTR":
check(checkTarget(target))
@@ -166,7 +165,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, fmt.Errorf("checkTargets: Unimplemented record type (%v) domain=%v name=%v",
errs = append(errs, errors.Errorf("checkTargets: Unimplemented record type (%v) domain=%v name=%v",
rec.Type, domain, rec.Name))
}
return
@@ -206,7 +205,7 @@ func importTransform(srcDomain, dstDomain *models.DomainConfig, transforms []tra
case "A":
trs, err := transform.TransformIPToList(net.ParseIP(rec.Target), transforms)
if err != nil {
return fmt.Errorf("import_transform: TransformIP(%v, %v) returned err=%s", rec.Target, transforms, err)
return errors.Errorf("import_transform: TransformIP(%v, %v) returned err=%s", rec.Target, transforms, err)
}
for _, tr := range trs {
r := newRec()
@@ -221,7 +220,7 @@ func importTransform(srcDomain, dstDomain *models.DomainConfig, transforms []tra
// Not imported.
continue
default:
return fmt.Errorf("import_transform: Unimplemented record type %v (%v)",
return errors.Errorf("import_transform: Unimplemented record type %v (%v)",
rec.Type, rec.Name)
}
}
@@ -253,7 +252,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.ProviderHasCabability(pType, providers.CantUseNOPURGE) {
errs = append(errs, fmt.Errorf("%s uses NO_PURGE which is not supported by %s(%s)", domain.Name, provider.Name, pType))
errs = append(errs, errors.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:
@@ -296,26 +295,26 @@ func NormalizeAndValidateConfig(config *models.DNSConfig) (errs []error) {
}
} else if rec.Type == "CAA" {
if rec.CaaTag != "issue" && rec.CaaTag != "issuewild" && rec.CaaTag != "iodef" {
errs = append(errs, fmt.Errorf("CAA tag %s is invalid", rec.CaaTag))
errs = append(errs, errors.Errorf("CAA tag %s is invalid", rec.CaaTag))
}
} else if rec.Type == "TLSA" {
if rec.TlsaUsage < 0 || rec.TlsaUsage > 3 {
errs = append(errs, fmt.Errorf("TLSA Usage %d is invalid in record %s (domain %s)",
errs = append(errs, errors.Errorf("TLSA Usage %d is invalid in record %s (domain %s)",
rec.TlsaUsage, rec.Name, domain.Name))
}
if rec.TlsaSelector < 0 || rec.TlsaSelector > 1 {
errs = append(errs, fmt.Errorf("TLSA Selector %d is invalid in record %s (domain %s)",
errs = append(errs, errors.Errorf("TLSA Selector %d is invalid in record %s (domain %s)",
rec.TlsaSelector, rec.Name, domain.Name))
}
if rec.TlsaMatchingType < 0 || rec.TlsaMatchingType > 2 {
errs = append(errs, fmt.Errorf("TLSA MatchingType %d is invalid in record %s (domain %s)",
errs = append(errs, errors.Errorf("TLSA MatchingType %d is invalid in record %s (domain %s)",
rec.TlsaMatchingType, rec.Name, 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,
fmt.Errorf("TXT records with multiple strings (label %v domain: %v) not supported by %s",
errors.Errorf("TXT records with multiple strings (label %v domain: %v) not supported by %s",
rec.Name, domain.Name, strings.Join(txtMultiDissenters, ",")))
}
@@ -377,14 +376,14 @@ func checkCNAMEs(dc *models.DomainConfig) (errs []error) {
for _, r := range dc.Records {
if r.Type == "CNAME" {
if cnames[r.Name] {
errs = append(errs, fmt.Errorf("Cannot have multiple CNAMEs with same name: %s", r.NameFQDN))
errs = append(errs, errors.Errorf("Cannot have multiple CNAMEs with same name: %s", r.NameFQDN))
}
cnames[r.Name] = true
}
}
for _, r := range dc.Records {
if cnames[r.Name] && r.Type != "CNAME" {
errs = append(errs, fmt.Errorf("Cannot have CNAME and %s record with same name: %s", r.Type, r.NameFQDN))
errs = append(errs, errors.Errorf("Cannot have CNAME and %s record with same name: %s", r.Type, r.NameFQDN))
}
}
return
@@ -414,7 +413,7 @@ func checkProviderCapabilities(dc *models.DomainConfig) error {
}
for _, provider := range dc.DNSProviderInstances {
if !providers.ProviderHasCabability(provider.ProviderType, ty.cap) {
return fmt.Errorf("Domain %s uses %s records, but DNS provider type %s does not support them", dc.Name, ty.rType, provider.ProviderType)
return errors.Errorf("Domain %s uses %s records, but DNS provider type %s does not support them", dc.Name, ty.rType, provider.ProviderType)
}
}
}

View File

@@ -7,6 +7,8 @@ import (
"bytes"
"io"
"github.com/pkg/errors"
)
// SPFRecord stores the parts of an SPF record.
@@ -46,7 +48,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, fmt.Errorf("Not an spf record")
return nil, errors.Errorf("Not an spf record")
}
parts := strings.Split(text, " ")
rec := &SPFRecord{}
@@ -74,11 +76,11 @@ func Parse(text string, dnsres Resolver) (*SPFRecord, error) {
}
p.IncludeRecord, err = Parse(subRecord, dnsres)
if err != nil {
return nil, fmt.Errorf("In included spf: %s", err)
return nil, errors.Errorf("In included spf: %s", err)
}
}
} else {
return nil, fmt.Errorf("Unsupported spf part %s", part)
return nil, errors.Errorf("Unsupported spf part %s", part)
}
}

View File

@@ -2,11 +2,12 @@ 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.
@@ -27,13 +28,13 @@ func (l LiveResolver) GetSPF(name string) (string, error) {
for _, v := range vals {
if strings.HasPrefix(v, "v=spf1") {
if spf != "" {
return "", fmt.Errorf("%s has multiple SPF records", name)
return "", errors.Errorf("%s has multiple SPF records", name)
}
spf = v
}
}
if spf == "" {
return "", fmt.Errorf("%s has no SPF record", name)
return "", errors.Errorf("%s has no SPF record", name)
}
return spf, nil
}

View File

@@ -26,7 +26,7 @@ func ReverseDomainName(cidr string) (string, error) {
bits, total := c.Mask.Size()
var toTrim int
if bits == 0 {
return "", fmt.Errorf("Cannot use /0 in reverse cidr")
return "", errors.Errorf("Cannot use /0 in reverse cidr")
}
// Handle IPv4 "Classless in-addr.arpa delegation" RFC2317:
@@ -41,16 +41,16 @@ func ReverseDomainName(cidr string) (string, error) {
// Handle IPv4 Class-full and IPv6:
if total == 32 {
if bits%8 != 0 {
return "", fmt.Errorf("IPv4 mask must be multiple of 8 bits")
return "", errors.Errorf("IPv4 mask must be multiple of 8 bits")
}
toTrim = (total - bits) / 8
} else if total == 128 {
if bits%4 != 0 {
return "", fmt.Errorf("IPv6 mask must be multiple of 4 bits")
return "", errors.Errorf("IPv6 mask must be multiple of 4 bits")
}
toTrim = (total - bits) / 4
} else {
return "", fmt.Errorf("Address is not IPv4 or IPv6: %v", cidr)
return "", errors.Errorf("Address is not IPv4 or IPv6: %v", cidr)
}
parts := strings.SplitN(base, ".", toTrim+1)

View File

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