Documentation
¶
Overview ¶
Package postgres is the Postgres-backed persistence for IAM: a hand-written pgx + pgtx + bob layer following the komeet pattern — a single *pgxpool.Pool, a pgtx transaction manager (tx.Trm) services use to run repo calls inside an ambient transaction, a ctx-aware TxDB executor, and a bob pool for typed query building.
Storage model: each record type maps to a table mirroring the queryable envelope columns (id, project_id, created_at, updated_at, plus any secondary keys) with the full domain object stored as a `data jsonb` column. Reads map pgx.ErrNoRows onto a domain not-found; unique violations onto a conflict.
Index ¶
- Variables
- func NewAuthenticator(db *DB, masterKey string) *pgAuthenticator
- func NewOutboxEmitter(ob *outbox.Outbox) *outboxEmitter
- func NewPgAccountStore(db *DB, emitter Emitter) *pgAccountStore
- func NewPgAdminAPIKeys(db *DB, emitter Emitter) *pgAdminAPIKeys
- func NewPgAdminAccessRequests(db *DB, emitter Emitter) *pgAdminAccessRequests
- func NewPgAdminApps(db *DB, emitter Emitter) *pgAdminApps
- func NewPgAdminConfig(db *DB, emitter Emitter) *pgAdminConfig
- func NewPgAdminConnections(db *DB, emitter Emitter) *pgAdminConnections
- func NewPgAdminKeys(db *DB, emitter Emitter) *pgAdminKeys
- func NewPgAdminServiceAccounts(db *DB, emitter Emitter) *pgAdminServiceAccounts
- func NewPgAdminUsers(db *DB, emitter Emitter) *pgAdminUsers
- func NewPgCoreAuth(db *DB, emitter Emitter) *pgCoreAuth
- func NewPgCoreAuthFlows(db *DB, emitter Emitter, accounts api.CoreAuthAccounts) *pgCoreAuthFlows
- func NewPgFederationConnections(db *DB, emitter Emitter) *pgFederationConnections
- func NewPgFederationRuntime(db *DB, emitter Emitter) *pgFederationRuntime
- func NewPgFederationScim(db *DB, emitter Emitter) *pgFederationScim
- func NewPgInvites(db *DB, emitter Emitter) *pgInvites
- func NewPgMFAAccounts(db *DB, emitter Emitter) *pgMFAAccounts
- func NewPgOAuthSocial(db *DB, emitter Emitter) *pgOAuthSocial
- func NewPgOIDCGrants(db *DB, emitter Emitter) *pgOIDCGrants
- func NewPgPasswordlessAccounts(db *DB, emitter Emitter) *pgPasswordlessAccounts
- func NewPgPlatform(db *DB) *pgPlatform
- func NewPgWebAuthnAccounts(db *DB, emitter Emitter) *pgWebAuthnAccounts
- type Cipher
- type ConnectOption
- type DB
- type Emitter
- type PgMachineIdentities
- func (a *PgMachineIdentities) CreateAPIKey(ctx context.Context, cmd domain.APIKeyCmd) (*domain.APIKey, string, error)
- func (a *PgMachineIdentities) CreateServiceAccount(ctx context.Context, cmd domain.ServiceAccountCmd) (*domain.ServiceAccount, error)
- func (a *PgMachineIdentities) CreateServiceAccountSecret(ctx context.Context, cmd domain.MachineIDSecretCmd) (*domain.MachineIDSecret, error)
- func (a *PgMachineIdentities) DeleteServiceAccount(ctx context.Context, projectID, serviceAccountID string) error
- func (a *PgMachineIdentities) GetServiceAccount(ctx context.Context, projectID, serviceAccountID string) (*domain.ServiceAccount, error)
- func (a *PgMachineIdentities) ListAPIKeys(ctx context.Context, projectID string) ([]*domain.APIKey, error)
- func (a *PgMachineIdentities) ListServiceAccounts(ctx context.Context, cmd domain.MachineIDServiceAccountListCmd) (*domain.MachineIDServiceAccountPage, error)
- func (a *PgMachineIdentities) MintToken(ctx context.Context, projectID, serviceAccountID string) (string, error)
- func (a *PgMachineIdentities) RevokeAPIKey(ctx context.Context, projectID, keyID string) error
- func (a *PgMachineIdentities) RevokeServiceAccountSecret(ctx context.Context, projectID, serviceAccountID, secretID string) error
- func (a *PgMachineIdentities) RotateAPIKey(ctx context.Context, projectID, keyID string) (*domain.APIKey, string, error)
- func (a *PgMachineIdentities) UpdateAPIKey(ctx context.Context, cmd domain.MachineIDAPIKeyPatchCmd) (*domain.APIKey, error)
- func (a *PgMachineIdentities) UpdateServiceAccount(ctx context.Context, cmd domain.MachineIDServiceAccountPatchCmd) (*domain.ServiceAccount, error)
- type PgOperator
- func (a *PgOperator) ApplyConfig(ctx context.Context, cmd domain.OperatorConfigCmd) (map[string]any, error)
- func (a *PgOperator) CreateEnvironment(ctx context.Context, cmd domain.EnvironmentCmd) (*domain.Environment, error)
- func (a *PgOperator) CreateProject(ctx context.Context, cmd domain.ProjectCmd) (*domain.Project, error)
- func (a *PgOperator) DeleteEnvironment(ctx context.Context, projectID, env string) error
- func (a *PgOperator) DeleteProject(ctx context.Context, projectID string, hard bool) error
- func (a *PgOperator) ExportConfig(ctx context.Context, projectID string) (map[string]any, error)
- func (a *PgOperator) GetEnvironment(ctx context.Context, projectID, env string) (*domain.Environment, error)
- func (a *PgOperator) GetFeatures(ctx context.Context, projectID string) (map[string]bool, error)
- func (a *PgOperator) GetProject(ctx context.Context, projectID string) (*domain.Project, error)
- func (a *PgOperator) ListAdminTokens(ctx context.Context, projectID string) ([]domain.OperatorAdminToken, error)
- func (a *PgOperator) ListEnvironments(ctx context.Context, projectID string) ([]domain.Environment, error)
- func (a *PgOperator) ListProjects(ctx context.Context) ([]domain.Project, error)
- func (a *PgOperator) MintAdminToken(ctx context.Context, cmd domain.OperatorAdminTokenCmd) (string, time.Time, error)
- func (a *PgOperator) PlanConfig(ctx context.Context, cmd domain.OperatorConfigCmd) (map[string]any, error)
- func (a *PgOperator) RevokeAdminToken(ctx context.Context, projectID, tokenID string) error
- func (a *PgOperator) UpdateFeatures(ctx context.Context, cmd domain.OperatorFeaturesCmd) (map[string]bool, error)
- func (a *PgOperator) UpdateProject(ctx context.Context, cmd domain.OperatorProjectPatchCmd) (*domain.Project, error)
- type Signer
- func (s *Signer) JWKS(ctx context.Context, projectID, env string) (map[string]any, error)
- func (s *Signer) Sign(ctx context.Context, projectID, env string, claims map[string]any, ...) (string, error)
- func (s *Signer) UnverifiedClaims(token string) map[string]any
- func (s *Signer) Verify(ctx context.Context, projectID, env, token string) (map[string]any, error)
- type Store
Constants ¶
This section is empty.
Variables ¶
var ( ErrNotFound error = domain.ErrNotFound ErrConflict = errors.New("conflict") )
ErrNotFound / ErrConflict are the storage-level sentinels the service layer translates to its API errors. (IAM has no proto layer; records are stored as plain JSON in the `data` column.)
ErrNotFound aliases domain.ErrNotFound so that an adapter which surfaces a no-rows result (directly or wrapped via translatePgErr) renders as a 404 at the API boundary instead of being masked as a 500 — errors.As in the ogen NewError hook finds the *domain.Error through the wrap. errors.Is checks against this sentinel keep working unchanged since it is the same value.
Functions ¶
func NewAuthenticator ¶
NewAuthenticator builds the Postgres-backed Authenticator. masterKey is the platform operator credential (empty disables the masterKey scheme).
func NewOutboxEmitter ¶
NewOutboxEmitter builds the outbox-backed Emitter over an initialised *outbox.Outbox (constructed with db.TxDB as its enqueue executor).
func NewPgAccountStore ¶
NewPgAccountStore builds the Postgres-backed AccountStore.
func NewPgAdminAPIKeys ¶
NewPgAdminAPIKeys constructs the adapter over an open *DB.
func NewPgAdminAccessRequests ¶
NewPgAdminAccessRequests builds the Postgres-backed AdminAccessRequests adapter.
func NewPgAdminApps ¶
NewPgAdminApps builds the Postgres-backed AdminApps adapter.
func NewPgAdminConfig ¶
NewPgAdminConfig builds the Postgres-backed AdminConfig adapter.
func NewPgAdminConnections ¶
NewPgAdminConnections builds the Postgres-backed AdminConnections adapter.
func NewPgAdminKeys ¶
NewPgAdminKeys builds the Postgres-backed AdminKeys adapter.
func NewPgAdminServiceAccounts ¶
NewPgAdminServiceAccounts builds the Postgres-backed AdminServiceAccounts adapter.
func NewPgAdminUsers ¶
NewPgAdminUsers builds the Postgres-backed AdminUsers adapter.
func NewPgCoreAuth ¶
NewPgCoreAuth builds the Postgres-backed Core Auth adapter.
func NewPgCoreAuthFlows ¶ added in v1.1.0
func NewPgCoreAuthFlows(db *DB, emitter Emitter, accounts api.CoreAuthAccounts) *pgCoreAuthFlows
NewPgCoreAuthFlows builds the flow adapter. accounts must be the same pgCoreAuth adapter already registered for the CoreAuth group.
func NewPgFederationConnections ¶
NewPgFederationConnections builds the Postgres-backed FederationConnections adapter.
func NewPgFederationRuntime ¶
NewPgFederationRuntime builds the Postgres-backed FederationRuntime adapter.
func NewPgFederationScim ¶
NewPgFederationScim builds the Postgres-backed FederationScim adapter.
func NewPgInvites ¶ added in v1.2.0
NewPgInvites builds the invite admin adapter.
func NewPgMFAAccounts ¶
NewPgMFAAccounts builds the MFA aggregate adapter over a *DB.
func NewPgOAuthSocial ¶
NewPgOAuthSocial builds the OAuthSocial adapter over the connection bundle.
func NewPgOIDCGrants ¶
NewPgOIDCGrants builds the OIDC-provider adapter over db.
func NewPgPasswordlessAccounts ¶
NewPgPasswordlessAccounts builds the Passwordless adapter over db.
func NewPgPlatform ¶
func NewPgPlatform(db *DB) *pgPlatform
NewPgPlatform builds the Postgres-backed PlatformConfig adapter.
func NewPgWebAuthnAccounts ¶
NewPgWebAuthnAccounts builds the WebAuthn adapter.
Types ¶
type Cipher ¶
type Cipher interface {
// Encrypt returns the enc:v1 ciphertext of plaintext.
Encrypt(plaintext string) (string, error)
// Decrypt returns the plaintext for an enc:v1 value, or the input unchanged
// when it is legacy plaintext (no enc: prefix).
Decrypt(value string) (string, error)
}
Cipher encrypts and decrypts reversible secrets at rest.
func NewCipher ¶
NewCipher builds an AES-256-GCM Cipher from a base64-encoded 32-byte key. An empty key yields the identity (passthrough) cipher.
func NewIdentityCipher ¶
func NewIdentityCipher() Cipher
NewIdentityCipher returns a passthrough Cipher (no encryption).
type ConnectOption ¶
type ConnectOption func(*connectOptions)
ConnectOption customizes Postgres connection instrumentation.
func WithLogger ¶
func WithLogger(logger *xlog.Logger) ConnectOption
WithLogger routes pgx query logs through xlog.
func WithMetrics ¶
func WithMetrics(enabled bool) ConnectOption
WithMetrics registers OpenTelemetry metrics for the pgx pool.
func WithQueryLogLevel ¶
func WithQueryLogLevel(level string) ConnectOption
WithQueryLogLevel overrides the pgx query log level.
type DB ¶
type DB struct {
Pool *pgxpool.Pool
TxManager pgtxlib.Trm
TxDB pgtxlib.DB
Bob bobpgx.Pool
// Cipher encrypts reversible secrets at rest (signing-key PEMs, TOTP secrets).
// Defaults to a passthrough cipher; cmd installs a real one via UseCipher.
Cipher Cipher
}
DB is the Postgres connection bundle: the raw pool, the pgtx transaction manager services run repo calls through, the ctx-aware TxDB executor (picks up the ambient transaction from ctx) and a bob pool over the same pool.
func (*DB) Migrate ¶
Migrate applies the embedded SQL migrations against the pool. It needs a live Postgres connection.
type Emitter ¶
Emitter is the port the Postgres adapters use to publish domain events. It is consumed inside the adapters' transactions; see outboxEmitter for the wiring.
type PgMachineIdentities ¶
type PgMachineIdentities struct {
// contains filtered or unexported fields
}
PgMachineIdentities is the Postgres-backed MachineIdentities adapter.
func NewPgMachineIdentities ¶
func NewPgMachineIdentities(db *DB, emitter Emitter) *PgMachineIdentities
NewPgMachineIdentities builds the adapter over a *DB.
func (*PgMachineIdentities) CreateAPIKey ¶
func (a *PgMachineIdentities) CreateAPIKey(ctx context.Context, cmd domain.APIKeyCmd) (*domain.APIKey, string, error)
CreateAPIKey creates an API key and returns the plaintext secret exactly once.
func (*PgMachineIdentities) CreateServiceAccount ¶
func (a *PgMachineIdentities) CreateServiceAccount(ctx context.Context, cmd domain.ServiceAccountCmd) (*domain.ServiceAccount, error)
CreateServiceAccount creates a service account in the command's project.
func (*PgMachineIdentities) CreateServiceAccountSecret ¶
func (a *PgMachineIdentities) CreateServiceAccountSecret(ctx context.Context, cmd domain.MachineIDSecretCmd) (*domain.MachineIDSecret, error)
CreateServiceAccountSecret mints a new client secret for a service account. The plaintext secret is returned once; only its sha256 hash is persisted.
func (*PgMachineIdentities) DeleteServiceAccount ¶
func (a *PgMachineIdentities) DeleteServiceAccount(ctx context.Context, projectID, serviceAccountID string) error
DeleteServiceAccount removes a service account in the given project.
func (*PgMachineIdentities) GetServiceAccount ¶
func (a *PgMachineIdentities) GetServiceAccount(ctx context.Context, projectID, serviceAccountID string) (*domain.ServiceAccount, error)
GetServiceAccount fetches one service account, enforcing the tenant boundary.
func (*PgMachineIdentities) ListAPIKeys ¶
func (a *PgMachineIdentities) ListAPIKeys(ctx context.Context, projectID string) ([]*domain.APIKey, error)
ListAPIKeys returns every API key in a project (tenant-scoped).
func (*PgMachineIdentities) ListServiceAccounts ¶
func (a *PgMachineIdentities) ListServiceAccounts(ctx context.Context, cmd domain.MachineIDServiceAccountListCmd) (*domain.MachineIDServiceAccountPage, error)
ListServiceAccounts returns a cursor page of service accounts in a project. The cursor is the id of the last item; rows are ordered by id ascending and one extra row is fetched to compute HasMore.
func (*PgMachineIdentities) MintToken ¶
func (a *PgMachineIdentities) MintToken(ctx context.Context, projectID, serviceAccountID string) (string, error)
MintToken issues a signed RS256 JWT access token (jwx Signer) for a service account, carrying the SA subject + its scopes.
func (*PgMachineIdentities) RevokeAPIKey ¶
func (a *PgMachineIdentities) RevokeAPIKey(ctx context.Context, projectID, keyID string) error
RevokeAPIKey deletes an API key in the given project.
func (*PgMachineIdentities) RevokeServiceAccountSecret ¶
func (a *PgMachineIdentities) RevokeServiceAccountSecret(ctx context.Context, projectID, serviceAccountID, secretID string) error
RevokeServiceAccountSecret marks a service-account client secret revoked.
func (*PgMachineIdentities) RotateAPIKey ¶
func (a *PgMachineIdentities) RotateAPIKey(ctx context.Context, projectID, keyID string) (*domain.APIKey, string, error)
RotateAPIKey mints a fresh secret for an existing key and returns the new plaintext exactly once; the prefix and hash columns are replaced.
func (*PgMachineIdentities) UpdateAPIKey ¶
func (a *PgMachineIdentities) UpdateAPIKey(ctx context.Context, cmd domain.MachineIDAPIKeyPatchCmd) (*domain.APIKey, error)
UpdateAPIKey patches API-key metadata / scopes / disabled state.
func (*PgMachineIdentities) UpdateServiceAccount ¶
func (a *PgMachineIdentities) UpdateServiceAccount(ctx context.Context, cmd domain.MachineIDServiceAccountPatchCmd) (*domain.ServiceAccount, error)
UpdateServiceAccount patches scopes / disabled state of a service account.
type PgOperator ¶
type PgOperator struct {
// contains filtered or unexported fields
}
PgOperator persists the operator/project aggregates.
func NewPgOperator ¶
func NewPgOperator(db *DB, emitter Emitter) *PgOperator
NewPgOperator builds the operator adapter.
func (*PgOperator) ApplyConfig ¶
func (a *PgOperator) ApplyConfig(ctx context.Context, cmd domain.OperatorConfigCmd) (map[string]any, error)
func (*PgOperator) CreateEnvironment ¶
func (a *PgOperator) CreateEnvironment(ctx context.Context, cmd domain.EnvironmentCmd) (*domain.Environment, error)
func (*PgOperator) CreateProject ¶
func (a *PgOperator) CreateProject(ctx context.Context, cmd domain.ProjectCmd) (*domain.Project, error)
func (*PgOperator) DeleteEnvironment ¶
func (a *PgOperator) DeleteEnvironment(ctx context.Context, projectID, env string) error
func (*PgOperator) DeleteProject ¶
func (*PgOperator) ExportConfig ¶
func (*PgOperator) GetEnvironment ¶
func (a *PgOperator) GetEnvironment(ctx context.Context, projectID, env string) (*domain.Environment, error)
func (*PgOperator) GetFeatures ¶
func (*PgOperator) GetProject ¶
GetProject reads + decodes a project row, mapping no-rows to project-not-found.
func (*PgOperator) ListAdminTokens ¶
func (a *PgOperator) ListAdminTokens(ctx context.Context, projectID string) ([]domain.OperatorAdminToken, error)
func (*PgOperator) ListEnvironments ¶
func (a *PgOperator) ListEnvironments(ctx context.Context, projectID string) ([]domain.Environment, error)
func (*PgOperator) ListProjects ¶
func (*PgOperator) MintAdminToken ¶
func (a *PgOperator) MintAdminToken(ctx context.Context, cmd domain.OperatorAdminTokenCmd) (string, time.Time, error)
func (*PgOperator) PlanConfig ¶
func (a *PgOperator) PlanConfig(ctx context.Context, cmd domain.OperatorConfigCmd) (map[string]any, error)
PlanConfig is a read-only diff: it returns the proposed config without persisting it. The current document is loaded for context; the plan echoes the incoming config plus the prior state under "current".
func (*PgOperator) RevokeAdminToken ¶
func (a *PgOperator) RevokeAdminToken(ctx context.Context, projectID, tokenID string) error
func (*PgOperator) UpdateFeatures ¶
func (a *PgOperator) UpdateFeatures(ctx context.Context, cmd domain.OperatorFeaturesCmd) (map[string]bool, error)
func (*PgOperator) UpdateProject ¶
func (a *PgOperator) UpdateProject(ctx context.Context, cmd domain.OperatorProjectPatchCmd) (*domain.Project, error)
type Signer ¶
type Signer struct {
// contains filtered or unexported fields
}
Signer is the JWT signer/verifier over a project+environment's signing keys.
func (*Signer) JWKS ¶
JWKS returns the public JWK set for project/env as a generic map (the /.well-known/jwks.json body).
func (*Signer) Sign ¶
func (s *Signer) Sign(ctx context.Context, projectID, env string, claims map[string]any, ttl time.Duration) (string, error)
Sign mints a signed RS256 JWT for project/env with the given claims and TTL. iss/aud/sub should be passed as claims by the caller.
func (*Signer) UnverifiedClaims ¶
UnverifiedClaims decodes a JWT's claims WITHOUT verifying the signature. Use only where the result is non-authoritative — e.g. routing an idempotent revoke by its sid claim. Returns nil on a malformed token.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store is the Postgres-backed persistence façade. It exposes typed repos that issue SQL through the sqld-generated query set bound to db.TxDB (the ctx-aware pgtx executor), so a repo call made inside a service's tx.Do* runs in that same transaction; outside one it runs on the pool (auto-commit).
Repos are added per domain entity as the IAM domain is designed; each follows the envelope pattern: marshal the record to JSON, call the generated query, translate pgx errors via translatePgErr / isUniqueViolation.
Source Files
¶
- account_pg.go
- admin_apikeys_pg.go
- admin_connections_pg.go
- admin_pg.go
- admin_serviceaccounts_pg.go
- authenticator_pg.go
- bobexec.go
- cipher.go
- cookie.go
- coreauth_flows_pg.go
- coreauth_flows_recovery_pg.go
- coreauth_flows_signin_pg.go
- coreauth_pg.go
- db.go
- emitter.go
- envscope.go
- federation_pg.go
- federation_saml_state.go
- helpers.go
- invites_pg.go
- machineid_pg.go
- mfa_pg.go
- migrate.go
- oauthsocial_pg.go
- oauthsocial_state.go
- oidc_pg.go
- operator_pg.go
- passwordless_pg.go
- platform_csrf_pg.go
- platform_pg.go
- provider_secrets.go
- signenv.go
- signer.go
- store.go
- tx.go
- util.go
- webauthn_pg.go