postgres

package
v0.40.0 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: AGPL-3.0 Imports: 18 Imported by: 0

Documentation

Overview

Package postgres keeps the records of the API keys in PostgreSQL (ADR 0004), through jackc/pgx: their keyed digests, never the keys.

The schema is changed by versioned SQL migrations embedded in the binary and applied by Migrate, from the admin command with a role that may change it; the API, connected with a role that can only read and write the tables, never migrates. A connection beyond the local machine must verify the server's certificate and host name (sslmode=verify-full), and every query is bounded by queryTimeout on top of its context.

Index

Constants

This section is empty.

Variables

View Source
var ErrInsecure = errors.New("database connection without verified TLS")

ErrInsecure means a connection string lets a connection to a remote host go without TLS, or with TLS that does not verify the certificate and the host name: sslmode=verify-full is required beyond the local machine.

Functions

func Migrate

func Migrate(ctx context.Context, conn string) error

Migrate applies every migration not yet applied to the database of conn, one at a time under an advisory lock, so that two runs never interleave. It is run by the administration command with a role that may change the schema, never by the API on start (ADR 0004). Cancelling ctx stops it after the migration in progress.

func ParseConfig

func ParseConfig(conn string) (*pgxpool.Config, error)

ParseConfig parses a PostgreSQL connection string, URL or key=value, and requires verified TLS (sslmode=verify-full) for every host but the local machine: a loopback address, "localhost" or a Unix socket. Its errors never quote the connection string, which may hold a password.

Types

type Keys

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

Keys keeps the records of API keys in the api_keys table. It implements app.KeyFinder and app.KeyIssuer.

func Open

func Open(ctx context.Context, conn string) (*Keys, error)

Open returns a Keys on the database of conn, which ParseConfig checks. It does not connect: the first query does, so the API starts and answers 503 while the database is unreachable.

func (*Keys) Close

func (k *Keys) Close()

Close closes every connection, waiting for those in use.

func (*Keys) CreateKey

func (k *Keys) CreateKey(ctx context.Context, r domain.KeyRecord) (domain.KeyRecord, error)

CreateKey stores r and returns it with its identifier set.

func (*Keys) FindKey

func (k *Keys) FindKey(ctx context.Context, d domain.KeyDigest) (domain.KeyRecord, error)

FindKey returns the record of the key of digest d, or app.ErrKeyNotFound.

func (*Keys) ListKeys

func (k *Keys) ListKeys(ctx context.Context) ([]domain.KeyRecord, error)

ListKeys returns every record, oldest first.

func (*Keys) RevokeKey

func (k *Keys) RevokeKey(ctx context.Context, id int64, at time.Time) error

RevokeKey marks the key id revoked at at, or returns app.ErrKeyNotFound. A key already revoked keeps its first revocation time.

Jump to

Keyboard shortcuts

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