Documentation
¶
Index ¶
- Constants
- Variables
- func BuiltInPolicies() []policyTY.Policy
- func BusinessName(kind string, item interface{}) string
- func FormatResource(kind, name string) string
- func IsBuiltInPolicyID(id string) bool
- func ItemAllowedForList(kind string, patterns []string, item interface{}) bool
- func MatchAction(pattern, action string) bool
- func MatchResource(pattern, resource string) bool
- func MatchResourceAllowCascade(pattern, resource string) bool
- func MatchResourceDenyCascade(pattern, resource string) bool
- func ResourceFromQuickID(quickID string) (string, error)
- type API
- func (a *API) Allowed(subject Subject, action, resource string) error
- func (a *API) AllowedKindWide(subject Subject, action, kind string) error
- func (a *API) AllowedMetricPath(subject Subject, fieldPath string) error
- func (a *API) AssignAdminToUsersWithoutPolicies() error
- func (a *API) AuthorizeActionRequest(subject Subject, r *http.Request) error
- func (a *API) AuthorizeBodyTargets(subject Subject, r *http.Request, access *RequestAccess) error
- func (a *API) AuthorizeMetricRequest(subject Subject, r *http.Request) error
- func (a *API) AuthorizeQuickIDRequest(subject Subject, r *http.Request) error
- func (a *API) AuthorizeSleepingQueueRequest(subject Subject, r *http.Request) error
- func (a *API) Cache() *Cache
- func (a *API) Delete(IDs []string) (int64, error)
- func (a *API) EnsureBuiltInPolicies() error
- func (a *API) EnsureServiceAccountActive(userID, tokenID string) error
- func (a *API) EnsureUserActive(userID string) (*userTY.User, error)
- func (a *API) GetByID(id string) (policyTY.Policy, error)
- func (a *API) GetEntityInterface() interface{}
- func (a *API) Import(data interface{}) error
- func (a *API) List(filters []storageTY.Filter, pagination *storageTY.Pagination) (*storageTY.Result, error)
- func (a *API) NotifyTokenDeleted(entityID, tokenID string)
- func (a *API) NotifyTokenUpdated(token *svcAccountTY.ServiceAccount)
- func (a *API) NotifyUserDeleted(id string)
- func (a *API) NotifyUserUpdated(user *userTY.User)
- func (a *API) ReportUsersWithoutPolicies() error
- func (a *API) ResolveBusinessName(kind, storageID string) (string, error)
- func (a *API) ResolveResource(access *RequestAccess)
- func (a *API) ResourceNamesForList(subject Subject, kind string) (unrestricted bool, patterns []string, err error)
- func (a *API) Save(policy *policyTY.Policy) error
- func (a *API) StorageFiltersForList(subject Subject, kind string) (unrestricted bool, filters []storageTY.Filter, err error)
- func (a *API) ValidatePoliciesExist(ids []string) error
- type Cache
- func (c *Cache) GetPolicy(id string) (*policyTY.Policy, error)
- func (c *Cache) GetToken(tokenID string) (*svcAccountTY.ServiceAccount, error)
- func (c *Cache) GetUser(id string) (*userTY.User, error)
- func (c *Cache) InvalidatePolicy(id string)
- func (c *Cache) InvalidateToken(tokenID string)
- func (c *Cache) InvalidateTokenByEntityID(entityID string)
- func (c *Cache) InvalidateUser(id string)
- func (c *Cache) PutPolicy(p *policyTY.Policy)
- func (c *Cache) PutToken(t *svcAccountTY.ServiceAccount)
- func (c *Cache) PutUser(u *userTY.User)
- func (c *Cache) WarmPolicies() error
- type RequestAccess
- type Subject
Constants ¶
const QuickIDParam = "quick_id"
QuickIDParam matches routes/metric.go
Variables ¶
Functions ¶
func BuiltInPolicies ¶
BuiltInPolicies returns system policies that should always exist.
func BusinessName ¶
BusinessName returns the policy resource name for an entity (not storage UUID when a path exists).
func FormatResource ¶
FormatResource builds "kind" or "kind:name".
func IsBuiltInPolicyID ¶
IsBuiltInPolicyID reports whether id is a code-owned system policy.
func ItemAllowedForList ¶
ItemAllowedForList reports whether one item's business name matches list patterns. Patterns may include "!" deny exclusions from ResourceNamesForList.
func MatchAction ¶
MatchAction returns true if pattern matches action. "*" matches any action; otherwise an exact match, ignoring case and surrounding space so a hand written or UI supplied "Get" behaves like "get". Actions are a closed vocabulary, so folding case cannot widen access to a different verb.
Resource *names* stay case sensitive: they are entity ids, compared exactly by storage, and folding them could merge two distinct entities.
func MatchResource ¶
MatchResource returns true if pattern matches resource (same kind only). Resource format: "kind", "kind:name", or "*". For device-tree parent→child cascade (Allow or Deny), use the Cascade helpers.
func MatchResourceAllowCascade ¶
MatchResourceAllowCascade matches an Allow pattern against a resource. Device-tree parent allows grant children under the same path:
Allow node:gw.n → field:gw.n.s.f, source:gw.n.s, metric:gw.n… Allow gateway:gw → node/source/field/metric under gw Allow node:gw.* → all sources/fields under that gateway
Named parent Allow also permits the child list API (filters scope rows). Kind-wide Allow on an ancestor (node / node:*) grants all of the child kind.
func MatchResourceDenyCascade ¶
MatchResourceDenyCascade matches a Deny pattern against a resource. Device-tree parent denies block children under the same path (same hierarchy as Allow). Named Deny does not block the kind-only collection resource (list entry check); list rows are filtered separately.
func ResourceFromQuickID ¶
ResourceFromQuickID maps a quick id (e.g. field:gw.n.s.f) to a policy resource string.
Types ¶
type API ¶
type API struct {
// contains filtered or unexported fields
}
func (*API) Allowed ¶
Allowed reports whether the subject may perform action on resource. resource should be FormatResource(kind, name) e.g. "field:gw.n.s.f" or "gateway" for list-all.
Rules:
- User must exist and not be disabled
- If service account: must exist, not expired, belong to user
- User policies must allow (ceiling)
- If token has restrictions, they must also allow (can only lower)
func (*API) AllowedKindWide ¶
AllowedKindWide reports whether the subject may perform action on *any* object of kind, i.e. it holds a kind-wide grant ("*", "kind" or "kind:*"), not merely a grant on some named object.
Needed for writes whose target cannot be named in advance - creating an object with a server generated id. Without this, "Allow update on user:self" would be enough to create new users, because a kind-only resource check is deliberately permissive (it answers "may you reach this collection endpoint?").
func (*API) AllowedMetricPath ¶
AllowedMetricPath checks get on metric:<fieldPath> (hierarchical match).
func (*API) AssignAdminToUsersWithoutPolicies ¶
AssignAdminToUsersWithoutPolicies grants the admin policy to users that have none. Only for the pre-RBAC migration, where every existing user was effectively an admin.
func (*API) AuthorizeActionRequest ¶
AuthorizeActionRequest enforces access for the /api/action* endpoints.
These endpoints take their targets from the query string or the request body, so a bare "action" resource in a policy must not be enough - every target is checked individually (like quickid), otherwise /api/action becomes a write channel into every field/node in the system.
GET /api/action/node?id=<uuid>&id=<uuid> -> action on node:<gatewayId.nodeId> for each id
GET /api/action/gateway?id=<id> -> action on gateway:<id> for each id
GET /api/action?resource=<quickId> -> action on the quick id target
POST /api/action [{resource: <quickId>}] -> action on every quick id in the body
Any denied target fails the whole request.
func (*API) AuthorizeBodyTargets ¶
AuthorizeBodyTargets authorizes the objects named in a write payload.
The path-based check in the middleware can only see the kind for collection endpoints - POST /api/gateway, POST /api/gateway/enable and DELETE /api/gateway all carry their targets in the body. A kind-only check is deliberately permissive (it answers "may you reach this endpoint?"), so without this second pass a grant on one named object would authorize writes to every object of that kind.
Two payload shapes cover the whole api:
{"id": "gw1", ...} one entity -> check kind:gw1
["gw1", "gw2"] bulk ids -> check kind:gw1 and kind:gw2
A payload that names no target (create with a server generated id) requires a kind-wide grant instead. Anything else falls back to the kind-level decision already made by the caller.
The body is restored so the handler can read it.
func (*API) AuthorizeMetricRequest ¶
AuthorizeMetricRequest enforces metric access for GET (quick_id) or POST (body queries). Resource patterns use kind "metric" with the same hierarchical names as fields:
metric:* all metrics metric:home-gw.* all fields under gateway metric:home-gw.living-room.* all under node metric:home-gw.n.s.* all under source metric:home-gw.n.s.temperature one field
Built-in policies that list bare "metric" still allow all (kind-only match).
func (*API) AuthorizeQuickIDRequest ¶
AuthorizeQuickIDRequest enforces access for GET /api/quickid?id=... Bare "quickid" in a policy only opens the API; each requested id is checked against the underlying resource (field:/node:/…), including device-tree cascade. Any denied id fails the whole request (403).
func (*API) AuthorizeSleepingQueueRequest ¶
AuthorizeSleepingQueueRequest checks the gateway/node named in the query. A kind-level grant on "gateway" must not be enough to read or clear another gateway's queue.
func (*API) Delete ¶
Delete removes policies. System policies are protected, and so are policies still attached to a user - detaching them silently would leave that user with an unresolvable policy id, and a user whose list becomes empty loses all access.
func (*API) EnsureBuiltInPolicies ¶
EnsureBuiltInPolicies creates system policies if missing and warms cache. Built-in statements are reset on every start: they are code, not user data.
func (*API) EnsureServiceAccountActive ¶
EnsureServiceAccountActive validates service account still valid for requests.
func (*API) EnsureUserActive ¶
EnsureUserActive loads user from cache and verifies not disabled (for auth middleware).
func (*API) GetEntityInterface ¶
func (a *API) GetEntityInterface() interface{}
func (*API) NotifyTokenDeleted ¶
NotifyTokenDeleted invalidates token cache.
func (*API) NotifyTokenUpdated ¶
func (a *API) NotifyTokenUpdated(token *svcAccountTY.ServiceAccount)
NotifyTokenUpdated refreshes token cache.
func (*API) NotifyUserDeleted ¶
NotifyUserDeleted removes user from cache.
func (*API) NotifyUserUpdated ¶
NotifyUserUpdated refreshes user cache after user write.
func (*API) ReportUsersWithoutPolicies ¶
ReportUsersWithoutPolicies logs users that cannot access anything and warms the user cache. It deliberately does not grant anything: an empty policy list means "no access", and silently promoting such a user to admin on every restart would undo an administrator's decision. Pre-RBAC installs are handled once by the 2.2.0-1 upgrade (see AssignAdminToUsersWithoutPolicies).
func (*API) ResolveBusinessName ¶
ResolveBusinessName loads the entity by storage id and returns the policy resource name.
func (*API) ResolveResource ¶
func (a *API) ResolveResource(access *RequestAccess)
ResolveResource rewrites access.Resource when path id is a storage UUID (or id) so policy match uses business names (e.g. gatewayId.nodeId) instead of UUID.
func (*API) ResourceNamesForList ¶
func (a *API) ResourceNamesForList(subject Subject, kind string) (unrestricted bool, patterns []string, err error)
ResourceNamesForList returns whether list is unrestricted for this kind, and optional name patterns from user policies (intersected with token resources). Used for filtered list queries. unrestricted=true means no name filter needed.
func (*API) Save ¶
Save persists a user authored policy.
System policies are code: their statements are rewritten on every start, so accepting edits here would silently discard them. The System flag itself is never taken from the request - a client cannot mark a custom policy undeletable or create a policy with a built-in id.
func (*API) StorageFiltersForList ¶
func (a *API) StorageFiltersForList(subject Subject, kind string) (unrestricted bool, filters []storageTY.Filter, err error)
StorageFiltersForList builds storage filters that enforce list resource patterns at query time (AND-ed with client filters). unrestricted means no extra filters. Patterns prefixed with "!" are Deny exclusions; others are Allow includes. Final query: (allow1 OR allow2 OR ...) AND (not deny1) AND (not deny2) ...
func (*API) ValidatePoliciesExist ¶
ValidatePoliciesExist ensures all policy ids exist.
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache holds users, policies, and service accounts in memory for fast auth checks. Call Invalidate* after any write so the next request reloads from storage.
func (*Cache) GetToken ¶
func (c *Cache) GetToken(tokenID string) (*svcAccountTY.ServiceAccount, error)
GetToken returns a cached service account by raw token id (Token.ID), or loads it.
func (*Cache) InvalidatePolicy ¶
InvalidatePolicy drops a policy from cache.
func (*Cache) InvalidateToken ¶
InvalidateToken drops a service account from cache by Token.ID.
func (*Cache) InvalidateTokenByEntityID ¶
InvalidateTokenByEntityID removes any cached token matching entity id.
func (*Cache) InvalidateUser ¶
InvalidateUser drops a user from cache (call on delete).
func (*Cache) PutToken ¶
func (c *Cache) PutToken(t *svcAccountTY.ServiceAccount)
PutToken updates the cache after a write (keyed by Token.ID).
func (*Cache) WarmPolicies ¶
WarmPolicies loads all policies into cache (startup).
type RequestAccess ¶
type RequestAccess struct {
Action string
Resource string // kind or kind:name
Kind string
Name string // path id or empty (may be UUID - fine for coarse check; handlers may re-check)
Skip bool // if true, authorization middleware skips (auth routes already public)
}
RequestAccess maps an HTTP request to action + resource for authorization. name is empty for collection list/create; filled for path id when available.
func MapRequest ¶
func MapRequest(r *http.Request) RequestAccess
MapRequest converts HTTP method + path to RBAC action/resource.