@hallohuman/hightea 0.1.30 → 0.1.31
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +5 -0
- package/dist/bin.js +24 -362
- package/dist/chunk-19pwmgzg.js +5 -0
- package/dist/chunk-5s85pw9m.js +13 -0
- package/dist/chunk-5zb06f9b.js +2 -0
- package/dist/chunk-69dva4cx.js +58 -0
- package/dist/chunk-8rbp82rh.js +139 -0
- package/dist/chunk-8x5daqht.js +1 -0
- package/dist/chunk-9pv1nvfv.js +5 -0
- package/dist/chunk-db8mr7mp.js +1 -0
- package/dist/chunk-dkzzzkex.js +13 -0
- package/dist/chunk-f04t1q6v.js +2 -0
- package/dist/chunk-gcqhggbh.js +1 -0
- package/dist/chunk-jdzvh3gk.js +1 -0
- package/dist/chunk-p616vcdm.js +4 -0
- package/dist/chunk-r9yjtfhp.js +5 -0
- package/dist/chunk-xj3c5jgm.js +5 -0
- package/dist/chunk-xy8j3npr.js +1 -0
- package/dist/plugin/plugin.json +1 -1
- package/dist/plugin/plugin.mjs +51 -15364
- package/package.json +2 -1
- package/dist/chunk-0ab6h39c.js +0 -402
- package/dist/chunk-0r70ekxd.js +0 -8
- package/dist/chunk-13tpn1g6.js +0 -465
- package/dist/chunk-3rgfnt23.js +0 -16
- package/dist/chunk-5mezgvms.js +0 -139
- package/dist/chunk-9e325ntw.js +0 -89
- package/dist/chunk-cx3yvvkz.js +0 -7295
- package/dist/chunk-d5h204vj.js +0 -14
- package/dist/chunk-dvnzt54t.js +0 -7168
- package/dist/chunk-dvt4hb8z.js +0 -1491
- package/dist/chunk-gk8tyqac.js +0 -184
- package/dist/chunk-h8pdqn31.js +0 -157
- package/dist/chunk-ph04jess.js +0 -17
- package/dist/chunk-sbqmqp8e.js +0 -123
- package/dist/chunk-w0ebwtch.js +0 -161
- package/dist/chunk-y5r2yxx1.js +0 -347
package/dist/chunk-gk8tyqac.js
DELETED
|
@@ -1,184 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
deployService,
|
|
3
|
-
deploys,
|
|
4
|
-
connectAhp
|
|
5
|
-
} from "./chunk-13tpn1g6.js";
|
|
6
|
-
import"./chunk-dvnzt54t.js";
|
|
7
|
-
import {
|
|
8
|
-
runningAhpds2
|
|
9
|
-
} from "./chunk-dvt4hb8z.js";
|
|
10
|
-
|
|
11
|
-
// src/deploy.ts
|
|
12
|
-
import { readFile } from "node:fs/promises";
|
|
13
|
-
import { join } from "node:path";
|
|
14
|
-
var DEPLOY_USAGE = `hightea deploy <service> --pin <sha> [--host <id>] [--json]
|
|
15
|
-
` + "hightea deploy status <service> [--host <id>] [--json]";
|
|
16
|
-
var FULL_SHA = /^[0-9a-fA-F]{40}$/u;
|
|
17
|
-
var consoleIo = () => ({ out: (line) => console.log(line), err: (line) => console.error(line) });
|
|
18
|
-
var isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
19
|
-
var labelOf = (row) => row.hostIds.length > 0 ? `${row.hostIds.join(",")} (pid ${String(row.pid)})` : `pid ${String(row.pid)}`;
|
|
20
|
-
async function portOf(row, read) {
|
|
21
|
-
if (row.port !== undefined)
|
|
22
|
-
return row.port;
|
|
23
|
-
try {
|
|
24
|
-
const parsed = JSON.parse(await read(row.config));
|
|
25
|
-
if (isRecord(parsed) && typeof parsed["port"] === "number" && Number.isInteger(parsed["port"]))
|
|
26
|
-
return parsed["port"];
|
|
27
|
-
} catch {
|
|
28
|
-
return;
|
|
29
|
-
}
|
|
30
|
-
return;
|
|
31
|
-
}
|
|
32
|
-
async function tokenOf(row, read) {
|
|
33
|
-
const file = row.connectionTokenFile ?? join(row.configDir, "connection.token");
|
|
34
|
-
try {
|
|
35
|
-
return (await read(file)).trim() || undefined;
|
|
36
|
-
} catch {
|
|
37
|
-
return;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
async function localMachine(host, deps = {}) {
|
|
41
|
-
const read = deps.readFile ?? ((path) => readFile(path, "utf8"));
|
|
42
|
-
let rows;
|
|
43
|
-
try {
|
|
44
|
-
rows = await (deps.runningAhpds ?? runningAhpds2)();
|
|
45
|
-
} catch (error) {
|
|
46
|
-
return { error: `DEPLOY_NO_AHPD: the process table could not be read: ${error.message}` };
|
|
47
|
-
}
|
|
48
|
-
const wanted = host?.trim();
|
|
49
|
-
let chosen;
|
|
50
|
-
if (wanted !== undefined && wanted !== "") {
|
|
51
|
-
const matches = rows.filter((row) => row.hostIds.includes(wanted));
|
|
52
|
-
if (matches.length === 0)
|
|
53
|
-
return { error: `DEPLOY_NO_AHPD: no running ahpd on this machine serves host ${wanted} (running: ${rows.map(labelOf).join("; ") || "none"})` };
|
|
54
|
-
if (matches.length > 1)
|
|
55
|
-
return { error: `DEPLOY_NO_AHPD: ${String(matches.length)} running ahpds serve host ${wanted} (${matches.map(labelOf).join("; ")}); the process table cannot tell them apart` };
|
|
56
|
-
chosen = matches[0];
|
|
57
|
-
} else {
|
|
58
|
-
if (rows.length === 0)
|
|
59
|
-
return { error: "DEPLOY_NO_AHPD: no running ahpd on this machine; start one with `hightea host join` first" };
|
|
60
|
-
if (rows.length > 1)
|
|
61
|
-
return { error: `DEPLOY_NO_AHPD: this machine runs ${String(rows.length)} ahpds (${rows.map(labelOf).join("; ")}); name one with --host <id>` };
|
|
62
|
-
chosen = rows[0];
|
|
63
|
-
}
|
|
64
|
-
if (chosen === undefined)
|
|
65
|
-
return { error: "DEPLOY_NO_AHPD: no running ahpd on this machine" };
|
|
66
|
-
const port = await portOf(chosen, read);
|
|
67
|
-
if (port === undefined)
|
|
68
|
-
return { error: `DEPLOY_NO_AHPD: ${chosen.config} names no port and its command line has no --port; cannot reach this ahpd` };
|
|
69
|
-
const token = await tokenOf(chosen, read);
|
|
70
|
-
return {
|
|
71
|
-
url: `ws://${chosen.host ?? "127.0.0.1"}:${String(port)}`,
|
|
72
|
-
...token === undefined ? {} : { token },
|
|
73
|
-
label: chosen.hostIds[0] ?? `pid ${String(chosen.pid)}`
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
async function dial(found, deps) {
|
|
77
|
-
try {
|
|
78
|
-
return await (deps.connect ?? connectAhp)(found.url, { token: found.token, clientId: "hightea-deploy" });
|
|
79
|
-
} catch (error) {
|
|
80
|
-
return { error: `DEPLOY_NO_AHPD: ${found.url} did not answer: ${error.message}` };
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
var refusalLine = (code, message) => {
|
|
84
|
-
if (code === "RUNNING")
|
|
85
|
-
return `DEPLOY_RUNNING: ${message}`;
|
|
86
|
-
if (code === "UNKNOWN_SERVICE")
|
|
87
|
-
return `DEPLOY_UNKNOWN_SERVICE: ${message}`;
|
|
88
|
-
return `DEPLOY_REFUSED ${code}: ${message}`;
|
|
89
|
-
};
|
|
90
|
-
var driverFailure = (error) => /No channel at x-hh-deploys:\/\//u.test(error.message) ? "DEPLOY_NO_DRIVER: this ahpd has no deploy driver loaded (x-hh-deploys://); its deploy plugin is not in its configuration" : `DEPLOY_DRIVER_UNREADABLE: ${error.message}`;
|
|
91
|
-
async function deployPlace(options, io = consoleIo(), deps = {}) {
|
|
92
|
-
const service = options.service.trim();
|
|
93
|
-
if (service === "") {
|
|
94
|
-
io.err(`DEPLOY_SERVICE_REQUIRED: name the service to deploy
|
|
95
|
-
${DEPLOY_USAGE}`);
|
|
96
|
-
return 1;
|
|
97
|
-
}
|
|
98
|
-
const pin = options.pin.trim();
|
|
99
|
-
if (!FULL_SHA.test(pin)) {
|
|
100
|
-
io.err(`DEPLOY_PIN_NOT_SHA: --pin must be a full 40-character commit sha; '${pin}' is not`);
|
|
101
|
-
return 1;
|
|
102
|
-
}
|
|
103
|
-
const found = await localMachine(options.host, deps);
|
|
104
|
-
if ("error" in found) {
|
|
105
|
-
io.err(found.error);
|
|
106
|
-
return 1;
|
|
107
|
-
}
|
|
108
|
-
const host = await dial(found, deps);
|
|
109
|
-
if ("error" in host) {
|
|
110
|
-
io.err(host.error);
|
|
111
|
-
return 1;
|
|
112
|
-
}
|
|
113
|
-
try {
|
|
114
|
-
const answer = await deployService(host, { service, version: pin.toLowerCase() });
|
|
115
|
-
if (!answer.ok) {
|
|
116
|
-
io.err(refusalLine(answer.code, answer.message));
|
|
117
|
-
return 1;
|
|
118
|
-
}
|
|
119
|
-
if (options.json)
|
|
120
|
-
io.out(JSON.stringify({ machine: found.label, service: answer.service, pin: answer.sha, deployId: answer.deployId, state: answer.state }));
|
|
121
|
-
else
|
|
122
|
-
io.out(`started ${answer.service} at ${answer.sha}: deploy ${answer.deployId} on ${found.label}`);
|
|
123
|
-
return 0;
|
|
124
|
-
} catch (error) {
|
|
125
|
-
io.err(driverFailure(error));
|
|
126
|
-
return 1;
|
|
127
|
-
} finally {
|
|
128
|
-
host.close();
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
var rowOf = (run, machine) => ({
|
|
132
|
-
machine,
|
|
133
|
-
service: run.service,
|
|
134
|
-
state: run.state,
|
|
135
|
-
pin: run.to ?? null,
|
|
136
|
-
from: run.from ?? null,
|
|
137
|
-
...run.step === undefined ? {} : { step: run.step },
|
|
138
|
-
...run.reason === undefined ? {} : { reason: run.reason },
|
|
139
|
-
...run.exit === undefined ? {} : { exit: run.exit },
|
|
140
|
-
deployId: run.deployId,
|
|
141
|
-
...run.at === undefined ? {} : { at: run.at }
|
|
142
|
-
});
|
|
143
|
-
async function deployStatus(options, io = consoleIo(), deps = {}) {
|
|
144
|
-
const service = options.service.trim();
|
|
145
|
-
if (service === "") {
|
|
146
|
-
io.err(`DEPLOY_SERVICE_REQUIRED: name the service to read
|
|
147
|
-
${DEPLOY_USAGE}`);
|
|
148
|
-
return 1;
|
|
149
|
-
}
|
|
150
|
-
const found = await localMachine(options.host, deps);
|
|
151
|
-
if ("error" in found) {
|
|
152
|
-
io.err(found.error);
|
|
153
|
-
return 1;
|
|
154
|
-
}
|
|
155
|
-
const host = await dial(found, deps);
|
|
156
|
-
if ("error" in host) {
|
|
157
|
-
io.err(host.error);
|
|
158
|
-
return 1;
|
|
159
|
-
}
|
|
160
|
-
try {
|
|
161
|
-
const state = await deploys(host);
|
|
162
|
-
const matching = state.runs.filter((run) => run.service === service);
|
|
163
|
-
if (matching.length === 0) {
|
|
164
|
-
const why = state.error === undefined ? "" : ` (${state.error})`;
|
|
165
|
-
io.err(`DEPLOY_UNKNOWN_SERVICE: ${service} has no deploy on ${state.machine || found.label}${why}`);
|
|
166
|
-
return 1;
|
|
167
|
-
}
|
|
168
|
-
const row = rowOf(matching[0], state.machine || found.label);
|
|
169
|
-
if (options.json)
|
|
170
|
-
io.out(JSON.stringify(row));
|
|
171
|
-
else
|
|
172
|
-
io.out(`${row.state} ${row.service} pin ${row.pin ?? "?"} ${row.step === undefined ? "" : `${row.step} `}deploy ${row.deployId}${row.reason === undefined ? "" : ` last error: ${row.reason}`}`);
|
|
173
|
-
return 0;
|
|
174
|
-
} catch (error) {
|
|
175
|
-
io.err(driverFailure(error));
|
|
176
|
-
return 1;
|
|
177
|
-
} finally {
|
|
178
|
-
host.close();
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
export {
|
|
182
|
-
deployPlace,
|
|
183
|
-
deployStatus
|
|
184
|
-
};
|
package/dist/chunk-h8pdqn31.js
DELETED
|
@@ -1,157 +0,0 @@
|
|
|
1
|
-
import"./chunk-dvnzt54t.js";
|
|
2
|
-
import {
|
|
3
|
-
trustedKeyId,
|
|
4
|
-
localHost2
|
|
5
|
-
} from "./chunk-dvt4hb8z.js";
|
|
6
|
-
import {
|
|
7
|
-
hostTrust2
|
|
8
|
-
} from "./chunk-w0ebwtch.js";
|
|
9
|
-
|
|
10
|
-
// ../ahp-host/src/approve.ts
|
|
11
|
-
var NOT_WAITING = "That code is not waiting here. It may have expired, or already have been used." + ' On the browser, press "Approve this browser" again for a new code.';
|
|
12
|
-
async function terminalConfirm(question) {
|
|
13
|
-
const { createInterface } = await import("node:readline/promises");
|
|
14
|
-
const prompt = createInterface({ input: process.stdin, output: process.stderr });
|
|
15
|
-
try {
|
|
16
|
-
return /^y(es)?$/iu.test((await prompt.question(question)).trim());
|
|
17
|
-
} finally {
|
|
18
|
-
prompt.close();
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
function text(value) {
|
|
22
|
-
return typeof value === "string" ? value : "";
|
|
23
|
-
}
|
|
24
|
-
async function jsonBody(answer) {
|
|
25
|
-
try {
|
|
26
|
-
const parsed = await answer.json();
|
|
27
|
-
return typeof parsed === "object" && parsed !== null ? parsed : null;
|
|
28
|
-
} catch {
|
|
29
|
-
return null;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
function refusalLine(body, status) {
|
|
33
|
-
const error = body?.error;
|
|
34
|
-
if (typeof error === "object" && error !== null) {
|
|
35
|
-
const row = error;
|
|
36
|
-
const code = text(row.code);
|
|
37
|
-
const message = text(row.message);
|
|
38
|
-
if (code !== "" && message !== "")
|
|
39
|
-
return `${code}: ${message}`;
|
|
40
|
-
if (message !== "")
|
|
41
|
-
return message;
|
|
42
|
-
}
|
|
43
|
-
return `the deployment answered ${String(status)}`;
|
|
44
|
-
}
|
|
45
|
-
async function hostApprove(input, deps = { localHost: (host) => localHost2(host), trust: (trust) => hostTrust2(trust), confirm: terminalConfirm, fetch: (url, init) => fetch(url, init) }) {
|
|
46
|
-
const found = await deps.localHost(input.host);
|
|
47
|
-
if ("error" in found) {
|
|
48
|
-
console.error(found.error.replace(/^SERVICE_/u, "APPROVE_"));
|
|
49
|
-
return 1;
|
|
50
|
-
}
|
|
51
|
-
const origin = (input.origin ?? found.gateway ?? "").trim().replace(/\/+$/u, "");
|
|
52
|
-
if (origin === "") {
|
|
53
|
-
console.error(`APPROVE_ORIGIN_UNKNOWN: this machine's claim for host ${found.hostId} records no deployment address; name it with --origin <url> and the workspace with --workspace <slug>; nothing was approved`);
|
|
54
|
-
return 1;
|
|
55
|
-
}
|
|
56
|
-
const workspace = (input.workspace ?? found.workspaceId).trim();
|
|
57
|
-
if (workspace === "") {
|
|
58
|
-
console.error("APPROVE_WORKSPACE_UNKNOWN: no workspace slug to ask about; name it with --workspace <slug>; nothing was approved");
|
|
59
|
-
return 1;
|
|
60
|
-
}
|
|
61
|
-
const code = input.code.trim();
|
|
62
|
-
if (code === "") {
|
|
63
|
-
console.error("APPROVE_CODE_MISSING: give the code the browser shows, e.g. `hightea approve 7QK4-2M8X`; nothing was approved");
|
|
64
|
-
return 1;
|
|
65
|
-
}
|
|
66
|
-
const readUrl = `${origin}/api/ws/${encodeURIComponent(workspace)}/browser-approval/${encodeURIComponent(code)}`;
|
|
67
|
-
let answer;
|
|
68
|
-
try {
|
|
69
|
-
answer = await deps.fetch(readUrl, { headers: { accept: "application/json" }, redirect: "error" });
|
|
70
|
-
} catch (error) {
|
|
71
|
-
console.error(`APPROVE_UNREACHABLE: could not reach ${origin} (${error.message}); nothing was approved`);
|
|
72
|
-
return 1;
|
|
73
|
-
}
|
|
74
|
-
if (answer.status === 404) {
|
|
75
|
-
console.error(NOT_WAITING);
|
|
76
|
-
return 1;
|
|
77
|
-
}
|
|
78
|
-
const body = await jsonBody(answer);
|
|
79
|
-
if (body === null) {
|
|
80
|
-
console.error(`APPROVE_ANSWER_INVALID: ${origin} answered ${String(answer.status)} without a JSON body; nothing was approved`);
|
|
81
|
-
return 1;
|
|
82
|
-
}
|
|
83
|
-
if (answer.status !== 200) {
|
|
84
|
-
console.error(`APPROVE_REFUSED: ${refusalLine(body, answer.status)}; nothing was approved`);
|
|
85
|
-
return 1;
|
|
86
|
-
}
|
|
87
|
-
const state = text(body.state);
|
|
88
|
-
if (state === "approved") {
|
|
89
|
-
console.error(NOT_WAITING);
|
|
90
|
-
return 1;
|
|
91
|
-
}
|
|
92
|
-
if (state !== "pending") {
|
|
93
|
-
console.error(`APPROVE_ANSWER_INVALID: ${origin} answered a state this command does not know (${JSON.stringify(state)}); nothing was approved`);
|
|
94
|
-
return 1;
|
|
95
|
-
}
|
|
96
|
-
const browser = text(body.browser).trim();
|
|
97
|
-
const check = text(body.check).trim();
|
|
98
|
-
const publicKey = text(body.publicKey);
|
|
99
|
-
const personName = text(body.personName).trim();
|
|
100
|
-
const expiresAt = text(body.expiresAt);
|
|
101
|
-
const keyId = trustedKeyId(publicKey);
|
|
102
|
-
if (browser === "" || check === "" || "error" in keyId) {
|
|
103
|
-
console.error("APPROVE_ANSWER_INVALID: the deployment's answer names no browser and check characters, or carries no usable public key; nothing was approved");
|
|
104
|
-
return 1;
|
|
105
|
-
}
|
|
106
|
-
const say = input.json === true ? (line) => console.error(line) : (line) => console.log(line);
|
|
107
|
-
say("Approve this browser?");
|
|
108
|
-
say("");
|
|
109
|
-
say(` ${browser}`);
|
|
110
|
-
say(` Check that your browser shows ${check}.`);
|
|
111
|
-
say(personName === "" ? " For: the person who asked on that browser" : ` For: ${personName}, to ask this computer's agents`);
|
|
112
|
-
say("");
|
|
113
|
-
if (!await deps.confirm("Type y to approve, anything else to cancel: ")) {
|
|
114
|
-
console.error("Nothing was approved. The code is still waiting until it expires.");
|
|
115
|
-
return 1;
|
|
116
|
-
}
|
|
117
|
-
const trusted = await deps.trust({
|
|
118
|
-
action: "add",
|
|
119
|
-
name: browser,
|
|
120
|
-
publicKey,
|
|
121
|
-
quiet: true,
|
|
122
|
-
...personName === "" ? {} : { personName },
|
|
123
|
-
...input.host === undefined ? {} : { host: input.host },
|
|
124
|
-
...input.configDir === undefined ? {} : { configDir: input.configDir }
|
|
125
|
-
});
|
|
126
|
-
if (trusted !== 0) {
|
|
127
|
-
console.error("Nothing was approved on the browser side; the code is still waiting.");
|
|
128
|
-
return 1;
|
|
129
|
-
}
|
|
130
|
-
const spendUrl = `${readUrl}/approved`;
|
|
131
|
-
let spent;
|
|
132
|
-
try {
|
|
133
|
-
spent = await deps.fetch(spendUrl, {
|
|
134
|
-
method: "POST",
|
|
135
|
-
headers: { "content-type": "application/json" },
|
|
136
|
-
body: JSON.stringify({ keyFingerprint: keyId.keyId }),
|
|
137
|
-
redirect: "error"
|
|
138
|
-
});
|
|
139
|
-
} catch (error) {
|
|
140
|
-
console.error(`APPROVE_NOT_MARKED: this computer now trusts ${browser}, but ${origin} could not be told (${error.message}). The browser keeps waiting; press "Approve this browser" again for a new code.`);
|
|
141
|
-
return 1;
|
|
142
|
-
}
|
|
143
|
-
if (spent.status !== 204) {
|
|
144
|
-
console.error(`APPROVE_NOT_MARKED: this computer now trusts ${browser}, but the deployment did not mark the code spent (${refusalLine(await jsonBody(spent), spent.status)}). The browser keeps waiting; press "Approve this browser" again for a new code.`);
|
|
145
|
-
return 1;
|
|
146
|
-
}
|
|
147
|
-
if (input.json === true) {
|
|
148
|
-
console.log(JSON.stringify({ approved: true, browser, keyId: keyId.keyId, personName: personName === "" ? null : personName, expiresAt }));
|
|
149
|
-
return 0;
|
|
150
|
-
}
|
|
151
|
-
console.log(`Approved. ${browser} can ask this computer's agents from now on.`);
|
|
152
|
-
console.log(`Remove it later: hightea browsers remove "${browser}"`);
|
|
153
|
-
return 0;
|
|
154
|
-
}
|
|
155
|
-
export {
|
|
156
|
-
hostApprove
|
|
157
|
-
};
|
package/dist/chunk-ph04jess.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
hostJoin2,
|
|
3
|
-
hostStatus2,
|
|
4
|
-
localHost2,
|
|
5
|
-
runningAhpds2,
|
|
6
|
-
hostServicesGrants2,
|
|
7
|
-
hostUpdate2
|
|
8
|
-
} from "./chunk-dvt4hb8z.js";
|
|
9
|
-
import"./chunk-dvnzt54t.js";
|
|
10
|
-
export {
|
|
11
|
-
hostJoin2 as hostJoin,
|
|
12
|
-
hostServicesGrants2 as hostServicesGrants,
|
|
13
|
-
hostStatus2 as hostStatus,
|
|
14
|
-
hostUpdate2 as hostUpdate,
|
|
15
|
-
localHost2 as localHost,
|
|
16
|
-
runningAhpds2 as runningAhpds
|
|
17
|
-
};
|
package/dist/chunk-sbqmqp8e.js
DELETED
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
checkedOrigin2,
|
|
3
|
-
ensureCredential2
|
|
4
|
-
} from "./chunk-0ab6h39c.js";
|
|
5
|
-
|
|
6
|
-
// src/owner-session.ts
|
|
7
|
-
import { chmod, mkdir, readFile, rm, stat, writeFile } from "node:fs/promises";
|
|
8
|
-
import { homedir } from "node:os";
|
|
9
|
-
import { join } from "node:path";
|
|
10
|
-
var NOT_OWNER = new Set(["FORBIDDEN", "NOT_A_MEMBER", "UNAUTHENTICATED"]);
|
|
11
|
-
function ownerIo() {
|
|
12
|
-
return {
|
|
13
|
-
fetch: (url, init) => globalThis.fetch(url, init),
|
|
14
|
-
readFile: (path) => readFile(path, "utf8"),
|
|
15
|
-
stat: (path) => stat(path),
|
|
16
|
-
writeFile: (path, data, options) => writeFile(path, data, options),
|
|
17
|
-
mkdir: async (path, options) => {
|
|
18
|
-
await mkdir(path, options);
|
|
19
|
-
},
|
|
20
|
-
chmod: (path, mode) => chmod(path, mode),
|
|
21
|
-
rm: (path, options) => rm(path, options),
|
|
22
|
-
env: process.env,
|
|
23
|
-
home: homedir(),
|
|
24
|
-
nowMs: () => Date.now(),
|
|
25
|
-
out: (line) => {
|
|
26
|
-
process.stdout.write(`${line}
|
|
27
|
-
`);
|
|
28
|
-
},
|
|
29
|
-
err: (line) => {
|
|
30
|
-
process.stderr.write(`${line}
|
|
31
|
-
`);
|
|
32
|
-
}
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
async function readSession(target, io, label, origin) {
|
|
36
|
-
const explicit = target.sessionFile?.trim() || io.env.HIGHTEA_SESSION_FILE?.trim();
|
|
37
|
-
if (explicit === undefined || explicit === "") {
|
|
38
|
-
const stored = await ensureCredential2(origin, io);
|
|
39
|
-
if ("credential" in stored)
|
|
40
|
-
return { cookie: stored.credential.sessionToken };
|
|
41
|
-
if (stored.error.startsWith("AUTH_NOT_SIGNED_IN")) {
|
|
42
|
-
const legacy = join(io.env.XDG_CONFIG_HOME?.trim() || join(io.home, ".config"), "hightea", "session");
|
|
43
|
-
const copied = await readSessionFile(legacy, io, label);
|
|
44
|
-
if (!("error" in copied))
|
|
45
|
-
return copied;
|
|
46
|
-
return { error: `${label}_SESSION_MISSING: ${stored.error}` };
|
|
47
|
-
}
|
|
48
|
-
if (stored.error.startsWith("AUTH_CREDENTIALS_EXPOSED"))
|
|
49
|
-
return { error: `${label}_SESSION_EXPOSED: ${stored.error}` };
|
|
50
|
-
if (stored.error.startsWith("AUTH_SESSION_EXPIRED") || stored.error.startsWith("AUTH_REFRESH")) {
|
|
51
|
-
return { error: `${label}_SESSION_EXPIRED: ${stored.error}` };
|
|
52
|
-
}
|
|
53
|
-
return { error: `${label}_SESSION_INVALID: ${stored.error}` };
|
|
54
|
-
}
|
|
55
|
-
return await readSessionFile(explicit, io, label);
|
|
56
|
-
}
|
|
57
|
-
async function readSessionFile(file, io, label) {
|
|
58
|
-
let raw;
|
|
59
|
-
let mode;
|
|
60
|
-
try {
|
|
61
|
-
raw = await io.readFile(file);
|
|
62
|
-
mode = (await io.stat(file)).mode;
|
|
63
|
-
} catch (error) {
|
|
64
|
-
return { error: `${label}_SESSION_MISSING: no session in ${file} (${error.message}). Sign in to the deployment in a browser and write its ht_session cookie to that file, or pass --session-file.` };
|
|
65
|
-
}
|
|
66
|
-
if ((mode & 63) !== 0) {
|
|
67
|
-
return { error: `${label}_SESSION_EXPOSED: ${file} is mode 0${(mode & 511).toString(8).padStart(3, "0")}; run chmod 600 ${file}.` };
|
|
68
|
-
}
|
|
69
|
-
const lines = raw.split(`
|
|
70
|
-
`).map((line) => line.trim()).filter((line) => line.length > 0);
|
|
71
|
-
if (lines.length !== 1) {
|
|
72
|
-
return { error: `${label}_SESSION_INVALID: ${file} must hold exactly one non-empty line (the ht_session cookie value); it holds ${lines.length}.` };
|
|
73
|
-
}
|
|
74
|
-
return { cookie: lines[0] };
|
|
75
|
-
}
|
|
76
|
-
async function sendOwner(target, io, request) {
|
|
77
|
-
const where = checkedOrigin2(target.origin, request.label);
|
|
78
|
-
if ("error" in where)
|
|
79
|
-
return { exit: 1, error: where.error };
|
|
80
|
-
const session = await readSession(target, io, request.label, where.origin);
|
|
81
|
-
if ("error" in session)
|
|
82
|
-
return { exit: 1, error: session.error };
|
|
83
|
-
const url = `${where.origin}/api/ws/${encodeURIComponent(target.workspace)}${request.path}`;
|
|
84
|
-
let response;
|
|
85
|
-
try {
|
|
86
|
-
response = await io.fetch(url, {
|
|
87
|
-
method: request.method,
|
|
88
|
-
headers: {
|
|
89
|
-
cookie: `ht_session=${session.cookie}`,
|
|
90
|
-
origin: where.origin,
|
|
91
|
-
accept: "application/json",
|
|
92
|
-
"cache-control": "no-store",
|
|
93
|
-
...request.body === undefined ? {} : { "content-type": "application/json" }
|
|
94
|
-
},
|
|
95
|
-
...request.body === undefined ? {} : { body: JSON.stringify(request.body) }
|
|
96
|
-
});
|
|
97
|
-
} catch (error) {
|
|
98
|
-
return { exit: 1, error: `${request.label}_UNREACHABLE: ${request.what} ${request.method} ${url} did not answer: ${error.message}` };
|
|
99
|
-
}
|
|
100
|
-
const text = await response.text();
|
|
101
|
-
let answer = {};
|
|
102
|
-
if (text.length > 0) {
|
|
103
|
-
try {
|
|
104
|
-
const parsed = JSON.parse(text);
|
|
105
|
-
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed))
|
|
106
|
-
throw new SyntaxError("not a JSON object");
|
|
107
|
-
answer = parsed;
|
|
108
|
-
} catch (error) {
|
|
109
|
-
if (!(error instanceof SyntaxError))
|
|
110
|
-
throw error;
|
|
111
|
-
return { exit: 1, error: `${request.label}_ANSWER_INVALID: ${request.what} ${request.method} ${url} answered HTTP ${response.status} with non-JSON: ${text.slice(0, 300)}` };
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
if (response.ok)
|
|
115
|
-
return { answer };
|
|
116
|
-
const refusal = answer.error;
|
|
117
|
-
const code = typeof refusal?.code === "string" ? refusal.code : `HTTP_${response.status}`;
|
|
118
|
-
const message = typeof refusal?.message === "string" ? refusal.message : text.slice(0, 300);
|
|
119
|
-
const exit = NOT_OWNER.has(code) ? 3 : request.extraExits?.[code] ?? 1;
|
|
120
|
-
return { exit, error: `${code}: ${message}`, body: answer };
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
export { ownerIo, sendOwner };
|
package/dist/chunk-w0ebwtch.js
DELETED
|
@@ -1,161 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
TRUSTED_KEY_NAME,
|
|
3
|
-
trustedKeyId,
|
|
4
|
-
readTrustedKeys,
|
|
5
|
-
localHost2,
|
|
6
|
-
runningAhpds2
|
|
7
|
-
} from "./chunk-dvt4hb8z.js";
|
|
8
|
-
|
|
9
|
-
// ../ahp-host/src/trust.ts
|
|
10
|
-
import { writeJsonAtomic } from "@ahpd/sdk";
|
|
11
|
-
import { existsSync } from "node:fs";
|
|
12
|
-
import { isAbsolute, join, resolve } from "node:path";
|
|
13
|
-
async function configDirFor(input, found, deps) {
|
|
14
|
-
if (input.configDir !== undefined) {
|
|
15
|
-
const named = isAbsolute(input.configDir) ? resolve(input.configDir) : undefined;
|
|
16
|
-
if (named === undefined) {
|
|
17
|
-
return { error: `TRUST_CONFIG_DIR_INVALID: --config-dir needs an absolute path; ${JSON.stringify(input.configDir)} is not one` };
|
|
18
|
-
}
|
|
19
|
-
if (!existsSync(join(named, "config.json"))) {
|
|
20
|
-
return { error: `TRUST_CONFIG_DIR_INVALID: ${named} holds no config.json; name the directory holding this host's ahpd configuration (in a hab-managed host that is its xdg-config/ahpd)` };
|
|
21
|
-
}
|
|
22
|
-
return { configDir: named, ...named === found.configDir ? {} : { note: `Trusting in the configuration you named at ${named}; this machine's claim names ${found.configDir}.` } };
|
|
23
|
-
}
|
|
24
|
-
const mine = (await deps.runningAhpds()).filter((process) => process.hostIds.includes(found.hostId));
|
|
25
|
-
if (mine.length > 1) {
|
|
26
|
-
return { error: `TRUST_HOST_AMBIGUOUS: ${String(mine.length)} running ahpd processes serve ${found.hostId} (${mine.map((process) => `${String(process.pid)} at ${process.configDir}`).join(", ")}); stop the extras or name one with --config-dir` };
|
|
27
|
-
}
|
|
28
|
-
const running = mine[0];
|
|
29
|
-
if (running !== undefined) {
|
|
30
|
-
return running.configDir === found.configDir ? { configDir: running.configDir } : { configDir: running.configDir, note: `Using the running host's configuration at ${running.configDir} (pid ${String(running.pid)}); this machine's claim names ${found.configDir}, which that host does not read.` };
|
|
31
|
-
}
|
|
32
|
-
if (input.action === "list") {
|
|
33
|
-
return { configDir: found.configDir, note: `No ahpd process serving ${found.hostId} is running on this machine; reading the claim's copy at ${found.configDir}, which a hab-managed host would not read.` };
|
|
34
|
-
}
|
|
35
|
-
return { error: `TRUST_NO_RUNNING_HOST: no ahpd process serving ${found.hostId} is running on this machine, so there is no configuration to change; start the host, or name its configuration with --config-dir <dir> (this machine's claim names ${found.configDir})` };
|
|
36
|
-
}
|
|
37
|
-
var TRUST_MEANS = "A trusted browser key lets posts made in that browser ask this machine's agents; a trusted host key " + "lets that machine's agents ask them too.";
|
|
38
|
-
async function hostTrust2(input, now = () => new Date, deps = { runningAhpds: () => runningAhpds2() }) {
|
|
39
|
-
const found = await localHost2(input.host);
|
|
40
|
-
if ("error" in found) {
|
|
41
|
-
console.error(found.error.replace(/^SERVICE_/u, "TRUST_"));
|
|
42
|
-
return 1;
|
|
43
|
-
}
|
|
44
|
-
const target = await configDirFor(input, found, deps);
|
|
45
|
-
if ("error" in target) {
|
|
46
|
-
console.error(target.error);
|
|
47
|
-
return 1;
|
|
48
|
-
}
|
|
49
|
-
let current;
|
|
50
|
-
try {
|
|
51
|
-
current = readTrustedKeys(target.configDir);
|
|
52
|
-
} catch (error) {
|
|
53
|
-
console.error(`${error.message}; it was left unchanged`);
|
|
54
|
-
return 1;
|
|
55
|
-
}
|
|
56
|
-
const keys = current.state === "read" ? current.keys : [];
|
|
57
|
-
const where = `host ${found.hostId} (${current.file})`;
|
|
58
|
-
if (target.note !== undefined)
|
|
59
|
-
console.error(target.note);
|
|
60
|
-
if (input.action === "list") {
|
|
61
|
-
if (input.json === true) {
|
|
62
|
-
console.log(JSON.stringify({ hostId: found.hostId, file: current.file, keys }));
|
|
63
|
-
return 0;
|
|
64
|
-
}
|
|
65
|
-
if (input.browsers === true) {
|
|
66
|
-
if (keys.length === 0) {
|
|
67
|
-
console.log(`No browsers are approved on ${where}. A browser's own page shows the code when someone presses` + ' "Approve this browser"; approve it here with `hightea approve <code>`.');
|
|
68
|
-
return 0;
|
|
69
|
-
}
|
|
70
|
-
console.log(`Browsers approved on ${where}:`);
|
|
71
|
-
for (const key of keys) {
|
|
72
|
-
console.log(` ${key.name.padEnd(22)} approved ${browserDate(key.addedAt)}${key.personName === undefined ? "" : ` ${key.personName}`}`);
|
|
73
|
-
}
|
|
74
|
-
return 0;
|
|
75
|
-
}
|
|
76
|
-
if (keys.length === 0) {
|
|
77
|
-
console.log(`No trusted keys for ${where}: room asks to this machine's agents are refused until you add one with` + " `hightea host trust add <name> <public key>`.");
|
|
78
|
-
return 0;
|
|
79
|
-
}
|
|
80
|
-
console.log(`Trusted keys for ${where}:`);
|
|
81
|
-
for (const key of keys)
|
|
82
|
-
console.log(` ${key.name} key ${key.keyId} added ${key.addedAt}`);
|
|
83
|
-
console.log(TRUST_MEANS);
|
|
84
|
-
return 0;
|
|
85
|
-
}
|
|
86
|
-
if (!TRUSTED_KEY_NAME.test(input.name)) {
|
|
87
|
-
console.error("TRUST_NAME_INVALID: a name is 1 to 64 letters, digits, '.', '_' or '-' (single spaces are allowed between them), starting with a letter or digit; nothing was changed");
|
|
88
|
-
return 1;
|
|
89
|
-
}
|
|
90
|
-
const named = keys.find((key) => key.name === input.name);
|
|
91
|
-
if (input.action === "remove") {
|
|
92
|
-
if (named === undefined) {
|
|
93
|
-
console.error(`TRUST_NAME_UNKNOWN: ${where} trusts no key named ${input.name} (trusted: ${keys.map((key) => key.name).join(", ") || "none"}); nothing was changed`);
|
|
94
|
-
return 1;
|
|
95
|
-
}
|
|
96
|
-
if (!write(current.file, keys.filter((key) => key !== named)))
|
|
97
|
-
return 1;
|
|
98
|
-
if (input.browsers === true) {
|
|
99
|
-
console.log(`Removed. The next message from ${named.name} will ask again.`);
|
|
100
|
-
return 0;
|
|
101
|
-
}
|
|
102
|
-
console.log(`Removed ${input.name} (key ${named.keyId}) from ${where}. Asks signed with it are refused from now on.`);
|
|
103
|
-
return 0;
|
|
104
|
-
}
|
|
105
|
-
const id = trustedKeyId(input.publicKey);
|
|
106
|
-
if ("error" in id) {
|
|
107
|
-
console.error(`${id.error}; nothing was changed`);
|
|
108
|
-
return 1;
|
|
109
|
-
}
|
|
110
|
-
const announce = (line) => {
|
|
111
|
-
if (input.quiet !== true)
|
|
112
|
-
console.log(line);
|
|
113
|
-
};
|
|
114
|
-
if (named !== undefined) {
|
|
115
|
-
if (named.publicKey === input.publicKey) {
|
|
116
|
-
announce(`${input.name} (key ${id.keyId}) is already trusted by ${where}; it was left unchanged.`);
|
|
117
|
-
return 0;
|
|
118
|
-
}
|
|
119
|
-
console.error(`TRUST_NAME_TAKEN: ${where} already trusts another key as ${input.name}; remove it first or pick another name. Nothing was changed`);
|
|
120
|
-
return 1;
|
|
121
|
-
}
|
|
122
|
-
const holder = keys.find((key) => key.keyId === id.keyId);
|
|
123
|
-
if (holder !== undefined) {
|
|
124
|
-
console.error(`TRUST_KEY_TAKEN: ${where} already trusts this key as ${holder.name}; nothing was changed`);
|
|
125
|
-
return 1;
|
|
126
|
-
}
|
|
127
|
-
const added = {
|
|
128
|
-
name: input.name,
|
|
129
|
-
keyId: id.keyId,
|
|
130
|
-
publicKey: input.publicKey,
|
|
131
|
-
addedAt: now().toISOString(),
|
|
132
|
-
...input.personName === undefined || input.personName === "" ? {} : { personName: input.personName }
|
|
133
|
-
};
|
|
134
|
-
if (!write(current.file, [...keys, added]))
|
|
135
|
-
return 1;
|
|
136
|
-
announce(`Trusted ${input.name} (key ${id.keyId}) on ${where}. ${TRUST_MEANS}`);
|
|
137
|
-
return 0;
|
|
138
|
-
}
|
|
139
|
-
function browserDate(iso) {
|
|
140
|
-
const at = new Date(iso);
|
|
141
|
-
if (Number.isNaN(at.getTime()))
|
|
142
|
-
return iso;
|
|
143
|
-
return new Intl.DateTimeFormat("sv-SE", {
|
|
144
|
-
year: "numeric",
|
|
145
|
-
month: "2-digit",
|
|
146
|
-
day: "2-digit",
|
|
147
|
-
hour: "2-digit",
|
|
148
|
-
minute: "2-digit"
|
|
149
|
-
}).format(at);
|
|
150
|
-
}
|
|
151
|
-
function write(file, keys) {
|
|
152
|
-
try {
|
|
153
|
-
writeJsonAtomic(file, { v: 1, keys }, { mode: 384 });
|
|
154
|
-
} catch (error) {
|
|
155
|
-
console.error(`TRUST_FILE_UNWRITABLE: ${file}: ${error.message}`);
|
|
156
|
-
return false;
|
|
157
|
-
}
|
|
158
|
-
return true;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
export { hostTrust2 };
|