@trustify-da/trustify-da-javascript-client 0.3.0-ea.62f6bc7 → 0.3.0-ea.6549d2a
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 +13 -1
- package/dist/package.json +7 -3
- package/dist/src/analysis.js +21 -76
- package/dist/src/cli.js +72 -6
- package/dist/src/cyclone_dx_sbom.d.ts +3 -1
- package/dist/src/cyclone_dx_sbom.js +16 -4
- package/dist/src/index.d.ts +5 -3
- package/dist/src/index.js +5 -3
- package/dist/src/license/compatibility.d.ts +18 -0
- package/dist/src/license/compatibility.js +45 -0
- package/dist/src/license/index.d.ts +28 -0
- package/dist/src/license/index.js +100 -0
- package/dist/src/license/licenses_api.d.ts +34 -0
- package/dist/src/license/licenses_api.js +91 -0
- package/dist/src/license/project_license.d.ts +25 -0
- package/dist/src/license/project_license.js +139 -0
- package/dist/src/provider.d.ts +12 -3
- package/dist/src/provider.js +16 -1
- package/dist/src/providers/base_javascript.d.ts +10 -4
- package/dist/src/providers/base_javascript.js +28 -4
- package/dist/src/providers/golang_gomodules.d.ts +8 -1
- package/dist/src/providers/golang_gomodules.js +12 -4
- package/dist/src/providers/java_gradle.d.ts +6 -0
- package/dist/src/providers/java_gradle.js +11 -2
- package/dist/src/providers/java_maven.d.ts +8 -1
- package/dist/src/providers/java_maven.js +31 -4
- package/dist/src/providers/python_controller.d.ts +5 -2
- package/dist/src/providers/python_controller.js +56 -58
- package/dist/src/providers/python_pip.d.ts +11 -4
- package/dist/src/providers/python_pip.js +45 -53
- package/dist/src/providers/requirements_parser.d.ts +6 -0
- package/dist/src/providers/requirements_parser.js +23 -0
- package/dist/src/sbom.d.ts +3 -1
- package/dist/src/sbom.js +3 -2
- package/dist/src/tools.d.ts +18 -0
- package/dist/src/tools.js +56 -1
- package/package.json +8 -4
package/README.md
CHANGED
|
@@ -83,12 +83,13 @@ Use as CLI Script
|
|
|
83
83
|
```shell
|
|
84
84
|
$ npx @trustify-da/trustify-da-javascript-client help
|
|
85
85
|
|
|
86
|
-
Usage: trustify-da-javascript-client {component|stack|image|validate-token}
|
|
86
|
+
Usage: trustify-da-javascript-client {component|stack|image|validate-token|license}
|
|
87
87
|
|
|
88
88
|
Commands:
|
|
89
89
|
trustify-da-javascript-client stack </path/to/manifest> [--html|--summary] produce stack report for manifest path
|
|
90
90
|
trustify-da-javascript-client component <path/to/manifest> [--summary] produce component report for a manifest type and content
|
|
91
91
|
trustify-da-javascript-client image <image-refs..> [--html|--summary] produce image analysis report for OCI image references
|
|
92
|
+
trustify-da-javascript-client license </path/to/manifest> display project license information from manifest and LICENSE file in JSON format
|
|
92
93
|
|
|
93
94
|
Options:
|
|
94
95
|
--help Show help [boolean]
|
|
@@ -123,6 +124,9 @@ $ npx @trustify-da/trustify-da-javascript-client image docker.io/library/node:18
|
|
|
123
124
|
|
|
124
125
|
# specify architecture using ^^ notation (e.g., httpd:2.4.49^^amd64)
|
|
125
126
|
$ npx @trustify-da/trustify-da-javascript-client image httpd:2.4.49^^amd64
|
|
127
|
+
|
|
128
|
+
# get project license information
|
|
129
|
+
$ npx @trustify-da/trustify-da-javascript-client license /path/to/package.json
|
|
126
130
|
```
|
|
127
131
|
</li>
|
|
128
132
|
|
|
@@ -161,6 +165,9 @@ $ trustify-da-javascript-client image docker.io/library/node:18 docker.io/librar
|
|
|
161
165
|
|
|
162
166
|
# specify architecture using ^^ notation (e.g., httpd:2.4.49^^amd64)
|
|
163
167
|
$ trustify-da-javascript-client image httpd:2.4.49^^amd64
|
|
168
|
+
|
|
169
|
+
# get project license information
|
|
170
|
+
$ trustify-da-javascript-client license /path/to/package.json
|
|
164
171
|
```
|
|
165
172
|
</li>
|
|
166
173
|
</ul>
|
|
@@ -372,6 +379,11 @@ const options = {
|
|
|
372
379
|
The proxy URL should be in the format: `http://host:port` or `https://host:port`. The API will automatically use the appropriate protocol (HTTP or HTTPS) based on the proxy URL provided.
|
|
373
380
|
</p>
|
|
374
381
|
|
|
382
|
+
<h4>License resolution and dependency license compliance</h4>
|
|
383
|
+
<p>
|
|
384
|
+
The client can resolve the <strong>project license</strong> from the manifest (e.g. <code>package.json</code> <code>license</code>, <code>pom.xml</code> <code><licenses></code>) and from a <code>LICENSE</code> or <code>LICENSE.md</code> file in the project, and report when they differ. For <strong>component analysis</strong>, you can optionally run a license check: the client fetches dependency licenses from the backend (by purl) and reports dependencies whose licenses are incompatible with the project license. See <a href="docs/license-resolution-and-compliance.md">License resolution and compliance</a> for design and behavior. To disable the check on component analysis, set <code>TRUSTIFY_DA_LICENSE_CHECK=false</code> or pass <code>licenseCheck: false</code> in the options.
|
|
385
|
+
</p>
|
|
386
|
+
|
|
375
387
|
<h4>Customizing Executables</h4>
|
|
376
388
|
<p>
|
|
377
389
|
This project uses each ecosystem's executable for creating dependency trees. These executables are expected to be
|
package/dist/package.json
CHANGED
|
@@ -46,23 +46,27 @@
|
|
|
46
46
|
"dependencies": {
|
|
47
47
|
"@babel/core": "^7.23.2",
|
|
48
48
|
"@cyclonedx/cyclonedx-library": "^6.13.0",
|
|
49
|
+
"eslint-import-resolver-typescript": "^4.4.4",
|
|
49
50
|
"fast-toml": "^0.5.4",
|
|
50
51
|
"fast-xml-parser": "^5.3.4",
|
|
51
52
|
"help": "^3.0.2",
|
|
52
53
|
"https-proxy-agent": "^7.0.6",
|
|
53
54
|
"node-fetch": "^3.3.2",
|
|
54
55
|
"packageurl-js": "~1.0.2",
|
|
55
|
-
"
|
|
56
|
+
"tree-sitter-requirements": "github:Strum355/tree-sitter-requirements#d0261ee76b84253997fe70d7d397e78c006c3801",
|
|
57
|
+
"web-tree-sitter": "^0.26.6",
|
|
58
|
+
"yargs": "^18.0.0"
|
|
56
59
|
},
|
|
57
60
|
"devDependencies": {
|
|
58
61
|
"@babel/core": "^7.23.2",
|
|
59
|
-
"@trustify-da/trustify-da-api-model": "^2.0.
|
|
62
|
+
"@trustify-da/trustify-da-api-model": "^2.0.7",
|
|
60
63
|
"@types/node": "^20.17.30",
|
|
61
64
|
"@types/which": "^3.0.4",
|
|
62
65
|
"babel-plugin-rewire": "^1.2.0",
|
|
63
|
-
"c8": "^
|
|
66
|
+
"c8": "^11.0.0",
|
|
64
67
|
"chai": "^4.3.7",
|
|
65
68
|
"eslint": "^8.42.0",
|
|
69
|
+
"eslint-import-resolver-typescript": "^4.4.4",
|
|
66
70
|
"eslint-plugin-editorconfig": "^4.0.3",
|
|
67
71
|
"eslint-plugin-import": "^2.29.1",
|
|
68
72
|
"esmock": "^2.6.2",
|
package/dist/src/analysis.js
CHANGED
|
@@ -1,28 +1,10 @@
|
|
|
1
1
|
import fs from "node:fs";
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
import { EOL } from "os";
|
|
4
|
-
import {
|
|
4
|
+
import { runLicenseCheck } from "./license/index.js";
|
|
5
5
|
import { generateImageSBOM, parseImageRef } from "./oci_image/utils.js";
|
|
6
|
-
import {
|
|
6
|
+
import { addProxyAgent, getCustom, getTokenHeaders, TRUSTIFY_DA_OPERATION_TYPE_HEADER, TRUSTIFY_DA_PACKAGE_MANAGER_HEADER } from "./tools.js";
|
|
7
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
|
-
}
|
|
26
8
|
/**
|
|
27
9
|
* Send a stack analysis request and get the report as 'text/html' or 'application/json'.
|
|
28
10
|
* @param {import('./provider').Provider} provider - the provided data for constructing the request
|
|
@@ -35,15 +17,15 @@ function addProxyAgent(options, opts) {
|
|
|
35
17
|
async function requestStack(provider, manifest, url, html = false, opts = {}) {
|
|
36
18
|
opts["source-manifest"] = Buffer.from(fs.readFileSync(manifest).toString()).toString('base64');
|
|
37
19
|
opts["manifest-type"] = path.parse(manifest).base;
|
|
38
|
-
let provided = provider.provideStack(manifest, opts); // throws error if content providing failed
|
|
20
|
+
let provided = await provider.provideStack(manifest, opts); // throws error if content providing failed
|
|
39
21
|
opts["source-manifest"] = "";
|
|
40
|
-
opts[
|
|
22
|
+
opts[TRUSTIFY_DA_OPERATION_TYPE_HEADER.toUpperCase().replaceAll("-", "_")] = "stack-analysis";
|
|
41
23
|
let startTime = new Date();
|
|
42
24
|
let endTime;
|
|
43
25
|
if (process.env["TRUSTIFY_DA_DEBUG"] === "true") {
|
|
44
26
|
console.log("Starting time of sending stack analysis request to the dependency analytics server= " + startTime);
|
|
45
27
|
}
|
|
46
|
-
opts[
|
|
28
|
+
opts[TRUSTIFY_DA_PACKAGE_MANAGER_HEADER.toUpperCase().replaceAll("-", "_")] = provided.ecosystem;
|
|
47
29
|
const fetchOptions = addProxyAgent({
|
|
48
30
|
method: 'POST',
|
|
49
31
|
headers: {
|
|
@@ -53,7 +35,7 @@ async function requestStack(provider, manifest, url, html = false, opts = {}) {
|
|
|
53
35
|
},
|
|
54
36
|
body: provided.content
|
|
55
37
|
}, opts);
|
|
56
|
-
const finalUrl = new URL(`${url}/api/
|
|
38
|
+
const finalUrl = new URL(`${url}/api/v5/analysis`);
|
|
57
39
|
if (opts['TRUSTIFY_DA_RECOMMENDATIONS_ENABLED'] === 'false') {
|
|
58
40
|
finalUrl.searchParams.append('recommend', 'false');
|
|
59
41
|
}
|
|
@@ -94,13 +76,13 @@ async function requestStack(provider, manifest, url, html = false, opts = {}) {
|
|
|
94
76
|
*/
|
|
95
77
|
async function requestComponent(provider, manifest, url, opts = {}) {
|
|
96
78
|
opts["source-manifest"] = Buffer.from(fs.readFileSync(manifest).toString()).toString('base64');
|
|
97
|
-
let provided = provider.provideComponent(manifest, opts); // throws error if content providing failed
|
|
79
|
+
let provided = await provider.provideComponent(manifest, opts); // throws error if content providing failed
|
|
98
80
|
opts["source-manifest"] = "";
|
|
99
|
-
opts[
|
|
81
|
+
opts[TRUSTIFY_DA_OPERATION_TYPE_HEADER.toUpperCase().replaceAll("-", "_")] = "component-analysis";
|
|
100
82
|
if (process.env["TRUSTIFY_DA_DEBUG"] === "true") {
|
|
101
83
|
console.log("Starting time of sending component analysis request to Trustify DA backend server= " + new Date());
|
|
102
84
|
}
|
|
103
|
-
opts[
|
|
85
|
+
opts[TRUSTIFY_DA_PACKAGE_MANAGER_HEADER.toUpperCase().replaceAll("-", "_")] = provided.ecosystem;
|
|
104
86
|
const fetchOptions = addProxyAgent({
|
|
105
87
|
method: 'POST',
|
|
106
88
|
headers: {
|
|
@@ -110,7 +92,7 @@ async function requestComponent(provider, manifest, url, opts = {}) {
|
|
|
110
92
|
},
|
|
111
93
|
body: provided.content
|
|
112
94
|
}, opts);
|
|
113
|
-
const finalUrl = new URL(`${url}/api/
|
|
95
|
+
const finalUrl = new URL(`${url}/api/v5/analysis`);
|
|
114
96
|
if (opts['TRUSTIFY_DA_RECOMMENDATIONS_ENABLED'] === 'false') {
|
|
115
97
|
finalUrl.searchParams.append('recommend', 'false');
|
|
116
98
|
}
|
|
@@ -127,6 +109,15 @@ async function requestComponent(provider, manifest, url, opts = {}) {
|
|
|
127
109
|
console.log(JSON.stringify(result, null, 4));
|
|
128
110
|
console.log("Ending time of sending component analysis request to Trustify DA backend server= " + new Date());
|
|
129
111
|
}
|
|
112
|
+
const licenseCheckEnabled = getCustom('TRUSTIFY_DA_LICENSE_CHECK', 'true', opts) !== 'false' && opts.licenseCheck !== false;
|
|
113
|
+
if (licenseCheckEnabled) {
|
|
114
|
+
try {
|
|
115
|
+
result.licenseSummary = await runLicenseCheck(provided.content, manifest, url, opts, result);
|
|
116
|
+
}
|
|
117
|
+
catch (licenseErr) {
|
|
118
|
+
result.licenseSummary = { error: licenseErr.message };
|
|
119
|
+
}
|
|
120
|
+
}
|
|
130
121
|
}
|
|
131
122
|
else {
|
|
132
123
|
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()}`);
|
|
@@ -146,7 +137,7 @@ async function requestImages(imageRefs, url, html = false, opts = {}) {
|
|
|
146
137
|
const parsedImageRef = parseImageRef(image, opts);
|
|
147
138
|
imageSboms[parsedImageRef.getPackageURL().toString()] = generateImageSBOM(parsedImageRef, opts);
|
|
148
139
|
}
|
|
149
|
-
const finalUrl = new URL(`${url}/api/
|
|
140
|
+
const finalUrl = new URL(`${url}/api/v5/batch-analysis`);
|
|
150
141
|
if (opts['TRUSTIFY_DA_RECOMMENDATIONS_ENABLED'] === 'false') {
|
|
151
142
|
finalUrl.searchParams.append('recommend', 'false');
|
|
152
143
|
}
|
|
@@ -195,7 +186,7 @@ async function validateToken(url, opts = {}) {
|
|
|
195
186
|
...getTokenHeaders(opts),
|
|
196
187
|
}
|
|
197
188
|
}, opts);
|
|
198
|
-
let resp = await fetch(`${url}/api/
|
|
189
|
+
let resp = await fetch(`${url}/api/v5/token`, fetchOptions);
|
|
199
190
|
if (process.env["TRUSTIFY_DA_DEBUG"] === "true") {
|
|
200
191
|
let exRequestId = resp.headers.get("ex-request-id");
|
|
201
192
|
if (exRequestId) {
|
|
@@ -204,49 +195,3 @@ async function validateToken(url, opts = {}) {
|
|
|
204
195
|
}
|
|
205
196
|
return resp.status;
|
|
206
197
|
}
|
|
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
|
-
}
|
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
|
|
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>',
|
|
@@ -22,9 +23,8 @@ const validateToken = {
|
|
|
22
23
|
command: 'validate-token <token-provider> [--token-value thevalue]',
|
|
23
24
|
desc: 'Validates input token if authentic and authorized',
|
|
24
25
|
builder: yargs => yargs.positional('token-provider', {
|
|
25
|
-
desc: 'the token provider',
|
|
26
|
-
type: 'string'
|
|
27
|
-
choices: ['snyk', 'oss-index'],
|
|
26
|
+
desc: 'the token provider name',
|
|
27
|
+
type: 'string'
|
|
28
28
|
}).options({
|
|
29
29
|
tokenValue: {
|
|
30
30
|
alias: 'value',
|
|
@@ -37,7 +37,7 @@ const validateToken = {
|
|
|
37
37
|
let opts = {};
|
|
38
38
|
if (args['tokenValue'] !== undefined && args['tokenValue'].trim() !== "") {
|
|
39
39
|
let tokenValue = args['tokenValue'].trim();
|
|
40
|
-
opts[`
|
|
40
|
+
opts[`TRUSTIFY_DA_PROVIDER_${tokenProvider}_TOKEN`] = tokenValue;
|
|
41
41
|
}
|
|
42
42
|
let res = await client.validateToken(opts);
|
|
43
43
|
console.log(res);
|
|
@@ -143,13 +143,79 @@ const stack = {
|
|
|
143
143
|
console.log(html ? res : JSON.stringify(!html && summary ? theProvidersObject : res, null, 2));
|
|
144
144
|
}
|
|
145
145
|
};
|
|
146
|
+
// command for license checking
|
|
147
|
+
const license = {
|
|
148
|
+
command: 'license </path/to/manifest>',
|
|
149
|
+
desc: 'Display project license information from manifest and LICENSE file in JSON format',
|
|
150
|
+
builder: yargs => yargs.positional('/path/to/manifest', {
|
|
151
|
+
desc: 'manifest path for license analysis',
|
|
152
|
+
type: 'string',
|
|
153
|
+
normalize: true,
|
|
154
|
+
}),
|
|
155
|
+
handler: async (args) => {
|
|
156
|
+
let manifestPath = args['/path/to/manifest'];
|
|
157
|
+
const opts = {}; // CLI options can be extended in the future
|
|
158
|
+
try {
|
|
159
|
+
selectTrustifyDABackend(opts);
|
|
160
|
+
}
|
|
161
|
+
catch (err) {
|
|
162
|
+
console.error(JSON.stringify({ error: err.message }, null, 2));
|
|
163
|
+
process.exit(1);
|
|
164
|
+
}
|
|
165
|
+
let localResult;
|
|
166
|
+
try {
|
|
167
|
+
localResult = getProjectLicense(manifestPath);
|
|
168
|
+
}
|
|
169
|
+
catch (err) {
|
|
170
|
+
console.error(JSON.stringify({ error: `Failed to read manifest: ${err.message}` }, null, 2));
|
|
171
|
+
process.exit(1);
|
|
172
|
+
}
|
|
173
|
+
const errors = [];
|
|
174
|
+
// Build LicenseInfo objects
|
|
175
|
+
const buildLicenseInfo = async (spdxId) => {
|
|
176
|
+
if (!spdxId) {
|
|
177
|
+
return null;
|
|
178
|
+
}
|
|
179
|
+
const licenseInfo = { spdxId };
|
|
180
|
+
try {
|
|
181
|
+
const details = await getLicenseDetails(spdxId, opts);
|
|
182
|
+
if (details) {
|
|
183
|
+
// Check if backend recognized the license as valid
|
|
184
|
+
if (details.category === 'UNKNOWN') {
|
|
185
|
+
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/`);
|
|
186
|
+
}
|
|
187
|
+
else {
|
|
188
|
+
Object.assign(licenseInfo, details);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
else {
|
|
192
|
+
errors.push(`No license details found for ${spdxId}`);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
catch (err) {
|
|
196
|
+
errors.push(`Failed to fetch details for ${spdxId}: ${err.message}`);
|
|
197
|
+
}
|
|
198
|
+
return licenseInfo;
|
|
199
|
+
};
|
|
200
|
+
const output = {
|
|
201
|
+
manifestLicense: await buildLicenseInfo(localResult.fromManifest),
|
|
202
|
+
fileLicense: await buildLicenseInfo(localResult.fromFile),
|
|
203
|
+
mismatch: localResult.mismatch
|
|
204
|
+
};
|
|
205
|
+
if (errors.length > 0) {
|
|
206
|
+
output.errors = errors;
|
|
207
|
+
}
|
|
208
|
+
console.log(JSON.stringify(output, null, 2));
|
|
209
|
+
}
|
|
210
|
+
};
|
|
146
211
|
// parse and invoke the command
|
|
147
212
|
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}`)
|
|
213
|
+
.usage(`Usage: ${process.argv[0].includes("node") ? path.parse(process.argv[1]).base : path.parse(process.argv[0]).base} {component|stack|image|validate-token|license}`)
|
|
149
214
|
.command(stack)
|
|
150
215
|
.command(component)
|
|
151
216
|
.command(image)
|
|
152
217
|
.command(validateToken)
|
|
218
|
+
.command(license)
|
|
153
219
|
.scriptName('')
|
|
154
220
|
.version(false)
|
|
155
221
|
.demandCommand(1)
|
|
@@ -6,9 +6,10 @@ export default class CycloneDxSbom {
|
|
|
6
6
|
sourceManifestForAuditTrail: any;
|
|
7
7
|
/**
|
|
8
8
|
* @param {PackageURL} root - add main/root component for sbom
|
|
9
|
+
* @param {string|Array} [licenses] - optional license(s) for the root component
|
|
9
10
|
* @return {CycloneDxSbom} the CycloneDxSbom Sbom Object
|
|
10
11
|
*/
|
|
11
|
-
addRoot(root: PackageURL): CycloneDxSbom;
|
|
12
|
+
addRoot(root: PackageURL, licenses?: string | any[]): CycloneDxSbom;
|
|
12
13
|
/**
|
|
13
14
|
* @return {{{"bom-ref": string, name, purl: string, type, version}}} root component of sbom.
|
|
14
15
|
*/
|
|
@@ -48,6 +49,7 @@ export default class CycloneDxSbom {
|
|
|
48
49
|
type: any;
|
|
49
50
|
version: any;
|
|
50
51
|
scope: any;
|
|
52
|
+
licenses?: any;
|
|
51
53
|
};
|
|
52
54
|
/**
|
|
53
55
|
* This method gets an array of dependencies to be ignored, and remove all of them from CycloneDx Sbom
|
|
@@ -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
|
-
* @
|
|
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
|
}
|
package/dist/src/index.d.ts
CHANGED
|
@@ -47,10 +47,11 @@ export type Options = {
|
|
|
47
47
|
TRUSTIFY_DA_SYFT_CONFIG_PATH?: string | undefined;
|
|
48
48
|
TRUSTIFY_DA_SYFT_PATH?: string | undefined;
|
|
49
49
|
TRUSTIFY_DA_YARN_PATH?: string | undefined;
|
|
50
|
+
TRUSTIFY_DA_LICENSE_CHECK?: string | undefined;
|
|
50
51
|
MATCH_MANIFEST_VERSIONS?: string | undefined;
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
52
|
+
TRUSTIFY_DA_SOURCE?: string | undefined;
|
|
53
|
+
TRUSTIFY_DA_TOKEN?: string | undefined;
|
|
54
|
+
TRUSTIFY_DA_TELEMETRY_ID?: string | undefined;
|
|
54
55
|
[key: string]: string | undefined;
|
|
55
56
|
};
|
|
56
57
|
/**
|
|
@@ -130,3 +131,4 @@ declare function imageAnalysis(imageRefs: Array<string>, html?: boolean | undefi
|
|
|
130
131
|
* @throws {Error} if the backend request failed.
|
|
131
132
|
*/
|
|
132
133
|
declare function validateToken(opts?: Options): Promise<object>;
|
|
134
|
+
export { getProjectLicense, findLicenseFilePath, identifyLicenseViaBackend, getLicenseDetails, licensesFromReport, normalizeLicensesResponse, runLicenseCheck, getCompatibility } from "./license/index.js";
|
package/dist/src/index.js
CHANGED
|
@@ -8,6 +8,7 @@ import.meta.dirname;
|
|
|
8
8
|
import * as url from 'url';
|
|
9
9
|
export { parseImageRef } from "./oci_image/utils.js";
|
|
10
10
|
export { ImageRef } from "./oci_image/images.js";
|
|
11
|
+
export { getProjectLicense, findLicenseFilePath, identifyLicenseViaBackend, getLicenseDetails, licensesFromReport, normalizeLicensesResponse, runLicenseCheck, getCompatibility } from "./license/index.js";
|
|
11
12
|
export default { componentAnalysis, stackAnalysis, imageAnalysis, validateToken };
|
|
12
13
|
/**
|
|
13
14
|
* @typedef {{
|
|
@@ -35,10 +36,11 @@ export default { componentAnalysis, stackAnalysis, imageAnalysis, validateToken
|
|
|
35
36
|
* TRUSTIFY_DA_SYFT_CONFIG_PATH?: string | undefined,
|
|
36
37
|
* TRUSTIFY_DA_SYFT_PATH?: string | undefined,
|
|
37
38
|
* TRUSTIFY_DA_YARN_PATH?: string | undefined,
|
|
39
|
+
* TRUSTIFY_DA_LICENSE_CHECK?: string | undefined,
|
|
38
40
|
* MATCH_MANIFEST_VERSIONS?: string | undefined,
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
41
|
+
* TRUSTIFY_DA_SOURCE?: string | undefined,
|
|
42
|
+
* TRUSTIFY_DA_TOKEN?: string | undefined,
|
|
43
|
+
* TRUSTIFY_DA_TELEMETRY_ID?: string | undefined,
|
|
42
44
|
* [key: string]: string | undefined,
|
|
43
45
|
* }} Options
|
|
44
46
|
*/
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* License compatibility: whether a dependency license is compatible with the project license.
|
|
3
|
+
* Relies on backend-provided license categories.
|
|
4
|
+
*
|
|
5
|
+
* Compatibility is based on restrictiveness hierarchy:
|
|
6
|
+
* PERMISSIVE < WEAK_COPYLEFT < STRONG_COPYLEFT
|
|
7
|
+
*
|
|
8
|
+
* A dependency is compatible if it's equal or less restrictive than the project license.
|
|
9
|
+
* A dependency is incompatible if it's more restrictive than the project license.
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* Check if a dependency's license is compatible with the project license based on backend categories.
|
|
13
|
+
*
|
|
14
|
+
* @param {string} [projectCategory] - backend category for project license: PERMISSIVE | WEAK_COPYLEFT | STRONG_COPYLEFT | UNKNOWN
|
|
15
|
+
* @param {string} [dependencyCategory] - backend category for dependency license: PERMISSIVE | WEAK_COPYLEFT | STRONG_COPYLEFT | UNKNOWN
|
|
16
|
+
* @returns {'compatible'|'incompatible'|'unknown'}
|
|
17
|
+
*/
|
|
18
|
+
export function getCompatibility(projectCategory?: string, dependencyCategory?: string): "compatible" | "incompatible" | "unknown";
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* License compatibility: whether a dependency license is compatible with the project license.
|
|
3
|
+
* Relies on backend-provided license categories.
|
|
4
|
+
*
|
|
5
|
+
* Compatibility is based on restrictiveness hierarchy:
|
|
6
|
+
* PERMISSIVE < WEAK_COPYLEFT < STRONG_COPYLEFT
|
|
7
|
+
*
|
|
8
|
+
* A dependency is compatible if it's equal or less restrictive than the project license.
|
|
9
|
+
* A dependency is incompatible if it's more restrictive than the project license.
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* Check if a dependency's license is compatible with the project license based on backend categories.
|
|
13
|
+
*
|
|
14
|
+
* @param {string} [projectCategory] - backend category for project license: PERMISSIVE | WEAK_COPYLEFT | STRONG_COPYLEFT | UNKNOWN
|
|
15
|
+
* @param {string} [dependencyCategory] - backend category for dependency license: PERMISSIVE | WEAK_COPYLEFT | STRONG_COPYLEFT | UNKNOWN
|
|
16
|
+
* @returns {'compatible'|'incompatible'|'unknown'}
|
|
17
|
+
*/
|
|
18
|
+
export function getCompatibility(projectCategory, dependencyCategory) {
|
|
19
|
+
if (!projectCategory || !dependencyCategory) {
|
|
20
|
+
return 'unknown';
|
|
21
|
+
}
|
|
22
|
+
const proj = projectCategory.toUpperCase();
|
|
23
|
+
const dep = dependencyCategory.toUpperCase();
|
|
24
|
+
// Unknown categories
|
|
25
|
+
if (proj === 'UNKNOWN' || dep === 'UNKNOWN') {
|
|
26
|
+
return 'unknown';
|
|
27
|
+
}
|
|
28
|
+
// Define restrictiveness levels (higher number = more restrictive)
|
|
29
|
+
const restrictiveness = {
|
|
30
|
+
'PERMISSIVE': 1,
|
|
31
|
+
'WEAK_COPYLEFT': 2,
|
|
32
|
+
'STRONG_COPYLEFT': 3
|
|
33
|
+
};
|
|
34
|
+
const projLevel = restrictiveness[proj];
|
|
35
|
+
const depLevel = restrictiveness[dep];
|
|
36
|
+
if (projLevel === undefined || depLevel === undefined) {
|
|
37
|
+
return 'unknown';
|
|
38
|
+
}
|
|
39
|
+
// Dependency is more restrictive than project → incompatible
|
|
40
|
+
if (depLevel > projLevel) {
|
|
41
|
+
return 'incompatible';
|
|
42
|
+
}
|
|
43
|
+
// Dependency is equal or less restrictive → compatible
|
|
44
|
+
return 'compatible';
|
|
45
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Run full license check: resolve project license (with backend identification and details),
|
|
3
|
+
* get dependency licenses from analysis report, and compute incompatibilities.
|
|
4
|
+
*
|
|
5
|
+
* @param {string} sbomContent - CycloneDX SBOM JSON string (the one sent for component analysis)
|
|
6
|
+
* @param {string} manifestPath - path to manifest
|
|
7
|
+
* @param {string} url - the backend url to send the request to
|
|
8
|
+
* @param {import('../index.js').Options} [opts={}]
|
|
9
|
+
* @param {import('@trustify-da/trustify-da-api-model/model/v5/AnalysisReport').AnalysisReport} [analysisResult] - analysis result that includes licenses array from backend
|
|
10
|
+
* @returns {Promise<{ projectLicense: { manifest: Object|null, file: Object|null, mismatch: boolean }, incompatibleDependencies: Array<{ purl: string, licenses: string[], category?: string, reason: string }>, error?: string }>}
|
|
11
|
+
*/
|
|
12
|
+
export function runLicenseCheck(sbomContent: string, manifestPath: string, url: string, opts?: import("../index.js").Options, analysisResult?: import("@trustify-da/trustify-da-api-model/model/v5/AnalysisReport").AnalysisReport): Promise<{
|
|
13
|
+
projectLicense: {
|
|
14
|
+
manifest: any | null;
|
|
15
|
+
file: any | null;
|
|
16
|
+
mismatch: boolean;
|
|
17
|
+
};
|
|
18
|
+
incompatibleDependencies: Array<{
|
|
19
|
+
purl: string;
|
|
20
|
+
licenses: string[];
|
|
21
|
+
category?: string;
|
|
22
|
+
reason: string;
|
|
23
|
+
}>;
|
|
24
|
+
error?: string;
|
|
25
|
+
}>;
|
|
26
|
+
export { getCompatibility } from "./compatibility.js";
|
|
27
|
+
export { getProjectLicense, findLicenseFilePath, identifyLicense as identifyLicenseViaBackend } from "./project_license.js";
|
|
28
|
+
export { licensesFromReport, normalizeLicensesResponse, getLicenseDetails } from "./licenses_api.js";
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* License resolution and dependency license compatibility for component analysis.
|
|
3
|
+
*/
|
|
4
|
+
import { getProjectLicense, findLicenseFilePath, identifyLicense } from './project_license.js';
|
|
5
|
+
import { licensesFromReport, getLicenseDetails } from './licenses_api.js';
|
|
6
|
+
import { getCompatibility } from './compatibility.js';
|
|
7
|
+
export { getProjectLicense, findLicenseFilePath, identifyLicense as identifyLicenseViaBackend } from './project_license.js';
|
|
8
|
+
export { licensesFromReport, normalizeLicensesResponse, getLicenseDetails } from './licenses_api.js';
|
|
9
|
+
export { getCompatibility } from './compatibility.js';
|
|
10
|
+
/**
|
|
11
|
+
* Run full license check: resolve project license (with backend identification and details),
|
|
12
|
+
* get dependency licenses from analysis report, and compute incompatibilities.
|
|
13
|
+
*
|
|
14
|
+
* @param {string} sbomContent - CycloneDX SBOM JSON string (the one sent for component analysis)
|
|
15
|
+
* @param {string} manifestPath - path to manifest
|
|
16
|
+
* @param {string} url - the backend url to send the request to
|
|
17
|
+
* @param {import('../index.js').Options} [opts={}]
|
|
18
|
+
* @param {import('@trustify-da/trustify-da-api-model/model/v5/AnalysisReport').AnalysisReport} [analysisResult] - analysis result that includes licenses array from backend
|
|
19
|
+
* @returns {Promise<{ projectLicense: { manifest: Object|null, file: Object|null, mismatch: boolean }, incompatibleDependencies: Array<{ purl: string, licenses: string[], category?: string, reason: string }>, error?: string }>}
|
|
20
|
+
*/
|
|
21
|
+
export async function runLicenseCheck(sbomContent, manifestPath, url, opts = {}, analysisResult = null) {
|
|
22
|
+
// Resolve project license from manifest and LICENSE file
|
|
23
|
+
const projectLicense = getProjectLicense(manifestPath, opts);
|
|
24
|
+
// Try backend identification for LICENSE file (more accurate than local pattern matching)
|
|
25
|
+
const licenseFilePath = findLicenseFilePath(manifestPath);
|
|
26
|
+
let backendFileId = null;
|
|
27
|
+
if (licenseFilePath) {
|
|
28
|
+
try {
|
|
29
|
+
backendFileId = await identifyLicense(licenseFilePath, { ...opts, TRUSTIFY_DA_BACKEND_URL: url });
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
// Fall back to local detection
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
// Determine final license identifiers
|
|
36
|
+
const manifestSpdx = projectLicense.fromManifest;
|
|
37
|
+
const fileSpdx = backendFileId || projectLicense.fromFile;
|
|
38
|
+
const mismatch = Boolean(manifestSpdx && fileSpdx && manifestSpdx.toLowerCase() !== fileSpdx.toLowerCase());
|
|
39
|
+
// Fetch detailed license info from backend (avoid duplicate calls if same license)
|
|
40
|
+
const licenseDetailsCache = new Map();
|
|
41
|
+
async function getDetails(spdxId) {
|
|
42
|
+
if (!spdxId || !url)
|
|
43
|
+
return null;
|
|
44
|
+
if (licenseDetailsCache.has(spdxId))
|
|
45
|
+
return licenseDetailsCache.get(spdxId);
|
|
46
|
+
try {
|
|
47
|
+
const details = await getLicenseDetails(spdxId, { ...opts, TRUSTIFY_DA_BACKEND_URL: url });
|
|
48
|
+
licenseDetailsCache.set(spdxId, details);
|
|
49
|
+
return details;
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
const manifestLicenseInfo = await getDetails(manifestSpdx);
|
|
56
|
+
const fileLicenseInfo = await getDetails(fileSpdx);
|
|
57
|
+
// Extract dependency purls from SBOM (exclude root component)
|
|
58
|
+
const sbomObj = typeof sbomContent === 'string' ? JSON.parse(sbomContent) : sbomContent;
|
|
59
|
+
const rootRef = sbomObj?.metadata?.component?.["bom-ref"] || sbomObj?.metadata?.component?.purl;
|
|
60
|
+
const purls = (sbomObj?.components || [])
|
|
61
|
+
.map(c => c.purl || c["bom-ref"])
|
|
62
|
+
.filter(Boolean)
|
|
63
|
+
.filter(purl => !rootRef || purl !== rootRef);
|
|
64
|
+
if (purls.length === 0) {
|
|
65
|
+
return {
|
|
66
|
+
projectLicense: { manifest: manifestLicenseInfo, file: fileLicenseInfo, mismatch },
|
|
67
|
+
incompatibleDependencies: []
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
// Get dependency licenses from analysis report
|
|
71
|
+
const licenseByPurl = licensesFromReport(analysisResult, purls);
|
|
72
|
+
if (licenseByPurl.size === 0 && analysisResult) {
|
|
73
|
+
return {
|
|
74
|
+
projectLicense: { manifest: manifestLicenseInfo, file: fileLicenseInfo, mismatch },
|
|
75
|
+
incompatibleDependencies: [],
|
|
76
|
+
error: 'No license data available in analysis report'
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
// Check compatibility for each dependency
|
|
80
|
+
const projectCategory = manifestLicenseInfo?.category || fileLicenseInfo?.category;
|
|
81
|
+
const incompatibleDependencies = [];
|
|
82
|
+
for (const purl of purls) {
|
|
83
|
+
const entry = licenseByPurl.get(purl);
|
|
84
|
+
if (!entry)
|
|
85
|
+
continue;
|
|
86
|
+
const status = getCompatibility(projectCategory, entry.category);
|
|
87
|
+
if (status === 'incompatible') {
|
|
88
|
+
incompatibleDependencies.push({
|
|
89
|
+
purl,
|
|
90
|
+
licenses: entry.licenses,
|
|
91
|
+
category: entry.category,
|
|
92
|
+
reason: 'Dependency license(s) are incompatible with the project license.'
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return {
|
|
97
|
+
projectLicense: { manifest: manifestLicenseInfo, file: fileLicenseInfo, mismatch },
|
|
98
|
+
incompatibleDependencies
|
|
99
|
+
};
|
|
100
|
+
}
|