Skip to content

Commit

Permalink
all: fix golint and go vet issues (#34)
Browse files Browse the repository at this point in the history
Signed-off-by: Long Hoang <long@mindworker.de>
  • Loading branch information
loong authored and arekkas committed Sep 23, 2016
1 parent 739758c commit f3f8e52
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 12 deletions.
2 changes: 1 addition & 1 deletion condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ type jsonCondition struct {
Options json.RawMessage `json:"options"`
}

// You can add custom conditions to ConditionFactories
// ConditionFactories is where you can add custom conditions
var ConditionFactories = map[string]func() Condition{
new(StringEqualCondition).GetName(): func() Condition {
return new(StringEqualCondition)
Expand Down
7 changes: 4 additions & 3 deletions condition_string_equal.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package ladon

// SubjectIsOwnerCondition is a condition which is fulfilled if the subject of a warden request is also the owner
// of the requested resource.
// StringEqualCondition is a condition which is fulfilled if the given
// string value is the same as specified in StringEqualCondition
type StringEqualCondition struct {
Equals string `json:"equals"`
}

// Fulfills returns true if the the request is fulfilled by the condition.
// Fulfills returns true if the given value is a string and is the
// same as in StringEqualCondition.Equals
func (c *StringEqualCondition) Fulfills(value interface{}, _ *Request) bool {
s, ok := value.(string)

Expand Down
5 changes: 2 additions & 3 deletions condition_subject_equal.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package ladon

// SubjectIsOwnerCondition is a condition which is fulfilled if the subject of a warden request is also the owner
// of the requested resource.
// EqualsSubjectCondition is a condition which is fulfilled if the request's subject is equal to the given value string
type EqualsSubjectCondition struct{}

// Fulfills returns true if the the request is fulfilled by the condition.
// Fulfills returns true if the request's subject is equal to the given value string
func (c *EqualsSubjectCondition) Fulfills(value interface{}, r *Request) bool {
s, ok := value.(string)

Expand Down
2 changes: 1 addition & 1 deletion ladon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func TestLadon(t *testing.T) {
err := warden.IsAllowed(c.r)
assert.Equal(t, c.expectErr, err != nil, "Failed (%d) %s", k, c.d)
if err != nil {
t.Logf("Error (%d) %s: %s", err.Error(), k, c.d)
t.Logf("Error (%s) %d: %s", err.Error(), k, c.d)
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions manager_memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@ import (
"github.com/go-errors/errors"
)

// Manager is a in-memory implementation of Manager.
// MemoryManager is an in-memory (non-persistent) implementation of Manager.
type MemoryManager struct {
Policies map[string]Policy
sync.RWMutex
}

// NewMemoryManager constructs and initalizes new MemoryManager with no policies
func NewMemoryManager() *MemoryManager {
return &MemoryManager{
Policies: map[string]Policy{},
}
}

// Create a new pollicy to MemoryManager
func (m *MemoryManager) Create(policy Policy) error {
m.Lock()
defer m.Unlock()
Expand Down Expand Up @@ -49,7 +51,7 @@ func (m *MemoryManager) Delete(id string) error {
return nil
}

// Finds all policies associated with the subject.
// FindPoliciesForSubject finds all policies associated with the subject.
func (m *MemoryManager) FindPoliciesForSubject(subject string) (Policies, error) {
m.RLock()
defer m.RUnlock()
Expand Down
8 changes: 7 additions & 1 deletion manager_postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,17 @@ var schemas = []string{
)`,
}

// Manager is a postgres implementation of Manager.
// PostgresManager is a postgres implementation for Manager to store policies persistently
type PostgresManager struct {
db *sql.DB
}

// NewPostgresManager initializes a new PostgresManager for given db instance
func NewPostgresManager(db *sql.DB) *PostgresManager {
return &PostgresManager{db}
}

// CreateSchemas creates ladon_policy tables
func (s *PostgresManager) CreateSchemas() error {
for _, query := range schemas {
if _, err := s.db.Exec(query); err != nil {
Expand All @@ -59,6 +61,7 @@ func (s *PostgresManager) CreateSchemas() error {
return nil
}

// Create inserts a new policy
func (s *PostgresManager) Create(policy Policy) (err error) {
conditions := []byte("[]")
if policy.GetConditions() != nil {
Expand Down Expand Up @@ -89,6 +92,7 @@ func (s *PostgresManager) Create(policy Policy) (err error) {
return nil
}

// Get retrieves a policy.
func (s *PostgresManager) Get(id string) (Policy, error) {
var p DefaultPolicy
var conditions []byte
Expand Down Expand Up @@ -122,11 +126,13 @@ func (s *PostgresManager) Get(id string) (Policy, error) {
return &p, nil
}

// Delete removes a policy.
func (s *PostgresManager) Delete(id string) error {
_, err := s.db.Exec("DELETE FROM ladon_policy WHERE id=$1", id)
return err
}

// FindPoliciesForSubject returns Policies (an array of policy) for a given subject
func (s *PostgresManager) FindPoliciesForSubject(subject string) (policies Policies, err error) {
find := func(query string, args ...interface{}) (ids []string, err error) {
rows, err := s.db.Query(query, args...)
Expand Down
7 changes: 6 additions & 1 deletion manager_rethink.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func rdbFromPolicy(p Policy) (*rdbSchema, error) {
}, err
}

// RethinkManager is a rethinkdb implementation of Manager to store policies persistently
type RethinkManager struct {
Session *r.Session
Table r.Term
Expand All @@ -70,6 +71,7 @@ type RethinkManager struct {
Policies map[string]Policy
}

// ColdStart loads all policies from rethinkdb without warming up the cache
func (m *RethinkManager) ColdStart() error {
m.Policies = map[string]Policy{}
policies, err := m.Table.Run(m.Session)
Expand All @@ -91,6 +93,7 @@ func (m *RethinkManager) ColdStart() error {
return nil
}

// Create inserts a new policy
func (m *RethinkManager) Create(policy Policy) error {
if err := m.publishCreate(policy); err != nil {
return err
Expand Down Expand Up @@ -121,7 +124,7 @@ func (m *RethinkManager) Delete(id string) error {
return nil
}

// Finds all policies associated with the subject.
// FindPoliciesForSubject returns Policies (an array of policy) for a given subject
func (m *RethinkManager) FindPoliciesForSubject(subject string) (Policies, error) {
m.RLock()
defer m.RUnlock()
Expand Down Expand Up @@ -173,6 +176,8 @@ func (m *RethinkManager) publishDelete(id string) error {
return nil
}

// Watch is used to watch for changes on rethinkdb (which happens
// asynchronous) and updates manager's policy accordingly
func (m *RethinkManager) Watch(ctx context.Context) {
go retry(time.Second*15, time.Minute, func() error {
policies, err := m.Table.Changes().Run(m.Session)
Expand Down
1 change: 1 addition & 0 deletions policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type DefaultPolicy struct {
Conditions Conditions `json:"conditions" gorethink:"conditions"`
}

// UnmarshalJSON overwrite own policy with values of the given in policy in JSON format
func (p *DefaultPolicy) UnmarshalJSON(data []byte) error {
var pol = struct {
ID string `json:"id" gorethink:"id"`
Expand Down

0 comments on commit f3f8e52

Please sign in to comment.