pgserve 1.2.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.genie/brainstorms/pgserve-v2/DESIGN.md +174 -0
- package/.genie/wishes/pgserve-v2/BRIEF-from-genie-pgserve.md +99 -0
- package/.genie/wishes/pgserve-v2/WISH.md +442 -0
- package/.genie/wishes/release-system-genie-pattern/WISH.md +9 -9
- package/.genie/wishes/release-system-genie-pattern/validation.md +43 -10
- package/.github/workflows/ci.yml +8 -4
- package/.github/workflows/version.yml +2 -2
- package/CHANGELOG.md +150 -0
- package/README.md +186 -1
- package/bin/pglite-server.js +253 -1
- package/eslint.config.js +2 -0
- package/package.json +1 -1
- package/src/admin-client.js +171 -0
- package/src/audit.js +168 -0
- package/src/control-db.js +313 -0
- package/src/daemon-control.js +408 -0
- package/src/daemon-shared.js +18 -0
- package/src/daemon-tcp.js +296 -0
- package/src/daemon.js +629 -0
- package/src/fingerprint.js +453 -0
- package/src/gc.js +351 -0
- package/src/index.js +11 -0
- package/src/protocol.js +131 -0
- package/src/router.js +8 -0
- package/src/tenancy.js +75 -0
- package/src/tokens.js +102 -0
- package/tests/audit.test.js +189 -0
- package/tests/control-db.test.js +285 -0
- package/tests/daemon-fingerprint-integration.test.js +109 -0
- package/tests/daemon-pr24-regression.test.js +201 -0
- package/tests/fingerprint.test.js +249 -0
- package/tests/fixtures/240-orphan-seed.sql +30 -0
- package/tests/orphan-cleanup.test.js +390 -0
- package/tests/tcp-listen.test.js +368 -0
- package/tests/tenancy.test.js +403 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to `pgserve` are documented here. The format follows
|
|
4
|
+
[Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and this project adheres
|
|
5
|
+
to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
6
|
+
|
|
7
|
+
## 2.0.0 — Unreleased
|
|
8
|
+
|
|
9
|
+
> The release date will replace "Unreleased" when the v2.0.0 release workflow
|
|
10
|
+
> fires. The CHANGELOG is committed ahead of the release trigger so consumers
|
|
11
|
+
> can review the migration plan before the artifact lands on npm.
|
|
12
|
+
|
|
13
|
+
### Pin guidance (read this first)
|
|
14
|
+
|
|
15
|
+
Existing v1 consumers should pin `pgserve@^1.x` in their `package.json` until
|
|
16
|
+
they have completed the migration described below. v2 changes the default
|
|
17
|
+
transport (Unix socket, no TCP), the identity model (kernel-rooted
|
|
18
|
+
fingerprint), the database layout (one DB per fingerprint), and the daemon
|
|
19
|
+
process model (singleton). A blind upgrade will break v1 connection strings.
|
|
20
|
+
|
|
21
|
+
```jsonc
|
|
22
|
+
// package.json — keep v1 until you migrate
|
|
23
|
+
{
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"pgserve": "^1.2.0"
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### Breaking changes
|
|
31
|
+
|
|
32
|
+
- **TCP is no longer the default.** v1 bound `127.0.0.1:8432` for every
|
|
33
|
+
consumer. v2 binds a Unix control socket at
|
|
34
|
+
`${XDG_RUNTIME_DIR:-/tmp}/pgserve/control.sock` (mode `0600`, dir mode
|
|
35
|
+
`0700`) plus a `.s.PGSQL.5432` symlink so libpq clients connect with no
|
|
36
|
+
host/port/user/password. To keep a TCP listener, opt in explicitly with
|
|
37
|
+
`--listen <port>` (see "Compat TCP via --listen" in the README).
|
|
38
|
+
- **Fingerprint enforcement is default-ON.** Each connecting peer is
|
|
39
|
+
identified via `SO_PEERCRED` + the resolved `package.json` `name`,
|
|
40
|
+
collapsed to a 12-hex fingerprint. The daemon refuses to route a peer
|
|
41
|
+
into a database that does not match its fingerprint with SQLSTATE
|
|
42
|
+
`28P01 invalid_authorization — database fingerprint mismatch`. The
|
|
43
|
+
emergency kill switch is `PGSERVE_DISABLE_FINGERPRINT_ENFORCEMENT=1`
|
|
44
|
+
(deprecated; the daemon emits a stderr warning at boot when the env var
|
|
45
|
+
is observed).
|
|
46
|
+
- **Database-per-fingerprint isolation.** v1 served arbitrary database
|
|
47
|
+
names freely. v2 auto-creates `app_<sanitized-name>_<12hex>` for each
|
|
48
|
+
unique fingerprint on first connect; cross-fingerprint reads are denied.
|
|
49
|
+
`psql -l` will show one row per consumer rather than the shared pool
|
|
50
|
+
v1 produced. Monorepo rule: the root `package.json` `name` wins for all
|
|
51
|
+
packages under it.
|
|
52
|
+
- **Singleton daemon via control socket.** v1 spun up a server per
|
|
53
|
+
invocation, leaving consumers to coordinate ports themselves. v2
|
|
54
|
+
enforces one daemon per host: a second `pgserve daemon` exits with
|
|
55
|
+
`already running, pid N`. Run it under PM2 or systemd (snippets in the
|
|
56
|
+
README) — there is no PM-managed multi-process mode anymore.
|
|
57
|
+
- **GC sweep emits `db_reaped_ttl` and `db_reaped_liveness` audit events.**
|
|
58
|
+
Default lifecycle is now ephemeral: a database whose `liveness_pid` is
|
|
59
|
+
dead AND whose `last_connection_at` is older than 24h is dropped on the
|
|
60
|
+
next sweep (boot, hourly, sampled on-connect). To opt out, add
|
|
61
|
+
`pgserve.persist: true` to the consumer's `package.json` — flagged
|
|
62
|
+
databases are never reaped.
|
|
63
|
+
|
|
64
|
+
### Migration guide
|
|
65
|
+
|
|
66
|
+
1. **Connection strings** — drop credentials and the port; switch to the
|
|
67
|
+
socket form.
|
|
68
|
+
|
|
69
|
+
```diff
|
|
70
|
+
- postgres://user:pass@localhost:5432/db
|
|
71
|
+
+ postgres:///db?host=${XDG_RUNTIME_DIR:-/tmp}/pgserve
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Equivalently, for `psql`:
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
psql -h "${XDG_RUNTIME_DIR:-/tmp}/pgserve" -d myapp
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
2. **Long-lived apps** — anything whose data needs to outlive a 24h idle
|
|
81
|
+
window (genie state stores, dashboards, anything with state worth
|
|
82
|
+
keeping) must declare persistence in its `package.json`:
|
|
83
|
+
|
|
84
|
+
```jsonc
|
|
85
|
+
{
|
|
86
|
+
"name": "my-long-lived-app",
|
|
87
|
+
"pgserve": { "persist": true }
|
|
88
|
+
}
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Without this flag, the GC sweep will reap the database after the TTL
|
|
92
|
+
plus liveness check passes.
|
|
93
|
+
|
|
94
|
+
3. **Need TCP?** Opt in with `--listen` and use issued tokens. TCP peers
|
|
95
|
+
cannot use `SO_PEERCRED`, so they must authenticate at connect time.
|
|
96
|
+
|
|
97
|
+
```bash
|
|
98
|
+
pgserve daemon --listen :5432
|
|
99
|
+
|
|
100
|
+
# Issue a bearer token for a known fingerprint (printed once):
|
|
101
|
+
pgserve daemon issue-token --fingerprint <12hex>
|
|
102
|
+
|
|
103
|
+
# TCP clients pass the token via libpq application_name as
|
|
104
|
+
# ?fingerprint=<hex>&token=<bearer>
|
|
105
|
+
# Revoke when done:
|
|
106
|
+
pgserve daemon revoke-token <token-id>
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
Without `--listen`, no TCP port is bound — verify with
|
|
110
|
+
`ss -tlnp | grep -v pgserve` returning no pgserve rows.
|
|
111
|
+
|
|
112
|
+
4. **Kill switch (emergency only).** If the fingerprint enforcement
|
|
113
|
+
denies a connection you cannot otherwise unblock, set
|
|
114
|
+
`PGSERVE_DISABLE_FINGERPRINT_ENFORCEMENT=1` for the daemon. The
|
|
115
|
+
bypassed connection emits an `enforcement_kill_switch_used` audit
|
|
116
|
+
event; the daemon logs a deprecation warning at boot whenever the
|
|
117
|
+
variable is observed. The kill switch will be removed in a future
|
|
118
|
+
major; treat it as a debugging tool, not a production setting.
|
|
119
|
+
|
|
120
|
+
### New features (group references map to wish execution groups)
|
|
121
|
+
|
|
122
|
+
- **Group 4 — Database-per-fingerprint + enforcement + kill switch.**
|
|
123
|
+
Auto-create `app_<name>_<12hex>` on first connect, deny
|
|
124
|
+
cross-fingerprint reads with SQLSTATE `28P01`, audit event
|
|
125
|
+
`connection_denied_fingerprint_mismatch`. Sanitizer collapses
|
|
126
|
+
non-`[a-z0-9]` runs to `_`, lowercases, truncates to 30 chars to keep
|
|
127
|
+
the resulting DB name ≤ 63 chars.
|
|
128
|
+
- **Group 5 — Lifecycle + persist flag + GC sweep.** Three-layer
|
|
129
|
+
lifecycle: liveness (peer pid alive), 24h TTL since last connection,
|
|
130
|
+
and `pgserve.persist: true` override. Sweep runs at daemon boot,
|
|
131
|
+
hourly, and sampled on-connect at 1/N where N = max(1, dbCount/10).
|
|
132
|
+
Reaped databases emit `db_reaped_ttl` or `db_reaped_liveness` audit
|
|
133
|
+
events; the on-connect sweep does not block accept latency past 50 ms
|
|
134
|
+
P99.
|
|
135
|
+
- **Group 6 — `--listen` opt-in TCP + token auth.** Daemon CLI accepts
|
|
136
|
+
`--listen [host:]port` (repeatable). Tokens issued via
|
|
137
|
+
`pgserve daemon issue-token --fingerprint <hex>`, hashed at rest into
|
|
138
|
+
`pgserve_meta.allowed_tokens`, verified with constant-time compare.
|
|
139
|
+
New audit events: `tcp_token_issued`, `tcp_token_used`,
|
|
140
|
+
`tcp_token_denied`. Without `--listen`, no TCP port is bound.
|
|
141
|
+
|
|
142
|
+
### Compatibility
|
|
143
|
+
|
|
144
|
+
- Node.js >= 18 (unchanged).
|
|
145
|
+
- Linux x64, macOS ARM64/x64, Windows x64. Windows uses named pipes for
|
|
146
|
+
the control socket; PM2/systemd snippets are Linux-first.
|
|
147
|
+
- `--ram` (Linux/WSL2 `/dev/shm`), `--pgvector`, `--sync-to`, and the
|
|
148
|
+
rest of the v1 runtime flags continue to work unchanged.
|
|
149
|
+
|
|
150
|
+
---
|
package/README.md
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
<a href="https://discord.gg/xcW8c7fF3R"><img src="https://img.shields.io/discord/1095114867012292758?style=flat-square&color=00D9FF&label=discord" alt="Discord"></a>
|
|
11
11
|
</p>
|
|
12
12
|
|
|
13
|
-
<p><em>Zero config, auto-provision databases, unlimited concurrent connections
|
|
13
|
+
<p><em>npx pgserve and it just works, no credentials needed. Zero config, auto-provision databases, unlimited concurrent connections.</em></p>
|
|
14
14
|
|
|
15
15
|
<p>
|
|
16
16
|
<a href="#-quick-start">Quick Start</a> •
|
|
@@ -35,6 +35,8 @@ Connect from any PostgreSQL client — databases auto-create on first connection
|
|
|
35
35
|
psql postgresql://localhost:8432/myapp
|
|
36
36
|
```
|
|
37
37
|
|
|
38
|
+
> Note: v2 default is the Unix socket — see [Daemon mode](#daemon-mode). The TCP form above is the v1 compat path.
|
|
39
|
+
|
|
38
40
|
<br>
|
|
39
41
|
|
|
40
42
|
## Features
|
|
@@ -168,6 +170,189 @@ pgserve --sync-to "postgresql://user:pass@db.example.com:5432/prod"
|
|
|
168
170
|
|
|
169
171
|
<br>
|
|
170
172
|
|
|
173
|
+
## Daemon mode
|
|
174
|
+
|
|
175
|
+
`pgserve@2` ships a singleton daemon that binds a Unix control socket
|
|
176
|
+
inside `$XDG_RUNTIME_DIR/pgserve` (fallback `/tmp/pgserve`). One daemon
|
|
177
|
+
per host serves every consumer on the box — no port conflicts, no
|
|
178
|
+
credentials, kernel-rooted identity. Run it under PM2 or systemd so it
|
|
179
|
+
restarts automatically.
|
|
180
|
+
|
|
181
|
+
```bash
|
|
182
|
+
# Foreground (for debugging)
|
|
183
|
+
pgserve daemon
|
|
184
|
+
|
|
185
|
+
# Stop a running daemon
|
|
186
|
+
pgserve daemon stop
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
A second `pgserve daemon` invocation while the first is running exits with
|
|
190
|
+
`already running, pid N`. A daemon killed with `kill -9` leaves an orphan
|
|
191
|
+
PID file + socket; the next `pgserve daemon` boot detects the dead pid and
|
|
192
|
+
cleans both up automatically.
|
|
193
|
+
|
|
194
|
+
Connect from any libpq client (no host/port/user/password required —
|
|
195
|
+
the daemon authenticates via SO_PEERCRED on accept):
|
|
196
|
+
|
|
197
|
+
```bash
|
|
198
|
+
psql -h "${XDG_RUNTIME_DIR:-/tmp}/pgserve" -d myapp
|
|
199
|
+
# or via connection URI
|
|
200
|
+
psql "postgresql:///myapp?host=${XDG_RUNTIME_DIR:-/tmp}/pgserve"
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
### Supervised by PM2
|
|
204
|
+
|
|
205
|
+
`ecosystem.config.cjs` snippet:
|
|
206
|
+
|
|
207
|
+
```javascript
|
|
208
|
+
module.exports = {
|
|
209
|
+
apps: [{
|
|
210
|
+
name: 'pgserve',
|
|
211
|
+
script: 'pgserve',
|
|
212
|
+
args: 'daemon',
|
|
213
|
+
autorestart: true,
|
|
214
|
+
max_memory_restart: '1G',
|
|
215
|
+
env: { XDG_RUNTIME_DIR: '/run/user/1000' },
|
|
216
|
+
}],
|
|
217
|
+
};
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
```bash
|
|
221
|
+
pm2 start ecosystem.config.cjs && pm2 save
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
### Supervised by systemd
|
|
225
|
+
|
|
226
|
+
`/etc/systemd/user/pgserve.service`:
|
|
227
|
+
|
|
228
|
+
```ini
|
|
229
|
+
[Unit]
|
|
230
|
+
Description=pgserve daemon
|
|
231
|
+
After=default.target
|
|
232
|
+
|
|
233
|
+
[Service]
|
|
234
|
+
Type=simple
|
|
235
|
+
ExecStart=/usr/bin/env npx pgserve daemon
|
|
236
|
+
Restart=on-failure
|
|
237
|
+
RestartSec=5
|
|
238
|
+
|
|
239
|
+
[Install]
|
|
240
|
+
WantedBy=default.target
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
Enable for the current user:
|
|
244
|
+
|
|
245
|
+
```bash
|
|
246
|
+
systemctl --user enable --now pgserve
|
|
247
|
+
journalctl --user -u pgserve -f
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
The systemd user unit inherits `XDG_RUNTIME_DIR` automatically; the daemon
|
|
251
|
+
binds `${XDG_RUNTIME_DIR}/pgserve/control.sock` (mode 0600, dir mode 0700)
|
|
252
|
+
plus a `.s.PGSQL.5432` symlink so off-the-shelf PostgreSQL clients connect
|
|
253
|
+
without further configuration.
|
|
254
|
+
|
|
255
|
+
<br>
|
|
256
|
+
|
|
257
|
+
## Fingerprint isolation
|
|
258
|
+
|
|
259
|
+
Each consumer is identified by a **kernel-rooted fingerprint** derived from
|
|
260
|
+
the peer's `SO_PEERCRED` plus the resolved `package.json` `name`, collapsed
|
|
261
|
+
to 12 hex chars. The daemon auto-creates one database per fingerprint —
|
|
262
|
+
`app_<sanitized-name>_<12hex>` — and refuses to route a peer into any other
|
|
263
|
+
database with SQLSTATE `28P01 invalid_authorization — database fingerprint
|
|
264
|
+
mismatch`.
|
|
265
|
+
|
|
266
|
+
```bash
|
|
267
|
+
# What `psql -l` shows on a host with three consumers:
|
|
268
|
+
$ psql -h "${XDG_RUNTIME_DIR:-/tmp}/pgserve" -l
|
|
269
|
+
Name | Owner | ...
|
|
270
|
+
-----------------------+----------+----
|
|
271
|
+
app_genie_a1b2c3d4e5f6 | postgres | ...
|
|
272
|
+
app_brain_4f3e2d1c0b9a | postgres | ...
|
|
273
|
+
app_omni_9876543210ab | postgres | ...
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
**Monorepo rule:** the **root** `package.json` `name` wins. Every workspace
|
|
277
|
+
under it shares one fingerprint and one database — sub-packages do **not**
|
|
278
|
+
get their own. If you need separate isolation, run them from separate
|
|
279
|
+
checkouts.
|
|
280
|
+
|
|
281
|
+
**Sanitization:** non-`[a-z0-9]` runs collapse to `_`, lowercased, truncated
|
|
282
|
+
to 30 chars so the final DB name stays within PostgreSQL's 63-char limit.
|
|
283
|
+
A name like `@scope/foo bar` becomes `_scope_foo_bar`.
|
|
284
|
+
|
|
285
|
+
**Emergency kill switch:** `PGSERVE_DISABLE_FINGERPRINT_ENFORCEMENT=1`
|
|
286
|
+
disables enforcement for the daemon process. Use it as a debugging tool
|
|
287
|
+
only — every bypassed connection emits an `enforcement_kill_switch_used`
|
|
288
|
+
audit event and the daemon logs a deprecation warning at boot.
|
|
289
|
+
|
|
290
|
+
<br>
|
|
291
|
+
|
|
292
|
+
## Long-running apps: `pgserve.persist`
|
|
293
|
+
|
|
294
|
+
Default lifecycle is **ephemeral**: a database whose `liveness_pid` is dead
|
|
295
|
+
AND whose `last_connection_at` is older than 24h is dropped on the next GC
|
|
296
|
+
sweep (boot, hourly, sampled on-connect). Reaped DBs emit
|
|
297
|
+
`db_reaped_ttl` or `db_reaped_liveness` audit events.
|
|
298
|
+
|
|
299
|
+
If your app holds state worth keeping past 24h of idle — genie's wish/agent
|
|
300
|
+
store, internal dashboards, anything you'd be unhappy to lose — declare
|
|
301
|
+
persistence in `package.json`:
|
|
302
|
+
|
|
303
|
+
```jsonc
|
|
304
|
+
{
|
|
305
|
+
"name": "my-long-lived-app",
|
|
306
|
+
"pgserve": { "persist": true }
|
|
307
|
+
}
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
Persisted databases are **never** reaped, regardless of liveness or TTL.
|
|
311
|
+
Dev workloads with long debug cycles do not normally need this — any new
|
|
312
|
+
connection slides the TTL window forward. Reach for `pgserve.persist` when
|
|
313
|
+
the app is genuinely long-lived (production daemon, dashboard, durable
|
|
314
|
+
agent state), not just for convenience.
|
|
315
|
+
|
|
316
|
+
<br>
|
|
317
|
+
|
|
318
|
+
## Compat TCP via `--listen`
|
|
319
|
+
|
|
320
|
+
TCP is **off by default** in v2. Bring it back only when you need it
|
|
321
|
+
(Kubernetes pods, remote sync, legacy clients that cannot speak Unix
|
|
322
|
+
sockets) by opting in:
|
|
323
|
+
|
|
324
|
+
```bash
|
|
325
|
+
pgserve daemon --listen :5432
|
|
326
|
+
# Repeatable for multiple binds:
|
|
327
|
+
pgserve daemon --listen :5432 --listen 0.0.0.0:5433
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
TCP peers cannot use `SO_PEERCRED`, so they **must** authenticate at
|
|
331
|
+
connect time. Issue a bearer token bound to a known fingerprint:
|
|
332
|
+
|
|
333
|
+
```bash
|
|
334
|
+
# Prints the token ONCE; the daemon stores only its hash.
|
|
335
|
+
pgserve daemon issue-token --fingerprint a1b2c3d4e5f6
|
|
336
|
+
|
|
337
|
+
# TCP client passes it via libpq application_name:
|
|
338
|
+
# ?fingerprint=a1b2c3d4e5f6&token=<bearer>
|
|
339
|
+
|
|
340
|
+
# Revoke when done:
|
|
341
|
+
pgserve daemon revoke-token <token-id>
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
Audit events: `tcp_token_issued`, `tcp_token_used`, `tcp_token_denied`.
|
|
345
|
+
Tokens are verified with constant-time compare. Without a valid token a
|
|
346
|
+
TCP connection is refused — there is no anonymous TCP path.
|
|
347
|
+
|
|
348
|
+
Verify no port is bound when `--listen` is **not** set:
|
|
349
|
+
|
|
350
|
+
```bash
|
|
351
|
+
ss -tlnp | grep pgserve # no rows expected
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
<br>
|
|
355
|
+
|
|
171
356
|
## API
|
|
172
357
|
|
|
173
358
|
```javascript
|
package/bin/pglite-server.js
CHANGED
|
@@ -12,6 +12,20 @@ import path from 'path';
|
|
|
12
12
|
import os from 'os';
|
|
13
13
|
import { startMultiTenantServer } from '../src/index.js';
|
|
14
14
|
import { startClusterServer } from '../src/cluster.js';
|
|
15
|
+
import {
|
|
16
|
+
PgserveDaemon,
|
|
17
|
+
stopDaemon,
|
|
18
|
+
resolveControlSocketDir,
|
|
19
|
+
resolveControlSocketPath,
|
|
20
|
+
} from '../src/daemon.js';
|
|
21
|
+
import { createAdminClient, readAdminDiscovery } from '../src/admin-client.js';
|
|
22
|
+
import {
|
|
23
|
+
ensureMetaSchema,
|
|
24
|
+
addAllowedToken,
|
|
25
|
+
revokeAllowedToken,
|
|
26
|
+
} from '../src/control-db.js';
|
|
27
|
+
import { mintToken } from '../src/tokens.js';
|
|
28
|
+
import { audit, AUDIT_EVENTS } from '../src/audit.js';
|
|
15
29
|
|
|
16
30
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
17
31
|
|
|
@@ -25,9 +39,247 @@ process.on('uncaughtException', (error) => {
|
|
|
25
39
|
process.exit(1);
|
|
26
40
|
});
|
|
27
41
|
|
|
28
|
-
// Parse CLI arguments
|
|
42
|
+
// Parse CLI arguments — `pgserve daemon [stop]` is dispatched before the
|
|
43
|
+
// classic `pgserve [options]` parser so daemon-mode flags do not collide
|
|
44
|
+
// with router flags.
|
|
29
45
|
const args = process.argv.slice(2);
|
|
30
46
|
|
|
47
|
+
if (args[0] === 'daemon') {
|
|
48
|
+
await runDaemonSubcommand(args.slice(1));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async function runDaemonSubcommand(daemonArgs) {
|
|
52
|
+
if (daemonArgs[0] === 'stop') {
|
|
53
|
+
const result = stopDaemon();
|
|
54
|
+
if (result.stopped) {
|
|
55
|
+
console.log(`pgserve daemon stopped (pid ${result.pid})`);
|
|
56
|
+
process.exit(0);
|
|
57
|
+
}
|
|
58
|
+
if (result.reason === 'no-pid-file') {
|
|
59
|
+
console.error('pgserve daemon: no PID file found — is the daemon running?');
|
|
60
|
+
process.exit(1);
|
|
61
|
+
}
|
|
62
|
+
if (result.reason === 'stale-pid' || result.reason === 'invalid-pid-file') {
|
|
63
|
+
console.log(`pgserve daemon: cleaned up stale lock (pid ${result.pid ?? '?'})`);
|
|
64
|
+
process.exit(0);
|
|
65
|
+
}
|
|
66
|
+
if (result.reason === 'timeout') {
|
|
67
|
+
console.error(`pgserve daemon: pid ${result.pid} did not exit within timeout`);
|
|
68
|
+
process.exit(1);
|
|
69
|
+
}
|
|
70
|
+
console.error(`pgserve daemon stop: ${result.reason}${result.error ? ` (${result.error})` : ''}`);
|
|
71
|
+
process.exit(1);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (daemonArgs[0] === 'issue-token') {
|
|
75
|
+
await runIssueTokenSubcommand(daemonArgs.slice(1));
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
if (daemonArgs[0] === 'revoke-token') {
|
|
79
|
+
await runRevokeTokenSubcommand(daemonArgs.slice(1));
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// `pgserve daemon` (long-running)
|
|
84
|
+
const opts = parseDaemonArgs(daemonArgs);
|
|
85
|
+
const daemon = new PgserveDaemon(opts);
|
|
86
|
+
try {
|
|
87
|
+
await daemon.start();
|
|
88
|
+
} catch (err) {
|
|
89
|
+
if (err.code === 'EALREADYRUNNING') {
|
|
90
|
+
console.error(`pgserve daemon: already running, pid ${err.pid}`);
|
|
91
|
+
process.exit(1);
|
|
92
|
+
}
|
|
93
|
+
console.error('pgserve daemon: failed to start:', err.message);
|
|
94
|
+
process.exit(1);
|
|
95
|
+
}
|
|
96
|
+
const dir = resolveControlSocketDir();
|
|
97
|
+
console.log(`
|
|
98
|
+
pgserve daemon — singleton mode
|
|
99
|
+
|
|
100
|
+
Control socket: ${resolveControlSocketPath(dir)}
|
|
101
|
+
PID lock: ${path.join(dir, 'pgserve.pid')}
|
|
102
|
+
PG socket: ${daemon.pgManager.getSocketPath() || '(TCP fallback)'}
|
|
103
|
+
|
|
104
|
+
Connect: psql 'host=${dir} dbname=mydb'
|
|
105
|
+
|
|
106
|
+
Press Ctrl+C or send SIGTERM to stop.
|
|
107
|
+
`);
|
|
108
|
+
|
|
109
|
+
// Daemon installs its own SIGTERM/SIGINT handlers; just wait forever.
|
|
110
|
+
await new Promise(() => {});
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function parseDaemonArgs(daemonArgs) {
|
|
114
|
+
const opts = {
|
|
115
|
+
baseDir: null,
|
|
116
|
+
useRam: false,
|
|
117
|
+
logLevel: 'info',
|
|
118
|
+
autoProvision: true,
|
|
119
|
+
tcpListens: [],
|
|
120
|
+
};
|
|
121
|
+
for (let i = 0; i < daemonArgs.length; i++) {
|
|
122
|
+
const arg = daemonArgs[i];
|
|
123
|
+
switch (arg) {
|
|
124
|
+
case '--data':
|
|
125
|
+
case '-d':
|
|
126
|
+
opts.baseDir = daemonArgs[++i];
|
|
127
|
+
break;
|
|
128
|
+
case '--ram':
|
|
129
|
+
opts.useRam = true;
|
|
130
|
+
break;
|
|
131
|
+
case '--log':
|
|
132
|
+
case '-l':
|
|
133
|
+
opts.logLevel = daemonArgs[++i];
|
|
134
|
+
break;
|
|
135
|
+
case '--no-provision':
|
|
136
|
+
opts.autoProvision = false;
|
|
137
|
+
break;
|
|
138
|
+
case '--listen':
|
|
139
|
+
opts.tcpListens.push(daemonArgs[++i]);
|
|
140
|
+
break;
|
|
141
|
+
case '--help':
|
|
142
|
+
console.log(`
|
|
143
|
+
pgserve daemon — singleton control-socket mode
|
|
144
|
+
|
|
145
|
+
USAGE:
|
|
146
|
+
pgserve daemon [options]
|
|
147
|
+
pgserve daemon stop
|
|
148
|
+
pgserve daemon issue-token --fingerprint <hex>
|
|
149
|
+
pgserve daemon revoke-token <id>
|
|
150
|
+
|
|
151
|
+
OPTIONS:
|
|
152
|
+
--data <path> Persistent data directory (default: in-memory)
|
|
153
|
+
--ram Use /dev/shm storage (Linux only)
|
|
154
|
+
--log <level> Log level: error|warn|info|debug (default: info)
|
|
155
|
+
--no-provision Disable auto-provisioning of databases
|
|
156
|
+
--listen [host:]port Bind opt-in TCP listener (repeatable)
|
|
157
|
+
--help Show this help
|
|
158
|
+
|
|
159
|
+
The daemon binds $XDG_RUNTIME_DIR/pgserve/control.sock (fallback /tmp/pgserve/control.sock).
|
|
160
|
+
A second invocation while the first is running exits with "already running".
|
|
161
|
+
|
|
162
|
+
TCP peers (--listen) MUST authenticate via libpq application_name shaped
|
|
163
|
+
"?fingerprint=<12hex>&token=<bearer>". Issue tokens with
|
|
164
|
+
"pgserve daemon issue-token --fingerprint <hex>". Revoke with
|
|
165
|
+
"pgserve daemon revoke-token <id>".
|
|
166
|
+
`);
|
|
167
|
+
process.exit(0);
|
|
168
|
+
// falls through (unreachable)
|
|
169
|
+
default:
|
|
170
|
+
if (arg.startsWith('-')) {
|
|
171
|
+
console.error(`Unknown daemon option: ${arg}`);
|
|
172
|
+
process.exit(1);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return opts;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
async function runIssueTokenSubcommand(args) {
|
|
180
|
+
let fingerprint = null;
|
|
181
|
+
for (let i = 0; i < args.length; i++) {
|
|
182
|
+
const arg = args[i];
|
|
183
|
+
if (arg === '--fingerprint') fingerprint = args[++i];
|
|
184
|
+
else if (arg === '--help') {
|
|
185
|
+
console.log(`
|
|
186
|
+
pgserve daemon issue-token --fingerprint <12hex>
|
|
187
|
+
|
|
188
|
+
Issues a fresh bearer token for an existing fingerprint. Prints the token
|
|
189
|
+
to stdout exactly once; only the sha256 hash is persisted. Use the printed
|
|
190
|
+
value in libpq application_name shaped "?fingerprint=<hex>&token=<bearer>".
|
|
191
|
+
`);
|
|
192
|
+
process.exit(0);
|
|
193
|
+
} else {
|
|
194
|
+
console.error(`Unknown option: ${arg}`);
|
|
195
|
+
process.exit(1);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
if (!fingerprint || !/^[0-9a-f]{12}$/.test(fingerprint)) {
|
|
199
|
+
console.error('issue-token: --fingerprint <12hex> required');
|
|
200
|
+
process.exit(1);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
let admin;
|
|
204
|
+
try {
|
|
205
|
+
const dir = resolveControlSocketDir();
|
|
206
|
+
const disc = readAdminDiscovery(dir);
|
|
207
|
+
admin = await createAdminClient({ socketDir: disc.socketDir, port: disc.port });
|
|
208
|
+
} catch (err) {
|
|
209
|
+
console.error('issue-token: cannot reach running daemon admin socket:', err.message);
|
|
210
|
+
console.error('Hint: start the daemon first with `pgserve daemon`.');
|
|
211
|
+
process.exit(1);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
try {
|
|
215
|
+
await ensureMetaSchema(admin);
|
|
216
|
+
const { id, cleartext, hash } = mintToken();
|
|
217
|
+
const result = await addAllowedToken(admin, {
|
|
218
|
+
fingerprint,
|
|
219
|
+
tokenId: id,
|
|
220
|
+
tokenHash: hash,
|
|
221
|
+
});
|
|
222
|
+
audit(AUDIT_EVENTS.TCP_TOKEN_ISSUED, {
|
|
223
|
+
fingerprint,
|
|
224
|
+
token_id: id,
|
|
225
|
+
database: result.databaseName,
|
|
226
|
+
});
|
|
227
|
+
console.log('Token issued. Save the bearer value below — it will not be shown again:');
|
|
228
|
+
console.log('');
|
|
229
|
+
console.log(` id: ${id}`);
|
|
230
|
+
console.log(` fingerprint: ${fingerprint}`);
|
|
231
|
+
console.log(` database: ${result.databaseName}`);
|
|
232
|
+
console.log(` token: ${cleartext}`);
|
|
233
|
+
console.log('');
|
|
234
|
+
console.log('Use as libpq application_name:');
|
|
235
|
+
console.log(` application_name='?fingerprint=${fingerprint}&token=${cleartext}'`);
|
|
236
|
+
process.exit(0);
|
|
237
|
+
} catch (err) {
|
|
238
|
+
if (err.code === 'EUNKNOWNFINGERPRINT') {
|
|
239
|
+
console.error(`issue-token: fingerprint ${fingerprint} not provisioned yet.`);
|
|
240
|
+
console.error('Connect once via Unix socket so pgserve creates the database first.');
|
|
241
|
+
process.exit(2);
|
|
242
|
+
}
|
|
243
|
+
console.error('issue-token failed:', err.message);
|
|
244
|
+
process.exit(1);
|
|
245
|
+
} finally {
|
|
246
|
+
try { await admin.end(); } catch { /* swallow */ }
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
async function runRevokeTokenSubcommand(args) {
|
|
251
|
+
if (args.length === 0 || args[0] === '--help') {
|
|
252
|
+
console.log('Usage: pgserve daemon revoke-token <id>');
|
|
253
|
+
process.exit(args.length === 0 ? 1 : 0);
|
|
254
|
+
}
|
|
255
|
+
const tokenId = args[0];
|
|
256
|
+
|
|
257
|
+
let admin;
|
|
258
|
+
try {
|
|
259
|
+
const dir = resolveControlSocketDir();
|
|
260
|
+
const disc = readAdminDiscovery(dir);
|
|
261
|
+
admin = await createAdminClient({ socketDir: disc.socketDir, port: disc.port });
|
|
262
|
+
} catch (err) {
|
|
263
|
+
console.error('revoke-token: cannot reach running daemon admin socket:', err.message);
|
|
264
|
+
process.exit(1);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
try {
|
|
268
|
+
const affected = await revokeAllowedToken(admin, tokenId);
|
|
269
|
+
if (affected === 0) {
|
|
270
|
+
console.error(`revoke-token: no token with id ${tokenId} found`);
|
|
271
|
+
process.exit(2);
|
|
272
|
+
}
|
|
273
|
+
console.log(`Token ${tokenId} revoked (affected ${affected} row${affected === 1 ? '' : 's'})`);
|
|
274
|
+
process.exit(0);
|
|
275
|
+
} catch (err) {
|
|
276
|
+
console.error('revoke-token failed:', err.message);
|
|
277
|
+
process.exit(1);
|
|
278
|
+
} finally {
|
|
279
|
+
try { await admin.end(); } catch { /* swallow */ }
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
31
283
|
/**
|
|
32
284
|
* Print usage help
|
|
33
285
|
*/
|
package/eslint.config.js
CHANGED