mirror of
https://github.com/bitbaum/sites.git
synced 2026-12-04 04:56:58 +01:00
The ask route refused any name classifyHost calls invalid, which includes every <tenant>.hirnli.orangecat.ch (two labels under the base) - so pointing Caddy's one ask at the engine would have stopped hirnli's certificates (the install script's parity check would have refused the switch instead). And a database error (tables not migrated, an outage) threw before the fallback was asked. The decision is now mayIssue() in src/lib/ask.ts, tested without a database. scripts/smoke.mjs checks a running engine: health, the published site by its free address and own domain, the preview (noindex), names nobody holds, the ask, and a form refused for a missing field. Run locally against Loki's 0140_sites.sql with a SELECT-only role: all checks pass. Claude-Session: https://claude.ai/code/session_019MDtmugQS9ffkEryKtQb5c Co-authored-by: Claude <noreply@anthropic.com>
95 lines
3.5 KiB
TypeScript
95 lines
3.5 KiB
TypeScript
/**
|
|
* Caddy's `ask` decides which names get a certificate. These pin who may say
|
|
* yes, and that a database error here does not take hirnli's names down too.
|
|
*/
|
|
import { test } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { mayIssue, type AskLookups } from '../src/lib/ask';
|
|
|
|
function lookups(over: Partial<AskLookups> = {}) {
|
|
const calls = { servable: [] as string[], fallback: [] as string[] };
|
|
const l: AskLookups = {
|
|
servable: async (kind, value) => {
|
|
calls.servable.push(`${kind}:${value}`);
|
|
return over.servable ? over.servable(kind, value) : false;
|
|
},
|
|
fallback: async (d) => {
|
|
calls.fallback.push(d);
|
|
return over.fallback ? over.fallback(d) : false;
|
|
},
|
|
};
|
|
return { l, calls };
|
|
}
|
|
|
|
test('a published site gets a certificate without asking the fallback', async () => {
|
|
const { l, calls } = lookups({ servable: async () => true });
|
|
assert.equal(await mayIssue('camille.orangecat.ch', l), true);
|
|
assert.deepEqual(calls.servable, ['label:camille']);
|
|
assert.deepEqual(calls.fallback, []);
|
|
});
|
|
|
|
test('an own domain is looked up as a domain', async () => {
|
|
const { l, calls } = lookups({ servable: async () => true });
|
|
assert.equal(await mayIssue('www.camille.ch', l), true);
|
|
assert.deepEqual(calls.servable, ['domain:www.camille.ch']);
|
|
});
|
|
|
|
test('the preview host always may; nothing is looked up', async () => {
|
|
const { l, calls } = lookups();
|
|
assert.equal(await mayIssue('sites.orangecat.ch', l), true);
|
|
assert.deepEqual(calls, { servable: [], fallback: [] });
|
|
});
|
|
|
|
test('a name nobody serves is refused after asking the fallback', async () => {
|
|
const { l, calls } = lookups();
|
|
assert.equal(await mayIssue('nobody.example.com', l), false);
|
|
assert.deepEqual(calls.fallback, ['nobody.example.com']);
|
|
});
|
|
|
|
test("hirnli's names still pass when this engine's lookup throws", async () => {
|
|
const { l, calls } = lookups({
|
|
servable: async () => {
|
|
throw new Error('relation "sites" does not exist');
|
|
},
|
|
fallback: async (d) => d === 'acme.hirnli.ch',
|
|
});
|
|
const errors: string[] = [];
|
|
const orig = console.error;
|
|
console.error = (m: string) => errors.push(m);
|
|
try {
|
|
assert.equal(await mayIssue('acme.hirnli.ch', l), true);
|
|
assert.equal(await mayIssue('nobody.example.com', l), false);
|
|
} finally {
|
|
console.error = orig;
|
|
}
|
|
assert.deepEqual(calls.fallback, ['acme.hirnli.ch', 'nobody.example.com']);
|
|
assert.equal(errors.length, 2, 'each failed lookup is logged');
|
|
});
|
|
|
|
test("hirnli's tenant names (two labels under the base) go to the fallback", async () => {
|
|
// Before this, classifyHost called them invalid and they were refused
|
|
// without asking hirnli: switching Caddy's ask would have stopped every
|
|
// hirnli tenant's certificate.
|
|
const { l, calls } = lookups({ fallback: async (d) => d === 'acme.hirnli.orangecat.ch' });
|
|
assert.equal(await mayIssue('acme.hirnli.orangecat.ch', l), true);
|
|
assert.equal(await mayIssue('other.hirnli.orangecat.ch', l), false);
|
|
assert.deepEqual(calls.servable, [], 'not a free address: no site lookup');
|
|
assert.deepEqual(calls.fallback, ['acme.hirnli.orangecat.ch', 'other.hirnli.orangecat.ch']);
|
|
});
|
|
|
|
test('garbage and local names are refused without any lookup', async () => {
|
|
const { l, calls } = lookups({ servable: async () => true, fallback: async () => true });
|
|
for (const d of [
|
|
'',
|
|
'localhost',
|
|
'127.0.0.1',
|
|
'[::1]:4042',
|
|
'not a host',
|
|
'-x.orangecat.ch',
|
|
'a..b',
|
|
]) {
|
|
assert.equal(await mayIssue(d, l), false, d);
|
|
}
|
|
assert.deepEqual(calls, { servable: [], fallback: [] });
|
|
});
|