Documentation ¶
Overview ¶
Package ash implements a highly configurable and callback based ACL that can be used to authorize controller operations in a declarative way.
Index ¶
- Constants
- func C(s *Strategy) *fire.Callback
- func Execute() *fire.Callback
- func Identify(identifier Identifier) *fire.Callback
- func IdentifyPublic() *fire.Callback
- func IdentifyToken(scope []string, identity func(*flame.AuthInfo) Identity) *fire.Callback
- func Select(selector Selector) *fire.Callback
- func SelectMatch(identity Identity, policy func(Identity) *Policy) *fire.Callback
- func SelectPublic(fn func() *Policy) *fire.Callback
- type Access
- type AccessMatrix
- type AccessTable
- type Authorizer
- func A(name string, m fire.Matcher, h Handler) *Authorizer
- func And(a, b *Authorizer) *Authorizer
- func Filter(filter bson.M) *Authorizer
- func Or(a, b *Authorizer) *Authorizer
- func Public() *Authorizer
- func Token(scope ...string) *Authorizer
- func Whitelist(m Matrix) []*Authorizer
- func WhitelistFields(fields Fields) *Authorizer
- func WhitelistProperties(readable []string) *Authorizer
- type Enforcer
- func AddFilter(filter bson.M) *Enforcer
- func AddRelationshipFilter(rel string, filter bson.M) *Enforcer
- func DenyAccess() *Enforcer
- func E(name string, m fire.Matcher, h fire.Handler) *Enforcer
- func GrantAccess() *Enforcer
- func SetReadableFieldsGetter(fn func(ctx *fire.Context, model coal.Model) []string) *Enforcer
- func SetReadablePropertiesGetter(fn func(ctx *fire.Context, model coal.Model) []string) *Enforcer
- func SetWritableFieldsGetter(fn func(ctx *fire.Context, model coal.Model) []string) *Enforcer
- func WhitelistReadableFields(fields ...string) *Enforcer
- func WhitelistReadableProperties(fields ...string) *Enforcer
- func WhitelistWritableFields(fields ...string) *Enforcer
- type Fields
- type Handler
- type Identifier
- type Identity
- type L
- type M
- type Matrix
- type NamedAccessMatrix
- type Policy
- type PublicIdentity
- type S
- type Selector
- type Strategy
Constants ¶
const IdentityDataKey = "ash:identity"
IdentityDataKey is the key used to store the identity.
const PolicyDataKey = "ash:policy"
PolicyDataKey is the key used to store policies.
Variables ¶
This section is empty.
Functions ¶
func Identify ¶ added in v0.32.0
func Identify(identifier Identifier) *fire.Callback
Identify will run the provided identifier for all requests to establish the requesters' identity.
func IdentifyPublic ¶ added in v0.32.0
IdentifyPublic will identify public access.
func IdentifyToken ¶ added in v0.32.0
IdentifyToken will identity token access.
func Select ¶ added in v0.32.0
Select will run the provided function to select a policy for the supplied identity.
func SelectMatch ¶ added in v0.32.0
SelectMatch will match the provided identity and on success use the provided factory to create a policy.
func SelectPublic ¶ added in v0.32.0
SelectPublic will match the public identity and use the provided factory to create a policy.
Types ¶
type AccessMatrix ¶ added in v0.32.0
AccessMatrix defines a string based access matrix.
func (AccessMatrix) Compile ¶ added in v0.32.0
func (m AccessMatrix) Compile(columns ...int) AccessTable
Compile return an access table for the provided column.
type AccessTable ¶ added in v0.32.0
AccessTable defines a string based access table.
func (AccessTable) Collect ¶ added in v0.32.0
func (t AccessTable) Collect(match Access) []string
Collect will return all strings with a matching access level.
type Authorizer ¶
type Authorizer struct { // The matcher that decides whether the authorizer can be run. Matcher fire.Matcher // The handler that gets executed with the context. Handler Handler }
An Authorizer should inspect the specified context and assesses if it is able to enforce authorization with the data that is available. If yes, the authorizer should return a non-zero set of enforcers that will enforce the authorization.
func A ¶ added in v0.12.0
func A(name string, m fire.Matcher, h Handler) *Authorizer
A is a shorthand function to construct an authorizer. It will also add tracing code around the execution of the authorizer.
func And ¶
func And(a, b *Authorizer) *Authorizer
And will match and run both authorizers and return immediately if one does not return a set of enforcers. The two successfully returned enforcer sets are merged into one and returned.
func Filter ¶ added in v0.29.0
func Filter(filter bson.M) *Authorizer
Filter will authorize the request by enforcing the provided filter.
func Or ¶
func Or(a, b *Authorizer) *Authorizer
Or will match and run the first authorizer and return its enforcers on success. If no enforcers are returned it will match and run the second authorizer and return its enforcers on success.
func Public ¶ added in v0.29.0
func Public() *Authorizer
Public will authorize the request if nobody is authenticated.
Note: This authorizer requires preliminary un-forced authorization using flame.Callback().
func Token ¶ added in v0.29.0
func Token(scope ...string) *Authorizer
Token will authorize the request if a token with the specified scope has been authenticated.
Note: This authorizer requires preliminary authorization using flame.Callback().
func Whitelist ¶ added in v0.21.1
func Whitelist(m Matrix) []*Authorizer
Whitelist will return a list of authorizers that will authorize field access for the specified candidates in the matrix. Fields is evaluated by checking for the "R" (readable), "C" (creatable), "U" (updatable) and "W" (writable) tag in the proper row and column of the matrix. It is recommended to authorize field access in a separate strategy following general resource access as the returned enforcers will always authorize the request:
ash.C(&ash.Strategy{ All: ash.Whitelist(ash.Matrix{ Model: &Post{}, Candidates: ash.L{Public(), Token("user")}, Fields: map[string][]string{ "Title": {"R", "RC"}, "Body": {"R", "RW"}, }, Properties: map[string][]bool{ "Info": {false, true}, }, }), }
func WhitelistFields ¶ added in v0.14.1
func WhitelistFields(fields Fields) *Authorizer
WhitelistFields is an authorizer that will whitelist the readable and writable fields on the context using enforcers. It is recommended to authorize field access in a separate strategy following general resource access as the returned enforcers will always authorize the request. Furthermore, the easiest is to implement a custom candidate authorizer with which this authorizer can be chained together:
Token("user").And(WhitelistFields(Fields{ Readable: []string{"Title", "Body"}, Writable: []string{"Body"}, }))
func WhitelistProperties ¶ added in v0.31.0
func WhitelistProperties(readable []string) *Authorizer
WhitelistProperties is an authorizer that will whitelist the readable properties on the context using enforcers. It is recommended to authorize property access in a separate strategy following general resource access as the returned enforcers will always authorize the request. Furthermore, the easiest is to implement a custom candidate authorizer with which this authorizer can be chained together:
Token("user").And(WhitelistProperties([]string{"Info"})
func (*Authorizer) And ¶
func (a *Authorizer) And(b *Authorizer) *Authorizer
And will run And() with the current and specified authorizer.
func (*Authorizer) Or ¶
func (a *Authorizer) Or(b *Authorizer) *Authorizer
Or will run Or() with the current and specified authorizer.
type Enforcer ¶
An Enforcer is returned by an Authorizer to enforce the previously inspected Authorization.
Enforcers should only return errors if the operation is clearly not allowed for the presented candidate and that this information is general knowledge (e.g. API documentation). In order to prevent the leakage of implementation details the enforcer should mutate the context's filter list to hide existing data from the candidate.
func AddFilter ¶ added in v0.11.0
AddFilter will enforce the authorization by adding the passed filter to the Filter list of the context. It should be used if the candidate is allowed to access the resource in general, but some documents should be filtered out.
Note: This enforcer cannot be used to authorize Create and CollectionAction operations.
func AddRelationshipFilter ¶ added in v0.26.1
AddRelationshipFilter will enforce the authorization by adding the passed relationship filter to the RelationshipFilter field of the context. It should be used if the candidate is allowed to access the relationship in general, but some documents should be filtered out.
Note: This enforcer cannot be used to authorize Create and CollectionAction operations.
func DenyAccess ¶ added in v0.12.0
func DenyAccess() *Enforcer
DenyAccess will enforce the authorization by directly returning an access denied error. It should be used if the operation should not be authorized in any case (.e.g a candidate accessing a resource he has clearly no access to).
Note: Usually access is denied by returning no enforcers. This enforcer should only be returned to immediately stop the authorization process and prevent other enforcers from authorizing the operation.
func GrantAccess ¶ added in v0.12.0
func GrantAccess() *Enforcer
GrantAccess will enforce the authorization without any changes to the context. It should be used stand-alone if the presented candidate has full access to the data (.e.g a superuser) or in an authorizer chain to delegate authorization to the next authorizer.
func SetReadableFieldsGetter ¶ added in v0.32.0
SetReadableFieldsGetter will enforce the authorization by setting the specified readable fields getter.
Note: This enforcer cannot be used to authorize Delete, ResourceAction and CollectionAction operations.
func SetReadablePropertiesGetter ¶ added in v0.32.0
SetReadablePropertiesGetter will enforce the authorization by setting the specified readable properties getter.
Note: This enforcer cannot be used to authorize Delete, ResourceAction and CollectionAction operations.
func SetWritableFieldsGetter ¶ added in v0.32.0
SetWritableFieldsGetter will enforce the authorization by setting the specified writable fields getter.
Note: This enforcer can only be used to authorize Create and Update operations.
func WhitelistReadableFields ¶ added in v0.15.0
WhitelistReadableFields will enforce the authorization by making sure only the specified fields are returned for the client.
Note: This enforcer cannot be used to authorize Delete, ResourceAction and CollectionAction operations.
func WhitelistReadableProperties ¶ added in v0.31.0
WhitelistReadableProperties will enforce the authorization by making sure only the specified properties are returned for the client.
Note: This enforcer cannot be used to authorize Delete, ResourceAction and CollectionAction operations.
func WhitelistWritableFields ¶ added in v0.15.0
WhitelistWritableFields will enforce the authorization by making sure only the specified fields can be changed by the client.
Note: This enforcer can only be used to authorize Create and Update operations.
type Handler ¶ added in v0.12.0
Handler is a function that inspects an operation context and potentially returns a set of enforcers or an error.
type Identifier ¶ added in v0.32.0
Identifier is the function run establish an identity.
type Identity ¶ added in v0.32.0
type Identity interface{}
Identity describes the common interface for identities which currently allows for any value to represent an identity.
type M ¶ added in v0.10.0
type M = map[string][]*Authorizer
M is a shorthand type to create a map of authorizers.
type Matrix ¶ added in v0.21.1
type Matrix struct { // Model is the model being authorized. Model coal.Model // Candidates are the authorizers that establish individual candidate // authorization. Candidates []*Authorizer // Fields is the matrix that specifies read and write permissions per field // and candidate using the tags "R", "C", "U" and "W". Fields map[string][]string // Properties is the matrix that specifies read permissions per property and // candidate. Properties map[string][]bool }
Matrix is used for declarative specification of field and property access for multiple candidates.
func (*Matrix) CollectFields ¶ added in v0.31.0
CollectFields will return a list of fields for the specified column in the matrix which match at least one of the provided tags.
func (*Matrix) CollectProperties ¶ added in v0.31.0
CollectProperties will return a list of properties for the specified column in the matrix.
type NamedAccessMatrix ¶ added in v0.32.0
type NamedAccessMatrix struct { Columns []string Matrix AccessMatrix }
NamedAccessMatrix defines a named string based access matrix.
func (NamedAccessMatrix) Compile ¶ added in v0.32.0
func (m NamedAccessMatrix) Compile(columns ...string) AccessTable
Compile return an access table for the provided column.
type Policy ¶ added in v0.32.0
type Policy struct { // Access defines the general access. Access Access // Actions defines the allowed actions. Actions map[string]bool // The default fields used to determine the field access level. If the // getter is set, these will only be used to establish valid filters and // sorters during the fire.List operation authorizer stage, as well as the // writable fields during the fire.Create operation, otherwise the model // specific fields are used instead. Fields AccessTable // GetFilter is called to obtain the general resource access filter. This // filter is used to narrow down accessible resources in all operations // except fire.Create and fire.CollectionAction operations. GetFilter func(ctx *fire.Context) bson.M // VerifyID is called to for every direct model lookup to verify resource // level access. This function is called for all operations except fire.List, // fire.Create and fire.CollectionAction. VerifyID func(ctx *fire.Context, id coal.ID) Access // VerifyModel is called for every model load from the database to determine // resource level access. This function is called for all operations except // fire.Create and fire.CollectionAction. // // Note: The verification is deferred to the fire.Verifier stage. VerifyModel func(ctx *fire.Context, model coal.Model) Access // VerifyCreate and VerifyUpdate determine resource level access after all // modification have been applied. This function is called for the // fire.Create and fire.Update operation. // // Note: The verification is deferred to the fire.Validator stage. VerifyCreate func(ctx *fire.Context, model coal.Model) bool VerifyUpdate func(ctx *fire.Context, model coal.Model) bool // GetFields and GetProperties is called for every model to determine the // field and property level access. The function is called for all operations // except fire.Delete, fire.CollectionAction and fire.ResourceAction. // // Note: The policy should refrain from creating a new map for every request // and instead pre-allocate possible combinations and return those. GetFields func(ctx *fire.Context, model coal.Model) AccessTable GetProperties func(ctx *fire.Context, model coal.Model) AccessTable }
Policy defines an authorization policy.
type PublicIdentity ¶ added in v0.32.0
type PublicIdentity struct{}
PublicIdentity is a generic public identity.
type Strategy ¶
type Strategy struct { // individual operations List []*Authorizer Find []*Authorizer Create []*Authorizer Update []*Authorizer Delete []*Authorizer // individual action operations CollectionAction map[string][]*Authorizer ResourceAction map[string][]*Authorizer // all action operations CollectionActions []*Authorizer ResourceActions []*Authorizer // all List and Find operations Read []*Authorizer // all Create, Update and Delete operations Write []*Authorizer // all CollectionAction and ResourceAction operations Actions []*Authorizer // all operations All []*Authorizer }
Strategy contains lists of authorizers that are used to authorize operations. The authorizes are run in order beginning with the most specific authorizer. The first authorizer that returns no error and a non-empty set of enforcers has its enforcers applied to the request. If the enforcers do not return and error the access is granted.