postgres

package
v0.0.0-...-230ac96 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: Apache-2.0 Imports: 34 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package postgres implements the AgentMon PostgreSQL proxy per docs/agentmon-db-access-spec.md §11–§14 and the macro design at docs/superpowers/specs/2026-05-10-db-plan-04-pg-proxy-skeleton-design.md.

Plan 04a ships only the listener skeleton: bind Unix sockets per declared db_service, peer-authenticate via SO_PEERCRED + UID-equality, accept and immediately close. Plan 04b adds the handshake / TLS layer; Plan 04c adds Simple Query classification and DBEvent emission.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Intercept

func Intercept(
	stmts []effects.ClassifiedStatement,
	decisions []policy.Decision,
	cache *preparedcache.Cache,
	s statemachine.ConnState,
	rs *policy.RuleSet,
) (handled bool, actions []statemachine.Action)

Intercept implements the spec §7.4 SQL-level prepared statement plus §9.2 R1 DISCARD coverage on the Simple Query path. Called from handleQuery after classify and evaluate; mutates the cache, may rewrite stmts[0] in place (for EXECUTE cache hits), and signals whether the caller still needs to forward the Q frame.

Returns Handled=true with an Action sequence when the proxy must NOT forward to upstream — currently:

  • PREPARE deny: cache untouched; emit DenyRoute.
  • EXECUTE cache miss: emit SynthError + RFQ.

Returns Handled=false when the caller should continue with its normal forward path. In that case, stmts[0] may have been rewritten:

  • PREPARE allow/audit: cache populated; stmts unchanged.
  • EXECUTE cache hit: stmts[0] replaced with cached classification so the caller's subsequent Evaluate sees the right effect set.
  • DEALLOCATE: cache entry removed (or cleared); stmts unchanged.
  • DISCARD ALL / DISCARD PLANS: cache cleared; stmts unchanged.
  • DISCARD TEMP / DISCARD SEQUENCES: no cache change; stmts unchanged.
  • Any other RawVerb: no-op.

`decisions` may be nil for verbs that don't require a prior evaluation (EXECUTE — the cached classification is the source of truth for re-eval inside the caller). `rs` may be nil in pure-unit tests; the deny path then falls through with a zero-valued StatementRule.

Types

type Config

type Config struct {
	Unavoidability  service.Unavoidability
	Services        []Service
	StateDir        string
	Sink            events.Sink
	Logger          *slog.Logger
	Policy          *policy.RuleSet // current rule set; nil means "no rules" (implicit deny). Hot-swappable in a later plan.
	Approver        policy.Approver // defaults to policy.NopApprover{} when nil.
	AgentSessionID  string
	SessionResolver SessionResolver

	// MaxQueryBytes caps the 'Q' frame body. Default 1 MiB when zero.
	// Statements above the cap get a synthetic ErrorResponse(54000) + close.
	MaxQueryBytes int

	// CancelMappingMax caps the proxy-wide BackendKeyData translation table.
	// Default 100k when zero.
	CancelMappingMax int

	// CancelGraceWindow retains disconnected cancel mappings for late
	// side-channel CancelRequests. Default 5 minutes when zero.
	CancelGraceWindow time.Duration

	// UpstreamTLSConfigForTest, when non-nil, overrides the production
	// upstream-TLS config (system roots, verify-full, MinVersion=TLS12,
	// ServerName from svc.Upstream). Test-only — production callsites must
	// leave this nil. dialUpstream uses this verbatim when non-nil.
	UpstreamTLSConfigForTest *tls.Config
	// contains filtered or unexported fields
}

Config captures the supervisor-supplied parameters for a Server. StateDir is always required. Services and Sink are required only when Unavoidability != UnavoidabilityOff. Logger defaults to slog.Default when nil.

type Server

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

Server runs the AgentMon PostgreSQL proxy listeners.

func New

func New(cfg Config) (*Server, error)

New validates cfg and returns a *Server. When cfg.Unavoidability == UnavoidabilityOff, returns a sentinel server whose Start/Shutdown are no-ops. Returns an error when required fields are missing.

func (*Server) SetPolicy

func (s *Server) SetPolicy(rs *policy.RuleSet)

SetPolicy atomically replaces the active rule set. A nil ruleset means "implicit deny everywhere" (matches policy.Evaluate(stmt, nil, _)).

func (*Server) Shutdown

func (s *Server) Shutdown(ctx context.Context) error

Shutdown stops accept loops, waits for in-flight conns to close, and unlinks Unix sockets. Returns nil for sentinel servers.

func (*Server) Start

func (s *Server) Start(ctx context.Context) error

Start binds listeners and runs accept loops until ctx is cancelled. For sentinel servers (Unavoidability == off), Start binds no listeners and blocks on ctx.Done() so callers can use a single goroutine pattern regardless of mode; returns ctx.Err(). Returns the first listener-bind error; subsequent listeners are torn down.

Plan 04a: connection handler is a no-op that closes the conn after the peercred check. Plan 04b plugs in the real handshake handler.

type Service

type Service struct {
	Name     string           // matches policy.DBService.Name
	Family   string           // "postgres"
	Dialect  string           // postgres / aurora_postgres / cockroachdb / redshift
	Upstream string           // host:port
	TLSMode  string           // terminate_reissue / passthrough / terminate_plaintext_upstream
	Listen   ServiceListener  // unix-socket path or tcp host:port
	Service  policy.DBService // full DBService for downstream evaluation
}

Service is the proxy-internal flattened view of one db_service. The proxy needs the listener path (from internal/db/service.Listener) and the upstream + tls_mode metadata (from internal/db/policy.DBService). Callers in internal/api are responsible for joining them.

type ServiceListener

type ServiceListener struct {
	Kind string // "unix" or "tcp"
	Path string // when Kind == "unix"
	Host string // when Kind == "tcp"
	Port int    // when Kind == "tcp"
}

ServiceListener mirrors internal/db/service.Listener but is the package- local concrete type the proxy operates on. Plan 04a only binds Kind=="unix".

type SessionResolver

type SessionResolver interface {
	ResolveSessionID(pid int32) (string, bool)
}

Directories

Path Synopsis
Package preparedcache is a per-connection LRU prepared-statement cache used by the PostgreSQL proxy.
Package preparedcache is a per-connection LRU prepared-statement cache used by the PostgreSQL proxy.
Package statemachine implements the PostgreSQL proxy's Extended Query and transaction state machine per docs/agentmon-db-access-spec.md §14 and the design doc 2026-05-11-db-plan-05-pg-extended-tx-design.md §4.
Package statemachine implements the PostgreSQL proxy's Extended Query and transaction state machine per docs/agentmon-db-access-spec.md §14 and the design doc 2026-05-11-db-plan-05-pg-extended-tx-design.md §4.

Jump to

Keyboard shortcuts

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