mirror of
https://github.com/StackExchange/dnscontrol.git
synced 2024-05-11 05:55:12 +00:00
Refactor in preparation to unexport RecordConfig.{Name,NameFQDN,Target} (#337)
* Preparing for the unexport of Name/NameFQDN/Target * Cleanups
This commit is contained in:
@ -26,9 +26,9 @@ func TestRR(t *testing.T) {
|
||||
experiment := RecordConfig{
|
||||
Type: "A",
|
||||
Name: "foo",
|
||||
NameFQDN: "foo.example.com",
|
||||
Target: "1.2.3.4",
|
||||
TTL: 0,
|
||||
NameFQDN: "foo.example.com",
|
||||
MxPreference: 0,
|
||||
}
|
||||
expected := "foo.example.com.\t300\tIN\tA\t1.2.3.4"
|
||||
@ -40,9 +40,9 @@ func TestRR(t *testing.T) {
|
||||
experiment = RecordConfig{
|
||||
Type: "CAA",
|
||||
Name: "@",
|
||||
NameFQDN: "example.com",
|
||||
Target: "mailto:test@example.com",
|
||||
TTL: 300,
|
||||
NameFQDN: "example.com",
|
||||
CaaTag: "iodef",
|
||||
CaaFlag: 1,
|
||||
}
|
||||
@ -55,9 +55,9 @@ func TestRR(t *testing.T) {
|
||||
experiment = RecordConfig{
|
||||
Type: "TLSA",
|
||||
Name: "@",
|
||||
NameFQDN: "_443._tcp.example.com",
|
||||
Target: "abcdef0123456789",
|
||||
TTL: 300,
|
||||
NameFQDN: "_443._tcp.example.com",
|
||||
TlsaUsage: 0,
|
||||
TlsaSelector: 0,
|
||||
TlsaMatchingType: 1,
|
||||
@ -74,17 +74,17 @@ func TestDowncase(t *testing.T) {
|
||||
&RecordConfig{Type: "MX", Name: "lower", Target: "targetmx"},
|
||||
&RecordConfig{Type: "MX", Name: "UPPER", Target: "TARGETMX"},
|
||||
}}
|
||||
Downcase(dc.Records)
|
||||
downcase(dc.Records)
|
||||
if !dc.HasRecordTypeName("MX", "lower") {
|
||||
t.Errorf("%v: expected (%v) got (%v)\n", dc.Records, false, true)
|
||||
}
|
||||
if !dc.HasRecordTypeName("MX", "upper") {
|
||||
t.Errorf("%v: expected (%v) got (%v)\n", dc.Records, false, true)
|
||||
}
|
||||
if dc.Records[0].Target != "targetmx" {
|
||||
t.Errorf("%v: target0 expected (%v) got (%v)\n", dc.Records, "targetmx", dc.Records[0].Target)
|
||||
if dc.Records[0].GetTargetField() != "targetmx" {
|
||||
t.Errorf("%v: target0 expected (%v) got (%v)\n", dc.Records, "targetmx", dc.Records[0].GetTargetField())
|
||||
}
|
||||
if dc.Records[1].Target != "targetmx" {
|
||||
t.Errorf("%v: target1 expected (%v) got (%v)\n", dc.Records, "targetmx", dc.Records[1].Target)
|
||||
if dc.Records[1].GetTargetField() != "targetmx" {
|
||||
t.Errorf("%v: target1 expected (%v) got (%v)\n", dc.Records, "targetmx", dc.Records[1].GetTargetField())
|
||||
}
|
||||
}
|
||||
|
@ -49,7 +49,7 @@ func (dc *DomainConfig) Copy() (*DomainConfig, error) {
|
||||
// HasRecordTypeName returns True if there is a record with this rtype and name.
|
||||
func (dc *DomainConfig) HasRecordTypeName(rtype, name string) bool {
|
||||
for _, r := range dc.Records {
|
||||
if r.Type == rtype && r.Name == name {
|
||||
if r.Type == rtype && r.GetLabel() == name {
|
||||
return true
|
||||
}
|
||||
}
|
||||
@ -73,19 +73,17 @@ func (dc *DomainConfig) Filter(f func(r *RecordConfig) bool) {
|
||||
// - NameFQDN
|
||||
// - Target (CNAME and MX only)
|
||||
func (dc *DomainConfig) Punycode() error {
|
||||
var err error
|
||||
for _, rec := range dc.Records {
|
||||
rec.Name, err = idna.ToASCII(rec.Name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rec.NameFQDN, err = idna.ToASCII(rec.NameFQDN)
|
||||
t, err := idna.ToASCII(rec.GetLabelFQDN())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rec.SetLabelFromFQDN(t, dc.Name)
|
||||
switch rec.Type { // #rtype_variations
|
||||
case "ALIAS", "MX", "NS", "CNAME", "PTR", "SRV", "URL", "URL301", "FRAME", "R53_ALIAS":
|
||||
rec.Target, err = idna.ToASCII(rec.Target)
|
||||
// These rtypes are hostnames, therefore need to be converted (unlike, for example, an AAAA record)
|
||||
t, err := idna.ToASCII(rec.GetTargetField())
|
||||
rec.SetTarget(t)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -120,6 +120,14 @@ func (rc *RecordConfig) SetLabel(short, origin string) {
|
||||
}
|
||||
}
|
||||
|
||||
// UnsafeSetLabelNull sets the label to "". Normally the FQDN is denoted by .Name being
|
||||
// "@" however this can be used to violate that assertion. It should only be used
|
||||
// on copies of a RecordConfig that is being used for non-standard things like
|
||||
// Marshalling yaml.
|
||||
func (rc *RecordConfig) UnsafeSetLabelNull() {
|
||||
rc.Name = ""
|
||||
}
|
||||
|
||||
// SetLabelFromFQDN sets the .Name/.NameFQDN fields given a FQDN and origin.
|
||||
// fqdn may have a trailing "." but it is not required.
|
||||
// origin may not have a trailing dot.
|
||||
@ -268,11 +276,11 @@ func (r Records) GroupedByLabel() ([]string, map[string]Records) {
|
||||
|
||||
// PostProcessRecords does any post-processing of the downloaded DNS records.
|
||||
func PostProcessRecords(recs []*RecordConfig) {
|
||||
Downcase(recs)
|
||||
downcase(recs)
|
||||
}
|
||||
|
||||
// Downcase converts all labels and targets to lowercase in a list of RecordConfig.
|
||||
func Downcase(recs []*RecordConfig) {
|
||||
func downcase(recs []*RecordConfig) {
|
||||
for _, r := range recs {
|
||||
r.Name = strings.ToLower(r.Name)
|
||||
r.NameFQDN = strings.ToLower(r.NameFQDN)
|
||||
|
@ -11,7 +11,7 @@ import (
|
||||
func (rc *RecordConfig) SetTargetCAA(flag uint8, tag string, target string) error {
|
||||
rc.CaaTag = tag
|
||||
rc.CaaFlag = flag
|
||||
rc.Target = target
|
||||
rc.SetTarget(target)
|
||||
if rc.Type == "" {
|
||||
rc.Type = "CAA"
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ import (
|
||||
// SetTargetMX sets the MX fields.
|
||||
func (rc *RecordConfig) SetTargetMX(pref uint16, target string) error {
|
||||
rc.MxPreference = pref
|
||||
rc.Target = target
|
||||
rc.SetTarget(target)
|
||||
if rc.Type == "" {
|
||||
rc.Type = "MX"
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ func (rc *RecordConfig) SetTargetSRV(priority, weight, port uint16, target strin
|
||||
rc.SrvPriority = priority
|
||||
rc.SrvWeight = weight
|
||||
rc.SrvPort = port
|
||||
rc.Target = target
|
||||
rc.SetTarget(target)
|
||||
if rc.Type == "" {
|
||||
rc.Type = "SRV"
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ func (rc *RecordConfig) SetTargetTLSA(usage, selector, matchingtype uint8, targe
|
||||
rc.TlsaUsage = usage
|
||||
rc.TlsaSelector = selector
|
||||
rc.TlsaMatchingType = matchingtype
|
||||
rc.Target = target
|
||||
rc.SetTarget(target)
|
||||
if rc.Type == "" {
|
||||
rc.Type = "TLSA"
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ package models
|
||||
|
||||
// SetTargetTXT sets the TXT fields when there is 1 string.
|
||||
func (rc *RecordConfig) SetTargetTXT(s string) error {
|
||||
rc.Target = s
|
||||
rc.SetTarget(s)
|
||||
rc.TxtStrings = []string{s}
|
||||
if rc.Type == "" {
|
||||
rc.Type = "TXT"
|
||||
@ -15,7 +15,7 @@ func (rc *RecordConfig) SetTargetTXT(s string) error {
|
||||
|
||||
// SetTargetTXTs sets the TXT fields when there are many strings.
|
||||
func (rc *RecordConfig) SetTargetTXTs(s []string) error {
|
||||
rc.Target = s[0]
|
||||
rc.SetTarget(s[0])
|
||||
rc.TxtStrings = s
|
||||
if rc.Type == "" {
|
||||
rc.Type = "TXT"
|
||||
|
@ -109,11 +109,11 @@ func (rc *RecordConfig) SetTarget(target string) error {
|
||||
// SetTargetIP sets the target to an IP, verifying this is an appropriate rtype.
|
||||
func (rc *RecordConfig) SetTargetIP(ip net.IP) error {
|
||||
// TODO(tlim): Verify the rtype is appropriate for an IP.
|
||||
rc.Target = ip.String()
|
||||
rc.SetTarget(ip.String())
|
||||
return nil
|
||||
}
|
||||
|
||||
// // SetTargetFQDN sets the target to an IP, verifying this is an appropriate rtype.
|
||||
// // SetTargetFQDN sets the target to a string, verifying this is an appropriate rtype.
|
||||
// func (rc *RecordConfig) SetTargetFQDN(target string) error {
|
||||
// // TODO(tlim): Verify the rtype is appropriate for an hostname.
|
||||
// rc.Target = target
|
||||
|
Reference in New Issue
Block a user