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(pdns): add validation for MX and SRV records #4871

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
39 changes: 39 additions & 0 deletions endpoint/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"net/netip"
"sort"
"strconv"
"strings"

log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -396,3 +397,41 @@ func RemoveDuplicates(endpoints []*Endpoint) []*Endpoint {

return result
}

// Check endpoint if is it properly formatted according to RFC standards
func CheckEndpoint(endpoint Endpoint) bool {
switch recordType := endpoint.RecordType; recordType {
case "MX":
for _, target := range endpoint.Targets {
// MX records must have a preference value to indicate priority, e.g. "10 example.com"
// as per https://www.rfc-editor.org/rfc/rfc974.txt
targetParts := strings.Fields(strings.TrimSpace(target))
if len(targetParts) != 2 {
return false
}

preferenceRaw := targetParts[0]
_, err := strconv.ParseInt(preferenceRaw, 10, 32)
if err != nil {
return false
}
}
case "SRV":
for _, target := range endpoint.Targets {
// SRV records must have a priority, weight, and port value, e.g. "10 5 5060 example.com"
// as per https://www.rfc-editor.org/rfc/rfc2782.txt
targetParts := strings.Fields(strings.TrimSpace(target))
if len(targetParts) != 4 {
return false
}

for _, part := range targetParts[:3] {
_, err := strconv.ParseInt(part, 10, 32)
if err != nil {
return false
}
}
}
}
return true
}
97 changes: 97 additions & 0 deletions endpoint/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package endpoint
import (
"reflect"
"testing"

"github.com/stretchr/testify/assert"
)

func TestNewEndpoint(t *testing.T) {
Expand Down Expand Up @@ -441,3 +443,98 @@ func TestDuplicatedEndpointsWithOverlappingZones(t *testing.T) {
})
}
}

func TestPDNScheckEndpoint(t *testing.T) {
tests := []struct {
description string
endpoint Endpoint
expected bool
}{
{
description: "Valid MX record target",
endpoint: Endpoint{
DNSName: "example.com",
RecordType: RecordTypeMX,
Targets: Targets{"10 example.com"},
},
expected: true,
},
{
description: "Valid MX record with multiple targets",
endpoint: Endpoint{
DNSName: "example.com",
RecordType: RecordTypeMX,
Targets: Targets{"10 example.com", "20 backup.example.com"},
},
expected: true,
},
{
description: "MX record with valid and invalid targets",
endpoint: Endpoint{
DNSName: "example.com",
RecordType: RecordTypeMX,
Targets: Targets{"example.com", "backup.example.com"},
},
expected: false,
},
{
description: "Invalid MX record with missing priority value",
endpoint: Endpoint{
DNSName: "example.com",
RecordType: RecordTypeMX,
Targets: Targets{"example.com"},
},
expected: false,
},
{
description: "Invalid MX record with too many arguments",
endpoint: Endpoint{
DNSName: "example.com",
RecordType: RecordTypeMX,
Targets: Targets{"10 example.com abc"},
},
expected: false,
},
{
description: "Invalid MX record with non-integer priority",
endpoint: Endpoint{
DNSName: "example.com",
RecordType: RecordTypeMX,
Targets: Targets{"abc example.com"},
},
expected: false,
},
{
description: "Valid SRV record target",
endpoint: Endpoint{
DNSName: "_service._tls.example.com",
RecordType: RecordTypeSRV,
Targets: Targets{"10 20 5060 service.example.com"},
},
expected: true,
},
{
description: "Invalid SRV record with missing part",
endpoint: Endpoint{
DNSName: "_service._tls.example.com",
RecordType: RecordTypeSRV,
Targets: Targets{"10 20 5060"},
},
expected: false,
},
{
description: "Invalid SRV record with non-integer part",
endpoint: Endpoint{
DNSName: "_service._tls.example.com",
RecordType: RecordTypeSRV,
Targets: Targets{"10 20 abc service.example.com"},
},
expected: false,
},
}

for _, tt := range tests {
actual := CheckEndpoint(tt.endpoint)
assert.Equal(t, tt.expected, actual)
}
}
13 changes: 13 additions & 0 deletions provider/pdns/pdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,19 @@ func (p *PDNSProvider) Records(ctx context.Context) (endpoints []*endpoint.Endpo
return endpoints, nil
}

