Skip to content

Commit

Permalink
Remove unnecessary export. Clarify comments. Address golangci-lint
Browse files Browse the repository at this point in the history
warnings.

Closes grafana#972
  • Loading branch information
krashanoff committed Jul 3, 2020
1 parent 9b59100 commit 4493e38
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
2 changes: 1 addition & 1 deletion js/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,7 @@ func TestVUIntegrationBlockHostnamesOption(t *testing.T) {
}

hostnames := lib.HostnameTrie{}
if err := hostnames.Insert("*.io"); !assert.NoError(t, err) {
if insertErr := hostnames.Insert("*.io"); !assert.NoError(t, insertErr) {
return
}
require.NoError(t, r1.SetOptions(lib.Options{
Expand Down
27 changes: 14 additions & 13 deletions lib/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,21 +193,20 @@ func ParseCIDR(s string) (*IPNet, error) {
// for wildcards exclusively at the start of the pattern. Items may only
// be inserted and searched. Internationalized hostnames are valid.
type HostnameTrie struct {
r rune
children []*HostnameTrie
r rune
terminal bool // end of a valid match
}

// describes a valid hostname pattern to block by. Global var to avoid
// compilation penalty each call to ValidHostname.
var validHostnamePattern *regexp.Regexp = regexp.MustCompile("^\\*?(\\pL|[0-9\\.])*")
// Regex description of hostname pattern to enforce blocks by. Global var
// to avoid compilation penalty at runtime.
// Matches against strings composed entirely of letters, numbers, or '.'s
// with an optional wildcard at the start.
var legalHostnamePattern *regexp.Regexp = regexp.MustCompile("^\\*?(\\pL|[0-9\\.])*")

// ValidHostname returns whether the provided hostname pattern
// has an optional wildcard at the start, and is composed entirely
// of letters, numbers, or '.'s.
func ValidHostname(s string) error {
if len(validHostnamePattern.FindString(s)) != len(s) {
return fmt.Errorf("invalid hostname pattern %s", s)
func legalHostname(s string) error {
if len(legalHostnamePattern.FindString(s)) != len(s) {
return errors.Errorf("invalid hostname pattern %s", s)
}
return nil
}
Expand Down Expand Up @@ -238,13 +237,14 @@ func (t *HostnameTrie) UnmarshalText(b []byte) error {
return nil
}

// Insert a string into the given HostnameTrie.
// Insert a hostname pattern into the given HostnameTrie. Returns an error
// if hostname pattern is illegal.
func (t *HostnameTrie) Insert(s string) error {
if len(s) == 0 {
return nil
}

if err := ValidHostname(s); err != nil {
if err := legalHostname(s); err != nil {
return err
}

Expand All @@ -256,7 +256,7 @@ func (t *HostnameTrie) Insert(s string) error {
}
}

n := &HostnameTrie{rStr[last], nil, len(rStr) == 1}
n := &HostnameTrie{nil, rStr[last], len(rStr) == 1}
t.children = append(t.children, n)
return n.Insert(string(rStr[:last]))
}
Expand All @@ -272,6 +272,7 @@ func (t *HostnameTrie) Contains(s string) (bool, string) {
return false, ""
}

// recursively traverse HostnameTrie children searching for a match.
func (t *HostnameTrie) childContains(s string, match string) (bool, string) {
if len(s) == 0 {
return false, ""
Expand Down
3 changes: 2 additions & 1 deletion lib/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,8 @@ func TestOptions(t *testing.T) {
})
t.Run("BlockedHostnames", func(t *testing.T) {
hostnames := HostnameTrie{}
hostnames.Insert("*")
err := hostnames.Insert("*")
assert.Nil(t, err)
opts := Options{}.Apply(Options{
BlockedHostnames: &hostnames,
})
Expand Down

0 comments on commit 4493e38

Please sign in to comment.