REFACTOR: .target should always be FQDN (#2421)

This commit is contained in:
Tom Limoncelli 2023-06-02 15:27:43 -04:00 committed by GitHub
parent 8b915a4760
commit f80f94f94b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 15 deletions

View file

@ -1561,22 +1561,22 @@ func makeTests(t *testing.T) []*TestGroup {
testgroup("AZURE_ALIAS_CNAME", testgroup("AZURE_ALIAS_CNAME",
requires(providers.CanUseAzureAlias), requires(providers.CanUseAzureAlias),
tc("create dependent CNAME records", tc("create dependent CNAME records",
cname("foo.cname", "google.com"), cname("foo.cname", "google.com."),
cname("quux.cname", "google2.com"), cname("quux.cname", "google2.com."),
), ),
tc("ALIAS to CNAME record in same zone", tc("ALIAS to CNAME record in same zone",
cname("foo.cname", "google.com"), cname("foo.cname", "google.com."),
cname("quux.cname", "google2.com"), cname("quux.cname", "google2.com."),
azureAlias("bar.cname", "CNAME", "/subscriptions/**subscription-id**/resourceGroups/**resource-group**/providers/Microsoft.Network/dnszones/**current-domain-no-trailing**/CNAME/foo.cname"), azureAlias("bar.cname", "CNAME", "/subscriptions/**subscription-id**/resourceGroups/**resource-group**/providers/Microsoft.Network/dnszones/**current-domain-no-trailing**/CNAME/foo.cname"),
), ),
tc("change aliasCNAME", tc("change aliasCNAME",
cname("foo.cname", "google.com"), cname("foo.cname", "google.com."),
cname("quux.cname", "google2.com"), cname("quux.cname", "google2.com."),
azureAlias("bar.cname", "CNAME", "/subscriptions/**subscription-id**/resourceGroups/**resource-group**/providers/Microsoft.Network/dnszones/**current-domain-no-trailing**/CNAME/quux.cname"), azureAlias("bar.cname", "CNAME", "/subscriptions/**subscription-id**/resourceGroups/**resource-group**/providers/Microsoft.Network/dnszones/**current-domain-no-trailing**/CNAME/quux.cname"),
), ),
tc("change backCNAME", tc("change backCNAME",
cname("foo.cname", "google.com"), cname("foo.cname", "google.com."),
cname("quux.cname", "google2.com"), cname("quux.cname", "google2.com."),
azureAlias("bar.cname", "CNAME", "/subscriptions/**subscription-id**/resourceGroups/**resource-group**/providers/Microsoft.Network/dnszones/**current-domain-no-trailing**/CNAME/foo.cname"), azureAlias("bar.cname", "CNAME", "/subscriptions/**subscription-id**/resourceGroups/**resource-group**/providers/Microsoft.Network/dnszones/**current-domain-no-trailing**/CNAME/foo.cname"),
), ),
), ),

View file

@ -566,14 +566,12 @@ func Downcase(recs []*RecordConfig) {
r.Name = strings.ToLower(r.Name) r.Name = strings.ToLower(r.Name)
r.NameFQDN = strings.ToLower(r.NameFQDN) r.NameFQDN = strings.ToLower(r.NameFQDN)
switch r.Type { // #rtype_variations switch r.Type { // #rtype_variations
case "ANAME", "CNAME", "DS", "MX", "NS", "PTR", "NAPTR", "SRV", "TLSA", "AKAMAICDN": case "AKAMAICDN", "AAAA", "ANAME", "CNAME", "DS", "MX", "NS", "NAPTR", "PTR", "SRV", "TLSA":
// These record types have a target that is case insensitive, so we downcase it. // Target is case insensitive. Downcase it.
r.target = strings.ToLower(r.target) r.target = strings.ToLower(r.target)
case "LOC": // BUGFIX(tlim): isn't ALIAS in the wrong case statement?
// Do nothing to affect case of letters. case "A", "ALIAS", "CAA", "CF_REDIRECT", "CF_TEMP_REDIRECT", "CF_WORKER_ROUTE", "IMPORT_TRANSFORM", "LOC", "SSHFP", "TXT":
case "A", "AAAA", "ALIAS", "CAA", "IMPORT_TRANSFORM", "TXT", "SSHFP", "CF_REDIRECT", "CF_TEMP_REDIRECT", "CF_WORKER_ROUTE": // Do nothing. (IP address or case sensitive target)
// These record types have a target that is case sensitive, or is an IP address. We leave them alone.
// Do nothing.
case "SOA": case "SOA":
if r.target != "DEFAULT_NOT_SET." { if r.target != "DEFAULT_NOT_SET." {
r.target = strings.ToLower(r.target) // .target stores the Ns r.target = strings.ToLower(r.target) // .target stores the Ns
@ -586,3 +584,25 @@ func Downcase(recs []*RecordConfig) {
} }
} }
} }
// CanonicalizeTargets turns Targets into FQDNs
func CanonicalizeTargets(recs []*RecordConfig, origin string) {
for _, r := range recs {
switch r.Type { // #rtype_variations
case "AKAMAICDN", "ANAME", "CNAME", "DS", "MX", "NS", "NAPTR", "PTR", "SRV":
// Target is a hostname that might be a shortname. Turn it into a FQDN.
r.target = dnsutil.AddOrigin(r.target, origin)
case "A", "ALIAS", "CAA", "CF_REDIRECT", "CF_TEMP_REDIRECT", "CF_WORKER_ROUTE", "IMPORT_TRANSFORM", "LOC", "SSHFP", "TLSA", "TXT":
// Do nothing.
case "SOA":
if r.target != "DEFAULT_NOT_SET." {
r.target = dnsutil.AddOrigin(r.target, origin) // .target stores the Ns
}
if r.SoaMbox != "DEFAULT_NOT_SET." {
r.SoaMbox = dnsutil.AddOrigin(r.SoaMbox, origin)
}
default:
// TODO: we'd like to panic here, but custom record types complicate things.
}
}
}

View file

@ -16,6 +16,9 @@ func CorrectZoneRecords(driver models.DNSProvider, dc *models.DomainConfig) ([]*
// downcase // downcase
models.Downcase(existingRecords) models.Downcase(existingRecords)
models.Downcase(dc.Records)
models.CanonicalizeTargets(existingRecords, dc.Name)
models.CanonicalizeTargets(dc.Records, dc.Name)
// Copy dc so that any corrections code that wants to // Copy dc so that any corrections code that wants to
// modify the records may. For example, if the provider only // modify the records may. For example, if the provider only