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

Validate TXT prefix #1507

Closed
wants to merge 1 commit into from
Closed
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
28 changes: 26 additions & 2 deletions registry/txt.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ package registry

import (
"context"
"errors"
"fmt"
"regexp"
"strings"
"time"

"github.com/pkg/errors"

log "github.com/sirupsen/logrus"

"sigs.k8s.io/external-dns/endpoint"
Expand Down Expand Up @@ -52,8 +54,30 @@ func NewTXTRegistry(provider provider.Provider, txtPrefix, txtSuffix, ownerID st
return nil, errors.New("txt-prefix and txt-suffix are mutual exclusive")
}

txtPrefix = strings.ToLower(txtPrefix)
txtSuffix = strings.ToLower(txtSuffix)
mapper := newaffixNameMapper(txtPrefix, txtSuffix)

if txtPrefix != "" {
matched, err := regexp.MatchString(`^[a-z0-9-.]+$`, txtPrefix)
if err != nil {
return nil, err
}
if !matched {
return nil, errors.Errorf(`invalid TXT prefix provided, expected "[a-z0-9-.]+" got "%s"`, txtPrefix)
}
}

if txtSuffix != "" {
matched, err := regexp.MatchString(`^[a-z0-9-.]+$`, txtSuffix)
if err != nil {
return nil, err
}
if !matched {
return nil, errors.Errorf(`invalid TXT suffix provided, expected "[a-z0-9-.]+" got "%s"`, txtSuffix)
}
}

return &TXTRegistry{
provider: provider,
ownerID: ownerID,
Expand Down Expand Up @@ -218,7 +242,7 @@ type affixNameMapper struct {
var _ nameMapper = affixNameMapper{}

func newaffixNameMapper(prefix string, suffix string) affixNameMapper {
return affixNameMapper{prefix: strings.ToLower(prefix), suffix: strings.ToLower(suffix)}
return affixNameMapper{prefix: prefix, suffix: suffix}
}

func (pr affixNameMapper) toEndpointName(txtDNSName string) string {
Expand Down
20 changes: 20 additions & 0 deletions registry/txt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,26 @@ func testTXTRegistryNew(t *testing.T) {
_, err = NewTXTRegistry(p, "txt", "txt", "owner", time.Hour)
require.Error(t, err)

_, err = NewTXTRegistry(p, "@invalid#", "", "owner", time.Hour)
require.Error(t, err, `invalid TXT prefix provided, expected "[a-z0-9-.]+" got "@invalid#"`)
Copy link
Contributor

Choose a reason for hiding this comment

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

This message and the one on like 66 are not accurate, I recommend changing it like:

Suggested change
require.Error(t, err, `invalid TXT prefix provided, expected "[a-z0-9-.]+" got "@invalid#"`)
require.Error(t, err)

Copy link
Author

Choose a reason for hiding this comment

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

Why are they not accurate? By comparing the error string I make sure that the guard that returns the error works. If I just accept any error, it would make it less good, right?

Copy link
Contributor

Choose a reason for hiding this comment

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

Because what is happening when you have an error is not that the TXT prefix is invalid, rather than it is expected to be invalid, but there is no error. The string you are providing is only changing the messaging to report, not changing any behavior at all. The default message instead, is correct.

Copy link
Member

Choose a reason for hiding this comment

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

@ruudk any thoughts on the suggestion from @Raffo

_, err = NewTXTRegistry(p, "", "@invalid#", "owner", time.Hour)
require.Error(t, err, `invalid TXT suffix provided, expected "[a-z0-9-.]+" got "@invalid#"`)

Copy link
Contributor

Choose a reason for hiding this comment

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

If I add

	_, err = NewTXTRegistry(p, "valid....", "", "owner", time.Hour)
	require.Error(t, err)

That also looks invalid to me, but the test fails. Are you sure the check that we are doing is fine?

Copy link
Author

Choose a reason for hiding this comment

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

Then we need to improve the regex that is used. Any suggestions?

Copy link
Contributor

Choose a reason for hiding this comment

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

^([a-z0-9]+(-[a-z0-9]+)*)$ maybe?

Copy link
Member

Choose a reason for hiding this comment

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

@ruudk any thoughts on the suggestion from @Raffo

_, err = NewTXTRegistry(p, "txt", "", "owner", time.Hour)
require.NoError(t, err)
_, err = NewTXTRegistry(p, "", "txt", "owner", time.Hour)
require.NoError(t, err)

_, err = NewTXTRegistry(p, "TxT", "", "owner", time.Hour)
require.NoError(t, err)
_, err = NewTXTRegistry(p, "", "TxT", "owner", time.Hour)
require.NoError(t, err)

r, err = NewTXTRegistry(p, "-sub-.", "", "owner", time.Hour)
require.NoError(t, err)
r, err = NewTXTRegistry(p, "", "-sub-.", "owner", time.Hour)
require.NoError(t, err)

_, ok := r.mapper.(affixNameMapper)
require.True(t, ok)
assert.Equal(t, "owner", r.ownerID)
Expand Down