@trustify-da/trustify-da-javascript-client 0.3.0-ea.e12bc82 → 0.3.0-ea.f2d5d72

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.
Files changed (64) hide show
  1. package/README.md +191 -11
  2. package/dist/package.json +22 -9
  3. package/dist/src/analysis.d.ts +21 -5
  4. package/dist/src/analysis.js +71 -78
  5. package/dist/src/batch_opts.d.ts +24 -0
  6. package/dist/src/batch_opts.js +35 -0
  7. package/dist/src/cli.js +192 -8
  8. package/dist/src/cyclone_dx_sbom.d.ts +10 -2
  9. package/dist/src/cyclone_dx_sbom.js +32 -5
  10. package/dist/src/index.d.ts +128 -11
  11. package/dist/src/index.js +272 -7
  12. package/dist/src/license/index.d.ts +28 -0
  13. package/dist/src/license/index.js +100 -0
  14. package/dist/src/license/license_utils.d.ts +40 -0
  15. package/dist/src/license/license_utils.js +134 -0
  16. package/dist/src/license/licenses_api.d.ts +34 -0
  17. package/dist/src/license/licenses_api.js +98 -0
  18. package/dist/src/license/project_license.d.ts +20 -0
  19. package/dist/src/license/project_license.js +62 -0
  20. package/dist/src/oci_image/images.d.ts +4 -5
  21. package/dist/src/oci_image/utils.d.ts +4 -4
  22. package/dist/src/provider.d.ts +17 -5
  23. package/dist/src/provider.js +27 -5
  24. package/dist/src/providers/base_java.d.ts +3 -5
  25. package/dist/src/providers/base_javascript.d.ts +29 -7
  26. package/dist/src/providers/base_javascript.js +129 -22
  27. package/dist/src/providers/base_pyproject.d.ts +147 -0
  28. package/dist/src/providers/base_pyproject.js +279 -0
  29. package/dist/src/providers/golang_gomodules.d.ts +20 -13
  30. package/dist/src/providers/golang_gomodules.js +112 -114
  31. package/dist/src/providers/gomod_parser.d.ts +4 -0
  32. package/dist/src/providers/gomod_parser.js +16 -0
  33. package/dist/src/providers/java_gradle.d.ts +9 -3
  34. package/dist/src/providers/java_gradle.js +12 -2
  35. package/dist/src/providers/java_gradle_groovy.d.ts +1 -1
  36. package/dist/src/providers/java_gradle_kotlin.d.ts +1 -1
  37. package/dist/src/providers/java_maven.d.ts +12 -5
  38. package/dist/src/providers/java_maven.js +33 -5
  39. package/dist/src/providers/javascript_pnpm.d.ts +1 -1
  40. package/dist/src/providers/javascript_pnpm.js +2 -2
  41. package/dist/src/providers/manifest.d.ts +2 -0
  42. package/dist/src/providers/manifest.js +22 -4
  43. package/dist/src/providers/processors/yarn_berry_processor.js +82 -3
  44. package/dist/src/providers/python_controller.d.ts +5 -2
  45. package/dist/src/providers/python_controller.js +56 -58
  46. package/dist/src/providers/python_pip.d.ts +11 -4
  47. package/dist/src/providers/python_pip.js +47 -54
  48. package/dist/src/providers/python_poetry.d.ts +42 -0
  49. package/dist/src/providers/python_poetry.js +146 -0
  50. package/dist/src/providers/python_uv.d.ts +26 -0
  51. package/dist/src/providers/python_uv.js +118 -0
  52. package/dist/src/providers/requirements_parser.d.ts +6 -0
  53. package/dist/src/providers/requirements_parser.js +24 -0
  54. package/dist/src/providers/rust_cargo.d.ts +52 -0
  55. package/dist/src/providers/rust_cargo.js +614 -0
  56. package/dist/src/providers/tree-sitter-gomod.wasm +0 -0
  57. package/dist/src/providers/tree-sitter-requirements.wasm +0 -0
  58. package/dist/src/sbom.d.ts +10 -1
  59. package/dist/src/sbom.js +12 -2
  60. package/dist/src/tools.d.ts +22 -6
  61. package/dist/src/tools.js +56 -1
  62. package/dist/src/workspace.d.ts +61 -0
  63. package/dist/src/workspace.js +256 -0
  64. package/package.json +23 -10
@@ -1,28 +1,12 @@
1
1
  import fs from "node:fs";
2
2
  import path from "node:path";
3
3
  import { EOL } from "os";
4
- import { HttpsProxyAgent } from "https-proxy-agent";
4
+ import { runLicenseCheck } from "./license/index.js";
5
5
  import { generateImageSBOM, parseImageRef } from "./oci_image/utils.js";
