sqlstore

package
v0.9.2 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 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) 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) GetPolicy

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

GetPolicy implements adminauth.Store.

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) 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) 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