Mail relay: the built-in client for the Intecion Notifications Webservice
Transport, enrolment with the confirm route, token and state in payload-kv, rotation, the panel's state block, doctor rules, the fake service and the copied contract 1.5.0. Tests against the contract's runner and the fake. Docs and HTTP tests follow. Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
co-authored by
Claude Fable 5.2
parent
9efd86a06f
commit
ea9703f8e3
@@ -0,0 +1,141 @@
|
||||
import type { DoctorContext } from '../project.js'
|
||||
import type { DoctorRule, RuleFinding } from '../types.js'
|
||||
|
||||
import { configFile, nextConfigFile } from '../project.js'
|
||||
import { stripComments } from '../util.js'
|
||||
|
||||
const CONFIRM = '/iapl/mail-relay/confirm'
|
||||
const cfg = (ctx: DoctorContext) => stripComments(ctx.read(configFile(ctx) ?? ''))
|
||||
const next = (ctx: DoctorContext) => stripComments(ctx.read(nextConfigFile(ctx) ?? ''))
|
||||
const relayOff = (code: string) => /\brelay\s*:\s*false\b/.test(code)
|
||||
const since = (iso?: null | string) => (iso ? `, since ${iso.slice(0, 16).replace('T', ' ')}` : '')
|
||||
|
||||
/**
|
||||
* E-mail (docs/email.md): which transport is in effect and whether it works, the site's state with the
|
||||
* Intecion Notifications Webservice, the deprecated Graph transport, the reply address, and whether
|
||||
* the service can reach the site's confirm route. Ids carry no brand, like `agents-md`.
|
||||
*/
|
||||
export const mailRules: DoctorRule[] = [
|
||||
{
|
||||
// Which transport a send takes right now, and whether the last sends worked.
|
||||
id: 'mail-transport',
|
||||
check: (ctx) => {
|
||||
const mail = ctx.deep?.mail
|
||||
if (!mail) {return []}
|
||||
const out: RuleFinding[] = []
|
||||
if (mail.transport === 'none') {
|
||||
out.push({
|
||||
fix: 'remove `mail: { relay: false }` to send through the Intecion mail service, or fill in an SMTP server in Site integrations → E-mail sending',
|
||||
message: 'no e-mail transport is set up: the Intecion mail service is switched off and the panel has no SMTP server — e-mails are not sent',
|
||||
severity: 'error',
|
||||
})
|
||||
}
|
||||
if (mail.status?.state === 'degraded') {
|
||||
const now = ctx.today === new Date().toISOString().slice(0, 10) ? Date.now() : Date.parse(`${ctx.today}T12:00:00Z`)
|
||||
const hours = mail.status.since ? Math.floor((now - Date.parse(mail.status.since)) / 3_600_000) : null
|
||||
out.push({
|
||||
fix: 'Site integrations → E-mail sending: read the state block, then send a test e-mail (iapl-debug, "E-mail")',
|
||||
message: `e-mail sending is failing (${mail.status.code ?? 'unknown'}, transport: ${mail.transport})${since(mail.status.since)}`,
|
||||
severity: hours !== null && hours >= 24 ? 'error' : 'warning',
|
||||
})
|
||||
}
|
||||
return out
|
||||
},
|
||||
docs: 'docs/email.md',
|
||||
group: 'mail',
|
||||
source: 'deep',
|
||||
},
|
||||
{
|
||||
// The site's state with the Intecion mail service, when that is how it sends.
|
||||
id: 'mail-relay-state',
|
||||
check: (ctx) => {
|
||||
const mail = ctx.deep?.mail
|
||||
const relay = mail?.relay
|
||||
if (!mail || mail.transport !== 'relay' || !relay) {return []}
|
||||
const out: RuleFinding[] = []
|
||||
if (relay.disconnected) {
|
||||
out.push({ fix: 'Site integrations → E-mail sending → "Connect again", or fill in an SMTP server', message: 'the site was disconnected from the Intecion mail service in the panel: e-mails are not sent', severity: 'error' })
|
||||
} else if (relay.state === 'suspended') {
|
||||
out.push({ fix: 'contact Intecion; meanwhile an SMTP server in the panel sends the site\'s mail', message: 'Intecion has stopped e-mail sending for this site (suspended)', severity: 'error' })
|
||||
} else if (relay.state === 'pending') {
|
||||
out.push({ fix: 'Intecion approves the host on the service (`ims approve <host>`); the same token then works', message: 'the site is enrolled with the Intecion mail service but waits for approval: e-mails are not sent yet', severity: 'warning' })
|
||||
} else if (!relay.connected && relay.refusal) {
|
||||
const path = relay.refusal === 'callback-failed' ? ` (${relay.reason ?? 'unknown'}): the service could not confirm the site at its public address` : ''
|
||||
out.push({
|
||||
fix: relay.refusal === 'callback-failed' ? 'the site must answer POST /api/iapl/mail-relay/confirm from the internet without a redirect (email.md, "When the site cannot be confirmed"), then "Connect again"' : 'email.md, "When the site cannot be confirmed"; then Site integrations → E-mail sending → "Connect again"',
|
||||
message: `the Intecion mail service did not connect this site: ${relay.refusal}${path}`,
|
||||
severity: 'warning',
|
||||
})
|
||||
} else if (!relay.connected) {
|
||||
out.push({ fix: 'send a test e-mail from Site integrations → E-mail sending', message: 'the site has not enrolled with the Intecion mail service yet (it does so on its first e-mail)', severity: 'info' })
|
||||
}
|
||||
if (relay.nameReplaced) {
|
||||
out.push({ fix: 'Site integrations → E-mail sending → Sender name: a name that is the client\'s own (no address, no other company\'s name)', message: 'the mail service did not accept the sender name, so e-mails leave under the site\'s host', severity: 'warning' })
|
||||
}
|
||||
return out
|
||||
},
|
||||
docs: 'docs/email.md',
|
||||
group: 'mail',
|
||||
source: 'deep',
|
||||
},
|
||||
{
|
||||
// The sender address of the service has no mailbox: without a reply address, answers are lost.
|
||||
id: 'mail-reply-address',
|
||||
check: (ctx) => {
|
||||
const mail = ctx.deep?.mail
|
||||
return mail?.transport === 'relay' && !mail.replyAddress
|
||||
? [{ fix: 'Site integrations → E-mail sending → Sender address: the client\'s own address', message: 'no reply address is set: replies to the site\'s e-mails (other than forms that set their own) go to an address without a mailbox', severity: 'warning' }]
|
||||
: []
|
||||
},
|
||||
docs: 'docs/email.md',
|
||||
group: 'mail',
|
||||
source: 'deep',
|
||||
},
|
||||
{
|
||||
// Microsoft Graph as the transport: deprecated in 1.17.0, removed in 2.0.
|
||||
id: 'mail-graph-deprecated',
|
||||
check: (ctx) => {
|
||||
const mail = ctx.deep?.mail
|
||||
const inCode = /\bgraphAdapter\s*\(|\bemailGraph\s*:\s*true\b/.test(cfg(ctx))
|
||||
if (!mail?.graphEnv && mail?.transport !== 'graph' && !inCode) {return []}
|
||||
return [{
|
||||
fix: 'remove the four GRAPH_* variables (Coolify) and send a test e-mail — the site then uses the Intecion mail service — or fill in an SMTP server (email.md, "Moving off Microsoft Graph")',
|
||||
message: mail?.transport === 'graph' ? 'e-mails go through Microsoft Graph, which is deprecated and will be removed in 2.0' : 'Microsoft Graph is still set up for this site (GRAPH_* variables or graphAdapter); it is deprecated and will be removed in 2.0',
|
||||
severity: 'warning',
|
||||
}]
|
||||
},
|
||||
docs: 'docs/email.md',
|
||||
group: 'mail',
|
||||
source: 'static',
|
||||
},
|
||||
{
|
||||
// The service calls exactly https://<host>/api/iapl/mail-relay/confirm and follows no redirect.
|
||||
id: 'mail-relay-route',
|
||||
check: async (ctx) => {
|
||||
const code = cfg(ctx)
|
||||
if (relayOff(code)) {return []}
|
||||
const out: RuleFinding[] = []
|
||||
const ask = 'ask Intecion to set this site\'s confirm address (`ims callback-path <host> <path>`), or change the site to answer at /api/iapl/mail-relay/confirm'
|
||||
if (/\btrailingSlash\s*:\s*true\b/.test(next(ctx))) {
|
||||
out.push({ file: nextConfigFile(ctx) ?? 'next.config.ts', fix: `${ask} — with trailing slashes the path ends in /confirm/`, message: 'trailingSlash: true redirects the mail service\'s call-back, and the service follows no redirect: the site cannot enrol', severity: 'warning' })
|
||||
}
|
||||
const api = /\broutes\s*:\s*\{[^}]*\bapi\s*:\s*['"`]([^'"`]+)['"`]/.exec(code)?.[1]
|
||||
if (api && api !== '/api') {
|
||||
out.push({ file: configFile(ctx) ?? 'src/payload.config.ts', fix: ask, message: `the API is served at ${api}, not /api: the mail service calls /api${CONFIRM} unless Intecion set another address for this host`, severity: 'warning' })
|
||||
}
|
||||
if (ctx.live && ctx.deep?.mail?.transport === 'relay') {
|
||||
const res = await ctx.live.fetch(`${api ?? '/api'}${CONFIRM}`, { redirect: 'manual' })
|
||||
// A GET finds no route there (the route is POST): 404 or 405 means the address is reached as it is.
|
||||
if (res.status >= 300 && res.status < 400) {
|
||||
out.push({ fix: `enrol the host the site really answers on (mail.relay.host), or ${ask}`, message: `live ${api ?? '/api'}${CONFIRM} redirects (${res.status} → ${res.headers.get('location') ?? '?'}); the mail service follows no redirect`, severity: 'warning' })
|
||||
} else if (res.status === 401 || res.status === 403 || res.status === 0 || res.status >= 500) {
|
||||
out.push({ fix: 'the confirm route must be reachable from the internet: no basic auth, no firewall rule, no maintenance page in front of it', message: `live ${api ?? '/api'}${CONFIRM} → ${res.status || res.error}: the mail service cannot call the site back`, severity: 'warning' })
|
||||
}
|
||||
}
|
||||
return out
|
||||
},
|
||||
docs: 'docs/email.md',
|
||||
group: 'mail',
|
||||
source: 'static',
|
||||
},
|
||||
]
|
||||
@@ -12,6 +12,7 @@ import { envRules } from './rules/env.js'
|
||||
import { layoutRules } from './rules/layout.js'
|
||||
import { legacyRules } from './rules/legacy.js'
|
||||
import { createLiveFetcher, liveRules } from './rules/live.js'
|
||||
import { mailRules } from './rules/mail.js'
|
||||
import { securityRules } from './rules/security.js'
|
||||
import { seedRules } from './rules/seeds.js'
|
||||
import { toolingRules } from './rules/tooling.js'
|
||||
@@ -33,6 +34,7 @@ export const doctorRules: DoctorRule[] = [
|
||||
...layoutRules,
|
||||
...envRules,
|
||||
...securityRules,
|
||||
...mailRules,
|
||||
...agentRules,
|
||||
...legacyRules,
|
||||
...seedRules,
|
||||
|
||||
@@ -8,6 +8,8 @@ import { PLUGIN_VERSION } from '../../version.js'
|
||||
import { openSignUpCollections, staffCollectionsOnOwnRoles, staffCollectionsOpenToOthers } from '../access/selfRegistration.js'
|
||||
import { countPendingCookies } from '../consent/scanner/approval.js'
|
||||
import { COOKIE_REVIEW_SLUG } from '../consent/scanner/collection.js'
|
||||
import { graphAvailable } from '../email/mailAdapter.js'
|
||||
import { mailPanelState } from '../email/relay/panel.js'
|
||||
import { secondFactorPassed, SETUP_FROM_FIELD } from '../security/totp/secondFactor.js'
|
||||
import { countAccountsWithoutTotp } from '../security/totp/setupRequired.js'
|
||||
import { readEnv } from '../shared/envName.js'
|
||||
@@ -59,6 +61,33 @@ export async function collectDoctorStatus(payload: BasePayload): Promise<DoctorS
|
||||
/* the global cannot be read: unknown */
|
||||
}
|
||||
}
|
||||
// E-mail: the transport in effect and the state with the mail service — read from the store, nothing is asked.
|
||||
let mail: DoctorStatus['mail'] = null
|
||||
if ((payload.config.globals ?? []).some((g) => g.slug === 'site-integrations')) {
|
||||
try {
|
||||
const state = await mailPanelState({ payload }, undefined)
|
||||
const integrations = (await payload.findGlobal({ slug: 'site-integrations' as never, depth: 0, overrideAccess: true })) as unknown as {
|
||||
emailStatus?: { code?: null | string; since?: null | string; state?: null | string } | null
|
||||
}
|
||||
mail = {
|
||||
graphEnv: graphAvailable(),
|
||||
relay: state.relay
|
||||
? {
|
||||
connected: state.relay.connected,
|
||||
disconnected: state.relay.disconnected,
|
||||
nameReplaced: Boolean(state.relay.nameReplaced),
|
||||
...(state.relay.refusal ? { refusal: state.relay.refusal.code, ...(state.relay.refusal.reason ? { reason: state.relay.refusal.reason } : {}) } : {}),
|
||||
...(state.relay.state ? { state: state.relay.state } : {}),
|
||||
}
|
||||
: null,
|
||||
replyAddress: Boolean(state.replyAddress),
|
||||
status: integrations.emailStatus?.state ? { code: integrations.emailStatus.code ?? null, since: integrations.emailStatus.since ?? null, state: integrations.emailStatus.state } : null,
|
||||
transport: state.transport,
|
||||
}
|
||||
} catch {
|
||||
/* the global cannot be read: unknown */
|
||||
}
|
||||
}
|
||||
let openSignUp: null | string[] = null
|
||||
try {
|
||||
openSignUp = await openSignUpCollections(payload as never)
|
||||
@@ -77,7 +106,7 @@ export async function collectDoctorStatus(payload: BasePayload): Promise<DoctorS
|
||||
} catch {
|
||||
staffOnOwnRoles = null
|
||||
}
|
||||
return { node: process.version, openSignUp, pendingCookies, plugin: pluginVersion(), staffOnOwnRoles, staffOpenToOthers, turnstile, turnstileSecretFromEnv: secretFromEnv, usersSetupClosed, usersWithout2fa }
|
||||
return { mail, node: process.version, openSignUp, pendingCookies, plugin: pluginVersion(), staffOnOwnRoles, staffOpenToOthers, turnstile, turnstileSecretFromEnv: secretFromEnv, usersSetupClosed, usersWithout2fa }
|
||||
}
|
||||
|
||||
export type DoctorEndpointOptions = {
|
||||
|
||||
@@ -60,6 +60,29 @@ export type DoctorStatus = {
|
||||
error?: string
|
||||
/** null = not compared (the endpoint cannot read files). */
|
||||
importMapCurrent?: boolean | null
|
||||
/**
|
||||
* E-mail: the transport in effect, the recorded state of sending, and the site's state with the
|
||||
* Intecion mail service. States and codes only — never a token, a password or an address.
|
||||
*/
|
||||
mail?: {
|
||||
/** The four GRAPH_* variables are set (Microsoft Graph: deprecated). */
|
||||
graphEnv: boolean
|
||||
/** null = the mail service is switched off in the project. */
|
||||
relay: {
|
||||
connected: boolean
|
||||
disconnected: boolean
|
||||
nameReplaced: boolean
|
||||
/** With `callback-failed`: why (`unreachable`, `timeout`, `redirect`, `status`, `confirmation`). */
|
||||
reason?: string
|
||||
/** The code of a refused enrolment. */
|
||||
refusal?: string
|
||||
state?: string
|
||||
} | null
|
||||
/** "Sender address" is filled in (with the mail service: where replies go). */
|
||||
replyAddress: boolean
|
||||
status?: { code?: null | string; since?: null | string; state?: null | string } | null
|
||||
transport: 'graph' | 'none' | 'relay' | 'smtp'
|
||||
} | null
|
||||
node?: string
|
||||
/** Auth collections whose `create` rule lets an anonymous request through (slugs only). */
|
||||
openSignUp?: null | string[]
|
||||
|
||||
Reference in New Issue
Block a user