Repository navigation
Commit b893b83
fix(postgres): re-read sslcert/sslkey/sslrootcert per connection so rotated certificates take effect (#463)
* fix(postgres): re-read sslcert/sslkey/sslrootcert per connection so rotated certificates take effect (#455)
The DSN parser read each PEM file once and stored the contents as strings in
the pool config, so a long-running server kept the certificate it started with
for the life of the process. With short-lived client certificates reissued on a
timer, every new connection failed once the cached pair expired, while the MCP
transport stayed up and nothing upstream could tell.
node-postgres copies the `ssl` object into the TLS options on every new
connection (`Object.assign(options, ssl)` then `options.key = ssl.key`), which
invokes property getters. Expose `ca`, `cert` and `key` as getters backed by a
RotatingPemFile that re-reads the file on each access. This matches libpq,
which opens sslcert/sslkey/sslrootcert per connection.
Startup validation is unchanged: a missing file or encrypted key still fails
`parse()` with FailedToReadCertificate. After that, a failed re-read or a
rotated key that is encrypted falls back to the last good contents and logs a
warning once per distinct problem, so the atomic-rename window of a rotation or
a transiently missing file does not fail connections outright.
The getters stay configurable because pg redefines `key` as non-enumerable to
keep it out of logs; that only toggles enumerability and leaves the getter in
place. The unit tests mirror that sequence.
Co-Authored-By: Claude Fable 5.1 <[email protected]>
* fix(postgres): reload PEMs in a pg.Client subclass and fail the connection instead of falling back
Address review on #463:
- Replace the per-property getters with a pg.Client subclass installed via
PoolConfig.Client. Its connect() reads sslrootcert/sslcert/sslkey together
before any socket is opened and writes them onto the shared ssl object, so a
half-rotated cert/key pair can never reach TLS.
- Drop the last-good-contents fallback. Like libpq, a connection whose PEM file
cannot be read, or whose key is encrypted, fails with FailedToReadCertificate
naming the file; the next connection retries. A getter cannot do this safely
because pg copies the ssl object inside a stream 'data' listener outside its
try block, where a throw would be an uncaught exception.
- Add an integration test that rotates real certificate files and forces the
pool to open a new connection, asserting on pg_stat_ssl.client_serial, plus
one for the missing-file failure path.
- Reword the docs: files are read at startup, not validated.
Co-Authored-By: Claude Fable 5.1 <[email protected]>
* docs(postgres): describe the cert/key read window accurately
Co-Authored-By: Claude Fable 5.1 <[email protected]>
---------
Co-authored-by: Claude Fable 5.1 <[email protected]>1 parent 70cd17c commit b893b83
4 files changed
Lines changed: 312 additions & 15 deletions
File tree
- docs/config
- src/connectors
- __tests__
- postgres
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
441 | 441 | | |
442 | 442 | | |
443 | 443 | | |
444 | | - | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
445 | 447 | | |
446 | 448 | | |
447 | 449 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
| |||
221 | 222 | | |
222 | 223 | | |
223 | 224 | | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
224 | 339 | | |
225 | 340 | | |
226 | 341 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
41 | | - | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
42 | 44 | | |
43 | 45 | | |
44 | 46 | | |
| |||
143 | 145 | | |
144 | 146 | | |
145 | 147 | | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
146 | 218 | | |
147 | 219 | | |
148 | 220 | | |
| |||
0 commit comments