Documentation
¶
Index ¶
- Variables
- func CheckGlobalRequest(c *fiber.Ctx, bCtx *BusinessContext) (allow bool)
- func CheckNamespacedRequest(c *fiber.Ctx, params map[string]string, ...) (allow bool, err error)
- func ConfigureSecurity(router fiber.Router, opts SecurityOpts) fiber.Handler
- func DecodeValue(decoders map[string]ValueDecoder, claims map[string]any, key string) (string, problems.Problem)
- func IsMock(opts []Option[*JWTOpts]) bool
- func IsObfuscated(ctx context.Context) bool
- func IsReadyOnlyRequest(c *fiber.Ctx) bool
- func NewBusinessCtxMiddleware(mwOpts BusinessContextOpts) fiber.Handler
- func NewBusinessCtxMiddlewareWithOpts(opts ...Option[*BusinessContextOpts]) fiber.Handler
- func NewCheckAccessMiddlewareWithOpts(opts ...Option[*CheckAccessOpts]) fiber.Handler
- func NewJWT(opts JWTOpts) fiber.Handler
- func NewJWTWithOpts(opts ...Option[*JWTOpts]) fiber.Handler
- func ToContext(ctx context.Context, bCtx *BusinessContext) context.Context
- type AccessType
- type BusinessContext
- type BusinessContextOpts
- type CheckAccessOpts
- type ClientType
- type CompareCtxInfo
- type ComparisonTemplates
- type JWTOpts
- type MatchType
- type Matcher
- func (m *Matcher) FullMatch(compareCtxInfo CompareCtxInfo) (bool, error)
- func (m *Matcher) StartsWith(compareCtxInfo CompareCtxInfo) (bool, error)
- func (m *Matcher) ToExpectedString(bCtx *BusinessContext, params map[string]string) (string, error)
- func (m *Matcher) ToString(compareCtxInfo CompareCtxInfo) (string, string, error)
- type MatcherFunc
- type Option
- func WithDefaultScope(scope string) Option[*BusinessContextOpts]
- func WithExpectedResourcePathFunc(f ResourcePathFunc) Option[*CheckAccessOpts]
- func WithLmsCheck(basePath string) Option[*JWTOpts]
- func WithLog(log logr.Logger) Option[*BusinessContextOpts]
- func WithPathParamKey(keys ...string) Option[*CheckAccessOpts]
- func WithPrefixFunc(f ResourcePathFunc) Option[*CheckAccessOpts]
- func WithScopePrefix(prefix string) Option[*BusinessContextOpts]
- func WithTemplates(templates map[ClientType]ComparisonTemplates) Option[*CheckAccessOpts]
- func WithTrustedIssuers(issuers []string) Option[*JWTOpts]
- func WithValueDecoder(key string, decoder ValueDecoder) Option[*BusinessContextOpts]
- func WithValueDecoders(decoders map[string]ValueDecoder) Option[*BusinessContextOpts]
- type ResourcePathFunc
- type SecurityOpts
- type ValueDecoder
Constants ¶
This section is empty.
Variables ¶
var UserContextKey = "user"
Functions ¶
func CheckGlobalRequest ¶
func CheckGlobalRequest(c *fiber.Ctx, bCtx *BusinessContext) (allow bool)
CheckGlobalRequest performs access checks for global requests Global requests are requests are all requests that do not have a direct resource reference e.g. requests for to list resource `GET /api/v1/foos`
func CheckNamespacedRequest ¶
func CheckNamespacedRequest(c *fiber.Ctx, params map[string]string, templates map[ClientType]ComparisonTemplates, bCtx *BusinessContext) (allow bool, err error)
CheckNamespacedRequest performs access checks for namespaced requests Namespaced requests are requests that have a direct resource reference e.g. requests for a specific resource `GET /api/v1/foos/<namespace>/<name>` It does to by matching the prefix of the namespace with the client's context and checking if the client has the required access rights
func ConfigureSecurity ¶
func ConfigureSecurity(router fiber.Router, opts SecurityOpts) fiber.Handler
ConfigureSecurity configures the security middlewares This is a convenience function that configures the JWT middleware and the business context middleware It also returns the check access middleware that should be configured on the route level
func DecodeValue ¶
func DecodeValue(decoders map[string]ValueDecoder, claims map[string]any, key string) (string, problems.Problem)
DecodeValue decodes a value from the claims map using a custom decoder if available If not, it will try to get the value directly from the claims map
func IsMock ¶
IsMock checks if the security middleware is mocked If the trusted issuers are not set, the middleware is considered mocked
func IsObfuscated ¶
func IsReadyOnlyRequest ¶
func NewBusinessCtxMiddleware ¶
func NewBusinessCtxMiddleware(mwOpts BusinessContextOpts) fiber.Handler
NewBusinessCtxMiddleware is a convencience function. See NewBusinessCtxMiddlewareWithOpts
func NewBusinessCtxMiddlewareWithOpts ¶
func NewBusinessCtxMiddlewareWithOpts(opts ...Option[*BusinessContextOpts]) fiber.Handler
NewBusinessCtxMiddlewareWithOpts creates a new middleware that extracts the business context from the JWT token The business context is used to determine the client type and access type The client type is used to determine which resources this client is allowed to see The access type is used to determine if the client has read or write access The actual check if the client is allowed to see a resource is done in `check_access.go`
Example of the JWT token structure:
{
"env": "dev",
"clientId": "group--team--user",
"scopes": "group:team:read group:team:write",
# These are used by the JWT middleware
"operation": "GET",
"requestPath": "/api/v1/resource",
}
func NewCheckAccessMiddlewareWithOpts ¶
func NewCheckAccessMiddlewareWithOpts(opts ...Option[*CheckAccessOpts]) fiber.Handler
NewCheckAccessMiddlewareWithOpts creates a new middleware that checks if the client has access to the requested resource
It is expected that `business_context` middleware is executed before this middleware ¶
The middleware checks the client's context and access rights to determine if the client has access to the requested resource.
As this middleware depends on path-params it has to be configured on the route level ```go app.Get("/api/v1/foos/:namespace/:name", NewCheckAccessMiddlewareWithOpts(), handler) ```
Types ¶
type AccessType ¶
type AccessType string
AccessType represents the access type of the client Therefore, it is used to determine if the client has read or write access
const ( AccessTypeReadOnly AccessType = "read" AccessTypeObfuscated AccessType = "obfuscated" AccessTypeReadWrite AccessType = "all" )
func DetermineAccess ¶
func DetermineAccess(scopes []string, prefix string) (AccessType, error)
DetermineAccess determines the access type based on the scopes It is expeceted that all relevant scopes follow the pattern `<prefix>:<clientType>:<accessType>` Therefore, it is used to determine if the client has read or write access
func (AccessType) IsObfuscated ¶
func (a AccessType) IsObfuscated() bool
func (AccessType) IsRead ¶
func (a AccessType) IsRead() bool
func (AccessType) IsWrite ¶
func (a AccessType) IsWrite() bool
type BusinessContext ¶
type BusinessContext struct {
Environment string
Group string
Team string
ClientType ClientType
AccessType AccessType
}
func FromContext ¶
func FromContext(ctx context.Context) (*BusinessContext, bool)
type BusinessContextOpts ¶
type CheckAccessOpts ¶
type CheckAccessOpts struct {
PathParamKeys []string
ExpectedResourcePath ResourcePathFunc
// Prefix is used to calculate the prefix that is used in the context
// It is expected that this prefix is then used to determine the access to the store
// The store uses a key-format `<namespace>/<name>` to store resources.
// At most the prefix should match the namespace part of the key
Prefix ResourcePathFunc
Templates map[ClientType]ComparisonTemplates
}
type ClientType ¶
type ClientType string
ClientType represents the type of the client Therefore, it is used to determine which resources this client is allowed to see
const ( ClientTypeTeam ClientType = "team" ClientTypeGroup ClientType = "group" ClientTypeAdmin ClientType = "admin" )
func DetermineClientType ¶
func DetermineClientType(scopes []string, prefix string) (ClientType, error)
DetermineClientType determines the client type based on the scopes It is expeceted that all relevant scopes follow the pattern `<prefix>:<clientType>:<accessType>` Therefore, it is used to determine which resources this client is allowed to see e.g. owned by the team, group or all
type CompareCtxInfo ¶
type CompareCtxInfo struct {
B *BusinessContext
P map[string]string
}
func NewCompareCtxInfo ¶
func NewCompareCtxInfo(bCtx *BusinessContext, pathParams map[string]string) CompareCtxInfo
type ComparisonTemplates ¶
type ComparisonTemplates struct {
ExpectedTemplate string
UserInputTemplate string
MatchType MatchType
}
func (ComparisonTemplates) String ¶
func (c ComparisonTemplates) String() string
type Matcher ¶
type Matcher struct {
ExpectedTemplate string
UserInputTemplate string
// contains filtered or unexported fields
}
func NewMatcher ¶
func (*Matcher) FullMatch ¶
func (m *Matcher) FullMatch(compareCtxInfo CompareCtxInfo) (bool, error)
func (*Matcher) StartsWith ¶
func (m *Matcher) StartsWith(compareCtxInfo CompareCtxInfo) (bool, error)
func (*Matcher) ToExpectedString ¶
type MatcherFunc ¶
type Option ¶
type Option[T any] func(T)
func WithDefaultScope ¶
func WithDefaultScope(scope string) Option[*BusinessContextOpts]
func WithExpectedResourcePathFunc ¶
func WithExpectedResourcePathFunc(f ResourcePathFunc) Option[*CheckAccessOpts]
func WithLmsCheck ¶
func WithPathParamKey ¶
func WithPathParamKey(keys ...string) Option[*CheckAccessOpts]
func WithPrefixFunc ¶
func WithPrefixFunc(f ResourcePathFunc) Option[*CheckAccessOpts]
func WithScopePrefix ¶
func WithScopePrefix(prefix string) Option[*BusinessContextOpts]
func WithTemplates ¶
func WithTemplates(templates map[ClientType]ComparisonTemplates) Option[*CheckAccessOpts]
func WithTrustedIssuers ¶
func WithValueDecoder ¶
func WithValueDecoder(key string, decoder ValueDecoder) Option[*BusinessContextOpts]
func WithValueDecoders ¶
func WithValueDecoders(decoders map[string]ValueDecoder) Option[*BusinessContextOpts]
type ResourcePathFunc ¶
type ResourcePathFunc func(*BusinessContext, map[string]string, map[ClientType]ComparisonTemplates) (string, error)
ResourcePathFunc must return the expected resource for the provided BusinessContext
type SecurityOpts ¶
type SecurityOpts struct {
Enabled bool
Log logr.Logger
JWTOpts []Option[*JWTOpts]
BusinessContextOpts []Option[*BusinessContextOpts]
CheckAccessOpts []Option[*CheckAccessOpts]
}