Skip to content

Commit

Permalink
Fix domain checks
Browse files Browse the repository at this point in the history
  • Loading branch information
eikehartmann committed Jan 26, 2020
1 parent 5f336c4 commit f95f2ad
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
19 changes: 15 additions & 4 deletions pkg/domains/domains.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@ func init() {
sort.Sort(ByLengthDesc(domains))
}

func Refresh() {
domains = cfg.Cfg.Domains
sort.Sort(ByLengthDesc(domains))
}

// Matches returns one of the domains we're configured for
// TODO return all matches
// Matches return the first match of the
func Matches(s string) string {
for i, v := range domains {
if strings.Contains(s, v) {
if s == v || strings.HasSuffix(s, "." + v) {
log.Debugf("domain %s matched array value at [%d]=%v", s, i, v)
return v
}
Expand All @@ -28,9 +33,15 @@ func Matches(s string) string {
return ""
}

// IsUnderManagement check if string contains a vouch managed domain
func IsUnderManagement(s string) bool {
match := Matches(s)
// IsUnderManagement check if an email is under vouch-managed domain
func IsUnderManagement(email string) bool {
split := strings.Split(email, "@")
if len(split) != 2 {
log.Warnf("not a valid email: %s", email)
return false
}

match := Matches(split[1])
if match != "" {
return true
}
Expand Down
42 changes: 42 additions & 0 deletions pkg/domains/domains_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package domains

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/vouch/vouch-proxy/pkg/cfg"
)

func init() {
cfg.InitForTestPurposes()
cfg.Cfg.Domains = []string{"vouch.github.io", "sub.test.mydomain.com", "test.mydomain.com"}
Refresh()
}

func TestIsUnderManagement(t *testing.T) {
assert.True(t, IsUnderManagement("[email protected]"))
assert.True(t, IsUnderManagement("[email protected]"))
assert.True(t, IsUnderManagement("[email protected]"))
assert.True(t, IsUnderManagement("[email protected]"))

assert.False(t, IsUnderManagement("[email protected]"))
assert.False(t, IsUnderManagement("[email protected]"))
assert.False(t, IsUnderManagement("[email protected]"))
assert.False(t, IsUnderManagement("[email protected]"))
}

func TestMatches(t *testing.T) {
// Full email should not be accepted
assert.Equal(t, "", Matches("[email protected]"))

assert.Equal(t, "vouch.github.io", Matches("vouch.github.io"))
assert.Equal(t, "vouch.github.io", Matches("sub.vouch.github.io"))
assert.Equal(t, "", Matches("a-different-vouch.github.io"))

assert.Equal(t, "", Matches("mydomain.com"))

assert.Equal(t, "test.mydomain.com", Matches("test.mydomain.com"))
assert.Equal(t, "sub.test.mydomain.com", Matches("sub.test.mydomain.com"))
assert.Equal(t, "sub.test.mydomain.com", Matches("subsub.sub.test.mydomain.com"))
assert.Equal(t, "test.mydomain.com", Matches("other.test.mydomain.com"))
}

0 comments on commit f95f2ad

Please sign in to comment.