@indigoai-us/hq-cli 5.345.220 → 5.345.222
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/CHANGELOG.md +164 -0
- package/dist/command-catalog.generated.d.ts +175 -2
- package/dist/command-catalog.generated.js +213 -2
- package/dist/commands/auth.js +6 -2
- package/dist/commands/bot-runtime.d.ts +18 -5
- package/dist/commands/bot-runtime.js +29 -12
- package/dist/commands/bot.d.ts +39 -2
- package/dist/commands/bot.js +140 -11
- package/dist/commands/core-delegate-w1b.js +9 -3
- package/dist/commands/core-stop-autosave.d.ts +21 -0
- package/dist/commands/core-stop-autosave.js +300 -0
- package/dist/commands/core.js +2 -0
- package/dist/commands/daemon.d.ts +6 -0
- package/dist/commands/daemon.js +46 -8
- package/dist/commands/doctor-usage.d.ts +5 -0
- package/dist/commands/doctor-usage.js +25 -0
- package/dist/commands/env.d.ts +49 -4
- package/dist/commands/env.js +355 -5
- package/dist/commands/lanes-shared.d.ts +3 -0
- package/dist/commands/lanes.d.ts +3 -0
- package/dist/commands/lanes.js +87 -3
- package/dist/commands/link.d.ts +2 -0
- package/dist/commands/link.js +14 -2
- package/dist/commands/mesh.js +23 -3
- package/dist/commands/sources.js +3 -3
- package/dist/commands/sync-core.d.ts +7 -0
- package/dist/commands/sync-core.js +132 -3
- package/dist/commands/whoami.js +96 -80
- package/dist/data/command-states.json +3 -0
- package/dist/lib/bot/config.d.ts +17 -0
- package/dist/lib/bot/config.js +13 -0
- package/dist/lib/bot/fleet-cron.js +24 -2
- package/dist/lib/bot/fleet-local.d.ts +30 -2
- package/dist/lib/bot/fleet-local.js +60 -1
- package/dist/lib/bot/grok-login-scope.d.ts +36 -0
- package/dist/lib/bot/grok-login-scope.js +93 -0
- package/dist/lib/bot/legacy-provider-login.d.ts +32 -9
- package/dist/lib/bot/legacy-provider-login.js +28 -22
- package/dist/lib/bot/runtime-sign-in-check.js +6 -1
- package/dist/lib/bot/runtime-sign-in.d.ts +2 -0
- package/dist/lib/core-utils/sentry-report.js +2 -0
- package/dist/lib/daemon/catalog.js +2 -0
- package/dist/lib/daemon/install.d.ts +2 -1
- package/dist/lib/daemon/install.js +9 -0
- package/dist/lib/daemon/legacy.d.ts +1 -0
- package/dist/lib/daemon/legacy.js +5 -2
- package/dist/lib/daemon/machine-check-owner.d.ts +2 -0
- package/dist/lib/daemon/machine-check-owner.js +6 -1
- package/dist/lib/daemon/machine-checks.d.ts +29 -2
- package/dist/lib/daemon/machine-checks.js +64 -13
- package/dist/lib/daemon/mesh-owner.d.ts +45 -0
- package/dist/lib/daemon/mesh-owner.js +166 -0
- package/dist/lib/daemon/native-notice-ledger.d.ts +27 -0
- package/dist/lib/daemon/native-notice-ledger.js +113 -0
- package/dist/lib/daemon/run.d.ts +15 -0
- package/dist/lib/daemon/run.js +137 -11
- package/dist/lib/daemon/selfcheck.d.ts +35 -2
- package/dist/lib/daemon/selfcheck.js +118 -9
- package/dist/lib/daemon/stop-autosave-lock.d.ts +19 -0
- package/dist/lib/daemon/stop-autosave-lock.js +166 -0
- package/dist/lib/daemon/stop-autosave.d.ts +28 -0
- package/dist/lib/daemon/stop-autosave.js +111 -0
- package/dist/lib/daemon/stop-diagnostics.d.ts +11 -0
- package/dist/lib/daemon/stop-diagnostics.js +136 -0
- package/dist/lib/daemon/stop-job-executor.d.ts +19 -0
- package/dist/lib/daemon/stop-job-executor.js +126 -0
- package/dist/lib/daemon/stop-jobs.d.ts +93 -0
- package/dist/lib/daemon/stop-jobs.js +599 -0
- package/dist/lib/doctor/checks/fleet-hook-core.js +1 -1
- package/dist/lib/doctor/checks/host-core.js +1 -1
- package/dist/lib/environment/access-probes.d.ts +234 -0
- package/dist/lib/environment/access-probes.js +918 -0
- package/dist/lib/environment/profile.d.ts +15 -2
- package/dist/lib/environment/profile.js +55 -20
- package/dist/lib/environment/rfc3339.d.ts +8 -0
- package/dist/lib/environment/rfc3339.js +26 -0
- package/dist/lib/environment/work-requirements.d.ts +169 -0
- package/dist/lib/environment/work-requirements.js +389 -0
- package/dist/lib/hook-engine/coverage.d.ts +146 -7
- package/dist/lib/hook-engine/coverage.js +1480 -4
- package/dist/lib/hook-engine/fleet-registration.js +2 -2
- package/dist/lib/hook-engine/git-stop-config.json +513 -0
- package/dist/lib/hook-engine/native-stop-compatibility.json +362 -0
- package/dist/lib/hook-engine/reconcile.js +2 -2
- package/dist/lib/hot-channels/alert.d.ts +2 -2
- package/dist/lib/hot-channels/alert.js +8 -5
- package/dist/lib/hot-channels/signals-pass.d.ts +126 -17
- package/dist/lib/hot-channels/signals-pass.js +296 -17
- package/dist/lib/hot-channels/signals-runtime.d.ts +30 -0
- package/dist/lib/hot-channels/signals-runtime.js +82 -0
- package/dist/lib/install/hook-target.js +1 -1
- package/dist/lib/lanes/host-capacity.d.ts +26 -2
- package/dist/lib/lanes/host-capacity.js +17 -2
- package/dist/lib/lanes/host-compute.d.ts +90 -0
- package/dist/lib/lanes/host-compute.js +185 -0
- package/dist/lib/lanes/index.d.ts +2 -0
- package/dist/lib/lanes/index.js +1 -0
- package/dist/lib/lanes/reconcile.js +22 -1
- package/dist/lib/lanes/resource-usage.d.ts +155 -0
- package/dist/lib/lanes/resource-usage.js +589 -0
- package/dist/lib/lanes/run-size.d.ts +46 -0
- package/dist/lib/lanes/run-size.js +197 -0
- package/dist/lib/lanes/store.js +27 -0
- package/dist/lib/lanes/throttle.d.ts +36 -1
- package/dist/lib/lanes/throttle.js +47 -10
- package/dist/lib/lanes/types.d.ts +40 -0
- package/dist/lib/lanes/types.js +2 -2
- package/dist/lib/lanes/watcher.js +13 -4
- package/dist/lib/lanes/worker-process.d.ts +25 -2
- package/dist/lib/lanes/worker-process.js +71 -19
- package/dist/lib/mesh/live/daemon/config.d.ts +8 -1
- package/dist/lib/mesh/live/daemon/config.js +16 -0
- package/dist/lib/mesh/live/daemon/install.d.ts +31 -2
- package/dist/lib/mesh/live/daemon/install.js +142 -1
- package/dist/lib/mesh/live/daemon/login-home.d.ts +5 -0
- package/dist/lib/mesh/live/daemon/login-home.js +27 -0
- package/dist/lib/mesh/live/daemon/unit-home.d.ts +49 -0
- package/dist/lib/mesh/live/daemon/unit-home.js +134 -0
- package/dist/lib/placement/match.d.ts +192 -0
- package/dist/lib/placement/match.js +667 -0
- package/dist/lib/registry/registry.d.ts +35 -7
- package/dist/lib/registry/registry.js +88 -13
- package/dist/lib/safe-store.d.ts +14 -0
- package/dist/lib/safe-store.js +77 -4
- package/dist/lib/scan-packages/index.js +24 -2
- package/dist/lib/search-index/index.d.ts +88 -4
- package/dist/lib/search-index/index.js +288 -24
- package/dist/lib/search-index/max-doc-bytes.d.ts +17 -6
- package/dist/lib/search-index/max-doc-bytes.js +49 -23
- package/dist/lib/service-manager/startup-migration.d.ts +1 -0
- package/dist/lib/service-manager/startup-migration.js +3 -0
- package/dist/lib/signals/codex-lockdown.d.ts +79 -0
- package/dist/lib/signals/codex-lockdown.js +271 -0
- package/dist/lib/signals/codex-login.d.ts +37 -0
- package/dist/lib/signals/codex-login.js +83 -0
- package/dist/lib/signals/engine.d.ts +27 -0
- package/dist/lib/signals/engine.js +70 -0
- package/dist/lib/signals/reply.js +22 -1
- package/dist/lib/signals/run-record.js +6 -1
- package/dist/lib/signals/settings.d.ts +5 -0
- package/dist/lib/signals/settings.js +2 -0
- package/dist/lib/signals/steps/extract.d.ts +85 -9
- package/dist/lib/signals/steps/extract.js +317 -68
- package/dist/lib/signals/steps/types.d.ts +2 -0
- package/dist/lib/sync-core/engine-holds.d.ts +14 -0
- package/dist/lib/sync-core/engine-holds.js +60 -0
- package/dist/lib/sync-core/engine-home.d.ts +37 -1
- package/dist/lib/sync-core/engine-home.js +119 -16
- package/dist/lib/sync-core/reconcile.js +8 -0
- package/dist/lib/sync-core/rollout-state.d.ts +2 -1
- package/dist/lib/sync-core/rollout-state.js +5 -2
- package/dist/lib/sync-core/vault-settings.d.ts +21 -0
- package/dist/lib/sync-core/vault-settings.js +131 -0
- package/dist/lib/sync-core/vault-uid.d.ts +2 -0
- package/dist/lib/sync-core/vault-uid.js +4 -0
- package/dist/lib/windows/install-followup.d.ts +2 -0
- package/dist/lib/windows/install-followup.js +4 -1
- package/dist/schemas/hq-work-requirements.v1.schema.json +145 -0
- package/dist/utils/auth-session-check.d.ts +16 -0
- package/dist/utils/auth-session-check.js +16 -0
- package/dist/utils/client-health-contract.d.ts +6 -0
- package/dist/utils/client-health-contract.js +13 -0
- package/dist/utils/client-health.d.ts +7 -0
- package/dist/utils/client-health.js +22 -1
- package/dist/utils/command-usage.d.ts +40 -0
- package/dist/utils/command-usage.js +61 -0
- package/dist/utils/duplicate-command-usage.d.ts +72 -0
- package/dist/utils/duplicate-command-usage.js +125 -0
- package/dist/utils/host-registration.js +5 -2
- package/dist/utils/pack-contributions.d.ts +3 -0
- package/dist/utils/pack-contributions.js +110 -23
- package/dist/utils/resident-package-preload.js +4 -0
- package/dist/utils/self-update.d.ts +3 -1
- package/dist/utils/self-update.js +56 -11
- package/dist/utils/sentry-fingerprint.js +9 -0
- package/dist/utils/update-lock.d.ts +151 -14
- package/dist/utils/update-lock.js +782 -46
- package/dist/utils/version-gate.js +12 -2
- package/dist/utils/windows-bash.d.ts +2 -0
- package/dist/utils/windows-bash.js +2 -1
- package/package.json +9 -8
- package/scripts/bench-core-compare.mjs +313 -0
- package/scripts/bench-core.mjs +399 -12
- package/scripts/check-function-manifest.mjs +116 -3
|
@@ -0,0 +1,918 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Access probes for `hq env probe` (PS-009): run each probe by name and
|
|
3
|
+
* record the result as a `readings.access` entry of hq-environment-profile.v1.
|
|
4
|
+
*
|
|
5
|
+
* Probes:
|
|
6
|
+
*
|
|
7
|
+
* vault:<company> a vault listing of companies/<company>/
|
|
8
|
+
* answers under this identity (`hq files browse`)
|
|
9
|
+
* repo:<name> the local checkout's origin answers
|
|
10
|
+
* `git ls-remote` with this machine's git login
|
|
11
|
+
* secret:<NAME>.exists the name exists in the company vault (HEAD,
|
|
12
|
+
* `hq secrets exists`)
|
|
13
|
+
* secret:<NAME>.resolvable this identity can resolve the value: it is
|
|
14
|
+
* injected by `hq secrets exec` into a child
|
|
15
|
+
* that checks only that the variable is non-empty
|
|
16
|
+
* integration:<slug> the app is connected and shared with this
|
|
17
|
+
* identity (`hq integrations list --usable`)
|
|
18
|
+
* data:<path> a path under the HQ root is present and not empty
|
|
19
|
+
* identity:agent/<check>, ... with --agent: the checks of `hq agent probe`
|
|
20
|
+
*
|
|
21
|
+
* The two secret readings carry the suffixes `.exists` and `.resolvable`. The
|
|
22
|
+
* schema's probe-name rule allows `.`, and a secret name cannot contain one,
|
|
23
|
+
* so the suffix never collides with a name.
|
|
24
|
+
*
|
|
25
|
+
* The secret value never reaches this process. `hq secrets exec` fetches it
|
|
26
|
+
* (and keeps its usual encrypted cache) and injects it into a grandchild that
|
|
27
|
+
* exits 0 or UNRESOLVED_EXIT and prints nothing. The exec child's stdout is
|
|
28
|
+
* discarded and its stderr is only matched against known failure phrases,
|
|
29
|
+
* after every occurrence of the secret name is removed; it is never copied
|
|
30
|
+
* into a reason. A reason is one of the fixed sentences in this file plus
|
|
31
|
+
* names the caller gave, and it is withheld if it looks like a credential.
|
|
32
|
+
*/
|
|
33
|
+
import { spawn } from "node:child_process";
|
|
34
|
+
import * as fs from "node:fs";
|
|
35
|
+
import * as path from "node:path";
|
|
36
|
+
import { CACHED_SECRETS_SERVER_UNAVAILABLE_MESSAGE } from "../../utils/secret-load-availability.js";
|
|
37
|
+
import { credentialShapesIn } from "./credential-shapes.js";
|
|
38
|
+
/** The check child exits with this code when the variable is missing or empty. */
|
|
39
|
+
export const UNRESOLVED_EXIT = 87;
|
|
40
|
+
/**
|
|
41
|
+
* Runs in the grandchild under `hq secrets exec`. It reads the variable named
|
|
42
|
+
* by its first argument and exits by its length only; it writes nothing.
|
|
43
|
+
*/
|
|
44
|
+
export const RESOLVE_CHECK_SCRIPT = `const v=process.env[process.argv[1]];process.exit(typeof v==="string"&&v.length>0?0:${UNRESOLVED_EXIT});`;
|
|
45
|
+
/** Same rule as `hq secrets exec --only`. */
|
|
46
|
+
export const SECRET_NAME_PATTERN = /^[A-Z][A-Z0-9_]*(\/[A-Z][A-Z0-9_]+)*$/;
|
|
47
|
+
export const COMPANY_SLUG_PATTERN = /^[a-z0-9][a-z0-9-]*$/;
|
|
48
|
+
export const REPO_NAME_PATTERN = /^[A-Za-z0-9._-]+$/;
|
|
49
|
+
export const INTEGRATION_SLUG_PATTERN = /^[A-Za-z0-9._-]+$/;
|
|
50
|
+
/** A relative path of plain segments; the schema's probe-name rule allows no more. */
|
|
51
|
+
export const DATA_PATH_PATTERN = /^[A-Za-z0-9._-]+(\/[A-Za-z0-9._-]+)*$/;
|
|
52
|
+
export const SECRET_EXISTS_SUFFIX = ".exists";
|
|
53
|
+
export const SECRET_RESOLVABLE_SUFFIX = ".resolvable";
|
|
54
|
+
export const SECRET_TTL_SECONDS = 3600;
|
|
55
|
+
export const VAULT_TTL_SECONDS = 3600;
|
|
56
|
+
export const INTEGRATION_TTL_SECONDS = 3600;
|
|
57
|
+
export const IDENTITY_TTL_SECONDS = 3600;
|
|
58
|
+
export const REPO_TTL_SECONDS = 86400;
|
|
59
|
+
export const DATA_TTL_SECONDS = 86400;
|
|
60
|
+
export const CHILD_TIMEOUT_MS = 60_000;
|
|
61
|
+
export const GIT_TIMEOUT_MS = 30_000;
|
|
62
|
+
const STDERR_CAP_BYTES = 64 * 1024;
|
|
63
|
+
const STDOUT_CAP_BYTES = 4 * 1024 * 1024;
|
|
64
|
+
export const CACHED_RESULT_REASON = "vault unreachable; result came from the local cache";
|
|
65
|
+
const WITHHELD_REASON = "the probe failed; its detail was withheld because it held a string shaped like a credential";
|
|
66
|
+
/** Checks a request before any probe runs; returns a list of problems. */
|
|
67
|
+
export function requestProblems(request) {
|
|
68
|
+
const problems = [];
|
|
69
|
+
if (!COMPANY_SLUG_PATTERN.test(request.company))
|
|
70
|
+
problems.push(`--company must match ${COMPANY_SLUG_PATTERN.source}`);
|
|
71
|
+
for (const name of request.secrets ?? []) {
|
|
72
|
+
if (!SECRET_NAME_PATTERN.test(name))
|
|
73
|
+
problems.push(`--secret names must match ${SECRET_NAME_PATTERN.source}; one does not`);
|
|
74
|
+
}
|
|
75
|
+
for (const repo of request.repos ?? []) {
|
|
76
|
+
if (!REPO_NAME_PATTERN.test(repo) || repo === "." || repo === "..")
|
|
77
|
+
problems.push(`--repo takes a checkout name such as hq-cli; one is not`);
|
|
78
|
+
}
|
|
79
|
+
for (const slug of request.integrations ?? []) {
|
|
80
|
+
if (!INTEGRATION_SLUG_PATTERN.test(slug))
|
|
81
|
+
problems.push(`--integration takes a provider slug such as github; one is not`);
|
|
82
|
+
}
|
|
83
|
+
for (const entry of request.data ?? []) {
|
|
84
|
+
const segments = entry.split("/");
|
|
85
|
+
if (!DATA_PATH_PATTERN.test(entry) || segments.some((s) => s === "." || s === "..")) {
|
|
86
|
+
problems.push(`--data takes a path relative to the HQ root, without . or .. segments; one is not`);
|
|
87
|
+
}
|
|
88
|
+
else if (segments[0].toLowerCase() === "companies"
|
|
89
|
+
&& (segments[0] !== "companies" || segments[1] !== request.company)) {
|
|
90
|
+
// Compared without case: on a case-insensitive disk Companies/other-co is companies/other-co.
|
|
91
|
+
problems.push(`--data paths under companies/ must be under companies/${request.company}/`);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
// A name shaped like a credential is refused before any child process or
|
|
95
|
+
// URL sees it, and is not echoed.
|
|
96
|
+
const shaped = [
|
|
97
|
+
["--secret", request.secrets],
|
|
98
|
+
["--repo", request.repos],
|
|
99
|
+
["--integration", request.integrations],
|
|
100
|
+
["--data", request.data],
|
|
101
|
+
];
|
|
102
|
+
for (const [flag, names] of shaped) {
|
|
103
|
+
if ((names ?? []).some((name) => credentialShapesIn(name).length > 0)) {
|
|
104
|
+
problems.push(`one ${flag} name is shaped like a credential, so nothing was probed`);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return [...new Set(problems)];
|
|
108
|
+
}
|
|
109
|
+
/** The readings.access entry for one probe line. */
|
|
110
|
+
export function toAccessReading(entry) {
|
|
111
|
+
return {
|
|
112
|
+
observed_at: entry.observed_at,
|
|
113
|
+
ttl_seconds: entry.ttl_seconds,
|
|
114
|
+
source: entry.source,
|
|
115
|
+
name: entry.probe,
|
|
116
|
+
result: entry.result,
|
|
117
|
+
checks: entry.checks,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
/** The base name a reading supersedes: secret:<NAME>.exists also replaces a hand-run secret:<NAME>. */
|
|
121
|
+
export function supersededNames(readingName) {
|
|
122
|
+
for (const suffix of [SECRET_EXISTS_SUFFIX, SECRET_RESOLVABLE_SUFFIX]) {
|
|
123
|
+
if (readingName.startsWith("secret:") && readingName.endsWith(suffix)) {
|
|
124
|
+
return [readingName, readingName.slice(0, -suffix.length)];
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return [readingName];
|
|
128
|
+
}
|
|
129
|
+
/** Remove every occurrence of the caller-given names, so a name cannot match a phrase. */
|
|
130
|
+
function withoutNames(text, names) {
|
|
131
|
+
let out = text;
|
|
132
|
+
for (const name of names)
|
|
133
|
+
if (name)
|
|
134
|
+
out = out.split(name).join(" ");
|
|
135
|
+
return out;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Map an `hq secrets exec` failure to a fixed sentence. The stderr text is
|
|
139
|
+
* matched, never copied: a buggy child could have written anything there.
|
|
140
|
+
* The secret name is removed first, so HQ_SANDBOX/API_KEY does not read as a
|
|
141
|
+
* sandbox refusal.
|
|
142
|
+
*/
|
|
143
|
+
export function classifyExecFailure(name, outcome, company = "") {
|
|
144
|
+
if (outcome.timedOut)
|
|
145
|
+
return { result: "unknown", reason: "hq secrets exec timed out" };
|
|
146
|
+
if (outcome.code === UNRESOLVED_EXIT) {
|
|
147
|
+
return { result: "fail", reason: `hq secrets exec ran the check but ${name} was empty or not injected` };
|
|
148
|
+
}
|
|
149
|
+
// The company slug is removed too: a company named login-co must not read as a sign-in error.
|
|
150
|
+
const stderr = withoutNames(outcome.stderr, [name, company]);
|
|
151
|
+
if (/no read permission|forbidden|\b403\b|access denied|not permitted|permission denied/i.test(stderr)) {
|
|
152
|
+
return { result: "fail", reason: `this identity is not allowed to read ${name}` };
|
|
153
|
+
}
|
|
154
|
+
if (/sandbox/i.test(stderr)) {
|
|
155
|
+
return { result: "fail", reason: `${name} is a high-security secret that resolves only inside hq secrets sandbox` };
|
|
156
|
+
}
|
|
157
|
+
if (/revoked/i.test(stderr))
|
|
158
|
+
return { result: "fail", reason: `${name} has been revoked` };
|
|
159
|
+
if (/has no value/i.test(stderr))
|
|
160
|
+
return { result: "fail", reason: `the vault returned no value of ${name} to this identity` };
|
|
161
|
+
if (/not found|\b404\b|does not exist|no such secret/i.test(stderr)) {
|
|
162
|
+
return { result: "fail", reason: `${name} was not found in this identity's vault scope` };
|
|
163
|
+
}
|
|
164
|
+
if (/outside this session's locked company set|locked company/i.test(stderr)) {
|
|
165
|
+
return { result: "fail", reason: "the company is outside this session's locked company set" };
|
|
166
|
+
}
|
|
167
|
+
if (/sign[- ]?in|not logged in|login|unauthori[sz]ed|\b401\b|expired/i.test(stderr)) {
|
|
168
|
+
return { result: "unknown", reason: "no usable HQ sign-in for hq secrets exec" };
|
|
169
|
+
}
|
|
170
|
+
if (/unavailable|ECONN|ENOTFOUND|ETIMEDOUT|timed out|\b5\d\d\b|network/i.test(stderr)) {
|
|
171
|
+
return { result: "unknown", reason: "the vault could not be reached" };
|
|
172
|
+
}
|
|
173
|
+
return { result: "unknown", reason: `hq secrets exec exited ${outcome.code} without a known reason` };
|
|
174
|
+
}
|
|
175
|
+
export function classifyGitFailure(repo, outcome) {
|
|
176
|
+
if (outcome.timedOut)
|
|
177
|
+
return { result: "unknown", reason: "git ls-remote timed out" };
|
|
178
|
+
if (outcome.code === 2)
|
|
179
|
+
return { result: "fail", reason: `origin of ${repo} answered but has no HEAD` };
|
|
180
|
+
const stderr = withoutNames(outcome.stderr, [repo]);
|
|
181
|
+
if (/authentication failed|permission denied|could not read username|access denied|\b403\b|terminal prompts disabled|host key verification failed/i.test(stderr)) {
|
|
182
|
+
return { result: "fail", reason: `this machine's git login cannot read the origin of ${repo}` };
|
|
183
|
+
}
|
|
184
|
+
if (/repository not found|not found|does not exist|\b404\b/i.test(stderr)) {
|
|
185
|
+
return { result: "fail", reason: `the origin of ${repo} was not found, or this login cannot see it` };
|
|
186
|
+
}
|
|
187
|
+
if (/no such remote|does not appear to be a git repository|not a git repository/i.test(stderr)) {
|
|
188
|
+
return { result: "fail", reason: `the checkout of ${repo} has no usable origin remote` };
|
|
189
|
+
}
|
|
190
|
+
if (/could not resolve host|connection|timed out|network|unable to access/i.test(stderr)) {
|
|
191
|
+
return { result: "unknown", reason: `the origin of ${repo} could not be reached` };
|
|
192
|
+
}
|
|
193
|
+
return { result: "unknown", reason: `git ls-remote exited ${outcome.code} without a known reason` };
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* The user's ssh command with BatchMode forced on, so ssh cannot prompt.
|
|
197
|
+
*
|
|
198
|
+
* For ssh the FIRST value given for an option wins, command-line -o included
|
|
199
|
+
* (ssh_config(5): "the first obtained value will be used"; checked with
|
|
200
|
+
* `ssh -G -o BatchMode=no -o BatchMode=yes`, which reports batchmode no). So
|
|
201
|
+
* the option goes straight after the program, before the user's own options.
|
|
202
|
+
*
|
|
203
|
+
* Returns null when no word of the command names ssh (plink, a custom
|
|
204
|
+
* wrapper): BatchMode is an ssh option, so nothing is added to it.
|
|
205
|
+
*/
|
|
206
|
+
export function batchSshCommand(existing) {
|
|
207
|
+
const base = existing?.trim() ? existing.trim() : "ssh";
|
|
208
|
+
// Split into shell words, keeping quotes, and note where each word ends.
|
|
209
|
+
const words = [];
|
|
210
|
+
let i = 0;
|
|
211
|
+
while (i < base.length) {
|
|
212
|
+
while (i < base.length && /\s/.test(base[i]))
|
|
213
|
+
i += 1;
|
|
214
|
+
if (i >= base.length)
|
|
215
|
+
break;
|
|
216
|
+
let text = "";
|
|
217
|
+
while (i < base.length && !/\s/.test(base[i])) {
|
|
218
|
+
const ch = base[i];
|
|
219
|
+
if (ch === '"' || ch === "'") {
|
|
220
|
+
const close = base.indexOf(ch, i + 1);
|
|
221
|
+
const stop = close === -1 ? base.length : close;
|
|
222
|
+
text += base.slice(i + 1, stop);
|
|
223
|
+
i = close === -1 ? base.length : close + 1;
|
|
224
|
+
}
|
|
225
|
+
else {
|
|
226
|
+
text += ch;
|
|
227
|
+
i += 1;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
words.push({ text, end: i });
|
|
231
|
+
}
|
|
232
|
+
// After the first word that names ssh (a wrapper such as env or torsocks may
|
|
233
|
+
// come before it). A word holding `=` is an assignment or a wrapper option
|
|
234
|
+
// (SSH_AUTH_SOCK=/run/ssh), never the program, so it is skipped.
|
|
235
|
+
const sshWord = words.find((word) => {
|
|
236
|
+
if (word.text.includes("="))
|
|
237
|
+
return false;
|
|
238
|
+
const name = word.text.split(/[\\/]/).pop().toLowerCase();
|
|
239
|
+
return name === "ssh" || name === "ssh.exe";
|
|
240
|
+
});
|
|
241
|
+
if (!sshWord)
|
|
242
|
+
return null;
|
|
243
|
+
if (sshWord.end >= base.length)
|
|
244
|
+
return `${base} -o BatchMode=yes`;
|
|
245
|
+
return `${base.slice(0, sshWord.end)} -o BatchMode=yes ${base.slice(sshWord.end).trim()}`;
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Whether an ssh command that does not name ssh is still known not to prompt:
|
|
249
|
+
* plink's `-batch`, or an explicit BatchMode=yes.
|
|
250
|
+
*/
|
|
251
|
+
export function knownNonInteractiveSsh(command) {
|
|
252
|
+
return /(^|\s)-batch(\s|$)/.test(command) || /BatchMode[=\s]+yes/i.test(command);
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* The environment the repo probe's git runs under: no terminal prompt, no
|
|
256
|
+
* Git Credential Manager dialog, and no askpass program. Git runs
|
|
257
|
+
* GIT_ASKPASS, core.askPass or SSH_ASKPASS before it looks at
|
|
258
|
+
* GIT_TERMINAL_PROMPT; an empty GIT_ASKPASS skips all three, and git then
|
|
259
|
+
* fails with "terminal prompts disabled". ssh's own askpass is cleared and
|
|
260
|
+
* SSH_ASKPASS_REQUIRE=never stops ssh from using one.
|
|
261
|
+
*/
|
|
262
|
+
export function gitProbeEnv(env) {
|
|
263
|
+
const out = {
|
|
264
|
+
...env,
|
|
265
|
+
GIT_TERMINAL_PROMPT: "0",
|
|
266
|
+
GCM_INTERACTIVE: "never",
|
|
267
|
+
GIT_ASKPASS: "",
|
|
268
|
+
SSH_ASKPASS_REQUIRE: "never",
|
|
269
|
+
};
|
|
270
|
+
delete out.SSH_ASKPASS;
|
|
271
|
+
return out;
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Whether a real path is inside a folder. Compared without case on macOS and
|
|
275
|
+
* Windows, whose default disks ignore case.
|
|
276
|
+
*/
|
|
277
|
+
function isInside(child, parent) {
|
|
278
|
+
const fold = process.platform === "darwin" || process.platform === "win32";
|
|
279
|
+
const c = fold ? child.toLowerCase() : child;
|
|
280
|
+
const p = fold ? parent.toLowerCase() : parent;
|
|
281
|
+
return c === p || c.startsWith(p.endsWith(path.sep) ? p : `${p}${path.sep}`);
|
|
282
|
+
}
|
|
283
|
+
function sameSlug(a, b) {
|
|
284
|
+
const fold = process.platform === "darwin" || process.platform === "win32";
|
|
285
|
+
return fold ? a.toLowerCase() === b.toLowerCase() : a === b;
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Why a resolved data path may not be read for this company, or null.
|
|
289
|
+
*
|
|
290
|
+
* - The company's own folder must be a real folder, not a symlink (the
|
|
291
|
+
* session-lock reader does not count a symlinked company folder either).
|
|
292
|
+
* - The target may not resolve inside any other company's resolved folder,
|
|
293
|
+
* wherever that folder points: companies/other-co may itself be a symlink
|
|
294
|
+
* to a place outside companies/.
|
|
295
|
+
* - A target inside the resolved companies/ folder must be inside the
|
|
296
|
+
* company's own folder.
|
|
297
|
+
*/
|
|
298
|
+
export function dataScopeFailure(hqRoot, company, relative, real) {
|
|
299
|
+
const companiesDir = path.join(hqRoot, "companies");
|
|
300
|
+
const own = path.join(companiesDir, company);
|
|
301
|
+
let ownReal;
|
|
302
|
+
try {
|
|
303
|
+
if (fs.lstatSync(own).isSymbolicLink()) {
|
|
304
|
+
return `companies/${company} is a symlink, so data is not read through it`;
|
|
305
|
+
}
|
|
306
|
+
ownReal = fs.realpathSync.native(own);
|
|
307
|
+
}
|
|
308
|
+
catch {
|
|
309
|
+
ownReal = undefined;
|
|
310
|
+
}
|
|
311
|
+
const outside = `${relative} resolves outside companies/${company}/, so it was not read`;
|
|
312
|
+
let entries;
|
|
313
|
+
try {
|
|
314
|
+
entries = fs.readdirSync(companiesDir);
|
|
315
|
+
}
|
|
316
|
+
catch {
|
|
317
|
+
entries = [];
|
|
318
|
+
}
|
|
319
|
+
for (const entry of entries) {
|
|
320
|
+
if (sameSlug(entry, company))
|
|
321
|
+
continue;
|
|
322
|
+
let otherReal;
|
|
323
|
+
try {
|
|
324
|
+
otherReal = fs.realpathSync.native(path.join(companiesDir, entry));
|
|
325
|
+
}
|
|
326
|
+
catch {
|
|
327
|
+
continue;
|
|
328
|
+
}
|
|
329
|
+
// A company folder that points at the HQ root or above would cover everything.
|
|
330
|
+
if (ownReal && isInside(ownReal, otherReal))
|
|
331
|
+
return `companies/${entry} resolves over companies/${company}/, so data is not read`;
|
|
332
|
+
if (isInside(real, otherReal))
|
|
333
|
+
return outside;
|
|
334
|
+
}
|
|
335
|
+
let companiesReal;
|
|
336
|
+
try {
|
|
337
|
+
companiesReal = fs.realpathSync.native(companiesDir);
|
|
338
|
+
}
|
|
339
|
+
catch {
|
|
340
|
+
companiesReal = undefined;
|
|
341
|
+
}
|
|
342
|
+
if (companiesReal && isInside(real, companiesReal) && !(ownReal && isInside(real, ownReal)))
|
|
343
|
+
return outside;
|
|
344
|
+
return null;
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* Whether a file path falls inside a company folder other than `company`,
|
|
348
|
+
* judged both by name and after the folder holding it is resolved, so a
|
|
349
|
+
* symlinked folder cannot hide it. Folder names are compared without case on
|
|
350
|
+
* macOS and Windows.
|
|
351
|
+
*/
|
|
352
|
+
export function pathInOtherCompany(hqRoot, company, filePath) {
|
|
353
|
+
const companiesDir = path.join(hqRoot, "companies");
|
|
354
|
+
const absolute = path.resolve(filePath);
|
|
355
|
+
const candidates = [absolute];
|
|
356
|
+
try {
|
|
357
|
+
candidates.push(path.join(fs.realpathSync.native(path.dirname(absolute)), path.basename(absolute)));
|
|
358
|
+
}
|
|
359
|
+
catch { /* the folder does not exist; the name is judged alone */ }
|
|
360
|
+
const roots = [companiesDir];
|
|
361
|
+
try {
|
|
362
|
+
roots.push(fs.realpathSync.native(companiesDir));
|
|
363
|
+
}
|
|
364
|
+
catch { /* no companies folder */ }
|
|
365
|
+
for (const candidate of candidates) {
|
|
366
|
+
for (const root of roots) {
|
|
367
|
+
if (!isInside(candidate, root) || candidate.length <= root.length)
|
|
368
|
+
continue;
|
|
369
|
+
const first = candidate.slice(root.length).split(path.sep).filter(Boolean)[0];
|
|
370
|
+
if (first && !sameSlug(first, company))
|
|
371
|
+
return true;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
let entries = [];
|
|
375
|
+
try {
|
|
376
|
+
entries = fs.readdirSync(companiesDir);
|
|
377
|
+
}
|
|
378
|
+
catch { /* no companies folder */ }
|
|
379
|
+
for (const entry of entries) {
|
|
380
|
+
if (sameSlug(entry, company))
|
|
381
|
+
continue;
|
|
382
|
+
let otherReal;
|
|
383
|
+
try {
|
|
384
|
+
otherReal = fs.realpathSync.native(path.join(companiesDir, entry));
|
|
385
|
+
}
|
|
386
|
+
catch {
|
|
387
|
+
continue;
|
|
388
|
+
}
|
|
389
|
+
if (candidates.some((candidate) => isInside(candidate, otherReal)))
|
|
390
|
+
return true;
|
|
391
|
+
}
|
|
392
|
+
return false;
|
|
393
|
+
}
|
|
394
|
+
const AGENT_CHECK_NAMES = {
|
|
395
|
+
whoami: "identity:agent/whoami",
|
|
396
|
+
company: "identity:agent/company",
|
|
397
|
+
files: "vault:agent/files",
|
|
398
|
+
presence: "identity:agent/presence",
|
|
399
|
+
dm: "integration:agent/dm",
|
|
400
|
+
secrets: "vault:agent/secrets",
|
|
401
|
+
report: "identity:agent/report",
|
|
402
|
+
};
|
|
403
|
+
const AGENT_CHECK_SENTENCES = {
|
|
404
|
+
whoami: "hq agent probe: the machine identity mints a token that names this agent.",
|
|
405
|
+
company: "hq agent probe: the agent's company resolves to a company uid.",
|
|
406
|
+
files: "hq agent probe: a vault listing of the agent's company answers on demand.",
|
|
407
|
+
presence: "hq agent probe: the agent inbox answers and the kit heartbeat is fresh.",
|
|
408
|
+
dm: "hq agent probe: a DM send is accepted and the agent inbox answers. Inbound delivery is not proven.",
|
|
409
|
+
secrets: "hq agent probe: the company secret list answers 200. No value is read.",
|
|
410
|
+
report: "hq agent probe: the probe result was accepted by the console.",
|
|
411
|
+
};
|
|
412
|
+
/** Run every requested probe in a fixed order and return one entry per probe. */
|
|
413
|
+
export async function runAccessProbes(request, deps) {
|
|
414
|
+
const now = deps.now ?? (() => new Date());
|
|
415
|
+
const stamp = () => now().toISOString().replace(/\.\d{3}Z$/, "Z");
|
|
416
|
+
const company = request.company;
|
|
417
|
+
const probes = [];
|
|
418
|
+
const startedAt = stamp();
|
|
419
|
+
const push = (fields) => {
|
|
420
|
+
const { failure, ...rest } = fields;
|
|
421
|
+
const entry = {
|
|
422
|
+
probe: rest.probe,
|
|
423
|
+
test: rest.test,
|
|
424
|
+
checks: rest.checks,
|
|
425
|
+
ok: rest.ok,
|
|
426
|
+
result: rest.ok ? "pass" : failure?.result ?? "fail",
|
|
427
|
+
observed_at: stamp(),
|
|
428
|
+
ttl_seconds: rest.ttl_seconds,
|
|
429
|
+
source: rest.source,
|
|
430
|
+
};
|
|
431
|
+
if (!rest.ok) {
|
|
432
|
+
const reason = failure?.reason ?? "the probe failed";
|
|
433
|
+
entry.reason = credentialShapesIn(reason).length > 0 ? WITHHELD_REASON : reason;
|
|
434
|
+
}
|
|
435
|
+
probes.push(entry);
|
|
436
|
+
};
|
|
437
|
+
// Filesystem reads first, outside the signal-handler window: a synchronous
|
|
438
|
+
// call stuck on a network mount must stay interruptible by Ctrl-C.
|
|
439
|
+
const checkouts = new Map();
|
|
440
|
+
for (const repo of request.repos ?? []) {
|
|
441
|
+
checkouts.set(repo, ["private", "public"]
|
|
442
|
+
.map((tier) => path.join(deps.hqRoot, "repos", tier, repo))
|
|
443
|
+
.find((dir) => fs.existsSync(path.join(dir, ".git"))));
|
|
444
|
+
}
|
|
445
|
+
// The probes that start children run with the interrupt cleanup installed,
|
|
446
|
+
// so Ctrl-C kills their process groups. The data probes run after it is
|
|
447
|
+
// removed.
|
|
448
|
+
const releaseInterrupts = (deps.installInterrupts ?? (() => () => undefined))();
|
|
449
|
+
try {
|
|
450
|
+
// 1. Company vault grant.
|
|
451
|
+
{
|
|
452
|
+
const target = `companies/${company}/`;
|
|
453
|
+
const outcome = await deps.runHq(["files", "browse", target]);
|
|
454
|
+
const stderr = withoutNames(outcome.stderr, [company]);
|
|
455
|
+
push({
|
|
456
|
+
probe: `vault:${company}`,
|
|
457
|
+
test: "grant",
|
|
458
|
+
checks: `a vault listing of ${target} answers under this identity. Does not prove read access to any one file.`,
|
|
459
|
+
ok: outcome.code === 0 && !outcome.timedOut,
|
|
460
|
+
ttl_seconds: VAULT_TTL_SECONDS,
|
|
461
|
+
source: `hq env probe: hq files browse ${target} (exit code only)`,
|
|
462
|
+
failure: outcome.timedOut
|
|
463
|
+
? { result: "unknown", reason: "hq files browse timed out" }
|
|
464
|
+
: /forbidden|\b403\b|not a member|no access|denied|not allowed/i.test(stderr)
|
|
465
|
+
? { result: "fail", reason: `this identity has no vault grant for ${company}` }
|
|
466
|
+
: /sign[- ]?in|not logged in|unauthori[sz]ed|\b401\b|expired/i.test(stderr)
|
|
467
|
+
? { result: "unknown", reason: "no usable HQ sign-in for hq files browse" }
|
|
468
|
+
: { result: "fail", reason: `hq files browse ${target} exited ${outcome.code}` },
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
// 2. Repos.
|
|
472
|
+
for (const repo of request.repos ?? []) {
|
|
473
|
+
const checkout = checkouts.get(repo);
|
|
474
|
+
const base = {
|
|
475
|
+
probe: `repo:${repo}`,
|
|
476
|
+
test: "reachable",
|
|
477
|
+
checks: "the checkout's origin answers git ls-remote with this machine's git login (read access). Does not prove push access or branch freshness.",
|
|
478
|
+
ttl_seconds: REPO_TTL_SECONDS,
|
|
479
|
+
};
|
|
480
|
+
if (!checkout) {
|
|
481
|
+
push({
|
|
482
|
+
...base,
|
|
483
|
+
ok: false,
|
|
484
|
+
source: "hq env probe: look for repos/private or repos/public checkout",
|
|
485
|
+
failure: { result: "fail", reason: `no checkout named ${repo} under repos/private or repos/public of the HQ root` },
|
|
486
|
+
});
|
|
487
|
+
continue;
|
|
488
|
+
}
|
|
489
|
+
const tier = path.basename(path.dirname(checkout));
|
|
490
|
+
// Keep the user's ssh command (GIT_SSH_COMMAND, else the checkout's
|
|
491
|
+
// core.sshCommand, which git prefers over GIT_SSH) and add BatchMode,
|
|
492
|
+
// passing it as GIT_SSH_COMMAND. Only a bare GIT_SSH program with no
|
|
493
|
+
// core.sshCommand is left in charge: it takes no options, and git adds
|
|
494
|
+
// -batch itself when it is plink.
|
|
495
|
+
let existing = deps.gitSshCommand;
|
|
496
|
+
if (!existing) {
|
|
497
|
+
const configured = await deps.runGit(["-C", checkout, "config", "--get", "core.sshCommand"], { captureStdout: true });
|
|
498
|
+
if (configured.code === 0 && configured.stdout?.trim())
|
|
499
|
+
existing = configured.stdout.trim();
|
|
500
|
+
}
|
|
501
|
+
let sshEnv = {};
|
|
502
|
+
if (existing) {
|
|
503
|
+
const batched = batchSshCommand(existing);
|
|
504
|
+
if (batched) {
|
|
505
|
+
sshEnv = { GIT_SSH_COMMAND: batched };
|
|
506
|
+
}
|
|
507
|
+
else if (knownNonInteractiveSsh(existing)) {
|
|
508
|
+
sshEnv = { GIT_SSH_COMMAND: existing };
|
|
509
|
+
}
|
|
510
|
+
else {
|
|
511
|
+
push({
|
|
512
|
+
...base,
|
|
513
|
+
ok: false,
|
|
514
|
+
source: `hq env probe: git ssh command for repos/${tier}/${repo} (not run)`,
|
|
515
|
+
failure: {
|
|
516
|
+
result: "unknown",
|
|
517
|
+
reason: `the ssh command git uses for ${repo} does not name ssh and is not known to be non-interactive, so git ls-remote was not run`,
|
|
518
|
+
},
|
|
519
|
+
});
|
|
520
|
+
continue;
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
else if (!deps.gitSshProgram) {
|
|
524
|
+
sshEnv = { GIT_SSH_COMMAND: "ssh -o BatchMode=yes" };
|
|
525
|
+
}
|
|
526
|
+
const outcome = await deps.runGit(["-C", checkout, "ls-remote", "--exit-code", "origin", "HEAD"], { env: sshEnv });
|
|
527
|
+
push({
|
|
528
|
+
...base,
|
|
529
|
+
ok: outcome.code === 0 && !outcome.timedOut,
|
|
530
|
+
source: `hq env probe: git ls-remote --exit-code origin HEAD in repos/${tier}/${repo} (exit code only)`,
|
|
531
|
+
failure: classifyGitFailure(repo, outcome),
|
|
532
|
+
});
|
|
533
|
+
}
|
|
534
|
+
// 3. Secrets: exists (HEAD), then resolvable (inside hq secrets exec).
|
|
535
|
+
for (const name of request.secrets ?? []) {
|
|
536
|
+
const exists = await deps.runHq(["secrets", "--company", company, "exists", name, "--quiet"]);
|
|
537
|
+
const existsFailure = exists.timedOut
|
|
538
|
+
? { result: "unknown", reason: "hq secrets exists timed out" }
|
|
539
|
+
: exists.code === 1
|
|
540
|
+
? { result: "fail", reason: `${name} is absent from the ${company} vault` }
|
|
541
|
+
: { result: "unknown", reason: `hq secrets exists could not tell (exit ${exists.code})` };
|
|
542
|
+
const absent = !exists.timedOut && exists.code === 1;
|
|
543
|
+
push({
|
|
544
|
+
probe: `secret:${name}${SECRET_EXISTS_SUFFIX}`,
|
|
545
|
+
test: "exists",
|
|
546
|
+
checks: `name exists in the ${company} vault (HEAD). Does not prove this identity can resolve the value.`,
|
|
547
|
+
ok: exists.code === 0 && !exists.timedOut,
|
|
548
|
+
ttl_seconds: SECRET_TTL_SECONDS,
|
|
549
|
+
source: `hq env probe: hq secrets exists --company ${company} ${name} (exit code only)`,
|
|
550
|
+
failure: existsFailure,
|
|
551
|
+
});
|
|
552
|
+
const resolvableBase = {
|
|
553
|
+
probe: `secret:${name}${SECRET_RESOLVABLE_SUFFIX}`,
|
|
554
|
+
test: "resolvable",
|
|
555
|
+
checks: `resolvable under this identity: hq secrets exec fetched ${name} (keeping its usual encrypted cache) and injected it ` +
|
|
556
|
+
"into a child that checked only that it is non-empty. This command never received, printed, stored or wrote the value.",
|
|
557
|
+
ttl_seconds: SECRET_TTL_SECONDS,
|
|
558
|
+
source: `hq env probe: hq secrets exec --company ${company} --only ${name} -- node length check (exit code only)`,
|
|
559
|
+
};
|
|
560
|
+
if (absent) {
|
|
561
|
+
push({
|
|
562
|
+
...resolvableBase,
|
|
563
|
+
ok: false,
|
|
564
|
+
failure: { result: "fail", reason: `not attempted: ${name} is absent from the ${company} vault` },
|
|
565
|
+
});
|
|
566
|
+
continue;
|
|
567
|
+
}
|
|
568
|
+
const resolved = await deps.runHq([
|
|
569
|
+
"secrets",
|
|
570
|
+
"exec",
|
|
571
|
+
"--company",
|
|
572
|
+
company,
|
|
573
|
+
"--only",
|
|
574
|
+
name,
|
|
575
|
+
"--",
|
|
576
|
+
deps.nodeBinary,
|
|
577
|
+
"-e",
|
|
578
|
+
RESOLVE_CHECK_SCRIPT,
|
|
579
|
+
name,
|
|
580
|
+
]);
|
|
581
|
+
// A success served from the offline cache shows the value was once
|
|
582
|
+
// readable, not that this identity can read it now.
|
|
583
|
+
const fromCache = resolved.code === 0
|
|
584
|
+
&& !resolved.timedOut
|
|
585
|
+
&& withoutNames(resolved.stderr, [name]).includes(CACHED_SECRETS_SERVER_UNAVAILABLE_MESSAGE);
|
|
586
|
+
push({
|
|
587
|
+
...resolvableBase,
|
|
588
|
+
ok: resolved.code === 0 && !resolved.timedOut && !fromCache,
|
|
589
|
+
failure: fromCache ? { result: "unknown", reason: CACHED_RESULT_REASON } : classifyExecFailure(name, resolved, company),
|
|
590
|
+
});
|
|
591
|
+
}
|
|
592
|
+
// 4. Integrations: one usable-apps listing serves every slug.
|
|
593
|
+
if ((request.integrations ?? []).length > 0) {
|
|
594
|
+
const outcome = await deps.runHq(["integrations", "list", "--company", company, "--usable", "--json", "--no-login"], { captureStdout: true });
|
|
595
|
+
let apps = null;
|
|
596
|
+
let viewerAccessKnown = true;
|
|
597
|
+
if (outcome.code === 0 && !outcome.timedOut) {
|
|
598
|
+
try {
|
|
599
|
+
const parsed = JSON.parse(outcome.stdout ?? "");
|
|
600
|
+
if (Array.isArray(parsed.apps))
|
|
601
|
+
apps = parsed.apps;
|
|
602
|
+
viewerAccessKnown = parsed.viewerAccessKnown !== false;
|
|
603
|
+
}
|
|
604
|
+
catch {
|
|
605
|
+
apps = null;
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
for (const slug of request.integrations ?? []) {
|
|
609
|
+
const base = {
|
|
610
|
+
probe: `integration:${slug}`,
|
|
611
|
+
test: "usable",
|
|
612
|
+
checks: `the ${slug} app is connected in ${company} and shared with this identity (hq integrations list --usable). Does not call the app.`,
|
|
613
|
+
ttl_seconds: INTEGRATION_TTL_SECONDS,
|
|
614
|
+
source: `hq env probe: hq integrations list --company ${company} --usable --json (provider names only)`,
|
|
615
|
+
};
|
|
616
|
+
if (apps === null) {
|
|
617
|
+
push({
|
|
618
|
+
...base,
|
|
619
|
+
ok: false,
|
|
620
|
+
failure: {
|
|
621
|
+
result: "unknown",
|
|
622
|
+
reason: outcome.timedOut
|
|
623
|
+
? "hq integrations list timed out"
|
|
624
|
+
: outcome.code === 0
|
|
625
|
+
? "hq integrations list printed no usable-apps list"
|
|
626
|
+
: `hq integrations list exited ${outcome.code}`,
|
|
627
|
+
},
|
|
628
|
+
});
|
|
629
|
+
continue;
|
|
630
|
+
}
|
|
631
|
+
if (!viewerAccessKnown) {
|
|
632
|
+
push({
|
|
633
|
+
...base,
|
|
634
|
+
ok: false,
|
|
635
|
+
failure: { result: "unknown", reason: "this HQ server cannot report which apps are shared with this identity" },
|
|
636
|
+
});
|
|
637
|
+
continue;
|
|
638
|
+
}
|
|
639
|
+
const wanted = slug.toLowerCase();
|
|
640
|
+
const found = apps.some((app) => {
|
|
641
|
+
const arg = typeof app.providerArg === "string" ? app.providerArg.toLowerCase() : "";
|
|
642
|
+
const provider = typeof app.provider === "string" ? app.provider.toLowerCase() : "";
|
|
643
|
+
return arg === wanted || provider === wanted || provider === `factory:${wanted}`;
|
|
644
|
+
});
|
|
645
|
+
push({
|
|
646
|
+
...base,
|
|
647
|
+
ok: found,
|
|
648
|
+
failure: { result: "fail", reason: `${slug} is not connected in ${company}, or not shared with this identity` },
|
|
649
|
+
});
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
// 5. hq agent probe, reused for identity, vault and DMs.
|
|
653
|
+
if (request.agent) {
|
|
654
|
+
const outcome = await deps.runHq(["agent", "probe", "--json"], { captureStdout: true });
|
|
655
|
+
let checks = null;
|
|
656
|
+
try {
|
|
657
|
+
const parsed = JSON.parse(outcome.stdout ?? "");
|
|
658
|
+
if (Array.isArray(parsed.checks))
|
|
659
|
+
checks = parsed.checks;
|
|
660
|
+
}
|
|
661
|
+
catch {
|
|
662
|
+
checks = null;
|
|
663
|
+
}
|
|
664
|
+
if (!checks || checks.length === 0) {
|
|
665
|
+
push({
|
|
666
|
+
probe: "identity:agent",
|
|
667
|
+
test: "agent",
|
|
668
|
+
checks: "hq agent probe ran and reported its checks (identity, vault, presence, DMs, secrets list).",
|
|
669
|
+
ok: false,
|
|
670
|
+
ttl_seconds: IDENTITY_TTL_SECONDS,
|
|
671
|
+
source: "hq env probe: hq agent probe --json",
|
|
672
|
+
failure: {
|
|
673
|
+
result: "unknown",
|
|
674
|
+
reason: checks
|
|
675
|
+
? "hq agent probe reported no checks"
|
|
676
|
+
: outcome.timedOut
|
|
677
|
+
? "hq agent probe timed out"
|
|
678
|
+
: `hq agent probe exited ${outcome.code} without a report; it needs an enrolled external agent kit on this host`,
|
|
679
|
+
},
|
|
680
|
+
});
|
|
681
|
+
}
|
|
682
|
+
else {
|
|
683
|
+
for (const check of checks) {
|
|
684
|
+
const name = typeof check.name === "string" && /^[a-z0-9-]{1,40}$/.test(check.name) ? check.name : "other";
|
|
685
|
+
push({
|
|
686
|
+
probe: AGENT_CHECK_NAMES[name] ?? `identity:agent/${name}`,
|
|
687
|
+
test: "agent",
|
|
688
|
+
checks: AGENT_CHECK_SENTENCES[name] ?? `hq agent probe check ${name}.`,
|
|
689
|
+
ok: check.ok === true,
|
|
690
|
+
ttl_seconds: IDENTITY_TTL_SECONDS,
|
|
691
|
+
source: `hq env probe: hq agent probe --json (check ${name})`,
|
|
692
|
+
// The check's detail text is never copied: it can name a company
|
|
693
|
+
// slug or uid, and the reason is printed and stored.
|
|
694
|
+
failure: { result: "fail", reason: `hq agent probe reported that its ${name} check did not pass` },
|
|
695
|
+
});
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
finally {
|
|
701
|
+
releaseInterrupts();
|
|
702
|
+
}
|
|
703
|
+
// 6. Local data, after the interrupt cleanup is removed. The real path is checked as well as the name: a path that
|
|
704
|
+
// resolves into companies/ must resolve into companies/<company>/.
|
|
705
|
+
for (const relative of request.data ?? []) {
|
|
706
|
+
const absolute = path.join(deps.hqRoot, ...relative.split("/"));
|
|
707
|
+
let failure;
|
|
708
|
+
try {
|
|
709
|
+
const real = fs.realpathSync.native(absolute);
|
|
710
|
+
const scope = dataScopeFailure(deps.hqRoot, company, relative, real);
|
|
711
|
+
if (scope) {
|
|
712
|
+
failure = { result: "fail", reason: scope };
|
|
713
|
+
}
|
|
714
|
+
else {
|
|
715
|
+
const stat = fs.statSync(real);
|
|
716
|
+
if (stat.isDirectory()) {
|
|
717
|
+
if (fs.readdirSync(real).length === 0)
|
|
718
|
+
failure = { result: "fail", reason: `${relative} is an empty folder` };
|
|
719
|
+
}
|
|
720
|
+
else if (stat.size === 0) {
|
|
721
|
+
failure = { result: "fail", reason: `${relative} is an empty file` };
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
catch (error) {
|
|
726
|
+
const code = error.code;
|
|
727
|
+
failure = code === "ENOENT"
|
|
728
|
+
? { result: "fail", reason: `${relative} is not present under the HQ root` }
|
|
729
|
+
: { result: "unknown", reason: `${relative} could not be read (${code ?? "error"})` };
|
|
730
|
+
}
|
|
731
|
+
push({
|
|
732
|
+
probe: `data:${relative}`,
|
|
733
|
+
test: "present",
|
|
734
|
+
checks: `${relative} is present under this HQ root and not empty. Freshness is not checked.`,
|
|
735
|
+
ok: failure === undefined,
|
|
736
|
+
ttl_seconds: DATA_TTL_SECONDS,
|
|
737
|
+
source: `hq env probe: stat ${relative} under the HQ root`,
|
|
738
|
+
...(failure ? { failure } : {}),
|
|
739
|
+
});
|
|
740
|
+
}
|
|
741
|
+
return { ok: probes.every((p) => p.ok), company, observed_at: startedAt, probes };
|
|
742
|
+
}
|
|
743
|
+
const liveGroups = new Set();
|
|
744
|
+
let exitHookInstalled = false;
|
|
745
|
+
function killGroup(child) {
|
|
746
|
+
if (child.pid === undefined)
|
|
747
|
+
return;
|
|
748
|
+
try {
|
|
749
|
+
// A negative pid names the process group the detached child leads.
|
|
750
|
+
process.kill(-child.pid, "SIGKILL");
|
|
751
|
+
}
|
|
752
|
+
catch {
|
|
753
|
+
try {
|
|
754
|
+
child.kill("SIGKILL");
|
|
755
|
+
}
|
|
756
|
+
catch { /* already gone */ }
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
/** Kill every process group this module started and has not seen exit. */
|
|
760
|
+
export function killLiveGroups(kill = process.kill) {
|
|
761
|
+
let killed = 0;
|
|
762
|
+
for (const pid of [...liveGroups]) {
|
|
763
|
+
try {
|
|
764
|
+
kill(-pid, "SIGKILL");
|
|
765
|
+
killed += 1;
|
|
766
|
+
}
|
|
767
|
+
catch { /* already gone */ }
|
|
768
|
+
liveGroups.delete(pid);
|
|
769
|
+
}
|
|
770
|
+
return killed;
|
|
771
|
+
}
|
|
772
|
+
export const INTERRUPT_SIGNALS = ["SIGINT", "SIGTERM", "SIGHUP"];
|
|
773
|
+
const processSignalHost = {
|
|
774
|
+
on: (signal, handler) => process.on(signal, handler),
|
|
775
|
+
off: (signal, handler) => process.off(signal, handler),
|
|
776
|
+
raise: (signal) => process.kill(process.pid, signal),
|
|
777
|
+
kill: (pid, signal) => process.kill(pid, signal),
|
|
778
|
+
};
|
|
779
|
+
/**
|
|
780
|
+
* While a probe run is in flight, SIGINT, SIGTERM and SIGHUP kill every
|
|
781
|
+
* child process group first (the children are detached, so the terminal's
|
|
782
|
+
* Ctrl-C does not reach them, and one may be an `hq agent probe` about to
|
|
783
|
+
* send its DM), then the handlers are removed and the signal is raised again,
|
|
784
|
+
* so the process ends the way it would have without them. Call the returned
|
|
785
|
+
* function when the run finishes.
|
|
786
|
+
*/
|
|
787
|
+
export function installInterruptCleanup(host = processSignalHost) {
|
|
788
|
+
let removed = false;
|
|
789
|
+
const remove = () => {
|
|
790
|
+
if (removed)
|
|
791
|
+
return;
|
|
792
|
+
removed = true;
|
|
793
|
+
for (const signal of INTERRUPT_SIGNALS)
|
|
794
|
+
host.off(signal, handler);
|
|
795
|
+
};
|
|
796
|
+
function handler(signal) {
|
|
797
|
+
killLiveGroups(host.kill);
|
|
798
|
+
remove();
|
|
799
|
+
host.raise(signal);
|
|
800
|
+
}
|
|
801
|
+
for (const signal of INTERRUPT_SIGNALS)
|
|
802
|
+
host.on(signal, handler);
|
|
803
|
+
return remove;
|
|
804
|
+
}
|
|
805
|
+
/**
|
|
806
|
+
* Spawn a child in its own process group with bounded capture. stdout is
|
|
807
|
+
* discarded unless asked for. On timeout the whole group is killed, so a
|
|
808
|
+
* grandchild (the check under `hq secrets exec`, or an ssh under git) cannot
|
|
809
|
+
* outlive it.
|
|
810
|
+
*/
|
|
811
|
+
export function runChild(file, args, options = {}) {
|
|
812
|
+
if (!exitHookInstalled) {
|
|
813
|
+
exitHookInstalled = true;
|
|
814
|
+
process.once("exit", () => {
|
|
815
|
+
for (const pid of liveGroups) {
|
|
816
|
+
try {
|
|
817
|
+
process.kill(-pid, "SIGKILL");
|
|
818
|
+
}
|
|
819
|
+
catch { /* already gone */ }
|
|
820
|
+
}
|
|
821
|
+
});
|
|
822
|
+
}
|
|
823
|
+
return new Promise((resolve) => {
|
|
824
|
+
let stderr = "";
|
|
825
|
+
let stdout = "";
|
|
826
|
+
let settled = false;
|
|
827
|
+
let timedOut = false;
|
|
828
|
+
const child = spawn(file, args, {
|
|
829
|
+
env: options.env ?? process.env,
|
|
830
|
+
stdio: ["ignore", options.captureStdout ? "pipe" : "ignore", "pipe"],
|
|
831
|
+
detached: process.platform !== "win32",
|
|
832
|
+
});
|
|
833
|
+
if (child.pid !== undefined && process.platform !== "win32")
|
|
834
|
+
liveGroups.add(child.pid);
|
|
835
|
+
const timer = setTimeout(() => {
|
|
836
|
+
timedOut = true;
|
|
837
|
+
killGroup(child);
|
|
838
|
+
}, options.timeoutMs ?? CHILD_TIMEOUT_MS);
|
|
839
|
+
function finish(code) {
|
|
840
|
+
if (settled)
|
|
841
|
+
return;
|
|
842
|
+
settled = true;
|
|
843
|
+
clearTimeout(timer);
|
|
844
|
+
if (child.pid !== undefined)
|
|
845
|
+
liveGroups.delete(child.pid);
|
|
846
|
+
resolve({
|
|
847
|
+
code,
|
|
848
|
+
stderr,
|
|
849
|
+
...(options.captureStdout ? { stdout } : {}),
|
|
850
|
+
...(timedOut ? { timedOut: true } : {}),
|
|
851
|
+
});
|
|
852
|
+
}
|
|
853
|
+
child.stderr?.setEncoding("utf8");
|
|
854
|
+
child.stderr?.on("data", (chunk) => {
|
|
855
|
+
if (stderr.length < STDERR_CAP_BYTES)
|
|
856
|
+
stderr += chunk.slice(0, STDERR_CAP_BYTES - stderr.length);
|
|
857
|
+
});
|
|
858
|
+
child.stdout?.setEncoding("utf8");
|
|
859
|
+
child.stdout?.on("data", (chunk) => {
|
|
860
|
+
if (stdout.length < STDOUT_CAP_BYTES)
|
|
861
|
+
stdout += chunk.slice(0, STDOUT_CAP_BYTES - stdout.length);
|
|
862
|
+
});
|
|
863
|
+
child.on("error", () => finish(127));
|
|
864
|
+
child.on("close", (code) => finish(code ?? 1));
|
|
865
|
+
});
|
|
866
|
+
}
|
|
867
|
+
/** Set together with HQ_ENV_PROBE_TEST_HQ_ENTRY by the built-CLI test; nothing else sets it. */
|
|
868
|
+
export const ENV_PROBE_TEST_STUB_ENV = "HQ_ENV_PROBE_TEST_STUB";
|
|
869
|
+
export const ENV_PROBE_TEST_HQ_ENTRY_ENV = "HQ_ENV_PROBE_TEST_HQ_ENTRY";
|
|
870
|
+
/**
|
|
871
|
+
* The hq entry the probes run as children. The built-CLI test swaps in a fake
|
|
872
|
+
* hq (a fake vault) with HQ_ENV_PROBE_TEST_HQ_ENTRY, honoured only when
|
|
873
|
+
* HQ_ENV_PROBE_TEST_STUB=1 is set too (the HQ_JOB_TEST_SCHEDULER_STUB pattern).
|
|
874
|
+
*/
|
|
875
|
+
export function hqEntryFor(env, argv1) {
|
|
876
|
+
const stub = env[ENV_PROBE_TEST_HQ_ENTRY_ENV]?.trim();
|
|
877
|
+
if (stub && env[ENV_PROBE_TEST_STUB_ENV] === "1")
|
|
878
|
+
return stub;
|
|
879
|
+
return argv1 || "hq";
|
|
880
|
+
}
|
|
881
|
+
export function defaultAccessProbeDeps(hqRoot, env = process.env) {
|
|
882
|
+
const entry = hqEntryFor(env, process.argv[1]);
|
|
883
|
+
// No hq child may open a browser sign-in; a missing sign-in is a result.
|
|
884
|
+
const hqEnv = { ...env, HQ_NO_BROWSER_LOGIN: "1" };
|
|
885
|
+
const gitEnv = gitProbeEnv(env);
|
|
886
|
+
return {
|
|
887
|
+
hqRoot,
|
|
888
|
+
nodeBinary: process.execPath,
|
|
889
|
+
installInterrupts: () => installInterruptCleanup(),
|
|
890
|
+
...(env.GIT_SSH_COMMAND?.trim() ? { gitSshCommand: env.GIT_SSH_COMMAND.trim() } : {}),
|
|
891
|
+
...(env.GIT_SSH?.trim() ? { gitSshProgram: env.GIT_SSH.trim() } : {}),
|
|
892
|
+
runHq: (args, options) => runChild(process.execPath, [entry, ...args], {
|
|
893
|
+
env: { ...hqEnv, ...options?.env },
|
|
894
|
+
captureStdout: options?.captureStdout,
|
|
895
|
+
}),
|
|
896
|
+
runGit: (args, options) => {
|
|
897
|
+
// The probe decides GIT_SSH_COMMAND; the askpass settings always win.
|
|
898
|
+
const merged = { ...gitEnv, ...options?.env, ...gitProbeEnv({}) };
|
|
899
|
+
if (!options?.env?.GIT_SSH_COMMAND)
|
|
900
|
+
delete merged.GIT_SSH_COMMAND;
|
|
901
|
+
return runChild("git", args, { env: merged, captureStdout: options?.captureStdout, timeoutMs: GIT_TIMEOUT_MS });
|
|
902
|
+
},
|
|
903
|
+
};
|
|
904
|
+
}
|
|
905
|
+
/** The company slug of this host's external agent kit, or null when there is none. */
|
|
906
|
+
export async function agentKitCompany(env = process.env) {
|
|
907
|
+
try {
|
|
908
|
+
const os = await import("node:os");
|
|
909
|
+
const { agentKitPaths } = await import("../agent-kit/paths.js");
|
|
910
|
+
const { readExternalMachineCreds } = await import("../agent-kit/creds.js");
|
|
911
|
+
const creds = readExternalMachineCreds(agentKitPaths(os.homedir(), env));
|
|
912
|
+
return creds?.companySlug ?? null;
|
|
913
|
+
}
|
|
914
|
+
catch {
|
|
915
|
+
return null;
|
|
916
|
+
}
|
|
917
|
+
}
|
|
918
|
+
//# sourceMappingURL=access-probes.js.map
|