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 ¶
- Variables
- func Migrate(ctx context.Context, conn string) error
- func OpenAuthenticator(ctx context.Context, cfg config.Config) (*app.Authenticator, func(), error)
- func ParseConfig(conn string) (*pgxpool.Config, error)
- type Keys
- func (k *Keys) Close()
- func (k *Keys) CreateKey(ctx context.Context, r domain.KeyRecord) (domain.KeyRecord, error)
- func (k *Keys) FindKey(ctx context.Context, d domain.KeyDigest) (domain.KeyRecord, error)
- func (k *Keys) ListKeys(ctx context.Context) ([]domain.KeyRecord, error)
- func (k *Keys) RevokeKey(ctx context.Context, id int64, at time.Time) error
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 OpenAuthenticator ¶ added in v0.41.0
OpenAuthenticator returns the authenticator of the API keys of the configured environment, stored in the configured database, and the function closing that database. The database is not reached until a key is checked; a valid key is then kept in memory for app.KeyCacheTTL.
func ParseConfig ¶
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 ¶
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.