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 #64 from angrylogic/enable_wildcard_with_prefix
Browse files Browse the repository at this point in the history
Enable wildcard with prefix.
  • Loading branch information
nemosupremo authored Sep 18, 2018
2 parents c6c460e + 9dd7582 commit 389484f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
9 changes: 8 additions & 1 deletion policy/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Policy struct {
Roles []string `json:"roles"`
NumUses int `json:"num_uses"`
strictestPath []byte
wildcard bool
}

func (p *Policy) merge(path []byte, other Policy) {
Expand Down Expand Up @@ -51,6 +52,9 @@ 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 ':'")
}
Expand Down Expand Up @@ -79,7 +83,10 @@ func (p *Policies) Get(path string) (*Policy, bool) {

walkFn := func(k []byte, _v interface{}) bool {
v := _v.(Policy)
if bytes.Equal(k, []byte(path)) || k[len(k)-1] == ':' {
if v.wildcard && bytes.HasPrefix([]byte(path), k) {
ret.merge(k, v)
foundPolicy = true
} else if bytes.Equal(k, []byte(path)) {
ret.merge(k, v)
foundPolicy = true
}
Expand Down
8 changes: 8 additions & 0 deletions policy/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ const samplePolicy = `{
"mesos:framework:task2":{
"roles":["mesos_framework_task2"],
"num_uses":1
},
"mesos:framework:service/*": {
"roles":["mesos_framework_service"],
"num_uses":1
}
}`

Expand Down Expand Up @@ -79,6 +83,10 @@ func TestSamplePolicy(t *testing.T) {
t.Fatalf("Test of '%s' failed. Expected: %v Had: %v", "mesos:jamp", expected, actual)
}

if pass, expected, actual := shouldContainAll(mustGet(pols.Get("mesos:framework:service/instance-1")), "wildcard", "mesos_child", "mesos_framework_child", "mesos_framework_service"); !pass {
t.Fatalf("Test of '%s' failed. Expected: %v Had: %v", "mesos:framework:service/instance-1", expected, actual)
}

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

0 comments on commit 389484f

Please sign in to comment.