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

Commit

Permalink
Merge pull request #77 from drbig/feature/regexp-policy-matcher
Browse files Browse the repository at this point in the history
Feature/regexp policy matcher
  • Loading branch information
nemosupremo authored Aug 29, 2019
2 parents f0bd1ad + 05794b2 commit 4faf5db
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
2 changes: 1 addition & 1 deletion gatekeeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ func (g *Gatekeeper) RenewalWorker(controlChan chan struct{}) {
if err := g.RenewToken(); err == nil {
log.Infof("Renewed Vault Token (original ttl: %v)", ttl)
} else {
log.Warn("Failed to renew Vault token. Is the policy set correctly? Gatekeeper will now be sealed: %v", err)
log.Warnf("Failed to renew Vault token. Is the policy set correctly? Gatekeeper will now be sealed: %v", err)
g.Seal()
return
}
Expand Down
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
9 changes: 9 additions & 0 deletions policy/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ const samplePolicy = `{
"mesos:framework:service/*": {
"roles":["mesos_framework_service"],
"num_uses":1
},
"mesos:marathone:*":{
"roles":["mesos_marathone_taskA"],
"regexp":"\\d{4}\\w{2}\\.taskA",
"num_uses":1
}
}`

Expand Down Expand Up @@ -91,6 +96,10 @@ func TestSamplePolicy(t *testing.T) {
t.Fatalf("Test of '%s' failed. 'task2' should not conatain permission of 'task'. Had: %v", "mesos:framework:task", actual)
}

if pass, _, actual := shouldContainAll(mustGet(pols.Get("mesos:marathone:6668wz.taskA")), "mesos_child", "mesos_marathone_taskA"); pass {
t.Fatalf("Test of '%s' failed. 'task2' should not conatain permission of 'task'. Had: %v", "mesos:framework:task", actual)
}

if policy, ok := pols.Get("mesos:framework:task"); ok {
if policy.Roles[0] != "mesos_framework_task" {
t.Fatalf("Expected most specific role of '%s'. Had: %v", "mesos:framework:task", policy.Roles[0])
Expand Down

0 comments on commit 4faf5db

Please sign in to comment.