@coffre/conformance 0.0.0 → 0.1.1
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/LICENSE +21 -0
- package/README.md +21 -3
- package/bin/coffre-conformance.js +13 -0
- package/dist/idp-DJlqS-JW.js +1122 -0
- package/dist/idp.d.ts +146 -0
- package/dist/idp.js +2 -0
- package/dist/main.js +2653 -0
- package/package.json +46 -5
- package/src/browser.ts +67 -0
- package/src/checks/access.ts +141 -0
- package/src/checks/audit.ts +267 -0
- package/src/checks/canaries.ts +121 -0
- package/src/checks/checkpoints.ts +64 -0
- package/src/checks/live.ts +321 -0
- package/src/checks/logins.ts +107 -0
- package/src/checks/members.ts +167 -0
- package/src/checks/people.ts +186 -0
- package/src/checks/readiness.ts +93 -0
- package/src/checks/signin.ts +130 -0
- package/src/checks/storage.ts +142 -0
- package/src/checks/surface.ts +74 -0
- package/src/database.ts +48 -0
- package/src/fixtures.ts +7 -0
- package/src/harness.ts +275 -0
- package/src/idp/authorize.ts +325 -0
- package/src/idp/github.ts +207 -0
- package/src/idp/http.ts +97 -0
- package/src/idp/idp.ts +331 -0
- package/src/idp/index.ts +5 -0
- package/src/idp/oidc.ts +209 -0
- package/src/idp/pages.ts +107 -0
- package/src/idp/people.ts +105 -0
- package/src/main.ts +123 -0
- package/src/report.ts +106 -0
- package/src/routes.ts +112 -0
- package/src/suite.ts +84 -0
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
// SQL around coffre, as the database's owner: what the checks read, change
|
|
2
|
+
// and put back behind its back, on Postgres or SQLite alike.
|
|
3
|
+
import type { Sql } from '../database.ts';
|
|
4
|
+
import { expect } from '../report.ts';
|
|
5
|
+
|
|
6
|
+
export const TABLES = [
|
|
7
|
+
'projects', 'environments', 'secrets', 'secret_versions', 'vault_members', 'vault_grants',
|
|
8
|
+
'audit_log', 'audit_chain_head', 'identities', 'credentials', 'device_authorizations', 'syncs', 'sync_keys',
|
|
9
|
+
] as const;
|
|
10
|
+
|
|
11
|
+
/** Inspect every table, including any a deployment added. Missing shared tables fail. */
|
|
12
|
+
export async function tables(sql: Sql): Promise<string[]> {
|
|
13
|
+
const rows = await sql.query<{ name: string }>(sql.engine === 'postgres'
|
|
14
|
+
? `SELECT table_name AS name FROM information_schema.tables WHERE table_schema = 'public' AND table_type = 'BASE TABLE'`
|
|
15
|
+
: `SELECT name FROM sqlite_master WHERE type = 'table' AND name NOT LIKE 'sqlite_%'`);
|
|
16
|
+
const names = rows.map((row) => row.name);
|
|
17
|
+
const missing = TABLES.filter((name) => !names.includes(name));
|
|
18
|
+
expect(missing.length === 0, 'shared tables could not be inspected', missing);
|
|
19
|
+
return names;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function quoted(name: string): string {
|
|
23
|
+
return `"${name.replaceAll('"', '""')}"`;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Both drivers return binary columns as bytes; casting records to text hides bytea. */
|
|
27
|
+
export async function scanTables(sql: Sql, names: string[], look: (where: string, bytes: string | Uint8Array) => void): Promise<void> {
|
|
28
|
+
for (const name of names) {
|
|
29
|
+
for (const row of await sql.query(`SELECT * FROM ${quoted(name)}`)) {
|
|
30
|
+
for (const value of Object.values(row)) {
|
|
31
|
+
look(`the database's ${name} table`, value instanceof Uint8Array ? value : JSON.stringify(value) ?? '');
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Parameters use one spelling in the checks, whatever the database. */
|
|
38
|
+
export function query<Row = Record<string, unknown>>(sql: Sql, statement: string, params: unknown[] = []): Promise<Row[]> {
|
|
39
|
+
return sql.query<Row>(sql.engine === 'sqlite' ? statement.replace(/\$\d+/g, '?') : statement, params);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Insert a saved or forged row as it is, but for its generated columns. */
|
|
43
|
+
export async function insertRow(sql: Sql, table: string, row: Record<string, unknown>): Promise<void> {
|
|
44
|
+
const fields = Object.keys(row).filter((field) => field !== 'active_subject');
|
|
45
|
+
await query(sql, `INSERT INTO ${quoted(table)} (${fields.map(quoted).join(', ')}) VALUES (${fields.map((_, i) => `$${i + 1}`).join(', ')})`,
|
|
46
|
+
fields.map((field) => row[field]));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Put a saved row back without changing its bytes or its generated columns. */
|
|
50
|
+
export async function restoreRow(sql: Sql, table: string, row: Record<string, unknown>, key: string): Promise<void> {
|
|
51
|
+
const fields = Object.keys(row).filter((field) => field !== key && field !== 'active_subject');
|
|
52
|
+
await query(sql, `UPDATE ${quoted(table)} SET ${fields.map((field, i) => `${quoted(field)} = $${i + 1}`).join(', ')} WHERE ${quoted(key)} = $${fields.length + 1}`,
|
|
53
|
+
[...fields.map((field) => row[field]), row[key]]);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Keep a deliberate rewrite, and its restoration, invisible until each is complete. */
|
|
57
|
+
export async function transaction<T>(sql: Sql, work: () => Promise<T>): Promise<T> {
|
|
58
|
+
await sql.exec('BEGIN');
|
|
59
|
+
try {
|
|
60
|
+
const result = await work();
|
|
61
|
+
await sql.exec('COMMIT');
|
|
62
|
+
return result;
|
|
63
|
+
} catch (error) {
|
|
64
|
+
await sql.exec('ROLLBACK');
|
|
65
|
+
throw error;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* `work` with the audit log's append-only triggers lifted, as only its owner
|
|
71
|
+
* can: Postgres disables them for the session, SQLite drops them and makes
|
|
72
|
+
* them again after.
|
|
73
|
+
*/
|
|
74
|
+
export async function appendOnlyLifted<T>(sql: Sql, work: () => Promise<T>): Promise<T> {
|
|
75
|
+
if (sql.engine === 'postgres') {
|
|
76
|
+
await sql.exec('ALTER TABLE audit_log DISABLE TRIGGER USER');
|
|
77
|
+
try {
|
|
78
|
+
return await work();
|
|
79
|
+
} finally {
|
|
80
|
+
await sql.exec('ALTER TABLE audit_log ENABLE TRIGGER USER');
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
const triggers = await sql.query<{ name: string; sql: string }>(
|
|
84
|
+
`SELECT name, sql FROM sqlite_master WHERE type = 'trigger' AND tbl_name = 'audit_log'`,
|
|
85
|
+
);
|
|
86
|
+
for (const trigger of triggers) await sql.exec(`DROP TRIGGER ${trigger.name}`);
|
|
87
|
+
try {
|
|
88
|
+
return await work();
|
|
89
|
+
} finally {
|
|
90
|
+
for (const trigger of triggers) await sql.exec(trigger.sql);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export const LOG_REFUSAL = 'coffre-conformance: the log refuses writes';
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* `work` with the audit log refusing appends, of one action or all, as a
|
|
98
|
+
* full disk or a lost connection would, without changing the deployment's
|
|
99
|
+
* code: a trigger, dropped after.
|
|
100
|
+
*/
|
|
101
|
+
export async function logRefuses<T>(sql: Sql, work: () => Promise<T>, action?: string): Promise<T> {
|
|
102
|
+
const when = action === undefined ? '' : ` WHEN (NEW.action = '${action}')`;
|
|
103
|
+
const create = sql.engine === 'postgres'
|
|
104
|
+
? `CREATE FUNCTION conformance_refuse() RETURNS trigger LANGUAGE plpgsql AS $$
|
|
105
|
+
BEGIN RAISE EXCEPTION '${LOG_REFUSAL}'; END $$;
|
|
106
|
+
CREATE TRIGGER conformance_refuse BEFORE INSERT ON audit_log FOR EACH ROW${when} EXECUTE FUNCTION conformance_refuse();`
|
|
107
|
+
: `CREATE TRIGGER conformance_refuse BEFORE INSERT ON audit_log${when}
|
|
108
|
+
BEGIN SELECT RAISE(ABORT, '${LOG_REFUSAL}'); END;`;
|
|
109
|
+
await sql.exec(create);
|
|
110
|
+
try {
|
|
111
|
+
return await work();
|
|
112
|
+
} finally {
|
|
113
|
+
await sql.exec(sql.engine === 'postgres'
|
|
114
|
+
? 'DROP TRIGGER conformance_refuse ON audit_log; DROP FUNCTION conformance_refuse();'
|
|
115
|
+
: 'DROP TRIGGER conformance_refuse;');
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** The log's fields in the order its format (`coffre.audit.v2`) encodes them. */
|
|
120
|
+
const ENTRY_FIELDS = [
|
|
121
|
+
'seq', 'author', 'key_id', 'occurred_at', 'actor', 'action', 'decision', 'code', 'subject_principal', 'project_id',
|
|
122
|
+
'environment_id', 'secret_id', 'secret_version_id', 'operation_id', 'request_id', 'source_ip', 'related_seq', 'metadata',
|
|
123
|
+
] as const;
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* An entry's fields as its MAC and hash cover them, written here from the
|
|
127
|
+
* format rather than taken from coffre, so a check does not trust the code
|
|
128
|
+
* it checks: each field length-prefixed in order, null as length -1.
|
|
129
|
+
*/
|
|
130
|
+
export function entryFields(row: Record<string, unknown>): Buffer {
|
|
131
|
+
return Buffer.concat(ENTRY_FIELDS.flatMap((field) => {
|
|
132
|
+
const header = Buffer.alloc(4);
|
|
133
|
+
const value = row[field];
|
|
134
|
+
if (value === null || value === undefined) {
|
|
135
|
+
header.writeInt32BE(-1);
|
|
136
|
+
return [header];
|
|
137
|
+
}
|
|
138
|
+
const bytes = Buffer.from(String(value));
|
|
139
|
+
header.writeInt32BE(bytes.length);
|
|
140
|
+
return [header, bytes];
|
|
141
|
+
}));
|
|
142
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// What anyone on the network sees: health, and the headers that keep a
|
|
2
|
+
// page from being framed, sniffed or made to run someone else's script.
|
|
3
|
+
// These need no sign-in, so `probe` runs them against any instance.
|
|
4
|
+
import type { Deployment } from '../harness.ts';
|
|
5
|
+
import { expect, until } from '../report.ts';
|
|
6
|
+
|
|
7
|
+
export async function health(deployment: Deployment): Promise<string> {
|
|
8
|
+
const { origin } = deployment;
|
|
9
|
+
const live = await fetch(`${origin}/livez`);
|
|
10
|
+
expect(live.ok, `/livez answered ${live.status}`);
|
|
11
|
+
// Readiness follows the audit heartbeat and the vault's checkpoint of it,
|
|
12
|
+
// which only the scheduled job writes: not ready until it has run.
|
|
13
|
+
if (deployment.beforeFirstBeat) {
|
|
14
|
+
const early = await fetch(`${origin}/readyz`);
|
|
15
|
+
expect(early.status === 503, `/readyz answered ${early.status} before any heartbeat`, await early.text());
|
|
16
|
+
await deployment.scheduled();
|
|
17
|
+
}
|
|
18
|
+
await until('/readyz', async () => (await fetch(`${origin}/readyz`)).ok, 20);
|
|
19
|
+
const ready = (await (await fetch(`${origin}/readyz`)).json()) as { checkpointed?: unknown };
|
|
20
|
+
expect(ready.checkpointed === true, '/readyz passed without a checkpoint covering the heartbeat', ready);
|
|
21
|
+
return deployment.beforeFirstBeat ? '/livez, and /readyz only once the heartbeat ran and was checkpointed' : '/livez and /readyz';
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** An instance someone else runs: up, and its scheduled job beating. */
|
|
25
|
+
export async function reachable(origin: string): Promise<string> {
|
|
26
|
+
const live = await fetch(`${origin}/livez`);
|
|
27
|
+
expect(live.ok, `/livez answered ${live.status}`);
|
|
28
|
+
const ready = await fetch(`${origin}/readyz`);
|
|
29
|
+
expect(ready.ok, `/readyz answered ${ready.status}: is the scheduled job running?`, await ready.text());
|
|
30
|
+
return '/livez and /readyz';
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export async function headers(origin: string): Promise<string> {
|
|
34
|
+
const anonymous = await fetch(`${origin}/api/me`);
|
|
35
|
+
expect(anonymous.status === 401, `/api/me answered ${anonymous.status} to no one`);
|
|
36
|
+
securityHeaders(anonymous, 'a refusal');
|
|
37
|
+
|
|
38
|
+
const login = await fetch(`${origin}/login`);
|
|
39
|
+
expect(login.status === 200, `/login answered ${login.status}`);
|
|
40
|
+
const csp = securityHeaders(login, '/login');
|
|
41
|
+
const html = await login.text();
|
|
42
|
+
const nonce = /'nonce-([^']+)'/.exec(csp)![1];
|
|
43
|
+
expect(html.includes(`nonce="${nonce}"`), "/login's scripts do not carry the policy's nonce");
|
|
44
|
+
const again = securityHeaders(await fetch(`${origin}/login`), '/login');
|
|
45
|
+
expect(!again.includes(`'nonce-${nonce}'`), 'two pages were served with the same nonce');
|
|
46
|
+
const asset = /\/_coffre\/assets\/[\w.-]+\.js/.exec(html)?.[0];
|
|
47
|
+
expect(asset !== undefined, '/login loads no script from /_coffre/assets/', html.slice(0, 2000));
|
|
48
|
+
const script = await fetch(`${origin}${asset}`);
|
|
49
|
+
expect(
|
|
50
|
+
script.ok && /javascript/.test(script.headers.get('content-type') ?? ''),
|
|
51
|
+
`${asset} answered ${script.status} ${script.headers.get('content-type')}`,
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
// No other site's page may read an answer: no CORS for anyone.
|
|
55
|
+
const foreign = await fetch(`${origin}/api/me`, { headers: { origin: 'https://attacker.example' } });
|
|
56
|
+
const allowed = foreign.headers.get('access-control-allow-origin');
|
|
57
|
+
expect(allowed === null, `the API allows another origin to read it: ${allowed}`);
|
|
58
|
+
return `a fresh CSP nonce per page, frames refused, nosniff; no CORS; ${asset} served`;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function securityHeaders(response: Response, what: string): string {
|
|
62
|
+
const csp = response.headers.get('content-security-policy') ?? '';
|
|
63
|
+
expect(/script-src 'self' 'nonce-[^']+'/.test(csp), `${what}: no nonce-based script-src`, csp);
|
|
64
|
+
expect(csp.includes("frame-ancestors 'none'"), `${what}: frames not refused`, csp);
|
|
65
|
+
expect(response.headers.get('x-content-type-options') === 'nosniff', `${what}: no nosniff`);
|
|
66
|
+
expect(response.headers.get('x-frame-options') === 'DENY', `${what}: no x-frame-options DENY`);
|
|
67
|
+
expect(response.headers.get('cross-origin-resource-policy') === 'same-origin', `${what}: other sites may embed it`);
|
|
68
|
+
expect(/no-store/.test(response.headers.get('cache-control') ?? ''), `${what}: may be cached`);
|
|
69
|
+
if (response.url.startsWith('https:')) {
|
|
70
|
+
const hsts = response.headers.get('strict-transport-security') ?? '';
|
|
71
|
+
expect(/max-age=\d{7,}/.test(hsts), `${what}: no strict-transport-security for months or more`, hsts);
|
|
72
|
+
}
|
|
73
|
+
return csp;
|
|
74
|
+
}
|
package/src/database.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// SQL on a deployment's shared database, as whoever holds its
|
|
2
|
+
// password or its file: what the checks use to go around coffre, to see
|
|
3
|
+
// what it wrote, and to break what it must notice.
|
|
4
|
+
import { DatabaseSync } from 'node:sqlite';
|
|
5
|
+
|
|
6
|
+
import pg from 'pg';
|
|
7
|
+
import { postgresConnection } from '@coffre/db/connect';
|
|
8
|
+
|
|
9
|
+
export type Sql = {
|
|
10
|
+
readonly engine: 'postgres' | 'sqlite';
|
|
11
|
+
/** One statement, with `$1`-style parameters on Postgres and `?` on SQLite. */
|
|
12
|
+
query<Row = Record<string, unknown>>(statement: string, params?: unknown[]): Promise<Row[]>;
|
|
13
|
+
/** Any number of statements, no parameters. */
|
|
14
|
+
exec(statements: string): Promise<void>;
|
|
15
|
+
close(): Promise<void>;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export async function postgres(url: string): Promise<Sql> {
|
|
19
|
+
const client = new pg.Client(postgresConnection(url));
|
|
20
|
+
await client.connect();
|
|
21
|
+
return {
|
|
22
|
+
engine: 'postgres',
|
|
23
|
+
query: async <Row>(statement: string, params: unknown[] = []) => (await client.query(statement, params)).rows as Row[],
|
|
24
|
+
exec: async (statements) => void (await client.query(statements)),
|
|
25
|
+
close: () => client.end(),
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** A SQLite file, perhaps open in another process: it waits out their locks. */
|
|
30
|
+
export function sqlite(file: string): Sql {
|
|
31
|
+
const db = new DatabaseSync(file, { timeout: 5000 });
|
|
32
|
+
return {
|
|
33
|
+
engine: 'sqlite',
|
|
34
|
+
query: async <Row>(statement: string, params: unknown[] = []) =>
|
|
35
|
+
db.prepare(statement).all(...(params as never[])) as Row[],
|
|
36
|
+
exec: async (statements) => db.exec(statements),
|
|
37
|
+
close: async () => db.close(),
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export async function using<T>(open: Sql | Promise<Sql>, use: (sql: Sql) => Promise<T>): Promise<T> {
|
|
42
|
+
const sql = await open;
|
|
43
|
+
try {
|
|
44
|
+
return await use(sql);
|
|
45
|
+
} finally {
|
|
46
|
+
await sql.close();
|
|
47
|
+
}
|
|
48
|
+
}
|
package/src/fixtures.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// Local fixtures, as in .env.dev: none of them is a secret anywhere else.
|
|
2
|
+
export const KEYS = {
|
|
3
|
+
KEK_ID: 'conformance-1',
|
|
4
|
+
KEK: 'Y29mZnJlLWxvY2FsLWRldi1rZWstMzItYnl0ZXMhISE=',
|
|
5
|
+
SIGNING_KEY: 'Y29mZnJlLWxvY2FsLXZhdWx0LXNpZ25pbmctc2VlZCE=',
|
|
6
|
+
AUDIT_CHAIN_KEY: 'Y29mZnJlLWxvY2FsLWF1ZGl0LWNoYWluLWtleS0zMmI=',
|
|
7
|
+
};
|
package/src/harness.ts
ADDED
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
// Run a deployment made like `coffre init` makes one, unedited: settings go
|
|
2
|
+
// in as the environment its wrangler.jsonc or server.env would give it, with
|
|
3
|
+
// GitHub's URLs pointed at a stand-in IdP running in this process.
|
|
4
|
+
//
|
|
5
|
+
// workers two Workers under `wrangler dev`, on a Postgres database of
|
|
6
|
+
// their own, created for the run and dropped after, each through
|
|
7
|
+
// its own login
|
|
8
|
+
// node the server and its vault process, on one SQLite file in a temp
|
|
9
|
+
// dir
|
|
10
|
+
//
|
|
11
|
+
// Ports: coffre on `port`, the IdP on the next, wrangler's inspector on the
|
|
12
|
+
// one after.
|
|
13
|
+
import { spawn, type ChildProcess } from 'node:child_process';
|
|
14
|
+
import { randomBytes } from 'node:crypto';
|
|
15
|
+
import { existsSync, mkdtempSync, rmSync } from 'node:fs';
|
|
16
|
+
import { tmpdir } from 'node:os';
|
|
17
|
+
import { join, resolve } from 'node:path';
|
|
18
|
+
import { setTimeout as sleep } from 'node:timers/promises';
|
|
19
|
+
|
|
20
|
+
import { postgres, sqlite, using, type Sql } from './database.ts';
|
|
21
|
+
import { KEYS } from './fixtures.ts';
|
|
22
|
+
import { DevIdp } from './idp/index.ts';
|
|
23
|
+
import { Failure, until } from './report.ts';
|
|
24
|
+
|
|
25
|
+
export type Kind = 'workers' | 'node';
|
|
26
|
+
|
|
27
|
+
export type HarnessOptions = {
|
|
28
|
+
port: number;
|
|
29
|
+
/** Postgres as a login that may create databases; workers only. */
|
|
30
|
+
postgres?: string;
|
|
31
|
+
/** The same server as coffre_runtime, the login the app runs as; workers only. */
|
|
32
|
+
runtime?: string;
|
|
33
|
+
/** The same server as coffre_vault_runtime, the login the vault runs as; workers only. */
|
|
34
|
+
vaultRuntime?: string;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export type Deployment = {
|
|
38
|
+
kind: Kind;
|
|
39
|
+
origin: string;
|
|
40
|
+
idp: DevIdp;
|
|
41
|
+
/** The one root admin the vault is told of. */
|
|
42
|
+
rootAdmin: string;
|
|
43
|
+
/** Whether no heartbeat has run yet, so /readyz fails until `scheduled` runs. */
|
|
44
|
+
beforeFirstBeat: boolean;
|
|
45
|
+
/** What the Cron trigger does every five minutes. */
|
|
46
|
+
scheduled(options?: { allowFailure?: boolean }): Promise<void>;
|
|
47
|
+
/** The app's database, as its owner. */
|
|
48
|
+
database(): Promise<Sql>;
|
|
49
|
+
/** The app's database as the login the app runs as, on an engine that has logins. */
|
|
50
|
+
runtime: (() => Promise<Sql>) | null;
|
|
51
|
+
/** The database as the login the vault runs as, likewise. */
|
|
52
|
+
vaultRuntime: (() => Promise<Sql>) | null;
|
|
53
|
+
/** The file holding the database, when it is one. */
|
|
54
|
+
databaseFile: string | null;
|
|
55
|
+
/** The processes' output: all of it, or the last `tail` characters of each. */
|
|
56
|
+
output(tail?: number): string;
|
|
57
|
+
stop(): Promise<void>;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const ROOT_ADMIN = 'root@conformance.example';
|
|
61
|
+
|
|
62
|
+
type Child = { name: string; child: ChildProcess; daemon: boolean; exited: boolean; output: Buffer[] };
|
|
63
|
+
|
|
64
|
+
export async function boot(kind: Kind, at: string, options: HarnessOptions): Promise<Deployment> {
|
|
65
|
+
const { port } = options;
|
|
66
|
+
const dir = resolve(at);
|
|
67
|
+
const origin = `http://127.0.0.1:${port}`;
|
|
68
|
+
for (const taken of [port, port + 1, port + 2]) {
|
|
69
|
+
const busy = await fetch(`http://127.0.0.1:${taken}/`).then(
|
|
70
|
+
() => true,
|
|
71
|
+
() => false,
|
|
72
|
+
);
|
|
73
|
+
if (busy) throw new Failure(`port ${taken} is taken; pass --port for a free run of three`);
|
|
74
|
+
}
|
|
75
|
+
if (kind === 'workers' && (options.postgres === undefined || options.runtime === undefined || options.vaultRuntime === undefined)) {
|
|
76
|
+
throw new Failure(
|
|
77
|
+
'workers run on Postgres: pass --postgres <owner URL>, --runtime <coffre_runtime URL> and --vault-runtime <coffre_vault_runtime URL>',
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const scratch = mkdtempSync(join(tmpdir(), `coffre-conformance-${kind}-`));
|
|
82
|
+
// The shell's own COFFRE_* (a sourced .env.dev, say) must not reach the
|
|
83
|
+
// deployment: it gets what it is handed here, and nothing else.
|
|
84
|
+
const shell = Object.fromEntries(Object.entries(process.env).filter(([name]) => !name.startsWith('COFFRE_')));
|
|
85
|
+
const children: Child[] = [];
|
|
86
|
+
const output = (tail = Infinity) =>
|
|
87
|
+
children.map((entry) => `--- ${entry.name}\n${Buffer.concat(entry.output).toString('utf8').slice(-tail)}`).join('\n');
|
|
88
|
+
const logs = () => output(4000);
|
|
89
|
+
const alive = () => !children.some((entry) => entry.daemon && entry.exited);
|
|
90
|
+
|
|
91
|
+
function start(name: string, command: string, args: string[], env: Record<string, string>, daemon = true): Child {
|
|
92
|
+
const child = spawn(command, args, {
|
|
93
|
+
cwd: dir,
|
|
94
|
+
env: { ...shell, ...env },
|
|
95
|
+
// Its own process group, so stopping it stops what it started (workerd).
|
|
96
|
+
detached: true,
|
|
97
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
98
|
+
});
|
|
99
|
+
const entry: Child = { name, child, daemon, exited: false, output: [] };
|
|
100
|
+
child.stdout!.on('data', (chunk: Buffer) => entry.output.push(chunk));
|
|
101
|
+
child.stderr!.on('data', (chunk: Buffer) => entry.output.push(chunk));
|
|
102
|
+
child.once('exit', () => (entry.exited = true));
|
|
103
|
+
// Not started at all: no 'exit' may follow.
|
|
104
|
+
child.once('error', (error) => {
|
|
105
|
+
entry.output.push(Buffer.from(`${error.message}\n`));
|
|
106
|
+
entry.exited = true;
|
|
107
|
+
});
|
|
108
|
+
children.push(entry);
|
|
109
|
+
return entry;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
async function run(command: string, args: string[]): Promise<void> {
|
|
113
|
+
const entry = start(args[0] ?? command, command, args, {}, false);
|
|
114
|
+
const code = await new Promise((done) => {
|
|
115
|
+
entry.child.once('exit', done);
|
|
116
|
+
entry.child.once('error', done);
|
|
117
|
+
});
|
|
118
|
+
if (code !== 0) throw new Failure(`${command} ${args.join(' ')} failed`, Buffer.concat(entry.output).toString('utf8'));
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function bin(name: string): string {
|
|
122
|
+
const path = join(dir, 'node_modules/.bin', name);
|
|
123
|
+
if (!existsSync(path)) throw new Failure(`${path} is missing: install ${dir} first`);
|
|
124
|
+
return path;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const idp = new DevIdp();
|
|
128
|
+
idp.listenPort = port + 1;
|
|
129
|
+
await idp.start();
|
|
130
|
+
const github = {
|
|
131
|
+
GITHUB_CLIENT_ID: 'coffre-local',
|
|
132
|
+
GITHUB_CLIENT_SECRET: 'coffre-local-secret',
|
|
133
|
+
GITHUB_URL: `${idp.origin}/github`,
|
|
134
|
+
GITHUB_API_URL: `${idp.origin}/github/api`,
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
let cleanup = async () => {};
|
|
138
|
+
async function stop(): Promise<void> {
|
|
139
|
+
for (const signal of ['SIGTERM', 'SIGKILL'] as const) {
|
|
140
|
+
for (const { child, exited } of children) {
|
|
141
|
+
if (!exited) {
|
|
142
|
+
try {
|
|
143
|
+
process.kill(-child.pid!, signal);
|
|
144
|
+
} catch {}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
await sleep(500);
|
|
148
|
+
}
|
|
149
|
+
await idp.stop().catch(() => {});
|
|
150
|
+
await cleanup().catch(() => {});
|
|
151
|
+
rmSync(scratch, { recursive: true, force: true });
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
try {
|
|
155
|
+
const deployment =
|
|
156
|
+
kind === 'workers'
|
|
157
|
+
? await startWorkers()
|
|
158
|
+
: await startNode();
|
|
159
|
+
await until(`${origin}/livez`, async () => (await fetch(`${origin}/livez`)).ok, 120, alive);
|
|
160
|
+
return deployment;
|
|
161
|
+
} catch (error) {
|
|
162
|
+
const seen = logs();
|
|
163
|
+
await stop();
|
|
164
|
+
if (error instanceof Failure) throw new Failure(error.message, `${error.detail ?? ''}\n${seen}`.trim());
|
|
165
|
+
throw error;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
async function startWorkers(): Promise<Deployment> {
|
|
169
|
+
const name = `coffre_conformance_${randomBytes(4).toString('hex')}`;
|
|
170
|
+
const owner = withDatabase(options.postgres!, name);
|
|
171
|
+
const runtime = withDatabase(options.runtime!, name);
|
|
172
|
+
const vaultRuntime = withDatabase(options.vaultRuntime!, name);
|
|
173
|
+
await using(postgres(withDatabase(options.postgres!, 'postgres')), (sql) => sql.exec(`CREATE DATABASE ${name}`));
|
|
174
|
+
cleanup = () =>
|
|
175
|
+
using(postgres(withDatabase(options.postgres!, 'postgres')), (sql) =>
|
|
176
|
+
sql.exec(`DROP DATABASE IF EXISTS ${name} WITH (FORCE)`),
|
|
177
|
+
);
|
|
178
|
+
// A fresh log has no heartbeat: only the scheduled job can make /readyz pass.
|
|
179
|
+
await run(bin('coffre-server'), ['migrate', owner]);
|
|
180
|
+
const state = join(scratch, 'state');
|
|
181
|
+
start('wrangler', bin('wrangler'), [
|
|
182
|
+
'dev',
|
|
183
|
+
...['-c', 'app/wrangler.jsonc', '-c', 'vault/wrangler.jsonc'],
|
|
184
|
+
...['--ip', '127.0.0.1', '--port', String(port), '--inspector-port', String(port + 2)],
|
|
185
|
+
...['--persist-to', state, '--show-interactive-dev-session=false'],
|
|
186
|
+
// Not in wrangler.jsonc's vars, so not taken from the environment.
|
|
187
|
+
...['--var', `GITHUB_URL:${github.GITHUB_URL}`, '--var', `GITHUB_API_URL:${github.GITHUB_API_URL}`],
|
|
188
|
+
], {
|
|
189
|
+
// Wrangler hands each Worker the vars and secrets its own
|
|
190
|
+
// wrangler.jsonc declares, from here: the app never sees a KEK.
|
|
191
|
+
PUBLIC_URL: origin,
|
|
192
|
+
GITHUB_CLIENT_ID: github.GITHUB_CLIENT_ID,
|
|
193
|
+
GITHUB_CLIENT_SECRET: github.GITHUB_CLIENT_SECRET,
|
|
194
|
+
AUDIT_CHAIN_KEY: KEYS.AUDIT_CHAIN_KEY,
|
|
195
|
+
KEK_ID: KEYS.KEK_ID,
|
|
196
|
+
KEK: KEYS.KEK,
|
|
197
|
+
SIGNING_KEY: KEYS.SIGNING_KEY,
|
|
198
|
+
ROOT_ADMINS: ROOT_ADMIN,
|
|
199
|
+
CLOUDFLARE_HYPERDRIVE_LOCAL_CONNECTION_STRING_HYPERDRIVE: runtime,
|
|
200
|
+
CLOUDFLARE_HYPERDRIVE_LOCAL_CONNECTION_STRING_VAULT_HYPERDRIVE: vaultRuntime,
|
|
201
|
+
WRANGLER_SEND_METRICS: 'false',
|
|
202
|
+
});
|
|
203
|
+
return {
|
|
204
|
+
kind,
|
|
205
|
+
origin,
|
|
206
|
+
idp,
|
|
207
|
+
rootAdmin: ROOT_ADMIN,
|
|
208
|
+
beforeFirstBeat: true,
|
|
209
|
+
async scheduled(options = {}) {
|
|
210
|
+
const response = await fetch(`${origin}/cdn-cgi/local/scheduled?cron=*/5+*+*+*+*`);
|
|
211
|
+
if (!response.ok && !options.allowFailure) throw new Failure(`the scheduled handler answered ${response.status}`, `${await response.text()}\n${logs()}`);
|
|
212
|
+
},
|
|
213
|
+
database: () => postgres(owner),
|
|
214
|
+
runtime: () => postgres(runtime),
|
|
215
|
+
vaultRuntime: () => postgres(vaultRuntime),
|
|
216
|
+
databaseFile: null,
|
|
217
|
+
output,
|
|
218
|
+
stop,
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
async function startNode(): Promise<Deployment> {
|
|
223
|
+
const database = join(scratch, 'coffre.db');
|
|
224
|
+
const socket = join(scratch, 'vault.sock');
|
|
225
|
+
await run(bin('coffre-server'), ['migrate', `file:${database}`]);
|
|
226
|
+
start('vault', process.execPath, ['src/vault.ts'], {
|
|
227
|
+
VAULT_SOCKET: socket,
|
|
228
|
+
DATABASE_URL: `file:${database}`,
|
|
229
|
+
KEK_ID: KEYS.KEK_ID,
|
|
230
|
+
KEK: KEYS.KEK,
|
|
231
|
+
SIGNING_KEY: KEYS.SIGNING_KEY,
|
|
232
|
+
ROOT_ADMINS: ROOT_ADMIN,
|
|
233
|
+
});
|
|
234
|
+
await until('the vault socket', async () => existsSync(socket), 30, alive);
|
|
235
|
+
const startServer = () =>
|
|
236
|
+
start('server', process.execPath, ['src/server.ts'], {
|
|
237
|
+
PORT: String(port),
|
|
238
|
+
PUBLIC_URL: origin,
|
|
239
|
+
DATABASE_URL: `file:${database}`,
|
|
240
|
+
VAULT_SOCKET: socket,
|
|
241
|
+
AUDIT_CHAIN_KEY: KEYS.AUDIT_CHAIN_KEY,
|
|
242
|
+
...github,
|
|
243
|
+
});
|
|
244
|
+
let server = startServer();
|
|
245
|
+
return {
|
|
246
|
+
kind,
|
|
247
|
+
origin,
|
|
248
|
+
idp,
|
|
249
|
+
rootAdmin: ROOT_ADMIN,
|
|
250
|
+
beforeFirstBeat: false,
|
|
251
|
+
// `serve` runs the job as it starts, and every five minutes after:
|
|
252
|
+
// so restart the server.
|
|
253
|
+
async scheduled() {
|
|
254
|
+
const exited = new Promise((done) => server.child.once('exit', done));
|
|
255
|
+
server.daemon = false;
|
|
256
|
+
process.kill(-server.child.pid!, 'SIGTERM');
|
|
257
|
+
await exited;
|
|
258
|
+
server = startServer();
|
|
259
|
+
await until('the restarted server', async () => (await fetch(`${origin}/livez`)).ok, 30, alive);
|
|
260
|
+
},
|
|
261
|
+
database: async () => sqlite(database),
|
|
262
|
+
runtime: null,
|
|
263
|
+
vaultRuntime: null,
|
|
264
|
+
databaseFile: database,
|
|
265
|
+
output,
|
|
266
|
+
stop,
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
function withDatabase(url: string, database: string): string {
|
|
272
|
+
const parsed = new URL(url);
|
|
273
|
+
parsed.pathname = `/${database}`;
|
|
274
|
+
return parsed.href;
|
|
275
|
+
}
|