Skip to content
Open
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
16 changes: 13 additions & 3 deletions source/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,23 @@ func (cs *crdSource) Endpoints(ctx context.Context) ([]*endpoint.Endpoint, error
}
isNAPTR := ep.RecordType == endpoint.RecordTypeNAPTR
isTXT := ep.RecordType == endpoint.RecordTypeTXT
isMx := ep.RecordType == endpoint.RecordTypeMX
illegalTarget := false
for _, target := range ep.Targets {
hasDot := strings.HasSuffix(target, ".")
// Skip dot validation for TXT records as they can contain arbitrary text
if !isTXT && ((isNAPTR && !hasDot) || (!isNAPTR && hasDot)) {
illegalTarget = true
break
if !isTXT {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nesting if is not a great idea

consider something like

for _, target := range ep.Targets {
	hasDot := strings.HasSuffix(target, ".")

	// TXT can contain arbitrary text, skip dot validation.
	if isTXT {
		continue
	}

	// Records that require a trailing dot (hostnames).
	requiresDot := isNAPTR || isMx

	if requiresDot && !hasDot {
		illegalTarget = true
		break
	}

	if !requiresDot && hasDot {
		illegalTarget = true
		break
	}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or we could avoid growing list of boolean variables entiretly

illegalTarget := false
for _, target := range ep.Targets {
	hasDot := strings.HasSuffix(target, ".")

	switch ep.RecordType {
	case endpoint.RecordTypeTXT:
		 continue // TXT records allow arbitrary text, skip validation
	case endpoint.RecordTypeNAPTR, endpoint.RecordTypeMX:
		illegalTarget = !hasDot // Must have trailing dot
	default:
		illegalTarget = hasDot // Must NOT have trailing dot
	}

	if illegalTarget {
		break
	}
}

// Allow trailing dots if record is NAPTR OR MX.
// Otherwise, if it's NOT NAPTR and NOT MX, a dot is illegal.
if isNAPTR || isMx {
if !hasDot {
illegalTarget = true
break
}
} else if hasDot {
illegalTarget = true
break
}
}
}
if illegalTarget {
Expand Down
21 changes: 21 additions & 0 deletions source/crd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,27 @@ func testCRDSourceEndpoints(t *testing.T) {
expectEndpoints: false,
expectError: false,
},
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we have test without trailing dot as well

title: "MX Record allowing trailing dot in target",
registeredAPIVersion: apiv1alpha1.GroupVersion.String(),
apiVersion: apiv1alpha1.GroupVersion.String(),
registeredKind: apiv1alpha1.DNSEndpointKind,
kind: apiv1alpha1.DNSEndpointKind,
namespace: "foo",
registeredNamespace: "foo",
labels: map[string]string{"test": "that"},
labelFilter: "test=that",
endpoints: []*endpoint.Endpoint{
{
DNSName: "example.org",
Targets: endpoint.Targets{"example.com."},
RecordType: endpoint.RecordTypeMX,
RecordTTL: 180,
},
},
expectEndpoints: true,
expectError: false,
},
} {
t.Run(ti.title, func(t *testing.T) {
t.Parallel()
Expand Down
Loading