@trustify-da/trustify-da-javascript-client 0.3.0-ea.f2c4df7 → 0.3.0-ea.f501753
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/README.md +151 -13
- package/dist/package.json +11 -3
- package/dist/src/analysis.d.ts +16 -0
- package/dist/src/analysis.js +53 -4
- package/dist/src/batch_opts.d.ts +24 -0
- package/dist/src/batch_opts.js +35 -0
- package/dist/src/cli.js +171 -4
- package/dist/src/cyclone_dx_sbom.d.ts +14 -1
- package/dist/src/cyclone_dx_sbom.js +34 -6
- package/dist/src/index.d.ts +132 -1
- package/dist/src/index.js +340 -4
- package/dist/src/license/licenses_api.js +9 -2
- package/dist/src/license/project_license.d.ts +0 -8
- package/dist/src/license/project_license.js +0 -11
- package/dist/src/oci_image/utils.js +11 -2
- package/dist/src/provider.d.ts +6 -3
- package/dist/src/provider.js +14 -5
- package/dist/src/providers/base_java.d.ts +0 -9
- package/dist/src/providers/base_java.js +2 -38
- package/dist/src/providers/base_javascript.d.ts +19 -3
- package/dist/src/providers/base_javascript.js +99 -18
- package/dist/src/providers/base_pyproject.d.ts +153 -0
- package/dist/src/providers/base_pyproject.js +315 -0
- package/dist/src/providers/golang_gomodules.d.ts +21 -12
- package/dist/src/providers/golang_gomodules.js +164 -118
- package/dist/src/providers/gomod_parser.d.ts +4 -0
- package/dist/src/providers/gomod_parser.js +16 -0
- package/dist/src/providers/java_gradle.d.ts +19 -0
- package/dist/src/providers/java_gradle.js +114 -0
- package/dist/src/providers/java_maven.d.ts +8 -0
- package/dist/src/providers/java_maven.js +93 -1
- package/dist/src/providers/javascript_npm.d.ts +1 -0
- package/dist/src/providers/javascript_npm.js +21 -0
- package/dist/src/providers/javascript_pnpm.d.ts +1 -1
- package/dist/src/providers/javascript_pnpm.js +8 -4
- package/dist/src/providers/manifest.d.ts +2 -0
- package/dist/src/providers/manifest.js +22 -4
- package/dist/src/providers/marker_evaluator.d.ts +14 -0
- package/dist/src/providers/marker_evaluator.js +191 -0
- package/dist/src/providers/processors/yarn_berry_processor.js +88 -5
- package/dist/src/providers/python_controller.d.ts +5 -1
- package/dist/src/providers/python_controller.js +8 -4
- package/dist/src/providers/python_pip.d.ts +4 -0
- package/dist/src/providers/python_pip.js +5 -5
- package/dist/src/providers/python_pip_pyproject.d.ts +61 -0
- package/dist/src/providers/python_pip_pyproject.js +144 -0
- package/dist/src/providers/python_poetry.d.ts +75 -0
- package/dist/src/providers/python_poetry.js +238 -0
- package/dist/src/providers/python_uv.d.ts +42 -0
- package/dist/src/providers/python_uv.js +160 -0
- package/dist/src/providers/requirements_parser.js +4 -3
- package/dist/src/providers/rust_cargo.d.ts +52 -0
- package/dist/src/providers/rust_cargo.js +614 -0
- package/dist/src/providers/tree-sitter-gomod.wasm +0 -0
- package/dist/src/providers/tree-sitter-requirements.wasm +0 -0
- package/dist/src/sbom.d.ts +14 -1
- package/dist/src/sbom.js +13 -2
- package/dist/src/tools.d.ts +26 -0
- package/dist/src/tools.js +58 -0
- package/dist/src/workspace.d.ts +61 -0
- package/dist/src/workspace.js +256 -0
- package/package.json +12 -4
package/dist/src/index.js
CHANGED
|
@@ -1,17 +1,25 @@
|
|
|
1
1
|
import path from "node:path";
|
|
2
2
|
import { EOL } from "os";
|
|
3
|
+
import pLimit from 'p-limit';
|
|
3
4
|
import { availableProviders, match } from './provider.js';
|
|
4
5
|
import analysis from './analysis.js';
|
|
5
6
|
import fs from 'node:fs';
|
|
6
7
|
import { getCustom } from "./tools.js";
|
|
8
|
+
import { resolveBatchMetadata, resolveContinueOnError } from './batch_opts.js';
|
|
9
|
+
import { discoverMavenModules } from './providers/java_maven.js';
|
|
10
|
+
import { discoverGradleSubprojects } from './providers/java_gradle.js';
|
|
11
|
+
import { discoverGoWorkspaceModules } from './providers/golang_gomodules.js';
|
|
12
|
+
import { discoverWorkspaceCrates, discoverWorkspacePackages, filterManifestPathsByDiscoveryIgnore, resolveWorkspaceDiscoveryIgnore, validatePackageJson, } from './workspace.js';
|
|
7
13
|
import.meta.dirname;
|
|
8
14
|
import * as url from 'url';
|
|
9
15
|
export { parseImageRef } from "./oci_image/utils.js";
|
|
10
16
|
export { ImageRef } from "./oci_image/images.js";
|
|
11
17
|
export { getProjectLicense, findLicenseFilePath, identifyLicense, getLicenseDetails, licensesFromReport, normalizeLicensesResponse, runLicenseCheck, getCompatibility } from "./license/index.js";
|
|
12
|
-
export default { componentAnalysis, stackAnalysis, imageAnalysis, validateToken };
|
|
18
|
+
export default { componentAnalysis, stackAnalysis, stackAnalysisBatch, imageAnalysis, validateToken, generateSbom };
|
|
19
|
+
export { discoverMavenModules, discoverGradleSubprojects, discoverGoWorkspaceModules, discoverWorkspacePackages, discoverWorkspaceCrates, validatePackageJson, resolveWorkspaceDiscoveryIgnore, filterManifestPathsByDiscoveryIgnore, resolveContinueOnError, resolveBatchMetadata, };
|
|
13
20
|
/**
|
|
14
21
|
* @typedef {{
|
|
22
|
+
* TRUSTIFY_DA_CARGO_PATH?: string | undefined,
|
|
15
23
|
* TRUSTIFY_DA_DOCKER_PATH?: string | undefined,
|
|
16
24
|
* TRUSTIFY_DA_GO_MVS_LOGIC_ENABLED?: string | undefined,
|
|
17
25
|
* TRUSTIFY_DA_GO_PATH?: string | undefined,
|
|
@@ -36,14 +44,36 @@ export default { componentAnalysis, stackAnalysis, imageAnalysis, validateToken
|
|
|
36
44
|
* TRUSTIFY_DA_SYFT_CONFIG_PATH?: string | undefined,
|
|
37
45
|
* TRUSTIFY_DA_SYFT_PATH?: string | undefined,
|
|
38
46
|
* TRUSTIFY_DA_YARN_PATH?: string | undefined,
|
|
47
|
+
* TRUSTIFY_DA_WORKSPACE_DIR?: string | undefined,
|
|
39
48
|
* TRUSTIFY_DA_LICENSE_CHECK?: string | undefined,
|
|
40
49
|
* MATCH_MANIFEST_VERSIONS?: string | undefined,
|
|
41
50
|
* TRUSTIFY_DA_SOURCE?: string | undefined,
|
|
42
51
|
* TRUSTIFY_DA_TOKEN?: string | undefined,
|
|
43
52
|
* TRUSTIFY_DA_TELEMETRY_ID?: string | undefined,
|
|
44
|
-
*
|
|
53
|
+
* TRUSTIFY_DA_WORKSPACE_DIR?: string | undefined,
|
|
54
|
+
* batchConcurrency?: number | undefined,
|
|
55
|
+
* TRUSTIFY_DA_BATCH_CONCURRENCY?: string | undefined,
|
|
56
|
+
* workspaceDiscoveryIgnore?: string[] | undefined,
|
|
57
|
+
* TRUSTIFY_DA_WORKSPACE_DISCOVERY_IGNORE?: string | undefined,
|
|
58
|
+
* continueOnError?: boolean | undefined,
|
|
59
|
+
* TRUSTIFY_DA_CONTINUE_ON_ERROR?: string | undefined,
|
|
60
|
+
* batchMetadata?: boolean | undefined,
|
|
61
|
+
* TRUSTIFY_DA_BATCH_METADATA?: string | undefined,
|
|
62
|
+
* TRUSTIFY_DA_UV_PATH?: string | undefined,
|
|
63
|
+
* TRUSTIFY_DA_POETRY_PATH?: string | undefined,
|
|
64
|
+
* [key: string]: string | number | boolean | string[] | undefined,
|
|
45
65
|
* }} Options
|
|
46
66
|
*/
|
|
67
|
+
/**
|
|
68
|
+
* @typedef {{
|
|
69
|
+
* workspaceRoot: string,
|
|
70
|
+
* ecosystem: 'javascript' | 'cargo' | 'unknown',
|
|
71
|
+
* total: number,
|
|
72
|
+
* successful: number,
|
|
73
|
+
* failed: number,
|
|
74
|
+
* errors: Array<{ manifestPath: string, phase: 'validation' | 'sbom', reason: string }>
|
|
75
|
+
* }} BatchAnalysisMetadata
|
|
76
|
+
*/
|
|
47
77
|
/**
|
|
48
78
|
* Logs messages to the console if the TRUSTIFY_DA_DEBUG environment variable is set to "true".
|
|
49
79
|
* @param {string} alongsideText - The text to prepend to the log message.
|
|
@@ -129,7 +159,7 @@ export function selectTrustifyDABackend(opts = {}) {
|
|
|
129
159
|
async function stackAnalysis(manifest, html = false, opts = {}) {
|
|
130
160
|
const theUrl = selectTrustifyDABackend(opts);
|
|
131
161
|
fs.accessSync(manifest, fs.constants.R_OK); // throws error if file unreadable
|
|
132
|
-
let provider = match(manifest, availableProviders); // throws error if no matching provider
|
|
162
|
+
let provider = match(manifest, availableProviders, opts); // throws error if no matching provider
|
|
133
163
|
return await analysis.requestStack(provider, manifest, theUrl, html, opts); // throws error request sending failed
|
|
134
164
|
}
|
|
135
165
|
/**
|
|
@@ -143,7 +173,7 @@ async function componentAnalysis(manifest, opts = {}) {
|
|
|
143
173
|
const theUrl = selectTrustifyDABackend(opts);
|
|
144
174
|
fs.accessSync(manifest, fs.constants.R_OK);
|
|
145
175
|
opts["manifest-type"] = path.basename(manifest);
|
|
146
|
-
let provider = match(manifest, availableProviders); // throws error if no matching provider
|
|
176
|
+
let provider = match(manifest, availableProviders, opts); // throws error if no matching provider
|
|
147
177
|
return await analysis.requestComponent(provider, manifest, theUrl, opts); // throws error request sending failed
|
|
148
178
|
}
|
|
149
179
|
/**
|
|
@@ -176,6 +206,312 @@ async function imageAnalysis(imageRefs, html = false, opts = {}) {
|
|
|
176
206
|
const theUrl = selectTrustifyDABackend(opts);
|
|
177
207
|
return await analysis.requestImages(imageRefs, theUrl, html, opts);
|
|
178
208
|
}
|
|
209
|
+
/**
|
|
210
|
+
* Max concurrent SBOM generations for batch workspace analysis. Env/opts override default 10.
|
|
211
|
+
* @param {Options} opts
|
|
212
|
+
* @returns {number}
|
|
213
|
+
* @private
|
|
214
|
+
*/
|
|
215
|
+
function resolveBatchConcurrency(opts) {
|
|
216
|
+
const fromEnv = getCustom('TRUSTIFY_DA_BATCH_CONCURRENCY', null, opts);
|
|
217
|
+
const raw = opts.batchConcurrency ?? fromEnv ?? '10';
|
|
218
|
+
const n = typeof raw === 'number' ? raw : parseInt(String(raw), 10);
|
|
219
|
+
if (!Number.isFinite(n) || n < 1) {
|
|
220
|
+
return 10;
|
|
221
|
+
}
|
|
222
|
+
return Math.min(256, n);
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* @param {string} root
|
|
226
|
+
* @param {'javascript' | 'cargo' | 'unknown'} ecosystem
|
|
227
|
+
* @param {number} totalSbomAttempts
|
|
228
|
+
* @param {number} successfulSbomCount
|
|
229
|
+
* @param {Array<{ manifestPath: string, phase: 'validation' | 'sbom', reason: string }>} errors
|
|
230
|
+
* @returns {BatchAnalysisMetadata}
|
|
231
|
+
* @private
|
|
232
|
+
*/
|
|
233
|
+
function buildBatchAnalysisMetadata(root, ecosystem, totalSbomAttempts, successfulSbomCount, errors) {
|
|
234
|
+
return {
|
|
235
|
+
workspaceRoot: root,
|
|
236
|
+
ecosystem,
|
|
237
|
+
total: totalSbomAttempts,
|
|
238
|
+
successful: successfulSbomCount,
|
|
239
|
+
failed: errors.length,
|
|
240
|
+
errors: [...errors],
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Generate a CycloneDX SBOM from a manifest file. No backend HTTP request is made.
|
|
245
|
+
*
|
|
246
|
+
* @param {string} manifestPath - path to the manifest file (e.g. pom.xml, package.json)
|
|
247
|
+
* @param {Options} [opts={}] - optional options (e.g. workspace dir, tool paths)
|
|
248
|
+
* @returns {Promise<object>} parsed CycloneDX SBOM JSON object
|
|
249
|
+
* @throws {Error} if the manifest is unsupported or SBOM generation fails
|
|
250
|
+
*/
|
|
251
|
+
export async function generateSbom(manifestPath, opts = {}) {
|
|
252
|
+
fs.accessSync(manifestPath, fs.constants.R_OK);
|
|
253
|
+
const result = await generateOneSbom(manifestPath, opts);
|
|
254
|
+
if (!result.ok) {
|
|
255
|
+
throw new Error(`Failed to generate SBOM for ${result.manifestPath}: ${result.reason}`);
|
|
256
|
+
}
|
|
257
|
+
return result.sbom;
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* @typedef {{ ok: true, purl: string, sbom: object } | { ok: false, manifestPath: string, reason: string }} SbomResult
|
|
261
|
+
*/
|
|
262
|
+
/**
|
|
263
|
+
* Generate an SBOM for a single manifest, returning a normalized result.
|
|
264
|
+
*
|
|
265
|
+
* @param {string} manifestPath
|
|
266
|
+
* @param {Options} workspaceOpts - opts with `TRUSTIFY_DA_WORKSPACE_DIR` set
|
|
267
|
+
* @returns {Promise<SbomResult>}
|
|
268
|
+
* @private
|
|
269
|
+
*/
|
|
270
|
+
async function generateOneSbom(manifestPath, workspaceOpts) {
|
|
271
|
+
const provider = match(manifestPath, availableProviders, workspaceOpts);
|
|
272
|
+
const provided = await provider.provideStack(manifestPath, workspaceOpts);
|
|
273
|
+
const sbom = JSON.parse(provided.content);
|
|
274
|
+
const purl = sbom?.metadata?.component?.purl || sbom?.metadata?.component?.['bom-ref'];
|
|
275
|
+
if (!purl) {
|
|
276
|
+
return { ok: false, manifestPath, reason: 'missing purl in SBOM' };
|
|
277
|
+
}
|
|
278
|
+
return { ok: true, purl, sbom };
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Detect the workspace ecosystem and discover manifest paths.
|
|
282
|
+
*
|
|
283
|
+
* @param {string} root - Resolved workspace root
|
|
284
|
+
* @param {Options} opts
|
|
285
|
+
* @returns {Promise<{ ecosystem: 'javascript' | 'cargo' | 'maven' | 'gradle' | 'gomodules' | 'unknown', manifestPaths: string[] }>}
|
|
286
|
+
* @private
|
|
287
|
+
*/
|
|
288
|
+
async function detectWorkspaceManifests(root, opts) {
|
|
289
|
+
const cargoToml = path.join(root, 'Cargo.toml');
|
|
290
|
+
const cargoLock = path.join(root, 'Cargo.lock');
|
|
291
|
+
const packageJson = path.join(root, 'package.json');
|
|
292
|
+
const pomXml = path.join(root, 'pom.xml');
|
|
293
|
+
if (fs.existsSync(cargoToml) && fs.existsSync(cargoLock)) {
|
|
294
|
+
return { ecosystem: 'cargo', manifestPaths: await discoverWorkspaceCrates(root, opts) };
|
|
295
|
+
}
|
|
296
|
+
if (fs.existsSync(pomXml)) {
|
|
297
|
+
const manifestPaths = await discoverMavenModules(root, opts);
|
|
298
|
+
if (manifestPaths.length > 0) {
|
|
299
|
+
return { ecosystem: 'maven', manifestPaths };
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
const hasGradleSettings = fs.existsSync(path.join(root, 'settings.gradle'))
|
|
303
|
+
|| fs.existsSync(path.join(root, 'settings.gradle.kts'));
|
|
304
|
+
if (hasGradleSettings) {
|
|
305
|
+
const manifestPaths = await discoverGradleSubprojects(root, opts);
|
|
306
|
+
if (manifestPaths.length > 0) {
|
|
307
|
+
return { ecosystem: 'gradle', manifestPaths };
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
if (fs.existsSync(path.join(root, 'go.work'))) {
|
|
311
|
+
const manifestPaths = await discoverGoWorkspaceModules(root, opts);
|
|
312
|
+
if (manifestPaths.length > 0) {
|
|
313
|
+
return { ecosystem: 'gomodules', manifestPaths };
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
const hasJsLock = fs.existsSync(path.join(root, 'pnpm-lock.yaml'))
|
|
317
|
+
|| fs.existsSync(path.join(root, 'yarn.lock'))
|
|
318
|
+
|| fs.existsSync(path.join(root, 'package-lock.json'));
|
|
319
|
+
if (fs.existsSync(packageJson) && hasJsLock) {
|
|
320
|
+
let manifestPaths = await discoverWorkspacePackages(root, opts);
|
|
321
|
+
if (manifestPaths.length === 0) {
|
|
322
|
+
manifestPaths = [packageJson];
|
|
323
|
+
}
|
|
324
|
+
return { ecosystem: 'javascript', manifestPaths };
|
|
325
|
+
}
|
|
326
|
+
return { ecosystem: 'unknown', manifestPaths: [] };
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* Validate discovered JS package.json manifests, collecting errors.
|
|
330
|
+
*
|
|
331
|
+
* @param {string[]} manifestPaths
|
|
332
|
+
* @param {boolean} continueOnError
|
|
333
|
+
* @param {Array<{ manifestPath: string, phase: 'validation' | 'sbom', reason: string }>} collectedErrors - mutated in place
|
|
334
|
+
* @returns {{ validPaths: string[] }}
|
|
335
|
+
* @throws {Error} on first invalid manifest when `continueOnError` is false
|
|
336
|
+
* @private
|
|
337
|
+
*/
|
|
338
|
+
function validateJsManifests(manifestPaths, continueOnError, collectedErrors) {
|
|
339
|
+
const validPaths = [];
|
|
340
|
+
for (const p of manifestPaths) {
|
|
341
|
+
const v = validatePackageJson(p);
|
|
342
|
+
if (v.valid) {
|
|
343
|
+
validPaths.push(p);
|
|
344
|
+
}
|
|
345
|
+
else {
|
|
346
|
+
collectedErrors.push({ manifestPath: p, phase: 'validation', reason: v.error });
|
|
347
|
+
console.warn(`Skipping invalid package.json (${v.error}): ${p}`);
|
|
348
|
+
if (!continueOnError) {
|
|
349
|
+
throw new Error(`Invalid package.json (${v.error}): ${p}`);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
return { validPaths };
|
|
354
|
+
}
|
|
355
|
+
/**
|
|
356
|
+
* Generate SBOMs for all manifests. In fail-fast mode, stops on first error.
|
|
357
|
+
* In continue-on-error mode, runs concurrently and collects failures.
|
|
358
|
+
*
|
|
359
|
+
* @param {string[]} manifestPaths
|
|
360
|
+
* @param {Options} workspaceOpts
|
|
361
|
+
* @param {boolean} continueOnError
|
|
362
|
+
* @param {number} concurrency
|
|
363
|
+
* @param {Array<{ manifestPath: string, phase: 'validation' | 'sbom', reason: string }>} collectedErrors - mutated in place
|
|
364
|
+
* @returns {Promise<Object.<string, object>>} sbomByPurl map
|
|
365
|
+
* @throws {Error} on first SBOM failure when `continueOnError` is false
|
|
366
|
+
* @private
|
|
367
|
+
*/
|
|
368
|
+
async function generateSboms(manifestPaths, workspaceOpts, continueOnError, concurrency, collectedErrors) {
|
|
369
|
+
/** @type {SbomResult[]} */
|
|
370
|
+
const results = [];
|
|
371
|
+
if (!continueOnError) {
|
|
372
|
+
for (const manifestPath of manifestPaths) {
|
|
373
|
+
const result = await generateOneSbom(manifestPath, workspaceOpts);
|
|
374
|
+
if (!result.ok) {
|
|
375
|
+
collectedErrors.push({ manifestPath: result.manifestPath, phase: 'sbom', reason: result.reason });
|
|
376
|
+
throw new Error(`${result.manifestPath}: ${result.reason}`);
|
|
377
|
+
}
|
|
378
|
+
results.push(result);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
else {
|
|
382
|
+
const limit = pLimit(concurrency);
|
|
383
|
+
const settled = await Promise.all(manifestPaths.map(manifestPath => limit(async () => {
|
|
384
|
+
try {
|
|
385
|
+
return await generateOneSbom(manifestPath, workspaceOpts);
|
|
386
|
+
}
|
|
387
|
+
catch (err) {
|
|
388
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
389
|
+
if (process.env["TRUSTIFY_DA_DEBUG"] === "true") {
|
|
390
|
+
console.log(`Skipping ${manifestPath}: ${msg}`);
|
|
391
|
+
}
|
|
392
|
+
return { ok: false, manifestPath, reason: msg };
|
|
393
|
+
}
|
|
394
|
+
})));
|
|
395
|
+
for (const r of settled) {
|
|
396
|
+
results.push(r);
|
|
397
|
+
if (!r.ok) {
|
|
398
|
+
collectedErrors.push({ manifestPath: r.manifestPath, phase: 'sbom', reason: r.reason });
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
const sbomByPurl = {};
|
|
403
|
+
for (const r of results) {
|
|
404
|
+
if (r.ok) {
|
|
405
|
+
sbomByPurl[r.purl] = r.sbom;
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
return sbomByPurl;
|
|
409
|
+
}
|
|
410
|
+
/**
|
|
411
|
+
* Create an Error with optional `batchMetadata` attached.
|
|
412
|
+
* @param {string} message
|
|
413
|
+
* @param {boolean} wantMetadata
|
|
414
|
+
* @param {BatchAnalysisMetadata} [metadata]
|
|
415
|
+
* @returns {Error}
|
|
416
|
+
* @private
|
|
417
|
+
*/
|
|
418
|
+
function batchError(message, wantMetadata, metadata) {
|
|
419
|
+
const err = new Error(message);
|
|
420
|
+
if (wantMetadata && metadata) {
|
|
421
|
+
err.batchMetadata = metadata;
|
|
422
|
+
}
|
|
423
|
+
return err;
|
|
424
|
+
}
|
|
425
|
+
/**
|
|
426
|
+
* @overload
|
|
427
|
+
* @param {string} workspaceRoot
|
|
428
|
+
* @param {true} html
|
|
429
|
+
* @param {Options & { batchMetadata: true }} opts
|
|
430
|
+
* @returns {Promise<{ analysis: string, metadata: BatchAnalysisMetadata }>}
|
|
431
|
+
* @throws {Error}
|
|
432
|
+
*/
|
|
433
|
+
/**
|
|
434
|
+
* @overload
|
|
435
|
+
* @param {string} workspaceRoot
|
|
436
|
+
* @param {true} html
|
|
437
|
+
* @param {Options & { batchMetadata?: false }} [opts={}]
|
|
438
|
+
* @returns {Promise<string>}
|
|
439
|
+
* @throws {Error}
|
|
440
|
+
*/
|
|
441
|
+
/**
|
|
442
|
+
* @overload
|
|
443
|
+
* @param {string} workspaceRoot
|
|
444
|
+
* @param {false} html
|
|
445
|
+
* @param {Options & { batchMetadata: true }} opts
|
|
446
|
+
* @returns {Promise<{ analysis: Object.<string, import('@trustify-da/trustify-da-api-model/model/v5/AnalysisReport').AnalysisReport>, metadata: BatchAnalysisMetadata }>}
|
|
447
|
+
* @throws {Error}
|
|
448
|
+
*/
|
|
449
|
+
/**
|
|
450
|
+
* @overload
|
|
451
|
+
* @param {string} workspaceRoot
|
|
452
|
+
* @param {false} html
|
|
453
|
+
* @param {Options & { batchMetadata?: false }} [opts={}]
|
|
454
|
+
* @returns {Promise<Object.<string, import('@trustify-da/trustify-da-api-model/model/v5/AnalysisReport').AnalysisReport>>}
|
|
455
|
+
* @throws {Error}
|
|
456
|
+
*/
|
|
457
|
+
/**
|
|
458
|
+
* Get stack analysis for all workspace packages/crates (batch).
|
|
459
|
+
* Detects ecosystem from workspace root: Cargo (Cargo.toml + Cargo.lock) or JS/TS (package.json + lock file).
|
|
460
|
+
* SBOMs are generated in parallel (see `batchConcurrency`) unless `continueOnError: false` (fail-fast sequential).
|
|
461
|
+
* With `opts.batchMetadata` / `TRUSTIFY_DA_BATCH_METADATA`, returns `{ analysis, metadata }` including validation and SBOM errors.
|
|
462
|
+
*
|
|
463
|
+
* @overload
|
|
464
|
+
* @param {string} workspaceRoot - Path to workspace root (containing lock file and workspace config)
|
|
465
|
+
* @param {boolean} [html=false] - true returns HTML, false returns JSON report
|
|
466
|
+
* @param {Options} [opts={}] - `batchConcurrency`, discovery ignores, `continueOnError` (default true), `batchMetadata` (default false)
|
|
467
|
+
* @returns {Promise<string|Object.<string, import('@trustify-da/trustify-da-api-model/model/v5/AnalysisReport').AnalysisReport>|{ analysis: string|Object.<string, import('@trustify-da/trustify-da-api-model/model/v5/AnalysisReport').AnalysisReport>, metadata: BatchAnalysisMetadata }>}
|
|
468
|
+
* @throws {Error} if workspace root invalid, no manifests found, no packages pass validation, no SBOMs produced, or backend request failed. When `opts.batchMetadata` is set, `error.batchMetadata` may be set on thrown errors.
|
|
469
|
+
*/
|
|
470
|
+
async function stackAnalysisBatch(workspaceRoot, html = false, opts = {}) {
|
|
471
|
+
const theUrl = selectTrustifyDABackend(opts);
|
|
472
|
+
const root = path.resolve(workspaceRoot);
|
|
473
|
+
fs.accessSync(root, fs.constants.R_OK);
|
|
474
|
+
const continueOnError = resolveContinueOnError(opts);
|
|
475
|
+
const wantMetadata = resolveBatchMetadata(opts);
|
|
476
|
+
/** @type {Array<{ manifestPath: string, phase: 'validation' | 'sbom', reason: string }>} */
|
|
477
|
+
const collectedErrors = [];
|
|
478
|
+
const { ecosystem, manifestPaths: discovered } = await detectWorkspaceManifests(root, opts);
|
|
479
|
+
let manifestPaths = discovered;
|
|
480
|
+
if (ecosystem === 'javascript') {
|
|
481
|
+
try {
|
|
482
|
+
const { validPaths } = validateJsManifests(manifestPaths, continueOnError, collectedErrors);
|
|
483
|
+
manifestPaths = validPaths;
|
|
484
|
+
}
|
|
485
|
+
catch (err) {
|
|
486
|
+
throw batchError(err.message, wantMetadata, buildBatchAnalysisMetadata(root, ecosystem, 0, 0, collectedErrors));
|
|
487
|
+
}
|
|
488
|
+
if (manifestPaths.length === 0 && discovered.length > 0) {
|
|
489
|
+
const detail = collectedErrors.map(e => `${e.manifestPath}: ${e.reason}`).join('; ');
|
|
490
|
+
throw batchError(`No valid packages after validation at ${root}. ${detail}`, wantMetadata, buildBatchAnalysisMetadata(root, ecosystem, 0, 0, collectedErrors));
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
if (manifestPaths.length === 0) {
|
|
494
|
+
throw new Error(`No workspace manifests found at ${root}. Ensure a supported workspace root exists (Cargo.toml+Cargo.lock, go.work, or package.json+lock file).`);
|
|
495
|
+
}
|
|
496
|
+
const workspaceOpts = { ...opts, TRUSTIFY_DA_WORKSPACE_DIR: root };
|
|
497
|
+
const concurrency = resolveBatchConcurrency(opts);
|
|
498
|
+
let sbomByPurl;
|
|
499
|
+
try {
|
|
500
|
+
sbomByPurl = await generateSboms(manifestPaths, workspaceOpts, continueOnError, concurrency, collectedErrors);
|
|
501
|
+
}
|
|
502
|
+
catch (err) {
|
|
503
|
+
throw batchError(err.message, wantMetadata, buildBatchAnalysisMetadata(root, ecosystem, manifestPaths.length, 0, collectedErrors));
|
|
504
|
+
}
|
|
505
|
+
if (Object.keys(sbomByPurl).length === 0) {
|
|
506
|
+
throw batchError(`No valid SBOMs produced from ${manifestPaths.length} manifest(s) at ${root}`, wantMetadata, buildBatchAnalysisMetadata(root, ecosystem, manifestPaths.length, 0, collectedErrors));
|
|
507
|
+
}
|
|
508
|
+
const analysisResult = await analysis.requestStackBatch(sbomByPurl, theUrl, html, opts);
|
|
509
|
+
const meta = buildBatchAnalysisMetadata(root, ecosystem, manifestPaths.length, Object.keys(sbomByPurl).length, collectedErrors);
|
|
510
|
+
if (wantMetadata) {
|
|
511
|
+
return { analysis: analysisResult, metadata: meta };
|
|
512
|
+
}
|
|
513
|
+
return analysisResult;
|
|
514
|
+
}
|
|
179
515
|
/**
|
|
180
516
|
* Validates the Exhort token.
|
|
181
517
|
* @param {Options} [opts={}] - Optional parameters, potentially including token override.
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* @see https://github.com/guacsec/trustify-dependency-analytics#license-analysis-apiv5licenses
|
|
5
5
|
* @see https://github.com/guacsec/trustify-da-api-spec/blob/main/api/v5/openapi.yaml
|
|
6
6
|
*/
|
|
7
|
+
import { PackageURL } from 'packageurl-js';
|
|
7
8
|
import { selectTrustifyDABackend } from '../index.js';
|
|
8
9
|
import { addProxyAgent, getTokenHeaders } from '../tools.js';
|
|
9
10
|
/**
|
|
@@ -39,6 +40,10 @@ export async function getLicenseDetails(spdxId, opts = {}) {
|
|
|
39
40
|
throw new Error(`Failed to fetch license details: ${err.message}`);
|
|
40
41
|
}
|
|
41
42
|
}
|
|
43
|
+
function normalizePurlString(purl) {
|
|
44
|
+
const parsed = PackageURL.fromString(purl);
|
|
45
|
+
return new PackageURL(parsed.type, parsed.namespace, parsed.name, parsed.version, null, null).toString();
|
|
46
|
+
}
|
|
42
47
|
/**
|
|
43
48
|
* Normalize the LicensesResponse shape (array of LicenseProviderResult) into a map of purl -> license info.
|
|
44
49
|
* Each provider result has { status, summary, packages } where packages is { [purl]: { concluded, evidence } }.
|
|
@@ -53,6 +58,7 @@ export function normalizeLicensesResponse(data, purls = []) {
|
|
|
53
58
|
if (!data || !Array.isArray(data)) {
|
|
54
59
|
return map;
|
|
55
60
|
}
|
|
61
|
+
const normalizedPurlsSet = purls.length > 0 ? new Set(purls.map(normalizePurlString)) : null;
|
|
56
62
|
for (const providerResult of data) {
|
|
57
63
|
const packages = providerResult?.packages;
|
|
58
64
|
if (!packages || typeof packages !== 'object') {
|
|
@@ -64,8 +70,9 @@ export function normalizeLicensesResponse(data, purls = []) {
|
|
|
64
70
|
const expression = concluded?.expression;
|
|
65
71
|
const licenses = identifiers.length > 0 ? identifiers : (expression ? [expression] : []);
|
|
66
72
|
const category = concluded?.category; // PERMISSIVE | WEAK_COPYLEFT | STRONG_COPYLEFT | UNKNOWN
|
|
67
|
-
|
|
68
|
-
|
|
73
|
+
const normalizedPurl = normalizePurlString(purl);
|
|
74
|
+
if (normalizedPurlsSet === null || normalizedPurlsSet.has(normalizedPurl)) {
|
|
75
|
+
map.set(normalizedPurl, { licenses: licenses.filter(Boolean), category });
|
|
69
76
|
}
|
|
70
77
|
}
|
|
71
78
|
// Use first provider that has packages; backend may return multiple (e.g. deps.dev)
|
|
@@ -17,12 +17,4 @@ export function getProjectLicense(manifestPath: string): {
|
|
|
17
17
|
* @returns {Promise<string|null>} - SPDX identifier or null
|
|
18
18
|
*/
|
|
19
19
|
export function identifyLicense(licenseFilePath: string, opts?: {}): Promise<string | null>;
|
|
20
|
-
/**
|
|
21
|
-
* Get license for SBOM root component with fallback to LICENSE file.
|
|
22
|
-
* Priority: manifest license > LICENSE file > null
|
|
23
|
-
* This is used by providers to populate the license field in the SBOM.
|
|
24
|
-
* @param {string} manifestPath - path to manifest
|
|
25
|
-
* @returns {string|null} - SPDX identifier or null
|
|
26
|
-
*/
|
|
27
|
-
export function getLicenseForSbom(manifestPath: string): string | null;
|
|
28
20
|
export { findLicenseFilePath, readLicenseFile } from "./license_utils.js";
|
|
@@ -60,14 +60,3 @@ export async function identifyLicense(licenseFilePath, opts = {}) {
|
|
|
60
60
|
return null; // Fallback to local detection on error
|
|
61
61
|
}
|
|
62
62
|
}
|
|
63
|
-
/**
|
|
64
|
-
* Get license for SBOM root component with fallback to LICENSE file.
|
|
65
|
-
* Priority: manifest license > LICENSE file > null
|
|
66
|
-
* This is used by providers to populate the license field in the SBOM.
|
|
67
|
-
* @param {string} manifestPath - path to manifest
|
|
68
|
-
* @returns {string|null} - SPDX identifier or null
|
|
69
|
-
*/
|
|
70
|
-
export function getLicenseForSbom(manifestPath) {
|
|
71
|
-
const { fromManifest, fromFile } = getProjectLicense(manifestPath);
|
|
72
|
-
return fromManifest || fromFile || null;
|
|
73
|
-
}
|
|
@@ -135,7 +135,7 @@ function execSyft(imageRef, opts = {}) {
|
|
|
135
135
|
function getSyftEnvs(dockerPath, podmanPath) {
|
|
136
136
|
let path = null;
|
|
137
137
|
if (dockerPath && podmanPath) {
|
|
138
|
-
path = `${dockerPath}${
|
|
138
|
+
path = `${dockerPath}${delimiter}${podmanPath}`;
|
|
139
139
|
}
|
|
140
140
|
else if (dockerPath) {
|
|
141
141
|
path = dockerPath;
|
|
@@ -276,7 +276,16 @@ function podmanGetVariant(opts = {}) {
|
|
|
276
276
|
* @returns {string} - The information
|
|
277
277
|
*/
|
|
278
278
|
function dockerPodmanInfo(dockerSupplier, podmanSupplier, opts = {}) {
|
|
279
|
-
|
|
279
|
+
try {
|
|
280
|
+
const result = dockerSupplier(opts);
|
|
281
|
+
if (result) {
|
|
282
|
+
return result;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
catch (_) {
|
|
286
|
+
// docker not available, fall through to podman
|
|
287
|
+
}
|
|
288
|
+
return podmanSupplier(opts);
|
|
280
289
|
}
|
|
281
290
|
/**
|
|
282
291
|
* Gets the digests for an image
|
package/dist/src/provider.d.ts
CHANGED
|
@@ -13,12 +13,15 @@ export function matchForLicense(manifestPath: string, providers: [Provider]): Pr
|
|
|
13
13
|
* Each provider MUST export 'provideStack' taking manifest path returning a {@link Provided}.
|
|
14
14
|
* @param {string} manifest - the name-type or path of the manifest
|
|
15
15
|
* @param {[Provider]} providers - list of providers to iterate over
|
|
16
|
+
* @param {{TRUSTIFY_DA_WORKSPACE_DIR?: string}} [opts={}] - optional; TRUSTIFY_DA_WORKSPACE_DIR overrides lock file location for workspaces
|
|
16
17
|
* @returns {Provider}
|
|
17
18
|
* @throws {Error} when the manifest is not supported and no provider was matched
|
|
18
19
|
*/
|
|
19
|
-
export function match(manifest: string, providers: [Provider]
|
|
20
|
+
export function match(manifest: string, providers: [Provider], opts?: {
|
|
21
|
+
TRUSTIFY_DA_WORKSPACE_DIR?: string;
|
|
22
|
+
}): Provider;
|
|
20
23
|
/** @typedef {{ecosystem: string, contentType: string, content: string}} Provided */
|
|
21
|
-
/** @typedef {{isSupported: function(string): boolean, validateLockFile: function(string): void, provideComponent: function(string, {}): Provided | Promise<Provided>, provideStack: function(string, {}): Provided | Promise<Provided>, readLicenseFromManifest: function(string): string | null}} Provider */
|
|
24
|
+
/** @typedef {{isSupported: function(string): boolean, validateLockFile: function(string, Object): void, provideComponent: function(string, {}): Provided | Promise<Provided>, provideStack: function(string, {}): Provided | Promise<Provided>, readLicenseFromManifest: function(string): string | null}} Provider */
|
|
22
25
|
/**
|
|
23
26
|
* MUST include all providers here.
|
|
24
27
|
* @type {[Provider]}
|
|
@@ -31,7 +34,7 @@ export type Provided = {
|
|
|
31
34
|
};
|
|
32
35
|
export type Provider = {
|
|
33
36
|
isSupported: (arg0: string) => boolean;
|
|
34
|
-
validateLockFile: (arg0: string) => void;
|
|
37
|
+
validateLockFile: (arg0: string, arg1: any) => void;
|
|
35
38
|
provideComponent: (arg0: string, arg1: {}) => Provided | Promise<Provided>;
|
|
36
39
|
provideStack: (arg0: string, arg1: {}) => Provided | Promise<Provided>;
|
|
37
40
|
readLicenseFromManifest: (arg0: string) => string | null;
|
package/dist/src/provider.js
CHANGED
|
@@ -7,8 +7,12 @@ import Javascript_npm from './providers/javascript_npm.js';
|
|
|
7
7
|
import Javascript_pnpm from './providers/javascript_pnpm.js';
|
|
8
8
|
import Javascript_yarn from './providers/javascript_yarn.js';
|
|
9
9
|
import pythonPipProvider from './providers/python_pip.js';
|
|
10
|
+
import Python_pip_pyproject from './providers/python_pip_pyproject.js';
|
|
11
|
+
import Python_poetry from './providers/python_poetry.js';
|
|
12
|
+
import Python_uv from './providers/python_uv.js';
|
|
13
|
+
import rustCargoProvider from './providers/rust_cargo.js';
|
|
10
14
|
/** @typedef {{ecosystem: string, contentType: string, content: string}} Provided */
|
|
11
|
-
/** @typedef {{isSupported: function(string): boolean, validateLockFile: function(string): void, provideComponent: function(string, {}): Provided | Promise<Provided>, provideStack: function(string, {}): Provided | Promise<Provided>, readLicenseFromManifest: function(string): string | null}} Provider */
|
|
15
|
+
/** @typedef {{isSupported: function(string): boolean, validateLockFile: function(string, Object): void, provideComponent: function(string, {}): Provided | Promise<Provided>, provideStack: function(string, {}): Provided | Promise<Provided>, readLicenseFromManifest: function(string): string | null}} Provider */
|
|
12
16
|
/**
|
|
13
17
|
* MUST include all providers here.
|
|
14
18
|
* @type {[Provider]}
|
|
@@ -17,11 +21,15 @@ export const availableProviders = [
|
|
|
17
21
|
new Java_maven(),
|
|
18
22
|
new Java_gradle_groovy(),
|
|
19
23
|
new Java_gradle_kotlin(),
|
|
20
|
-
new Javascript_npm(),
|
|
21
24
|
new Javascript_pnpm(),
|
|
22
25
|
new Javascript_yarn(),
|
|
26
|
+
new Javascript_npm(),
|
|
23
27
|
golangGomodulesProvider,
|
|
24
|
-
pythonPipProvider
|
|
28
|
+
pythonPipProvider,
|
|
29
|
+
new Python_poetry(),
|
|
30
|
+
new Python_uv(),
|
|
31
|
+
new Python_pip_pyproject(),
|
|
32
|
+
rustCargoProvider
|
|
25
33
|
];
|
|
26
34
|
/**
|
|
27
35
|
* Match a provider by manifest type only (no lock file check). Used for license reading.
|
|
@@ -45,16 +53,17 @@ export function matchForLicense(manifestPath, providers) {
|
|
|
45
53
|
* Each provider MUST export 'provideStack' taking manifest path returning a {@link Provided}.
|
|
46
54
|
* @param {string} manifest - the name-type or path of the manifest
|
|
47
55
|
* @param {[Provider]} providers - list of providers to iterate over
|
|
56
|
+
* @param {{TRUSTIFY_DA_WORKSPACE_DIR?: string}} [opts={}] - optional; TRUSTIFY_DA_WORKSPACE_DIR overrides lock file location for workspaces
|
|
48
57
|
* @returns {Provider}
|
|
49
58
|
* @throws {Error} when the manifest is not supported and no provider was matched
|
|
50
59
|
*/
|
|
51
|
-
export function match(manifest, providers) {
|
|
60
|
+
export function match(manifest, providers, opts = {}) {
|
|
52
61
|
const manifestPath = path.parse(manifest);
|
|
53
62
|
const supported = providers.filter(prov => prov.isSupported(manifestPath.base));
|
|
54
63
|
if (supported.length === 0) {
|
|
55
64
|
throw new Error(`${manifestPath.base} is not supported`);
|
|
56
65
|
}
|
|
57
|
-
const provider = supported.find(prov => prov.validateLockFile(manifestPath.dir));
|
|
66
|
+
const provider = supported.find(prov => prov.validateLockFile(manifestPath.dir, opts));
|
|
58
67
|
if (!provider) {
|
|
59
68
|
throw new Error(`${manifestPath.base} requires a lock file. Use your preferred package manager to generate the lock file.`);
|
|
60
69
|
}
|
|
@@ -57,15 +57,6 @@ export default class Base_Java {
|
|
|
57
57
|
* @returns string
|
|
58
58
|
*/
|
|
59
59
|
selectToolBinary(manifestPath: string, opts: {}): string | null;
|
|
60
|
-
/**
|
|
61
|
-
*
|
|
62
|
-
* @param {string} startingManifest - the path of the manifest from which to start searching for the wrapper
|
|
63
|
-
* @param {string} repoRoot - the root of the repository at which point to stop searching for mvnw, derived via git if unset and then fallsback
|
|
64
|
-
* to the root of the drive the manifest is on (assumes absolute path is given)
|
|
65
|
-
* @returns {string|undefined}
|
|
66
|
-
*/
|
|
67
|
-
traverseForWrapper(startingManifest: string, repoRoot?: string): string | undefined;
|
|
68
|
-
normalizePath(thePath: any): string;
|
|
69
60
|
#private;
|
|
70
61
|
}
|
|
71
62
|
export type Provided = import("../provider").Provided;
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import fs from 'node:fs';
|
|
2
1
|
import path from 'node:path';
|
|
3
2
|
import { PackageURL } from 'packageurl-js';
|
|
4
|
-
import { getCustomPath,
|
|
3
|
+
import { getCustomPath, getWrapperPreference, invokeCommand, traverseForWrapper } from "../tools.js";
|
|
5
4
|
/** @typedef {import('../provider').Provider} */
|
|
6
5
|
/** @typedef {import('../provider').Provided} Provided */
|
|
7
6
|
/** @typedef {{name: string, version: string}} Package */
|
|
@@ -131,7 +130,7 @@ export default class Base_Java {
|
|
|
131
130
|
const toolPath = getCustomPath(this.globalBinary, opts);
|
|
132
131
|
const useWrapper = getWrapperPreference(this.globalBinary, opts);
|
|
133
132
|
if (useWrapper) {
|
|
134
|
-
const wrapper = this.
|
|
133
|
+
const wrapper = traverseForWrapper(manifestDir, this.localWrapper);
|
|
135
134
|
if (wrapper !== undefined) {
|
|
136
135
|
try {
|
|
137
136
|
this._invokeCommand(wrapper, ['--version'], { cwd: manifestDir });
|
|
@@ -156,39 +155,4 @@ export default class Base_Java {
|
|
|
156
155
|
}
|
|
157
156
|
return toolPath;
|
|
158
157
|
}
|
|
159
|
-
/**
|
|
160
|
-
*
|
|
161
|
-
* @param {string} startingManifest - the path of the manifest from which to start searching for the wrapper
|
|
162
|
-
* @param {string} repoRoot - the root of the repository at which point to stop searching for mvnw, derived via git if unset and then fallsback
|
|
163
|
-
* to the root of the drive the manifest is on (assumes absolute path is given)
|
|
164
|
-
* @returns {string|undefined}
|
|
165
|
-
*/
|
|
166
|
-
traverseForWrapper(startingManifest, repoRoot = undefined) {
|
|
167
|
-
const normalizedManifest = this.normalizePath(startingManifest);
|
|
168
|
-
const currentDir = this.normalizePath(path.dirname(normalizedManifest));
|
|
169
|
-
repoRoot = repoRoot || getGitRootDir(currentDir) || path.parse(normalizedManifest).root;
|
|
170
|
-
const wrapperPath = path.join(currentDir, this.localWrapper);
|
|
171
|
-
try {
|
|
172
|
-
fs.accessSync(wrapperPath, fs.constants.X_OK);
|
|
173
|
-
return wrapperPath;
|
|
174
|
-
}
|
|
175
|
-
catch (error) {
|
|
176
|
-
if (error.code === 'ENOENT') {
|
|
177
|
-
const rootDir = path.parse(currentDir).root;
|
|
178
|
-
if (currentDir === repoRoot || currentDir === rootDir) {
|
|
179
|
-
return undefined;
|
|
180
|
-
}
|
|
181
|
-
const parentDir = path.dirname(currentDir);
|
|
182
|
-
if (parentDir === currentDir || parentDir === rootDir) {
|
|
183
|
-
return undefined;
|
|
184
|
-
}
|
|
185
|
-
return this.traverseForWrapper(path.join(parentDir, path.basename(normalizedManifest)), repoRoot);
|
|
186
|
-
}
|
|
187
|
-
throw new Error(`failure searching for ${this.localWrapper}`, { cause: error });
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
normalizePath(thePath) {
|
|
191
|
-
const normalized = path.resolve(thePath).normalize();
|
|
192
|
-
return process.platform === 'win32' ? normalized.toLowerCase() : normalized;
|
|
193
|
-
}
|
|
194
158
|
}
|