accessapitest

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 16, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package accessapitest provides hand-written fakes of every access/api contract, for tests that need access's answers without a store or the access module: a configurable Decider, Authenticator and Authorizer, a Tokens that records its calls, map-backed Keys, Identity, Projects and AnonymousAccess, and scripted Grants and Restrictions.

Role

access module, test support for the api layer. It imports only access/api, kernel and the standard library, as an api package does (decision 0044, dependency rule 5), so a module's tests reach access through it without importing access internals, which any test may (rule 1). A test that needs access's real answers over a store builds the real module with the access test kit, access/accesstest, instead.

Inputs

The answers a test puts in each fake's fields: values, maps, and functions with the signature of the method they answer.

Outputs

  • Decider, Authenticator and Authorizer: configurable. Authorizer evaluates through DecideFunc and returns a Decider, by default an empty one.
  • Tokens, with TokenCalls: records the arguments of every call, and by default issues numbered tokens.
  • Keys, Identity, Projects, with Project, and AnonymousAccess: map-backed.
  • Grants and Restrictions: scripted, for a scan's outcome or a user's restriction.

Invariants

  • Every fake is used by pointer. A Decider value holds functions, so an interface holding one would panic when compared, and Tokens and AnonymousAccess hold a lock.
  • A field left nil answers as access does when it has nothing to say: no credential verifies, nothing is held, nothing changes access, no one is restricted, and a record that is not there matches kernel.ErrNotFound. A recheck (Authenticator.Current and CurrentRegistry) returns the principal unchanged.
  • The contract's error texts hold where a contract states them: Authorizer wraps an evaluation error as "auth: permissions: %w" in Authorize and "evaluate permissions: %w" in For, and returns it unwrapped from Decide. The fakes' own not-found texts are not the store's; a test matches them with errors.Is, never by text.
  • A fake returns copies of the slices it holds, so a caller that changes one changes nothing in the fake. Tokens and AnonymousAccess, which change state, are safe for concurrent use; the other fakes only read their fields.

References

  • Decision 0044: docs/research/decisions/0044-modular-monolith-migration.md, "Dependency rules" and "Names the migration introduces".
  • Plan: docs/implementation/plan.md#p9d-modular-monolith-migration, "ACC tickets (access module)": consumers' tests use these fakes or access.New over the in-memory store.
  • https://github.com/weaveplatform/artifactweave/issues/966

Status

Implemented P9d.

Testing

  • TestUnitDecider: the zero value refuses everything, an administrator is allowed what no function narrows, functions answer when set, and a question for no action is refused. VisibleRepos follows Admin without its function.
  • TestUnitDeciderAllowsAll: AllowsAllFunc answers when set; without it, an administrator is allowed every path only when neither AllowsFunc nor AllowsFolderFunc narrows a path, so a test that scripts path answers keeps its lists on the path-by-path filter.
  • TestUnitAuthenticator, TestUnitAuthorizer: the defaults, the functions, and Authorizer's error wraps and nil Decider on error.
  • TestUnitTokens: the recorded calls, the numbered tokens and the functions, under concurrent use.
  • TestUnitKeys, TestUnitIdentity, TestUnitProjects, TestUnitAnonymousAccess: the maps, kernel.ErrNotFound for what is not there, Err, and the copies.
  • TestUnitGrants, TestUnitRestrictions: the defaults and the scripts.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AnonymousAccess

type AnonymousAccess struct {
	// On is the setting.
	On bool
	// Unwritten makes Enabled answer as for a setting never written: an
	// error matching kernel.ErrNotFound, which callers read as off.
	// SetEnabled clears it.
	Unwritten bool
	// Err, when set, is every call's error, and SetEnabled then changes
	// nothing, as when the store cannot be read or written.
	Err error
	// contains filtered or unexported fields
}

AnonymousAccess is an AnonymousAccess over one setting held in memory. The zero value is a setting written as off. It is safe for concurrent use; set its fields before sharing it, and read the setting back with Enabled.

func (*AnonymousAccess) Enabled

func (a *AnonymousAccess) Enabled(context.Context) (bool, error)

Enabled returns On; Err, when set; or, while Unwritten, an error matching kernel.ErrNotFound.

func (*AnonymousAccess) SetEnabled

func (a *AnonymousAccess) SetEnabled(_ context.Context, on bool) error

