Skip to content

feat(db): connect to Postgres over TLS - #847

Merged
cjorge-thegraph merged 3 commits into
mainfrom
cjorge/connect-to-postgres-over-tls
Nov 19, 2026
Merged

cjorge-thegraph merged 3 commits into
mainfrom
cjorge/connect-to-postgres-over-tls

Conversation

@cjorge-thegraph

@cjorge-thegraph cjorge-thegraph commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Dipper's sqlx was built without TLS (features = ["postgres", "time", "uuid"]), so it could only reach Postgres in plain text. A db.url with sslmode=require, verify-ca or verify-full failed at startup before sending anything, and a managed database that refuses unencrypted connections could not be used at all.

  • sqlx now carries its rustls backend on aws-lc-rs (tls-rustls-aws-lc-rs), which dipper already builds for Kafka. The only new crate is a small webpki-roots 0.26 shim over the 1.0 already in the tree.
  • db.url's query string sets the connection's TLS: verify-full, verify-ca, require, and sslrootcert=<path> for a CA to trust in addition to the public roots (only with a verify-* mode). sqlx reads that file each time it opens a connection, so a replaced CA applies to connections opened after it. DbConfig.url and k8s/README.md document it.

Note

Without sslmode, sqlx's default prefer now uses TLS when the server offers it, where dipper always stayed in plain text before. prefer does not check the certificate, so a server that offers TLS keeps working.

Tests: sslmode_require_starts_a_tls_handshake runs a stub server that accepts the SSLRequest and checks the next bytes are a TLS handshake. On main dipper hung up without asking for TLS (early eof); now it passes. just fmt-check, just check and just test-unit pass locally.

Changes after review

  • Fixed a startup panic with sslmode=verify-ca. sqlx checks such a certificate through rustls's process-wide crypto provider, and with both of rustls's backends linked (ring and aws-lc-rs) rustls could not pick one. dipper now installs aws-lc-rs first thing in main and again in db::connect (idempotent; the Kafka producer installs the same one). The earlier claim that there was no ambiguous default provider was wrong.
  • verify-ca behaves as verify-full for now. sqlx 0.8 skips the name check only on the mismatch error rustls used to return; rustls 0.23 returns a newer one. A test pins this, so a sqlx that fixes it fails the test and prompts a docs update. The docs recommend verify-full with a name the certificate covers, and no longer suggest verify-ca for a private IP.
  • Real certificate tests: 6 tests run a TLS server with a certificate from a throwaway CA (rcgen, tokio-rustls as dev-dependencies). verify-full accepts it for its host and refuses it for another host or without the CA; verify-ca accepts it (this panicked before) and refuses it for another host.
  • The docs add that prefer does not fall back to plain text when a TLS handshake fails.

🤖 Generated with Claude Code

cjorge-thegraph and others added 3 commits November 18, 2026 12:27
Dipper's sqlx was built without TLS, so it could only reach Postgres in plain text: a
`db.url` asking for `sslmode=require`, `verify-ca` or `verify-full` failed at startup, and a
managed database that refuses unencrypted connections could not be used at all. sqlx now
carries its rustls backend on aws-lc-rs, which dipper already builds for Kafka, so the only new
crate is a small webpki-roots shim.

`db.url`'s query string now sets the connection's TLS the way libpq's does: `verify-full`,
`verify-ca` or `require`, and `sslrootcert=<path>` for a CA to trust beyond the public roots.
Without `sslmode`, dipper now uses TLS when the server offers it (`prefer`, sqlx's default)
instead of always staying in plain text; `prefer` does not check the certificate, so a server
that offers TLS keeps working.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Review found `sslmode=verify-ca` made dipper panic at startup. sqlx checks a certificate without
its hostname through rustls's process-wide crypto provider, and with both of rustls's backends
linked (ring through some dependencies, aws-lc-rs through others) rustls cannot pick one by
itself. The Kafka producer installed aws-lc-rs, but only after the database connected. dipper
now installs it first thing in main and again in `db::connect`, which a test without main also
goes through; installing is idempotent and the producer installs the same provider.

The one test only checked that dipper starts a TLS handshake. 6 tests now run a TLS server with
a certificate from a throwaway CA: `verify-full` accepts it for its host and refuses it for
another host or without the CA, `verify-ca` accepts it (which panicked before), and refuses it
for another host. That last one is sqlx 0.8 with rustls 0.23: sqlx skips the name check only on
the error rustls used to give for a mismatch, so `verify-ca` behaves as `verify-full`. The docs
on `db.url` and in the k8s README now say so, recommend `verify-full` with a name the
certificate covers, and add that `sslrootcert` adds to the public roots, takes effect only with
a `verify-*` mode and applies to connections opened after it changes, and that `prefer` does not
fall back to plain text when the handshake fails.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…tgres-over-tls

# Conflicts:
#	Cargo.lock
#	bin/dipper-service/Cargo.toml
#	bin/dipper-service/src/db.rs
#	k8s/README.md
@cjorge-thegraph
cjorge-thegraph marked this pull request as ready for review November 19, 2026 20:22
@cjorge-thegraph
cjorge-thegraph merged commit 2673181 into main Nov 19, 2026
13 checks passed
cjorge-thegraph added a commit that referenced this pull request Nov 19, 2026
…res-and-iisa-at-start

Resolves db.rs against #847 and #850. The startup probe now fails before the pool does, so a
refused password check gets #850's explanation there as well, and the TLS tests, whose stub
drops each connection after its handshake, set a 1s connect wait instead of waiting 120s out.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
MoonBoi9001 pushed a commit that referenced this pull request Nov 23, 2026
🤖 I have created a release *beep* *boop*
---


##
[0.3.4](v0.3.3...v0.3.4)
(2026-11-23)


### Added

* **config:** type secrets as such and read config from several files
([#857](#857))
([fe92407](fe92407))
* **db:** connect to Postgres over TLS
([#847](#847))
([2673181](2673181))
* **db:** let only 1 dipper process write, enforced by the database
([#858](#858))
([6f88900](6f88900))
* **db:** make the database password optional
([#850](#850))
([cb5d3b1](cb5d3b1))
* **metrics:** serve Prometheus metrics and optionally log as JSON
([#851](#851))
([523819e](523819e))
* **rpc:** expose effective indexing targets and chain identity
([#860](#860))
([aafc0ce](aafc0ce))
* **startup:** wait for Postgres and IISA instead of exiting
([#849](#849))
([d1406d4](d1406d4))


### Fixed

* **db:** sslmode=verify-ca skips the hostname check, as libpq does
([#859](#859))
([5fec97d](5fec97d))
* **k8s:** roll dipper out one pod at a time
([#846](#846))
([de57e46](de57e46))
* **shutdown:** stop taking jobs first and fit the teardown in its
budget ([#854](#854))
([a1bc47d](a1bc47d))


### Tests

* run each test's Postgres on its Unix socket only
([#861](#861))
([bc9e2b2](bc9e2b2))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@MoonBoi9001
MoonBoi9001 deleted the cjorge/connect-to-postgres-over-tls branch November 26, 2026 13:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant