Merge remote-tracking branch 'q102/feat/alert-channel-pushover' into feat/ops-hardening-q100-q101-q102
commit
b2b9472d51
|
|
@ -106,6 +106,12 @@ jobs:
|
|||
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
|
||||
TF_VAR_CLOUDFLARE_ACCOUNT_ID: ${{ steps.fetch_account_id.outputs.account_id }}
|
||||
D1_ID: ${{ env.D1_ID }}
|
||||
# Alert channel (worker/src/pushover.ts). If these repository secrets
|
||||
# are unset the apply still succeeds, the bindings are simply omitted,
|
||||
# and the worker logs "[ALERT-CHANNEL-BROKEN] … missing Worker
|
||||
# secret(s) …" for every alert it could not deliver.
|
||||
TF_VAR_PUSHOVER_TOKEN: ${{ secrets.PUSHOVER_TOKEN }}
|
||||
TF_VAR_PUSHOVER_USER: ${{ secrets.PUSHOVER_USER }}
|
||||
|
||||
# Currently Terraform Cloudflare provider doesn't support direct upload, use wrangler to upload instead.
|
||||
- name: Upload pages
|
||||
|
|
|
|||
36
deploy.tf
36
deploy.tf
|
|
@ -21,6 +21,24 @@ variable "enable_do_migration" {
|
|||
default = false
|
||||
}
|
||||
|
||||
# Pushover alert channel (worker/src/pushover.ts). Read from the environment as
|
||||
# $TF_VAR_PUSHOVER_TOKEN / $TF_VAR_PUSHOVER_USER, which deploy.yml fills from
|
||||
# the GitHub repository secrets of the same name. Defaulted to "" so a fork or
|
||||
# a first-time deploy without the secrets still applies cleanly — the worker
|
||||
# then logs "[ALERT-CHANNEL-BROKEN] … missing Worker secret(s) …" on every
|
||||
# alert it had to drop, instead of failing silently.
|
||||
variable "PUSHOVER_TOKEN" {
|
||||
type = string
|
||||
sensitive = true
|
||||
default = ""
|
||||
}
|
||||
|
||||
variable "PUSHOVER_USER" {
|
||||
type = string
|
||||
sensitive = true
|
||||
default = ""
|
||||
}
|
||||
|
||||
resource "cloudflare_d1_database" "uptimeflare_d1" {
|
||||
account_id = var.CLOUDFLARE_ACCOUNT_ID
|
||||
name = "uptimeflare_d1"
|
||||
|
|
@ -51,7 +69,10 @@ resource "cloudflare_workers_script" "uptimeflare_worker" {
|
|||
new_sqlite_classes = ["RemoteChecker"]
|
||||
} : null
|
||||
|
||||
bindings = [{
|
||||
# `bindings` is authoritative: whatever is NOT listed here is removed from the
|
||||
# worker on the next apply. So the Pushover secrets must be bound here — a
|
||||
# `wrangler secret put` would survive exactly until the next deploy.
|
||||
bindings = concat([{
|
||||
name = "REMOTE_CHECKER_DO"
|
||||
class_name = "RemoteChecker"
|
||||
type = "durable_object_namespace"
|
||||
|
|
@ -59,7 +80,18 @@ resource "cloudflare_workers_script" "uptimeflare_worker" {
|
|||
name = "UPTIMEFLARE_D1"
|
||||
type = "d1"
|
||||
id = cloudflare_d1_database.uptimeflare_d1.id
|
||||
}]
|
||||
}],
|
||||
# Only bound when both are actually supplied; Cloudflare rejects an empty
|
||||
# secret_text, and a half-configured channel would be worse than none.
|
||||
var.PUSHOVER_TOKEN != "" && var.PUSHOVER_USER != "" ? [{
|
||||
name = "PUSHOVER_TOKEN"
|
||||
type = "secret_text"
|
||||
text = var.PUSHOVER_TOKEN
|
||||
}, {
|
||||
name = "PUSHOVER_USER"
|
||||
type = "secret_text"
|
||||
text = var.PUSHOVER_USER
|
||||
}] : [])
|
||||
}
|
||||
|
||||
resource "cloudflare_workers_cron_trigger" "uptimeflare_worker_cron" {
|
||||
|
|
|
|||
|
|
@ -110,14 +110,22 @@ const workerConfig: WorkerConfig = {
|
|||
// ---------------------------------------------------------------------
|
||||
],
|
||||
notification: {
|
||||
// Wire Pushover here (same account as ops/minimax-usage.sh) — needs the
|
||||
// owner's Pushover token/user key; do not commit real keys, use the
|
||||
// full-config option of reading from env if desired.
|
||||
// webhook: {
|
||||
// url: 'https://api.pushover.net/1/messages.json',
|
||||
// payloadType: 'x-www-form-urlencoded',
|
||||
// payload: { token: 'APP_TOKEN', user: 'USER_KEY', message: '$MSG' },
|
||||
// },
|
||||
// ALERT CHANNEL — Pushover, wired in worker/src/pushover.ts.
|
||||
//
|
||||
// `webhook` stays UNSET here on purpose and is not a gap: the upstream
|
||||
// webhook path (worker/src/util.ts:83) reads its credentials out of THIS
|
||||
// committed object, and this repository is public, so a Pushover app token
|
||||
// and user key cannot live here. The fork sends the identical formatted
|
||||
// message from Worker SECRET bindings instead (PUSHOVER_TOKEN /
|
||||
// PUSHOVER_USER, see worker/src/index.ts Env and deploy.tf).
|
||||
//
|
||||
// Everything below still applies to it: it is dispatched by the same
|
||||
// `formatAndNotify`, so skipNotificationIds, maintenance windows and the
|
||||
// grace period gate Pushover exactly as they gated the webhook.
|
||||
//
|
||||
// If the secrets are not set, the channel is FAIL-CLOSED AND LOUD: every
|
||||
// dropped alert logs `[ALERT-CHANNEL-BROKEN] … missing Worker secret(s) …`
|
||||
// as an error, so an outage nobody could be told about is itself visible.
|
||||
timeZone: 'Europe/Berlin',
|
||||
// Avoid flapping alerts: notify only after 3 consecutive failed checks.
|
||||
gracePeriod: 3,
|
||||
|
|
|
|||
|
|
@ -15,6 +15,12 @@ import pLimit from 'p-limit'
|
|||
export interface Env {
|
||||
REMOTE_CHECKER_DO: DurableObjectNamespace<RemoteChecker>
|
||||
UPTIMEFLARE_D1: D1Database
|
||||
// Pushover alert channel (worker/src/pushover.ts). Bound as Worker SECRETS
|
||||
// by deploy.tf from the GitHub repository secrets of the same name — never
|
||||
// committed, this repository is public. Optional in the type on purpose:
|
||||
// when they are absent the run must still work and must SAY they are absent.
|
||||
PUSHOVER_TOKEN?: string
|
||||
PUSHOVER_USER?: string
|
||||
}
|
||||
|
||||
const Worker = {
|
||||
|
|
@ -88,7 +94,14 @@ const Worker = {
|
|||
currentTimeSecond - lastIncident.start[0] >=
|
||||
(workerConfig.notification.gracePeriod + 1) * 60 - 30
|
||||
) {
|
||||
await formatAndNotify(monitor, true, lastIncident.start[0], currentTimeSecond, 'OK')
|
||||
await formatAndNotify(
|
||||
env,
|
||||
monitor,
|
||||
true,
|
||||
lastIncident.start[0],
|
||||
currentTimeSecond,
|
||||
'OK'
|
||||
)
|
||||
} else {
|
||||
console.log(
|
||||
`grace period (${workerConfig.notification?.gracePeriod}m) not met, skipping webhook UP notification for ${monitor.name}`
|
||||
|
|
@ -156,6 +169,7 @@ const Worker = {
|
|||
)
|
||||
} else {
|
||||
await formatAndNotify(
|
||||
env,
|
||||
monitor,
|
||||
false,
|
||||
currentIncident.start[0],
|
||||
|
|
|
|||
|
|
@ -0,0 +1,109 @@
|
|||
// Pushover alert channel for the status worker.
|
||||
//
|
||||
// WHY THIS FILE EXISTS
|
||||
// The fork ships exactly one built-in notification path, `notification.webhook`
|
||||
// in uptime.config.ts (dispatched by worker/src/util.ts:82 `webhookNotify`).
|
||||
// That path takes its credentials from the *committed* config object, so wiring
|
||||
// Pushover through it would put the app token and the user key into a PUBLIC
|
||||
// git repository. It stayed commented out for that reason — which meant every
|
||||
// outage, including an outage of the check regions themselves, notified nobody.
|
||||
//
|
||||
// This module sends the same formatted message to Pushover, but reads the two
|
||||
// credentials from Worker *secret bindings* (env), so nothing secret is ever
|
||||
// committed. It is deliberately FAIL-CLOSED AND LOUD: if a credential is
|
||||
// missing the run does not silently skip, it logs an error that names exactly
|
||||
// what is missing and what the operator has to do.
|
||||
//
|
||||
// Same account/endpoint as the Mac mini's existing alert path
|
||||
// (`~/osg/ops/minimax-usage.sh`, credentials in `~/osg/shared/.env` under the
|
||||
// keys PUSHOVER_TOKEN / PUSHOVER_USER). Pushover's API is a plain HTTPS POST
|
||||
// with a form body and needs no SDK, no account beyond the existing one and no
|
||||
// egress that a Cloudflare Worker cannot do.
|
||||
|
||||
/** Secret bindings this channel needs. Optional on purpose: absence is a state
|
||||
* we must detect and report, not a compile error. */
|
||||
export type PushoverEnv = {
|
||||
PUSHOVER_TOKEN?: string
|
||||
PUSHOVER_USER?: string
|
||||
}
|
||||
|
||||
const PUSHOVER_API = 'https://api.pushover.net/1/messages.json'
|
||||
|
||||
/** Marker every failure of the alert channel itself carries, so it can be
|
||||
* grepped in `wrangler tail` / Workers Logs without knowing this file. */
|
||||
export const ALERT_CHANNEL_BROKEN = '[ALERT-CHANNEL-BROKEN]'
|
||||
|
||||
/**
|
||||
* Send one status-change message to Pushover.
|
||||
*
|
||||
* Never throws: a broken alert channel must not abort the monitoring run that
|
||||
* produced the alert. Every failure path logs with `console.error` so it shows
|
||||
* up as an error in Workers Logs (observability is on, deploy.tf:38-45).
|
||||
*/
|
||||
export async function pushoverNotify(
|
||||
env: PushoverEnv | undefined,
|
||||
message: string,
|
||||
isUp: boolean,
|
||||
monitorName: string,
|
||||
timeoutMs = 8000
|
||||
): Promise<boolean> {
|
||||
const token = env?.PUSHOVER_TOKEN?.trim()
|
||||
const user = env?.PUSHOVER_USER?.trim()
|
||||
|
||||
const missing: string[] = []
|
||||
if (!token) missing.push('PUSHOVER_TOKEN')
|
||||
if (!user) missing.push('PUSHOVER_USER')
|
||||
if (missing.length > 0) {
|
||||
// FAIL-CLOSED: no credential -> no alert. Say so loudly instead of
|
||||
// returning quietly, otherwise this looks exactly like "all is well".
|
||||
console.error(
|
||||
`${ALERT_CHANNEL_BROKEN} Pushover alert for "${monitorName}" (${
|
||||
isUp ? 'UP' : 'DOWN'
|
||||
}) was NOT sent: missing Worker secret(s) ${missing.join(
|
||||
' and '
|
||||
)}. Nobody is being notified about outages. Fix: set the GitHub repository secret(s) ` +
|
||||
`${missing.join(' and ')} on k55f97/osg-status; deploy.yml passes them as TF_VAR_* and ` +
|
||||
`deploy.tf binds them as Worker secrets. Message that was dropped: ${message}`
|
||||
)
|
||||
return false
|
||||
}
|
||||
|
||||
// priority 1 = bypass quiet hours for a real outage, 0 = normal for recovery.
|
||||
const body = new URLSearchParams({
|
||||
token: token as string,
|
||||
user: user as string,
|
||||
title: `OpenShopGraph Status: ${monitorName} ${isUp ? 'recovered' : 'DOWN'}`,
|
||||
message,
|
||||
priority: isUp ? '0' : '1',
|
||||
})
|
||||
|
||||
const controller = new AbortController()
|
||||
const timer = setTimeout(() => controller.abort(), timeoutMs)
|
||||
try {
|
||||
const resp = await fetch(PUSHOVER_API, {
|
||||
method: 'POST',
|
||||
headers: { 'content-type': 'application/x-www-form-urlencoded' },
|
||||
body: body.toString(),
|
||||
signal: controller.signal,
|
||||
})
|
||||
// Response body is small JSON and carries the error reason on 4xx; it never
|
||||
// echoes the credentials back, so it is safe to log. The request body is
|
||||
// NOT logged — it contains the token and the user key.
|
||||
const text = await resp.text()
|
||||
if (!resp.ok) {
|
||||
console.error(
|
||||
`${ALERT_CHANNEL_BROKEN} Pushover rejected the alert for "${monitorName}": HTTP ${resp.status} ${text}`
|
||||
)
|
||||
return false
|
||||
}
|
||||
console.log(`Pushover alert sent for "${monitorName}" (${isUp ? 'UP' : 'DOWN'}): ${text}`)
|
||||
return true
|
||||
} catch (e) {
|
||||
console.error(
|
||||
`${ALERT_CHANNEL_BROKEN} Pushover alert for "${monitorName}" could not be delivered: ${e}`
|
||||
)
|
||||
return false
|
||||
} finally {
|
||||
clearTimeout(timer)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
import { MonitorTarget, WebhookConfig } from '../../types/config'
|
||||
import { maintenances, workerConfig } from '../../uptime.config'
|
||||
import { pushoverNotify, PushoverEnv } from './pushover'
|
||||
|
||||
async function getWorkerLocation() {
|
||||
const res = await fetch('https://cloudflare.com/cdn-cgi/trace')
|
||||
|
|
@ -148,6 +149,11 @@ async function webhookNotify(webhook: WebhookConfig, message: string) {
|
|||
|
||||
// Auxiliary function to format notification and send it via webhook
|
||||
const formatAndNotify = async (
|
||||
// `env` carries the Worker secret bindings for credential-based channels
|
||||
// (Pushover). It is optional so that callers/tests without an env still
|
||||
// compile; a missing env is treated as "credential missing" and is reported
|
||||
// loudly by pushoverNotify rather than silently skipped.
|
||||
env: PushoverEnv | undefined,
|
||||
monitor: MonitorTarget,
|
||||
isUp: boolean,
|
||||
timeIncidentStart: number,
|
||||
|
|
@ -176,19 +182,26 @@ const formatAndNotify = async (
|
|||
return
|
||||
}
|
||||
|
||||
const notification = formatStatusChangeNotification(
|
||||
monitor,
|
||||
isUp,
|
||||
timeIncidentStart,
|
||||
timeNow,
|
||||
reason,
|
||||
workerConfig.notification?.timeZone ?? 'Etc/GMT'
|
||||
)
|
||||
|
||||
// Channel 1 (upstream): generic webhook, credentials would live in the
|
||||
// committed config — unusable for this PUBLIC repo, so normally unset.
|
||||
if (workerConfig.notification?.webhook) {
|
||||
const notification = formatStatusChangeNotification(
|
||||
monitor,
|
||||
isUp,
|
||||
timeIncidentStart,
|
||||
timeNow,
|
||||
reason,
|
||||
workerConfig.notification?.timeZone ?? 'Etc/GMT'
|
||||
)
|
||||
await webhookNotify(workerConfig.notification.webhook, notification)
|
||||
} else {
|
||||
console.log(`Webhook not set, skipping notification for ${monitor.name}`)
|
||||
}
|
||||
|
||||
// Channel 2 (fork): Pushover with credentials from Worker secret bindings.
|
||||
// Always attempted. If the secrets are missing this does NOT fall through
|
||||
// quietly — it logs an error naming the missing secret, so an unnotifiable
|
||||
// outage is itself visible in the logs.
|
||||
await pushoverNotify(env, notification, isUp, monitor.name)
|
||||
}
|
||||
|
||||
export {
|
||||
|
|
|
|||
Loading…
Reference in New Issue