cloudflare-dns: config from settings, not static manifest env (ADR 0051)
Which zone, domain and ingress are a mesh's facts, not the module's — so they are settings merged into a config file the mesh manages, read by fromEnv, rather than the empty env placeholders I wrongly baked in. The token stays the one own-secret. The module now describes a Cloudflare registrar; which zone is a setting, so the same description serves every mesh. Typechecks; manifest parses.
This commit is contained in:
@@ -23,14 +23,19 @@ export class CloudflareClient {
|
|||||||
) {}
|
) {}
|
||||||
|
|
||||||
static fromEnv(env: NodeJS.ProcessEnv = process.env): CloudflareClient {
|
static fromEnv(env: NodeJS.ProcessEnv = process.env): CloudflareClient {
|
||||||
|
// Which zone, domain and ingress are a mesh's own facts, not this module's — so they are
|
||||||
|
// settings, merged into a config file the mesh manages (novox/hq ADR 0051), read here. The
|
||||||
|
// token is the one secret and stays an own-secret. Env is honoured as a fallback for a
|
||||||
|
// hand-run instance, but the deployed path is the config file settings fill.
|
||||||
|
const config = readConfig(env.MESH_CLOUDFLARE_CONFIG_FILE);
|
||||||
const token = env.MESH_CLOUDFLARE_TOKEN ?? readSecret(env.MESH_CLOUDFLARE_TOKEN_FILE);
|
const token = env.MESH_CLOUDFLARE_TOKEN ?? readSecret(env.MESH_CLOUDFLARE_TOKEN_FILE);
|
||||||
const zoneId = env.MESH_CLOUDFLARE_ZONE_ID;
|
const zoneId = config.zone ?? env.MESH_CLOUDFLARE_ZONE_ID;
|
||||||
const domain = env.MESH_PUBLIC_DOMAIN;
|
const domain = config.domain ?? env.MESH_PUBLIC_DOMAIN;
|
||||||
const ingress = env.MESH_PUBLIC_INGRESS;
|
const ingress = config.ingress ?? env.MESH_PUBLIC_INGRESS;
|
||||||
if (!token || !zoneId || !domain || !ingress) {
|
if (!token || !zoneId || !domain || !ingress) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
"cloudflare-dns needs MESH_CLOUDFLARE_TOKEN (or _FILE), MESH_CLOUDFLARE_ZONE_ID, " +
|
"cloudflare-dns is not configured — set its zone, domain and ingress in settings (and the " +
|
||||||
"MESH_PUBLIC_DOMAIN and MESH_PUBLIC_INGRESS — it cannot register a name without them",
|
"token as its own-secret); until then it registers nothing",
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return new CloudflareClient(token, zoneId, domain, ingress);
|
return new CloudflareClient(token, zoneId, domain, ingress);
|
||||||
@@ -103,3 +108,20 @@ function readSecret(path: string | undefined): string | undefined {
|
|||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface Config {
|
||||||
|
zone?: string;
|
||||||
|
domain?: string;
|
||||||
|
ingress?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The settings-managed config file (a JSON document the mesh merges settings into). Absent or
|
||||||
|
* unparseable yields an empty config, which fromEnv then reports as unconfigured. */
|
||||||
|
function readConfig(path: string | undefined): Config {
|
||||||
|
if (!path) return {};
|
||||||
|
try {
|
||||||
|
return JSON.parse(readFileSync(path, "utf8")) as Config;
|
||||||
|
} catch {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -37,6 +37,14 @@
|
|||||||
"path": "/var/lib/cloudflare-dns/grants",
|
"path": "/var/lib/cloudflare-dns/grants",
|
||||||
"mode": "0700"
|
"mode": "0700"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"id": "config",
|
||||||
|
"type": "file",
|
||||||
|
"path": "/var/lib/cloudflare-dns/config.json",
|
||||||
|
"merge": "json",
|
||||||
|
"content": "{}",
|
||||||
|
"mode": "0600"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"id": "provisioner",
|
"id": "provisioner",
|
||||||
"type": "container",
|
"type": "container",
|
||||||
@@ -47,11 +55,10 @@
|
|||||||
"GRANTS": "/grants",
|
"GRANTS": "/grants",
|
||||||
"MESH_CLOUDFLARE_TOKEN_FILE": "/run/secrets/token",
|
"MESH_CLOUDFLARE_TOKEN_FILE": "/run/secrets/token",
|
||||||
"MESH_BROKER_FILE": "/run/secrets/broker",
|
"MESH_BROKER_FILE": "/run/secrets/broker",
|
||||||
"MESH_CLOUDFLARE_ZONE_ID": "",
|
"MESH_CLOUDFLARE_CONFIG_FILE": "/run/config/config.json"
|
||||||
"MESH_PUBLIC_DOMAIN": "",
|
|
||||||
"MESH_PUBLIC_INGRESS": ""
|
|
||||||
},
|
},
|
||||||
"volumes": [
|
"volumes": [
|
||||||
|
"/var/lib/cloudflare-dns/config.json:/run/config/config.json:ro",
|
||||||
"/var/lib/cloudflare-dns/grants:/grants",
|
"/var/lib/cloudflare-dns/grants:/grants",
|
||||||
"/var/lib/cloudflare-dns/token:/run/secrets/token:ro",
|
"/var/lib/cloudflare-dns/token:/run/secrets/token:ro",
|
||||||
"/var/lib/cloudflare-dns/broker:/run/secrets/broker:ro"
|
"/var/lib/cloudflare-dns/broker:/run/secrets/broker:ro"
|
||||||
|
|||||||
Reference in New Issue
Block a user