6
- import { RegexNotToBeLogged, getCustom } from "./tools.js";
7
- export default { requestComponent, requestStack, requestImages, validateToken };
8
- const rhdaTokenHeader = "rhda-token";
9
- const rhdaTelemetryId = "rhda-telemetry-id";
10
- const rhdaSourceHeader = "rhda-source";
11
- const rhdaOperationTypeHeader = "rhda-operation-type";
12
- const rhdaPackageManagerHeader = "rhda-pkg-manager";
13
- /**
14
- * Adds proxy agent configuration to fetch options if a proxy URL is specified
15
- * @param {RequestInit} options - The base fetch options
16
- * @param {import("index.js").Options} opts - The trustify DA options that may contain proxy configuration
17
- * @returns {RequestInit} The fetch options with proxy agent if applicable
18
- */
19
- function addProxyAgent(options, opts) {
20
- const proxyUrl = getCustom('TRUSTIFY_DA_PROXY_URL', null, opts);
21
- if (proxyUrl) {
22
- options.agent = new HttpsProxyAgent(proxyUrl);
23
- }
24
- return options;
25
- }
6
+ import { addProxyAgent, getCustom, getTokenHeaders, TRUSTIFY_DA_OPERATION_TYPE_HEADER, TRUSTIFY_DA_PACKAGE_MANAGER_HEADER } from "./tools.js";
7
+ /** Media type for CycloneDX JSON batch payloads (batch-analysis API). */
8
+ export const CYCLONEDX_JSON_MEDIA_TYPE = 'application/vnd.cyclonedx+json';
9
+ export default { requestComponent, requestStack, requestStackBatch, requestImages, validateToken };
26
10
  /**
27
11
  * Send a stack analysis request and get the report as 'text/html' or 'application/json'.
28
12
  * @param {import('./provider').Provider} provider - the provided data for constructing the request
@@ -35,15 +19,15 @@ function addProxyAgent(options, opts) {
35
19
  async function requestStack(provider, manifest, url, html = false, opts = {}) {
36
20
  opts["source-manifest"] = Buffer.from(fs.readFileSync(manifest).toString()).toString('base64');
37
21
  opts["manifest-type"] = path.parse(manifest).base;
38
- let provided = provider.provideStack(manifest, opts); // throws error if content providing failed
22
+ let provided = await provider.provideStack(manifest, opts); // throws error if content providing failed
39
23
  opts["source-manifest"] = "";
40
- opts[rhdaOperationTypeHeader.toUpperCase().replaceAll("-", "_")] = "stack-analysis";
24
+ opts[TRUSTIFY_DA_OPERATION_TYPE_HEADER.toUpperCase().replaceAll("-", "_")] = "stack-analysis";
41
25
  let startTime = new Date();
42
26
  let endTime;
43
27
  if (process.env["TRUSTIFY_DA_DEBUG"] === "true") {
44
28
  console.log("Starting time of sending stack analysis request to the dependency analytics server= " + startTime);
45
29
  }
46
- opts[rhdaPackageManagerHeader.toUpperCase().replaceAll("-", "_")] = provided.ecosystem;
30
+ opts[TRUSTIFY_DA_PACKAGE_MANAGER_HEADER.toUpperCase().replaceAll("-", "_")] = provided.ecosystem;
47
31
  const fetchOptions = addProxyAgent({
48
32
  method: 'POST',
49
33
  headers: {
@@ -53,7 +37,7 @@ async function requestStack(provider, manifest, url, html = false, opts = {}) {
53
37
  },
54
38
  body: provided.content
55
39
  }, opts);
56
- const finalUrl = new URL(`${url}/api/v4/analysis`);
40
+ const finalUrl = new URL(`${url}/api/v5/analysis`);
57
41
  if (opts['TRUSTIFY_DA_RECOMMENDATIONS_ENABLED'] === 'false') {
58
42
  finalUrl.searchParams.append('recommend', 'false');
59
43
  }
@@ -94,13 +78,13 @@ async function requestStack(provider, manifest, url, html = false, opts = {}) {
94
78
  */
