@starci/hfs 4.11.11 → 4.11.13

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.
Files changed (34) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/README.md +1 -1
  3. package/package.json +1 -1
  4. package/release/index.mjs +2 -2
  5. package/runtime/knowledge/hfs/canon-pins.yaml +6 -6
  6. package/runtime/knowledge/hfs/rules.yaml +1 -1
  7. package/runtime/modules/kernel/failure-codes.yaml +5 -5
  8. package/runtime/scripts/api/sops/lib.mjs +25 -3
  9. package/runtime/scripts/api/sops/seal.mjs +4 -3
  10. package/runtime/scripts/hfs/path-findings.mjs +4 -2
  11. package/runtime/scripts/hfs/secret-init.mjs +15 -0
  12. package/runtime/scripts/hfs/secret-sops.mjs +65 -33
  13. package/runtime/scripts/lib/sops-envelope.mjs +36 -11
  14. package/scaffold/add-inbox-lite.mjs +6 -10
  15. package/scaffold/add-queue-lite.mjs +7 -2
  16. package/scaffold/add-table.mjs +5 -0
  17. package/scaffold/app.mjs +15 -3
  18. package/scaffold/lite-exports.mjs +23 -0
  19. package/src/main.mjs +3 -3
  20. package/stack/age-install.mjs +10 -2
  21. package/stack/context.mjs +2 -2
  22. package/stack/remote.mjs +29 -8
  23. package/stack/sealed.mjs +15 -11
  24. package/stack/server-key.mjs +31 -7
  25. package/templates/app/ci-workflows/github/workflows/stack-key-restore.yml +1 -1
  26. package/templates/app/ci-workflows-lite/github/workflows/stack-key-restore.yml +1 -1
  27. package/templates/be/skeleton-lite/src/modules/platform/database/database.options.ts +4 -1
  28. package/templates/be/skeleton-lite/src/modules/platform/database/entities.ts +4 -1
  29. package/templates/be/skeleton-lite/src/modules/platform/database/index.ts +0 -2
  30. package/templates/be/skeleton-lite/src/modules/platform/http-security/index.ts +1 -0
  31. package/templates/be/skeleton-lite/src/modules/platform/http-security/persistence/entities/index.ts +4 -0
  32. package/templates/be/skeleton-lite/src/modules/platform/http-security/persistence/entities/rate-window.entity.ts +17 -0
  33. package/templates/be/skeleton-lite/src/modules/platform/http-security/rate-limit.guard.spec.ts +260 -0
  34. package/templates/be/skeleton-lite/src/modules/platform/http-security/rate-limit.guard.ts +133 -0
package/scaffold/app.mjs CHANGED
@@ -353,13 +353,25 @@ const CLI_APP_DIR = 'apps/cli/';
353
353
  const declaresCliApp = app => app.sides.be.apps.some(entry => entry.kind === 'cli' && entry.name === 'cli');
354
354
 
355
355
  const LITE_APP_REUSED = new Set(['.editorconfig', '.gitattributes', '.nvmrc']);
356
+ /**
357
+ * The lite be files a generator writes when it first needs them, never the starter: the cli's runners, the in-order relay, and the
358
+ * raw SQL helpers of platform/database (the sql tag, and the database-function call statement with its spec), which only the table,
359
+ * queue, jobs and webhook capabilities call; a fresh app reads and writes through entities alone (R334).
360
+ */
356
361
  const LITE_DEFERRED_GENERATOR_FILES = new Set([
362
+ 'src/modules/platform/database/database.client.ts',
363
+ 'src/modules/platform/database/database.client.spec.ts',
364
+ 'src/modules/platform/database/database.sql.ts',
357
365
  'src/modules/platform/database/migration-runner.service.ts',
358
366
  'src/modules/platform/database/seed-runner.service.ts',
359
367
  'src/modules/platform/primitives/sequence.policy.ts',
360
368
  ]);
361
- /** The TypeORM schema files of a platform capability: lite's schema authority is db/migrations (be.persistence lite overlay). */
362
- const LITE_TYPEORM_SCHEMA = /^src\/modules\/platform\/[^/]+\/persistence\/(?:connection\.ts$|entities\/|migrations\/)/;
369
+ /**
370
+ * The persistence folder of a full platform capability (TypeORM migrations, connection arrays, `sql`-tagged statements and rows):
371
+ * lite's schema authority is db/migrations and its code reads and writes through entities (be.persistence lite overlay, R334), so a
372
+ * lite platform capability takes its persistence/ from the lite overlay alone.
373
+ */
374
+ const FULL_PLATFORM_PERSISTENCE = /^src\/modules\/platform\/[^/]+\/persistence\//;
363
375
  const liteBaseSource = (rel) => !rel.startsWith('apps/cli/')
364
376
  && !rel.startsWith('src/features/cli/')
365
377
  && !rel.startsWith('src/modules/domain/note/')
