-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.ts
More file actions
526 lines (499 loc) · 15 KB
/
Copy pathcommands.ts
File metadata and controls
526 lines (499 loc) · 15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
import { spawn } from "node:child_process";
import * as vscode from "vscode";
import {
DevGuardClient,
DevGuardHttpError,
DevGuardNetworkError,
} from "./api/client";
import { isValidTokenFormat } from "./api/signing";
import * as config from "./config";
import { ConnectionManager } from "./connection";
import { InsightController } from "./controller";
import { PurlCache } from "./enrich/cache";
import { Logger } from "./logger";
import { SbomDocumentProvider } from "./sbomDocuments";
import { AssetSelection, SelectedAsset } from "./selection";
import {
gitPostCommitHookFilePath,
gitPreCommitHookFilePath,
removeExistingGitHooks,
setupGitCommitHooks,
preCommitHooksExists,
//postCommitHooksExists,
} from "./commitHooks";
import { StatusBarCommitHooks } from "./ui/statusBarCommitHooks";
export interface CommandDeps {
client: DevGuardClient;
connection: ConnectionManager;
selection: AssetSelection;
controller: InsightController;
cache: PurlCache;
sbomProvider: SbomDocumentProvider;
logger: Logger;
commitHookStatusBar: StatusBarCommitHooks;
}
export function registerCommands(deps: CommandDeps): vscode.Disposable[] {
return [
vscode.commands.registerCommand("devguard.connect", () => connect(deps)),
vscode.commands.registerCommand("devguard.disconnect", () =>
disconnect(deps),
),
vscode.commands.registerCommand("devguard.selectAsset", () =>
selectAsset(deps),
),
vscode.commands.registerCommand("devguard.refresh", () => refresh(deps)),
vscode.commands.registerCommand("devguard.setupDependencyProxy", () =>
setupDependencyProxy(deps),
),
vscode.commands.registerCommand("devguard.viewSbom", () => viewSbom(deps)),
vscode.commands.registerCommand("devguard.generateSbom", () =>
generateSbom(deps),
),
vscode.commands.registerCommand("devguard.setupGitHooks", () => {
setupGitHooks(deps);
}),
vscode.commands.registerCommand("devguard.removeGitHooks", () => {
removeGitHooks(deps);
}),
];
}
type ConnectResult =
| { ok: true }
| { ok: false; kind: "unverified" }
| { ok: false; kind: "error"; err: unknown };
async function connect({
client,
connection,
controller,
logger,
}: CommandDeps): Promise<void> {
if (!config.isApiUrlValid()) {
vscode.window.showErrorMessage(
"DevGuard: set a valid devguard.apiUrl before connecting.",
);
return;
}
if (config.isInsecureRemote()) {
const proceed = await vscode.window.showWarningMessage(
"DevGuard: the API URL uses plain http on a non-local host. Connecting would send signed requests over an unencrypted connection.",
{ modal: true },
"Connect anyway",
);
if (proceed !== "Connect anyway") {
return;
}
}
const token = (
await vscode.window.showInputBox({
title: "DevGuard Personal Access Token",
prompt:
"Paste your DevGuard PAT. Stored securely in VS Code Secret Storage — never written to settings or logs.",
placeHolder: "hex-encoded token",
password: true,
ignoreFocusOut: true,
})
)?.trim();
if (!token) {
return;
}
if (!isValidTokenFormat(token)) {
vscode.window.showErrorMessage(
"DevGuard: that does not look like a valid token (a hex-encoded key was expected).",
);
return;
}
const result = await vscode.window.withProgress<ConnectResult>(
{
location: vscode.ProgressLocation.Notification,
title: "DevGuard: validating token…",
},
async () => {
try {
const who = await connection.withCandidate(token, () =>
client.whoami(),
);
if (who.userID && who.userID !== "NO_SESSION") {
await connection.setToken(token, who.userID);
return { ok: true };
}
return { ok: false, kind: "unverified" };
} catch (err) {
return { ok: false, kind: "error", err };
}
},
);
if (result.ok) {
controller.refreshVisible();
const action = await vscode.window.showInformationMessage(
"DevGuard: connected.",
"Select asset",
);
if (action === "Select asset") {
await vscode.commands.executeCommand("devguard.selectAsset");
}
} else if (result.kind === "unverified") {
vscode.window.showErrorMessage(
"DevGuard: the token could not be verified (signature rejected). Check that you pasted the whole token and that your system clock is accurate.",
);
} else {
reportError("validate the token", result.err, logger);
}
}
async function disconnect({
connection,
selection,
controller,
}: CommandDeps): Promise<void> {
await connection.disconnect();
await selection.clear();
controller.refreshVisible();
vscode.window.showInformationMessage("DevGuard: disconnected.");
}
async function selectAsset({
connection,
selection,
controller,
logger,
}: CommandDeps): Promise<void> {
if (!connection.isConnected()) {
const action = await vscode.window.showInformationMessage(
"DevGuard: connect first to select an asset.",
"Connect",
);
if (action === "Connect") {
await vscode.commands.executeCommand("devguard.connect");
}
return;
}
let selected: SelectedAsset | undefined;
try {
selected = await selection.pick();
} catch (err) {
reportError("load organizations and assets", err, logger);
return;
}
if (!selected) {
return;
}
// Eagerly load the asset's risk data: warms the cache and surfaces the count
// (or the real error) rather than failing silently in a background refresh.
try {
const xref = await vscode.window.withProgress(
{
location: vscode.ProgressLocation.Notification,
title: `DevGuard: loading risk data for ${selected.label}…`,
},
() => selection.loadXref(),
);
const packages = xref?.size ?? 0;
logger.info(
`Loaded asset risk data for ${selected.label}: ${packages} package(s) with open risks.`,
);
vscode.window.showInformationMessage(
packages > 0
? `DevGuard: using ${selected.label} — ${packages} package(s) with open risks.`
: `DevGuard: using ${selected.label} (no open dependency risks found — has it been scanned?).`,
);
} catch (err) {
reportError("load asset risk data", err, logger);
}
controller.refreshVisible();
}
async function refresh({
cache,
selection,
controller,
}: CommandDeps): Promise<void> {
cache.invalidateAll();
selection.invalidateXref();
controller.refreshVisible();
}
async function setupDependencyProxy({ logger }: CommandDeps): Promise<void> {
const folder = await pickWorkspaceFolder();
if (!folder) {
return;
}
const secret = (
await vscode.window.showInputBox({
title: "DevGuard Dependency Proxy",
prompt:
"Optional organization proxy secret. Leave blank to use the public proxy path.",
ignoreFocusOut: true,
password: true,
})
)?.trim();
const apiUrl = config.getApiUrl();
const registry = secret
? `${apiUrl}/api/v1/dependency-proxy/${encodeURIComponent(secret)}/npm/`
: `${apiUrl}/api/v1/dependency-proxy/npm/`;
const npmrc = vscode.Uri.joinPath(folder.uri, ".npmrc");
let existing = "";
try {
existing = Buffer.from(await vscode.workspace.fs.readFile(npmrc)).toString(
"utf8",
);
} catch {
// no existing .npmrc
}
if (existing) {
const ok = await vscode.window.showWarningMessage(
`Update ${vscode.workspace.asRelativePath(npmrc)} to route npm through the DevGuard proxy?`,
{ modal: true, detail: `registry=${registry}` },
"Update .npmrc",
);
if (ok !== "Update .npmrc") {
return;
}
}
try {
await vscode.workspace.fs.writeFile(
npmrc,
Buffer.from(upsertRegistry(existing, registry), "utf8"),
);
await vscode.window.showTextDocument(
await vscode.workspace.openTextDocument(npmrc),
);
vscode.window.showInformationMessage(
'DevGuard: .npmrc updated. Run "npm install" to use the proxy.',
);
} catch (err) {
reportError("write .npmrc", err, logger);
}
}
async function viewSbom({
client,
selection,
sbomProvider,
logger,
}: CommandDeps): Promise<void> {
const selected = selection.getSelected();
if (!selected) {
vscode.window.showInformationMessage("DevGuard: select an asset first.");
return;
}
await vscode.window.withProgress(
{
location: vscode.ProgressLocation.Notification,
title: `DevGuard: fetching SBOM for ${selected.label}…`,
},
async () => {
try {
const raw = await client.getSbom(
selected.orgSlug,
selected.projectSlug,
selected.assetSlug,
selected.refSlug,
);
let pretty = raw;
try {
pretty = JSON.stringify(JSON.parse(raw), null, 2);
} catch {
// leave raw if not valid JSON
}
await sbomProvider.open(
`${selected.assetSlug}-${selected.refSlug}-sbom`,
pretty,
);
} catch (err) {
reportError("fetch the SBOM", err, logger);
}
},
);
}
async function generateSbom({
connection,
selection,
controller,
logger,
}: CommandDeps): Promise<void> {
const selected = selection.getSelected();
if (!connection.isConnected() || !selected) {
vscode.window.showInformationMessage(
"DevGuard: connect and select an asset before generating an SBOM.",
);
return;
}
const folder = await pickWorkspaceFolder();
if (!folder) {
return;
}
const token = await connection.getToken();
if (!token) {
vscode.window.showErrorMessage(
"DevGuard: no token available — reconnect and try again.",
);
return;
}
const scannerPath = config.getScannerPath();
const assetName = `${selected.orgSlug}/${selected.projectSlug}/${selected.assetSlug}`;
// Token is passed via DEVGUARD_TOKEN env (not argv) so it does not appear in the process list.
const args = [
"sca",
"--path",
folder.uri.fsPath,
"--assetName",
assetName,
"--apiUrl",
config.getApiUrl(),
"--ref",
selected.refSlug,
];
logger.show();
logger.info(
`Running ${scannerPath} sca for ${assetName} (ref ${selected.refSlug})…`,
);
let output = "";
const capture = (chunk: Buffer): void => {
const text = chunk.toString();
logger.append(text);
output = (output + text).slice(-8000);
};
const exitCode = await vscode.window.withProgress<number | undefined>(
{
location: vscode.ProgressLocation.Notification,
title: `DevGuard: scanning ${selected.label}…`,
cancellable: true,
},
(_progress, cancelToken) =>
new Promise<number | undefined>((resolve) => {
let child: ReturnType<typeof spawn> | undefined;
try {
child = spawn(scannerPath, args, {
cwd: folder.uri.fsPath,
env: { ...process.env, DEVGUARD_TOKEN: token },
});
} catch (err) {
logger.error("could not start the scanner", err);
resolve(undefined);
return;
}
cancelToken.onCancellationRequested(() => child?.kill());
child.stdout?.on("data", capture);
child.stderr?.on("data", capture);
child.on("error", (err) => {
logger.error("scanner process error", err);
resolve(undefined);
});
child.on("close", (code) => resolve(code ?? undefined));
}),
);
if (exitCode === undefined) {
vscode.window.showErrorMessage(
`DevGuard: could not run "${scannerPath}". Install the devguard-scanner CLI or set devguard.scannerPath.`,
);
return;
}
// The SBOM is uploaded before the scanner applies its fail-on-risk gate, so a
// non-zero exit usually means "findings present", not a failure to upload.
selection.invalidateXref();
controller.refreshVisible();
if (exitCode === 0) {
vscode.window.showInformationMessage(
"DevGuard: SCA complete — SBOM uploaded. Insights refreshed.",
);
return;
}
if (/invalid specification version|not a valid CycloneDX/i.test(output)) {
vscode.window.showErrorMessage(
"DevGuard: the generated SBOM used a CycloneDX version the scanner could not read. Rebuild devguard-scanner from source (go install ./cmd/devguard-scanner) or align your trivy version, then retry.",
);
return;
}
vscode.window.showWarningMessage(
`DevGuard: scanner exited with code ${exitCode} (see the DevGuard output). Insights refreshed.`,
);
}
async function pickWorkspaceFolder(): Promise<
vscode.WorkspaceFolder | undefined
> {
const folders = vscode.workspace.workspaceFolders ?? [];
if (folders.length === 0) {
vscode.window.showWarningMessage("DevGuard: open a project folder first.");
return undefined;
}
if (folders.length === 1) {
return folders[0];
}
return vscode.window.showWorkspaceFolderPick();
}
async function setupGitHooks({
selection,
commitHookStatusBar,
}: CommandDeps): Promise<void> {
try {
await removeExistingGitHooks();
if (
await preCommitHooksExists() /* && (await postCommitHooksExists()) */
) {
commitHookStatusBar.setActive(true);
} else {
commitHookStatusBar.setActive(false);
}
vscode.window.showInformationMessage(
`Git Hooks have been removed successfully from ${gitPreCommitHookFilePath} and ${gitPostCommitHookFilePath}.`,
);
} catch {
return;
}
}
async function removeGitHooks({
commitHookStatusBar,
}: CommandDeps): Promise<void> {
try {
await removeExistingGitHooks();
if (
await preCommitHooksExists() /* && (await postCommitHooksExists()) */
) {
commitHookStatusBar.setActive(true);
} else {
commitHookStatusBar.setActive(false);
}
vscode.window.showInformationMessage(
`Git Hooks have been removed successfully from ${gitPreCommitHookFilePath} and ${gitPostCommitHookFilePath}.`,
);
} catch {
return;
}
}
/** Replaces an existing `registry=` line or appends one, preserving other lines. */
export function upsertRegistry(content: string, registry: string): string {
const line = `registry=${registry}`;
const lines = content.split(/\r?\n/);
let replaced = false;
const out = lines.map((l) => {
if (/^\s*registry\s*=/.test(l)) {
replaced = true;
return line;
}
return l;
});
if (!replaced) {
while (out.length > 0 && out[out.length - 1].trim() === "") {
out.pop();
}
out.push(line);
}
return `${out.join("\n").replace(/\n+$/, "")}\n`;
}
function reportError(action: string, err: unknown, logger: Logger): void {
logger.error(`while trying to ${action}`, err);
if (err instanceof DevGuardHttpError) {
if (err.status === 401) {
vscode.window.showErrorMessage(
"DevGuard: token not recognized by the server. Create a new PAT and try again.",
);
} else {
vscode.window.showErrorMessage(
`DevGuard: the server returned ${err.status} while trying to ${action} (see DevGuard output).`,
);
}
return;
}
if (err instanceof DevGuardNetworkError) {
vscode.window.showErrorMessage(`DevGuard: ${err.message}.`);
return;
}
vscode.window.showErrorMessage(
`DevGuard: failed to ${action} (see DevGuard output).`,
);
}