Repository navigation
refactor(config): env accessors for secrets, operators, port and snapshot params (#47) - #97
SherlockOmss wants to merge 1 commit into
Conversation
|
Pushed b36878d (first push). Files: lib/env.ts, lib/tenant.ts, lib/cron-auth.ts, lib/platform-operator.ts, lib/session-lookup.ts, lib/snapshot-publish.ts, app/api/cron/build-snapshots/route.ts, instrumentation-node.ts, docs/configuration.md, 7 unit test files. Local checks: tsc clean, lint 0 errors (113 warnings), unit 1499 passed. CI: pending at the time of writing. |
…arams through lib/env (#47) Add proxySecret, cronSecret, platformOperatorIds and listenPort to lib/env.ts and use them in lib/tenant.ts, lib/cron-auth.ts, lib/platform-operator.ts and lib/session-lookup.ts. Move the snapshot parameter builder to lib/snapshot-publish.ts as snapshotParamsFromEnv. Check PORT and the SNAPSHOT_* numbers at start with AUTH_TRUSTED_ORIGINS. An unset or empty secret still refuses every call. Refs #47 Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Signed-off-by: SherlockOmss <[email protected]>
b36878d to
06226ca
Compare
SoFrexs
left a comment
There was a problem hiding this comment.
Reviewed 06226ca.
The secrets keep failing closed. proxySecret() and cronSecret() return undefined for unset or empty, and the callers still refuse (500 for cron; verifyProxyAuth throws "not configured"). An empty or missing operator list still allows nobody. No new error message carries a secret: only PORT and SNAPSHOT_* values are echoed. The SNAPSHOT_* defaults and validation moved unchanged. Renaming the local to presentedSecret in verifyProxyAuth avoids shadowing the new import, and a listenPort throw inside lookupSession is caught and returns null, so that path still fails closed.
[should-fix] lib/env.ts:101: listenPort accepts values Next reads differently. It uses Number(raw), while the standalone server listens on parseInt(process.env.PORT, 10) || 3000. So PORT=0x1F90 passes the start check as 8080 while Next listens on 3000, and PORT=1e3 gives 1000 against 1. The middleware then calls the session route on the wrong port and every console request redirects to /login, which is the mistake a start-time check is there to catch. Accept only /^\d+$/, then the range check, and add "0x1F90" and "1e3" to the "throws on anything else" test.
[should-fix] A bad SNAPSHOT_* value now stops the whole server. Before, it failed only the build-snapshots job; now ingest goes down with it, even when SYNTHAEA_SNAPSHOT_SIGNER is unset and no snapshot is ever built. Failing at start is defensible and #47 asks for it, but name it under "Behavior" next to the PORT change so whoever deploys knows. If it isn't intended, check SNAPSHOT_* only when a signer is configured.
[should-fix] Closes #47 closes what this PR leaves out. #47 also asks to move the lake/prevalence parameter builders into their lib modules, and names zod. Skipping both is fine, but closing the issue loses the reasons: record them in #47, or use Refs #47 with a follow-up for PREVALENCE_*/LAKE_WRITER_MAX_BATCHES start-up validation.
[nit] lib/session-lookup.ts:13-14: two blank lines after the import.
[nit] paramsFromEnv's doc comment was dropped. "Provisional starting points, set from the environment until slice 6 calibrates them" is a why-comment; keep it on snapshotParamsFromEnv or DEFAULT_SNAPSHOT_PARAMS.
[nit] Tests set the environment two ways. The new verifyProxyAuth block in tenant.test.ts and the cron-auth.test.ts case save and restore process.env by hand; platform-operator.test.ts and session-lookup.test.ts use vi.stubEnv/vi.unstubAllEnvs. Prefer vi.stubEnv: process.env.X = saved with saved undefined stores the string "undefined", a latent cross-test leak.
Verified
- Diff and #47 read; the only direct env reads left are
NODE_ENVinlib/prisma.tsand a doc comment incron-auth.ts. tsc --noEmitclean;npm run lint0 errors, 113 warnings (the ceiling);vitest run tests/unit1487 passed, 5 skipped (Windows).- The
NumbervsparseIntdifference, in node. - Not run: integration tests (no Docker here).
🤖 Generated with Claude Code
| /** The port the Next.js server listens on (`PORT`, 3000 when unset or empty). A value that is not 1-65535 throws. */ | ||
| export function listenPort(env: Environment): number { | ||
| const raw = env.PORT; | ||
| if (raw === undefined || raw === "") return 3000; |
There was a problem hiding this comment.
[should-fix] Number(raw) accepts 0x1F90 (8080) and 1e3 (1000), but the standalone server listens on parseInt(process.env.PORT, 10) || 3000 (3000 and 1). Accept /^\d+$/ only, and add both values to the "throws" test.
| */ | ||
| import { listenPort } from "@/lib/env"; | ||
|
|
||
|
|
There was a problem hiding this comment.
[nit] Double blank line after the import.
#53) (#100) * refactor(storage): one module for the storage root and its layout (Refs #53) Eight callers built `path.join(process.cwd(), "storage", ...)` inline. lib/storage-paths.ts now names the root (storageRoot, storagePath) and the layouts used more than once (artifactsDir, artifactFile, corpusExportDir). keyRotationFile and releaseArtifactFile keep their signatures and build on storagePath. Behaviour is unchanged: the root is still storage/ under the working directory, read on each call, so tests that chdir into a sandbox keep working. Two callers had a local variable named like the new helper (artifactsDir in the IOC feed route, storagePath in loadManifestBytes); they are renamed, since the helper would otherwise be read inside its own initializer. Relocating the root (STORAGE_DIR, as #53 proposes) is left for a follow-up through lib/env.ts once #97 lands, to avoid two PRs editing the same accessors. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Jihair54 <[email protected]> * refactor(storage): drop three uncalled path builders, group the new imports Review of #100: contentArtifactPath, contentManifestPath and releaseManifestPath had no caller in app/, lib/, scripts/ or tests/, and contentArtifactPath named the artifact layout a second time. They are removed; the #804 rationale that sat on contentArtifactPath moves to artifactFile. The storage-paths import joins the other @/lib imports in the artifact route and corpus.ts. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Jihair54 <[email protected]> --------- Signed-off-by: Jihair54 <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
Second PR of SV-014 (#47); the first was #90.
What changes
lib/env.tsgainsproxySecret,cronSecret,platformOperatorIdsandlistenPort.lib/tenant.ts,lib/cron-auth.ts,lib/platform-operator.tsandlib/session-lookup.tsread through them instead ofprocess.env.paramsFromEnvleavesapp/api/cron/build-snapshots/route.tsand becomessnapshotParamsFromEnvinlib/snapshot-publish.ts.assertStartupConfigalso checksPORTand theSNAPSHOT_*numbers, so a wrong value logsInvalid configuration, exitingand exits with status 1.docs/configuration.mdsays which values are checked at start.Behavior
Unchanged where it matters: an unset or empty
NGINX_PROXY_SECRETorCRON_SECRETstill refuses every call, an unset or emptyPLATFORM_OPERATOR_USER_IDSstill allows nobody, and no secret is required to start. One difference:PORT=""now means 3000 (it gave127.0.0.1:before), and aPORToutside 1-65535 stops the server at start.Not done, on purpose
lib/env.tsis imported by the Edge middleware. The issue named zod.LAKE_WRITER_MAX_BATCHESandPREVALENCE_*stay in their routes (one line each, already throughpositiveIntFromEnv) and are not checked at start.Checks
tsc --noEmitclean;npm run lint0 errors, 113 warnings (same as main);vitest run tests/unit1499 passed, 1 skipped.env.test.ts(accessors),platform-operator.test.ts(new file),tenant.test.ts(verifyProxyAuth),cron-auth.test.ts(empty secret),snapshot-publish.test.ts,session-lookup.test.ts(PORT),instrumentation.test.ts.admin-tenantssetsPLATFORM_OPERATOR_USER_IDS), and a real Next start with a bad value.Closes #47
🤖 Generated with Claude Code