95
79
  async function requestComponent(provider, manifest, url, opts = {}) {
96
80
  opts["source-manifest"] = Buffer.from(fs.readFileSync(manifest).toString()).toString('base64');
97
- let provided = provider.provideComponent(manifest, opts); // throws error if content providing failed
81
+ let provided = await provider.provideComponent(manifest, opts); // throws error if content providing failed
98
82
  opts["source-manifest"] = "";
99
- opts[rhdaOperationTypeHeader.toUpperCase().replaceAll("-", "_")] = "component-analysis";
83
+ opts[TRUSTIFY_DA_OPERATION_TYPE_HEADER.toUpperCase().replaceAll("-", "_")] = "component-analysis";
100
84
  if (process.env["TRUSTIFY_DA_DEBUG"] === "true") {
101
85
  console.log("Starting time of sending component analysis request to Trustify DA backend server= " + new Date());
102
86
  }
103
- opts[rhdaPackageManagerHeader.toUpperCase().replaceAll("-", "_")] = provided.ecosystem;
87
+ opts[TRUSTIFY_DA_PACKAGE_MANAGER_HEADER.toUpperCase().replaceAll("-", "_")] = provided.ecosystem;
104
88
  const fetchOptions = addProxyAgent({
105
89
  method: 'POST',
106
90
  headers: {
@@ -110,7 +94,7 @@ async function requestComponent(provider, manifest, url, opts = {}) {
110
94
  },
111
95
  body: provided.content
112
96
  }, opts);
113
- const finalUrl = new URL(`${url}/api/v4/analysis`);
97
+ const finalUrl = new URL(`${url}/api/v5/analysis`);
114
98
  if (opts['TRUSTIFY_DA_RECOMMENDATIONS_ENABLED'] === 'false') {
115
99
  finalUrl.searchParams.append('recommend', 'false');
116
100
  }
@@ -127,12 +111,67 @@ async function requestComponent(provider, manifest, url, opts = {}) {
127
111
  console.log(JSON.stringify(result, null, 4));
128
112
  console.log("Ending time of sending component analysis request to Trustify DA backend server= " + new Date());
129
113
  }
114
+ const licenseCheckEnabled = getCustom('TRUSTIFY_DA_LICENSE_CHECK', 'true', opts) !== 'false' && opts.licenseCheck !== false;
115
+ if (licenseCheckEnabled) {
116
+ try {
117
+ result.licenseSummary = await runLicenseCheck(provided.content, manifest, url, opts, result);
118
+ }
119
+ catch (licenseErr) {
120
+ result.licenseSummary = { error: licenseErr.message };
121
+ }
122
+ }
130
123
  }
131
124
  else {
132
125
  throw new Error(`Got error response from Trustify DA backend - http return code : ${resp.status}, ex-request-id: ${resp.headers.get("ex-request-id")} error message => ${await resp.text()}`);
133
126
  }
134
127
  return Promise.resolve(result);
135
128
  }
