sqlstore

package
v0.10.7 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package sqlstore persists administrative principals, token digests and policies in SQL.

Design

SQLite, PostgreSQL and MySQL share the adminauth.Store contract and a separate adminauth_schema_migrations set. Credential digests support lookup, rotation and revocation without storing raw API tokens. Policies survive process restarts and use versions for cache invalidation. Records are not sealed with a keyring, so database authorization and backups remain deployment responsibilities.

References

Index

Constants

View Source
const MigrationsTable = "adminauth_schema_migrations"

MigrationsTable records the applied versions of the admin schema, separate from the storage, DDM, DEP, and ACME tables so the version sequences never mix.

Variables

View Source
var ErrUnsupportedDialect = errors.New("sqlstore: unsupported dialect")

ErrUnsupportedDialect is returned for a dialect without embedded migrations.

Functions

func Migrate

func Migrate(ctx context.Context, db *sql.DB, d sqlcommon.Dialect) ([]int, error)

Migrate applies every pending admin migration and returns the versions applied.

func MigrationSet

func MigrationSet(d sqlcommon.Dialect) (sqlcommon.MigrationSet, error)

MigrationSet returns the admin migrations for the dialect.

func Rollback

func Rollback(ctx context.Context, db *sql.DB, d sqlcommon.Dialect, target int) ([]int, error)

Rollback reverts admin migrations newer than target (0 reverts all).

func Version

func Version(ctx context.Context, db *sql.DB, d sqlcommon.Dialect) (int, error)

Version returns the highest applied admin migration (0 when none).

Types

type Options

type Options struct {
	// SkipMigrate leaves the schema alone; the caller has run Migrate.
	SkipMigrate bool
}

Options tune Open.

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store implements adminauth.Store over a *sql.DB it does not own: closing the pool is the caller's job.

func Open

func Open(ctx context.Context, db *sql.DB, d sqlcommon.Dialect, o Options) (*Store, error)

Open wraps an opened pool for the dialect and, unless o.SkipMigrate, applies pending migrations.

func (*Store) ApplyPrincipal

func (s *Store) ApplyPrincipal(
	ctx context.Context,
	name string,
	change adminauth.PrincipalChange,
	now time.Time,
) (adminauth.Principal, error)

ApplyPrincipal uses the existing singleton row to serialize lifecycle changes across connections, before any snapshot reads. It does not bump policy version.

func (*Store) BootstrapPrincipal added in v0.10.0

func (s *Store) BootstrapPrincipal(ctx context.Context, p adminauth.Principal, digest string, now time.Time) (adminauth.Principal, error)

BootstrapPrincipal atomically installs the first active root credential and closes bootstrap. It requires an empty, uninitialized principal store and receives only the credential digest.

func (*Store) CountRoot

func (s *Store) CountRoot(ctx context.Context) (int, error)

CountRoot implements adminauth.Store.

func (*Store) CreatePrincipal

func (s *Store) CreatePrincipal(ctx context.Context, p adminauth.Principal, digest string, now time.Time) (adminauth.Principal, error)

CreatePrincipal implements adminauth.Store.

func (*Store) DB

func (s *Store) DB() *sql.DB

DB exposes the pool for health checks and tests.

func (*Store) DeletePolicy

func (s *Store) DeletePolicy(ctx context.Context, name string) error

DeletePolicy implements adminauth.Store.

func (*Store) DeletePrincipal

func (s *Store) DeletePrincipal(ctx context.Context, name string) error

DeletePrincipal implements adminauth.Store.

func (*Store) DeleteRole added in v0.10.0

func (s *Store) DeleteRole(ctx context.Context, name string) error

DeleteRole removes an unreferenced role. Principal membership or a policy reference returns ErrConflict; an absent role returns ErrNotFound.

func (*Store) GetPolicy

func (s *Store) GetPolicy(ctx context.Context, name string) (adminauth.Policy, error)

GetPolicy implements adminauth.Store.

func (*Store) Initialized added in v0.10.0

func (s *Store) Initialized(ctx context.Context) (bool, error)

Initialized reports whether principal initialization has permanently consumed the bootstrap opportunity.

func (*Store) Policies

func (s *Store) Policies(ctx context.Context) ([]adminauth.Policy, error)

Policies implements adminauth.Store, ordered by name.

func (*Store) PolicyVersion

func (s *Store) PolicyVersion(ctx context.Context) (int64, error)

PolicyVersion implements adminauth.Store.

func (*Store) Principal

func (s *Store) Principal(ctx context.Context, name string) (adminauth.Principal, error)

Principal implements adminauth.Store.

func (*Store) PrincipalByDigest

func (s *Store) PrincipalByDigest(ctx context.Context, digest string) (adminauth.Principal, error)

PrincipalByDigest implements adminauth.Store. This is the authentication path: one indexed lookup on the unique digest index.

func (*Store) Principals

Principals implements adminauth.Store with a keyset cursor on name.

func (*Store) PutPolicy

func (s *Store) PutPolicy(ctx context.Context, p adminauth.Policy, now time.Time) (adminauth.Policy, error)

PutPolicy implements adminauth.Store. The write and the version bump share one transaction, so a compiled set never sees a version that does not match the policies it would read.

func (*Store) PutRole added in v0.10.0

func (s *Store) PutRole(ctx context.Context, role adminauth.Role, now time.Time) (adminauth.Role, error)

PutRole creates or updates a named role, preserving its creation time and advancing the policy version. Role membership alone grants no operational permission.

func (*Store) RevokeToken

func (s *Store) RevokeToken(ctx context.Context, name string, now time.Time) error

RevokeToken implements adminauth.Store. The digest becomes NULL rather than an empty string, so the unique index still admits many revoked rows.

func (*Store) Role added in v0.10.0

func (s *Store) Role(ctx context.Context, name string) (adminauth.Role, error)

Role returns the named managed role, or ErrNotFound when no such role exists.

func (*Store) Roles added in v0.10.0

Roles returns a page of managed roles in name order using an exclusive cursor.

func (*Store) SetToken

func (s *Store) SetToken(ctx context.Context, name, digest, tokenID string, expires, now time.Time) (adminauth.Principal, error)

SetToken implements adminauth.Store, replacing the current digest so the previous token stops working at once.

func (*Store) UpdatePrincipal

func (s *Store) UpdatePrincipal(ctx context.Context, name string, roles []string, root bool, now time.Time) (adminauth.Principal, error)

UpdatePrincipal implements adminauth.Store.

Jump to

Keyboard shortcuts

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