cloud / internal-packages / installer / test / proof-worker / prove-enrol.mjsmain at aacbc74
// prove-enrol.mjs — A DEPLOYMENT THE WIZARD DID NOT MAKE ENROLS WITH THE CLOUD, by a member's
// consent, as a script: the Cloud's enrol page, the deployment's own /keys/enrol as a signed-in
// member, Allow, and the Cloud's "Finish here". It prints what each page says. Exit 0: enrolled.
//
// node test/proof-worker/prove-enrol.mjs <domain> <member's session file> [the Cloud's origin]
// CUT_FIRST=1 node … first the member removes the Cloud's key on the deployment's /keys (the
// owner cuts the cord), so the run shows a deployment that does not trust
// the Cloud, and then the consent that makes it enrol
//
// The session file is JSON with `header`, the member's `cookie:` header on iterate.<domain> (a
// person's browser has it after they sign in). It is read, sent to iterate.<domain> alone, and
// never printed.
import { readFileSync } from "node:fs";
const [
,
,
domain,
sessionFile,
cloud = "https://iterate-access-proof.iterate-dev-preview.workers.dev",
] = process.argv;
if (!domain || !sessionFile) {
console.error("usage: see the head of this file");
process.exit(2);
}
const issuer = `https://iterate.${domain}`;
const cookie = JSON.parse(readFileSync(sessionFile, "utf8")).header;
const words = (html) =>
html
.replace(/<style[\s\S]*?<\/style>|<input[^>]*>/g, "")
.replace(/<[^>]+>/g, "\n")
.replace(/&/g, "&")
.replace(/'/g, "'")
.replace(/"/g, '"')
.split("\n")
.map((line) => line.trim())
.filter(Boolean)
.join(" ");
const say = (title, text) => console.log(`--- ${title}\n${text.slice(0, 600)}`);
if (process.env.CUT_FIRST) {
const keys = await (await fetch(`${issuer}/keys`, { headers: { cookie } })).text();
const key = /action="\/keys\/remove"[\s\S]*?name="key" value="([^"]+)"/.exec(keys)?.[1];
if (key) {
const removed = await fetch(`${issuer}/keys/remove`, {
method: "POST",
redirect: "manual",
headers: { cookie, origin: issuer, "content-type": "application/x-www-form-urlencoded" },
body: new URLSearchParams({ key }),
});
say(
`the member removes the key ${key.slice(0, 8)} on the deployment [${removed.status}]`,
words(await removed.text()) || "(sent back to the keys page)",
);
} else say("the member's keys page", "no key of a service is listed: nothing to remove");
}
const before = await fetch(`${cloud}/enrolled?domain=${domain}`);
say(`the Cloud's /enrolled before the consent [${before.status}]`, words(await before.text()));
const enrolPage = await (await fetch(`${cloud}/enrol?domain=${domain}`)).text();
say("the Cloud's enrol page", words(enrolPage));
const link = /href="([^"]*\/keys\/enrol[^"]*)"/.exec(enrolPage)?.[1].replaceAll("&", "&");
if (!link?.startsWith(`${issuer}/keys/enrol?`))
throw new Error("the enrol page has no link to the deployment's /keys/enrol");
const asked = await fetch(link, { headers: { cookie }, redirect: "manual" });
const askedHtml = await asked.text();
say(`the deployment's page, as the member [${asked.status}]`, words(askedHtml));
if (asked.status !== 200)
throw new Error("the member's session did not open the page: sign in again");
const fields = [...askedHtml.matchAll(/<input type="hidden" name="([^"]+)" value="([^"]*)"/g)].map(
([, name, value]) => [
name,
value.replaceAll("&", "&").replaceAll("'", "'").replaceAll(""", '"'),
],
);
const allowed = await fetch(`${issuer}/keys/enrol`, {
method: "POST",
redirect: "manual",
headers: { cookie, origin: issuer, "content-type": "application/x-www-form-urlencoded" },
body: new URLSearchParams([...fields, ["decision", "allow"]]),
});
say(
`Allow [${allowed.status}${allowed.headers.get("location") ? ` to ${allowed.headers.get("location")}` : ""}]`,
words(await allowed.text()),
);
const done = await fetch(`${cloud}/enrolled?domain=${domain}`);
say(`the Cloud's "Finish here" [${done.status}]`, words(await done.text()));
process.exit(done.status === 200 ? 0 : 1);