SetEnabled sets On to on and clears Unwritten, or returns Err, when set, and changes nothing.

type Authenticator

type Authenticator struct {
	// BasicFunc answers Basic.
	BasicFunc func(ctx context.Context, name, password string) (kernel.Principal, error)
	// TokenFunc answers Token.
	TokenFunc func(ctx context.Context, raw, user string) (kernel.Principal, error)
	// AnonymousAllowedFunc answers AnonymousAllowed.
	AnonymousAllowedFunc func(ctx context.Context) (bool, error)
	// CurrentFunc answers Current.
	CurrentFunc func(ctx context.Context, p kernel.Principal) (kernel.Principal, error)
	// CurrentRegistryFunc answers CurrentRegistry.
	CurrentRegistryFunc func(ctx context.Context, p kernel.Principal) (kernel.Principal, error)
	// RestoreDownloadFunc answers RestoreDownload.
	RestoreDownloadFunc func(
		ctx context.Context,
		name string,
		ref kernel.DownloadCredential,
	) (kernel.Principal, error)
}

Authenticator is an Authenticator whose function fields set its answers, each with the signature of the method it answers. Where a field is nil it answers as an authenticator that verifies no credential and finds nothing changed since it verified one: Basic, Token and RestoreDownload answer kernel.ErrBadCredentials, AnonymousAllowed answers false, as a setting that was never written does, and Current and CurrentRegistry return the principal they are given. It holds no state, so it may be used from several goroutines as long as its functions may.

func (*Authenticator) AnonymousAllowed

func (a *Authenticator) AnonymousAllowed(ctx context.Context) (bool, error)

AnonymousAllowed answers with AnonymousAllowedFunc, or false when it is nil.

func (*Authenticator) Basic

func (a *Authenticator) Basic(
	ctx context.Context,
	name, password string,
) (kernel.Principal, error)

Basic answers with BasicFunc, or kernel.ErrBadCredentials when it is nil.

func (*Authenticator) Current

Current answers with CurrentFunc, or returns p when it is nil.

func (*Authenticator) CurrentRegistry

func (a *Authenticator) CurrentRegistry(
	ctx context.Context,
	p kernel.Principal,
) (kernel.Principal, error)

CurrentRegistry answers with CurrentRegistryFunc, or returns p when it is nil.

func (*Authenticator) RestoreDownload

func (a *Authenticator) RestoreDownload(
	ctx context.Context,
	name string,
	ref kernel.DownloadCredential,
) (kernel.Principal, error)

RestoreDownload answers with RestoreDownloadFunc, or kernel.ErrBadCredentials when it is nil.

func (*Authenticator) Token

func (a *Authenticator) Token(ctx context.Context, raw, user string) (kernel.Principal, error)

Token answers with TokenFunc, or kernel.ErrBadCredentials when it is nil.

type Authorizer

type Authorizer struct {
	// DecideFunc evaluates s, with the anonymous user's grants applying only
	// while anonymousEnabled holds, and returns a Decider or an error. Nil
	// answers every subject with an empty *Decider, which holds nothing.
	DecideFunc func(
		ctx context.Context,
		s accessapi.Subject,
		anonymousEnabled bool,
	) (accessapi.Decider, error)
	// AnonymousEnabled is the anonymous access setting that For reads.
	AnonymousEnabled bool
}

Authorizer is an Authorizer whose evaluations DecideFunc answers: Authorize and For evaluate the subject that accessapi.SubjectOf builds from the principal, and Decide the subject it is given. The contract's error wraps apply, so a test reads the texts the access module gives. It holds no state, so it may be used from several goroutines as long as DecideFunc may.

func (*Authorizer) Authorize

func (a *Authorizer) Authorize(
	ctx context.Context,
	p kernel.Principal,
	anonymousEnabled bool,
) (kernel.Principal, accessapi.Decider, error)

Authorize evaluates p and returns it with Admin and Groups set from the Decider, with the Decider, as the access module does. An evaluation error is wrapped as "auth: permissions: %w" and returned with the zero principal and a nil Decider.

func (*Authorizer) Decide

func (a *Authorizer) Decide(
	ctx context.Context,
	s accessapi.Subject,
	anonymousEnabled bool,
) (accessapi.Decider, error)

Decide evaluates s. An evaluation error is returned unwrapped, for the caller to wrap, with a nil Decider.

func (*Authorizer) For

For evaluates p with AnonymousEnabled as the anonymous access setting. An evaluation error is wrapped as "evaluate permissions: %w" and returned with a nil Decider.

type Decider

type Decider struct {
	// Admin is IsAdmin's answer, and the answer to every question whose
	// function field is nil.
	Admin bool
	// GroupNames is what Groups returns, copied.
	GroupNames []string
	// IsRepoVisibleFunc answers IsRepoVisible.
	IsRepoVisibleFunc func(repo accessapi.Repo) bool
	// VisibleReposFunc answers VisibleRepos. A test that sets
	// IsRepoVisibleFunc and lists repositories sets it too, to the same
	// set: a listing filters in its store by VisibleRepos, never asking
	// IsRepoVisible about each repository.
	VisibleReposFunc func() accessapi.RepoVisibility
	// AllowsFunc answers Allows for a question that asks for some action.
	AllowsFunc func(
		rt accessapi.ResourceType,
		repo accessapi.Repo,
		path string,
		want accessapi.Actions,
	) bool
	// AllowsFolderFunc answers AllowsFolder for a question that asks for
	// some action.
	AllowsFolderFunc func(
		rt accessapi.ResourceType,
		repo accessapi.Repo,
		path string,
		want accessapi.Actions,
	) bool
	// AllowsAllFunc answers AllowsAll for a question that asks for some
	// action.
	AllowsAllFunc func(
		rt accessapi.ResourceType,
		repo accessapi.Repo,
		folder string,
		want accessapi.Actions,
	) bool
	// SystemFunc answers System.
	SystemFunc func(area string, want kernel.ScopeActions) bool
}

Decider is a Decider whose fields set its answers. The zero value is a subject that holds nothing: no administrator, in no group, seeing no repository and allowed nothing. A question whose function field is nil is answered with Admin, so an administrator is allowed whatever no function narrows, as the evaluator allows an administrator whose token scope does not narrow it; AllowsAll is the exception, as its comment says. Allows, AllowsFolder and AllowsAll refuse a question that asks for no action before they consult a field, as every Decider does.

Use a *Decider: a Decider value holds functions, and an interface holding such a value panics when compared, as tests compare the decider a context carries. Its methods only read its fields, so once built it may be used from several goroutines, as long as its functions may.

func (*Decider) Allows

func (d *Decider) Allows(
	rt accessapi.ResourceType,
	repo accessapi.Repo,
	path string,
	want accessapi.Actions,
) bool

Allows refuses a question that asks for no action, and answers any other with AllowsFunc, or with Admin when it is nil.

func (*Decider) AllowsAll

func (d *Decider) AllowsAll(
	rt accessapi.ResourceType,
	repo accessapi.Repo,
	folder string,
	want accessapi.Actions,
) bool

AllowsAll refuses a question that asks for no action, and answers any other with AllowsAllFunc. When it is nil, it answers true only for an administrator whose Allows and AllowsFolder no function narrows: a fake that narrows them without saying what it allows as a whole is taken to narrow every folder, so a list asks it path by path, as it would ask an evaluator whose permissions name paths.

func (*Decider) AllowsFolder

func (d *Decider) AllowsFolder(
	rt accessapi.ResourceType,
	repo accessapi.Repo,
	path string,
	want accessapi.Actions,
) bool

AllowsFolder refuses a question that asks for no action, and answers any other with AllowsFolderFunc, or with Admin when it is nil. It does not fall back to AllowsFunc: the evaluator also admits a folder above a permitted path, which a test that asks about folders states itself.

func (*Decider) Groups

func (d *Decider) Groups() []string

Groups returns a copy of GroupNames, which the caller may change.

func (*Decider) IsAdmin

func (d *Decider) IsAdmin() bool

IsAdmin reports Admin.

func (*Decider) IsRepoVisible

func (d *Decider) IsRepoVisible(repo accessapi.Repo) bool

IsRepoVisible answers with IsRepoVisibleFunc, or with Admin when it is nil.

func (*Decider) System

func (d *Decider) System(area string, want kernel.ScopeActions) bool

System answers with SystemFunc, or with Admin when it is nil.

func (*Decider) VisibleRepos

func (d *Decider) VisibleRepos() accessapi.RepoVisibility

