@botiverse/stamp-cli 0.3.14 → 0.4.0
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 +21 -0
- package/bin/launch.js +124 -0
- package/bin/stamp-staging.js +3 -0
- package/bin/stamp.js +3 -0
- package/package.json +16 -13
- package/README.md +0 -225
- package/bin/stamp-staging.mjs +0 -15
- package/bin/stamp.mjs +0 -65
- package/lib/agent-login.mjs +0 -208
- package/lib/agent-refresh.mjs +0 -192
- package/lib/auth.mjs +0 -202
- package/lib/client.mjs +0 -92
- package/lib/commands/changelog.mjs +0 -58
- package/lib/commands/identity.mjs +0 -12
- package/lib/commands/key.mjs +0 -104
- package/lib/commands/knownhosts.mjs +0 -150
- package/lib/commands/pr.mjs +0 -298
- package/lib/commands/queue.mjs +0 -67
- package/lib/commands/receipt.mjs +0 -33
- package/lib/commands/repo.mjs +0 -130
- package/lib/commands/search.mjs +0 -63
- package/lib/commands/session.mjs +0 -71
- package/lib/commands/subscribe.mjs +0 -35
- package/lib/errors.mjs +0 -47
- package/lib/expiry.mjs +0 -27
- package/lib/flags.mjs +0 -26
- package/lib/locale.mjs +0 -385
- package/lib/main.mjs +0 -124
- package/lib/pr/close.mjs +0 -56
- package/lib/pr/comment.mjs +0 -45
- package/lib/pr/create.mjs +0 -165
- package/lib/pr/edit.mjs +0 -54
- package/lib/pr/queue.mjs +0 -32
- package/lib/pr/read.mjs +0 -473
- package/lib/pr/review.mjs +0 -88
- package/lib/pr/verdict.mjs +0 -66
- package/lib/pr/write.mjs +0 -180
- package/lib/queue/setup.mjs +0 -143
- package/lib/receipt/list.mjs +0 -54
- package/lib/repo/clone.mjs +0 -101
- package/lib/repo/identities.mjs +0 -45
- package/lib/repo/ssh.mjs +0 -326
- package/lib/update-check.mjs +0 -50
package/lib/client.mjs
DELETED
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
import { AgentError } from "./errors.mjs";
|
|
2
|
-
|
|
3
|
-
// NetworkError is an AgentError about the transport, not about the request. A
|
|
4
|
-
// WRITE that fails this way has an UNKNOWN outcome (the request may have
|
|
5
|
-
// reached the gateway), so the write commands must keep their "outcome is
|
|
6
|
-
// unknown, do not resend" contract instead of inviting a retry.
|
|
7
|
-
export class NetworkError extends AgentError {}
|
|
8
|
-
|
|
9
|
-
export class ApiError extends Error {
|
|
10
|
-
constructor(status, body, text) {
|
|
11
|
-
super(`${status}: ${body?.error ?? (text ?? "").slice(0, 200)}`);
|
|
12
|
-
this.status = status;
|
|
13
|
-
this.body = body;
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
// A connection-level failure (never an HTTP status) arrives from undici as
|
|
18
|
-
// TypeError("fetch failed") with the real reason on `.cause`. The CLI used to
|
|
19
|
-
// hand that bare string to the operator; task #476 showed a first `pr list` on a
|
|
20
|
-
// cold instance failing that way while a rerun worked, because nothing retried
|
|
21
|
-
// the one attempt and nothing said what to try next. Text and JSON output share
|
|
22
|
-
// this client — the failure happens before either formatter runs — so the fix
|
|
23
|
-
// belongs here, not in one output path.
|
|
24
|
-
const RETRYABLE_NETWORK_CODES = new Set([
|
|
25
|
-
"ECONNRESET", "ECONNREFUSED", "ETIMEDOUT", "EPIPE", "EAI_AGAIN",
|
|
26
|
-
"UND_ERR_CONNECT_TIMEOUT", "UND_ERR_SOCKET", "UND_ERR_HEADERS_TIMEOUT", "UND_ERR_BODY_TIMEOUT",
|
|
27
|
-
]);
|
|
28
|
-
const DNS_NETWORK_CODES = new Set(["ENOTFOUND", "EAI_AGAIN"]);
|
|
29
|
-
|
|
30
|
-
export function networkFailureOf(error) {
|
|
31
|
-
const code = String(error?.cause?.code ?? error?.code ?? "");
|
|
32
|
-
const message = String(error?.cause?.message ?? error?.message ?? "");
|
|
33
|
-
if (DNS_NETWORK_CODES.has(code)) return { code, retryable: true };
|
|
34
|
-
if (RETRYABLE_NETWORK_CODES.has(code)) return { code, retryable: true };
|
|
35
|
-
if (code || /fetch failed/i.test(message)) {
|
|
36
|
-
// Unknown transport failure: report it, but do not spin on it.
|
|
37
|
-
return { code: code || message || "fetch failed", retryable: false };
|
|
38
|
-
}
|
|
39
|
-
return null;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
function networkErrorKey(failure) {
|
|
43
|
-
return DNS_NETWORK_CODES.has(failure.code) ? "networkHostUnresolved" : "networkUnreachable";
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export function makeClient(auth, fetchImpl = fetch, options = {}) {
|
|
47
|
-
// Reads are idempotent, so a bounded retry is safe; writes are not retried
|
|
48
|
-
// here (they carry their own idempotency key and a retry decision).
|
|
49
|
-
const readRetries = Number.isInteger(options.readRetries) ? Math.max(0, options.readRetries) : 2;
|
|
50
|
-
const retryDelayMs = Number.isInteger(options.retryDelayMs) ? Math.max(0, options.retryDelayMs) : 250;
|
|
51
|
-
const sleep = options.sleep ?? ((ms) => new Promise((resolve) => setTimeout(resolve, ms)));
|
|
52
|
-
|
|
53
|
-
async function once(method, path, body, token) {
|
|
54
|
-
const attempts = method === "GET" ? readRetries + 1 : 1;
|
|
55
|
-
for (let attempt = 1; ; attempt++) {
|
|
56
|
-
let res;
|
|
57
|
-
try {
|
|
58
|
-
res = await fetchImpl(`${auth.apiBase}${path}`, {
|
|
59
|
-
method,
|
|
60
|
-
headers: {
|
|
61
|
-
authorization: `Bearer ${token}`, "content-type": "application/json", accept: "application/json",
|
|
62
|
-
...(options.cliVersion ? { "stamp-cli-version": options.cliVersion } : {}),
|
|
63
|
-
},
|
|
64
|
-
body: body ? JSON.stringify(body) : undefined,
|
|
65
|
-
});
|
|
66
|
-
} catch (error) {
|
|
67
|
-
const failure = networkFailureOf(error);
|
|
68
|
-
if (!failure) throw error;
|
|
69
|
-
if (failure.retryable && attempt < attempts) {
|
|
70
|
-
await sleep(retryDelayMs * attempt);
|
|
71
|
-
continue;
|
|
72
|
-
}
|
|
73
|
-
throw new NetworkError(networkErrorKey(failure), { base: auth.apiBase, cause: failure.code });
|
|
74
|
-
}
|
|
75
|
-
options.onVersions?.(res.headers?.get?.("stamp-cli-latest") ?? "", res.headers?.get?.("stamp-cli-minimum") ?? "");
|
|
76
|
-
const text = await res.text();
|
|
77
|
-
let json = null;
|
|
78
|
-
try { json = text ? JSON.parse(text) : null; } catch { /* non-JSON error page */ }
|
|
79
|
-
return { res, json, text };
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
return async function api(method, path, body) {
|
|
83
|
-
const used = await auth.token();
|
|
84
|
-
let { res, json, text } = await once(method, path, body, used);
|
|
85
|
-
if (res.status === 401 && auth.mode === "agent") {
|
|
86
|
-
const next = await auth.retryToken(used);
|
|
87
|
-
if (next) ({ res, json, text } = await once(method, path, body, next));
|
|
88
|
-
}
|
|
89
|
-
if (!res.ok) throw new ApiError(res.status, json, text);
|
|
90
|
-
return json;
|
|
91
|
-
};
|
|
92
|
-
}
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import { t } from "../locale.mjs";
|
|
2
|
-
|
|
3
|
-
// `stamp changelog [--limit N]`: the gateway's release history (one release
|
|
4
|
-
// per staging-v tag with the pull requests it carried), read from
|
|
5
|
-
// GET /changelog.json. The build block says which image answered; the running
|
|
6
|
-
// release is the one whose tag equals build.version (the same source commit
|
|
7
|
-
// can be cut as two tags, so the SHA cannot tell them apart).
|
|
8
|
-
//
|
|
9
|
-
// Audience: by default only entries whose `audience` includes "agent" (the
|
|
10
|
-
// CLI and the HTTP/SSH surface an agent drives) — artin: the human view and
|
|
11
|
-
// the agent view differ by content. `--all` shows everything; the JSON says
|
|
12
|
-
// which view it is (`view.audience`) so a reader never mistakes a filtered
|
|
13
|
-
// document for the whole history.
|
|
14
|
-
const AUDIENCE = "agent";
|
|
15
|
-
|
|
16
|
-
export function visibleEntries(entries, all) {
|
|
17
|
-
const list = Array.isArray(entries) ? entries : [];
|
|
18
|
-
if (all) return { entries: list, hidden: 0 };
|
|
19
|
-
const kept = list.filter((e) => Array.isArray(e?.audience) && e.audience.includes(AUDIENCE));
|
|
20
|
-
return { entries: kept, hidden: list.length - kept.length };
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export async function run(ctx) {
|
|
24
|
-
let limit = Infinity;
|
|
25
|
-
let all = false;
|
|
26
|
-
const rest = [];
|
|
27
|
-
for (let i = 0; i < ctx.args.length; i++) {
|
|
28
|
-
const arg = ctx.args[i];
|
|
29
|
-
if (arg === "--limit") {
|
|
30
|
-
const value = Number(ctx.args[++i]);
|
|
31
|
-
if (!Number.isInteger(value) || value < 1) throw new Error(t(ctx.locale, "usageChangelog"));
|
|
32
|
-
limit = value;
|
|
33
|
-
} else if (arg === "--all") all = true;
|
|
34
|
-
else rest.push(arg);
|
|
35
|
-
}
|
|
36
|
-
if (rest.length) throw new Error(t(ctx.locale, "usageChangelog"));
|
|
37
|
-
const d = await ctx.api("GET", "/changelog.json");
|
|
38
|
-
const releases = (Array.isArray(d.releases) ? d.releases.slice(0, limit) : []).map((r) => {
|
|
39
|
-
const { entries, hidden } = visibleEntries(r.entries, all);
|
|
40
|
-
return { ...r, entries, hidden };
|
|
41
|
-
});
|
|
42
|
-
ctx.emit({ ...d, view: { audience: all ? "all" : AUDIENCE }, releases }, () => {
|
|
43
|
-
ctx.out(t(ctx.locale, "changelogBuild", { version: d.build?.version ?? "-", commit: String(d.build?.commit ?? "").slice(0, 8) || "-" }));
|
|
44
|
-
ctx.out(t(ctx.locale, all ? "changelogScopeAll" : "changelogScopeAgent"));
|
|
45
|
-
if (!releases.length) { ctx.out(t(ctx.locale, "changelogEmpty")); return; }
|
|
46
|
-
for (const r of releases) {
|
|
47
|
-
const current = r.tag && r.tag === d.build?.version ? ` ${t(ctx.locale, "changelogCurrentMark")}` : "";
|
|
48
|
-
ctx.out(`${r.tag} (${String(r.source ?? "").slice(0, 8)})${current}`);
|
|
49
|
-
if (!r.entries.length) {
|
|
50
|
-
ctx.out(` ${t(ctx.locale, r.hidden ? "changelogNoneForAgents" : "changelogNoEntries", { hidden: r.hidden })}`);
|
|
51
|
-
continue;
|
|
52
|
-
}
|
|
53
|
-
for (const e of r.entries) ctx.out(` #${e.number} ${String(e.title ?? "").replace(/\s+/g, " ")}${e.author ? ` (${e.author})` : ""}`);
|
|
54
|
-
if (r.hidden) ctx.out(` ${t(ctx.locale, "changelogHiddenCount", { hidden: r.hidden })}`);
|
|
55
|
-
}
|
|
56
|
-
});
|
|
57
|
-
return ctx.exit(0);
|
|
58
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { t } from "../locale.mjs";
|
|
2
|
-
import { takeFlag } from "../flags.mjs";
|
|
3
|
-
|
|
4
|
-
export async function run(ctx) {
|
|
5
|
-
const sub = ctx.args.shift();
|
|
6
|
-
if (sub !== "bind") throw new Error(t(ctx.locale, "usageIdentityBind"));
|
|
7
|
-
const agentEmail = takeFlag(ctx.args, "email");
|
|
8
|
-
if (!agentEmail) throw new Error(t(ctx.locale, "usageIdentityBind"));
|
|
9
|
-
const d = await ctx.api("POST", "/v1/identities", { agentEmail });
|
|
10
|
-
ctx.emit(d, () => ctx.out(t(ctx.locale, "bound", { email: d.agentEmail })));
|
|
11
|
-
return ctx.exit(0);
|
|
12
|
-
}
|
package/lib/commands/key.mjs
DELETED
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
import { existsSync } from "node:fs";
|
|
2
|
-
import { t } from "../locale.mjs";
|
|
3
|
-
import { resolve } from "node:path";
|
|
4
|
-
import { takeFlag, readPublicKey } from "../flags.mjs";
|
|
5
|
-
import { readRecordedIdentity, recordIdentity } from "../repo/identities.mjs";
|
|
6
|
-
import { defaultIdentityPath } from "../repo/ssh.mjs";
|
|
7
|
-
import { mkdirSync } from "node:fs";
|
|
8
|
-
import { dirname } from "node:path";
|
|
9
|
-
import { spawnSync } from "node:child_process";
|
|
10
|
-
|
|
11
|
-
// generateAgentKey creates an ed25519 key with no passphrase at path (0600,
|
|
12
|
-
// in a 0700 directory), labelled with the agent it belongs to.
|
|
13
|
-
function generateAgentKey(path, agentId) {
|
|
14
|
-
mkdirSync(dirname(path), { recursive: true, mode: 0o700 });
|
|
15
|
-
const res = spawnSync("ssh-keygen", ["-q", "-t", "ed25519", "-N", "", "-C", `stamp-agent-${agentId ?? "unknown"}`, "-f", path], { encoding: "utf8" });
|
|
16
|
-
if (res.error || res.status !== 0) {
|
|
17
|
-
throw new Error(`ssh-keygen failed: ${(res.stderr || res.error?.message || "").trim()}`);
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
// ensureAgentKey gives an agent its own SSH key without being asked, as its
|
|
22
|
-
// commit email is (artin 09-28: "enroll should only be a hint"): when the
|
|
23
|
-
// agent has no key recorded for this instance, it creates one in the agent's
|
|
24
|
-
// own directory (or reuses the one already there), enrolls it and records it.
|
|
25
|
-
// It returns what it did, or null when there was nothing to do (a person, or
|
|
26
|
-
// a key already recorded). `key enroll --public-key` stays for a key the
|
|
27
|
-
// agent chooses itself.
|
|
28
|
-
export async function ensureAgentKey(ctx) {
|
|
29
|
-
if (!ctx.env.STAMP_AGENT_STATE_DIR) return null;
|
|
30
|
-
if (readRecordedIdentity(ctx.env, ctx.auth.apiBase)) return null;
|
|
31
|
-
const privatePath = defaultIdentityPath(ctx.env);
|
|
32
|
-
const publicPath = `${privatePath}.pub`;
|
|
33
|
-
let created = false;
|
|
34
|
-
if (!existsSync(publicPath)) {
|
|
35
|
-
await (ctx.opts.sshKeygenImpl ?? generateAgentKey)(privatePath, ctx.auth?.agentEnv?.agentId);
|
|
36
|
-
created = true;
|
|
37
|
-
}
|
|
38
|
-
const d = await ctx.api("POST", "/v1/keys", { publicKey: readPublicKey(publicPath) });
|
|
39
|
-
recordIdentity(ctx.env, ctx.auth.apiBase, {
|
|
40
|
-
identityPath: privatePath, fingerprint: d.fingerprint ?? null, keyId: d.keyId ?? null, enrolledAt: new Date().toISOString(),
|
|
41
|
-
});
|
|
42
|
-
return { path: privatePath, fingerprint: d.fingerprint ?? "", created };
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export async function run(ctx) {
|
|
46
|
-
const sub = ctx.args.shift();
|
|
47
|
-
if (sub === "list") {
|
|
48
|
-
if (ctx.args.length !== 0) throw new Error(t(ctx.locale, "usageKeyList"));
|
|
49
|
-
const d = await ctx.api("GET", "/v1/keys");
|
|
50
|
-
const items = Array.isArray(d?.items) ? d.items : [];
|
|
51
|
-
ctx.emit(d, () => {
|
|
52
|
-
if (items.length === 0) ctx.out(t(ctx.locale, "keyListEmpty"));
|
|
53
|
-
else for (const item of items) ctx.out(`${item.fingerprint}\t${item.keyId}`);
|
|
54
|
-
});
|
|
55
|
-
return ctx.exit(0);
|
|
56
|
-
}
|
|
57
|
-
if (sub === "revoke") {
|
|
58
|
-
const keyId = ctx.args.shift();
|
|
59
|
-
if (!keyId || ctx.args.length !== 0) throw new Error(t(ctx.locale, "usageKeyRevoke"));
|
|
60
|
-
const d = await ctx.api("DELETE", `/v1/keys/${encodeURIComponent(keyId)}`);
|
|
61
|
-
ctx.emit(d, () => ctx.out(t(ctx.locale, "keyRevoked", { keyId: d.keyId ?? keyId })));
|
|
62
|
-
return ctx.exit(0);
|
|
63
|
-
}
|
|
64
|
-
if (sub !== "enroll") throw new Error(t(ctx.locale, "usageKeyEnroll"));
|
|
65
|
-
const agentDefault = `${defaultIdentityPath(ctx.env)}.pub`;
|
|
66
|
-
const keyPath = takeFlag(ctx.args, "public-key", agentDefault);
|
|
67
|
-
// An agent enrolling with no --public-key gets a key of its own, created in
|
|
68
|
-
// its own directory (artin 09-27: a shared ~/.ssh/id_ed25519 made another
|
|
69
|
-
// agent's key push for BoBo). Nothing to configure by hand.
|
|
70
|
-
let generated = false;
|
|
71
|
-
if (keyPath === agentDefault && ctx.env.STAMP_AGENT_STATE_DIR && !existsSync(keyPath)) {
|
|
72
|
-
await (ctx.opts.sshKeygenImpl ?? generateAgentKey)(keyPath.replace(/\.pub$/, ""), ctx.auth?.agentEnv?.agentId);
|
|
73
|
-
generated = true;
|
|
74
|
-
}
|
|
75
|
-
if (keyPath !== "-" && !existsSync(keyPath)) {
|
|
76
|
-
// The default path is the interesting case: `key enroll` on a machine with
|
|
77
|
-
// no key reads a path the operator never typed, so name it and name the two
|
|
78
|
-
// ways out (task #480).
|
|
79
|
-
throw new Error(t(ctx.locale, "keyPublicKeyMissing", { path: keyPath, private: keyPath.replace(/\.pub$/i, "") }));
|
|
80
|
-
}
|
|
81
|
-
const publicKey = readPublicKey(keyPath);
|
|
82
|
-
const d = await ctx.api("POST", "/v1/keys", { publicKey });
|
|
83
|
-
// Task #514: remember which private key this enrollment belongs to, for this
|
|
84
|
-
// instance, so a later `repo clone` uses it without --identity. The private
|
|
85
|
-
// half is the public path without `.pub` — the only derivation, no search.
|
|
86
|
-
// A public key from stdin, or one whose private half is not here, records
|
|
87
|
-
// nothing and says so.
|
|
88
|
-
let identity = null;
|
|
89
|
-
if (keyPath !== "-") {
|
|
90
|
-
const privatePath = resolve(keyPath.replace(/\.pub$/i, ""));
|
|
91
|
-
if (privatePath !== resolve(keyPath) && existsSync(privatePath)) {
|
|
92
|
-
identity = { identityPath: privatePath, fingerprint: d.fingerprint ?? null, keyId: d.keyId ?? null, enrolledAt: new Date().toISOString() };
|
|
93
|
-
recordIdentity(ctx.env, ctx.auth.apiBase, identity);
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
ctx.emit({ ...d, identity, generated }, () => {
|
|
97
|
-
if (generated) ctx.out(t(ctx.locale, "keyGenerated", { path: keyPath.replace(/\.pub$/, "") }));
|
|
98
|
-
ctx.out(t(ctx.locale, "enrolled", { fingerprint: d.fingerprint, keyId: d.keyId }));
|
|
99
|
-
ctx.out(identity
|
|
100
|
-
? t(ctx.locale, "enrolledIdentityRecorded", { path: identity.identityPath })
|
|
101
|
-
: t(ctx.locale, "enrolledIdentityNotRecorded", { path: keyPath === "-" ? "-" : resolve(keyPath.replace(/\.pub$/i, "")) }));
|
|
102
|
-
});
|
|
103
|
-
return ctx.exit(0);
|
|
104
|
-
}
|
|
@@ -1,150 +0,0 @@
|
|
|
1
|
-
import { createHash } from "node:crypto";
|
|
2
|
-
import { t } from "../locale.mjs";
|
|
3
|
-
import { takeFlag } from "../flags.mjs";
|
|
4
|
-
import {
|
|
5
|
-
fetchPublishedKnownHosts,
|
|
6
|
-
knownHostsIsUsedBy,
|
|
7
|
-
probeRepoKnownHosts,
|
|
8
|
-
resolveIdentityPath,
|
|
9
|
-
resolveKnownHostsPath,
|
|
10
|
-
sshCommandFor,
|
|
11
|
-
writePinnedKnownHosts,
|
|
12
|
-
} from "../repo/ssh.mjs";
|
|
13
|
-
import { readRecordedIdentity } from "../repo/identities.mjs";
|
|
14
|
-
|
|
15
|
-
// The SSH fingerprint of a known_hosts line: base64(sha256(raw key)), which is
|
|
16
|
-
// what `ssh-keygen -lf` prints and what an announcement quotes. Printing it is
|
|
17
|
-
// the point of this command — an operator compares it to the published value
|
|
18
|
-
// rather than reading a base64 blob.
|
|
19
|
-
export function fingerprintOf(line) {
|
|
20
|
-
const base64 = line.trim().split(/\s+/)[2];
|
|
21
|
-
if (!base64) return null;
|
|
22
|
-
return `SHA256:${createHash("sha256").update(Buffer.from(base64, "base64")).digest("base64").replace(/=+$/, "")}`;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
// `known-hosts refresh` exists because rotating the gateway's host key
|
|
26
|
-
// otherwise has no legitimate client-side route: the fetch-and-pin pair lived
|
|
27
|
-
// only inside `repo clone`, so an agent that already had a clone could either
|
|
28
|
-
// clone again or disable host key checking. The second is what people actually
|
|
29
|
-
// do, and after a host key leak it is the worst available outcome (#592).
|
|
30
|
-
//
|
|
31
|
-
// The write only happens after a usable line is in hand, so a failed fetch
|
|
32
|
-
// leaves known_hosts byte-for-byte unchanged: a refresh that damages the file
|
|
33
|
-
// when the network is down would be worse than having no command.
|
|
34
|
-
export async function run(ctx) {
|
|
35
|
-
const sub = ctx.args.shift();
|
|
36
|
-
if (sub !== "refresh") throw new Error(t(ctx.locale, "usageKnownHostsRefresh"));
|
|
37
|
-
const knownHostsFlag = takeFlag(ctx.args, "known-hosts", "");
|
|
38
|
-
if (ctx.args.length !== 0) throw new Error(t(ctx.locale, "usageKnownHostsRefresh"));
|
|
39
|
-
|
|
40
|
-
const knownHostsPath = resolveKnownHostsPath(ctx.env, knownHostsFlag);
|
|
41
|
-
const fetchImpl = ctx.opts.fetchImpl ?? fetch;
|
|
42
|
-
const url = `${ctx.auth.apiBase}/.well-known/ssh-known-hosts`;
|
|
43
|
-
const published = await fetchPublishedKnownHosts({ apiBase: ctx.auth.apiBase, fetchImpl });
|
|
44
|
-
if (!published.ok) {
|
|
45
|
-
throw new Error(t(ctx.locale, published.invalid ? "cloneKnownHostsInvalid" : "cloneKnownHostsUnavailable", {
|
|
46
|
-
url,
|
|
47
|
-
status: published.status ?? "?",
|
|
48
|
-
}));
|
|
49
|
-
}
|
|
50
|
-
writePinnedKnownHosts(knownHostsPath, published.pattern, published.line);
|
|
51
|
-
const fingerprint = fingerprintOf(published.line);
|
|
52
|
-
|
|
53
|
-
// Writing the file is not the goal; the goal is that this repo's next push
|
|
54
|
-
// reads it. Those differ silently, and the difference only shows up on
|
|
55
|
-
// rotation day (#595), so the check belongs here rather than in a runbook.
|
|
56
|
-
//
|
|
57
|
-
// An explicit --known-hosts is exempt: it names one file on purpose, often a
|
|
58
|
-
// scratch one, and "this repo does not read it" is then a statement of the
|
|
59
|
-
// obvious that would break scripted use. STAMP_KNOWN_HOSTS is not exempt —
|
|
60
|
-
// that is where an agent says its canonical file lives, so it is exactly the
|
|
61
|
-
// case worth checking.
|
|
62
|
-
const probe = knownHostsFlag
|
|
63
|
-
// Skipped, not unknown-for-lack-of-a-repository. Carrying its own reason
|
|
64
|
-
// matters: the shared `{ known: false }` shape previously fell through to
|
|
65
|
-
// "not inside a git repository", which is a plain falsehood when the user
|
|
66
|
-
// ran this inside one (飞牛, reviewing #610).
|
|
67
|
-
? { known: false, skippedReason: "knownHostsUncheckedExplicitPath" }
|
|
68
|
-
: probeRepoKnownHosts({
|
|
69
|
-
cwd: ctx.opts.cwd ?? process.cwd(),
|
|
70
|
-
pattern: published.pattern,
|
|
71
|
-
execImpl: ctx.opts.sshProbeExec,
|
|
72
|
-
});
|
|
73
|
-
// Three states, not two. Treating "could not answer" as agreement was right
|
|
74
|
-
// about the exit code and wrong about the output: it made an unrunnable probe
|
|
75
|
-
// print exactly what success prints, so a reader who saw no complaint
|
|
76
|
-
// concluded their repository was aligned (#610, found by cross).
|
|
77
|
-
//
|
|
78
|
-
// That matters here more than anywhere: this command exists so nobody has to
|
|
79
|
-
// disable host key checking, and the person who runs it, sees a clean result
|
|
80
|
-
// and believes they are ready is precisely the person who cannot push on
|
|
81
|
-
// rotation day and reaches for StrictHostKeyChecking=no.
|
|
82
|
-
//
|
|
83
|
-
// The exit code still only distinguishes "misaligned" from everything else —
|
|
84
|
-
// that direction is unchanged, because a false failure sends a correctly
|
|
85
|
-
// configured agent looking for something to change.
|
|
86
|
-
const alignment = !probe.known
|
|
87
|
-
? "unknown"
|
|
88
|
-
: knownHostsIsUsedBy(probe.effective, knownHostsPath) ? "aligned" : "elsewhere";
|
|
89
|
-
|
|
90
|
-
ctx.emit({
|
|
91
|
-
path: knownHostsPath,
|
|
92
|
-
pattern: published.pattern,
|
|
93
|
-
fingerprint,
|
|
94
|
-
// Reported per repository, never per machine: one agent can have a
|
|
95
|
-
// configured clone and an unconfigured one side by side.
|
|
96
|
-
repoUsesThisFile: probe.known ? alignment === "aligned" : null,
|
|
97
|
-
repoEffectiveKnownHosts: probe.known ? probe.effective.raw : null,
|
|
98
|
-
}, () => {
|
|
99
|
-
ctx.out(t(ctx.locale, "knownHostsRefreshed", { path: knownHostsPath, pattern: published.pattern, fingerprint }));
|
|
100
|
-
if (alignment === "elsewhere") {
|
|
101
|
-
ctx.err(t(ctx.locale, probe.configured ? "knownHostsRepoElsewhere" : "knownHostsRepoUnconfigured", {
|
|
102
|
-
written: knownHostsPath,
|
|
103
|
-
effective: probe.effective.raw,
|
|
104
|
-
sshCommand: sshCommandFor({
|
|
105
|
-
// The key this instance enrolled (task #645, cross dogfooding #643):
|
|
106
|
-
// suggesting ~/.ssh/id_ed25519 to an agent that enrolled another key
|
|
107
|
-
// turns a host-key fix into a publickey denial. Same order as
|
|
108
|
-
// `repo clone`: STAMP_SSH_IDENTITY, then the recorded key, then the
|
|
109
|
-
// default path.
|
|
110
|
-
identityPath: resolveIdentityPath(ctx.env, "", readRecordedIdentity(ctx.env, ctx.auth?.apiBase)),
|
|
111
|
-
knownHostsPath,
|
|
112
|
-
}),
|
|
113
|
-
}));
|
|
114
|
-
return;
|
|
115
|
-
}
|
|
116
|
-
if (alignment === "unknown") {
|
|
117
|
-
// Says which check did not happen, not just that something is unknown:
|
|
118
|
-
// "could not confirm" with no reason reads as a glitch and gets ignored.
|
|
119
|
-
ctx.out(t(ctx.locale, "knownHostsRepoUnchecked", {
|
|
120
|
-
written: knownHostsPath,
|
|
121
|
-
// Explicit skip first: it is the only case that knows why on its own.
|
|
122
|
-
// The other two are distinguished by whether we got far enough to ask
|
|
123
|
-
// the repository anything at all.
|
|
124
|
-
reason: t(ctx.locale, probe.skippedReason
|
|
125
|
-
?? (probe.inRepo ? "knownHostsUncheckedProbe" : "knownHostsUncheckedNoRepo")),
|
|
126
|
-
}));
|
|
127
|
-
return;
|
|
128
|
-
}
|
|
129
|
-
// Aligned — and the caveat rides here too. It was already written, but only
|
|
130
|
-
// in the unconfigured branch, which is the one branch a correctly set up
|
|
131
|
-
// reader never reaches (#610). Same error as #607: the qualifier existed
|
|
132
|
-
// somewhere other than where the reader stops.
|
|
133
|
-
ctx.out(t(ctx.locale, "knownHostsRepoAligned", { effective: probe.effective.raw }));
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
// A repo that reads somewhere else did not get refreshed, whatever the exit
|
|
137
|
-
// code of the write says. Silence here is what lets someone believe they are
|
|
138
|
-
// ready for the rotation.
|
|
139
|
-
//
|
|
140
|
-
// Every other outcome exits 0, including a probe that could not run: this
|
|
141
|
-
// command exists to stop people from disabling host key checking, so telling
|
|
142
|
-
// a correctly configured agent that it has a problem is the worse error. It
|
|
143
|
-
// also cannot see a per-invocation GIT_SSH_COMMAND at all, which the
|
|
144
|
-
// unconfigured message says outright rather than implying coverage.
|
|
145
|
-
// Only "elsewhere" is a failure. "unknown" stays 0 deliberately: the check
|
|
146
|
-
// could not run, and turning that into a non-zero exit would send a correctly
|
|
147
|
-
// configured agent hunting for a problem — the direction this command must
|
|
148
|
-
// never fail in. The distinction now lives in the output instead of vanishing.
|
|
149
|
-
return ctx.exit(alignment === "elsewhere" ? 1 : 0);
|
|
150
|
-
}
|