@trustify-da/trustify-da-javascript-client 0.3.0-ea.29f6867 → 0.3.0-ea.2ea1d77
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 +79 -6
- package/dist/package.json +5 -0
- package/dist/src/analysis.d.ts +14 -0
- package/dist/src/analysis.js +47 -1
- package/dist/src/batch_opts.d.ts +24 -0
- package/dist/src/batch_opts.js +35 -0
- package/dist/src/cli.js +121 -3
- package/dist/src/cyclone_dx_sbom.js +2 -1
- package/dist/src/index.d.ts +62 -1
- package/dist/src/index.js +265 -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/provider.d.ts +6 -3
- package/dist/src/provider.js +8 -5
- package/dist/src/providers/base_javascript.d.ts +9 -3
- package/dist/src/providers/base_javascript.js +18 -10
- package/dist/src/providers/javascript_pnpm.d.ts +1 -1
- package/dist/src/providers/javascript_pnpm.js +2 -2
- package/dist/src/providers/rust_cargo.d.ts +52 -0
- package/dist/src/providers/rust_cargo.js +614 -0
- package/dist/src/workspace.d.ts +61 -0
- package/dist/src/workspace.js +256 -0
- package/package.json +6 -1
package/dist/src/index.js
CHANGED
|
@@ -1,17 +1,22 @@
|
|
|
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 { discoverWorkspaceCrates, discoverWorkspacePackages, filterManifestPathsByDiscoveryIgnore, resolveWorkspaceDiscoveryIgnore, validatePackageJson, } from './workspace.js';
|
|
7
10
|
import.meta.dirname;
|
|
8
11
|
import * as url from 'url';
|
|
9
12
|
export { parseImageRef } from "./oci_image/utils.js";
|
|
10
13
|
export { ImageRef } from "./oci_image/images.js";
|
|
11
14
|
export { getProjectLicense, findLicenseFilePath, identifyLicense, getLicenseDetails, licensesFromReport, normalizeLicensesResponse, runLicenseCheck, getCompatibility } from "./license/index.js";
|
|
12
|
-
export default { componentAnalysis, stackAnalysis, imageAnalysis, validateToken };
|
|
15
|
+
export default { componentAnalysis, stackAnalysis, stackAnalysisBatch, imageAnalysis, validateToken };
|
|
16
|
+
export { discoverWorkspacePackages, discoverWorkspaceCrates, validatePackageJson, resolveWorkspaceDiscoveryIgnore, filterManifestPathsByDiscoveryIgnore, resolveContinueOnError, resolveBatchMetadata, };
|
|
13
17
|
/**
|
|
14
18
|
* @typedef {{
|
|
19
|
+
* TRUSTIFY_DA_CARGO_PATH?: string | undefined,
|
|
15
20
|
* TRUSTIFY_DA_DOCKER_PATH?: string | undefined,
|
|
16
21
|
* TRUSTIFY_DA_GO_MVS_LOGIC_ENABLED?: string | undefined,
|
|
17
22
|
* TRUSTIFY_DA_GO_PATH?: string | undefined,
|
|
@@ -36,14 +41,34 @@ export default { componentAnalysis, stackAnalysis, imageAnalysis, validateToken
|
|
|
36
41
|
* TRUSTIFY_DA_SYFT_CONFIG_PATH?: string | undefined,
|
|
37
42
|
* TRUSTIFY_DA_SYFT_PATH?: string | undefined,
|
|
38
43
|
* TRUSTIFY_DA_YARN_PATH?: string | undefined,
|
|
44
|
+
* TRUSTIFY_DA_WORKSPACE_DIR?: string | undefined,
|
|
39
45
|
* TRUSTIFY_DA_LICENSE_CHECK?: string | undefined,
|
|
40
46
|
* MATCH_MANIFEST_VERSIONS?: string | undefined,
|
|
41
47
|
* TRUSTIFY_DA_SOURCE?: string | undefined,
|
|
42
48
|
* TRUSTIFY_DA_TOKEN?: string | undefined,
|
|
43
49
|
* TRUSTIFY_DA_TELEMETRY_ID?: string | undefined,
|
|
44
|
-
*
|
|
50
|
+
* TRUSTIFY_DA_WORKSPACE_DIR?: string | undefined,
|
|
51
|
+
* batchConcurrency?: number | undefined,
|
|
52
|
+
* TRUSTIFY_DA_BATCH_CONCURRENCY?: string | undefined,
|
|
53
|
+
* workspaceDiscoveryIgnore?: string[] | undefined,
|
|
54
|
+
* TRUSTIFY_DA_WORKSPACE_DISCOVERY_IGNORE?: string | undefined,
|
|
55
|
+
* continueOnError?: boolean | undefined,
|
|
56
|
+
* TRUSTIFY_DA_CONTINUE_ON_ERROR?: string | undefined,
|
|
57
|
+
* batchMetadata?: boolean | undefined,
|
|
58
|
+
* TRUSTIFY_DA_BATCH_METADATA?: string | undefined,
|
|
59
|
+
* [key: string]: string | number | boolean | string[] | undefined,
|
|
45
60
|
* }} Options
|
|
46
61
|
*/
|
|
62
|
+
/**
|
|
63
|
+
* @typedef {{
|
|
64
|
+
* workspaceRoot: string,
|
|
65
|
+
* ecosystem: 'javascript' | 'cargo' | 'unknown',
|
|
66
|
+
* total: number,
|
|
67
|
+
* successful: number,
|
|
68
|
+
* failed: number,
|
|
69
|
+
* errors: Array<{ manifestPath: string, phase: 'validation' | 'sbom', reason: string }>
|
|
70
|
+
* }} BatchAnalysisMetadata
|
|
71
|
+
*/
|
|
47
72
|
/**
|
|
48
73
|
* Logs messages to the console if the TRUSTIFY_DA_DEBUG environment variable is set to "true".
|
|
49
74
|
* @param {string} alongsideText - The text to prepend to the log message.
|
|
@@ -129,7 +154,7 @@ export function selectTrustifyDABackend(opts = {}) {
|
|
|
129
154
|
async function stackAnalysis(manifest, html = false, opts = {}) {
|
|
130
155
|
const theUrl = selectTrustifyDABackend(opts);
|
|
131
156
|
fs.accessSync(manifest, fs.constants.R_OK); // throws error if file unreadable
|
|
132
|
-
let provider = match(manifest, availableProviders); // throws error if no matching provider
|
|
157
|
+
let provider = match(manifest, availableProviders, opts); // throws error if no matching provider
|
|
133
158
|
return await analysis.requestStack(provider, manifest, theUrl, html, opts); // throws error request sending failed
|
|
134
159
|
}
|
|
135
160
|
/**
|
|
@@ -143,7 +168,7 @@ async function componentAnalysis(manifest, opts = {}) {
|
|
|
143
168
|
const theUrl = selectTrustifyDABackend(opts);
|
|
144
169
|
fs.accessSync(manifest, fs.constants.R_OK);
|
|
145
170
|
opts["manifest-type"] = path.basename(manifest);
|
|
146
|
-
let provider = match(manifest, availableProviders); // throws error if no matching provider
|
|
171
|
+
let provider = match(manifest, availableProviders, opts); // throws error if no matching provider
|
|
147
172
|
return await analysis.requestComponent(provider, manifest, theUrl, opts); // throws error request sending failed
|
|
148
173
|
}
|
|
149
174
|
/**
|
|
@@ -176,6 +201,242 @@ async function imageAnalysis(imageRefs, html = false, opts = {}) {
|
|
|
176
201
|
const theUrl = selectTrustifyDABackend(opts);
|
|
177
202
|
return await analysis.requestImages(imageRefs, theUrl, html, opts);
|
|
178
203
|
}
|
|
204
|
+
/**
|
|
205
|
+
* Max concurrent SBOM generations for batch workspace analysis. Env/opts override default 10.
|
|
206
|
+
* @param {Options} opts
|
|
207
|
+
* @returns {number}
|
|
208
|
+
* @private
|
|
209
|
+
*/
|
|
210
|
+
function resolveBatchConcurrency(opts) {
|
|
211
|
+
const fromEnv = getCustom('TRUSTIFY_DA_BATCH_CONCURRENCY', null, opts);
|
|
212
|
+
const raw = opts.batchConcurrency ?? fromEnv ?? '10';
|
|
213
|
+
const n = typeof raw === 'number' ? raw : parseInt(String(raw), 10);
|
|
214
|
+
if (!Number.isFinite(n) || n < 1) {
|
|
215
|
+
return 10;
|
|
216
|
+
}
|
|
217
|
+
return Math.min(256, n);
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* @param {string} root
|
|
221
|
+
* @param {'javascript' | 'cargo' | 'unknown'} ecosystem
|
|
222
|
+
* @param {number} totalSbomAttempts
|
|
223
|
+
* @param {number} successfulSbomCount
|
|
224
|
+
* @param {Array<{ manifestPath: string, phase: 'validation' | 'sbom', reason: string }>} errors
|
|
225
|
+
* @returns {BatchAnalysisMetadata}
|
|
226
|
+
* @private
|
|
227
|
+
*/
|
|
228
|
+
function buildBatchAnalysisMetadata(root, ecosystem, totalSbomAttempts, successfulSbomCount, errors) {
|
|
229
|
+
return {
|
|
230
|
+
workspaceRoot: root,
|
|
231
|
+
ecosystem,
|
|
232
|
+
total: totalSbomAttempts,
|
|
233
|
+
successful: successfulSbomCount,
|
|
234
|
+
failed: errors.length,
|
|
235
|
+
errors: [...errors],
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* @typedef {{ ok: true, purl: string, sbom: object } | { ok: false, manifestPath: string, reason: string }} SbomResult
|
|
240
|
+
*/
|
|
241
|
+
/**
|
|
242
|
+
* Generate an SBOM for a single manifest, returning a normalized result.
|
|
243
|
+
*
|
|
244
|
+
* @param {string} manifestPath
|
|
245
|
+
* @param {Options} workspaceOpts - opts with `TRUSTIFY_DA_WORKSPACE_DIR` set
|
|
246
|
+
* @returns {Promise<SbomResult>}
|
|
247
|
+
* @private
|
|
248
|
+
*/
|
|
249
|
+
async function generateOneSbom(manifestPath, workspaceOpts) {
|
|
250
|
+
const provider = match(manifestPath, availableProviders, workspaceOpts);
|
|
251
|
+
const provided = await provider.provideStack(manifestPath, workspaceOpts);
|
|
252
|
+
const sbom = JSON.parse(provided.content);
|
|
253
|
+
const purl = sbom?.metadata?.component?.purl || sbom?.metadata?.component?.['bom-ref'];
|
|
254
|
+
if (!purl) {
|
|
255
|
+
return { ok: false, manifestPath, reason: 'missing purl in SBOM' };
|
|
256
|
+
}
|
|
257
|
+
return { ok: true, purl, sbom };
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Detect the workspace ecosystem and discover manifest paths.
|
|
261
|
+
*
|
|
262
|
+
* @param {string} root - Resolved workspace root
|
|
263
|
+
* @param {Options} opts
|
|
264
|
+
* @returns {Promise<{ ecosystem: 'javascript' | 'cargo' | 'unknown', manifestPaths: string[] }>}
|
|
265
|
+
* @private
|
|
266
|
+
*/
|
|
267
|
+
async function detectWorkspaceManifests(root, opts) {
|
|
268
|
+
const cargoToml = path.join(root, 'Cargo.toml');
|
|
269
|
+
const cargoLock = path.join(root, 'Cargo.lock');
|
|
270
|
+
const packageJson = path.join(root, 'package.json');
|
|
271
|
+
if (fs.existsSync(cargoToml) && fs.existsSync(cargoLock)) {
|
|
272
|
+
return { ecosystem: 'cargo', manifestPaths: await discoverWorkspaceCrates(root, opts) };
|
|
273
|
+
}
|
|
274
|
+
const hasJsLock = fs.existsSync(path.join(root, 'pnpm-lock.yaml'))
|
|
275
|
+
|| fs.existsSync(path.join(root, 'yarn.lock'))
|
|
276
|
+
|| fs.existsSync(path.join(root, 'package-lock.json'));
|
|
277
|
+
if (fs.existsSync(packageJson) && hasJsLock) {
|
|
278
|
+
let manifestPaths = await discoverWorkspacePackages(root, opts);
|
|
279
|
+
if (manifestPaths.length === 0) {
|
|
280
|
+
manifestPaths = [packageJson];
|
|
281
|
+
}
|
|
282
|
+
return { ecosystem: 'javascript', manifestPaths };
|
|
283
|
+
}
|
|
284
|
+
return { ecosystem: 'unknown', manifestPaths: [] };
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Validate discovered JS package.json manifests, collecting errors.
|
|
288
|
+
*
|
|
289
|
+
* @param {string[]} manifestPaths
|
|
290
|
+
* @param {boolean} continueOnError
|
|
291
|
+
* @param {Array<{ manifestPath: string, phase: 'validation' | 'sbom', reason: string }>} collectedErrors - mutated in place
|
|
292
|
+
* @returns {{ validPaths: string[] }}
|
|
293
|
+
* @throws {Error} on first invalid manifest when `continueOnError` is false
|
|
294
|
+
* @private
|
|
295
|
+
*/
|
|
296
|
+
function validateJsManifests(manifestPaths, continueOnError, collectedErrors) {
|
|
297
|
+
const validPaths = [];
|
|
298
|
+
for (const p of manifestPaths) {
|
|
299
|
+
const v = validatePackageJson(p);
|
|
300
|
+
if (v.valid) {
|
|
301
|
+
validPaths.push(p);
|
|
302
|
+
}
|
|
303
|
+
else {
|
|
304
|
+
collectedErrors.push({ manifestPath: p, phase: 'validation', reason: v.error });
|
|
305
|
+
console.warn(`Skipping invalid package.json (${v.error}): ${p}`);
|
|
306
|
+
if (!continueOnError) {
|
|
307
|
+
throw new Error(`Invalid package.json (${v.error}): ${p}`);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
return { validPaths };
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* Generate SBOMs for all manifests. In fail-fast mode, stops on first error.
|
|
315
|
+
* In continue-on-error mode, runs concurrently and collects failures.
|
|
316
|
+
*
|
|
317
|
+
* @param {string[]} manifestPaths
|
|
318
|
+
* @param {Options} workspaceOpts
|
|
319
|
+
* @param {boolean} continueOnError
|
|
320
|
+
* @param {number} concurrency
|
|
321
|
+
* @param {Array<{ manifestPath: string, phase: 'validation' | 'sbom', reason: string }>} collectedErrors - mutated in place
|
|
322
|
+
* @returns {Promise<Object.<string, object>>} sbomByPurl map
|
|
323
|
+
* @throws {Error} on first SBOM failure when `continueOnError` is false
|
|
324
|
+
* @private
|
|
325
|
+
*/
|
|
326
|
+
async function generateSboms(manifestPaths, workspaceOpts, continueOnError, concurrency, collectedErrors) {
|
|
327
|
+
/** @type {SbomResult[]} */
|
|
328
|
+
const results = [];
|
|
329
|
+
if (!continueOnError) {
|
|
330
|
+
for (const manifestPath of manifestPaths) {
|
|
331
|
+
const result = await generateOneSbom(manifestPath, workspaceOpts);
|
|
332
|
+
if (!result.ok) {
|
|
333
|
+
collectedErrors.push({ manifestPath: result.manifestPath, phase: 'sbom', reason: result.reason });
|
|
334
|
+
throw new Error(`${result.manifestPath}: ${result.reason}`);
|
|
335
|
+
}
|
|
336
|
+
results.push(result);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
else {
|
|
340
|
+
const limit = pLimit(concurrency);
|
|
341
|
+
const settled = await Promise.all(manifestPaths.map(manifestPath => limit(async () => {
|
|
342
|
+
try {
|
|
343
|
+
return await generateOneSbom(manifestPath, workspaceOpts);
|
|
344
|
+
}
|
|
345
|
+
catch (err) {
|
|
346
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
347
|
+
if (process.env["TRUSTIFY_DA_DEBUG"] === "true") {
|
|
348
|
+
console.log(`Skipping ${manifestPath}: ${msg}`);
|
|
349
|
+
}
|
|
350
|
+
return { ok: false, manifestPath, reason: msg };
|
|
351
|
+
}
|
|
352
|
+
})));
|
|
353
|
+
for (const r of settled) {
|
|
354
|
+
results.push(r);
|
|
355
|
+
if (!r.ok) {
|
|
356
|
+
collectedErrors.push({ manifestPath: r.manifestPath, phase: 'sbom', reason: r.reason });
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
const sbomByPurl = {};
|
|
361
|
+
for (const r of results) {
|
|
362
|
+
if (r.ok) {
|
|
363
|
+
sbomByPurl[r.purl] = r.sbom;
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
return sbomByPurl;
|
|
367
|
+
}
|
|
368
|
+
/**
|
|
369
|
+
* Create an Error with optional `batchMetadata` attached.
|
|
370
|
+
* @param {string} message
|
|
371
|
+
* @param {boolean} wantMetadata
|
|
372
|
+
* @param {BatchAnalysisMetadata} [metadata]
|
|
373
|
+
* @returns {Error}
|
|
374
|
+
* @private
|
|
375
|
+
*/
|
|
376
|
+
function batchError(message, wantMetadata, metadata) {
|
|
377
|
+
const err = new Error(message);
|
|
378
|
+
if (wantMetadata && metadata) {
|
|
379
|
+
err.batchMetadata = metadata;
|
|
380
|
+
}
|
|
381
|
+
return err;
|
|
382
|
+
}
|
|
383
|
+
/**
|
|
384
|
+
* Get stack analysis for all workspace packages/crates (batch).
|
|
385
|
+
* Detects ecosystem from workspace root: Cargo (Cargo.toml + Cargo.lock) or JS/TS (package.json + lock file).
|
|
386
|
+
* SBOMs are generated in parallel (see `batchConcurrency`) unless `continueOnError: false` (fail-fast sequential).
|
|
387
|
+
* With `opts.batchMetadata` / `TRUSTIFY_DA_BATCH_METADATA`, returns `{ analysis, metadata }` including validation and SBOM errors.
|
|
388
|
+
*
|
|
389
|
+
* @param {string} workspaceRoot - Path to workspace root (containing lock file and workspace config)
|
|
390
|
+
* @param {boolean} [html=false] - true returns HTML, false returns JSON report
|
|
391
|
+
* @param {Options} [opts={}] - `batchConcurrency`, discovery ignores, `continueOnError` (default true), `batchMetadata` (default false)
|
|
392
|
+
* @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 }>}
|
|
393
|
+
* @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.
|
|
394
|
+
*/
|
|
395
|
+
async function stackAnalysisBatch(workspaceRoot, html = false, opts = {}) {
|
|
396
|
+
const theUrl = selectTrustifyDABackend(opts);
|
|
397
|
+
const root = path.resolve(workspaceRoot);
|
|
398
|
+
fs.accessSync(root, fs.constants.R_OK);
|
|
399
|
+
const continueOnError = resolveContinueOnError(opts);
|
|
400
|
+
const wantMetadata = resolveBatchMetadata(opts);
|
|
401
|
+
/** @type {Array<{ manifestPath: string, phase: 'validation' | 'sbom', reason: string }>} */
|
|
402
|
+
const collectedErrors = [];
|
|
403
|
+
const { ecosystem, manifestPaths: discovered } = await detectWorkspaceManifests(root, opts);
|
|
404
|
+
let manifestPaths = discovered;
|
|
405
|
+
if (ecosystem === 'javascript') {
|
|
406
|
+
try {
|
|
407
|
+
const { validPaths } = validateJsManifests(manifestPaths, continueOnError, collectedErrors);
|
|
408
|
+
manifestPaths = validPaths;
|
|
409
|
+
}
|
|
410
|
+
catch (err) {
|
|
411
|
+
throw batchError(err.message, wantMetadata, buildBatchAnalysisMetadata(root, ecosystem, 0, 0, collectedErrors));
|
|
412
|
+
}
|
|
413
|
+
if (manifestPaths.length === 0 && discovered.length > 0) {
|
|
414
|
+
const detail = collectedErrors.map(e => `${e.manifestPath}: ${e.reason}`).join('; ');
|
|
415
|
+
throw batchError(`No valid packages after validation at ${root}. ${detail}`, wantMetadata, buildBatchAnalysisMetadata(root, ecosystem, 0, 0, collectedErrors));
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
if (manifestPaths.length === 0) {
|
|
419
|
+
throw new Error(`No workspace manifests found at ${root}. Ensure Cargo.toml+Cargo.lock or package.json+lock file exist.`);
|
|
420
|
+
}
|
|
421
|
+
const workspaceOpts = { ...opts, TRUSTIFY_DA_WORKSPACE_DIR: root };
|
|
422
|
+
const concurrency = resolveBatchConcurrency(opts);
|
|
423
|
+
let sbomByPurl;
|
|
424
|
+
try {
|
|
425
|
+
sbomByPurl = await generateSboms(manifestPaths, workspaceOpts, continueOnError, concurrency, collectedErrors);
|
|
426
|
+
}
|
|
427
|
+
catch (err) {
|
|
428
|
+
throw batchError(err.message, wantMetadata, buildBatchAnalysisMetadata(root, ecosystem, manifestPaths.length, 0, collectedErrors));
|
|
429
|
+
}
|
|
430
|
+
if (Object.keys(sbomByPurl).length === 0) {
|
|
431
|
+
throw batchError(`No valid SBOMs produced from ${manifestPaths.length} manifest(s) at ${root}`, wantMetadata, buildBatchAnalysisMetadata(root, ecosystem, manifestPaths.length, 0, collectedErrors));
|
|
432
|
+
}
|
|
433
|
+
const analysisResult = await analysis.requestStackBatch(sbomByPurl, theUrl, html, opts);
|
|
434
|
+
const meta = buildBatchAnalysisMetadata(root, ecosystem, manifestPaths.length, Object.keys(sbomByPurl).length, collectedErrors);
|
|
435
|
+
if (wantMetadata) {
|
|
436
|
+
return { analysis: analysisResult, metadata: meta };
|
|
437
|
+
}
|
|
438
|
+
return analysisResult;
|
|
439
|
+
}
|
|
179
440
|
/**
|
|
180
441
|
* Validates the Exhort token.
|
|
181
442
|
* @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
|
-
}
|
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,9 @@ 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 rustCargoProvider from './providers/rust_cargo.js';
|
|
10
11
|
/** @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 */
|
|
12
|
+
/** @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
13
|
/**
|
|
13
14
|
* MUST include all providers here.
|
|
14
15
|
* @type {[Provider]}
|
|
@@ -17,11 +18,12 @@ export const availableProviders = [
|
|
|
17
18
|
new Java_maven(),
|
|
18
19
|
new Java_gradle_groovy(),
|
|
19
20
|
new Java_gradle_kotlin(),
|
|
20
|
-
new Javascript_npm(),
|
|
21
21
|
new Javascript_pnpm(),
|
|
22
22
|
new Javascript_yarn(),
|
|
23
|
+
new Javascript_npm(),
|
|
23
24
|
golangGomodulesProvider,
|
|
24
|
-
pythonPipProvider
|
|
25
|
+
pythonPipProvider,
|
|
26
|
+
rustCargoProvider
|
|
25
27
|
];
|
|
26
28
|
/**
|
|
27
29
|
* Match a provider by manifest type only (no lock file check). Used for license reading.
|
|
@@ -45,16 +47,17 @@ export function matchForLicense(manifestPath, providers) {
|
|
|
45
47
|
* Each provider MUST export 'provideStack' taking manifest path returning a {@link Provided}.
|
|
46
48
|
* @param {string} manifest - the name-type or path of the manifest
|
|
47
49
|
* @param {[Provider]} providers - list of providers to iterate over
|
|
50
|
+
* @param {{TRUSTIFY_DA_WORKSPACE_DIR?: string}} [opts={}] - optional; TRUSTIFY_DA_WORKSPACE_DIR overrides lock file location for workspaces
|
|
48
51
|
* @returns {Provider}
|
|
49
52
|
* @throws {Error} when the manifest is not supported and no provider was matched
|
|
50
53
|
*/
|
|
51
|
-
export function match(manifest, providers) {
|
|
54
|
+
export function match(manifest, providers, opts = {}) {
|
|
52
55
|
const manifestPath = path.parse(manifest);
|
|
53
56
|
const supported = providers.filter(prov => prov.isSupported(manifestPath.base));
|
|
54
57
|
if (supported.length === 0) {
|
|
55
58
|
throw new Error(`${manifestPath.base} is not supported`);
|
|
56
59
|
}
|
|
57
|
-
const provider = supported.find(prov => prov.validateLockFile(manifestPath.dir));
|
|
60
|
+
const provider = supported.find(prov => prov.validateLockFile(manifestPath.dir, opts));
|
|
58
61
|
if (!provider) {
|
|
59
62
|
throw new Error(`${manifestPath.base} requires a lock file. Use your preferred package manager to generate the lock file.`);
|
|
60
63
|
}
|
|
@@ -67,11 +67,16 @@ export default class Base_javascript {
|
|
|
67
67
|
*/
|
|
68
68
|
isSupported(manifestName: string): boolean;
|
|
69
69
|
/**
|
|
70
|
-
* Checks if a required lock file exists in the
|
|
70
|
+
* Checks if a required lock file exists in the manifest directory or at the workspace root.
|
|
71
|
+
* When TRUSTIFY_DA_WORKSPACE_DIR is provided (via env var or opts),
|
|
72
|
+
* checks only that directory for the lock file.
|
|
71
73
|
* @param {string} manifestDir - The base directory where the manifest is located
|
|
74
|
+
* @param {{TRUSTIFY_DA_WORKSPACE_DIR?: string}} [opts={}] - optional workspace root
|
|
72
75
|
* @returns {boolean} True if the lock file exists
|
|
73
76
|
*/
|
|
74
|
-
validateLockFile(manifestDir: string
|
|
77
|
+
validateLockFile(manifestDir: string, opts?: {
|
|
78
|
+
TRUSTIFY_DA_WORKSPACE_DIR?: string;
|
|
79
|
+
}): boolean;
|
|
75
80
|
/**
|
|
76
81
|
* Provides content and content type for stack analysis
|
|
77
82
|
* @param {string} manifestPath - The manifest path or name
|
|
@@ -95,10 +100,11 @@ export default class Base_javascript {
|
|
|
95
100
|
/**
|
|
96
101
|
* Builds the dependency tree for the project
|
|
97
102
|
* @param {boolean} includeTransitive - Whether to include transitive dependencies
|
|
103
|
+
* @param {Object} [opts={}] - Configuration options; when `TRUSTIFY_DA_WORKSPACE_DIR` is set, commands run from workspace root
|
|
98
104
|
* @returns {Object} The dependency tree
|
|
99
105
|
* @protected
|
|
100
106
|
*/
|
|
101
|
-
protected _buildDependencyTree(includeTransitive: boolean): any;
|
|
107
|
+
protected _buildDependencyTree(includeTransitive: boolean, opts?: any): any;
|
|
102
108
|
/**
|
|
103
109
|
* Recursively builds the Sbom from the JSON that npm listing returns
|
|
104
110
|
* @param {Sbom} sbom - The SBOM object to add dependencies to
|
|
@@ -3,7 +3,7 @@ import os from "node:os";
|
|
|
3
3
|
import path from 'node:path';
|
|
4
4
|
import { getLicense } from '../license/license_utils.js';
|
|
5
5
|
import Sbom from '../sbom.js';
|
|
6
|
-
import { getCustom, getCustomPath, invokeCommand, toPurl, toPurlFromString } from
|
|
6
|
+
import { getCustom, getCustomPath, invokeCommand, toPurl, toPurlFromString } from '../tools.js';
|
|
7
7
|
import Manifest from './manifest.js';
|
|
8
8
|
/** @typedef {import('../provider').Provider} */
|
|
9
9
|
/** @typedef {import('../provider').Provided} Provided */
|
|
@@ -97,12 +97,17 @@ export default class Base_javascript {
|
|
|
97
97
|
return 'package.json' === manifestName;
|
|
98
98
|
}
|
|
99
99
|
/**
|
|
100
|
-
* Checks if a required lock file exists in the
|
|
100
|
+
* Checks if a required lock file exists in the manifest directory or at the workspace root.
|
|
101
|
+
* When TRUSTIFY_DA_WORKSPACE_DIR is provided (via env var or opts),
|
|
102
|
+
* checks only that directory for the lock file.
|
|
101
103
|
* @param {string} manifestDir - The base directory where the manifest is located
|
|
104
|
+
* @param {{TRUSTIFY_DA_WORKSPACE_DIR?: string}} [opts={}] - optional workspace root
|
|
102
105
|
* @returns {boolean} True if the lock file exists
|
|
103
106
|
*/
|
|
104
|
-
validateLockFile(manifestDir) {
|
|
105
|
-
const
|
|
107
|
+
validateLockFile(manifestDir, opts = {}) {
|
|
108
|
+
const workspaceDir = getCustom('TRUSTIFY_DA_WORKSPACE_DIR', null, opts);
|
|
109
|
+
const dirToCheck = workspaceDir ? path.resolve(workspaceDir) : manifestDir;
|
|
110
|
+
const lock = path.join(dirToCheck, this._lockFileName());
|
|
106
111
|
return fs.existsSync(lock);
|
|
107
112
|
}
|
|
108
113
|
/**
|
|
@@ -159,14 +164,17 @@ export default class Base_javascript {
|
|
|
159
164
|
/**
|
|
160
165
|
* Builds the dependency tree for the project
|
|
161
166
|
* @param {boolean} includeTransitive - Whether to include transitive dependencies
|
|
167
|
+
* @param {Object} [opts={}] - Configuration options; when `TRUSTIFY_DA_WORKSPACE_DIR` is set, commands run from workspace root
|
|
162
168
|
* @returns {Object} The dependency tree
|
|
163
169
|
* @protected
|
|
164
170
|
*/
|
|
165
|
-
_buildDependencyTree(includeTransitive) {
|
|
171
|
+
_buildDependencyTree(includeTransitive, opts = {}) {
|
|
166
172
|
this._version();
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
173
|
+
const manifestDir = path.dirname(this.#manifest.manifestPath);
|
|
174
|
+
const workspaceDir = getCustom('TRUSTIFY_DA_WORKSPACE_DIR', null, opts);
|
|
175
|
+
const cmdDir = workspaceDir ? path.resolve(workspaceDir) : manifestDir;
|
|
176
|
+
this.#createLockFile(cmdDir);
|
|
177
|
+
let output = this.#executeListCmd(includeTransitive, cmdDir);
|
|
170
178
|
output = this._parseDepTreeOutput(output);
|
|
171
179
|
return JSON.parse(output);
|
|
172
180
|
}
|
|
@@ -177,7 +185,7 @@ export default class Base_javascript {
|
|
|
177
185
|
* @private
|
|
178
186
|
*/
|
|
179
187
|
#getSBOM(opts = {}) {
|
|
180
|
-
const depsObject = this._buildDependencyTree(true);
|
|
188
|
+
const depsObject = this._buildDependencyTree(true, opts);
|
|
181
189
|
let mainComponent = toPurl(purlType, this.#manifest.name, this.#manifest.version);
|
|
182
190
|
const license = this.readLicenseFromManifest(this.#manifest.manifestPath);
|
|
183
191
|
let sbom = new Sbom();
|
|
@@ -229,7 +237,7 @@ export default class Base_javascript {
|
|
|
229
237
|
* @private
|
|
230
238
|
*/
|
|
231
239
|
#getDirectDependencySbom(opts = {}) {
|
|
232
|
-
const depTree = this._buildDependencyTree(false);
|
|
240
|
+
const depTree = this._buildDependencyTree(false, opts);
|
|
233
241
|
let mainComponent = toPurl(purlType, this.#manifest.name, this.#manifest.version);
|
|
234
242
|
const license = this.readLicenseFromManifest(this.#manifest.manifestPath);
|
|
235
243
|
let sbom = new Sbom();
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export default class Javascript_pnpm extends Base_javascript {
|
|
2
2
|
_listCmdArgs(includeTransitive: any): string[];
|
|
3
|
-
_buildDependencyTree(includeTransitive: any,
|
|
3
|
+
_buildDependencyTree(includeTransitive: any, opts?: {}): any;
|
|
4
4
|
}
|
|
5
5
|
import Base_javascript from './base_javascript.js';
|
|
@@ -12,8 +12,8 @@ export default class Javascript_pnpm extends Base_javascript {
|
|
|
12
12
|
_updateLockFileCmdArgs() {
|
|
13
13
|
return ['install', '--frozen-lockfile'];
|
|
14
14
|
}
|
|
15
|
-
_buildDependencyTree(includeTransitive,
|
|
16
|
-
const tree = super._buildDependencyTree(includeTransitive,
|
|
15
|
+
_buildDependencyTree(includeTransitive, opts = {}) {
|
|
16
|
+
const tree = super._buildDependencyTree(includeTransitive, opts);
|
|
17
17
|
if (Array.isArray(tree) && tree.length > 0) {
|
|
18
18
|
return tree[0];
|
|
19
19
|
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
declare namespace _default {
|
|
2
|
+
export { isSupported };
|
|
3
|
+
export { validateLockFile };
|
|
4
|
+
export { provideComponent };
|
|
5
|
+
export { provideStack };
|
|
6
|
+
export { readLicenseFromManifest };
|
|
7
|
+
}
|
|
8
|
+
export default _default;
|
|
9
|
+
export type Provided = import("../provider").Provided;
|
|
10
|
+
/**
|
|
11
|
+
* @param {string} manifestName - the subject manifest name-type
|
|
12
|
+
* @returns {boolean} - return true if `Cargo.toml` is the manifest name-type
|
|
13
|
+
*/
|
|
14
|
+
declare function isSupported(manifestName: string): boolean;
|
|
15
|
+
/**
|
|
16
|
+
* Validates that Cargo.lock exists in the manifest directory or in a parent
|
|
17
|
+
* workspace root directory. In Cargo workspaces the lock file always lives at
|
|
18
|
+
* the workspace root, so when a member crate's Cargo.toml is provided we walk
|
|
19
|
+
* up the directory tree looking for Cargo.lock (stopping when we find a
|
|
20
|
+
* Cargo.toml that contains a [workspace] section, or when we reach the
|
|
21
|
+
* filesystem root).
|
|
22
|
+
* When TRUSTIFY_DA_WORKSPACE_DIR is provided (via env var or opts),
|
|
23
|
+
* checks only that directory for Cargo.lock — no walk-up.
|
|
24
|
+
* @param {string} manifestDir - the directory where the manifest lies
|
|
25
|
+
* @param {{TRUSTIFY_DA_WORKSPACE_DIR?: string}} [opts={}] - optional workspace root
|
|
26
|
+
* @returns {boolean} true if Cargo.lock is found
|
|
27
|
+
*/
|
|
28
|
+
declare function validateLockFile(manifestDir: string, opts?: {
|
|
29
|
+
TRUSTIFY_DA_WORKSPACE_DIR?: string;
|
|
30
|
+
}): boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Provide content and content type for Cargo component analysis.
|
|
33
|
+
* @param {string} manifest - path to Cargo.toml for component report
|
|
34
|
+
* @param {{}} [opts={}] - optional various options to pass along the application
|
|
35
|
+
* @returns {Provided}
|
|
36
|
+
*/
|
|
37
|
+
declare function provideComponent(manifest: string, opts?: {}): Provided;
|
|
38
|
+
/**
|
|
39
|
+
* Provide content and content type for Cargo stack analysis.
|
|
40
|
+
* @param {string} manifest - the manifest path
|
|
41
|
+
* @param {{}} [opts={}] - optional various options to pass along the application
|
|
42
|
+
* @returns {Provided}
|
|
43
|
+
*/
|
|
44
|
+
declare function provideStack(manifest: string, opts?: {}): Provided;
|
|
45
|
+
/**
|
|
46
|
+
* Read project license from Cargo.toml, with fallback to LICENSE file.
|
|
47
|
+
* Supports the `license` field under `[package]` (single crate / workspace
|
|
48
|
+
* with root) and under `[workspace.package]` (virtual workspaces).
|
|
49
|
+
* @param {string} manifestPath - path to Cargo.toml
|
|
50
|
+
* @returns {string|null} SPDX identifier or null
|
|
51
|
+
*/
|
|
52
|
+
declare function readLicenseFromManifest(manifestPath: string): string | null;
|