VisibleRepos answers with VisibleReposFunc, or, when it is nil, with every repository for Admin and none otherwise, as IsRepoVisible answers without its function.

type Grants

type Grants struct {
	// ItemGrantsFunc answers ItemGrants.
	ItemGrantsFunc func(
		ctx context.Context,
		targets []string,
		path string,
		folder bool,
	) (users, groups map[string]accessapi.Actions, err error)
	// AccessChangeFunc answers AccessChange.
	AccessChangeFunc func(
		ctx context.Context,
		repoKey, class string,
		pairs []accessapi.PathPair,
		limit int,
	) (permission string, err error)
}

Grants is a Grants whose answers its function fields script, each with the signature of the method it answers, so a test states the scan's outcome, such as accessapi.ErrScanLimit or the name of a permission that a move would change, without storing the permissions that lead to it. Where a field is nil it answers as a store without permissions: no one holds anything, and no permission changes access. It holds no state, so it may be used from several goroutines as long as its functions may.

func (*Grants) AccessChange

func (g *Grants) AccessChange(
	ctx context.Context,
	repoKey, class string,
	pairs []accessapi.PathPair,
	limit int,
) (string, error)

AccessChange answers with AccessChangeFunc, or "" when it is nil.

func (*Grants) ItemGrants

func (g *Grants) ItemGrants(
	ctx context.Context,
	targets []string,
	path string,
	folder bool,
) (map[string]accessapi.Actions, map[string]accessapi.Actions, error)

ItemGrants answers with ItemGrantsFunc, or with two empty maps when it is nil.

type Identity

type Identity struct {
	// Users maps a user name to the account.
	Users map[string]accessapi.UserInfo
	// AutoJoin is the names of the groups new users join, in order.
	AutoJoin []string
	// Err, when set, is every call's error.
	Err error
}

Identity is an Identity over a map of accounts and a list of auto-join groups, which it only reads. A name missing from Users answers an error matching kernel.ErrNotFound; Err, when set, answers every call instead, as a store that cannot be read does. The groups it returns are copies, so a caller that changes them changes nothing here. It may be used from several goroutines while no one changes its fields.

func (*Identity) AutoJoinGroups

func (i *Identity) AutoJoinGroups(context.Context) ([]string, error)

AutoJoinGroups returns a copy of AutoJoin, or Err.

func (*Identity) User

func (i *Identity) User(_ context.Context, name string) (accessapi.UserInfo, error)

User returns the account called name, Err, or an error matching kernel.ErrNotFound when Users has no such name.

type Keys

type Keys struct {
	// Keypairs maps a key pair's name to it.
	Keypairs map[string]accessapi.Keypair
	// Certificates maps a certificate's alias to it.
	Certificates map[string]accessapi.Certificate
	// Err, when set, is every call's error.
	Err error
}

Keys is a Keys over maps, which it only reads: key pairs by name and certificates by alias. A name or alias missing from its map answers an error matching kernel.ErrNotFound; Err, when set, answers every call instead, as a store that cannot be read does. It may be used from several goroutines while no one changes its fields.

func (*Keys) Certificate

func (k *Keys) Certificate(_ context.Context, alias string) (accessapi.Certificate, error)

Certificate returns the certificate with alias, Err, or an error matching kernel.ErrNotFound when Certificates has no such alias.

func (*Keys) Keypair

func (k *Keys) Keypair(_ context.Context, name string) (accessapi.Keypair, error)

Keypair returns the key pair called name, Err, or an error matching kernel.ErrNotFound when Keypairs has no such name.

type Project

type Project struct {
	// Environments are the project's environment names, in any order;
	// Projects.Environments sorts them.
	Environments []string
	// Members are the names of the users who are members of the project.
	Members []string
}

Project is one project as Projects holds it.

type Projects

type Projects struct {
	// Projects maps a project key to the project.
	Projects map[string]Project
	// Global is the global environments' names, in any order, which
	// Environments returns for project "".
	Global []string
	// Err, when set, is every call's error.
	Err error
}

Projects is a Projects over a map of projects and a list of global environments, which it only reads. A project missing from the map exists for no call: it has no environments and no members, and Exists answers an error matching kernel.ErrNotFound. Err, when set, answers every call instead, as a store that cannot be read does. It may be used from several goroutines while no one changes its fields.