129
+ /**
130
+ * Send a batch stack analysis request for multiple manifests (SBOMs keyed by purl).
131
+ * @param {Object.<string, object>} sbomByPurl - Map of root purl to CycloneDX SBOM object
132
+ * @param {string} url - the backend url
133
+ * @param {boolean} [html=false] - true returns HTML, false returns JSON
134
+ * @param {import("index.js").Options} [opts={}]
135
+ * @returns {Promise<string|Object.<string, import('@trustify-da/trustify-da-api-model/model/v5/AnalysisReport').AnalysisReport>>}
136
+ */
137
+ async function requestStackBatch(sbomByPurl, url, html = false, opts = {}) {
138
+ const finalUrl = new URL(`${url}/api/v5/batch-analysis`);
139
+ if (opts['TRUSTIFY_DA_RECOMMENDATIONS_ENABLED'] === 'false') {
140
+ finalUrl.searchParams.append('recommend', 'false');
141
+ }
142
+ const fetchOptions = addProxyAgent({
143
+ method: 'POST',
144
+ headers: {
145
+ 'Accept': html ? 'text/html' : 'application/json',
146
+ 'Content-Type': CYCLONEDX_JSON_MEDIA_TYPE,
147
+ ...getTokenHeaders(opts)
148
+ },
149
+ body: JSON.stringify(sbomByPurl)
150
+ }, opts);
151
+ const resp = await fetch(finalUrl, fetchOptions);
152
+ if (resp.status === 200) {
153
+ let result;
154
+ if (!html) {
155
+ result = await resp.json();
156
+ }
157
+ else {
158
+ result = await resp.text();
159
+ }
160
+ if (process.env["TRUSTIFY_DA_DEBUG"] === "true") {
161
+ const exRequestId = resp.headers.get("ex-request-id");
162
+ if (exRequestId) {
163
+ console.log("Unique Identifier associated with this request - ex-request-id=" + exRequestId);
164
+ }
165
+ console.log("Response body received from Trustify DA backend server : " + EOL + EOL);
166
+ console.log(JSON.stringify(result, null, 4));
167
+ console.log("Ending time of sending batch stack analysis request to Trustify DA backend server= " + new Date());
168
+ }
169
+ return result;
170
+ }
171
+ else {
172
+ throw new Error(`Got error response from Trustify DA backend - http return code : ${resp.status}, ex-request-id: ${resp.headers.get("ex-request-id")} error message => ${await resp.text()}`);
173
+ }
174
+ }
136
175
  /**
137
176
  *
138
177
  * @param {Array<string>} imageRefs
@@ -146,7 +185,7 @@ async function requestImages(imageRefs, url, html = false, opts = {}) {
146
185
  const parsedImageRef = parseImageRef(image, opts);
147
186
  imageSboms[parsedImageRef.getPackageURL().toString()] = generateImageSBOM(parsedImageRef, opts);
148
187
  }
149
- const finalUrl = new URL(`${url}/api/v4/batch-analysis`);
188
+ const finalUrl = new URL(`${url}/api/v5/batch-analysis`);
150
189
  if (opts['TRUSTIFY_DA_RECOMMENDATIONS_ENABLED'] === 'false') {
151
190
  finalUrl.searchParams.append('recommend', 'false');
152
191
  }
@@ -154,7 +193,7 @@ async function requestImages(imageRefs, url, html = false, opts = {}) {
154
193
  method: 'POST',
155
194
  headers: {
156
195
  'Accept': html ? 'text/html' : 'application/json',
157
- 'Content-Type': 'application/vnd.cyclonedx+json',
196
+ 'Content-Type': CYCLONEDX_JSON_MEDIA_TYPE,
158
197
  ...getTokenHeaders(opts)
159
198
  },
160
199
  body: JSON.stringify(imageSboms),
@@ -195,7 +234,7 @@ async function validateToken(url, opts = {}) {
195
234
  ...getTokenHeaders(opts),
196
235
  }
197
236
  }, opts);
198
- let resp = await fetch(`${url}/api/v4/token`, fetchOptions);
237
+ let resp = await fetch(`${url}/api/v5/token`, fetchOptions);
199
238
  if (process.env["TRUSTIFY_DA_DEBUG"] === "true") {
200
239
  let exRequestId = resp.headers.get("ex-request-id");
201
240
  if (exRequestId) {
@@ -204,49 +243,3 @@ async function validateToken(url, opts = {}) {
204
243
  }
205
244
  return resp.status;
206
245
  }
207
- /**
208
- *
209
- * @param {string} headerName - the header name to populate in request
210
- * @param headers
211
- * @param {import("index.js").Options} [opts={}] - optional various options to pass along the application
212
- * @private
213
- */
214
- function setRhdaHeader(headerName, headers, opts) {
215
- let rhdaHeaderValue = getCustom(headerName.toUpperCase().replaceAll("-", "_"), null, opts);
216
- if (rhdaHeaderValue) {
217
- headers[headerName] = rhdaHeaderValue;
218
- }
219
- }
220
- /**
221
- * Utility function for fetching vendor tokens
222
- * @param {import("index.js").Options} [opts={}] - optional various options to pass along the application
223
- * @returns {{}}
224
- */
225
- function getTokenHeaders(opts = {}) {
226
- let supportedTokens = ['snyk', 'oss-index'];
227
- let headers = {};
228
- supportedTokens.forEach(vendor => {
229
- let token = getCustom(`TRUSTIFY_DA_${vendor.replace("-", "_").toUpperCase()}_TOKEN`, null, opts);
230
- if (token) {
231
- headers[`ex-${vendor}-token`] = token;
232
- }
233
- let user = getCustom(`TRUSTIFY_DA_${vendor.replace("-", "_").toUpperCase()}_USER`, null, opts);
234
- if (user) {
235
- headers[`ex-${vendor}-user`] = user;
236
- }
237
- });
238
- setRhdaHeader(rhdaTokenHeader, headers, opts);
239
- setRhdaHeader(rhdaSourceHeader, headers, opts);
240
- setRhdaHeader(rhdaOperationTypeHeader, headers, opts);
241
- setRhdaHeader(rhdaPackageManagerHeader, headers, opts);
242
- setRhdaHeader(rhdaTelemetryId, headers, opts);
243
- if (process.env["TRUSTIFY_DA_DEBUG"] === "true") {
244
- console.log("Headers Values to be sent to Trustify DA backend:" + EOL);
245
- for (const headerKey in headers) {
246
- if (!headerKey.match(RegexNotToBeLogged)) {
247
- console.log(`${headerKey}: ${headers[headerKey]}`);
248
- }
249
- }
250
- }
251
- return headers;
252
- }
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Whether to skip failed manifests and continue (default), or fail on first SBOM/validation error.
3
+ * `opts.continueOnError` overrides; env `TRUSTIFY_DA_CONTINUE_ON_ERROR=false` disables continuation.
4
+ *
5
+ * @param {{ continueOnError?: boolean, TRUSTIFY_DA_CONTINUE_ON_ERROR?: string, [key: string]: unknown }} [opts={}]
6
+ * @returns {boolean} true = collect errors (default), false = fail-fast
7
+ */
8
+ export function resolveContinueOnError(opts?: {
9
+ continueOnError?: boolean;
10
+ TRUSTIFY_DA_CONTINUE_ON_ERROR?: string;
11
+ [key: string]: unknown;
12
+ }): boolean;
13
+ /**
14
+ * When true, `stackAnalysisBatch` returns `{ analysis, metadata }` instead of the backend response only.
15
+ * `opts.batchMetadata` overrides; env `TRUSTIFY_DA_BATCH_METADATA=true` enables.
16
+ *
17
+ * @param {{ batchMetadata?: boolean, TRUSTIFY_DA_BATCH_METADATA?: string, [key: string]: unknown }} [opts={}]
18
+ * @returns {boolean}
19
+ */
20
+ export function resolveBatchMetadata(opts?: {
21
+ batchMetadata?: boolean;
22
+ TRUSTIFY_DA_BATCH_METADATA?: string;
23
+ [key: string]: unknown;
24
+ }): boolean;
@@ -0,0 +1,35 @@
1
+ import { getCustom } from './tools.js';
2
+ /**
3
+ * Whether to skip failed manifests and continue (default), or fail on first SBOM/validation error.
4
+ * `opts.continueOnError` overrides; env `TRUSTIFY_DA_CONTINUE_ON_ERROR=false` disables continuation.
5
+ *
6
+ * @param {{ continueOnError?: boolean, TRUSTIFY_DA_CONTINUE_ON_ERROR?: string, [key: string]: unknown }} [opts={}]
7
+ * @returns {boolean} true = collect errors (default), false = fail-fast
8
+ */
9
+ export function resolveContinueOnError(opts = {}) {
10
+ if (typeof opts.continueOnError === 'boolean') {
11
+ return opts.continueOnError;
12
+ }
13
+ const v = getCustom('TRUSTIFY_DA_CONTINUE_ON_ERROR', null, opts);
14
+ if (v != null && String(v).trim() !== '') {
15
+ return String(v).toLowerCase() !== 'false';
16
+ }
17
+ return true;
18
+ }
19
+ /**
20
+ * When true, `stackAnalysisBatch` returns `{ analysis, metadata }` instead of the backend response only.
21
+ * `opts.batchMetadata` overrides; env `TRUSTIFY_DA_BATCH_METADATA=true` enables.
22
+ *
23
+ * @param {{ batchMetadata?: boolean, TRUSTIFY_DA_BATCH_METADATA?: string, [key: string]: unknown }} [opts={}]
24
+ * @returns {boolean}
25
+ */
26
+ export function resolveBatchMetadata(opts = {}) {
27
+ if (typeof opts.batchMetadata === 'boolean') {
28
+ return opts.batchMetadata;
29
+ }
30
+ const v = getCustom('TRUSTIFY_DA_BATCH_METADATA', null, opts);
31
+ if (v != null && String(v).trim() !== '') {
32
+ return String(v).toLowerCase() === 'true';
33
+ }
34
+ return false;
35
+ }
package/dist/src/cli.js CHANGED
@@ -2,7 +2,8 @@
2
2
  import * as path from "path";
