mirror of
https://github.com/StackExchange/dnscontrol.git
synced 2025-09-11 15:44:55 +08:00
IMPORT_TRANSFORM_SUFFIX: Fix for CNAMEs (#3192)
This commit is contained in:
parent
1fde1332c1
commit
583cba3855
3 changed files with 28 additions and 24 deletions
|
@ -16,10 +16,13 @@ Don't use this feature. It was added for a very specific situation at Stack Over
|
||||||
`IMPORT_TRANSFORM_STRIP` is the same as `IMPORT_TRANSFORM` with an additional parameter: `suffixstrip`.
|
`IMPORT_TRANSFORM_STRIP` is the same as `IMPORT_TRANSFORM` with an additional parameter: `suffixstrip`.
|
||||||
|
|
||||||
When `IMPORT_TRANSFORM_STRIP` is generating the label for new records, it
|
When `IMPORT_TRANSFORM_STRIP` is generating the label for new records, it
|
||||||
checks the label. If the label ends with `suffixstrip`, that suffix is removed.
|
checks the label. If the label ends with `.` + `suffixstrip`, that suffix is removed.
|
||||||
If the label does not end with `suffixstrip`, an error is returned.
|
If the label does not end with `suffixstrip`, an error is returned.
|
||||||
|
|
||||||
|
For CNAMEs, the `suffixstrip` is stripped from the beginning (prefix) of the target domain.
|
||||||
|
|
||||||
For example, if the domain is `com.extra` and the label is `foo.com`,
|
For example, if the domain is `com.extra` and the label is `foo.com`,
|
||||||
`IMPORT_TRANSFORM` would generate a label `foo.com.com.extra`.
|
`IMPORT_TRANSFORM` would generate a label `foo.com.com.extra`.
|
||||||
`IMPORT_TRANSFORM_STRIP(... , '.com')` would generate
|
`IMPORT_TRANSFORM_STRIP(... , '.com')` would generate
|
||||||
the label `foo.com.extra` instead.
|
the label `foo.com.extra` instead.
|
||||||
|
A CNAME's target would be `foo.com.extra`.
|
||||||
|
|
|
@ -9,7 +9,6 @@ import (
|
||||||
"github.com/StackExchange/dnscontrol/v4/models"
|
"github.com/StackExchange/dnscontrol/v4/models"
|
||||||
"github.com/StackExchange/dnscontrol/v4/pkg/transform"
|
"github.com/StackExchange/dnscontrol/v4/pkg/transform"
|
||||||
"github.com/StackExchange/dnscontrol/v4/providers"
|
"github.com/StackExchange/dnscontrol/v4/providers"
|
||||||
"github.com/miekg/dns"
|
|
||||||
"github.com/miekg/dns/dnsutil"
|
"github.com/miekg/dns/dnsutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -239,13 +238,14 @@ func checkTargets(rec *models.RecordConfig, domain string) (errs []error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func transformCNAME(target, oldDomain, newDomain, suffixstrip string) string {
|
func transformCNAME(target, oldDomain, newDomain, suffixstrip string) string {
|
||||||
// Canonicalize. If it isn't a FQDN, add the newDomain.
|
// Canonicalize the target. Add the newDomain minus the suffixstrip.
|
||||||
result := dnsutil.AddOrigin(target, oldDomain)
|
// foo -> foo.oldDomain.newDomain
|
||||||
if dns.IsFqdn(result) {
|
// foo. -> foo.newDomain
|
||||||
result = result[:len(result)-1]
|
nd := strings.TrimPrefix(newDomain, suffixstrip+".")
|
||||||
|
if strings.HasSuffix(target, ".") {
|
||||||
|
return target + nd + "."
|
||||||
}
|
}
|
||||||
result = strings.TrimSuffix(result, suffixstrip)
|
return dnsutil.AddOrigin(target, oldDomain) + "." + nd + "."
|
||||||
return dnsutil.AddOrigin(result, newDomain) + "."
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func newRec(rec *models.RecordConfig, ttl uint32) *models.RecordConfig {
|
func newRec(rec *models.RecordConfig, ttl uint32) *models.RecordConfig {
|
||||||
|
@ -260,6 +260,7 @@ func transformLabel(label, suffixstrip string) (string, error) {
|
||||||
if suffixstrip == "" {
|
if suffixstrip == "" {
|
||||||
return label, nil
|
return label, nil
|
||||||
}
|
}
|
||||||
|
suffixstrip = "." + suffixstrip
|
||||||
if !strings.HasSuffix(label, suffixstrip) {
|
if !strings.HasSuffix(label, suffixstrip) {
|
||||||
return "", fmt.Errorf("label %q does not end with %q", label, suffixstrip)
|
return "", fmt.Errorf("label %q does not end with %q", label, suffixstrip)
|
||||||
}
|
}
|
||||||
|
|
|
@ -186,37 +186,37 @@ func Test_transform_cname_strip(t *testing.T) {
|
||||||
p []string
|
p []string
|
||||||
expected string
|
expected string
|
||||||
}{
|
}{
|
||||||
{[]string{"ai.meta.stackexchange.com.", "stackexchange.com", "com.internal", ".com"},
|
{[]string{"ai.meta.stackexchange.com.", "stackexchange.com", "com.internal", "com"},
|
||||||
"ai.meta.stackexchange.com.internal."},
|
"ai.meta.stackexchange.com.internal."},
|
||||||
{[]string{"askubuntu.com.", "askubuntu.com", "com.internal", ".com"},
|
{[]string{"askubuntu.com.", "askubuntu.com", "com.internal", "com"},
|
||||||
"askubuntu.com.internal."},
|
"askubuntu.com.internal."},
|
||||||
{[]string{"blogoverflow.com.", "stackoverflow.com", "com.internal", ".com"},
|
{[]string{"blogoverflow.com.", "stackoverflow.com", "com.internal", "com"},
|
||||||
"blogoverflow.com.internal."},
|
"blogoverflow.com.internal."},
|
||||||
{[]string{"careers.stackoverflow.com.", "stackoverflow.com", "com.internal", ".com"},
|
{[]string{"careers.stackoverflow.com.", "stackoverflow.com", "com.internal", "com"},
|
||||||
"careers.stackoverflow.com.internal."},
|
"careers.stackoverflow.com.internal."},
|
||||||
{[]string{"chat.stackexchange.com.", "askubuntu.com", "com.internal", ".com"},
|
{[]string{"chat.stackexchange.com.", "askubuntu.com", "com.internal", "com"},
|
||||||
"chat.stackexchange.com.internal."},
|
"chat.stackexchange.com.internal."},
|
||||||
{[]string{"chat.stackexchange.com.", "stackoverflow.com", "com.internal", ".com"},
|
{[]string{"chat.stackexchange.com.", "stackoverflow.com", "com.internal", "com"},
|
||||||
"chat.stackexchange.com.internal."},
|
"chat.stackexchange.com.internal."},
|
||||||
{[]string{"chat.stackexchange.com.", "superuser.com", "com.internal", ".com"},
|
{[]string{"chat.stackexchange.com.", "superuser.com", "com.internal", "com"},
|
||||||
"chat.stackexchange.com.internal."},
|
"chat.stackexchange.com.internal."},
|
||||||
{[]string{"sstatic.net.", "sstatic.net", "net.internal", ".net"},
|
{[]string{"sstatic.net.", "sstatic.net", "net.internal", "net"},
|
||||||
"sstatic.net.internal."},
|
"sstatic.net.internal."},
|
||||||
{[]string{"stackapps.com.", "stackapps.com", "com.internal", ".com"},
|
{[]string{"stackapps.com.", "stackapps.com", "com.internal", "com"},
|
||||||
"stackapps.com.internal."},
|
"stackapps.com.internal."},
|
||||||
{[]string{"stackexchange.com.", "stackexchange.com", "com.internal", ".com"},
|
{[]string{"stackexchange.com.", "stackexchange.com", "com.internal", "com"},
|
||||||
"stackexchange.com.internal."},
|
"stackexchange.com.internal."},
|
||||||
{[]string{"stackoverflow.com.", "stackoverflow.com", "com.internal", ".com"},
|
{[]string{"stackoverflow.com.", "stackoverflow.com", "com.internal", "com"},
|
||||||
"stackoverflow.com.internal."},
|
"stackoverflow.com.internal."},
|
||||||
{[]string{"superuser.com.", "superuser.com", "com.internal", ".com"},
|
{[]string{"superuser.com.", "superuser.com", "com.internal", "com"},
|
||||||
"superuser.com.internal."},
|
"superuser.com.internal."},
|
||||||
{[]string{"teststackoverflow.com.", "teststackoverflow.com", "com.internal", ".com"},
|
{[]string{"teststackoverflow.com.", "teststackoverflow.com", "com.internal", "com"},
|
||||||
"teststackoverflow.com.internal."},
|
"teststackoverflow.com.internal."},
|
||||||
{[]string{"webapps.stackexchange.com.", "stackexchange.com", "com.internal", ".com"},
|
{[]string{"webapps.stackexchange.com.", "stackexchange.com", "com.internal", "com"},
|
||||||
"webapps.stackexchange.com.internal."},
|
"webapps.stackexchange.com.internal."},
|
||||||
//
|
//
|
||||||
{[]string{"sstatic.net.", "sstatic.net", "com.internal", ".com"},
|
{[]string{"sstatic.net.", "sstatic.net", "com.internal", "com"},
|
||||||
"sstatic.net.com.internal."},
|
"sstatic.net.internal."},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
|
|
Loading…
Add table
Reference in a new issue