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 {
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 ¶
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 ¶
SetPolicy atomically replaces the active rule set. A nil ruleset means "implicit deny everywhere" (matches policy.Evaluate(stmt, nil, _)).
func (*Server) Shutdown ¶
Shutdown stops accept loops, waits for in-flight conns to close, and unlinks Unix sockets. Returns nil for sentinel servers.
func (*Server) Start ¶
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 ¶
Source Files
¶
- approvalwait.go
- authforward.go
- cancel.go
- cancelmap.go
- catalog_runtime.go
- classifiers.go
- connect_rule.go
- copyframes.go
- deny.go
- eventbuilder.go
- eventid.go
- extquery.go
- funccall.go
- handshake.go
- listener_unix.go
- passthrough.go
- peercred_linux.go
- proxyconn.go
- redirect_runtime.go
- resolve_runtime.go
- server.go
- simplequery.go
- sni.go
- sqlprepared.go
- tls.go
- upstream.go
- upstreamread.go
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. |