Documentation ¶
Index ¶
- Constants
- Variables
- func AssertPolicyEqual(t *testing.T, expected, got Policy)
- func Bool(data []byte, err error) (bool, error)
- func Int64(data []byte, err error) (int64, error)
- func JsonQuery(data []byte, query string) ([]byte, error)
- func NewErrResourceNotFound(err error) error
- func String(data []byte, err error) (string, error)
- func TestHelperCreateGetDelete(s Manager) func(t *testing.T)
- func TestHelperFindPoliciesForSubject(k string, s Manager) func(t *testing.T)
- func TestHelperGetErrors(s Manager) func(t *testing.T)
- type BodyArrayMatchCondition
- type BodyMatchCondition
- type CIDRCondition
- type Condition
- type Conditions
- type Context
- type DefaultPolicies
- type DefaultPolicy
- func (p *DefaultPolicy) AllowAccess() bool
- func (p *DefaultPolicy) AttachIdentity(ids ...string) error
- func (p *DefaultPolicy) CheckID() (string, string, error)
- func (p *DefaultPolicy) DetachIdentity(ids ...string) error
- func (p *DefaultPolicy) GetActions() []string
- func (p *DefaultPolicy) GetConditions() Conditions
- func (p *DefaultPolicy) GetDescription() string
- func (p *DefaultPolicy) GetEffect() string
- func (p *DefaultPolicy) GetEndDelimiter() byte
- func (p *DefaultPolicy) GetID() string
- func (p *DefaultPolicy) GetResources() []string
- func (p *DefaultPolicy) GetStartDelimiter() byte
- func (p *DefaultPolicy) GetSubjects() []string
- func (p *DefaultPolicy) HasIdentity(id string) bool
- func (p *DefaultPolicy) UnmarshalJSON(data []byte) error
- func (p *DefaultPolicy) Validate() error
- func (p *DefaultPolicy) ValidateEffect() error
- func (p *DefaultPolicy) ValidateSubjects() error
- type EqualsSubjectCondition
- type Ladon
- type Manager
- type ManagerMigrator
- type Policies
- type Policy
- type RegexpMatcher
- type Request
- type StringEqualCondition
- type StringMatchCondition
- type StringPairsEqualCondition
- type Warden
Constants ¶
const ( Matchall = "all" Matchany = "any" )
const AllowAccess = "allow"
AllowAccess should be used as effect for policies that allow access.
const DenyAccess = "deny"
DenyAccess should be used as effect for policies that deny access.
const (
KeyRawRequest = "http-request"
)
Variables ¶
var ( // ErrRequestDenied is returned when an access request can not be satisfied by any policy. ErrRequestDenied = errors.WithStack(&errorWithContext{ error: errors.New("Request was denied by default"), code: http.StatusForbidden, status: http.StatusText(http.StatusForbidden), reason: "The request was denied because no matching policy was found.", }) // ErrRequestForcefullyDenied is returned when an access request is explicitly denied by a policy. ErrRequestForcefullyDenied = errors.WithStack(&errorWithContext{ error: errors.New("Request was forcefully denied"), code: http.StatusForbidden, status: http.StatusText(http.StatusForbidden), reason: "The request was denied because a policy denied request.", }) // ErrNotFound is returned when a resource can not be found. ErrNotFound = errors.WithStack(&errorWithContext{ error: errors.New("Resource could not be found"), code: http.StatusNotFound, status: http.StatusText(http.StatusNotFound), }) )
var ConditionFactories = map[string]func() Condition{ new(StringEqualCondition).GetName(): func() Condition { return new(StringEqualCondition) }, new(CIDRCondition).GetName(): func() Condition { return new(CIDRCondition) }, new(EqualsSubjectCondition).GetName(): func() Condition { return new(EqualsSubjectCondition) }, new(StringPairsEqualCondition).GetName(): func() Condition { return new(StringPairsEqualCondition) }, new(StringMatchCondition).GetName(): func() Condition { return new(StringMatchCondition) }, new(BodyMatchCondition).GetName(): func() Condition { return new(BodyMatchCondition) }, new(BodyArrayMatchCondition).GetName(): func() Condition { return new(BodyArrayMatchCondition) }, }
ConditionFactories is where you can add custom conditions
var DefaultMatcher = NewRegexpMatcher(512)
var (
ErrPolicyNotFound = errors.New("policy not found")
)
var TestManagerPolicies = []*DefaultPolicy{ { ID: uuid.New(), Description: "description", Subjects: []string{"user", "anonymous"}, Effect: AllowAccess, Resources: []string{"article", "user"}, Actions: []string{"create", "update"}, Conditions: Conditions{}, }, { ID: uuid.New(), Description: "description", Subjects: []string{}, Effect: AllowAccess, Resources: []string{"<article|user>"}, Actions: []string{"view"}, Conditions: Conditions{}, }, { ID: uuid.New(), Description: "description", Subjects: []string{}, Effect: AllowAccess, Resources: []string{}, Actions: []string{"view"}, Conditions: Conditions{}, }, { ID: uuid.New(), Description: "description", Subjects: []string{}, Effect: AllowAccess, Resources: []string{}, Actions: []string{}, Conditions: Conditions{}, }, { ID: uuid.New(), Description: "description", Subjects: []string{}, Effect: AllowAccess, Resources: []string{"foo"}, Actions: []string{}, Conditions: Conditions{}, }, { ID: uuid.New(), Description: "description", Subjects: []string{"foo"}, Effect: AllowAccess, Resources: []string{"foo"}, Actions: []string{}, Conditions: Conditions{}, }, { ID: uuid.New(), Description: "description", Subjects: []string{"foo"}, Effect: AllowAccess, Resources: []string{}, Actions: []string{}, Conditions: Conditions{}, }, { ID: uuid.New(), Description: "description", Effect: AllowAccess, Conditions: Conditions{}, }, { ID: uuid.New(), Description: "description", Subjects: []string{"<peter|max>"}, Effect: DenyAccess, Resources: []string{"article", "user"}, Actions: []string{"view"}, Conditions: Conditions{ "owner": &EqualsSubjectCondition{}, }, }, { ID: uuid.New(), Description: "description", Subjects: []string{"<user|max|anonymous>", "peter"}, Effect: DenyAccess, Resources: []string{".*"}, Actions: []string{"disable"}, Conditions: Conditions{ "ip": &CIDRCondition{ CIDR: "1234", }, "owner": &EqualsSubjectCondition{}, }, }, { ID: uuid.New(), Description: "description", Subjects: []string{"<.*>"}, Effect: AllowAccess, Resources: []string{"<article|user>"}, Actions: []string{"view"}, Conditions: Conditions{ "ip": &CIDRCondition{ CIDR: "1234", }, "owner": &EqualsSubjectCondition{}, }, }, { ID: uuid.New(), Description: "description", Subjects: []string{"<us[er]+>"}, Effect: AllowAccess, Resources: []string{"<article|user>"}, Actions: []string{"view"}, Conditions: Conditions{ "ip": &CIDRCondition{ CIDR: "1234", }, "owner": &EqualsSubjectCondition{}, }, }, }
Functions ¶
func AssertPolicyEqual ¶
func NewErrResourceNotFound ¶
func TestHelperGetErrors ¶
Types ¶
type BodyArrayMatchCondition ¶
type BodyArrayMatchCondition struct { Mode string `json:"mode"` Path string `json:"path"` Matches string `json:"matches"` }
BodyArrayMatchCondition is a condition which is fulfilled if the value at the path in the body is an array and all/any of its elements match the regex pattern specified in BodyArrayMatchCondition
func (*BodyArrayMatchCondition) Fulfills ¶
func (c *BodyArrayMatchCondition) Fulfills(_ interface{}, r *Request) bool
Fulfills returns true if the value at the path in the body is an array and all/any of its elements match the regex pattern specified in BodyMatchCondition
func (*BodyArrayMatchCondition) GetName ¶
func (c *BodyArrayMatchCondition) GetName() string
GetName returns the condition's name.
type BodyMatchCondition ¶
BodyMatchCondition is a condition which is fulfilled if the value at the path in the body matches the regex pattern specified in BodyMatchCondition
func (*BodyMatchCondition) Fulfills ¶
func (c *BodyMatchCondition) Fulfills(_ interface{}, r *Request) bool
Fulfills returns true if the value at the path in the body matches the regex pattern specified in BodyMatchCondition
func (*BodyMatchCondition) GetName ¶
func (c *BodyMatchCondition) GetName() string
GetName returns the condition's name.
type CIDRCondition ¶
type CIDRCondition struct {
CIDR string `json:"cidr"`
}
CIDRCondition makes sure that the warden requests' IP address is in the given CIDR.
func (*CIDRCondition) Fulfills ¶
func (c *CIDRCondition) Fulfills(value interface{}, _ *Request) bool
Fulfills returns true if the the request is fulfilled by the condition.
func (*CIDRCondition) GetName ¶
func (c *CIDRCondition) GetName() string
GetName returns the condition's name.
type Condition ¶
type Condition interface { // GetName returns the condition's name. GetName() string // Fulfills returns true if the request is fulfilled by the condition. Fulfills(interface{}, *Request) bool }
Condition either do or do not fulfill an access request.
type Conditions ¶
Conditions is a collection of conditions.
func (Conditions) AddCondition ¶
func (cs Conditions) AddCondition(key string, c Condition)
AddCondition adds a condition to the collection.
func (Conditions) MarshalJSON ¶
func (cs Conditions) MarshalJSON() ([]byte, error)
MarshalJSON marshals a list of conditions to json.
func (Conditions) UnmarshalJSON ¶
func (cs Conditions) UnmarshalJSON(data []byte) error
UnmarshalJSON unmarshals a list of conditions from json.
type DefaultPolicies ¶
type DefaultPolicies struct { // in: body DefaultPolicies []*DefaultPolicy `json:"policies"` }
swagger:response Policies
type DefaultPolicy ¶
type DefaultPolicy struct { ID string `json:"id" gorethink:"id"` Description string `json:"description" gorethink:"description"` Subjects []string `json:"subjects" gorethink:"subjects"` Effect string `json:"effect" gorethink:"effect"` Resources []string `json:"resources" gorethink:"resources"` Actions []string `json:"actions" gorethink:"actions"` Conditions Conditions `json:"conditions" gorethink:"conditions"` }
DefaultPolicy is the default implementation of the policy interface.
swagger:model Policy
func (*DefaultPolicy) AllowAccess ¶
func (p *DefaultPolicy) AllowAccess() bool
AllowAccess returns true if the policy effect is allow, otherwise false.
func (*DefaultPolicy) AttachIdentity ¶
func (p *DefaultPolicy) AttachIdentity(ids ...string) error
AttachIdentity adds identities to the Subjects
func (*DefaultPolicy) DetachIdentity ¶
func (p *DefaultPolicy) DetachIdentity(ids ...string) error
DetachIdentity removes a user,group,role or any other identity from the subject of the policy
func (*DefaultPolicy) GetActions ¶
func (p *DefaultPolicy) GetActions() []string
GetActions returns the policies actions.
func (*DefaultPolicy) GetConditions ¶
func (p *DefaultPolicy) GetConditions() Conditions
GetConditions returns the policies conditions.
func (*DefaultPolicy) GetDescription ¶
func (p *DefaultPolicy) GetDescription() string
GetDescription returns the policies description.
func (*DefaultPolicy) GetEffect ¶
func (p *DefaultPolicy) GetEffect() string
GetEffect returns the policies effect which might be 'allow' or 'deny'.
func (*DefaultPolicy) GetEndDelimiter ¶
func (p *DefaultPolicy) GetEndDelimiter() byte
GetEndDelimiter returns the delimiter which identifies the end of a regular expression.
func (*DefaultPolicy) GetResources ¶
func (p *DefaultPolicy) GetResources() []string
GetResources returns the policies resources.
func (*DefaultPolicy) GetStartDelimiter ¶
func (p *DefaultPolicy) GetStartDelimiter() byte
GetStartDelimiter returns the delimiter which identifies the beginning of a regular expression.
func (*DefaultPolicy) GetSubjects ¶
func (p *DefaultPolicy) GetSubjects() []string
GetSubjects returns the policies subjects.
func (*DefaultPolicy) HasIdentity ¶
func (p *DefaultPolicy) HasIdentity(id string) bool
HasIdentity returns true if the provided identity is part of the policy i.e. contained in the subject.
func (*DefaultPolicy) UnmarshalJSON ¶
func (p *DefaultPolicy) UnmarshalJSON(data []byte) error
UnmarshalJSON overwrite own policy with values of the given in policy in JSON format
func (*DefaultPolicy) Validate ¶
func (p *DefaultPolicy) Validate() error
Validate validates properties and values of the policy
func (*DefaultPolicy) ValidateEffect ¶
func (p *DefaultPolicy) ValidateEffect() error
ValidateEffect validates the effect
func (*DefaultPolicy) ValidateSubjects ¶
func (p *DefaultPolicy) ValidateSubjects() error
ValidateSubjects validates each subject to make sure they are syntactically correct
type EqualsSubjectCondition ¶
type EqualsSubjectCondition struct{}
EqualsSubjectCondition is a condition which is fulfilled if the request's subject is equal to the given value string
func (*EqualsSubjectCondition) Fulfills ¶
func (c *EqualsSubjectCondition) Fulfills(value interface{}, r *Request) bool
Fulfills returns true if the request's subject is equal to the given value string
func (*EqualsSubjectCondition) GetName ¶
func (c *EqualsSubjectCondition) GetName() string
GetName returns the condition's name.
type Ladon ¶
type Ladon struct { Manager Manager Matcher matcher }
Ladon is an implementation of Warden.
type Manager ¶
type Manager interface { // Create persists the policy. Create(policy Policy) error // Update updates an existing policy. Update(policy Policy) error // Get retrieves a policy. Get(id string) (Policy, error) // Delete removes a policy. Delete(id string) error // GetAll retrieves all policies. GetAll(limit, offset int64) (Policies, error) // FindRequestCandidates returns candidates that could match the request object. It either returns // a set that exactly matches the request, or a superset of it. If an error occurs, it returns nil and // the error. FindRequestCandidates(r *Request) (Policies, error) }
Manager is responsible for managing and persisting policies.
type ManagerMigrator ¶
type Policy ¶
type Policy interface { // GetID returns the policies id. GetID() string // GetDescription returns the policies description. GetDescription() string // GetSubjects returns the policies subjects. GetSubjects() []string // AllowAccess returns true if the policy effect is allow, otherwise false. AllowAccess() bool // GetEffect returns the policies effect which might be 'allow' or 'deny'. GetEffect() string // GetResources returns the policies resources. GetResources() []string // GetActions returns the policies actions. GetActions() []string // GetConditions returns the policies conditions. GetConditions() Conditions // GetStartDelimiter returns the delimiter which identifies the beginning of a regular expression. GetStartDelimiter() byte // GetEndDelimiter returns the delimiter which identifies the end of a regular expression. GetEndDelimiter() byte }
Policy represent a policy model.
type RegexpMatcher ¶
func NewRegexpMatcher ¶
func NewRegexpMatcher(size int) *RegexpMatcher
type Request ¶
type Request struct { // Resource is the resource that access is requested to. Resource string `json:"resource"` // Action is the action that is requested on the resource. Action string `json:"action"` // Subejct is the subject that is requesting access. Subjects []string `json:"subject"` // Context is the request's environmental context. Context Context `json:"context"` }
Request is the warden's request object.
type StringEqualCondition ¶
type StringEqualCondition struct {
Equals string `json:"equals"`
}
StringEqualCondition is a condition which is fulfilled if the given string value is the same as specified in StringEqualCondition
func (*StringEqualCondition) Fulfills ¶
func (c *StringEqualCondition) Fulfills(value interface{}, _ *Request) bool
Fulfills returns true if the given value is a string and is the same as in StringEqualCondition.Equals
func (*StringEqualCondition) GetName ¶
func (c *StringEqualCondition) GetName() string
GetName returns the condition's name.
type StringMatchCondition ¶
type StringMatchCondition struct {
Matches string `json:"matches"`
}
StringMatchCondition is a condition which is fulfilled if the given string value matches the regex pattern specified in StringMatchCondition
func (*StringMatchCondition) Fulfills ¶
func (c *StringMatchCondition) Fulfills(value interface{}, _ *Request) bool
Fulfills returns true if the given value is a string and matches the regex pattern in StringMatchCondition.Matches
func (*StringMatchCondition) GetName ¶
func (c *StringMatchCondition) GetName() string
GetName returns the condition's name.
type StringPairsEqualCondition ¶
type StringPairsEqualCondition struct{}
StringPairsEqualCondition is a condition which is fulfilled if the given array of pairs contains two-element string arrays where both elements in the string array are equal
func (*StringPairsEqualCondition) Fulfills ¶
func (c *StringPairsEqualCondition) Fulfills(value interface{}, _ *Request) bool
Fulfills returns true if the given value is an array of string arrays and each string array has exactly two values which are equal
func (*StringPairsEqualCondition) GetName ¶
func (c *StringPairsEqualCondition) GetName() string
GetName returns the condition's name.
type Warden ¶
type Warden interface { // IsAllowed returns nil if subject s can perform action a on resource r with context c or an error otherwise. // if err := guard.IsAllowed(&Request{Resource: "article/1234", Action: "update", Subject: "peter"}); err != nil { // return errors.New("Not allowed") // } IsAllowed(r *Request) error }
Warden is responsible for deciding if subject s can perform action a on resource r with context c.
Source Files ¶
- condition.go
- condition_body_array_match.go
- condition_body_match.go
- condition_cidr.go
- condition_string_equal.go
- condition_string_match.go
- condition_string_pairs_equal.go
- condition_subject_equal.go
- const.go
- context.go
- errors.go
- jq.go
- ladon.go
- manager.go
- manager_migrator.go
- manager_test_helper.go
- matcher.go
- matcher_regexp.go
- policy.go
- warden.go
Directories ¶
Path | Synopsis |
---|---|
Package compiler offers a regexp compiler which compiles regex templates to regexp.Regexp reg, err := compiler.CompileRegex("foo:bar.baz:<[0-9]{2,10}>", '<', '>') // if err != nil ...
|
Package compiler offers a regexp compiler which compiles regex templates to regexp.Regexp reg, err := compiler.CompileRegex("foo:bar.baz:<[0-9]{2,10}>", '<', '>') // if err != nil ... |
manager
|
|