Skip to content

Commit

Permalink
vendor: add semver to glide (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
arekkas authored Aug 8, 2016
1 parent 8319836 commit 6784572
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 151 deletions.
9 changes: 2 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,10 @@ matrix:
- go: 1.4

install:
- go get github.com/bradfitz/goimports
- go get github.com/axw/gocov/gocov
- go get github.com/mattn/goveralls
- go get golang.org/x/tools/cmd/cover
- go get github.com/pierrre/gotestcover
- go get -t ./...
- go get github.com/mattn/goveralls golang.org/x/tools/cmd/cover github.com/pierrre/gotestcover github.com/Masterminds/glide
- glide install

script:
- goimports -d .
- gotestcover -coverprofile="cover.out" -race -covermode="count" $(go list ./... | grep -v /vendor/)

after_success:
Expand Down
30 changes: 22 additions & 8 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 13 additions & 2 deletions glide.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package: github.com/ory-am/ladon
import:
- package: github.com/dancannon/gorethink
version: ~2.1.3
- package: github.com/Sirupsen/logrus
version: ~0.10.0
- package: github.com/go-errors/errors
- package: github.com/golang/mock
subpackages:
Expand All @@ -10,3 +10,14 @@ import:
subpackages:
- compiler
- pkg
- package: golang.org/x/net
subpackages:
- context
- package: gopkg.in/dancannon/gorethink.v2
version: ~2.1.3
testImport:
- package: github.com/stretchr/testify
version: ~1.1.3
subpackages:
- assert
- require
53 changes: 26 additions & 27 deletions ladon_test.go
Original file line number Diff line number Diff line change
@@ -1,65 +1,64 @@
package ladon_test
package ladon

import (
"testing"

"github.com/ory-am/ladon"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

var pols = []ladon.Policy{
&ladon.DefaultPolicy{
var pols = []Policy{
&DefaultPolicy{
ID: "68819e5a-738b-41ec-b03c-b58a1b19d043",
Description: "something humanly readable",
Subjects: []string{"max", "peter", "<zac|ken>"},
Resources: []string{"myrn:some.domain.com:resource:123", "myrn:some.domain.com:resource:345", "myrn:something:foo:<.+>"},
Actions: []string{"<create|delete>", "get"},
Effect: ladon.AllowAccess,
Conditions: ladon.Conditions{
"owner": &ladon.EqualsSubjectCondition{},
"clientIP": &ladon.CIDRCondition{
Effect: AllowAccess,
Conditions: Conditions{
"owner": &EqualsSubjectCondition{},
"clientIP": &CIDRCondition{
CIDR: "127.0.0.1/32",
},
},
},
&ladon.DefaultPolicy{
&DefaultPolicy{
ID: "38819e5a-738b-41ec-b03c-b58a1b19d041",
Subjects: []string{"max"},
Actions: []string{"update"},
Resources: []string{"<.*>"},
Effect: ladon.AllowAccess,
Effect: AllowAccess,
},
&ladon.DefaultPolicy{
&DefaultPolicy{
ID: "38919e5a-738b-41ec-b03c-b58a1b19d041",
Subjects: []string{"max"},
Actions: []string{"broadcast"},
Resources: []string{"<.*>"},
Effect: ladon.DenyAccess,
Effect: DenyAccess,
},
}

func TestLadon(t *testing.T) {
warden := &ladon.Ladon{
Manager: ladon.NewMemoryManager(),
warden := &Ladon{
Manager: NewMemoryManager(),
}
for _, pol := range pols {
require.Nil(t, warden.Manager.Create(pol))
}

for k, c := range []struct {
d string
r *ladon.Request
r *Request
expectErr bool
}{
{
d: "should fail because client ip mismatch",
r: &ladon.Request{
r: &Request{
Subject: "peter",
Action: "delete",
Resource: "myrn:some.domain.com:resource:123",

Context: ladon.Context{
Context: Context{
"owner": "peter",
"clientIP": "0.0.0.0",
},
Expand All @@ -68,12 +67,12 @@ func TestLadon(t *testing.T) {
},
{
d: "should fail because subject is not owner",
r: &ladon.Request{
r: &Request{
Subject: "peter",
Action: "delete",
Resource: "myrn:some.domain.com:resource:123",

Context: ladon.Context{
Context: Context{
"owner": "zac",
"clientIP": "127.0.0.1",
},
Expand All @@ -82,12 +81,12 @@ func TestLadon(t *testing.T) {
},
{
d: "should pass because policy is satisfied",
r: &ladon.Request{
r: &Request{
Subject: "peter",
Action: "delete",
Resource: "myrn:some.domain.com:resource:123",

Context: ladon.Context{
Context: Context{
"owner": "peter",
"clientIP": "127.0.0.1",
},
Expand All @@ -96,7 +95,7 @@ func TestLadon(t *testing.T) {
},
{
d: "should pass because max is allowed to update all resources",
r: &ladon.Request{
r: &Request{
Subject: "max",
Action: "update",
Resource: "myrn:some.domain.com:resource:123",
Expand All @@ -105,7 +104,7 @@ func TestLadon(t *testing.T) {
},
{
d: "should pass because max is allowed to update all resource, even if none is given",
r: &ladon.Request{
r: &Request{
Subject: "max",
Action: "update",
Resource: "",
Expand All @@ -114,7 +113,7 @@ func TestLadon(t *testing.T) {
},
{
d: "should be rejected",
r: &ladon.Request{
r: &Request{
Subject: "max",
Action: "broadcast",
Resource: "myrn:some.domain.com:resource:123",
Expand All @@ -123,7 +122,7 @@ func TestLadon(t *testing.T) {
},
{
d: "should be rejected",
r: &ladon.Request{
r: &Request{
Subject: "max",
Action: "broadcast",
},
Expand All @@ -140,6 +139,6 @@ func TestLadon(t *testing.T) {
}

func TestLadonEmpty(t *testing.T) {
warden := &ladon.Ladon{Manager: ladon.NewMemoryManager()}
assert.NotNil(t, warden.IsAllowed(&ladon.Request{}))
warden := &Ladon{Manager: NewMemoryManager()}
assert.NotNil(t, warden.IsAllowed(&Request{}))
}
103 changes: 62 additions & 41 deletions manager_rethink.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"encoding/json"
"sync"

"time"

"github.com/Sirupsen/logrus"
"github.com/go-errors/errors"
"golang.org/x/net/context"
r "github.com/dancannon/gorethink"
r "gopkg.in/dancannon/gorethink.v2"
)

// stupid hack
Expand Down Expand Up @@ -75,9 +77,9 @@ func (m *RethinkManager) ColdStart() error {
return errors.New(err)
}

var tbl rdbSchema
m.Lock()
defer m.Unlock()
var tbl rdbSchema
for policies.Next(&tbl) {
policy, err := rdbToPolicy(&tbl)
if err != nil {
Expand Down Expand Up @@ -171,50 +173,69 @@ func (m *RethinkManager) publishDelete(id string) error {
return nil
}

func (m *RethinkManager) Watch(ctx context.Context) error {
policies, err := m.Table.Changes().Run(m.Session)
if err != nil {
return errors.New(err)
}
func (m *RethinkManager) Watch(ctx context.Context) {
go retry(time.Second*15, time.Minute, func() error {
policies, err := m.Table.Changes().Run(m.Session)
if err != nil {
return errors.New(err)
}

go func() {
for {
var update = make(map[string]*rdbSchema)
for policies.Next(&update) {
newVal, err := rdbToPolicy(update["new_val"])
if err != nil {
logrus.Error(err)
continue
}

oldVal, err := rdbToPolicy(update["old_val"])
if err != nil {
logrus.Error(err)
continue
}

m.Lock()
if newVal == nil && oldVal != nil {
delete(m.Policies, oldVal.GetID())
} else if newVal != nil && oldVal != nil {
delete(m.Policies, oldVal.GetID())
m.Policies[newVal.GetID()] = newVal
} else {
m.Policies[newVal.GetID()] = newVal
}
m.Unlock()
defer policies.Close()
var update = make(map[string]*rdbSchema)
for policies.Next(&update) {
logrus.Debug("Received update from RethinkDB Cluster in policy manager.")
newVal, err := rdbToPolicy(update["new_val"])
if err != nil {
logrus.Error(err)
continue
}

policies.Close()
if policies.Err() != nil {
logrus.Error(errors.New(policies.Err()))
oldVal, err := rdbToPolicy(update["old_val"])
if err != nil {
logrus.Error(err)
continue
}

policies, err = m.Table.Changes().Run(m.Session)
if err != nil {
logrus.Error(errors.New(policies.Err()))
m.Lock()
if newVal == nil && oldVal != nil {
delete(m.Policies, oldVal.GetID())
} else if newVal != nil && oldVal != nil {
delete(m.Policies, oldVal.GetID())
m.Policies[newVal.GetID()] = newVal
} else {
m.Policies[newVal.GetID()] = newVal
}
m.Unlock()
}
}()
return nil

if policies.Err() != nil {
logrus.Error(errors.New(policies.Err()))
}
return nil
})
}

func retry(maxWait time.Duration, failAfter time.Duration, f func() error) (err error) {
var lastStart time.Time
err = errors.New("Did not connect.")
loopWait := time.Millisecond * 500
retryStart := time.Now()
for retryStart.Add(failAfter).After(time.Now()) {
lastStart = time.Now()
if err = f(); err == nil {
return nil
}

if lastStart.Add(maxWait * 2).Before(time.Now()) {
retryStart = time.Now()
}

logrus.Infof("Retrying in %f seconds...", loopWait.Seconds())
time.Sleep(loopWait)
loopWait = loopWait * time.Duration(int64(2))
if loopWait > maxWait {
loopWait = maxWait
}
}
return err
}
Loading

0 comments on commit 6784572

Please sign in to comment.