Fix SRV record handling when target is shortname (#422)

* models/record.go: SRV targets are case insensitive. Downcase them.
* models/t_srv.go: Rename setTargetIntAndStrings() to setTargetSRVIntAndStrings() (makes it easier to search for /setTargetSRV/).
* pkg/js/parse_tests/021-srv.js*: SRV: Add parse_tests
* pkg/normalize/validate.go: SRV targets are hostnames, turn into FQDNs.
* Add  #rtype_variations warnings for future developers
This commit is contained in:
Tom Limoncelli 2018-12-07 16:30:04 -05:00 committed by GitHub
parent 292ea28208
commit 4ef9e8bc40
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 72 additions and 8 deletions

View file

@ -292,10 +292,12 @@ 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":
switch r.Type { // #rtype_variations
case "ANAME", "CNAME", "MX", "NS", "PTR", "SRV":
// These record types have a target that is case insensitive, so we downcase it.
r.Target = strings.ToLower(r.Target)
case "A", "AAAA", "ALIAS", "CAA", "IMPORT_TRANSFORM", "SRV", "TLSA", "TXT", "SOA", "CF_REDIRECT", "CF_TEMP_REDIRECT":
case "A", "AAAA", "ALIAS", "CAA", "IMPORT_TRANSFORM", "TLSA", "TXT", "SOA", "CF_REDIRECT", "CF_TEMP_REDIRECT":
// These record types have a target that is case sensitive, or is an IP address. We leave them alone.
// Do nothing.
default:
// TODO: we'd like to panic here, but custom record types complicate things.

View file

@ -22,8 +22,8 @@ func (rc *RecordConfig) SetTargetSRV(priority, weight, port uint16, target strin
return nil
}
// setTargetIntAndStrings is like SetTargetSRV but accepts priority as an int, the other parameters as strings.
func (rc *RecordConfig) setTargetIntAndStrings(priority uint16, weight, port, target string) (err error) {
// setTargetSRVIntAndStrings is like SetTargetSRV but accepts priority as an int, the other parameters as strings.
func (rc *RecordConfig) setTargetSRVIntAndStrings(priority uint16, weight, port, target string) (err error) {
var i64weight, i64port uint64
if i64weight, err = strconv.ParseUint(weight, 10, 16); err == nil {
if i64port, err = strconv.ParseUint(port, 10, 16); err == nil {
@ -37,7 +37,7 @@ func (rc *RecordConfig) setTargetIntAndStrings(priority uint16, weight, port, ta
func (rc *RecordConfig) SetTargetSRVStrings(priority, weight, port, target string) (err error) {
var i64priority uint64
if i64priority, err = strconv.ParseUint(priority, 10, 16); err == nil {
return rc.setTargetIntAndStrings(uint16(i64priority), weight, port, target)
return rc.setTargetSRVIntAndStrings(uint16(i64priority), weight, port, target)
}
return errors.Wrap(err, "SRV value too big for uint16")
}
@ -51,7 +51,7 @@ func (rc *RecordConfig) SetTargetSRVPriorityString(priority uint16, s string) er
if len(part) != 3 {
return errors.Errorf("SRV value does not contain 3 fields: (%#v)", s)
}
return rc.setTargetIntAndStrings(priority, part[0], part[1], part[2])
return rc.setTargetSRVIntAndStrings(priority, part[0], part[1], part[2])
}
// SetTargetSRVString is like SetTargetSRV but accepts one big string to be parsed.

View file

@ -0,0 +1,7 @@
D("foo.com","none"
, SRV('_ntp._udp', 1, 100, 123, 'one.foo.com.')
, SRV('_ntp._udp', 2, 100, 123, 'two')
, SRV('_ntp._udp', 3, 100, 123, 'localhost')
, SRV('_ntp._udp', 4, 100, 123, 'three.example.com.')
, SRV('_ntp._udp', 0, 0, 1, 'zeros')
);

View file

@ -0,0 +1,51 @@
{
"dns_providers": [],
"domains": [
{
"dnsProviders": {},
"name": "foo.com",
"records": [
{
"name": "_ntp._udp",
"srvport": 123,
"srvpriority": 1,
"srvweight": 100,
"target": "one.foo.com.",
"type": "SRV"
},
{
"name": "_ntp._udp",
"srvport": 123,
"srvpriority": 2,
"srvweight": 100,
"target": "two",
"type": "SRV"
},
{
"name": "_ntp._udp",
"srvport": 123,
"srvpriority": 3,
"srvweight": 100,
"target": "localhost",
"type": "SRV"
},
{
"name": "_ntp._udp",
"srvport": 123,
"srvpriority": 4,
"srvweight": 100,
"target": "three.example.com.",
"type": "SRV"
},
{
"name": "_ntp._udp",
"srvport": 1,
"target": "zeros",
"type": "SRV"
}
],
"registrar": "none"
}
],
"registrars": []
}

View file

@ -285,7 +285,11 @@ func NormalizeAndValidateConfig(config *models.DNSConfig) (errs []error) {
}
// Canonicalize Targets.
if rec.Type == "CNAME" || rec.Type == "MX" || rec.Type == "NS" {
if rec.Type == "CNAME" || rec.Type == "MX" || rec.Type == "NS" || rec.Type == "SRV" {
// #rtype_variations
// These record types have a target that is a hostname.
// We normalize them to a FQDN so there is less variation to handle. If a
// provider API requires a shortname, the provider must do the shortening.
rec.SetTarget(dnsutil.AddOrigin(rec.GetTargetField(), domain.Name+"."))
} else if rec.Type == "A" || rec.Type == "AAAA" {
rec.SetTarget(net.ParseIP(rec.GetTargetField()).String())