@@ -367,7 +379,7 @@ const liteBaseSource = (rel) => !rel.startsWith('apps/cli/')
367
379
  && rel !== 'src/modules/platform/database/migrate-connections.client.ts'
368
380
  && rel !== 'src/modules/platform/database/seed-connections.client.ts'
369
381
  && !LITE_DEFERRED_GENERATOR_FILES.has(rel)
370
- && !LITE_TYPEORM_SCHEMA.test(rel)
382
+ && !FULL_PLATFORM_PERSISTENCE.test(rel)
371
383
  && !rel.startsWith('src/tests/');
372
384
  /** The be files the lite overlay replaces: a base unit spec of such a file tests the full variant, so lite carries its own. */
373
385
  const liteOverlaid = (() => {
@@ -51,6 +51,22 @@ const REGISTRATIONS = Object.freeze({
51
51
  names: ["sql"],
52
52
  beforeSource: "./errors/database.error",
53
53
  },
54
+ {
55
+ // The outbox write and the relay call the queue functions of their migration (R334).
56
+ file: "be/src/modules/platform/database/index.ts",
57
+ source: "./database.client",
58
+ names: ["callDbFunction"],
59
+ beforeSource: "./primary.decorators",
60
+ },
61
+ ],
62
+ // The jobs capability (add job, the fenced-job pattern): its tick takes a transaction-scoped lock before it claims the minute (R335).
63
+ jobs: [
64
+ {
65
+ file: "be/src/modules/platform/database/index.ts",
66
+ source: "./database.client",
67
+ names: ["advisoryXactLock", "callDbFunction"],
68
+ beforeSource: "./primary.decorators",
69
+ },
54
70
  ],
55
71
  webhook: [
56
72
  {
@@ -59,6 +75,13 @@ const REGISTRATIONS = Object.freeze({
59
75
  names: ["BATCH_ROWS", "sql"],
60
76
  beforeSource: "./errors/database.error",
61
77
  },
78
+ {
79
+ // The inbox claim and the lite event bus call the functions of their migration (R334).
80
+ file: "be/src/modules/platform/database/index.ts",
81
+ source: "./database.client",
82
+ names: ["callDbFunction"],
83
+ beforeSource: "./primary.decorators",
84
+ },
62
85
  {
63
86
  // PRIMARY_ENTITY_MANAGER: the token the inbox service's unit spec provides its EntityManager double under.
64
87
  file: "be/src/modules/platform/database/index.ts",
package/src/main.mjs CHANGED
@@ -435,8 +435,8 @@ function declaredEdition(cwd) {
435
435
  * `starci app stack`: the deploy actions (prepare, doctor, render, deploy, converge, verify, rollback, prune, backup, jobs, ...) are the Swarm
436
436
  * tooling of stack/cli.mjs; up, down and status are the full edition's test-world infrastructure.
437
437
  */
438
- async function stackAppMain(argv, { cwd, edition, stackMain, stdout: out, stderr: err, env, sops, stackIo }) {
439
- if (STACK_ACTIONS.includes(argv[0])) return deployStackMain(argv, { ...stackIo, cwd, stdout: out, stderr: err, env, ...(sops ? { sops } : {}) });
438
+ async function stackAppMain(argv, { cwd, edition, stackMain, stdout: out, stderr: err, env, sops, hostIdentity, stackIo }) {
439
+ if (STACK_ACTIONS.includes(argv[0])) return deployStackMain(argv, { ...stackIo, cwd, stdout: out, stderr: err, env, hostIdentity, ...(sops ? { sops } : {}) });
440
440
  if ((edition ?? declaredEdition(cwd)) === 'lite') throw new Error('starci app stack up|down|status manage the local test world of the full edition; a lite app starts its local database with npm run db:start');
441
441
  const run = stackMain ?? await installedStackMain(cwd);
442
442
  return run(argv, { cwd, out, err });
@@ -462,7 +462,7 @@ function errorMessage(error) {
462
462
  * `harness` ({ version }) is the CLI that runs this package in-process: its version is the starci-harness an app is judged and
463
463
  * scaffolded at (runtime/scripts/hfs/canon-pins.mjs). `run` injects the database runner, `runLinter` the linter transport, `checkApp`
464
464
  * the HFS check and `stackIo` Swarm tools; production defaults execute every check. `hostIdentity` ({ label, read }) is the
465
- * harness CLI's own secret.env SOPS_AGE_KEY, which `secret` reads last for the age identity; `prompt` replaces its masked prompt.
465
+ * harness CLI's own secret.env SOPS_AGE_KEY, which every verb that opens a sealed member (secret, stack, release, db) reads last for the age identity (decryptionIdentity); `prompt` replaces its masked prompt.
466
466
  */
467
467
  export async function main(argv, {
468
468
  cwd: baseCwd = process.cwd(), harness, stdout = (s) => process.stdout.write(s), stderr = (s) => process.stderr.write(s), presets, prettier, lock, run, stackMain,
@@ -17,10 +17,18 @@ const STATIC_INSTALL = [
17
17
  `echo "${AGE_SHA256} $tmp/age.tgz" | sha256sum -c -`,
18
18
  'tar -xzf "$tmp/age.tgz" -C "$tmp"',
19
19
  'sudo -n install -m 0755 "$tmp/age/age" "$tmp/age/age-keygen" /usr/local/bin/',
20
+ // sudo's secure_path (AlmaLinux 10, RHEL) leaves /usr/local/bin out: a link in /usr/bin makes both tools visible to the deploy
21
+ // user and to root alike; an existing /usr/bin tool (a packaged age) is kept.
22
+ 'for b in age age-keygen; do if [ ! -e "/usr/bin/$b" ]; then sudo -n ln -s "/usr/local/bin/$b" "/usr/bin/$b"; fi; done',
20
23
  ].join('; ');
21
24
 
22
25
  const packageManager = (run) => run('for m in dnf yum apk apt-get; do command -v $m >/dev/null 2>&1 && echo $m && break; done').stdout.trim();
23
- const present = (run) => run('command -v age').status === 0;
26
+ /**
27
+ * Both tools, as the deploy user and, where passwordless sudo exists, as root: an install the root PATH cannot see is not taken
28
+ * for present.
29
+ */
30
+ const PRESENT = 'command -v age >/dev/null 2>&1 && command -v age-keygen >/dev/null 2>&1 && { [ "$(id -u)" -eq 0 ] || ! sudo -n true 2>/dev/null || sudo -n sh -c \'command -v age >/dev/null 2>&1 && command -v age-keygen >/dev/null 2>&1\'; }';
31
+ const present = (run) => run(PRESENT).status === 0;
24
32
 
25
33
  /** Installs age when it is missing; reports through `report`. */
26
34
  export function installAge(run, report) {
@@ -33,6 +41,6 @@ export function installAge(run, report) {
33
41
  failure = `${manager} could not install age: ${firstLine(packaged.stderr)}`;
34
42
  }
35
43
  const fetched = run(STATIC_INSTALL);
36
- if (fetched.status === 0 && present(run)) report.note(`installed the official static age ${AGE_VERSION} (sha256 verified) into /usr/local/bin; ${failure}`);
44
+ if (fetched.status === 0 && present(run)) report.note(`installed the official static age ${AGE_VERSION} (sha256 verified) into /usr/local/bin, linked from /usr/bin; ${failure}`);
37
45
  else report.bad(`cannot install age (${failure}); the static install failed too: ${firstLine(fetched.stderr)} (needs passwordless sudo once, or install age by hand)`);
38
46
  }
package/stack/context.mjs CHANGED
@@ -57,7 +57,7 @@ const realSleep = (ms) => new Promise((done) => { setTimeout(done, ms); });
57
57
  * stdout, stderr text writers
58
58
  * fetch the HTTP seam of verify (url, init) -> Response
59
59
  * pipe (source, sink) -> Promise<{ source, sink, stderr }>: one process's stdout streamed into another's stdin (realPipe)
60
- * sops the secret seam (decrypt(file, format) -> map; decryptBinary(file) -> text); `io.sops`, else on first use `io.openSops()` or the app's own sops/age tools
60
+ * sops the secret seam (decrypt(file, format) -> map; decryptBinary(file) -> text); `io.sops`, else on first use `io.openSops()` or the app's own sops/age tools with the age identity decryptionIdentity finds (`io.hostIdentity` the harness CLI's secret.env)
61
61
  */
62
62
  export function stackContext(io = {}) {
63
63
  const cwd = io.cwd ?? process.cwd();
@@ -78,7 +78,7 @@ export function stackContext(io = {}) {
78
78
  pipe: io.pipe ?? realPipe,
79
79
  /** The secret seam: the app's own sops/age tools, created on first use so verbs that open no custody never need them. */
80
80
  get sops() {
81
- sops ??= io.openSops?.() ?? defaultSops(env, cwd);
81
+ sops ??= io.openSops?.() ?? defaultSops(env, cwd, io.platform ?? process.platform, undefined, io.hostIdentity ?? null);
82
82
  return sops;
83
83
  },
84
84
  });
package/stack/remote.mjs CHANGED
@@ -64,24 +64,36 @@ function sshCommandArgv(sshCmd) {
64
64
  return parts;
65
65
  }
66
66
 
67
- /** A sealed binary member is the file itself; a JSON object around it ({ data | <names> }, the re-sealed shape of `starci app secret rename`) is unwrapped. */
68
- function unwrap(text, names) {
67
+ /**
68
+ * The key each sealed SSH member of `deploy.remote.sealed` holds (contexts/project/stacks.md PROJECT-STACKS-5): the one owner of
69
+ * these names. `sshKey` names the member whose `privateKey` is the PEM private key, `knownHosts` the member whose `knownHosts` is
70
+ * the pinned known_hosts text.
71
+ */
72
+ export const SEALED_SSH_KEYS = Object.freeze({ sshKey: 'privateKey', knownHosts: 'knownHosts' });
73
+
74
+ /** A legacy binary member is the file itself; a JSON object around it ({ data | <key> }, the re-sealed shape of `starci app secret rename`) is unwrapped. */
75
+ function unwrap(text, key) {
69
76
  const body = lfOnly(text);
70
77
  const trimmed = body.trim();
71
78
  if (!trimmed.startsWith('{')) return body;
72
79
  let parsed;
73
80
  try { parsed = JSON.parse(trimmed); } catch { return body; }
74
- for (const name of ['data', ...names]) if (typeof parsed[name] === 'string') return lfOnly(parsed[name]);
81
+ for (const name of ['data', key]) if (typeof parsed[name] === 'string') return lfOnly(parsed[name]);
75
82
  return body;
76
83
  }
77
84
 
85
+ /** The sops format a sealed member is read as: a `.enc.yaml` member is YAML, a legacy `.enc` one dotenv. */
86
+ const memberFormat = (file) => (isLegacyName(file) ? 'dotenv' : 'yaml');
87
+
88
+ /** Opens one member through the sops seam; a failure is refused naming the member and the seam's own reason (format, recipients, identity). */
78
89
  function openMember(slug, read) {
79
90
  try { return read(); } catch (error) { throw refusal('STACK_REMOTE_SEALED', `cannot open the sealed member ${slug}: ${error.message}`); }
80
91
  }
81
92
 
82
93
  function opsAccount(ctx, config) {
83
94
  const slug = config.remote.sealed.ops;
84
- const map = openMember(slug, () => ctx.sops.decrypt(memberFile(config.secretsDir, slug), isLegacyName(memberFile(config.secretsDir, slug)) ? 'dotenv' : 'yaml'));
95
+ const file = memberFile(config.secretsDir, slug);
96
+ const map = openMember(slug, () => ctx.sops.decrypt(file, memberFormat(file)));
85
97
  for (const name of ['VPS_HOST', 'VPS_USER']) {
86
98
  const value = map[name];
87
99
  if (typeof value !== 'string' || !ACCOUNT.test(value) || value.startsWith('-')) throw refusal('STACK_REMOTE_OPS', `${slug} must hold ${name} (letters, digits, . _ @ -)`);
@@ -89,8 +101,17 @@ function opsAccount(ctx, config) {
89
101
  return `${map.VPS_USER}@${map.VPS_HOST}`;
90
102
  }
91
103
 
92
- function binaryMember(ctx, config, slug, names) {
93
- return unwrap(openMember(slug, () => ctx.sops.decryptBinary(memberFile(config.secretsDir, slug))), names);
104
+ /**
105
+ * The text a sealed SSH member holds under `key` (SEALED_SSH_KEYS): a `.enc.yaml` member is read as YAML and the key taken from
106
+ * its map; a legacy `.enc` member is the sops binary or JSON envelope it was sealed as.
107
+ */
108
+ function sshMember(ctx, config, slug, key) {
109
+ const file = memberFile(config.secretsDir, slug);
110
+ if (isLegacyName(file)) return unwrap(openMember(slug, () => ctx.sops.decryptBinary(file)), key);
111
+ const map = openMember(slug, () => ctx.sops.decrypt(file, 'yaml'));
112
+ if (typeof map[key] !== 'string' || map[key] === '')
113
+ throw refusal('STACK_REMOTE_SEALED', `the sealed member ${slug} holds no ${key} (it holds ${Object.keys(map).join(', ') || 'no key'}): seal it with starci app secret set ${config.env ?? '<env>'} ${slug} ${key} --from-file ${key}=<file>`);
114
+ return lfOnly(map[key]);
94
115
  }
95
116
 
96
117
  const withNewline = (text) => `${text.trim()}\n`;
@@ -107,9 +128,9 @@ export function openSealedSession(ctx, config) {
107
128
  const account = opsAccount(ctx, config);
108
129
  if (config.nativeTarget && account !== `${config.nativeTarget.user}@${config.nativeTarget.host}`)
109
130
  throw refusal('STACK_REMOTE_OPS', 'the sealed SSH account differs from the accepted native target');
110
- const key = binaryMember(ctx, config, sealed.sshKey, ['privateKey', 'key']);
131
+ const key = sshMember(ctx, config, sealed.sshKey, SEALED_SSH_KEYS.sshKey);
111
132
  if (!key.includes('PRIVATE KEY')) throw refusal('STACK_REMOTE_KEY', `${sealed.sshKey} does not hold a PEM private key`);
112
- const known = binaryMember(ctx, config, sealed.knownHosts, ['knownHosts', 'known_hosts']).trim();
133
+ const known = sshMember(ctx, config, sealed.knownHosts, SEALED_SSH_KEYS.knownHosts).trim();
113
134
  assertStackAccount(config.nativeTarget, account, known);
114
135
  const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'stack-ssh-'));
115
136
  const keyFile = path.join(dir, 'id');
package/stack/sealed.mjs CHANGED
@@ -5,7 +5,6 @@ import { createHash, createHmac } from 'node:crypto';
5
5
  import { memberFile } from '../runtime/scripts/lib/sealed-member.mjs';
6
6
  import fs from 'node:fs';
7
7
  import path from 'node:path';
8
- import { secretRoots } from '../runtime/scripts/hfs/secret-files.mjs';
9
8
  import { readMap } from '../runtime/scripts/hfs/secret-store.mjs';
10
9
  import { refusal } from './errors.mjs';
11
10
 
@@ -52,27 +51,32 @@ function secretRuntimeName(stack, key, value, versionKey) {
52
51
  return name;
53
52
  }
54
53
 
55
- const AGE_IDENTITY_ENV = /^SOPS_AGE_KEY(?:_FILE)?$/iu;
56
- const AGE_HINT = 'load the age identity into SOPS_AGE_KEY (the secret.env of the owner machine, or the CI secret) and retry';
54
+ const AGE_HINT = 'add SOPS_AGE_KEY to .claude/secret.env of the app or the harness secret.env of the owner machine (the CI secret in a workflow) and retry';
57
55
 
58
- /** True when an age identity is reachable: SOPS_AGE_KEY or SOPS_AGE_KEY_FILE in the environment, the repository's .claude/secret.env or the per-user keys.txt. */
59
- export function hasAgeIdentity(ctx, config) {
60
- const named = Object.entries(ctx.env).some(([name, value]) => AGE_IDENTITY_ENV.test(name) && value);
61
- return named || fs.existsSync(path.join(config.repoRoot, '.claude', 'secret.env')) || fs.existsSync(secretRoots(ctx.env).identity);
56
+ /**
57
+ * True when the sops seam found an age identity (secret-init.mjs decryptionIdentity, the one lookup every decrypt uses). A seam
58
+ * that reports no `identity` (a spec double) decides for itself.
59
+ */
60
+ export function hasAgeIdentity(ctx) {
61
+ const { identity } = ctx.sops;
62
+ return identity === undefined || identity.source !== null;
62
63
  }
63
64
 
64
65
  /** Refuses up front, before any work, when sealed custody must be opened and no age identity is reachable. */
65
66
  export function requireAgeIdentity(ctx, config, purpose) {
66
- if (!hasAgeIdentity(ctx, config)) throw refusal('STACK_AGE_IDENTITY_MISSING', `${purpose} needs the sealed members, and no age identity is available: ${AGE_HINT}`);
67
+ if (!hasAgeIdentity(ctx)) throw refusal('STACK_AGE_IDENTITY_MISSING', `${purpose} needs the sealed members, and no age identity is available (looked in ${ctx.sops.identity.looked.join(', ')}): ${AGE_HINT}`);
67
68
  }
68
69
 
69
- /** Opens one sealed member (`<secretsDir>/<member>.enc.yaml`, or a legacy `.enc`) into a { KEY: value } map. Names the member only on failure. */
70
+ /**
71
+ * Opens one sealed member (`<secretsDir>/<member>.enc.yaml`, or a legacy `.enc`) into a { KEY: value } map. A failure is refused
72
+ * naming the member and the seam's reason (its format, recipients and the identity tried); no value.
73
+ */
70
74
  export function openMember(ctx, config, member) {
71
75
  try {
72
76
  const map = readMap(memberFile(config.secretsDir, member), ctx.sops);
73
77
  return Object.fromEntries(Object.entries(map).map(([key, value]) => [key, typeof value === 'string' && !value.includes(String.fromCodePoint(0)) ? lfText(value) : value]));
74
- } catch {
75
- throw refusal('STACK_SECRET_MEMBER_UNREADABLE', `the sealed member ${member} cannot be opened${hasAgeIdentity(ctx, config) ? ' (is the age identity a recipient of this member?)' : `: no age identity is available, ${AGE_HINT}`}`);
78
+ } catch (error) {
79
+ throw refusal('STACK_SECRET_MEMBER_UNREADABLE', `the sealed member ${member} cannot be opened: ${error.message}${hasAgeIdentity(ctx) ? '' : `; ${AGE_HINT}`}`);
76
80
  }
77
81
  }
78
82
 
@@ -42,6 +42,22 @@ const RECIPIENT = /^age1[a-z0-9]+$/u;
42
42
  /** A command run as root: directly when the deploy user is root, else over passwordless sudo, the script read from stdin. */
43
43
  const AS_ROOT = 'if [ "$(id -u)" -eq 0 ]; then sh -s; else sudo -n sh -s; fi';
44
44
 
45
+ /**
46
+ * The directories, after root's PATH, where age-keygen is looked for: sudo's secure_path (AlmaLinux 10, RHEL) leaves out
47
+ * /usr/local/bin, where the static installer puts it, so the presence check and every call resolve it the same way, as root.
48
+ */
49
+ export const AGE_KEYGEN_DIRS = ['/usr/local/bin', '/usr/bin', '/bin'];
50
+
51
+ /** The shell lines that set k to age-keygen's absolute path as root sees it, or exit 127 naming it missing. Every root script holds them. */
52
+ export const RESOLVE_AGE_KEYGEN = [
53
+ 'k="$(command -v age-keygen 2>/dev/null || true)"',
54
+ `for d in ${AGE_KEYGEN_DIRS.join(' ')}; do if [ -z "$k" ] && [ -x "$d/age-keygen" ]; then k="$d/age-keygen"; fi; done`,
55
+ 'if [ -z "$k" ]; then echo "age-keygen is not installed" >&2; exit 127; fi',
56
+ ].join('\n');
57
+
58
+ /** The root script that only resolves age-keygen and prints its path: the presence check, run exactly as the calls run. */
59
+ export const ageKeygenScript = () => ['set -eu', RESOLVE_AGE_KEYGEN, String.raw`printf '%s\n' "$k"`].join('\n');
60
+
45
61
  /**
46
62
  * The GitHub Environment secret that holds an environment's server identity. It carries the name the deploy workflows read
47
63
  * (secrets.SOPS_AGE_KEY), scoped to the Environment <env>: for a job bound to <env> it overrides a repository secret of that name.
@@ -54,24 +70,25 @@ export function ensureIdentityScript(file) {
54
70
  return [
55
71
  'set -eu',
56
72
  'umask 077',
73
+ RESOLVE_AGE_KEYGEN,
57
74
  `f=${quoted}`,
58
75
  'if [ -e "$f" ] || [ -L "$f" ]; then',
59
76
  ' if [ -L "$f" ] || [ ! -f "$f" ]; then echo "the identity path is not an ordinary file" >&2; exit 4; fi',
60
77
  ' echo "identity kept" >&2',
61
78
  'else',
62
79
  ' mkdir -p "$(dirname "$f")"',
63
- ' age-keygen -o "$f" >/dev/null 2>&1',
80
+ ' "$k" -o "$f" >/dev/null 2>&1',
64
81
  ' echo "identity created" >&2',
65
82
  'fi',
66
83
  'chown root:root "$f"',
67
84
  'chmod 600 "$f"',
68
- 'age-keygen -y "$f"',
85
+ '"$k" -y "$f"',
69
86
  ].join('\n');
70
87
  }
71
88
 
72
89
  /** The shell that prints only the public key of `file`, or exits 5 when there is none. */
73
90
  export function publicKeyScript(file) {
74
- return ['set -eu', `f=${shellQuote(file)}`, '[ -f "$f" ] || exit 5', 'age-keygen -y "$f"'].join('\n');
91
+ return ['set -eu', RESOLVE_AGE_KEYGEN, `f=${shellQuote(file)}`, '[ -f "$f" ] || exit 5', '"$k" -y "$f"'].join('\n');
75
92
  }
76
93
 
77
94
  /** The shell that swaps the staged `<file>.next` in, keeping the old identity as `retired`, then prints only the new public key. */
@@ -79,6 +96,7 @@ export function swapIdentityScript(file, retired) {
79
96
  return [
80
97
  'set -eu',
81
98
  'umask 077',
99
+ RESOLVE_AGE_KEYGEN,
82
100
  `f=${shellQuote(file)}`,
83
101
  `r=${shellQuote(retired)}`,
84
102
  'n="$f.next"',
@@ -87,7 +105,7 @@ export function swapIdentityScript(file, retired) {
87
105
  'mv "$f" "$r"',
88
106
  'mv "$n" "$f"',
89
107
  'chmod 600 "$f" "$r"',
90
- 'age-keygen -y "$f"',
108
+ '"$k" -y "$f"',
91
109
  ].join('\n');
92
110
  }
93
111
 
@@ -112,14 +130,20 @@ function declaredRemote(repoRoot, env) {
112
130
  return remote;
113
131
  }
114
132
 
115
- /** Ensures age-keygen on the server: stack prepare's installer owns the install (with --execute); without it the exact commands. */
133
+ /** True when root resolves age-keygen the way every server-key script does (ageKeygenScript). */
134
+ const hasKeygen = (run) => asRoot(run, ageKeygenScript()).status === 0;
135
+
136
+ /**
137
+ * Ensures age-keygen on the server, checked as root exactly as it is called: stack prepare's installer owns the install (with
138
+ * --execute); without it the exact commands.
139
+ */
116
140
  function ensureTool(run, execute, lines) {
117
- if (run('command -v age-keygen').status === 0) return true;
141
+ if (hasKeygen(run)) return true;
118
142
  const install = `install age on the server (one of: ${Object.values(AGE_INSTALL).join('; ')})`;
119
143
  if (!execute) { lines.push(`dry run: age-keygen is missing on the server; --execute installs it with stack prepare's installer, or ${install}`); return false; }
120
144
  const problems = [];
121
145
  installAge(run, { note: (text) => lines.push(text), bad: (text) => problems.push(text) });
122
- if (problems.length || run('command -v age-keygen').status !== 0)
146
+ if (problems.length || !hasKeygen(run))
123
147
  throw refusal('STACK_SERVER_KEY_NO_AGE', `age-keygen is not on the server and could not be installed (${problems.join('; ') || 'still missing'}): ${install}`);
124
148
  return true;
125
149
  }
@@ -53,6 +53,6 @@ jobs:
53
53
  printf '%s\n' "$VPS_SSH_KEY" > "$dir/id"
54
54
  printf '%s\n' "$VPS_KNOWN_HOSTS" > "$dir/known_hosts"
55
55
  # The server writes the identity beside its target, keeps it only when its public key is the recipient, then moves it in.
56
- remote="f={{serverAgeIdentity}}; r=$RECIPIENT; "'if [ "$(id -u)" -eq 0 ]; then S=; else S="sudo -n"; fi; if $S test -e "$f"; then cat >/dev/null; echo "the server already holds $f; move it aside first" >&2; exit 3; fi; $S sh -c '"'"'umask 077; f="$1"; r="$2"; d="$(dirname "$f")"; mkdir -p "$d"; t="$(mktemp "$d/.restore.XXXXXX")"; cat > "$t"; if [ "$(age-keygen -y "$t" 2>/dev/null)" != "$r" ]; then rm -f "$t"; echo "the secret is not the identity of $r (or age-keygen is missing); nothing was written" >&2; exit 6; fi; chown root:root "$t"; chmod 600 "$t"; mv "$t" "$f"; age-keygen -y "$f"'"'"' sh "$f" "$r"'
56
+ remote="f={{serverAgeIdentity}}; r=$RECIPIENT; "'if [ "$(id -u)" -eq 0 ]; then S=; else S="sudo -n"; fi; if $S test -e "$f"; then cat >/dev/null; echo "the server already holds $f; move it aside first" >&2; exit 3; fi; $S sh -c '"'"'PATH="$PATH:/usr/local/bin"; umask 077; f="$1"; r="$2"; d="$(dirname "$f")"; mkdir -p "$d"; t="$(mktemp "$d/.restore.XXXXXX")"; cat > "$t"; if [ "$(age-keygen -y "$t" 2>/dev/null)" != "$r" ]; then rm -f "$t"; echo "the secret is not the identity of $r (or age-keygen is missing); nothing was written" >&2; exit 6; fi; chown root:root "$t"; chmod 600 "$t"; mv "$t" "$f"; age-keygen -y "$f"'"'"' sh "$f" "$r"'
57
57
  printf '%s\n' "$AGE_IDENTITY" | ssh -T -i "$dir/id" -o IdentitiesOnly=yes -o UserKnownHostsFile="$dir/known_hosts" \
58
58
  -o StrictHostKeyChecking=yes -o BatchMode=yes -o ConnectTimeout=20 "$VPS_USER@$VPS_HOST" "$remote"
@@ -53,6 +53,6 @@ jobs:
53
53
  printf '%s\n' "$VPS_SSH_KEY" > "$dir/id"
54
54
  printf '%s\n' "$VPS_KNOWN_HOSTS" > "$dir/known_hosts"
55
55
  # The server writes the identity beside its target, keeps it only when its public key is the recipient, then moves it in.
56
- remote="f={{serverAgeIdentity}}; r=$RECIPIENT; "'if [ "$(id -u)" -eq 0 ]; then S=; else S="sudo -n"; fi; if $S test -e "$f"; then cat >/dev/null; echo "the server already holds $f; move it aside first" >&2; exit 3; fi; $S sh -c '"'"'umask 077; f="$1"; r="$2"; d="$(dirname "$f")"; mkdir -p "$d"; t="$(mktemp "$d/.restore.XXXXXX")"; cat > "$t"; if [ "$(age-keygen -y "$t" 2>/dev/null)" != "$r" ]; then rm -f "$t"; echo "the secret is not the identity of $r (or age-keygen is missing); nothing was written" >&2; exit 6; fi; chown root:root "$t"; chmod 600 "$t"; mv "$t" "$f"; age-keygen -y "$f"'"'"' sh "$f" "$r"'
56
+ remote="f={{serverAgeIdentity}}; r=$RECIPIENT; "'if [ "$(id -u)" -eq 0 ]; then S=; else S="sudo -n"; fi; if $S test -e "$f"; then cat >/dev/null; echo "the server already holds $f; move it aside first" >&2; exit 3; fi; $S sh -c '"'"'PATH="$PATH:/usr/local/bin"; umask 077; f="$1"; r="$2"; d="$(dirname "$f")"; mkdir -p "$d"; t="$(mktemp "$d/.restore.XXXXXX")"; cat > "$t"; if [ "$(age-keygen -y "$t" 2>/dev/null)" != "$r" ]; then rm -f "$t"; echo "the secret is not the identity of $r (or age-keygen is missing); nothing was written" >&2; exit 6; fi; chown root:root "$t"; chmod 600 "$t"; mv "$t" "$f"; age-keygen -y "$f"'"'"' sh "$f" "$r"'
57
57
  printf '%s\n' "$AGE_IDENTITY" | ssh -T -i "$dir/id" -o IdentitiesOnly=yes -o UserKnownHostsFile="$dir/known_hosts" \
58
58
  -o StrictHostKeyChecking=yes -o BatchMode=yes -o ConnectTimeout=20 "$VPS_USER@$VPS_HOST" "$remote"
@@ -1,5 +1,8 @@
1
1
  import type { Secret } from "@modules/platform/config"
2
2
 
3
+ /** An entity class as TypeORM registers it. */
4
+ export type EntityClass = new () => object
5
+
3
6
  /** What the primary connection parser reads from the environment. */
4
7
  export interface DatabaseConnectionConfig {
5
8
  /** The logical connection name declared in `hfs.json`. */
@@ -21,5 +24,5 @@ export interface DatabaseOptions {
21
24
  /** The connections this app opens; one per physical database. */
22
25
  readonly connections: ReadonlyArray<DatabaseConnectionOptions>
23
26
  /** The TypeORM entities the connections map: PRIMARY_ENTITIES of `@modules/platform/database/entities`, passed by the app's composition root only. */
24
- readonly entities: ReadonlyArray<Function>
27
+ readonly entities: ReadonlyArray<EntityClass>
25
28
  }
@@ -1,7 +1,10 @@
1
+ import { httpSecurityEntities } from "@modules/platform/http-security"
2
+ import type { EntityClass } from "./database.options"
3
+
1
4
  /**
2
5
  * The entity list of the primary connection: each capability's `<c>Entities` array (its `persistence/entities/index.ts`), by
3
6
  * name, no glob (the entity registry, slot be.platform.database.entities). Only app composition and the test world import this
4
7
  * file, from the deep path `@modules/platform/database/entities`; DatabaseModule and the database barrel never do, since the
5
8
  * capability barrels import the database capability back.
6
9
  */
7
- export const PRIMARY_ENTITIES: ReadonlyArray<Function> = []
10
+ export const PRIMARY_ENTITIES: ReadonlyArray<EntityClass> = [...httpSecurityEntities]
@@ -1,9 +1,7 @@
1
1
  export { DatabaseModule } from "./database.module"
2
- export { advisoryXactLock, callDbFunction, notify } from "./database.client"
3
2
  export { DATABASE_PROBE, InjectDatabaseProbe, InjectPrimaryEntityManager, PRIMARY_ENTITY_MANAGER } from "./primary.decorators"
4
3
  export { DatabaseProbeService } from "./database-probe.service"
5
4
  export type { DatabaseConnectionOptions } from "./database.options"
6
5
  export { PRIMARY_DATA_SOURCE } from "./database.port"
7
- export { sql } from "./database.sql"
8
6
  export { DATABASE_ERROR_KINDS } from "./errors/database.error"
9
7
  export { parsePrimaryDatabaseConfig } from "./primary.config"
@@ -5,6 +5,7 @@ export { HttpSecurityModule } from "./http-security.module"
5
5
  export type { HttpSecurityOptions } from "./http-security.options"
6
6
  export { HTTP_SECURITY_MESSAGES } from "./messages/http-security.messages"
7
7
  export { OriginGuard, ServerToServer } from "./origin.guard"
8
+ export { httpSecurityEntities } from "./persistence/entities"
8
9
  export { RateLimitGuard } from "./rate-limit.guard"
9
10
  export { applyHttpSecurity } from "./edge-security.service"
10
11
  export { ReadinessService } from "./readiness.service"
@@ -0,0 +1,4 @@
1
+ import { RateWindowEntity } from "./rate-window.entity"
2
+
3
+ /** The entities of the http-security capability, named in PRIMARY_ENTITIES of platform/database/entities.ts. */
4
+ export const httpSecurityEntities = [RateWindowEntity]
@@ -0,0 +1,17 @@
1
+ import { Column, Entity, PrimaryColumn } from "typeorm"
2
+
3
+ @Entity({ schema: "public", name: "http_security_rate_windows" })
4
+ /** One rate limit window of one caller and tier, mapped from public.http_security_rate_windows of the baseline migration in db/migrations. */
5
+ export class RateWindowEntity {
6
+ /** The tier and caller address the window counts. */
7
+ @PrimaryColumn({ name: "key", type: "varchar", length: 128 })
8
+ key!: string
9
+
10
+ /** The hits counted in the window. */
11
+ @Column({ name: "hits", type: "integer" })
12
+ hits!: number
13
+
14
+ /** When the window ends; a hit after it opens a fresh one. */
15
+ @Column({ name: "reset_at", type: "timestamptz" })
16
+ resetAt!: Date
17
+ }