// AdjustEndpoints performs checks on the provided endpoints and will skip any potentially failing changes.
func (p *PDNSProvider) AdjustEndpoints(endpoints []*endpoint.Endpoint) ([]*endpoint.Endpoint, error) {
var validEndpoints []*endpoint.Endpoint
for i := 0; i < len(endpoints); i++ {
if !endpoint.CheckEndpoint(*endpoints[i]) {
log.Warnf("Ignoring Endpoint because of invalid %v record formatting: {Target: '%v'}", endpoints[i].RecordType, endpoints[i].Targets)
continue
}
validEndpoints = append(validEndpoints, endpoints[i])
}
return validEndpoints, nil
}

// ApplyChanges takes a list of changes (endpoints) and updates the PDNS server
// by sending the correct HTTP PATCH requests to a matching zone
func (p *PDNSProvider) ApplyChanges(ctx context.Context, changes *plan.Changes) error {
Expand Down
63 changes: 63 additions & 0 deletions provider/pdns/pdns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,24 @@ var (
endpoint.NewEndpointWithTTL("example.com", endpoint.RecordTypeA, endpoint.TTL(300), "8.8.8.8", "8.8.4.4", "4.4.4.4"),
}

endpointsMXRecord = []*endpoint.Endpoint{
endpoint.NewEndpointWithTTL("mail.example.com", endpoint.RecordTypeMX, endpoint.TTL(300), "10 example.com"),
}

endpointsMXRecordInvalidFormatTooManyArgs = []*endpoint.Endpoint{
endpoint.NewEndpointWithTTL("mail.example.com", endpoint.RecordTypeMX, endpoint.TTL(300), "10 example.com abc"),
}

endpointsMultipleMXRecordsWithSingleInvalid = []*endpoint.Endpoint{
endpoint.NewEndpointWithTTL("mail.example.com", endpoint.RecordTypeMX, endpoint.TTL(300), "abc example.com"),
endpoint.NewEndpointWithTTL("mail.example.com", endpoint.RecordTypeMX, endpoint.TTL(300), "20 backup.example.com"),
}

endpointsMultipleInvalidMXRecords = []*endpoint.Endpoint{
endpoint.NewEndpointWithTTL("mail.example.com", endpoint.RecordTypeMX, endpoint.TTL(300), "example.com"),
endpoint.NewEndpointWithTTL("mail.example.com", endpoint.RecordTypeMX, endpoint.TTL(300), "backup.example.com"),
}

endpointsMixedRecords = []*endpoint.Endpoint{
endpoint.NewEndpointWithTTL("cname.example.com", endpoint.RecordTypeCNAME, endpoint.TTL(300), "example.com"),
endpoint.NewEndpointWithTTL("example.com", endpoint.RecordTypeTXT, endpoint.TTL(300), "'would smell as sweet'"),
Expand Down Expand Up @@ -1116,6 +1134,51 @@ func (suite *NewPDNSProviderTestSuite) TestPDNSClientPartitionZones() {
assert.Equal(suite.T(), partitionResultResidualSingleFilter, residualZones)
}

// Validate whether invalid endpoints are removed by AdjustEndpoints
func (suite *NewPDNSProviderTestSuite) TestPDNSAdjustEndpoints() {
// Function definition: AdjustEndpoints(endpoints []*endpoint.Endpoint) []*endpoint.Endpoint

// Create a new provider to run tests against
p := &PDNSProvider{}

tests := []struct {
description string
endpoints []*endpoint.Endpoint
expected []*endpoint.Endpoint
}{
{
description: "Valid MX endpoint is not removed",
endpoints: endpointsMXRecord,
expected: []*endpoint.Endpoint{
endpoint.NewEndpointWithTTL("mail.example.com", endpoint.RecordTypeMX, endpoint.TTL(300), "10 example.com"),
},
},
{
description: "Invalid MX endpoint with too many arguments is removed",
endpoints: endpointsMXRecordInvalidFormatTooManyArgs,
expected: []*endpoint.Endpoint([]*endpoint.Endpoint(nil)),
},
{
description: "Invalid MX endpoint is removed among valid endpoints",
endpoints: endpointsMultipleMXRecordsWithSingleInvalid,
expected: []*endpoint.Endpoint{
endpoint.NewEndpointWithTTL("mail.example.com", endpoint.RecordTypeMX, endpoint.TTL(300), "20 backup.example.com"),
},
},
{
description: "Multiple invalid MX endpoints are removed",
endpoints: endpointsMultipleInvalidMXRecords,
expected: []*endpoint.Endpoint([]*endpoint.Endpoint(nil)),
},
}

for _, tt := range tests {
actual, err := p.AdjustEndpoints(tt.endpoints)
assert.Nil(suite.T(), err)
assert.Equal(suite.T(), tt.expected, actual)
}
}

func TestNewPDNSProviderTestSuite(t *testing.T) {
suite.Run(t, new(NewPDNSProviderTestSuite))
}