diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 970744d..6e44935 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -104,6 +104,7 @@ jobs: env: 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 }} 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 0055c26..c07e802 100644 --- a/deploy.tf +++ b/deploy.tf @@ -16,6 +16,13 @@ variable "CLOUDFLARE_ACCOUNT_ID" { type = string } +# Cottage: private monitor addresses (see `secret:` targets in uptime.config.ts). +variable "APPS_US_EAST_TARGET" { + # read from $TF_VAR_APPS_US_EAST_TARGET (the GitHub Actions secret of the same name) + type = string + sensitive = true +} + variable "enable_do_migration" { type = bool default = false @@ -59,6 +66,10 @@ resource "cloudflare_workers_script" "uptimeflare_worker" { name = "UPTIMEFLARE_D1" type = "d1" id = cloudflare_d1_database.uptimeflare_d1.id + }, { + name = "APPS_US_EAST_TARGET" + type = "secret_text" + text = var.APPS_US_EAST_TARGET }] } diff --git a/uptime.config.ts b/uptime.config.ts index 9ebabc0..840c59e 100644 --- a/uptime.config.ts +++ b/uptime.config.ts @@ -43,14 +43,15 @@ const workerConfig: WorkerConfig = { headers: { 'User-Agent': 'cottage-status (+https://status.cottage.host)' }, }, { - // A small app Cottage runs for itself on the region's servers (ntfy, "Status canary" on - // Cottage's own internal account), so this checks the whole path a customer's app takes: - // Cloudflare, the node's proxy, Harbor and a running container. + // A real app running on the region's servers (on Cottage's own internal account), so this + // checks the whole path a customer's app takes: Cloudflare, the node's proxy, Harbor and a + // running container. Its address is private: the worker reads it from the Worker secret + // APPS_US_EAST_TARGET (GitHub Actions secret of the same name, bound in deploy.tf). id: 'apps-us-east', name: 'Apps in US East', tooltip: 'Customer apps hosted in Virginia', method: 'GET', - target: 'https://status-canary-m7ec.cottagehost.com/v1/health', + target: 'secret:APPS_US_EAST_TARGET', expectedCodes: [200], timeout: 10000, headers: { 'User-Agent': 'cottage-status (+https://status.cottage.host)' }, diff --git a/worker/src/index.ts b/worker/src/index.ts index 64e4e30..df3b257 100644 --- a/worker/src/index.ts +++ b/worker/src/index.ts @@ -11,6 +11,38 @@ export interface Env { UPTIMEFLARE_D1: D1Database } +// Cottage: a monitor's target may be `secret:NAME`, read at check time from the Worker secret +// NAME (bound in deploy.tf from a GitHub Actions secret), so a private address never appears in +// this public repo. Error text is scrubbed of the address before it's stored or shown. +type CheckStatus = { ping: number; up: boolean; err: string } + +function secretTarget(monitor: MonitorTarget, env: Env): { monitor: MonitorTarget; secret?: string } | { missing: string } { + if (!monitor.target.startsWith('secret:')) return { monitor } + const name = monitor.target.slice('secret:'.length) + const value = (env as unknown as Record)[name] + if (typeof value !== 'string' || value === '') return { missing: name } + return { monitor: { ...monitor, target: value }, secret: value } +} + +function scrub(status: CheckStatus, secret: string): CheckStatus { + let err = status.err + const parts = [secret] + try { + parts.push(new URL(secret).host, new URL(secret).hostname) + } catch {} + for (const p of parts) if (p) err = err.split(p).join('the app') + return { ...status, err } +} + +async function checkMonitor(monitor: MonitorTarget, location: string, env: Env) { + const r = secretTarget(monitor, env) + if ('missing' in r) { + return { id: monitor.id, location, status: { ping: 0, up: false, err: `Check address not configured (${r.missing})` } } + } + const result = await doMonitor(r.monitor, location, env) + return r.secret ? { ...result, status: scrub(result.status, r.secret) } : result +} + const Worker = { async scheduled(event: ScheduledEvent, env: Env, ctx: ExecutionContext): Promise { const workerLocation = (await getWorkerLocation()) || 'ERROR' @@ -31,7 +63,7 @@ const Worker = { let checkResult: Record = {}; const limit = pLimit(5); for (const monitor of workerConfig.monitors) { - checkQueue.push(limit(() => doMonitor(monitor, workerLocation, env))) + checkQueue.push(limit(() => checkMonitor(monitor, workerLocation, env))) } for (const result of await Promise.all(checkQueue)) { checkResult[result.id] = result