feat: enforce statements on the PostgreSQL wire protocol - #2
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Teaches the proxy the PostgreSQL protocol so it can see and refuse individual statements. The relay is now driven by a dialect:
internal/pgparses the startup packet, relays authentication untouched, frames the simple and extended query protocols, and consults a handler on every statement. No policy is wired yet, so the handler allows everything; this is the seam the policy and the ledger attach to.Design
internal/wireholds dialect-neutral types (Startup,Statement,Verdict,Handler) and theDialect/Sessioninterfaces. Nothing in the proxy, and nothing a future policy or ledger touches, imports a database-specific package. MySQL is a planned second dialect and slots in behind the same interface.internal/pgimplements the interface for PostgreSQL v3.internal/proxydrives oneSessionper connection: bounded handshake, thenFrontendandBackendrun concurrently with half-close propagation.Statement handling
Query(simple protocol) and onParse(the only extended-protocol message that carries SQL).FunctionCallis refused (fast-path is not supported).Sync, so a batch is accepted or rejected as a unit. If any statement in a batch is denied, nothing in it reaches the upstream and the client receives oneErrorResponsefollowed by theReadyForQueryfrom a forwardedSync: no partial execution, no desync, no hung upstream.ErrorResponsethe client can read; the transaction status in the followingReadyForQueryreflects the upstream's last report.Safety and limits
SSLRequestwithN, sosslmode=preferclients fall back to plaintext. The CLI warns when listening outside loopback. TLS termination is a later step.Testing
make testandmake lintpass; the protocol tests were also run under-race -count=100.internal/pg: password and SASL handshakes, cancel-request pass-through, upstream rejection, unknown protocol, allowed and atomically-rejected batches, denial-then-recovery, oversized statements,FunctionCallrefusal, copy-data pass-through, and transaction-status tracking.internal/cli: an end-to-end test that runs the command against a fake PostgreSQL over TCP.postgres:16andpostgres:18withpsql: simple multi-statement queries, extended\bind, pipeline mode with a mid-pipeline server error and recovery, andCOPYin and out.Not in this PR
Statement classification and policy (the handler is always-allow), the access ledger, TLS, and MySQL.