func (*Projects) Environments

func (p *Projects) Environments(_ context.Context, project string) ([]string, error)

Environments returns the names of project's environments, or of the global ones for project "", sorted by name in a new slice; none for a project that does not exist. Err, when set, is returned instead.

func (*Projects) Exists

func (p *Projects) Exists(_ context.Context, project string) error

Exists returns nil when Projects holds project, Err, or an error matching kernel.ErrNotFound when it does not.

func (*Projects) Member

func (p *Projects) Member(_ context.Context, project, user string) error

Member returns nil when user is among project's Members, Err, or an error matching kernel.ErrNotFound when the user is not a member or the project does not exist.

type Restrictions

type Restrictions struct {
	// CheckFunc answers Check. Nil restricts no one.
	CheckFunc func(ctx context.Context, user string, now time.Time) error
}

Restrictions is a Restrictions whose CheckFunc scripts its answer: a test restricts a user by returning kernel.ErrAPIRestricted for them, which answers 403 "API access is temporarily restricted" as the access module's check does, and fails the check by returning any other error. It holds no state, so it may be used from several goroutines as long as CheckFunc may.

func (*Restrictions) Check

func (r *Restrictions) Check(ctx context.Context, user string, now time.Time) error

Check answers with CheckFunc, or nil when it is nil.

type TokenCalls

type TokenCalls struct {
	Issue             []accessapi.TokenRequest
	IssueRegistry     []accessapi.RegistryTokenRequest
	RevokeValue       []string
	EncryptedPassword []string
	DeleteExpired     []time.Time
}

TokenCalls is what a Tokens recorded: each method's arguments, in call order.

type Tokens

type Tokens struct {
	// IssueFunc answers Issue.
	IssueFunc func(ctx context.Context, r accessapi.TokenRequest) (accessapi.IssuedToken, error)
	// IssueRegistryFunc answers IssueRegistry.
	IssueRegistryFunc func(
		ctx context.Context,
		r accessapi.RegistryTokenRequest,
	) (accessapi.IssuedToken, error)
	// RevokeValueFunc answers RevokeValue.
	RevokeValueFunc func(ctx context.Context, raw string) error
	// EncryptedPasswordFunc answers EncryptedPassword.
	EncryptedPasswordFunc func(ctx context.Context, user string) (string, error)
	// DeleteExpiredFunc answers DeleteExpired.
	DeleteExpiredFunc func(ctx context.Context, before time.Time) (int64, error)
	// contains filtered or unexported fields
}

Tokens is a Tokens that records every call, then answers it from its function fields, each with the signature of the method it answers. Where a field is nil it answers as a token service that refuses nothing:

  • Issue and IssueRegistry return the n-th token asked for, counting both: "access-token-n" with the id "token-n", and with the request's lifetime in whole seconds. Issue adds "refresh-token-n" for a refreshable request and "reference-token-n" for one that asks for a reference token or has a purpose, as the contract says. A request without a lifetime gets 0, since a fake has no default lifetime to apply.
  • RevokeValue revokes, and DeleteExpired removes nothing.
  • EncryptedPassword returns "encrypted-password-" and the user name, the same value on every call, as the contract says.

It is safe for concurrent use. The functions run outside its lock, so one may call Calls.

func (*Tokens) Calls

func (t *Tokens) Calls() TokenCalls

Calls returns a copy of the calls recorded so far.

func (*Tokens) DeleteExpired

func (t *Tokens) DeleteExpired(ctx context.Context, before time.Time) (int64, error)

DeleteExpired records before and answers with DeleteExpiredFunc, or 0 when it is nil.

func (*Tokens) EncryptedPassword

func (t *Tokens) EncryptedPassword(ctx context.Context, user string) (string, error)

EncryptedPassword records user and answers with EncryptedPasswordFunc, or "encrypted-password-" and user when it is nil.

func (*Tokens) Issue

Issue records r and answers with IssueFunc, or with the next numbered token when it is nil.

func (*Tokens) IssueRegistry

IssueRegistry records r and answers with IssueRegistryFunc, or with the next numbered token when it is nil.

func (*Tokens) RevokeValue

func (t *Tokens) RevokeValue(ctx context.Context, raw string) error

RevokeValue records raw and answers with RevokeValueFunc, or nil when it is nil.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL