Skip to content

Commit

Permalink
assume /32 prefix if not passed
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronhurt authored and magiconair committed Feb 18, 2018
1 parent c7b06ff commit b88044e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
13 changes: 11 additions & 2 deletions route/access_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func (t *Target) denyByIP(ip net.IP) bool {

func (t *Target) parseAccessRule(allowDeny string) error {
var accessTag string
var value string
var temps []string

// init rules if needed
Expand All @@ -114,10 +115,17 @@ func (t *Target) parseAccessRule(allowDeny string) error {
if temps = strings.SplitN(c, ":", 2); len(temps) != 2 {
return fmt.Errorf("invalid access item, expected <type>:<data>, got %s", temps)
}
accessTag = allowDeny + ":" + strings.ToLower(temps[0])

// form access type tag
accessTag = allowDeny + ":" + strings.ToLower(strings.TrimSpace(temps[0]))

// switch on formed access tag - currently only ip types are implemented
switch accessTag {
case ipAllowTag, ipDenyTag:
_, net, err := net.ParseCIDR(strings.TrimSpace(temps[1]))
if value = strings.TrimSpace(temps[1]); !strings.Contains(value, "/") {
value = value + "/32"
}
_, net, err := net.ParseCIDR(value)
if err != nil {
return fmt.Errorf("failed to parse CIDR %s with error: %s",
c, err.Error())
Expand All @@ -128,6 +136,7 @@ func (t *Target) parseAccessRule(allowDeny string) error {
return fmt.Errorf("unknown access item type: %s", temps[0])
}
}

return nil
}

Expand Down
5 changes: 5 additions & 0 deletions route/access_rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ func TestAccessRules_parseAccessRule(t *testing.T) {
allowDeny: "ip:10.0.0.0/255",
fail: true,
},
{
desc: "single ip with no mask",
allowDeny: "ip:1.2.3.4",
fail: false,
},
}

for i, tt := range tests {
Expand Down

0 comments on commit b88044e

Please sign in to comment.