Read the apps check address from a Worker secret (#5)
A monitor's target can be secret:NAME: the worker reads the address from the Worker secret NAME at check time, bound in deploy.tf from the GitHub Actions secret of the same name. The address never appears in this repository, and any error text that mentions it is scrubbed before it's stored or shown. Apps in US East now checks a real app this way.
This commit is contained in:
1 parent
035d23d1e3
commit
cf977e65da
4 files changed
+50
-5
No files matched your search
@@ -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.
|
||||
|
||||
@@ -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
|
||||
}]
|
||||
}
|
||||
|
||||
|
||||
+5
-4
@@ -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)' },
|
||||
|
||||
+33
-1
@@ -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<string, unknown>)[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<void> {
|
||||
const workerLocation = (await getWorkerLocation()) || 'ERROR'
|
||||
@@ -31,7 +63,7 @@ const Worker = {
|
||||
let checkResult: Record<string, CheckResult> = {};
|
||||
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
|
||||
|
||||
Reference in new issue
Block a user