Skip to content
This repository has been archived by the owner on Jan 8, 2024. It is now read-only.

Commit

Permalink
Rework prefix matching to regexp matching
Browse files Browse the repository at this point in the history
Should preserve existing behaviour.
  • Loading branch information
drbig committed Aug 27, 2019
1 parent eddb64c commit 461cfc1
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions policy/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"errors"
"regexp"
"strings"

log "github.com/sirupsen/logrus"
Expand All @@ -13,9 +14,10 @@ import (

type Policy struct {
Roles []string `json:"roles"`
Regexp string `json:"regexp,omitempty"`
NumUses int `json:"num_uses"`
strictestPath []byte
wildcard bool
regexp *regexp.Regexp
}

func (p *Policy) merge(path []byte, other Policy) {
Expand Down Expand Up @@ -54,18 +56,26 @@ func LoadPoliciesFromJson(data []byte) (*Policies, error) {
tree := iradix.New()
txn := tree.Txn()
for k, v := range pol {
if strings.HasSuffix(k, "*") {
v.wildcard = true
}
if strings.HasSuffix(k, ":") {
return nil, errors.New("Invalid key name '" + k + "'. Keys must not end with a ':'")
}
if v.NumUses < 1 {
return nil, errors.New("Invalid num_uses for key '" + k + "'.")
}
wildcard := false
if k != "*" {
wildcard = strings.HasSuffix(k, "*")
k = strings.TrimSuffix(k, "*")
}
if wildcard {
v.Regexp = k + v.Regexp
}
if v.Regexp != "" {
v.regexp, err = regexp.Compile(v.Regexp)
if err != nil {
return nil, errors.New("Invalid regexp for key '" + k + "'.")
}
}
txn.Insert([]byte(k), v)
}
tree = txn.Commit()
Expand All @@ -85,12 +95,14 @@ func (p *Policies) Get(path string) (*Policy, bool) {

walkFn := func(k []byte, _v interface{}) bool {
v := _v.(Policy)
if v.wildcard && bytes.HasPrefix([]byte(path), k) {
ret.merge(k, v)
foundPolicy = true
} else if bytes.Equal(k, []byte(path)) {
if bytes.Equal(k, []byte(path)) {
ret.merge(k, v)
foundPolicy = true
} else if v.regexp != nil {
if v.regexp.MatchString(path) {
ret.merge(k, v)
foundPolicy = true
}
}

return false
Expand Down

0 comments on commit 461cfc1

Please sign in to comment.