Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(import_transform_suffix): Fix for CNAMEs #3192

Merged
merged 2 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

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.

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`,
`IMPORT_TRANSFORM` would generate a label `foo.com.com.extra`.
`IMPORT_TRANSFORM_STRIP(... , '.com')` would generate
the label `foo.com.extra` instead.
A CNAME's target would be `foo.com.extra`.
17 changes: 9 additions & 8 deletions pkg/normalize/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/StackExchange/dnscontrol/v4/models"
"github.com/StackExchange/dnscontrol/v4/pkg/transform"
"github.com/StackExchange/dnscontrol/v4/providers"
"github.com/miekg/dns"
"github.com/miekg/dns/dnsutil"
)

Expand Down Expand Up @@ -239,13 +238,14 @@ func checkTargets(rec *models.RecordConfig, domain string) (errs []error) {
}

func transformCNAME(target, oldDomain, newDomain, suffixstrip string) string {
// Canonicalize. If it isn't a FQDN, add the newDomain.
result := dnsutil.AddOrigin(target, oldDomain)
if dns.IsFqdn(result) {
result = result[:len(result)-1]
}
result = strings.TrimSuffix(result, suffixstrip)
return dnsutil.AddOrigin(result, newDomain) + "."
// Canonicalize the target. Add the newDomain minus the suffixstrip.
// foo -> foo.oldDomain.newDomain
// foo. -> foo.newDomain
nd := strings.TrimPrefix(newDomain, suffixstrip+".")
if strings.HasSuffix(target, ".") {
return target + nd + "."
}
return dnsutil.AddOrigin(target, oldDomain) + "." + nd + "."
}

func newRec(rec *models.RecordConfig, ttl uint32) *models.RecordConfig {
Expand All @@ -260,6 +260,7 @@ func transformLabel(label, suffixstrip string) (string, error) {
if suffixstrip == "" {
return label, nil
}
suffixstrip = "." + suffixstrip
if !strings.HasSuffix(label, suffixstrip) {
return "", fmt.Errorf("label %q does not end with %q", label, suffixstrip)
}
Expand Down
32 changes: 16 additions & 16 deletions pkg/normalize/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,37 +186,37 @@ func Test_transform_cname_strip(t *testing.T) {
p []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."},
{[]string{"askubuntu.com.", "askubuntu.com", "com.internal", ".com"},
{[]string{"askubuntu.com.", "askubuntu.com", "com.internal", "com"},
"askubuntu.com.internal."},
{[]string{"blogoverflow.com.", "stackoverflow.com", "com.internal", ".com"},
{[]string{"blogoverflow.com.", "stackoverflow.com", "com.internal", "com"},
"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."},
{[]string{"chat.stackexchange.com.", "askubuntu.com", "com.internal", ".com"},
{[]string{"chat.stackexchange.com.", "askubuntu.com", "com.internal", "com"},
"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."},
{[]string{"chat.stackexchange.com.", "superuser.com", "com.internal", ".com"},
{[]string{"chat.stackexchange.com.", "superuser.com", "com.internal", "com"},
"chat.stackexchange.com.internal."},
{[]string{"sstatic.net.", "sstatic.net", "net.internal", ".net"},
{[]string{"sstatic.net.", "sstatic.net", "net.internal", "net"},
"sstatic.net.internal."},
{[]string{"stackapps.com.", "stackapps.com", "com.internal", ".com"},
{[]string{"stackapps.com.", "stackapps.com", "com.internal", "com"},
"stackapps.com.internal."},
{[]string{"stackexchange.com.", "stackexchange.com", "com.internal", ".com"},
{[]string{"stackexchange.com.", "stackexchange.com", "com.internal", "com"},
"stackexchange.com.internal."},
{[]string{"stackoverflow.com.", "stackoverflow.com", "com.internal", ".com"},
{[]string{"stackoverflow.com.", "stackoverflow.com", "com.internal", "com"},
"stackoverflow.com.internal."},
{[]string{"superuser.com.", "superuser.com", "com.internal", ".com"},
{[]string{"superuser.com.", "superuser.com", "com.internal", "com"},
"superuser.com.internal."},
{[]string{"teststackoverflow.com.", "teststackoverflow.com", "com.internal", ".com"},
{[]string{"teststackoverflow.com.", "teststackoverflow.com", "com.internal", "com"},
"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."},
//
{[]string{"sstatic.net.", "sstatic.net", "com.internal", ".com"},
"sstatic.net.com.internal."},
{[]string{"sstatic.net.", "sstatic.net", "com.internal", "com"},
"sstatic.net.internal."},
}

for _, test := range tests {
Expand Down
Loading