@trustify-da/trustify-da-javascript-client 0.3.0-ea.6549d2a → 0.3.0-ea.7144952
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 +107 -4
- package/dist/package.json +9 -1
- package/dist/src/analysis.d.ts +16 -0
- package/dist/src/analysis.js +50 -2
- 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 +63 -2
- package/dist/src/index.js +266 -5
- package/dist/src/license/index.d.ts +2 -2
- package/dist/src/license/index.js +4 -4
- package/dist/src/license/license_utils.d.ts +40 -0
- package/dist/src/license/license_utils.js +134 -0
- package/dist/src/license/licenses_api.js +9 -2
- package/dist/src/license/project_license.d.ts +1 -6
- package/dist/src/license/project_license.js +4 -81
- 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 +25 -15
- package/dist/src/providers/golang_gomodules.js +2 -1
- package/dist/src/providers/java_gradle.js +2 -1
- package/dist/src/providers/java_maven.d.ts +1 -1
- package/dist/src/providers/java_maven.js +10 -9
- package/dist/src/providers/javascript_pnpm.d.ts +1 -1
- package/dist/src/providers/javascript_pnpm.js +2 -2
- package/dist/src/providers/python_pip.js +2 -1
- 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-requirements.wasm +0 -0
- package/dist/src/workspace.d.ts +61 -0
- package/dist/src/workspace.js +256 -0
- package/package.json +10 -2
- package/dist/src/license/compatibility.d.ts +0 -18
- package/dist/src/license/compatibility.js +0 -45
package/dist/src/index.d.ts
CHANGED
|
@@ -18,11 +18,13 @@ export { ImageRef } from "./oci_image/images.js";
|
|
|
18
18
|
declare namespace _default {
|
|
19
19
|
export { componentAnalysis };
|
|
20
20
|
export { stackAnalysis };
|
|
21
|
+
export { stackAnalysisBatch };
|
|
21
22
|
export { imageAnalysis };
|
|
22
23
|
export { validateToken };
|
|
23
24
|
}
|
|
24
25
|
export default _default;
|
|
25
26
|
export type Options = {
|
|
27
|
+
TRUSTIFY_DA_CARGO_PATH?: string | undefined;
|
|
26
28
|
TRUSTIFY_DA_DOCKER_PATH?: string | undefined;
|
|
27
29
|
TRUSTIFY_DA_GO_MVS_LOGIC_ENABLED?: string | undefined;
|
|
28
30
|
TRUSTIFY_DA_GO_PATH?: string | undefined;
|
|
@@ -47,12 +49,43 @@ export type Options = {
|
|
|
47
49
|
TRUSTIFY_DA_SYFT_CONFIG_PATH?: string | undefined;
|
|
48
50
|
TRUSTIFY_DA_SYFT_PATH?: string | undefined;
|
|
49
51
|
TRUSTIFY_DA_YARN_PATH?: string | undefined;
|
|
52
|
+
TRUSTIFY_DA_WORKSPACE_DIR?: string | undefined;
|
|
50
53
|
TRUSTIFY_DA_LICENSE_CHECK?: string | undefined;
|
|
51
54
|
MATCH_MANIFEST_VERSIONS?: string | undefined;
|
|
52
55
|
TRUSTIFY_DA_SOURCE?: string | undefined;
|
|
53
56
|
TRUSTIFY_DA_TOKEN?: string | undefined;
|
|
54
57
|
TRUSTIFY_DA_TELEMETRY_ID?: string | undefined;
|
|
55
|
-
|
|
58
|
+
TRUSTIFY_DA_WORKSPACE_DIR?: string | undefined;
|
|
59
|
+
batchConcurrency?: number | undefined;
|
|
60
|
+
TRUSTIFY_DA_BATCH_CONCURRENCY?: string | undefined;
|
|
61
|
+
workspaceDiscoveryIgnore?: string[] | undefined;
|
|
62
|
+
TRUSTIFY_DA_WORKSPACE_DISCOVERY_IGNORE?: string | undefined;
|
|
63
|
+
continueOnError?: boolean | undefined;
|
|
64
|
+
TRUSTIFY_DA_CONTINUE_ON_ERROR?: string | undefined;
|
|
65
|
+
batchMetadata?: boolean | undefined;
|
|
66
|
+
TRUSTIFY_DA_BATCH_METADATA?: string | undefined;
|
|
67
|
+
[key: string]: string | number | boolean | string[] | undefined;
|
|
68
|
+
};
|
|
69
|
+
export type BatchAnalysisMetadata = {
|
|
70
|
+
workspaceRoot: string;
|
|
71
|
+
ecosystem: "javascript" | "cargo" | "unknown";
|
|
72
|
+
total: number;
|
|
73
|
+
successful: number;
|
|
74
|
+
failed: number;
|
|
75
|
+
errors: Array<{
|
|
76
|
+
manifestPath: string;
|
|
77
|
+
phase: "validation" | "sbom";
|
|
78
|
+
reason: string;
|
|
79
|
+
}>;
|
|
80
|
+
};
|
|
81
|
+
export type SbomResult = {
|
|
82
|
+
ok: true;
|
|
83
|
+
purl: string;
|
|
84
|
+
sbom: object;
|
|
85
|
+
} | {
|
|
86
|
+
ok: false;
|
|
87
|
+
manifestPath: string;
|
|
88
|
+
reason: string;
|
|
56
89
|
};
|
|
57
90
|
/**
|
|
58
91
|
* Get component analysis report for a manifest content.
|
|
@@ -91,6 +124,26 @@ declare function stackAnalysis(manifest: string, html: false, opts?: Options | u
|
|
|
91
124
|
* or backend request failed
|
|
92
125
|
*/
|
|
93
126
|
declare function stackAnalysis(manifest: string, html?: boolean | undefined, opts?: Options | undefined): Promise<string | import("@trustify-da/trustify-da-api-model/model/v5/AnalysisReport").AnalysisReport>;
|
|
127
|
+
/**
|
|
128
|
+
* Get stack analysis for all workspace packages/crates (batch).
|
|
129
|
+
* Detects ecosystem from workspace root: Cargo (Cargo.toml + Cargo.lock) or JS/TS (package.json + lock file).
|
|
130
|
+
* SBOMs are generated in parallel (see `batchConcurrency`) unless `continueOnError: false` (fail-fast sequential).
|
|
131
|
+
* With `opts.batchMetadata` / `TRUSTIFY_DA_BATCH_METADATA`, returns `{ analysis, metadata }` including validation and SBOM errors.
|
|
132
|
+
*
|
|
133
|
+
* @param {string} workspaceRoot - Path to workspace root (containing lock file and workspace config)
|
|
134
|
+
* @param {boolean} [html=false] - true returns HTML, false returns JSON report
|
|
135
|
+
* @param {Options} [opts={}] - `batchConcurrency`, discovery ignores, `continueOnError` (default true), `batchMetadata` (default false)
|
|
136
|
+
* @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 }>}
|
|
137
|
+
* @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.
|
|
138
|
+
*/
|
|
139
|
+
declare function stackAnalysisBatch(workspaceRoot: string, html?: boolean, opts?: Options): Promise<string | {
|
|
140
|
+
[x: string]: import("@trustify-da/trustify-da-api-model/model/v5/AnalysisReport").AnalysisReport;
|
|
141
|
+
} | {
|
|
142
|
+
analysis: string | {
|
|
143
|
+
[x: string]: import("@trustify-da/trustify-da-api-model/model/v5/AnalysisReport").AnalysisReport;
|
|
144
|
+
};
|
|
145
|
+
metadata: BatchAnalysisMetadata;
|
|
146
|
+
}>;
|
|
94
147
|
/**
|
|
95
148
|
* @overload
|
|
96
149
|
* @param {Array<string>} imageRefs
|
|
@@ -131,4 +184,12 @@ declare function imageAnalysis(imageRefs: Array<string>, html?: boolean | undefi
|
|
|
131
184
|
* @throws {Error} if the backend request failed.
|
|
132
185
|
*/
|
|
133
186
|
declare function validateToken(opts?: Options): Promise<object>;
|
|
134
|
-
|
|
187
|
+
import { discoverWorkspacePackages } from './workspace.js';
|
|
188
|
+
import { discoverWorkspaceCrates } from './workspace.js';
|
|
189
|
+
import { validatePackageJson } from './workspace.js';
|
|
190
|
+
import { resolveWorkspaceDiscoveryIgnore } from './workspace.js';
|
|
191
|
+
import { filterManifestPathsByDiscoveryIgnore } from './workspace.js';
|
|
192
|
+
import { resolveContinueOnError } from './batch_opts.js';
|
|
193
|
+
import { resolveBatchMetadata } from './batch_opts.js';
|
|
194
|
+
export { discoverWorkspacePackages, discoverWorkspaceCrates, validatePackageJson, resolveWorkspaceDiscoveryIgnore, filterManifestPathsByDiscoveryIgnore, resolveContinueOnError, resolveBatchMetadata };
|
|
195
|
+
export { getProjectLicense, findLicenseFilePath, identifyLicense, getLicenseDetails, licensesFromReport, normalizeLicensesResponse, runLicenseCheck, getCompatibility } from "./license/index.js";
|
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
|
-
export { getProjectLicense, findLicenseFilePath,
|
|
12
|
-
export default { componentAnalysis, stackAnalysis, imageAnalysis, validateToken };
|
|
14
|
+
export { getProjectLicense, findLicenseFilePath, identifyLicense, getLicenseDetails, licensesFromReport, normalizeLicensesResponse, runLicenseCheck, getCompatibility } from "./license/index.js";
|
|
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.
|
|
@@ -23,6 +23,6 @@ export function runLicenseCheck(sbomContent: string, manifestPath: string, url:
|
|
|
23
23
|
}>;
|
|
24
24
|
error?: string;
|
|
25
25
|
}>;
|
|
26
|
-
export { getCompatibility } from "./
|
|
27
|
-
export { getProjectLicense, findLicenseFilePath, identifyLicense
|
|
26
|
+
export { getCompatibility } from "./license_utils.js";
|
|
27
|
+
export { getProjectLicense, findLicenseFilePath, identifyLicense } from "./project_license.js";
|
|
28
28
|
export { licensesFromReport, normalizeLicensesResponse, getLicenseDetails } from "./licenses_api.js";
|
|
@@ -3,10 +3,10 @@
|
|
|
3
3
|
*/
|
|
4
4
|
import { getProjectLicense, findLicenseFilePath, identifyLicense } from './project_license.js';
|
|
5
5
|
import { licensesFromReport, getLicenseDetails } from './licenses_api.js';
|
|
6
|
-
import { getCompatibility } from './
|
|
7
|
-
export { getProjectLicense, findLicenseFilePath, identifyLicense
|
|
6
|
+
import { getCompatibility } from './license_utils.js';
|
|
7
|
+
export { getProjectLicense, findLicenseFilePath, identifyLicense } from './project_license.js';
|
|
8
8
|
export { licensesFromReport, normalizeLicensesResponse, getLicenseDetails } from './licenses_api.js';
|
|
9
|
-
export { getCompatibility } from './
|
|
9
|
+
export { getCompatibility } from './license_utils.js';
|
|
10
10
|
/**
|
|
11
11
|
* Run full license check: resolve project license (with backend identification and details),
|
|
12
12
|
* get dependency licenses from analysis report, and compute incompatibilities.
|
|
@@ -20,7 +20,7 @@ export { getCompatibility } from './compatibility.js';
|
|
|
20
20
|
*/
|
|
21
21
|
export async function runLicenseCheck(sbomContent, manifestPath, url, opts = {}, analysisResult = null) {
|
|
22
22
|
// Resolve project license from manifest and LICENSE file
|
|
23
|
-
const projectLicense = getProjectLicense(manifestPath
|
|
23
|
+
const projectLicense = getProjectLicense(manifestPath);
|
|
24
24
|
// Try backend identification for LICENSE file (more accurate than local pattern matching)
|
|
25
25
|
const licenseFilePath = findLicenseFilePath(manifestPath);
|
|
26
26
|
let backendFileId = null;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Find LICENSE file path in the same directory as the manifest.
|
|
3
|
+
* @param {string} manifestPath
|
|
4
|
+
* @returns {string|null} - path to LICENSE file or null if not found
|
|
5
|
+
*/
|
|
6
|
+
export function findLicenseFilePath(manifestPath: string): string | null;
|
|
7
|
+
/**
|
|
8
|
+
* Very simple SPDX detection from common license text (first ~500 chars).
|
|
9
|
+
* @param {string} text
|
|
10
|
+
* @returns {string|null}
|
|
11
|
+
*/
|
|
12
|
+
export function detectSpdxFromText(text: string): string | null;
|
|
13
|
+
/**
|
|
14
|
+
* Read LICENSE file and detect SPDX identifier.
|
|
15
|
+
* @param {string} manifestPath - path to manifest
|
|
16
|
+
* @returns {string|null} - SPDX identifier from LICENSE file or null
|
|
17
|
+
*/
|
|
18
|
+
export function readLicenseFile(manifestPath: string): string | null;
|
|
19
|
+
/**
|
|
20
|
+
* Get project license from manifest or LICENSE file.
|
|
21
|
+
* Returns manifestLicense if provided, otherwise tries LICENSE file.
|
|
22
|
+
* @param {string|null} manifestLicense - license from manifest (or null)
|
|
23
|
+
* @param {string} manifestPath - path to manifest
|
|
24
|
+
* @returns {string|null} - SPDX identifier or null
|
|
25
|
+
*/
|
|
26
|
+
export function getLicense(manifestLicense: string | null, manifestPath: string): string | null;
|
|
27
|
+
/**
|
|
28
|
+
* Normalize SPDX identifier for comparison (lowercase, strip common suffixes).
|
|
29
|
+
* @param {string} spdxOrName
|
|
30
|
+
* @returns {string}
|
|
31
|
+
*/
|
|
32
|
+
export function normalizeSpdx(spdxOrName: string): string;
|
|
33
|
+
/**
|
|
34
|
+
* Check if a dependency's license is compatible with the project license based on backend categories.
|
|
35
|
+
*
|
|
36
|
+
* @param {string} [projectCategory] - backend category for project license: PERMISSIVE | WEAK_COPYLEFT | STRONG_COPYLEFT | UNKNOWN
|
|
37
|
+
* @param {string} [dependencyCategory] - backend category for dependency license: PERMISSIVE | WEAK_COPYLEFT | STRONG_COPYLEFT | UNKNOWN
|
|
38
|
+
* @returns {'compatible'|'incompatible'|'unknown'}
|
|
39
|
+
*/
|
|
40
|
+
export function getCompatibility(projectCategory?: string, dependencyCategory?: string): "compatible" | "incompatible" | "unknown";
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* License utilities: file reading, SPDX detection, normalization, compatibility.
|
|
3
|
+
* This module has NO dependencies on providers or backend to avoid circular dependencies.
|
|
4
|
+
*/
|
|
5
|
+
import fs from 'node:fs';
|
|
6
|
+
import path from 'node:path';
|
|
7
|
+
const LICENSE_FILES = ['LICENSE', 'LICENSE.md', 'LICENSE.txt'];
|
|
8
|
+
/**
|
|
9
|
+
* Find LICENSE file path in the same directory as the manifest.
|
|
10
|
+
* @param {string} manifestPath
|
|
11
|
+
* @returns {string|null} - path to LICENSE file or null if not found
|
|
12
|
+
*/
|
|
13
|
+
export function findLicenseFilePath(manifestPath) {
|
|
14
|
+
const manifestDir = path.dirname(path.resolve(manifestPath));
|
|
15
|
+
for (const name of LICENSE_FILES) {
|
|
16
|
+
const filePath = path.join(manifestDir, name);
|
|
17
|
+
try {
|
|
18
|
+
if (fs.statSync(filePath).isFile()) {
|
|
19
|
+
return filePath;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
// skip
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Very simple SPDX detection from common license text (first ~500 chars).
|
|
30
|
+
* @param {string} text
|
|
31
|
+
* @returns {string|null}
|
|
32
|
+
*/
|
|
33
|
+
export function detectSpdxFromText(text) {
|
|
34
|
+
const head = text.slice(0, 500);
|
|
35
|
+
if (/Apache License,?\s*Version 2\.0/i.test(head)) {
|
|
36
|
+
return 'Apache-2.0';
|
|
37
|
+
}
|
|
38
|
+
if (/MIT License/i.test(head) && /Permission is hereby granted/i.test(head)) {
|
|
39
|
+
return 'MIT';
|
|
40
|
+
}
|
|
41
|
+
if (/GNU AFFERO GENERAL PUBLIC LICENSE\s+Version 3/i.test(head)) {
|
|
42
|
+
return 'AGPL-3.0-only';
|
|
43
|
+
}
|
|
44
|
+
if (/GNU LESSER GENERAL PUBLIC LICENSE\s+Version 3/i.test(head)) {
|
|
45
|
+
return 'LGPL-3.0-only';
|
|
46
|
+
}
|
|
47
|
+
if (/GNU LESSER GENERAL PUBLIC LICENSE\s+Version 2\.1/i.test(head)) {
|
|
48
|
+
return 'LGPL-2.1-only';
|
|
49
|
+
}
|
|
50
|
+
if (/GNU GENERAL PUBLIC LICENSE\s+Version 2/i.test(head)) {
|
|
51
|
+
return 'GPL-2.0-only';
|
|
52
|
+
}
|
|
53
|
+
if (/GNU GENERAL PUBLIC LICENSE\s+Version 3/i.test(head)) {
|
|
54
|
+
return 'GPL-3.0-only';
|
|
55
|
+
}
|
|
56
|
+
if (/BSD 2-Clause/i.test(head)) {
|
|
57
|
+
return 'BSD-2-Clause';
|
|
58
|
+
}
|
|
59
|
+
if (/BSD 3-Clause/i.test(head)) {
|
|
60
|
+
return 'BSD-3-Clause';
|
|
61
|
+
}
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Read LICENSE file and detect SPDX identifier.
|
|
66
|
+
* @param {string} manifestPath - path to manifest
|
|
67
|
+
* @returns {string|null} - SPDX identifier from LICENSE file or null
|
|
68
|
+
*/
|
|
69
|
+
export function readLicenseFile(manifestPath) {
|
|
70
|
+
const licenseFilePath = findLicenseFilePath(manifestPath);
|
|
71
|
+
if (!licenseFilePath) {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
try {
|
|
75
|
+
const content = fs.readFileSync(licenseFilePath, 'utf-8');
|
|
76
|
+
return detectSpdxFromText(content) || content.split('\n')[0]?.trim() || null;
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Get project license from manifest or LICENSE file.
|
|
84
|
+
* Returns manifestLicense if provided, otherwise tries LICENSE file.
|
|
85
|
+
* @param {string|null} manifestLicense - license from manifest (or null)
|
|
86
|
+
* @param {string} manifestPath - path to manifest
|
|
87
|
+
* @returns {string|null} - SPDX identifier or null
|
|
88
|
+
*/
|
|
89
|
+
export function getLicense(manifestLicense, manifestPath) {
|
|
90
|
+
return manifestLicense || readLicenseFile(manifestPath) || null;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Normalize SPDX identifier for comparison (lowercase, strip common suffixes).
|
|
94
|
+
* @param {string} spdxOrName
|
|
95
|
+
* @returns {string}
|
|
96
|
+
*/
|
|
97
|
+
export function normalizeSpdx(spdxOrName) {
|
|
98
|
+
const s = String(spdxOrName).trim().toLowerCase();
|
|
99
|
+
if (s.endsWith(' license')) {
|
|
100
|
+
return s.slice(0, -8);
|
|
101
|
+
}
|
|
102
|
+
return s;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Check if a dependency's license is compatible with the project license based on backend categories.
|
|
106
|
+
*
|
|
107
|
+
* @param {string} [projectCategory] - backend category for project license: PERMISSIVE | WEAK_COPYLEFT | STRONG_COPYLEFT | UNKNOWN
|
|
108
|
+
* @param {string} [dependencyCategory] - backend category for dependency license: PERMISSIVE | WEAK_COPYLEFT | STRONG_COPYLEFT | UNKNOWN
|
|
109
|
+
* @returns {'compatible'|'incompatible'|'unknown'}
|
|
110
|
+
*/
|
|
111
|
+
export function getCompatibility(projectCategory, dependencyCategory) {
|
|
112
|
+
if (!projectCategory || !dependencyCategory) {
|
|
113
|
+
return 'unknown';
|
|
114
|
+
}
|
|
115
|
+
const proj = projectCategory.toUpperCase();
|
|
116
|
+
const dep = dependencyCategory.toUpperCase();
|
|
117
|
+
if (proj === 'UNKNOWN' || dep === 'UNKNOWN') {
|
|
118
|
+
return 'unknown';
|
|
119
|
+
}
|
|
120
|
+
const restrictiveness = {
|
|
121
|
+
'PERMISSIVE': 1,
|
|
122
|
+
'WEAK_COPYLEFT': 2,
|
|
123
|
+
'STRONG_COPYLEFT': 3
|
|
124
|
+
};
|
|
125
|
+
const projLevel = restrictiveness[proj];
|
|
126
|
+
const depLevel = restrictiveness[dep];
|
|
127
|
+
if (projLevel === undefined || depLevel === undefined) {
|
|
128
|
+
return 'unknown';
|
|
129
|
+
}
|
|
130
|
+
if (depLevel > projLevel) {
|
|
131
|
+
return 'incompatible';
|
|
132
|
+
}
|
|
133
|
+
return 'compatible';
|
|
134
|
+
}
|
|
@@ -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)
|