@installagent/sdk 0.6.0 → 0.8.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/dist/access-check.d.ts +32 -0
- package/dist/access-check.js +339 -0
- package/dist/client-manifest.d.ts +32 -0
- package/dist/client-manifest.js +64 -0
- package/dist/doctor.js +94 -0
- package/dist/env-fill.js +3 -35
- package/dist/env-push.js +3 -16
- package/dist/gcp-dwd.d.ts +73 -0
- package/dist/gcp-dwd.js +269 -0
- package/dist/gcp-inspect.d.ts +126 -0
- package/dist/gcp-inspect.js +292 -0
- package/dist/gcp-service-account-key.d.ts +2 -0
- package/dist/gcp-service-account-key.js +1 -0
- package/dist/gcp-setup.d.ts +58 -0
- package/dist/gcp-setup.js +318 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +6 -0
- package/dist/op-cli.d.ts +14 -0
- package/dist/op-cli.js +35 -3
- package/dist/op-store.d.ts +60 -0
- package/dist/op-store.js +230 -0
- package/dist/pr-open.d.ts +3 -1
- package/dist/pr-open.js +9 -0
- package/dist/template-sync.d.ts +10 -0
- package/dist/template-sync.js +9 -0
- package/package.json +1 -1
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { type ExecFileFn } from "./exec.js";
|
|
2
|
+
import { type PlatformInfo } from "./platform.js";
|
|
3
|
+
export type AccessService = "google-service-account" | "google-oauth" | "google-oauth-client" | "slack" | "github";
|
|
4
|
+
export type AccessStatus = "valid" | "rejected" | "unverified" | "skipped";
|
|
5
|
+
export interface AccessCheckItem {
|
|
6
|
+
/** The vault item's title — a name, never a value. */
|
|
7
|
+
title: string;
|
|
8
|
+
service: AccessService | null;
|
|
9
|
+
status: AccessStatus;
|
|
10
|
+
detail: string;
|
|
11
|
+
}
|
|
12
|
+
export interface AccessCheckResult {
|
|
13
|
+
slug: string;
|
|
14
|
+
vault: string;
|
|
15
|
+
items: AccessCheckItem[];
|
|
16
|
+
counts: Record<AccessStatus, number>;
|
|
17
|
+
}
|
|
18
|
+
export interface AccessCheckOptions {
|
|
19
|
+
workspaceDir?: string;
|
|
20
|
+
/** Test this vault instead of the one in CLIENT.md (use when CLIENT.md names a vault that does not exist). */
|
|
21
|
+
vault?: string;
|
|
22
|
+
execFileImpl?: ExecFileFn;
|
|
23
|
+
fetchImpl?: typeof fetch;
|
|
24
|
+
now?: () => number;
|
|
25
|
+
platform?: PlatformInfo;
|
|
26
|
+
}
|
|
27
|
+
/** The vault named in the client's CLIENT.md, else the portal's default name. */
|
|
28
|
+
export declare function clientVaultFor(slug: string, workspaceDir: string): string;
|
|
29
|
+
/** What a key is, from the shape of its value. Null when no probe exists for it. */
|
|
30
|
+
export declare function classifyKey(value: string): AccessService | null;
|
|
31
|
+
/** Test every API-key item in the client's vault. Logins and unknown keys are listed, never read or tested. */
|
|
32
|
+
export declare function accessCheck(slug: string, options?: AccessCheckOptions): Promise<AccessCheckResult>;
|
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { join, resolve } from "node:path";
|
|
3
|
+
import { readManifestKey } from "./client-manifest.js";
|
|
4
|
+
import { findWorkspaceRoot } from "./env-file.js";
|
|
5
|
+
import { execFailureDetail, execFileAsync } from "./exec.js";
|
|
6
|
+
import { GoogleServiceAccountAuthError, getServiceAccountAccessToken, } from "./google-service-account.js";
|
|
7
|
+
import { SLUG_PATTERN } from "./new-client.js";
|
|
8
|
+
import { opReachable } from "./op-cli.js";
|
|
9
|
+
import { detectPlatformInfo } from "./platform.js";
|
|
10
|
+
// `ia access check <slug>`: does each key the client handed over still work?
|
|
11
|
+
// The access tracker says HAVE from a person's word; nothing tested it, so a
|
|
12
|
+
// token that expired or was revoked still read HAVE. This runs ONE read-only
|
|
13
|
+
// call per key in the client's 1Password vault (the same idea as the live
|
|
14
|
+
// checks in `ia doctor`) and reports valid, rejected or unverified.
|
|
15
|
+
//
|
|
16
|
+
// Secret handling: only the vault's item list (titles, ids, categories) is
|
|
17
|
+
// read in bulk. The `credential` field is read from API-key items, and a small
|
|
18
|
+
// .json file from Document items (where `ia secrets mint` stores a Google
|
|
19
|
+
// key). A value is kept in a local variable, sent to its own service, and
|
|
20
|
+
// dropped. A result holds
|
|
21
|
+
// fixed words and, at most, a workspace name or an expiry date. Logins
|
|
22
|
+
// (username + password) are never read: no safe test exists for them.
|
|
23
|
+
const CHECK_TIMEOUT_MS = 5000;
|
|
24
|
+
// A service-account key file is about 2.4 KB; anything past this is not one.
|
|
25
|
+
const KEY_FILE_MAX_BYTES = 16 * 1024;
|
|
26
|
+
const GOOGLE_TOKEN_URL = "https://oauth2.googleapis.com/token";
|
|
27
|
+
// The narrowest scope that still mints a token. A service account may ask for
|
|
28
|
+
// any scope for itself (no delegation), so this proves the key and nothing more.
|
|
29
|
+
const GOOGLE_PROBE_SCOPE = "https://www.googleapis.com/auth/userinfo.email";
|
|
30
|
+
/** `op item list --format json` reports "API_CREDENTIAL"; the SDK names it "ApiCredentials". */
|
|
31
|
+
const categoryKey = (category) => category.replace(/[^a-z]/gi, "").toUpperCase();
|
|
32
|
+
/** The vault named in the client's CLIENT.md, else the portal's default name. */
|
|
33
|
+
export function clientVaultFor(slug, workspaceDir) {
|
|
34
|
+
const clientMd = join(workspaceDir, "clients", `client-${slug}`, "CLIENT.md");
|
|
35
|
+
const declared = existsSync(clientMd)
|
|
36
|
+
? readManifestKey(readFileSync(clientMd, "utf8"), "onepassword_vault")
|
|
37
|
+
: undefined;
|
|
38
|
+
// Real CLIENT.md lines carry a note after the name: "client-x # convention", "client-x (created …)".
|
|
39
|
+
const name = declared?.split(/\s+(?:#|\(|—|–)/)[0].trim();
|
|
40
|
+
const usable = name && !/^<.*>$/.test(name) && !name.includes("_HERE");
|
|
41
|
+
return usable && name.toLowerCase() !== "none" ? name : `client-${slug}`;
|
|
42
|
+
}
|
|
43
|
+
/** What a key is, from the shape of its value. Null when no probe exists for it. */
|
|
44
|
+
export function classifyKey(value) {
|
|
45
|
+
const text = value.trim();
|
|
46
|
+
if (text.startsWith("{")) {
|
|
47
|
+
try {
|
|
48
|
+
const json = JSON.parse(text);
|
|
49
|
+
if (json.type === "service_account" && json.client_email && json.private_key) {
|
|
50
|
+
return "google-service-account";
|
|
51
|
+
}
|
|
52
|
+
if (json.client_id && json.client_secret && json.refresh_token)
|
|
53
|
+
return "google-oauth";
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
// The portal intake stores an OAuth client as username = client id, credential = secret.
|
|
61
|
+
if (/^GOCSPX-/.test(text))
|
|
62
|
+
return "google-oauth-client";
|
|
63
|
+
if (/^xox[abp]-/.test(text))
|
|
64
|
+
return "slack";
|
|
65
|
+
if (/^(ghp_|gho_|ghu_|ghs_|github_pat_)/.test(text))
|
|
66
|
+
return "github";
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
const unverified = (detail) => ({ status: "unverified", detail });
|
|
70
|
+
async function probeSlack(token, fetchImpl) {
|
|
71
|
+
const res = await fetchImpl("https://slack.com/api/auth.test", {
|
|
72
|
+
method: "POST",
|
|
73
|
+
headers: { Authorization: `Bearer ${token}` },
|
|
74
|
+
signal: AbortSignal.timeout(CHECK_TIMEOUT_MS),
|
|
75
|
+
});
|
|
76
|
+
if (res.status === 429)
|
|
77
|
+
return unverified("Slack is rate limiting; try again in a minute");
|
|
78
|
+
const body = (await res.json());
|
|
79
|
+
if (body.ok)
|
|
80
|
+
return {
|
|
81
|
+
status: "valid",
|
|
82
|
+
detail: `Slack accepts it${body.team ? ` (workspace ${body.team})` : ""}`,
|
|
83
|
+
};
|
|
84
|
+
const dead = ["invalid_auth", "token_revoked", "token_expired", "account_inactive", "not_authed"];
|
|
85
|
+
if (body.error && dead.includes(body.error)) {
|
|
86
|
+
return {
|
|
87
|
+
status: "rejected",
|
|
88
|
+
detail: `Slack rejected it (${body.error}); ask the client for a new token`,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
return unverified(`Slack gave an answer this check does not know (${body.error ?? `HTTP ${res.status}`})`);
|
|
92
|
+
}
|
|
93
|
+
async function probeGithub(token, fetchImpl) {
|
|
94
|
+
// /rate_limit works for every kind of token and needs no scope. It answers
|
|
95
|
+
// 200 with NO token too, so the 401 is the signal and the header is the proof.
|
|
96
|
+
const res = await fetchImpl("https://api.github.com/rate_limit", {
|
|
97
|
+
headers: {
|
|
98
|
+
Authorization: `Bearer ${token}`,
|
|
99
|
+
Accept: "application/vnd.github+json",
|
|
100
|
+
"User-Agent": "ia-access-check",
|
|
101
|
+
},
|
|
102
|
+
signal: AbortSignal.timeout(CHECK_TIMEOUT_MS),
|
|
103
|
+
});
|
|
104
|
+
if (res.status === 401) {
|
|
105
|
+
return {
|
|
106
|
+
status: "rejected",
|
|
107
|
+
detail: "GitHub rejected it (HTTP 401); the token expired or was revoked",
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
if (!res.ok)
|
|
111
|
+
return unverified(`GitHub gave HTTP ${res.status}`);
|
|
112
|
+
const expiry = res.headers.get("github-authentication-token-expiration");
|
|
113
|
+
return {
|
|
114
|
+
status: "valid",
|
|
115
|
+
detail: `GitHub accepts it${expiry ? ` (expires ${expiry.slice(0, 10)})` : " (no expiry date)"}`,
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
async function probeGoogleServiceAccount(raw, fetchImpl, now) {
|
|
119
|
+
try {
|
|
120
|
+
await getServiceAccountAccessToken(raw, GOOGLE_PROBE_SCOPE, fetchImpl, now);
|
|
121
|
+
return { status: "valid", detail: "Google accepts the service-account key" };
|
|
122
|
+
}
|
|
123
|
+
catch (error) {
|
|
124
|
+
if (error instanceof GoogleServiceAccountAuthError) {
|
|
125
|
+
const http = /HTTP (\d{3})/.exec(error.message)?.[1];
|
|
126
|
+
if (http && (http.startsWith("5") || http === "429"))
|
|
127
|
+
return unverified(`Google gave HTTP ${http}`);
|
|
128
|
+
return {
|
|
129
|
+
status: "rejected",
|
|
130
|
+
detail: `Google rejected the service-account key${http ? ` (HTTP ${http})` : ""}; the key was deleted or disabled`,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
// Name only: a message can echo request data.
|
|
134
|
+
return unverified(`could not finish the check (${error instanceof Error ? error.name : "error"})`);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Tests the OAuth client pair, not a refresh token (the vault rarely holds one;
|
|
139
|
+
* n8n does). A refresh request with a dummy token answers `invalid_grant` when
|
|
140
|
+
* Google accepts the client id and secret, and `invalid_client` when it does not.
|
|
141
|
+
*/
|
|
142
|
+
async function probeGoogleOauthClient(clientId, clientSecret, fetchImpl) {
|
|
143
|
+
const res = await fetchImpl(GOOGLE_TOKEN_URL, {
|
|
144
|
+
method: "POST",
|
|
145
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
146
|
+
body: new URLSearchParams({
|
|
147
|
+
grant_type: "refresh_token",
|
|
148
|
+
client_id: clientId,
|
|
149
|
+
client_secret: clientSecret,
|
|
150
|
+
refresh_token: "ia-access-check",
|
|
151
|
+
}),
|
|
152
|
+
signal: AbortSignal.timeout(CHECK_TIMEOUT_MS),
|
|
153
|
+
});
|
|
154
|
+
const body = (await res.json().catch(() => ({})));
|
|
155
|
+
if (body.error === "invalid_grant") {
|
|
156
|
+
return {
|
|
157
|
+
status: "valid",
|
|
158
|
+
detail: "Google accepts the OAuth client id and secret. The refresh token lives in n8n and is not tested here",
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
if (body.error === "invalid_client") {
|
|
162
|
+
return {
|
|
163
|
+
status: "rejected",
|
|
164
|
+
detail: "Google rejected the OAuth client id or secret (invalid_client); the secret was reset or the client deleted",
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
return unverified(`Google gave an answer this check does not know (${body.error ?? `HTTP ${res.status}`})`);
|
|
168
|
+
}
|
|
169
|
+
async function probeGoogleOauth(raw, fetchImpl) {
|
|
170
|
+
const json = JSON.parse(raw);
|
|
171
|
+
const res = await fetchImpl(GOOGLE_TOKEN_URL, {
|
|
172
|
+
method: "POST",
|
|
173
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
174
|
+
body: new URLSearchParams({
|
|
175
|
+
grant_type: "refresh_token",
|
|
176
|
+
client_id: json.client_id,
|
|
177
|
+
client_secret: json.client_secret,
|
|
178
|
+
refresh_token: json.refresh_token,
|
|
179
|
+
}),
|
|
180
|
+
signal: AbortSignal.timeout(CHECK_TIMEOUT_MS),
|
|
181
|
+
});
|
|
182
|
+
if (res.ok)
|
|
183
|
+
return { status: "valid", detail: "Google accepts the refresh token" };
|
|
184
|
+
const body = (await res.json().catch(() => ({})));
|
|
185
|
+
if (body.error === "invalid_grant") {
|
|
186
|
+
return {
|
|
187
|
+
status: "rejected",
|
|
188
|
+
detail: "Google rejected the refresh token (invalid_grant): it expired or was revoked. " +
|
|
189
|
+
"An OAuth consent screen in Testing status expires it after 7 days",
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
if (body.error === "invalid_client") {
|
|
193
|
+
return {
|
|
194
|
+
status: "rejected",
|
|
195
|
+
detail: "Google rejected the OAuth client id or secret (invalid_client)",
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
return unverified(`Google gave an answer this check does not know (${body.error ?? `HTTP ${res.status}`})`);
|
|
199
|
+
}
|
|
200
|
+
/** Test every API-key item in the client's vault. Logins and unknown keys are listed, never read or tested. */
|
|
201
|
+
export async function accessCheck(slug, options = {}) {
|
|
202
|
+
if (!SLUG_PATTERN.test(slug))
|
|
203
|
+
throw new Error(`"${slug}" is not a client slug (lowercase letters, digits, hyphens)`);
|
|
204
|
+
const workspaceDir = resolve(options.workspaceDir ?? findWorkspaceRoot() ?? ".");
|
|
205
|
+
const execFileImpl = options.execFileImpl ?? execFileAsync;
|
|
206
|
+
const fetchImpl = options.fetchImpl ?? fetch;
|
|
207
|
+
const now = options.now ?? Date.now;
|
|
208
|
+
const vault = options.vault?.trim() || clientVaultFor(slug, workspaceDir);
|
|
209
|
+
if (!/^[^\n/]{1,100}$/.test(vault))
|
|
210
|
+
throw new Error("the vault name must be one line, at most 100 characters, with no slash");
|
|
211
|
+
const reachable = await opReachable(execFileImpl, options.platform ?? detectPlatformInfo());
|
|
212
|
+
if (reachable !== true)
|
|
213
|
+
throw new Error(`1Password is not usable: ${reachable}`);
|
|
214
|
+
let listed;
|
|
215
|
+
try {
|
|
216
|
+
const { stdout } = await execFileImpl("op", [
|
|
217
|
+
"item",
|
|
218
|
+
"list",
|
|
219
|
+
"--vault",
|
|
220
|
+
vault,
|
|
221
|
+
"--format",
|
|
222
|
+
"json",
|
|
223
|
+
]);
|
|
224
|
+
listed = JSON.parse(stdout);
|
|
225
|
+
}
|
|
226
|
+
catch (error) {
|
|
227
|
+
throw new Error(`could not list the 1Password vault "${vault}": ${execFailureDetail(error).detail}`);
|
|
228
|
+
}
|
|
229
|
+
const items = [];
|
|
230
|
+
for (const item of listed) {
|
|
231
|
+
const base = { title: item.title, service: null };
|
|
232
|
+
const kind = categoryKey(item.category ?? "");
|
|
233
|
+
if (kind === "LOGIN" || kind === "PASSWORD") {
|
|
234
|
+
items.push({
|
|
235
|
+
...base,
|
|
236
|
+
status: "skipped",
|
|
237
|
+
detail: "login or password: no safe test; confirm it by hand",
|
|
238
|
+
});
|
|
239
|
+
continue;
|
|
240
|
+
}
|
|
241
|
+
if (kind !== "APICREDENTIAL" && kind !== "DOCUMENT") {
|
|
242
|
+
items.push({
|
|
243
|
+
...base,
|
|
244
|
+
status: "skipped",
|
|
245
|
+
detail: "not an API-key or key-file item; this check does not read it",
|
|
246
|
+
});
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
let value;
|
|
250
|
+
try {
|
|
251
|
+
if (kind === "DOCUMENT") {
|
|
252
|
+
// `ia secrets mint gcp-service-account-key --to-op` stores the key as a
|
|
253
|
+
// Document. Look at the file list first (metadata only); read the file
|
|
254
|
+
// only when it is a small .json, so no contract or PDF is ever pulled in.
|
|
255
|
+
const { stdout: meta } = await execFileImpl("op", [
|
|
256
|
+
"item",
|
|
257
|
+
"get",
|
|
258
|
+
item.id,
|
|
259
|
+
"--vault",
|
|
260
|
+
vault,
|
|
261
|
+
"--format",
|
|
262
|
+
"json",
|
|
263
|
+
]);
|
|
264
|
+
const files = JSON.parse(meta).files ?? [];
|
|
265
|
+
const keyFile = files.find((file) => /\.json$/i.test(file.name ?? "") && (file.size ?? 0) <= KEY_FILE_MAX_BYTES);
|
|
266
|
+
if (!keyFile) {
|
|
267
|
+
items.push({
|
|
268
|
+
...base,
|
|
269
|
+
status: "skipped",
|
|
270
|
+
detail: "a document, but not a small .json key file",
|
|
271
|
+
});
|
|
272
|
+
continue;
|
|
273
|
+
}
|
|
274
|
+
const { stdout } = await execFileImpl("op", ["document", "get", item.id, "--vault", vault]);
|
|
275
|
+
value = stdout.trim();
|
|
276
|
+
}
|
|
277
|
+
else {
|
|
278
|
+
const { stdout } = await execFileImpl("op", [
|
|
279
|
+
"read",
|
|
280
|
+
`op://${vault}/${item.id}/credential`,
|
|
281
|
+
]);
|
|
282
|
+
value = stdout.trim();
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
catch {
|
|
286
|
+
items.push({
|
|
287
|
+
...base,
|
|
288
|
+
status: "unverified",
|
|
289
|
+
detail: "could not read the key from 1Password",
|
|
290
|
+
});
|
|
291
|
+
continue;
|
|
292
|
+
}
|
|
293
|
+
const service = classifyKey(value);
|
|
294
|
+
if (!service) {
|
|
295
|
+
items.push({ ...base, status: "skipped", detail: "no test exists for this kind of key" });
|
|
296
|
+
continue;
|
|
297
|
+
}
|
|
298
|
+
let clientId = "";
|
|
299
|
+
if (service === "google-oauth-client") {
|
|
300
|
+
try {
|
|
301
|
+
const { stdout } = await execFileImpl("op", ["read", `op://${vault}/${item.id}/username`]);
|
|
302
|
+
clientId = stdout.trim();
|
|
303
|
+
}
|
|
304
|
+
catch {
|
|
305
|
+
// falls through to the skip below
|
|
306
|
+
}
|
|
307
|
+
if (!clientId.endsWith(".apps.googleusercontent.com")) {
|
|
308
|
+
items.push({
|
|
309
|
+
title: item.title,
|
|
310
|
+
service,
|
|
311
|
+
status: "skipped",
|
|
312
|
+
detail: "an OAuth client secret with no client id in the username field",
|
|
313
|
+
});
|
|
314
|
+
continue;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
let verdict;
|
|
318
|
+
try {
|
|
319
|
+
verdict =
|
|
320
|
+
service === "google-oauth-client"
|
|
321
|
+
? await probeGoogleOauthClient(clientId, value, fetchImpl)
|
|
322
|
+
: service === "slack"
|
|
323
|
+
? await probeSlack(value, fetchImpl)
|
|
324
|
+
: service === "github"
|
|
325
|
+
? await probeGithub(value, fetchImpl)
|
|
326
|
+
: service === "google-service-account"
|
|
327
|
+
? await probeGoogleServiceAccount(value, fetchImpl, now)
|
|
328
|
+
: await probeGoogleOauth(value, fetchImpl);
|
|
329
|
+
}
|
|
330
|
+
catch (error) {
|
|
331
|
+
verdict = unverified(`could not reach the service (${error instanceof Error ? error.name : "error"})`);
|
|
332
|
+
}
|
|
333
|
+
items.push({ title: item.title, service, ...verdict });
|
|
334
|
+
}
|
|
335
|
+
const counts = { valid: 0, rejected: 0, unverified: 0, skipped: 0 };
|
|
336
|
+
for (const item of items)
|
|
337
|
+
counts[item.status] += 1;
|
|
338
|
+
return { slug, vault, items, counts };
|
|
339
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The two Google Cloud pointers in a client repo's CLIENT.md (client-template,
|
|
3
|
+
* 2026-10-25): `gcp_project` — the project ID that holds the client's OAuth
|
|
4
|
+
* client / service account / Pub/Sub topic — and `gcp_account` — the Google
|
|
5
|
+
* account every gcloud command for it runs as (`--account=`). `none` means
|
|
6
|
+
* the client has no project. repo-lint does NOT require them (its
|
|
7
|
+
* CLIENT_MD_KEYS list is unchanged), so older repos simply lack them;
|
|
8
|
+
* `ia template-sync` lists those repos and `ia doctor` checks the accounts
|
|
9
|
+
* are logged in. CLIENT.md is per-client and never synced, so nothing here
|
|
10
|
+
* writes it.
|
|
11
|
+
*/
|
|
12
|
+
export declare const GCP_MANIFEST_KEYS: readonly ["gcp_project", "gcp_account"];
|
|
13
|
+
export type GcpManifestKey = (typeof GCP_MANIFEST_KEYS)[number];
|
|
14
|
+
export interface GcpManifest {
|
|
15
|
+
/** The project ID, or null when the key is missing, unfilled, invalid or `none`. */
|
|
16
|
+
project: string | null;
|
|
17
|
+
/** The gcloud account, same rules. */
|
|
18
|
+
account: string | null;
|
|
19
|
+
/** True when gcp_project is `none` (the client has no Google Cloud project). */
|
|
20
|
+
none: boolean;
|
|
21
|
+
/** Keys CLIENT.md does not have at all (a repo made before 2026-10-25). */
|
|
22
|
+
missing: GcpManifestKey[];
|
|
23
|
+
/** Keys still holding the template placeholder (or empty). */
|
|
24
|
+
unfilled: GcpManifestKey[];
|
|
25
|
+
/** Keys whose value is not a project ID / an email / `none`. */
|
|
26
|
+
invalid: GcpManifestKey[];
|
|
27
|
+
}
|
|
28
|
+
/** One `key: value` line of CLIENT.md; undefined when the key is absent. */
|
|
29
|
+
export declare function readManifestKey(clientMd: string, key: string): string | undefined;
|
|
30
|
+
export declare function readGcpManifest(clientMd: string): GcpManifest;
|
|
31
|
+
/** The manifest of `<repoDir>/CLIENT.md`, or null when the repo has no CLIENT.md. */
|
|
32
|
+
export declare function readGcpManifestFromRepo(repoDir: string): GcpManifest | null;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { GCP_PROJECT_PATTERN } from "./gcp-service-account-key.js";
|
|
4
|
+
/**
|
|
5
|
+
* The two Google Cloud pointers in a client repo's CLIENT.md (client-template,
|
|
6
|
+
* 2026-10-25): `gcp_project` — the project ID that holds the client's OAuth
|
|
7
|
+
* client / service account / Pub/Sub topic — and `gcp_account` — the Google
|
|
8
|
+
* account every gcloud command for it runs as (`--account=`). `none` means
|
|
9
|
+
* the client has no project. repo-lint does NOT require them (its
|
|
10
|
+
* CLIENT_MD_KEYS list is unchanged), so older repos simply lack them;
|
|
11
|
+
* `ia template-sync` lists those repos and `ia doctor` checks the accounts
|
|
12
|
+
* are logged in. CLIENT.md is per-client and never synced, so nothing here
|
|
13
|
+
* writes it.
|
|
14
|
+
*/
|
|
15
|
+
export const GCP_MANIFEST_KEYS = ["gcp_project", "gcp_account"];
|
|
16
|
+
const EMAIL = /^[^\s@<>]+@[^\s@<>]+\.[^\s@<>]+$/;
|
|
17
|
+
/** One `key: value` line of CLIENT.md; undefined when the key is absent. */
|
|
18
|
+
export function readManifestKey(clientMd, key) {
|
|
19
|
+
const match = clientMd.match(new RegExp(`^${key}\\s*:(.*)$`, "m"));
|
|
20
|
+
return match ? match[1].trim() : undefined;
|
|
21
|
+
}
|
|
22
|
+
export function readGcpManifest(clientMd) {
|
|
23
|
+
const result = {
|
|
24
|
+
project: null,
|
|
25
|
+
account: null,
|
|
26
|
+
none: false,
|
|
27
|
+
missing: [],
|
|
28
|
+
unfilled: [],
|
|
29
|
+
invalid: [],
|
|
30
|
+
};
|
|
31
|
+
for (const key of GCP_MANIFEST_KEYS) {
|
|
32
|
+
const value = readManifestKey(clientMd, key);
|
|
33
|
+
if (value === undefined) {
|
|
34
|
+
result.missing.push(key);
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
if (!value || /^<.*>$/.test(value) || value.includes("_HERE")) {
|
|
38
|
+
result.unfilled.push(key);
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
if (value.toLowerCase() === "none") {
|
|
42
|
+
if (key === "gcp_project")
|
|
43
|
+
result.none = true;
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
const valid = key === "gcp_project" ? GCP_PROJECT_PATTERN.test(value) : EMAIL.test(value);
|
|
47
|
+
if (!valid) {
|
|
48
|
+
result.invalid.push(key);
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
if (key === "gcp_project")
|
|
52
|
+
result.project = value;
|
|
53
|
+
else
|
|
54
|
+
result.account = value;
|
|
55
|
+
}
|
|
56
|
+
return result;
|
|
57
|
+
}
|
|
58
|
+
/** The manifest of `<repoDir>/CLIENT.md`, or null when the repo has no CLIENT.md. */
|
|
59
|
+
export function readGcpManifestFromRepo(repoDir) {
|
|
60
|
+
const path = join(repoDir, "CLIENT.md");
|
|
61
|
+
if (!existsSync(path))
|
|
62
|
+
return null;
|
|
63
|
+
return readGcpManifest(readFileSync(path, "utf8"));
|
|
64
|
+
}
|
package/dist/doctor.js
CHANGED
|
@@ -2,6 +2,7 @@ import { existsSync, readFileSync } from "node:fs";
|
|
|
2
2
|
import { join, resolve } from "node:path";
|
|
3
3
|
import { N8N_API_KEY_ENV, N8N_API_URL_ENV } from "./agent-templates.js";
|
|
4
4
|
import { AIRTABLE_API_TOKEN_ENV, GOOGLE_SERVICE_ACCOUNT_KEY_ENV } from "./client-drive.js";
|
|
5
|
+
import { readGcpManifestFromRepo } from "./client-manifest.js";
|
|
5
6
|
import { DOCS_SEARCH_TOKEN_ENV, DOCS_SESSION_ENV, TEAM_DOCS_BASE_URL } from "./docs-search.js";
|
|
6
7
|
import { parseEnvFile } from "./env-file.js";
|
|
7
8
|
import { DEFAULT_TECH_VAULT, opSecretReference, TECH_ENV_VARS, } from "./env-registry.js";
|
|
@@ -101,6 +102,7 @@ export async function doctor(options = {}) {
|
|
|
101
102
|
checks.push(await ghCheck(execFileImpl, env, platform));
|
|
102
103
|
checks.push(await opCheck(execFileImpl, platform));
|
|
103
104
|
checks.push(await gcloudCheck(execFileImpl));
|
|
105
|
+
checks.push(await clientGcpAccountsCheck(workspaceDir, execFileImpl));
|
|
104
106
|
// Technician vars only. Admin vars are all doctor:false today, so this
|
|
105
107
|
// changes nothing now — it is here so that adding a doctor:true infra var
|
|
106
108
|
// later cannot start naming it in every technician's checkup.
|
|
@@ -177,6 +179,89 @@ function lefthookCheck(workspaceDir, fixed) {
|
|
|
177
179
|
"runs without it",
|
|
178
180
|
};
|
|
179
181
|
}
|
|
182
|
+
/**
|
|
183
|
+
* CLIENT.md's `gcp_account` names the Google account every gcloud command for
|
|
184
|
+
* that client runs as. This checks each named account is logged in on this
|
|
185
|
+
* machine (`gcloud auth list` — account names only, never a token), and that
|
|
186
|
+
* the two keys hold a project ID / an email / `none`. Repos without the keys
|
|
187
|
+
* (made before 2026-10-25) are not a problem here — `ia template-sync` lists
|
|
188
|
+
* them — so the check stays quiet until a repo records an account. Read-only.
|
|
189
|
+
*/
|
|
190
|
+
async function clientGcpAccountsCheck(workspaceDir, execFileImpl) {
|
|
191
|
+
const base = {
|
|
192
|
+
id: "workspace/client-gcp-accounts",
|
|
193
|
+
group: "workspace",
|
|
194
|
+
label: "gcloud accounts named in client CLIENT.md files",
|
|
195
|
+
};
|
|
196
|
+
const needed = new Map();
|
|
197
|
+
const invalid = [];
|
|
198
|
+
let unrecorded = 0;
|
|
199
|
+
for (const name of clientCloneNames(workspaceDir)) {
|
|
200
|
+
if (name === "client-template")
|
|
201
|
+
continue;
|
|
202
|
+
const manifest = readGcpManifestFromRepo(join(workspaceDir, "clients", name));
|
|
203
|
+
if (!manifest)
|
|
204
|
+
continue;
|
|
205
|
+
for (const key of manifest.invalid)
|
|
206
|
+
invalid.push(`${name} ${key}`);
|
|
207
|
+
if (manifest.missing.length > 0 || manifest.unfilled.length > 0)
|
|
208
|
+
unrecorded++;
|
|
209
|
+
if (manifest.account)
|
|
210
|
+
needed.set(manifest.account, [...(needed.get(manifest.account) ?? []), name]);
|
|
211
|
+
}
|
|
212
|
+
const unrecordedNote = unrecorded > 0
|
|
213
|
+
? ` ${unrecorded} repo(s) do not record gcp_project/gcp_account yet (\`ia template-sync\` lists them).`
|
|
214
|
+
: "";
|
|
215
|
+
if (invalid.length > 0) {
|
|
216
|
+
return {
|
|
217
|
+
...base,
|
|
218
|
+
status: "warn",
|
|
219
|
+
detail: `CLIENT.md value is not a project ID, an email or \`none\`: ${invalid.join(", ")}.${unrecordedNote}`,
|
|
220
|
+
fixIt: "fix the value in that repo's CLIENT.md (gcp_project = the project ID, gcp_account = an email, or `none`)",
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
if (needed.size === 0) {
|
|
224
|
+
return {
|
|
225
|
+
...base,
|
|
226
|
+
status: "ok",
|
|
227
|
+
verboseOnly: true,
|
|
228
|
+
detail: `no client repo names a gcp_account yet.${unrecordedNote}`.trim(),
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
let loggedIn;
|
|
232
|
+
try {
|
|
233
|
+
const { stdout } = await execFileImpl("gcloud", ["auth", "list", "--format=value(account)"]);
|
|
234
|
+
loggedIn = new Set(stdout
|
|
235
|
+
.split("\n")
|
|
236
|
+
.map((line) => (line.split("\t")[0] ?? "").trim().toLowerCase())
|
|
237
|
+
.filter(Boolean));
|
|
238
|
+
}
|
|
239
|
+
catch {
|
|
240
|
+
// tools/gcloud already says why gcloud is unusable; do not repeat it as a second warning.
|
|
241
|
+
return {
|
|
242
|
+
...base,
|
|
243
|
+
status: "ok",
|
|
244
|
+
verboseOnly: true,
|
|
245
|
+
detail: `could not run \`gcloud auth list\` — see the gcloud check.${unrecordedNote}`,
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
const absent = [...needed.entries()].filter(([account]) => !loggedIn.has(account.toLowerCase()));
|
|
249
|
+
if (absent.length === 0) {
|
|
250
|
+
return {
|
|
251
|
+
...base,
|
|
252
|
+
status: "ok",
|
|
253
|
+
detail: `every named account is logged in (${[...needed.keys()].join(", ")}).${unrecordedNote}`,
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
return {
|
|
257
|
+
...base,
|
|
258
|
+
status: "warn",
|
|
259
|
+
detail: absent
|
|
260
|
+
.map(([account, repos]) => `${account} (needed by ${repos.join(", ")}) is not logged in to gcloud`)
|
|
261
|
+
.join("; "),
|
|
262
|
+
fixIt: absent.map(([account]) => `gcloud auth login ${account}`).join(" && "),
|
|
263
|
+
};
|
|
264
|
+
}
|
|
180
265
|
/**
|
|
181
266
|
* The root .env is created once from env.example and never overwritten, so
|
|
182
267
|
* vars added to the registry later are silently absent from older files —
|
|
@@ -414,6 +499,15 @@ async function opCheck(execFileImpl, platform) {
|
|
|
414
499
|
fixIt: `needed by \`ia env fill\` — ${opInstallFix(platform)}`,
|
|
415
500
|
};
|
|
416
501
|
}
|
|
502
|
+
// The desktop app integration fails `whoami` but authorizes real calls
|
|
503
|
+
// (see opReachable in op-cli.ts).
|
|
504
|
+
try {
|
|
505
|
+
await execFileImpl("op", ["vault", "list", "--format=json"]);
|
|
506
|
+
return { ...base, status: "ok", detail: "reachable through the 1Password app" };
|
|
507
|
+
}
|
|
508
|
+
catch {
|
|
509
|
+
// fall through to the warning
|
|
510
|
+
}
|
|
417
511
|
return {
|
|
418
512
|
...base,
|
|
419
513
|
status: "warn",
|
package/dist/env-fill.js
CHANGED
|
@@ -3,8 +3,8 @@ import { copyFile, readFile, writeFile } from "node:fs/promises";
|
|
|
3
3
|
import { join, resolve } from "node:path";
|
|
4
4
|
import { parseEnvFile } from "./env-file.js";
|
|
5
5
|
import { ADMIN_ENV_VARS, DEFAULT_TECH_VAULT, envVarsForRole, TECH_ENV_VARS, } from "./env-registry.js";
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
6
|
+
import { execFileAsync } from "./exec.js";
|
|
7
|
+
import { opReachable } from "./op-cli.js";
|
|
8
8
|
import { detectPlatformInfo } from "./platform.js";
|
|
9
9
|
import { envExampleTemplatePath } from "./templates.js";
|
|
10
10
|
/** Values op read fields are tried in when the registry doesn't pin one. */
|
|
@@ -28,7 +28,7 @@ export async function envFill(options = {}) {
|
|
|
28
28
|
const platform = options.platform ?? detectPlatformInfo();
|
|
29
29
|
const envPath = join(workspaceDir, ".env");
|
|
30
30
|
const adminSkipped = includeAdmin ? 0 : ADMIN_ENV_VARS.length;
|
|
31
|
-
const opReady = await
|
|
31
|
+
const opReady = await opReachable(execFileImpl, platform);
|
|
32
32
|
if (opReady !== true) {
|
|
33
33
|
return {
|
|
34
34
|
ok: false,
|
|
@@ -108,38 +108,6 @@ export async function envFill(options = {}) {
|
|
|
108
108
|
nextSteps,
|
|
109
109
|
};
|
|
110
110
|
}
|
|
111
|
-
/**
|
|
112
|
-
* True when op is installed and can reach the vaults; otherwise the fix-it step.
|
|
113
|
-
*
|
|
114
|
-
* `op whoami` is the obvious probe but NOT a sufficient one: with the desktop
|
|
115
|
-
* app integration on, op 2.35 answers "account is not signed in" to `whoami`
|
|
116
|
-
* while `op read` and `op vault list` both work — the CLI has no session of its
|
|
117
|
-
* own, and the app authorizes each call. Failing preflight there told a
|
|
118
|
-
* technician to enable an integration that was already enabled, and blocked a
|
|
119
|
-
* fill that would have succeeded. So a `whoami` failure falls through to a
|
|
120
|
-
* vault-listing probe, which is what this command actually needs to do.
|
|
121
|
-
*/
|
|
122
|
-
async function opPreflight(execFileImpl, platform) {
|
|
123
|
-
try {
|
|
124
|
-
await execFileImpl("op", ["whoami"]);
|
|
125
|
-
return true;
|
|
126
|
-
}
|
|
127
|
-
catch (error) {
|
|
128
|
-
const { code } = execFailureDetail(error);
|
|
129
|
-
if (code === "ENOENT")
|
|
130
|
-
return opInstallFix(platform);
|
|
131
|
-
try {
|
|
132
|
-
await execFileImpl("op", ["vault", "list", "--format=json"]);
|
|
133
|
-
return true;
|
|
134
|
-
}
|
|
135
|
-
catch (fallbackError) {
|
|
136
|
-
const detail = execFailureDetail(fallbackError);
|
|
137
|
-
if (detail.code === "ENOENT")
|
|
138
|
-
return opInstallFix(platform);
|
|
139
|
-
return `authenticate the 1Password CLI: ${opIntegrationFix(platform)}`;
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
111
|
async function opRead(execFileImpl, vault, spec) {
|
|
144
112
|
const item = spec.item ?? spec.name;
|
|
145
113
|
const fields = spec.field ? [spec.field] : OP_FIELD_FALLBACKS;
|