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

New feature: IGNORE_TARGET. Rename INGORE to IGNORE_NAME (#806)

This commit is contained in:
Mike Cochrane
2020-08-19 03:14:34 +12:00
committed by GitHub
parent f88c60a8f3
commit 960dc66bd2
13 changed files with 397 additions and 202 deletions

View File

@ -35,8 +35,11 @@ func New(dc *models.DomainConfig, extraValues ...func(*models.RecordConfig) map[
dc: dc,
extraValues: extraValues,
// compile IGNORE glob patterns
compiledIgnoredLabels: compileIgnoredLabels(dc.IgnoredLabels),
// compile IGNORE_NAME glob patterns
compiledIgnoredNames: compileIgnoredNames(dc.IgnoredNames),
// compile IGNORE_TARGET glob patterns
compiledIgnoredTargets: compileIgnoredTargets(dc.IgnoredTargets),
}
}
@ -44,7 +47,8 @@ type differ struct {
dc *models.DomainConfig
extraValues []func(*models.RecordConfig) map[string]string
compiledIgnoredLabels []glob.Glob
compiledIgnoredNames []glob.Glob
compiledIgnoredTargets []glob.Glob
}
// get normalized content for record. target, ttl, mxprio, and specified metadata
@ -93,16 +97,20 @@ func (d *differ) IncrementalDiff(existing []*models.RecordConfig) (unchanged, cr
existingByNameAndType := map[models.RecordKey][]*models.RecordConfig{}
desiredByNameAndType := map[models.RecordKey][]*models.RecordConfig{}
for _, e := range existing {
if d.matchIgnored(e.GetLabel()) {
printer.Debugf("Ignoring record %s %s due to IGNORE\n", e.GetLabel(), e.Type)
if d.matchIgnoredName(e.GetLabel()) {
printer.Debugf("Ignoring record %s %s due to IGNORE_NAME\n", e.GetLabel(), e.Type)
} else if d.matchIgnoredTarget(e.GetTargetField(), e.Type) {
printer.Debugf("Ignoring record %s %s due to IGNORE_TARGET\n", e.GetLabel(), e.Type)
} else {
k := e.Key()
existingByNameAndType[k] = append(existingByNameAndType[k], e)
}
}
for _, dr := range desired {
if d.matchIgnored(dr.GetLabel()) {
panic(fmt.Sprintf("Trying to update/add IGNOREd record: %s %s", dr.GetLabel(), dr.Type))
if d.matchIgnoredName(dr.GetLabel()) {
panic(fmt.Sprintf("Trying to update/add IGNORE_NAMEd record: %s %s", dr.GetLabel(), dr.Type))
} else if d.matchIgnoredTarget(dr.GetTargetField(), dr.Type) {
panic(fmt.Sprintf("Trying to update/add IGNORE_TARGETd record: %s %s", dr.GetLabel(), dr.Type))
} else {
k := dr.Key()
desiredByNameAndType[k] = append(desiredByNameAndType[k], dr)
@ -312,13 +320,13 @@ func sortedKeys(m map[string]*models.RecordConfig) []string {
return s
}
func compileIgnoredLabels(ignoredLabels []string) []glob.Glob {
result := make([]glob.Glob, 0, len(ignoredLabels))
func compileIgnoredNames(ignoredNames []string) []glob.Glob {
result := make([]glob.Glob, 0, len(ignoredNames))
for _, tst := range ignoredLabels {
for _, tst := range ignoredNames {
g, err := glob.Compile(tst, '.')
if err != nil {
panic(fmt.Sprintf("Failed to compile IGNORE pattern %q: %v", tst, err))
panic(fmt.Sprintf("Failed to compile IGNORE_NAME pattern %q: %v", tst, err))
}
result = append(result, g)
@ -327,11 +335,46 @@ func compileIgnoredLabels(ignoredLabels []string) []glob.Glob {
return result
}
func (d *differ) matchIgnored(name string) bool {
for _, tst := range d.compiledIgnoredLabels {
func compileIgnoredTargets(ignoredTargets []*models.IgnoreTarget) []glob.Glob {
result := make([]glob.Glob, 0, len(ignoredTargets))
for _, tst := range ignoredTargets {
fmt.Sprintf("rType for IGNORE_TARGET %v", tst.Type)
if tst.Type != "CNAME" {
panic(fmt.Sprintf("Invalid rType for IGNORE_TARGET %v", tst.Type))
}
g, err := glob.Compile(tst.Pattern, '.')
if err != nil {
panic(fmt.Sprintf("Failed to compile IGNORE_TARGET pattern %q: %v", tst, err))
}
result = append(result, g)
}
return result
}
func (d *differ) matchIgnoredName(name string) bool {
for _, tst := range d.compiledIgnoredNames {
if tst.Match(name) {
return true
}
}
return false
}
func (d *differ) matchIgnoredTarget(target string, rType string) bool {
if rType != "CNAME" {
return false
}
for _, tst := range d.compiledIgnoredTargets {
if tst.Match(target) {
return true
}
}
return false
}

View File

@ -158,15 +158,16 @@ func checkLengths(t *testing.T, existing, desired []*models.RecordConfig, unCoun
}
func checkLengthsWithKeepUnknown(t *testing.T, existing, desired []*models.RecordConfig, unCount, createCount, delCount, modCount int, keepUnknown bool, valFuncs ...func(*models.RecordConfig) map[string]string) (un, cre, del, mod Changeset) {
return checkLengthsFull(t, existing, desired, unCount, createCount, delCount, modCount, keepUnknown, []string{}, valFuncs...)
return checkLengthsFull(t, existing, desired, unCount, createCount, delCount, modCount, keepUnknown, []string{}, nil, valFuncs...)
}
func checkLengthsFull(t *testing.T, existing, desired []*models.RecordConfig, unCount, createCount, delCount, modCount int, keepUnknown bool, ignoredRecords []string, valFuncs ...func(*models.RecordConfig) map[string]string) (un, cre, del, mod Changeset) {
func checkLengthsFull(t *testing.T, existing, desired []*models.RecordConfig, unCount, createCount, delCount, modCount int, keepUnknown bool, ignoredRecords []string, ignoredTargets []*models.IgnoreTarget, valFuncs ...func(*models.RecordConfig) map[string]string) (un, cre, del, mod Changeset) {
dc := &models.DomainConfig{
Name: "example.com",
Records: desired,
KeepUnknown: keepUnknown,
IgnoredLabels: ignoredRecords,
Name: "example.com",
Records: desired,
KeepUnknown: keepUnknown,
IgnoredNames: ignoredRecords,
IgnoredTargets: ignoredTargets,
}
d := New(dc, valFuncs...)
un, cre, del, mod = d.IncrementalDiff(existing)
@ -209,7 +210,7 @@ func TestIgnoredRecords(t *testing.T) {
desired := []*models.RecordConfig{
myRecord("www3 MX 1 2.2.2.2"),
}
checkLengthsFull(t, existing, desired, 0, 0, 0, 1, false, []string{"www1", "www2"})
checkLengthsFull(t, existing, desired, 0, 0, 0, 1, false, []string{"www1", "www2"}, nil)
}
func TestModifyingIgnoredRecords(t *testing.T) {
@ -228,10 +229,10 @@ func TestModifyingIgnoredRecords(t *testing.T) {
}
}()
checkLengthsFull(t, existing, desired, 0, 0, 0, 1, false, []string{"www1", "www2"})
checkLengthsFull(t, existing, desired, 0, 0, 0, 1, false, []string{"www1", "www2"}, nil)
}
func TestGlobIgnoredRecords(t *testing.T) {
func TestGlobIgnoredName(t *testing.T) {
existing := []*models.RecordConfig{
myRecord("www1 MX 1 1.1.1.1"),
myRecord("foo.www2 MX 1 1.1.1.1"),
@ -241,10 +242,10 @@ func TestGlobIgnoredRecords(t *testing.T) {
desired := []*models.RecordConfig{
myRecord("www4 MX 1 2.2.2.2"),
}
checkLengthsFull(t, existing, desired, 0, 0, 0, 1, false, []string{"www1", "*.www2", "**.www3"})
checkLengthsFull(t, existing, desired, 0, 0, 0, 1, false, []string{"www1", "*.www2", "**.www3"}, nil)
}
func TestInvalidGlobIgnoredRecord(t *testing.T) {
func TestInvalidGlobIgnoredName(t *testing.T) {
existing := []*models.RecordConfig{
myRecord("www1 MX 1 1.1.1.1"),
myRecord("www2 MX 1 1.1.1.1"),
@ -256,11 +257,64 @@ func TestInvalidGlobIgnoredRecord(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("should panic: invalid glob pattern for IGNORE")
t.Errorf("should panic: invalid glob pattern for IGNORE_NAME")
}
}()
checkLengthsFull(t, existing, desired, 0, 1, 0, 0, false, []string{"www1", "www2", "[.www3"})
checkLengthsFull(t, existing, desired, 0, 1, 0, 0, false, []string{"www1", "www2", "[.www3"}, nil)
}
func TestGlobIgnoredTarget(t *testing.T) {
existing := []*models.RecordConfig{
myRecord("www1 CNAME 1 ignoreme.com"),
myRecord("foo.www2 MX 1 1.1.1.2"),
myRecord("foo.bar.www3 MX 1 1.1.1.1"),
myRecord("www4 MX 1 1.1.1.1"),
}
desired := []*models.RecordConfig{
myRecord("foo.www2 MX 1 1.1.1.2"),
myRecord("foo.bar.www3 MX 1 1.1.1.1"),
myRecord("www4 MX 1 2.2.2.2"),
}
checkLengthsFull(t, existing, desired, 2, 0, 0, 1, false, nil, []*models.IgnoreTarget{{Pattern: "ignoreme.com", Type: "CNAME"}})
}
func TestInvalidGlobIgnoredTarget(t *testing.T) {
existing := []*models.RecordConfig{
myRecord("www1 MX 1 1.1.1.1"),
myRecord("www2 MX 1 1.1.1.1"),
myRecord("www3 MX 1 1.1.1.1"),
}
desired := []*models.RecordConfig{
myRecord("www4 MX 1 2.2.2.2"),
}
defer func() {
if r := recover(); r == nil {
t.Errorf("should panic: invalid glob pattern for IGNORE_TARGET")
}
}()
checkLengthsFull(t, existing, desired, 0, 1, 0, 0, false, nil, []*models.IgnoreTarget{{Pattern: "[.www3", Type: "CNAME"}} )
}
func TestInvalidTypeIgnoredTarget(t *testing.T) {
existing := []*models.RecordConfig{
myRecord("www1 MX 1 1.1.1.1"),
myRecord("www2 MX 1 1.1.1.1"),
myRecord("www3 MX 1 1.1.1.1"),
}
desired := []*models.RecordConfig{
myRecord("www4 MX 1 2.2.2.2"),
}
defer func() {
if r := recover(); r == nil {
t.Errorf("should panic: Invalid rType for IGNORE_TARGET A")
}
}()
checkLengthsFull(t, existing, desired, 0, 1, 0, 0, false, nil, []*models.IgnoreTarget{{Pattern: "1.1.1.1", Type: "A"}} )
}
// from https://github.com/StackExchange/dnscontrol/issues/552
@ -284,12 +338,12 @@ func TestCaas(t *testing.T) {
desired[1].SetTargetCAA(3, "issue", "amazon.com.")
desired[2].SetTargetCAA(3, "issuewild", "letsencrypt.org.")
checkLengthsFull(t, existing, desired, 3, 0, 0, 0, false, nil)
checkLengthsFull(t, existing, desired, 3, 0, 0, 0, false, nil, nil)
// Make sure it passes with a different ordering. Not ok.
desired[2].SetTargetCAA(3, "issue", "letsencrypt.org.")
desired[1].SetTargetCAA(3, "issue", "amazon.com.")
desired[0].SetTargetCAA(3, "issuewild", "letsencrypt.org.")
checkLengthsFull(t, existing, desired, 3, 0, 0, 0, false, nil)
checkLengthsFull(t, existing, desired, 3, 0, 0, 0, false, nil, nil)
}