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

Downcase DNS names (#253)

* Downcase DNS names
* Document opinions
This commit is contained in:
Tom Limoncelli
2017-11-07 14:12:17 -08:00
committed by Tom Limoncelli
parent b614501d56
commit e7472f76f3
20 changed files with 235 additions and 3 deletions

View File

@@ -89,12 +89,16 @@ func (r *RecordConfig) String() (content string) {
content = fmt.Sprintf("%s %s %s %d", r.Type, r.NameFQDN, r.Target, r.TTL)
switch r.Type { // #rtype_variations
case "A", "AAAA", "CNAME", "PTR", "TXT":
case "A", "AAAA", "CNAME", "NS", "PTR", "TXT":
// Nothing special.
case "MX":
content += fmt.Sprintf(" priority=%d", r.MxPreference)
content += fmt.Sprintf(" pref=%d", r.MxPreference)
case "SOA":
content = fmt.Sprintf("%s %s %s %d", r.Type, r.Name, r.Target, r.TTL)
case "SRV":
content += fmt.Sprintf(" srvpriority=%d srvweight=%d srvport=%d", r.SrvPriority, r.SrvWeight, r.SrvPort)
case "TLSA":
content += fmt.Sprintf(" tlsausage=%d tlsaselector=%d tlsamatchingtype=%d", r.TlsaUsage, r.TlsaSelector, r.TlsaMatchingType)
case "CAA":
content += fmt.Sprintf(" caatag=%s caaflag=%d", r.CaaTag, r.CaaFlag)
default:
@@ -246,6 +250,25 @@ func (r Records) Grouped() map[RecordKey]Records {
return groups
}
// Downcase converts all labels and targets to lowercase in a list of RecordConfig.
func Downcase(recs []*RecordConfig) {
for _, r := range recs {
r.Name = strings.ToLower(r.Name)
r.NameFQDN = strings.ToLower(r.NameFQDN)
switch r.Type {
case "ANAME", "CNAME", "MX", "NS", "PTR":
r.Target = strings.ToLower(r.Target)
case "A", "AAAA", "ALIAS", "CAA", "IMPORT_TRANSFORM", "SRV", "TLSA", "TXT", "SOA":
// Do nothing.
default:
panic(fmt.Sprintf("Downcase: Unimplemented rtype %v", r.Type))
// We panic so that we quickly find any switch statements
// that have not been updated for a new RR type.
}
}
return
}
type RecordKey struct {
Name string
Type string

View File

@@ -68,3 +68,23 @@ func TestRR(t *testing.T) {
t.Errorf("RR expected (%#v) got (%#v)\n", expected, found)
}
}
func TestDowncase(t *testing.T) {
dc := DomainConfig{Records: Records{
&RecordConfig{Type: "MX", Name: "lower", Target: "targetmx"},
&RecordConfig{Type: "MX", Name: "UPPER", Target: "TARGETMX"},
}}
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[1].Target != "targetmx" {
t.Errorf("%v: target1 expected (%v) got (%v)\n", dc.Records, "targetmx", dc.Records[1].Target)
}
}