Documentation
¶
Overview ¶
Package basicperms provides a self-contained set of HTTP handlers for Zanzibar-style relationship-based authorization stored in either SQLite or Postgres.
Typical usage:
store, _ := basicperms.Open(ctx, "postgres://...", "perms")
defer store.Close()
svc := basicperms.NewService(store, nil)
mux.Mount("/perms", svc.Router())
Index ¶
- Constants
- Variables
- type CheckResponse
- type CreateNamespaceInput
- type CreateObjectInput
- type CreateRelationInput
- type CreateRelationRuleInput
- type CreateSubjectInput
- type Dialect
- type ErrorResponse
- type ExpandNode
- type Namespace
- type NodeType
- type Object
- type Relation
- type RelationFilter
- type RelationRule
- type RuleType
- type Service
- type Store
- func (s *Store) CheckKey(ctx context.Context, key string) (bool, error)
- func (s *Store) CheckRelation(ctx context.Context, namespace, subject, relation, object string) (bool, error)
- func (s *Store) Close() error
- func (s *Store) CreateKey(ctx context.Context, key string) error
- func (s *Store) CreateNamespace(ctx context.Context, name string) (*Namespace, error)
- func (s *Store) CreateObject(ctx context.Context, namespace, id string) (*Object, error)
- func (s *Store) CreateRelation(ctx context.Context, namespace, subject, relation, object string) (*Relation, error)
- func (s *Store) CreateRelationRule(ctx context.Context, in CreateRelationRuleInput) (*RelationRule, error)
- func (s *Store) CreateSubject(ctx context.Context, namespace, id string) (*Subject, error)
- func (s *Store) DeleteKey(ctx context.Context, key string) error
- func (s *Store) DeleteNamespace(ctx context.Context, name string) error
- func (s *Store) DeleteObject(ctx context.Context, namespace, id string) error
- func (s *Store) DeleteRelation(ctx context.Context, namespace, subject, relation, object string) error
- func (s *Store) DeleteRelationRule(ctx context.Context, namespace, relation string, ruleType RuleType, ...) error
- func (s *Store) DeleteSubject(ctx context.Context, namespace, id string) error
- func (s *Store) ExpandRelation(ctx context.Context, namespace, object, relation string) (ExpandNode, error)
- func (s *Store) ListNamespaces(ctx context.Context, q string) ([]Namespace, error)
- func (s *Store) ListRelationRules(ctx context.Context, namespace, relation string) ([]RelationRule, error)
- func (s *Store) ListRelations(ctx context.Context, f RelationFilter) ([]Relation, error)
- func (s *Store) SeedKey(ctx context.Context, key string) error
- type Subject
- type Tuple
Constants ¶
const DefaultSchema = "perms"
DefaultSchema is the default Postgres schema name when none is supplied. Ignored for SQLite.
Variables ¶
Functions ¶
This section is empty.
Types ¶
type CheckResponse ¶
type CheckResponse struct {
Allowed bool `json:"allowed"`
}
CheckResponse is the response body for GET /check.
type CreateNamespaceInput ¶
type CreateNamespaceInput struct {
Name string `json:"name"`
}
func (CreateNamespaceInput) Validate ¶
func (i CreateNamespaceInput) Validate() error
type CreateObjectInput ¶
func (CreateObjectInput) Validate ¶
func (i CreateObjectInput) Validate() error
type CreateRelationInput ¶
type CreateRelationInput struct {
Namespace string `json:"namespace"`
Subject string `json:"subject"`
Relation string `json:"relation"`
Object string `json:"object"`
}
func (CreateRelationInput) Validate ¶
func (i CreateRelationInput) Validate() error
type CreateRelationRuleInput ¶
type CreateRelationRuleInput struct {
Namespace string `json:"namespace"`
Relation string `json:"relation"`
RuleType RuleType `json:"rule_type"`
Arg1 string `json:"arg1"`
Arg2 string `json:"arg2,omitempty"`
}
CreateRelationRuleInput is the request body for POST /admin/rule.
func (CreateRelationRuleInput) Validate ¶
func (i CreateRelationRuleInput) Validate() error
type CreateSubjectInput ¶
func (CreateSubjectInput) Validate ¶
func (i CreateSubjectInput) Validate() error
type ErrorResponse ¶
type ErrorResponse struct {
Message string `json:"message"`
}
ErrorResponse is the JSON shape for all error responses.
type ExpandNode ¶
type ExpandNode struct {
Tuple Tuple `json:"tuple"`
Type NodeType `json:"type"`
Children []ExpandNode `json:"children"`
}
ExpandNode is a node in the expand result tree. Leaf nodes (type 0) have no meaningful children.
type Namespace ¶
type Namespace struct {
Name string `json:"name"`
}
Namespace is a logical permission domain.
type Relation ¶
type Relation struct {
Namespace string `json:"namespace"`
Subject string `json:"subject"`
Relation string `json:"relation"`
Object string `json:"object"`
}
Relation is a subject-relation-object tuple within a namespace.
type RelationFilter ¶
RelationFilter holds optional filters for ListRelations. Empty string means no filter on that field.
type RelationRule ¶
type RelationRule struct {
Namespace string `json:"namespace"`
Relation string `json:"relation"`
RuleType RuleType `json:"rule_type"`
Arg1 string `json:"arg1"`
Arg2 string `json:"arg2,omitempty"`
}
RelationRule is a userset rewrite rule that extends a relation with derived membership computed from other relations.
type RuleType ¶
type RuleType string
RuleType identifies the kind of userset rewrite a RelationRule applies.
const ( // RuleTypeComputedUserset grants access to any subject that holds Arg1 // on the same object. RuleTypeComputedUserset RuleType = "computed_userset" // RuleTypeTupleToUserset grants access to any subject that holds Arg2 on // the object reached by following Arg1 from the current object. RuleTypeTupleToUserset RuleType = "tuple_to_userset" )
type Service ¶
type Service struct {
Store *Store
Logger *slog.Logger
// MountPrefix is the path prefix the service is mounted under (e.g.
// "/perms"). Used for informational purposes; the empty string means root.
MountPrefix string
// RequireAuth enables Basic-auth enforcement on every request. When true,
// callers must supply a valid key (stored in the key table) as the HTTP
// Basic password. Defaults to false so the embedded library mode inside a
// host application remains unaffected.
RequireAuth bool
}
Service binds a Store to the HTTP handlers.
func NewService ¶
NewService constructs a Service with defaults for optional fields. Pass nil for logger to use slog.Default().
type Store ¶
Store wraps a *sql.DB and tracks the dialect and (for Postgres) the schema.
func (*Store) CheckRelation ¶
func (s *Store) CheckRelation(ctx context.Context, namespace, subject, relation, object string) (bool, error)
CheckRelation reports whether subject holds relation on object within namespace. Stored wildcard ("*") values match any concrete value. Userset rewrite rules (computed_userset, tuple_to_userset) are followed transitively with cycle detection.
func (*Store) CreateNamespace ¶
CreateNamespace inserts a new namespace. Returns ErrDuplicate if the name already exists.
func (*Store) CreateObject ¶
CreateObject inserts a new object into the given namespace. Returns ErrDuplicate if the (namespace, id) pair already exists, or ErrForeignKey if the namespace does not exist.
func (*Store) CreateRelation ¶
func (s *Store) CreateRelation(ctx context.Context, namespace, subject, relation, object string) (*Relation, error)
CreateRelation inserts a new relation tuple. Returns ErrDuplicate if the tuple already exists, or ErrForeignKey if the namespace does not exist.
func (*Store) CreateRelationRule ¶
func (s *Store) CreateRelationRule(ctx context.Context, in CreateRelationRuleInput) (*RelationRule, error)
CreateRelationRule adds a userset rewrite rule. Returns ErrDuplicate if the rule already exists, ErrForeignKey if the namespace does not exist.
func (*Store) CreateSubject ¶
CreateSubject inserts a new subject into the given namespace. Returns ErrDuplicate if the (namespace, id) pair already exists, or ErrForeignKey if the namespace does not exist.
func (*Store) DeleteNamespace ¶
DeleteNamespace removes the namespace (and via FK cascade, its subjects, objects, and relations). Returns ErrNotFound if it does not exist.
func (*Store) DeleteObject ¶
DeleteObject removes the object. Returns ErrNotFound if it does not exist.
func (*Store) DeleteRelation ¶
func (s *Store) DeleteRelation(ctx context.Context, namespace, subject, relation, object string) error
DeleteRelation removes the relation tuple. Returns ErrNotFound if it does not exist.
func (*Store) DeleteRelationRule ¶
func (s *Store) DeleteRelationRule(ctx context.Context, namespace, relation string, ruleType RuleType, arg1, arg2 string) error
DeleteRelationRule removes a rule. Returns ErrNotFound if it does not exist.
func (*Store) DeleteSubject ¶
DeleteSubject removes the subject. Returns ErrNotFound if it does not exist.
func (*Store) ExpandRelation ¶
func (s *Store) ExpandRelation(ctx context.Context, namespace, object, relation string) (ExpandNode, error)
ExpandRelation returns a union tree of all subjects who hold the given relation to the given object within the namespace, following userset rewrite rules transitively. The root is a union node; its children are leaf nodes (one per unique subject found, deduplicated across all rule paths).
func (*Store) ListNamespaces ¶
ListNamespaces returns all namespaces whose name contains q (case-insensitive substring match). An empty q returns all namespaces.
func (*Store) ListRelationRules ¶
func (s *Store) ListRelationRules(ctx context.Context, namespace, relation string) ([]RelationRule, error)
ListRelationRules returns rules optionally filtered by namespace and relation.
func (*Store) ListRelations ¶
ListRelations returns all relation tuples matching the provided filter. Any zero-value filter field is ignored.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
basicperms
command
Command basicperms runs the basicperms package as a standalone HTTP server.
|
Command basicperms runs the basicperms package as a standalone HTTP server. |