Mail relay: contract 1.6.0, the token tied to its host, IWSN in the panel

- contracts/mail-relay refreshed from IWDL 83af8c6 (1.6.0)
- an answer that is not the service's JSON is 'not confirmed' unless its status is 400 to 499
- a site with a token issued by hand does not enrol to heal: it stops and says what to ask Intecion for
- a store made on another host (a database copied to staging) is not used: the copy enrols as itself, nothing is revoked
- the panel calls the service IWSN (RELAY_SHORT_NAME); docs, logs and the doctor keep the full name

Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-11-25 21:51:47 +01:00
co-authored by Claude Fable 5.2
parent 7d766b6d58
commit 0ee74ab2a4
36 changed files with 380 additions and 128 deletions
+31 -7
View File
@@ -12,6 +12,7 @@ import {
clearRelayStore,
dropRelayCandidate,
holdNonce,
readOwnRelayStore,
readRelayCandidate,
readRelayStore,
type RelaySiteState,
@@ -21,6 +22,9 @@ import {
updateRelayStore,
} from './store.js'
/** The stored entry of this site — never one made on another host (store.ts). */
export const readSiteStore = (payload: BasePayload, options: Pick<ResolvedRelayOptions, 'host'>) => readOwnRelayStore(payload, relayHost(payload, options))
const sleep = (ms: number) => new Promise<void>((resolve) => setTimeout(resolve, ms))
const refusal = (code: string, message: string, extra: Partial<RelayRefusal> = {}): RelayRefusal => ({ code, message, noAnswer: false, ok: false, retryable: false, status: 0, ...extra })
@@ -65,7 +69,7 @@ export function enrolRelay(payload: BasePayload, options: ResolvedRelayOptions,
}
async function enrolOnce(payload: BasePayload, options: ResolvedRelayOptions, force: boolean): Promise<EnrolResult> {
const stored = await readRelayStore(payload)
const stored = await readSiteStore(payload, options)
if (stored.disconnected && !force) {return enrolFailure(refusal(LOCAL_CODES.disconnected, 'The site was disconnected from the mail service in the panel.'))}
if (!force && stored.refusal && Date.parse(stored.refusal.until) > Date.now()) {
return enrolFailure(refusal(stored.refusal.code, stored.refusal.message ?? stored.refusal.code, { reason: stored.refusal.reason }))
@@ -104,6 +108,7 @@ async function enrolOnce(payload: BasePayload, options: ResolvedRelayOptions, fo
disconnected: false,
enrolledAt: new Date().toISOString(),
host,
manual: false,
refusal: null,
source: 'enrolment',
state,
@@ -134,6 +139,21 @@ async function enrolOnce(payload: BasePayload, options: ResolvedRelayOptions, fo
return enrolFailure(failed)
}
const HAND_ISSUED = {
'invalid-token': 'The token issued by hand for this site is no longer valid, and the site cannot enrol by itself: ask Intecion for a new token.',
reverify: 'The site sends from a server address the mail service does not know, and with a token issued by hand it cannot enrol by itself: ask Intecion to add this server\'s address.',
} as const
/**
* A site with a token issued by hand cannot be called back, so "enrol again" is not open to it
* (contract §2): it stops, says what to ask Intecion for, and keeps the token.
*/
async function handIssued(payload: BasePayload, code: keyof typeof HAND_ISSUED): Promise<RelayRefusal> {
const now = new Date().toISOString()
await updateRelayStore(payload, (current) => (current.refusal?.code === code ? current : { ...current, refusal: { at: now, code, message: HAND_ISSUED[code], until: now } }))
return refusal(code, HAND_ISSUED[code])
}
/** A token from the `token` option (a relay of one's own, a deployment secret), when the project set one. */
async function optionToken(options: ResolvedRelayOptions): Promise<string | undefined> {
if (options.token === undefined) {return undefined}
@@ -164,7 +184,7 @@ export async function withRelayToken<T>(
if (options.token !== undefined) {return refusal(LOCAL_CODES.noToken, 'mail.relay.token returned no token.')}
for (let round = 0; round < 4; round++) {
const stored = await readRelayStore(payload)
const stored = await readSiteStore(payload, options)
if (stored.disconnected) {return refusal(LOCAL_CODES.disconnected, 'The site was disconnected from the mail service in the panel.')}
const candidate = await readRelayCandidate(payload)
const token = candidate?.token ?? stored.token
@@ -196,16 +216,18 @@ export async function withRelayToken<T>(
await dropRelayCandidate(payload)
continue
}
if (stored.manual) {return handIssued(payload, 'invalid-token')}
const failed = await enrolNow()
if (failed) {return use.enrolment && !use.enrolment.ok ? failed : answer}
continue
}
if (candidate) {
// Seen accepted: this is the token now.
await updateRelayStore(payload, (current) => ({ ...current, enrolledAt: candidate.at, host: relayHost(payload, options) || current.host, source: 'enrolment', state: candidate.state, token: candidate.token }))
await updateRelayStore(payload, (current) => ({ ...current, enrolledAt: candidate.at, host: relayHost(payload, options) || current.host, manual: false, source: 'enrolment', state: candidate.state, token: candidate.token }))
await dropRelayCandidate(payload)
}
if (!answer.ok && answer.code === 'reverify') {
if (stored.manual) {return handIssued(payload, 'reverify')}
const failed = await enrolNow()
if (failed) {return use.enrolment && !use.enrolment.ok ? failed : answer}
continue
@@ -238,7 +260,7 @@ export async function refreshRelayStatus(payload: BasePayload, options: Resolved
{},
{ first: false },
)
if (!answer.ok) {return { error: answer, stored: await readRelayStore(payload) }}
if (!answer.ok) {return { error: answer, stored: await readSiteStore(payload, options) }}
const body = answer.body
const stored = await updateRelayStore(payload, (current) => ({
...current,
@@ -263,7 +285,7 @@ export async function refreshRelayStatus(payload: BasePayload, options: Resolved
* when the store already holds a newer token, else the site enrols again on its next send.
*/
export async function rotateRelayToken(payload: BasePayload, options: ResolvedRelayOptions, { force = false }: { force?: boolean } = {}): Promise<'failed' | 'not-due' | 'rotated'> {
const stored = await readRelayStore(payload)
const stored = await readSiteStore(payload, options)
if (!stored.token || options.token !== undefined) {return 'not-due'}
const due = stored.service?.rotateAfter ? Date.parse(stored.service.rotateAfter) : Number.NaN
if (!force && !(due <= Date.now())) {return 'not-due'}
@@ -293,7 +315,8 @@ export async function rotateRelayToken(payload: BasePayload, options: ResolvedRe
/** Gives the token up on the service (every token of the site stops working) and forgets it here. */
export async function disconnectRelay(payload: BasePayload, options: ResolvedRelayOptions): Promise<void> {
const stored = await readRelayStore(payload)
// (a store copied from another host is dropped by this read: that site's token is never revoked from here)
const stored = await readSiteStore(payload, options)
if (stored.token && options.token === undefined) {
await relayRequest({ fetchImpl: options.fetchImpl, method: 'POST', path: '/v1/revoke', token: stored.token, url: options.url })
}
@@ -312,6 +335,7 @@ export async function setRelayToken(payload: BasePayload, token: string, options
disconnected: false,
enrolledAt: new Date().toISOString(),
...(options.host ? { host: options.host } : {}),
manual: true,
refusal: null,
service: null,
source: 'manual',
@@ -357,7 +381,7 @@ export function describeRelayStore(payload: BasePayload, options: ResolvedRelayO
* (`/v1/status`); otherwise what was last stored is returned and nothing leaves the server.
*/
export async function getRelayStatus(payload: BasePayload, options: ResolvedRelayOptions, { refresh = false }: { refresh?: boolean } = {}): Promise<{ error?: string } & RelayStatus> {
if (!refresh) {return describeRelayStore(payload, options, await readRelayStore(payload))}
if (!refresh) {return describeRelayStore(payload, options, await readSiteStore(payload, options))}
const { error, stored } = await refreshRelayStatus(payload, options)
return { ...describeRelayStore(payload, options, stored), ...(error && error.code !== LOCAL_CODES.noToken ? { error: error.code } : {}) }
}