3
3
  import yargs from 'yargs';
4
4
  import { hideBin } from 'yargs/helpers';
5
- import client from './index.js';
5
+ import { getProjectLicense, getLicenseDetails } from './license/index.js';
6
+ import client, { selectTrustifyDABackend } from './index.js';
6
7
  // command for component analysis take manifest type and content
7
8
  const component = {
8
9
  command: 'component </path/to/manifest>',
@@ -11,10 +12,18 @@ const component = {
11
12
  desc: 'manifest path for analyzing',
12
13
  type: 'string',
13
14
  normalize: true,
15
+ }).options({
16
+ workspaceDir: {
17
+ alias: 'w',
18
+ desc: 'Workspace root directory (for monorepos; lock file is expected here)',
19
+ type: 'string',
20
+ normalize: true,
21
+ }
14
22
  }),
15
23
  handler: async (args) => {
16
24
  let manifestName = args['/path/to/manifest'];
17
- let res = await client.componentAnalysis(manifestName);
25
+ const opts = args.workspaceDir ? { TRUSTIFY_DA_WORKSPACE_DIR: args.workspaceDir } : {};
26
+ let res = await client.componentAnalysis(manifestName, opts);
18
27
  console.log(JSON.stringify(res, null, 2));
19
28
  }
20
29
  };
