{"modulePath":"github.com/diffsec/agentmon","version":"v0.0.0-20261111001054-230ac962f687","isLatest":true,"isStandardLibrary":false,"goos":"linux","goarch":"amd64","docs":"# package postgres\n\nPackage 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.\n\nPlan 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.\n\n## Functions\n\n```go\nfunc Intercept(\n\tstmts []effects.ClassifiedStatement,\n\tdecisions []policy.Decision,\n\tcache *preparedcache.Cache,\n\ts statemachine.ConnState,\n\trs *policy.RuleSet,\n) (handled bool, actions []statemachine.Action)\n```\nIntercept 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.\n\nReturns Handled=true with an Action sequence when the proxy must NOT forward to upstream — currently:\n\n  - PREPARE deny: cache untouched; emit DenyRoute.\n  - EXECUTE cache miss: emit SynthError + RFQ.\n\nReturns Handled=false when the caller should continue with its normal forward path. In that case, stmts\\[0] may have been rewritten:\n\n  - PREPARE allow/audit: cache populated; stmts unchanged.\n  - EXECUTE cache hit: stmts\\[0] replaced with cached classification so the caller's subsequent Evaluate sees the right effect set.\n  - DEALLOCATE: cache entry removed (or cleared); stmts unchanged.\n  - DISCARD ALL / DISCARD PLANS: cache cleared; stmts unchanged.\n  - DISCARD TEMP / DISCARD SEQUENCES: no cache change; stmts unchanged.\n  - Any other RawVerb: no-op.\n\n\\`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.\n\n## Types\n\n```go\ntype Config struct {\n\tUnavoidability  service.Unavoidability\n\tServices        []Service\n\tStateDir        string\n\tSink            events.Sink\n\tLogger          *slog.Logger\n\tPolicy          *policy.RuleSet // current rule set; nil means \"no rules\" (implicit deny). Hot-swappable in a later plan.\n\tApprover        policy.Approver // defaults to policy.NopApprover{} when nil.\n\tAgentSessionID  string\n\tSessionResolver SessionResolver\n\n\t// MaxQueryBytes caps the 'Q' frame body. Default 1 MiB when zero.\n\t// Statements above the cap get a synthetic ErrorResponse(54000) + close.\n\tMaxQueryBytes int\n\n\t// CancelMappingMax caps the proxy-wide BackendKeyData translation table.\n\t// Default 100k when zero.\n\tCancelMappingMax int\n\n\t// CancelGraceWindow retains disconnected cancel mappings for late\n\t// side-channel CancelRequests. Default 5 minutes when zero.\n\tCancelGraceWindow time.Duration\n\n\t// UpstreamTLSConfigForTest, when non-nil, overrides the production\n\t// upstream-TLS config (system roots, verify-full, MinVersion=TLS12,\n\t// ServerName from svc.Upstream). Test-only — production callsites must\n\t// leave this nil. dialUpstream uses this verbatim when non-nil.\n\tUpstreamTLSConfigForTest *tls.Config\n\t// contains filtered or unexported fields\n}\n```\nConfig 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.\n\n```go\ntype Server struct {\n\t// contains filtered or unexported fields\n}\n```\nServer runs the AgentMon PostgreSQL proxy listeners.\n\n```go\nfunc New(cfg Config) (*Server, error)\n```\nNew 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.\n\n```go\nfunc (s *Server) SetPolicy(rs *policy.RuleSet)\n```\nSetPolicy atomically replaces the active rule set. A nil ruleset means \"implicit deny everywhere\" (matches policy.Evaluate(stmt, nil, \\_)).\n\n```go\nfunc (s *Server) Shutdown(ctx context.Context) error\n```\nShutdown stops accept loops, waits for in-flight conns to close, and unlinks Unix sockets. Returns nil for sentinel servers.\n\n```go\nfunc (s *Server) Start(ctx context.Context) error\n```\nStart 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.\n\nPlan 04a: connection handler is a no-op that closes the conn after the peercred check. Plan 04b plugs in the real handshake handler.\n\n```go\ntype Service struct {\n\tName     string           // matches policy.DBService.Name\n\tFamily   string           // \"postgres\"\n\tDialect  string           // postgres / aurora_postgres / cockroachdb / redshift\n\tUpstream string           // host:port\n\tTLSMode  string           // terminate_reissue / passthrough / terminate_plaintext_upstream\n\tListen   ServiceListener  // unix-socket path or tcp host:port\n\tService  policy.DBService // full DBService for downstream evaluation\n}\n```\nService 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.\n\n```go\ntype ServiceListener struct {\n\tKind string // \"unix\" or \"tcp\"\n\tPath string // when Kind == \"unix\"\n\tHost string // when Kind == \"tcp\"\n\tPort int    // when Kind == \"tcp\"\n}\n```\nServiceListener mirrors internal/db/service.Listener but is the package- local concrete type the proxy operates on. Plan 04a only binds Kind==\"unix\".\n\n```go\ntype SessionResolver interface {\n\tResolveSessionID(pid int32) (string, bool)\n}\n```\n\n","path":"github.com/diffsec/agentmon/internal/db/proxy/postgres","name":"postgres","synopsis":"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.","isRedistributable":true}
