sqlstore

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package sqlstore is the SQL-backed adminauth.Store for SQLite, PostgreSQL, and MySQL.

Why

Admin credentials have to be revocable without restarting the process, and policies have to survive one, so both live in the database rather than in configuration. Fleet's API-only tokens are the counter-example this exists to avoid: they never expire and there is no way to say otherwise.

The schema is its own migration set, `adminauth_schema_migrations`, so the admin tables version independently of the MDM, DDM, DEP, and ACME sets, the same separation records 0020 and 0031 made. There is no keyring here: the only credential-shaped column holds a SHA-256 digest of a token, and a digest is not a secret. Sealing it would protect nothing and would add a strict-mode failure path on the authentication hot path.

References

  • Decision record: docs/research/decisions/0034-admin-api-and-authorization.md
  • Plan of record: docs/research/implementation_plan.md (phase 8)
  • Threat model: docs/security/threat-model.md (admin API)
  • Contract suite: adminauth/adminauthtest
  • Migration mechanics: storage/sqlcommon

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