@@ -22,9 +31,8 @@ const validateToken = {
22
31
  command: 'validate-token <token-provider> [--token-value thevalue]',
23
32
  desc: 'Validates input token if authentic and authorized',
24
33
  builder: yargs => yargs.positional('token-provider', {
25
- desc: 'the token provider',
26
- type: 'string',
27
- choices: ['snyk', 'oss-index'],
34
+ desc: 'the token provider name',
35
+ type: 'string'
28
36
  }).options({
29
37
  tokenValue: {
30
38
  alias: 'value',
@@ -37,7 +45,7 @@ const validateToken = {
37
45
  let opts = {};
38
46
  if (args['tokenValue'] !== undefined && args['tokenValue'].trim() !== "") {
39
47
  let tokenValue = args['tokenValue'].trim();
40
- opts[`TRUSTIFY_DA_${tokenProvider}_TOKEN`] = tokenValue;
48
+ opts[`TRUSTIFY_DA_PROVIDER_${tokenProvider}_TOKEN`] = tokenValue;
41
49
  }
42
50
  let res = await client.validateToken(opts);
43
51
  console.log(res);
@@ -117,15 +125,22 @@ const stack = {
117
125
  desc: 'For JSON report, get only the \'summary\'',
118
126
  type: 'boolean',
119
127
  conflicts: 'html'
128
+ },
129
+ workspaceDir: {
130
+ alias: 'w',
131
+ desc: 'Workspace root directory (for monorepos; lock file is expected here)',
132
+ type: 'string',
133
+ normalize: true,
120
134
  }
121
135
  }),
122
136
  handler: async (args) => {
123
137
  let manifest = args['/path/to/manifest'];
124
138
  let html = args['html'];
125
139
  let summary = args['summary'];
140
+ const opts = args.workspaceDir ? { TRUSTIFY_DA_WORKSPACE_DIR: args.workspaceDir } : {};
126
141
  let theProvidersSummary = new Map();
127
142
  let theProvidersObject = {};
128
- let res = await client.stackAnalysis(manifest, html);
143
+ let res = await client.stackAnalysis(manifest, html, opts);
129
144
  if (summary) {
130
145
  for (let provider in res.providers) {
131
146
  if (res.providers[provider].sources !== undefined) {
@@ -143,13 +158,182 @@ const stack = {
143
158
  console.log(html ? res : JSON.stringify(!html && summary ? theProvidersObject : res, null, 2));
144
159
  }
145
160
  };
161
+ // command for batch stack analysis (workspace)
162
+ const stackBatch = {
163
+ command: 'stack-batch </path/to/workspace-root> [--html|--summary] [--concurrency <n>] [--ignore <pattern>...] [--metadata] [--fail-fast]',
164
+ desc: 'produce stack report for all packages/crates in a workspace (Cargo or JS/TS)',
165
+ builder: yargs => yargs.positional('/path/to/workspace-root', {
166
+ desc: 'workspace root directory (containing Cargo.toml+Cargo.lock or package.json+lock file)',
167
+ type: 'string',
168
+ normalize: true,
169
+ }).options({
170
+ html: {
171
+ alias: 'r',
172
+ desc: 'Get the report as HTML instead of JSON',
173
+ type: 'boolean',
174
+ conflicts: 'summary'
175
+ },
176
+ summary: {
177
+ alias: 's',
178
+ desc: 'For JSON report, get only the \'summary\' per package',
179
+ type: 'boolean',
180
+ conflicts: 'html'
181
+ },
182
+ concurrency: {
183
+ alias: 'c',
184
+ desc: 'Max parallel SBOM generations (default: 10, env: TRUSTIFY_DA_BATCH_CONCURRENCY)',
185
+ type: 'number',
186
+ },
187
+ ignore: {
188
+ alias: 'i',
189
+ desc: 'Extra glob patterns excluded from workspace discovery (merged with defaults). Repeat flag per pattern. Env: TRUSTIFY_DA_WORKSPACE_DISCOVERY_IGNORE (comma-separated)',
190
+ type: 'string',
191
+ array: true,
192
+ },
193
+ metadata: {
194
+ alias: 'm',
195
+ desc: 'Return { analysis, metadata } with per-manifest errors (env: TRUSTIFY_DA_BATCH_METADATA=true)',
196
+ type: 'boolean',
197
+ default: false,
198
+ },
199
+ failFast: {
200
+ desc: 'Stop on first invalid package.json or SBOM error (env: TRUSTIFY_DA_CONTINUE_ON_ERROR=false)',
201
+ type: 'boolean',
202
+ default: false,
203
+ }
204
+ }),
205
+ handler: async (args) => {
206
+ const workspaceRoot = args['/path/to/workspace-root'];
207
+ const html = args['html'];
208
+ const summary = args['summary'];
209
+ const opts = {};
210
+ if (args.concurrency != null) {
211
+ opts.batchConcurrency = args.concurrency;
212
+ }
213
+ const extraIgnores = Array.isArray(args.ignore) ? args.ignore.filter(p => p != null && String(p).trim()) : [];
214
+ if (extraIgnores.length > 0) {
215
+ opts.workspaceDiscoveryIgnore = extraIgnores;
216
+ }
217
+ if (args.metadata) {
218
+ opts.batchMetadata = true;
219
+ }
220
+ if (args.failFast) {
221
+ opts.continueOnError = false;
222
+ }
223
+ let res = await client.stackAnalysisBatch(workspaceRoot, html, opts);
224
+ const batchAnalysis = res && typeof res === 'object' && res != null && 'analysis' in res ? res.analysis : res;
225
+ if (summary && !html && typeof batchAnalysis === 'object') {
226
+ const summaries = {};
227
+ for (const [purl, report] of Object.entries(batchAnalysis)) {
228
+ if (report?.providers) {
229
+ for (const provider of Object.keys(report.providers)) {
230
+ const sources = report.providers[provider]?.sources;
231
+ if (sources) {
232
+ for (const [source, data] of Object.entries(sources)) {
233
+ if (data?.summary) {
234
+ if (!summaries[purl]) {
235
+ summaries[purl] = {};
236
+ }
237
+ if (!summaries[purl][provider]) {
238
+ summaries[purl][provider] = {};
239
+ }
240
+ summaries[purl][provider][source] = data.summary;
241
+ }
242
+ }
243
+ }
244
+ }
245
+ }
246
+ }
247
+ if (res && typeof res === 'object' && res != null && 'metadata' in res) {
248
+ res = { analysis: summaries, metadata: res.metadata };
249
+ }
250
+ else {
251
+ res = summaries;
252
+ }
253
+ }
254
+ if (html) {
255
+ const htmlContent = res && typeof res === 'object' && 'analysis' in res ? res.analysis : res;
256
+ console.log(htmlContent);
257
+ }
258
+ else {
259
+ console.log(JSON.stringify(res, null, 2));
260
+ }
261
+ }
262
+ };
263
+ // command for license checking
264
+ const license = {
265
+ command: 'license </path/to/manifest>',
266
+ desc: 'Display project license information from manifest and LICENSE file in JSON format',
267
+ builder: yargs => yargs.positional('/path/to/manifest', {
268
+ desc: 'manifest path for license analysis',
269
+ type: 'string',
270
+ normalize: true,
271
+ }),
272
+ handler: async (args) => {
273
+ let manifestPath = args['/path/to/manifest'];
274
+ const opts = {}; // CLI options can be extended in the future
275
+ try {
276
+ selectTrustifyDABackend(opts);
277
+ }
278
+ catch (err) {
279
+ console.error(JSON.stringify({ error: err.message }, null, 2));
280
+ process.exit(1);
281
+ }
282
+ let localResult;
283
+ try {
284
+ localResult = getProjectLicense(manifestPath);
285
+ }
286
+ catch (err) {
287
+ console.error(JSON.stringify({ error: `Failed to read manifest: ${err.message}` }, null, 2));
288
+ process.exit(1);
289
+ }
290
+ const errors = [];
291
+ // Build LicenseInfo objects
292
+ const buildLicenseInfo = async (spdxId) => {
293
+ if (!spdxId) {
294
+ return null;
295
+ }
296
+ const licenseInfo = { spdxId };
297
+ try {
298
+ const details = await getLicenseDetails(spdxId, opts);
299
+ if (details) {
300
+ // Check if backend recognized the license as valid
301
+ if (details.category === 'UNKNOWN') {
302
+ errors.push(`"${spdxId}" is not a valid SPDX license identifier. Please use a valid SPDX expression (e.g., "Apache-2.0", "MIT"). See https://spdx.org/licenses/`);
303
+ }
304
+ else {
305
+ Object.assign(licenseInfo, details);
306
+ }
307
+ }
308
+ else {
309
+ errors.push(`No license details found for ${spdxId}`);
310
+ }
311
+ }
312
+ catch (err) {
313
+ errors.push(`Failed to fetch details for ${spdxId}: ${err.message}`);
314
+ }
315
+ return licenseInfo;
316
+ };
317
+ const output = {
318
+ manifestLicense: await buildLicenseInfo(localResult.fromManifest),
319
+ fileLicense: await buildLicenseInfo(localResult.fromFile),
320
+ mismatch: localResult.mismatch
321
+ };
322
+ if (errors.length > 0) {
323
+ output.errors = errors;
324
+ }
325
+ console.log(JSON.stringify(output, null, 2));
326
+ }
327
+ };
146
328
  // parse and invoke the command
147
329
  yargs(hideBin(process.argv))
148
- .usage(`Usage: ${process.argv[0].includes("node") ? path.parse(process.argv[1]).base : path.parse(process.argv[0]).base} {component|stack|image|validate-token}`)
330
+ .usage(`Usage: ${process.argv[0].includes("node") ? path.parse(process.argv[1]).base : path.parse(process.argv[0]).base} {component|stack|stack-batch|image|validate-token|license}`)
149
331
  .command(stack)
332
+ .command(stackBatch)
150
333
  .command(component)
151
334
  .command(image)
152
335
  .command(validateToken)
336
+ .command(license)
153
337
  .scriptName('')
154
338
  .version(false)
155
339
  .demandCommand(1)
@@ -1,4 +1,3 @@
1
- /// <reference types="packageurl-js/src/package-url" />
2
1
  export default class CycloneDxSbom {
3
2
  sbomObject: any;
4
3
  rootComponent: any;
@@ -7,9 +6,10 @@ export default class CycloneDxSbom {
7
6
  sourceManifestForAuditTrail: any;
8
7
  /**
9
8
  * @param {PackageURL} root - add main/root component for sbom
9
+ * @param {string|Array} [licenses] - optional license(s) for the root component
10
10
  * @return {CycloneDxSbom} the CycloneDxSbom Sbom Object
11
11
  */
12
- addRoot(root: PackageURL): CycloneDxSbom;
12
+ addRoot(root: PackageURL, licenses?: string | any[]): CycloneDxSbom;
13
13
  /**
14
14
  * @return {{{"bom-ref": string, name, purl: string, type, version}}} root component of sbom.
15
15
  */
@@ -49,6 +49,7 @@ export default class CycloneDxSbom {
49
49
  type: any;
50
50
  version: any;
51
51
  scope: any;
52
+ licenses?: any;
52
53
  };
53
54
  /**
54
55
  * This method gets an array of dependencies to be ignored, and remove all of them from CycloneDx Sbom
@@ -69,6 +70,13 @@ export default class CycloneDxSbom {
69
70
  * @return {boolean}
70
71
  */
71
72
  checkIfPackageInsideDependsOnList(component: any, name: string): boolean;
73
+ /**
74
+ * Checks if any entry in the dependsOn list of sourceRef starts with the given purl prefix.
75
+ * @param {PackageURL} sourceRef - The source component
76
+ * @param {string} purlPrefix - The purl prefix to match (e.g. "pkg:npm/minimist@")
77
+ * @return {boolean}
78
+ */
79
+ checkDependsOnByPurlPrefix(sourceRef: PackageURL, purlPrefix: string): boolean;
72
80
  /** Removes the root component from the sbom
73
81
  */
74
82
  removeRootComponent(): void;
@@ -5,10 +5,11 @@ import { PackageURL } from "packageurl-js";
5
5
  * @param component {PackageURL}
6
6
  * @param type type of package - application or library
7
7
  * @param scope scope of the component - runtime or compile
8
- * @return {{"bom-ref": string, name, purl: string, type, version, scope}}
8
+ * @param licenses optional license string or array of licenses for the component
9
+ * @return {{"bom-ref": string, name, purl: string, type, version, scope, licenses?}}
9
10
  * @private
10
11
  */
11
- function getComponent(component, type, scope) {
12
+ function getComponent(component, type, scope, licenses) {
12
13
  let componentObject;
13
14
  if (component instanceof PackageURL) {
14
15
  if (component.namespace) {
@@ -36,6 +37,16 @@ function getComponent(component, type, scope) {
36
37
  else {
37
38
  componentObject = component;
38
39
  }
40
+ // Add licenses if provided (CycloneDX format). Callers must provide valid SPDX identifiers.
41
+ if (licenses) {
42
+ const licenseArray = Array.isArray(licenses) ? licenses : [licenses];
43
+ componentObject.licenses = licenseArray.map(lic => {
44
+ if (typeof lic === 'string') {
45
+ return { license: { id: lic } };
46
+ }
47
+ return lic;
48
+ });
49
+ }
39
50
  return componentObject;
40
51
  }
41
52
  function createDependency(dependency) {
@@ -56,11 +67,12 @@ export default class CycloneDxSbom {
56
67
  }
57
68
  /**
58
69
  * @param {PackageURL} root - add main/root component for sbom
70
+ * @param {string|Array} [licenses] - optional license(s) for the root component
59
71
  * @return {CycloneDxSbom} the CycloneDxSbom Sbom Object
60
72
  */
61
- addRoot(root) {
73
+ addRoot(root, licenses) {
62
74
  this.rootComponent =
63
- getComponent(root, "application");
75
+ getComponent(root, "application", undefined, licenses);
64
76
  this.components.push(this.rootComponent);
65
77
  return this;
66
78
  }
@@ -108,6 +120,7 @@ export default class CycloneDxSbom {
108
120
  getAsJsonString(opts) {
109
121
  let manifestType = opts["manifest-type"];
110
122
  this.setSourceManifest(opts["source-manifest"]);
123
+ const rootPurl = this.rootComponent?.purl;
111
124
  this.sbomObject = {
112
125
  "bomFormat": "CycloneDX",
113
126
  "specVersion": "1.4",
@@ -117,7 +130,7 @@ export default class CycloneDxSbom {
117
130
  "component": this.rootComponent,
118
131
  "properties": new Array()
119
132
  },
120
- "components": this.components,
133
+ "components": this.components.filter(c => c.purl !== rootPurl),
121
134
  "dependencies": this.dependencies
122
135
  };
123
136
  if (this.rootComponent === undefined) {
@@ -229,6 +242,20 @@ export default class CycloneDxSbom {
229
242
  return false;
230
243
  }
231
244
  }
245
+ /**
246
+ * Checks if any entry in the dependsOn list of sourceRef starts with the given purl prefix.
247
+ * @param {PackageURL} sourceRef - The source component
248
+ * @param {string} purlPrefix - The purl prefix to match (e.g. "pkg:npm/minimist@")
249
+ * @return {boolean}
250
+ */
251
+ checkDependsOnByPurlPrefix(sourceRef, purlPrefix) {
252
+ const sourcePurl = sourceRef.toString();
253
+ const depIndex = this.getDependencyIndex(sourcePurl);
254
+ if (depIndex < 0) {
255
+ return false;
256
+ }
257
+ return this.dependencies[depIndex].dependsOn.some(dep => dep.startsWith(purlPrefix));
258
+ }
232
259
  /** Removes the root component from the sbom
233
260
  */
234
261
  removeRootComponent() {