diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 6e44935..02d384c 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -105,6 +105,7 @@ jobs: CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} TF_VAR_CLOUDFLARE_ACCOUNT_ID: ${{ steps.fetch_account_id.outputs.account_id }} TF_VAR_APPS_US_EAST_TARGET: ${{ secrets.APPS_US_EAST_TARGET }} + TF_VAR_MATTERMOST_WEBHOOK: ${{ secrets.MATTERMOST_WEBHOOK }} D1_ID: ${{ env.D1_ID }} # Currently Terraform Cloudflare provider doesn't support direct upload, use wrangler to upload instead. diff --git a/deploy.tf b/deploy.tf index c07e802..edc23c3 100644 --- a/deploy.tf +++ b/deploy.tf @@ -23,6 +23,12 @@ variable "APPS_US_EAST_TARGET" { sensitive = true } +variable "MATTERMOST_WEBHOOK" { + # read from $TF_VAR_MATTERMOST_WEBHOOK (the GitHub Actions secret of the same name) + type = string + sensitive = true +} + variable "enable_do_migration" { type = bool default = false @@ -70,6 +76,10 @@ resource "cloudflare_workers_script" "uptimeflare_worker" { name = "APPS_US_EAST_TARGET" type = "secret_text" text = var.APPS_US_EAST_TARGET + }, { + name = "MATTERMOST_WEBHOOK" + type = "secret_text" + text = var.MATTERMOST_WEBHOOK }] } diff --git a/uptime.config.ts b/uptime.config.ts index 840c59e..e04f2d6 100644 --- a/uptime.config.ts +++ b/uptime.config.ts @@ -57,7 +57,19 @@ const workerConfig: WorkerConfig = { headers: { 'User-Agent': 'cottage-status (+https://status.cottage.host)' }, }, ], - // Alerts come from Grafana Cloud (hearth infra/monitoring). A webhook here could page too. + // Down and back-up messages go to the founder's Mattermost (channel "alerts"). The webhook address + // is private: the worker reads it from the Worker secret MATTERMOST_WEBHOOK (GitHub Actions secret of + // the same name, bound in deploy.tf). Grafana Cloud (hearth infra/monitoring) alerts there too. + notification: { + webhook: { + url: 'secret:MATTERMOST_WEBHOOK', + payloadType: 'json', + payload: { text: '$MSG', username: 'Status' }, + }, + timeZone: 'Etc/UTC', + // Wait a few minutes before saying something is down, so a single failed check doesn't page. + gracePeriod: 3, + }, } // Notices shown on the page: incident updates and planned maintenance. Newest last. diff --git a/worker/src/index.ts b/worker/src/index.ts index df3b257..9a0dd0f 100644 --- a/worker/src/index.ts +++ b/worker/src/index.ts @@ -2,7 +2,7 @@ import { DurableObject } from 'cloudflare:workers' import { MonitorTarget } from '../../types/config' import { workerConfig } from '../../uptime.config' import { doMonitor, getStatus } from './monitor' -import { formatAndNotify, getWorkerLocation } from './util' +import { formatAndNotify, getWorkerLocation, setWebhookSecrets } from './util' import { CompactedMonitorStateWrapper, getFromStore, setToStore } from './store' import pLimit from 'p-limit' @@ -45,6 +45,7 @@ async function checkMonitor(monitor: MonitorTarget, location: string, env: Env) const Worker = { async scheduled(event: ScheduledEvent, env: Env, ctx: ExecutionContext): Promise { + setWebhookSecrets(env) const workerLocation = (await getWorkerLocation()) || 'ERROR' console.log(`Running scheduled event on ${workerLocation}...`) diff --git a/worker/src/util.ts b/worker/src/util.ts index 6fbd54b..84c4876 100644 --- a/worker/src/util.ts +++ b/worker/src/util.ts @@ -79,6 +79,24 @@ function templateWebhookPlayload(payload: any, message: string) { } } +// Cottage: a webhook url may be `secret:NAME`, read at send time from the Worker secret NAME (bound in +// deploy.tf from a GitHub Actions secret), so the address never appears in this public repo or the logs. +let webhookSecrets: Record = {} + +function setWebhookSecrets(env: object) { + webhookSecrets = env as Record +} + +function resolveWebhookUrl(url: string): string | undefined { + if (!url.startsWith('secret:')) return url + const value = webhookSecrets[url.slice('secret:'.length)] + return typeof value === 'string' && value !== '' ? value : undefined +} + +function hideUrl(text: string, url: string, real: string): string { + return real === url ? text : text.split(real).join(url) +} + async function webhookNotify(webhook: WebhookConfig, message: string) { if (Array.isArray(webhook)) { for (const w of webhook) { @@ -90,8 +108,13 @@ async function webhookNotify(webhook: WebhookConfig, message: string) { console.log( 'Sending webhook notification: ' + JSON.stringify(message) + ' to webhook ' + webhook.url ) + const realUrl = resolveWebhookUrl(webhook.url) + if (realUrl === undefined) { + console.log(`Webhook address not configured (${webhook.url}), skipping notification`) + return + } try { - let url = webhook.url + let url = realUrl let method = webhook.method let headers = new Headers(webhook.headers as any) let payloadTemplated: { [key: string]: string | number } = JSON.parse( @@ -128,7 +151,7 @@ async function webhookNotify(webhook: WebhookConfig, message: string) { } console.log( - `Webhook finalized parameters: ${method} ${url}, headers ${JSON.stringify( + `Webhook finalized parameters: ${method} ${hideUrl(url, webhook.url, realUrl)}, headers ${JSON.stringify( Object.fromEntries(headers.entries()) )}, body ${JSON.stringify(body)}` ) @@ -142,7 +165,7 @@ async function webhookNotify(webhook: WebhookConfig, message: string) { console.log('Webhook notification sent successfully, code: ' + resp.status) } } catch (e) { - console.log('Error calling webhook server: ' + e) + console.log('Error calling webhook server: ' + hideUrl(String(e), webhook.url, realUrl)) } } @@ -198,4 +221,5 @@ export { webhookNotify, formatStatusChangeNotification, formatAndNotify, + setWebhookSecrets, }