a certificate for a name Let's Encrypt cannot visit
the whole reason the credential store exists. a tailnet-only hostname cannot be reached on port 80, so HTTP-01 is impossible and a DNS record is the only proof Let's Encrypt accepts — which needs the provider's API key. POST /domains/certificate is the one route here that is deliberately not a pass-through, and it has to be: the request needs dns_provider_credentials, and officer never returns a stored key to any caller. so the platform resolves which provider owns the name from the Domains list, decrypts, builds the request and returns NPM's own answer unchanged. same arrangement as the NPM token one level up — the thing that wants the work done is not the thing holding the password. NOT under /_config. reading and writing a DNS key stays owner-only; USING it on the owner's behalf is the feature, and gating this to the owner would defeat it. a read-level account still cannot reach it, because it is a POST. three refusals, all before anything is placed at the registrar: a name whose apex is not in the domain list, a provider with no key stored, and a certificate spanning two providers — NPM takes one credential per certificate, and discovering that halfway through leaves a challenge record behind. tested, because each one otherwise fails deep inside certbot with an error about something else. ten minute timeout, 120s default propagation. both err long on purpose: NPM waits for the record to spread before asking Let's Encrypt to look, and abandoning the request does not stop it. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Officer-Session: 656f5c85-e49a-4f1f-b9f3-042a04b4d043
This commit is contained in:
+103
-2
@@ -190,8 +190,8 @@ export async function removeCredential(provider: string): Promise<void> {
|
||||
/**
|
||||
* Decrypted, for the code that places a challenge record. **Never serialise the result of this.**
|
||||
*
|
||||
* Nothing calls it yet — issuing a certificate through DNS-01 is the next piece, and it is the only thing
|
||||
* that should. It exists now so the storage above is provably usable rather than write-only.
|
||||
* `issueWithDnsChallenge` below is the only caller and the only thing that should be — it is the whole
|
||||
* point of storing these at all.
|
||||
*/
|
||||
export async function readCredentialFields(provider: string): Promise<Record<string, string> | null> {
|
||||
const [row] = await db
|
||||
@@ -201,3 +201,104 @@ export async function readCredentialFields(provider: string): Promise<Record<str
|
||||
if (!row) return null;
|
||||
return JSON.parse(decryptSecret(PURPOSE, row.secret)) as Record<string, string>;
|
||||
}
|
||||
|
||||
// ── Issuing a certificate with the stored DNS credential ────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* Which stored domain owns a hostname.
|
||||
*
|
||||
* `private.officer.dev` belongs to the `officer.dev` row. Longest match wins, so a machine holding both
|
||||
* `officer.dev` and `dev.officer.dev` attributes `a.dev.officer.dev` to the more specific one — which is
|
||||
* the one whose provider is likelier to be right if they ever differ.
|
||||
*/
|
||||
export async function ownerOf(hostname: string): Promise<DomainView | null> {
|
||||
const name = normaliseDomain(hostname);
|
||||
const rows = await listDomains();
|
||||
return (
|
||||
rows
|
||||
.filter((row) => name === row.domain || name.endsWith(`.${row.domain}`))
|
||||
.sort((a, b) => b.domain.length - a.domain.length)[0] ?? null
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* certbot wants an ini file; we hold a JSON object. This is the whole translation.
|
||||
*
|
||||
* Field names came from NPM's own `dns-providers` template when the key was entered, so they are already
|
||||
* the ones certbot expects — nothing is renamed here, and nothing should be.
|
||||
*/
|
||||
const toIni = (fields: Record<string, string>): string =>
|
||||
Object.entries(fields)
|
||||
.map(([key, value]) => `${key} = ${value}`)
|
||||
.join('\n');
|
||||
|
||||
export type IssueParams = {
|
||||
domainNames: string[];
|
||||
/**
|
||||
* How long to wait for the challenge record to be visible before asking Let's Encrypt to look.
|
||||
*
|
||||
* Two minutes by default, which is generous for Cloudflare and barely enough for GoDaddy — their TTLs
|
||||
* are minutes, not seconds. Too short is the failure that wastes a Let's Encrypt attempt and reads as a
|
||||
* credential problem, so the default errs long.
|
||||
*/
|
||||
propagationSeconds?: number;
|
||||
};
|
||||
|
||||
/**
|
||||
* Issue a certificate over DNS-01, using the credential Officer holds.
|
||||
*
|
||||
* **This is the one route in this plugin that is deliberately not a pass-through**, and the reason is the
|
||||
* credential: it is never returned across the API, so no caller — agent or human — can assemble this
|
||||
* request themselves. The platform decrypts it, puts it in the request, and hands back what NPM said.
|
||||
*
|
||||
* That is the same arrangement as the NPM token in `create-proxy.ts`, one level up: the thing that wants
|
||||
* the work done is not the thing holding the password.
|
||||
*
|
||||
* ── Why this exists at all ──
|
||||
*
|
||||
* A hostname that only resolves on a VPN cannot be visited by Let's Encrypt, so HTTP-01 is impossible and
|
||||
* a DNS record is the only proof it will accept. Every publicly-pointing name should use the plain
|
||||
* `POST /npm/nginx/certificates` instead — it needs no credential and no waiting.
|
||||
*/
|
||||
export async function issueWithDnsChallenge(params: IssueParams): Promise<{ provider: string; body: unknown }> {
|
||||
const names = params.domainNames.map(normaliseDomain).filter(Boolean);
|
||||
if (names.length === 0) throw new DomainError('At least one domain name is required.', 400);
|
||||
|
||||
// Every name must belong to the same provider: NPM takes ONE credential per certificate, so a
|
||||
// certificate spanning two providers is not a thing that can be requested, and discovering that
|
||||
// halfway through would leave a challenge record behind.
|
||||
const owners = await Promise.all(names.map((name) => ownerOf(name)));
|
||||
const missing = names.filter((_, index) => !owners[index]);
|
||||
if (missing.length > 0) {
|
||||
throw new DomainError(
|
||||
`Not in the domain list: ${missing.join(', ')}. Add the domain first so Officer knows whose DNS it is.`,
|
||||
400,
|
||||
);
|
||||
}
|
||||
|
||||
const providers = [...new Set(owners.map((owner) => owner!.dnsProvider).filter(Boolean))];
|
||||
if (providers.length === 0) throw new DomainError('That domain has no recognised DNS provider.', 400);
|
||||
if (providers.length > 1) {
|
||||
throw new DomainError(`One certificate cannot span two DNS providers (${providers.join(', ')}).`, 400);
|
||||
}
|
||||
|
||||
const provider = providers[0]!;
|
||||
const fields = await readCredentialFields(provider);
|
||||
if (!fields) {
|
||||
throw new DomainError(`No API key stored for ${provider}. Add one from the Domains section.`, 409);
|
||||
}
|
||||
|
||||
return {
|
||||
provider,
|
||||
body: {
|
||||
provider: 'letsencrypt',
|
||||
domain_names: names,
|
||||
meta: {
|
||||
dns_challenge: true,
|
||||
dns_provider: provider,
|
||||
dns_provider_credentials: toIni(fields),
|
||||
propagation_seconds: params.propagationSeconds ?? 120,
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user