postgres

package
v0.1.44 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package scoped provides authenticated, transaction-scoped access to a Codefly Postgres service. It deliberately has no admin capability: schema ownership and cross-tenant maintenance belong to separately wired control plane dependencies, never to request handlers.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnauthenticated = errors.New("postgres scope requires an authenticated principal")
	ErrUnauthorized    = errors.New("postgres write capability is not authorized")
)

Functions

This section is empty.

Types

type Authenticator

type Authenticator interface {
	AuthenticatedPrincipal(context.Context) (Principal, error)
	AuthorizeDatabaseWrite(context.Context, Principal) error
}

Authenticator resolves an already verified principal from context and owns the application-specific decision to issue a write capability.

type Factory

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

Factory owns the private reader/writer pools and converts authenticated request context into bound capabilities. It never exposes either raw pool.

func NewFactory

func NewFactory(reader, writer *pgxpool.Pool, authenticator Authenticator, options ...Option) (*Factory, error)

NewFactory constructs the request-time database boundary. The pools must be built from Codefly's read-only-connection and read-write-connection secrets, respectively.

func (*Factory) Reader

func (f *Factory) Reader(ctx context.Context) (*Reader, error)

Reader resolves the authenticated principal and returns a bound read-only capability. It does not authorize or construct a writer.

func (*Factory) Writer

func (f *Factory) Writer(ctx context.Context) (*Writer, error)

Writer resolves the authenticated principal and requires an explicit application authorization decision before returning a write capability.

type Option

type Option func(*config) error

Option configures the RLS settings used by application policies.

func WithScopeSettings

func WithScopeSettings(tenantSetting, userSetting string) Option

WithScopeSettings changes the transaction-local Postgres settings. Custom settings must use the extension-style namespace required by Postgres, e.g. "app.current_tenant_id" and "app.current_user_id".

type Principal

type Principal interface {
	DatabaseTenantID() string
	DatabaseUserID() string
}

Principal is the minimal authenticated identity required by tenant RLS. Applications adapt their verified auth/proto identity to this interface at the request boundary; repositories never accept tenant or user IDs directly.

type ReadTx

type ReadTx interface {
	Query(context.Context, string, ...any) (pgx.Rows, error)
	QueryRow(context.Context, string, ...any) pgx.Row
}

ReadTx intentionally exposes query operations only. Even if it is circumvented, the transaction and its dedicated credential are read-only at the database layer as defense in depth.

type Reader

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

Reader is an authenticated, principal-bound read capability.

func (*Reader) InTransaction

func (r *Reader) InTransaction(ctx context.Context, fn func(context.Context, ReadTx) error) error

InTransaction runs fn in a database-enforced read-only transaction after installing tenant/user scope with transaction-local settings.

type WriteTx

type WriteTx interface {
	ReadTx
	Exec(context.Context, string, ...any) (pgconn.CommandTag, error)
}

WriteTx adds mutation to ReadTx. A writer may query to enforce invariants; a reader can never obtain Exec through this API.

type Writer

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

Writer is an authenticated, authorized, principal-bound write capability.

func (*Writer) InTransaction

func (w *Writer) InTransaction(ctx context.Context, fn func(context.Context, WriteTx) error) error

InTransaction runs fn in a read-write transaction after installing transaction-local tenant/user scope.

Jump to

Keyboard shortcuts

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