@trustify-da/trustify-da-javascript-client 0.3.0-ea.e12bc82 → 0.3.0-ea.ff266a3
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 +12 -8
- package/dist/src/analysis.d.ts +5 -5
- package/dist/src/analysis.js +21 -76
- package/dist/src/cli.js +72 -6
- package/dist/src/cyclone_dx_sbom.d.ts +3 -2
- package/dist/src/cyclone_dx_sbom.js +16 -4
- package/dist/src/index.d.ts +65 -11
- 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/oci_image/images.d.ts +4 -5
- package/dist/src/oci_image/utils.d.ts +4 -4
- package/dist/src/provider.d.ts +12 -3
- package/dist/src/provider.js +16 -1
- package/dist/src/providers/base_java.d.ts +3 -5
- 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 +11 -4
- package/dist/src/providers/golang_gomodules.js +12 -4
- package/dist/src/providers/java_gradle.d.ts +9 -3
- package/dist/src/providers/java_gradle.js +11 -2
- package/dist/src/providers/java_gradle_groovy.d.ts +1 -1
- package/dist/src/providers/java_gradle_kotlin.d.ts +1 -1
- package/dist/src/providers/java_maven.d.ts +12 -5
- package/dist/src/providers/java_maven.js +32 -5
- 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 +22 -6
- package/dist/src/tools.js +56 -1
- package/package.json +13 -9
|
@@ -4,8 +4,8 @@ import path from 'node:path';
|
|
|
4
4
|
import Sbom from '../sbom.js';
|
|
5
5
|
import { getCustom, getCustomPath, invokeCommand, toPurl, toPurlFromString } from "../tools.js";
|
|
6
6
|
import Manifest from './manifest.js';
|
|
7
|
-
/** @typedef {import('../provider
|
|
8
|
-
/** @typedef {import('../provider
|
|
7
|
+
/** @typedef {import('../provider').Provider} */
|
|
8
|
+
/** @typedef {import('../provider').Provided} Provided */
|
|
9
9
|
/**
|
|
10
10
|
* The ecosystem identifier for JavaScript/npm packages
|
|
11
11
|
* @type {string}
|
|
@@ -132,6 +132,28 @@ export default class Base_javascript {
|
|
|
132
132
|
contentType: 'application/vnd.cyclonedx+json'
|
|
133
133
|
};
|
|
134
134
|
}
|
|
135
|
+
/**
|
|
136
|
+
* Read license from manifest (package.json). Reused by npm, pnpm, yarn.
|
|
137
|
+
* @param {string} manifestPath - path to package.json
|
|
138
|
+
* @returns {string|null}
|
|
139
|
+
*/
|
|
140
|
+
readLicenseFromManifest(manifestPath) {
|
|
141
|
+
try {
|
|
142
|
+
const content = JSON.parse(fs.readFileSync(manifestPath, 'utf-8'));
|
|
143
|
+
if (typeof content.license === 'string') {
|
|
144
|
+
return content.license.trim() || null;
|
|
145
|
+
}
|
|
146
|
+
if (Array.isArray(content.licenses) && content.licenses.length > 0) {
|
|
147
|
+
const first = content.licenses[0];
|
|
148
|
+
const name = first.type || first.name;
|
|
149
|
+
return typeof name === 'string' ? name.trim() : null;
|
|
150
|
+
}
|
|
151
|
+
return null;
|
|
152
|
+
}
|
|
153
|
+
catch {
|
|
154
|
+
return null;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
135
157
|
/**
|
|
136
158
|
* Builds the dependency tree for the project
|
|
137
159
|
* @param {boolean} includeTransitive - Whether to include transitive dependencies
|
|
@@ -155,8 +177,9 @@ export default class Base_javascript {
|
|
|
155
177
|
#getSBOM(opts = {}) {
|
|
156
178
|
const depsObject = this._buildDependencyTree(true);
|
|
157
179
|
let mainComponent = toPurl(purlType, this.#manifest.name, this.#manifest.version);
|
|
180
|
+
const license = this.readLicenseFromManifest(this.#manifest.manifestPath);
|
|
158
181
|
let sbom = new Sbom();
|
|
159
|
-
sbom.addRoot(mainComponent);
|
|
182
|
+
sbom.addRoot(mainComponent, license);
|
|
160
183
|
this._addDependenciesToSbom(sbom, depsObject);
|
|
161
184
|
sbom.filterIgnoredDeps(this.#manifest.ignored);
|
|
162
185
|
return sbom.getAsJsonString(opts);
|
|
@@ -206,8 +229,9 @@ export default class Base_javascript {
|
|
|
206
229
|
#getDirectDependencySbom(opts = {}) {
|
|
207
230
|
const depTree = this._buildDependencyTree(false);
|
|
208
231
|
let mainComponent = toPurl(purlType, this.#manifest.name, this.#manifest.version);
|
|
232
|
+
const license = this.readLicenseFromManifest(this.#manifest.manifestPath);
|
|
209
233
|
let sbom = new Sbom();
|
|
210
|
-
sbom.addRoot(mainComponent);
|
|
234
|
+
sbom.addRoot(mainComponent, license);
|
|
211
235
|
const rootDeps = this._getRootDependencies(depTree);
|
|
212
236
|
const sortedDepsKeys = Array
|
|
213
237
|
.from(rootDeps.keys())
|
|
@@ -3,9 +3,10 @@ declare namespace _default {
|
|
|
3
3
|
export { validateLockFile };
|
|
4
4
|
export { provideComponent };
|
|
5
5
|
export { provideStack };
|
|
6
|
+
export { readLicenseFromManifest };
|
|
6
7
|
}
|
|
7
8
|
export default _default;
|
|
8
|
-
export type Provided = import(
|
|
9
|
+
export type Provided = import("../provider").Provided;
|
|
9
10
|
export type Package = {
|
|
10
11
|
name: string;
|
|
11
12
|
version: string;
|
|
@@ -20,7 +21,7 @@ export type Dependency = {
|
|
|
20
21
|
/**
|
|
21
22
|
* @param {string} manifestName - the subject manifest name-type
|
|
22
23
|
* @returns {boolean} - return true if `pom.xml` is the manifest name-type
|
|
23
|
-
|
|
24
|
+
*/
|
|
24
25
|
declare function isSupported(manifestName: string): boolean;
|
|
25
26
|
/**
|
|
26
27
|
* @param {string} manifestDir - the directory where the manifest lies
|
|
@@ -32,11 +33,17 @@ declare function validateLockFile(): boolean;
|
|
|
32
33
|
* @param {{}} [opts={}] - optional various options to pass along the application
|
|
33
34
|
* @returns {Provided}
|
|
34
35
|
*/
|
|
35
|
-
declare function provideComponent(manifest: string, opts?: {}
|
|
36
|
+
declare function provideComponent(manifest: string, opts?: {}): Provided;
|
|
36
37
|
/**
|
|
37
38
|
* Provide content and content type for maven-maven stack analysis.
|
|
38
39
|
* @param {string} manifest - the manifest path or name
|
|
39
40
|
* @param {{}} [opts={}] - optional various options to pass along the application
|
|
40
41
|
* @returns {Provided}
|
|
41
42
|
*/
|
|
42
|
-
declare function provideStack(manifest: string, opts?: {}
|
|
43
|
+
declare function provideStack(manifest: string, opts?: {}): Provided;
|
|
44
|
+
/**
|
|
45
|
+
* Go modules have no standard license field in go.mod
|
|
46
|
+
* @param {string} manifestPath - path to go.mod
|
|
47
|
+
* @returns {string|null}
|
|
48
|
+
*/
|
|
49
|
+
declare function readLicenseFromManifest(manifestPath: string): string | null;
|
|
@@ -4,7 +4,7 @@ import { EOL } from "os";
|
|
|
4
4
|
import { PackageURL } from 'packageurl-js';
|
|
5
5
|
import Sbom from '../sbom.js';
|
|
6
6
|
import { getCustom, getCustomPath, invokeCommand } from "../tools.js";
|
|
7
|
-
export default { isSupported, validateLockFile, provideComponent, provideStack };
|
|
7
|
+
export default { isSupported, validateLockFile, provideComponent, provideStack, readLicenseFromManifest };
|
|
8
8
|
/** @typedef {import('../provider').Provider} */
|
|
9
9
|
/** @typedef {import('../provider').Provided} Provided */
|
|
10
10
|
/** @typedef {{name: string, version: string}} Package */
|
|
@@ -12,16 +12,23 @@ export default { isSupported, validateLockFile, provideComponent, provideStack }
|
|
|
12
12
|
/**
|
|
13
13
|
* @type {string} ecosystem for npm-npm is 'maven'
|
|
14
14
|
* @private
|
|
15
|
-
|
|
15
|
+
*/
|
|
16
16
|
const ecosystem = 'golang';
|
|
17
17
|
const defaultMainModuleVersion = "v0.0.0";
|
|
18
18
|
/**
|
|
19
19
|
* @param {string} manifestName - the subject manifest name-type
|
|
20
20
|
* @returns {boolean} - return true if `pom.xml` is the manifest name-type
|
|
21
|
-
|
|
21
|
+
*/
|
|
22
22
|
function isSupported(manifestName) {
|
|
23
23
|
return 'go.mod' === manifestName;
|
|
24
24
|
}
|
|
25
|
+
/**
|
|
26
|
+
* Go modules have no standard license field in go.mod
|
|
27
|
+
* @param {string} manifestPath - path to go.mod
|
|
28
|
+
* @returns {string|null}
|
|
29
|
+
*/
|
|
30
|
+
// eslint-disable-next-line no-unused-vars
|
|
31
|
+
function readLicenseFromManifest(manifestPath) { return null; }
|
|
25
32
|
/**
|
|
26
33
|
* @param {string} manifestDir - the directory where the manifest lies
|
|
27
34
|
*/
|
|
@@ -250,7 +257,8 @@ function getSBOM(manifest, opts = {}, includeTransitive) {
|
|
|
250
257
|
performManifestVersionsCheck(root, rows, manifest);
|
|
251
258
|
}
|
|
252
259
|
const mainModule = toPurl(root, "@");
|
|
253
|
-
|
|
260
|
+
const license = readLicenseFromManifest(manifest);
|
|
261
|
+
sbom.addRoot(mainModule, license);
|
|
254
262
|
const exhortGoMvsLogicEnabled = getCustom("TRUSTIFY_DA_GO_MVS_LOGIC_ENABLED", "true", opts);
|
|
255
263
|
if (includeTransitive && exhortGoMvsLogicEnabled === "true") {
|
|
256
264
|
rows = getFinalPackagesVersionsForModule(rows, manifest, goBin);
|
|
@@ -15,21 +15,27 @@ export default class Java_gradle extends Base_java {
|
|
|
15
15
|
* @param {string} manifestDir - the directory where the manifest lies
|
|
16
16
|
*/
|
|
17
17
|
validateLockFile(): boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Gradle manifests (build.gradle, build.gradle.kts) have no standard license field.
|
|
20
|
+
* @param {string} manifestPath - path to manifest
|
|
21
|
+
* @returns {null}
|
|
22
|
+
*/
|
|
23
|
+
readLicenseFromManifest(manifestPath: string): null;
|
|
18
24
|
/**
|
|
19
25
|
* Provide content and content type for stack analysis.
|
|
20
26
|
* @param {string} manifest - the manifest path or name
|
|
21
27
|
* @param {{}} [opts={}] - optional various options to pass along the application
|
|
22
28
|
* @returns {Provided}
|
|
23
29
|
*/
|
|
24
|
-
provideStack(manifest: string, opts?: {}
|
|
30
|
+
provideStack(manifest: string, opts?: {}): Provided;
|
|
25
31
|
/**
|
|
26
32
|
* Provide content and content type for maven-maven component analysis.
|
|
27
33
|
* @param {string} manifest - path to pom.xml for component report
|
|
28
34
|
* @param {{}} [opts={}] - optional various options to pass along the application
|
|
29
35
|
* @returns {Provided}
|
|
30
36
|
*/
|
|
31
|
-
provideComponent(manifest: string, opts?: {}
|
|
37
|
+
provideComponent(manifest: string, opts?: {}): Provided;
|
|
32
38
|
#private;
|
|
33
39
|
}
|
|
34
|
-
export type Provided = import(
|
|
40
|
+
export type Provided = import("../provider.js").Provided;
|
|
35
41
|
import Base_java from "./base_java.js";
|
|
@@ -49,6 +49,13 @@ export default class Java_gradle extends Base_java {
|
|
|
49
49
|
* @param {string} manifestDir - the directory where the manifest lies
|
|
50
50
|
*/
|
|
51
51
|
validateLockFile() { return true; }
|
|
52
|
+
/**
|
|
53
|
+
* Gradle manifests (build.gradle, build.gradle.kts) have no standard license field.
|
|
54
|
+
* @param {string} manifestPath - path to manifest
|
|
55
|
+
* @returns {null}
|
|
56
|
+
*/
|
|
57
|
+
// eslint-disable-next-line no-unused-vars
|
|
58
|
+
readLicenseFromManifest(manifestPath) { return null; }
|
|
52
59
|
/**
|
|
53
60
|
* Provide content and content type for stack analysis.
|
|
54
61
|
* @param {string} manifest - the manifest path or name
|
|
@@ -158,7 +165,8 @@ export default class Java_gradle extends Base_java {
|
|
|
158
165
|
let sbom = new Sbom();
|
|
159
166
|
let root = `${properties.group}:${properties[ROOT_PROJECT_KEY_NAME].match(/Root project '(.+)'/)[1]}:jar:${properties.version}`;
|
|
160
167
|
let rootPurl = this.parseDep(root);
|
|
161
|
-
|
|
168
|
+
const license = this.readLicenseFromManifest(manifestPath);
|
|
169
|
+
sbom.addRoot(rootPurl, license);
|
|
162
170
|
let ignoredDeps = this.#getIgnoredDeps(manifestPath);
|
|
163
171
|
const [runtimeConfig, compileConfig] = this.#extractConfigurations(content);
|
|
164
172
|
const processedDeps = new Set();
|
|
@@ -298,7 +306,8 @@ export default class Java_gradle extends Base_java {
|
|
|
298
306
|
let sbom = new Sbom();
|
|
299
307
|
let root = `${properties.group}:${properties[ROOT_PROJECT_KEY_NAME].match(/Root project '(.+)'/)[1]}:jar:${properties.version}`;
|
|
300
308
|
let rootPurl = this.parseDep(root);
|
|
301
|
-
|
|
309
|
+
const license = this.readLicenseFromManifest(manifestPath);
|
|
310
|
+
sbom.addRoot(rootPurl, license);
|
|
302
311
|
let ignoredDeps = this.#getIgnoredDeps(manifestPath);
|
|
303
312
|
const [runtimeConfig, compileConfig] = this.#extractConfigurations(content);
|
|
304
313
|
let directDependencies = new Map();
|
|
@@ -3,5 +3,5 @@ export default class Java_gradle_groovy extends Java_gradle {
|
|
|
3
3
|
_parseAliasForLibsNotation(alias: any): any;
|
|
4
4
|
_extractDepToBeIgnored(dep: any): any;
|
|
5
5
|
}
|
|
6
|
-
export type Provided = import(
|
|
6
|
+
export type Provided = import("../provider").Provided;
|
|
7
7
|
import Java_gradle from './java_gradle.js';
|
|
@@ -7,5 +7,5 @@ export default class Java_gradle_kotlin extends Java_gradle {
|
|
|
7
7
|
_parseAliasForLibsNotation(alias: any): any;
|
|
8
8
|
_extractDepToBeIgnored(dep: any): string | null;
|
|
9
9
|
}
|
|
10
|
-
export type Provided = import(
|
|
10
|
+
export type Provided = import("../provider").Provided;
|
|
11
11
|
import Java_gradle from './java_gradle.js';
|
|
@@ -19,25 +19,32 @@ export default class Java_maven extends Base_java {
|
|
|
19
19
|
* @param {{}} [opts={}] - optional various options to pass along the application
|
|
20
20
|
* @returns {Provided}
|
|
21
21
|
*/
|
|
22
|
-
provideStack(manifest: string, opts?: {}
|
|
22
|
+
provideStack(manifest: string, opts?: {}): Provided;
|
|
23
23
|
/**
|
|
24
24
|
* Provide content and content type for maven-maven component analysis.
|
|
25
25
|
* @param {string} manifest - path to the manifest file
|
|
26
26
|
* @param {{}} [opts={}] - optional various options to pass along the application
|
|
27
27
|
* @returns {Provided}
|
|
28
28
|
*/
|
|
29
|
-
provideComponent(manifest: string, opts?: {}
|
|
29
|
+
provideComponent(manifest: string, opts?: {}): Provided;
|
|
30
|
+
/**
|
|
31
|
+
* Read license from pom.xml manifest
|
|
32
|
+
* @param {string} manifestPath - path to pom.xml
|
|
33
|
+
* @returns {string|null}
|
|
34
|
+
*/
|
|
35
|
+
readLicenseFromManifest(manifestPath: string): string | null;
|
|
30
36
|
/**
|
|
31
37
|
*
|
|
32
38
|
* @param {String} textGraphList Text graph String of the manifest
|
|
33
39
|
* @param {[String]} ignoredDeps List of ignored dependencies to be omitted from sbom
|
|
40
|
+
* @param {String} manifestPath Path to the pom.xml manifest
|
|
34
41
|
* @return {String} formatted sbom Json String with all dependencies
|
|
35
42
|
*/
|
|
36
|
-
createSbomFileFromTextFormat(textGraphList: string, ignoredDeps: [string], opts: any): string;
|
|
43
|
+
createSbomFileFromTextFormat(textGraphList: string, ignoredDeps: [string], opts: any, manifestPath: string): string;
|
|
37
44
|
#private;
|
|
38
45
|
}
|
|
39
|
-
export type Java_maven = import(
|
|
40
|
-
export type Provided = import(
|
|
46
|
+
export type Java_maven = import("../provider").Provider;
|
|
47
|
+
export type Provided = import("../provider").Provided;
|
|
41
48
|
export type Package = {
|
|
42
49
|
name: string;
|
|
43
50
|
version: string;
|
|
@@ -51,6 +51,30 @@ export default class Java_maven extends Base_java {
|
|
|
51
51
|
contentType: 'application/vnd.cyclonedx+json'
|
|
52
52
|
};
|
|
53
53
|
}
|
|
54
|
+
/**
|
|
55
|
+
* Read license from pom.xml manifest
|
|
56
|
+
* @param {string} manifestPath - path to pom.xml
|
|
57
|
+
* @returns {string|null}
|
|
58
|
+
*/
|
|
59
|
+
readLicenseFromManifest(manifestPath) {
|
|
60
|
+
try {
|
|
61
|
+
const xml = fs.readFileSync(manifestPath, 'utf-8');
|
|
62
|
+
const parser = new XMLParser({ ignoreAttributes: false });
|
|
63
|
+
const obj = parser.parse(xml);
|
|
64
|
+
const project = obj?.project;
|
|
65
|
+
if (!project?.licenses?.license) {
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
const license = Array.isArray(project.licenses.license)
|
|
69
|
+
? project.licenses.license[0]
|
|
70
|
+
: project.licenses.license;
|
|
71
|
+
const name = (license?.name && license.name.trim()) || null;
|
|
72
|
+
return name || null;
|
|
73
|
+
}
|
|
74
|
+
catch {
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
54
78
|
/**
|
|
55
79
|
* Create a Dot Graph dependency tree for a manifest path.
|
|
56
80
|
* @param {string} manifest - path for pom.xml
|
|
@@ -105,7 +129,7 @@ export default class Java_maven extends Base_java {
|
|
|
105
129
|
if (process.env["TRUSTIFY_DA_DEBUG"] === "true") {
|
|
106
130
|
console.error("Dependency tree that will be used as input for creating the BOM =>" + EOL + EOL + content.toString());
|
|
107
131
|
}
|
|
108
|
-
let sbom = this.createSbomFileFromTextFormat(content.toString(), ignoredDeps, opts);
|
|
132
|
+
let sbom = this.createSbomFileFromTextFormat(content.toString(), ignoredDeps, opts, manifest);
|
|
109
133
|
// delete temp file and directory
|
|
110
134
|
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
111
135
|
// return dependency graph as string
|
|
@@ -115,15 +139,17 @@ export default class Java_maven extends Base_java {
|
|
|
115
139
|
*
|
|
116
140
|
* @param {String} textGraphList Text graph String of the manifest
|
|
117
141
|
* @param {[String]} ignoredDeps List of ignored dependencies to be omitted from sbom
|
|
142
|
+
* @param {String} manifestPath Path to the pom.xml manifest
|
|
118
143
|
* @return {String} formatted sbom Json String with all dependencies
|
|
119
144
|
*/
|
|
120
|
-
createSbomFileFromTextFormat(textGraphList, ignoredDeps, opts) {
|
|
145
|
+
createSbomFileFromTextFormat(textGraphList, ignoredDeps, opts, manifestPath) {
|
|
121
146
|
let lines = textGraphList.split(EOL);
|
|
122
147
|
// get root component
|
|
123
148
|
let root = lines[0];
|
|
124
149
|
let rootPurl = this.parseDep(root);
|
|
150
|
+
const license = this.readLicenseFromManifest(manifestPath);
|
|
125
151
|
let sbom = new Sbom();
|
|
126
|
-
sbom.addRoot(rootPurl);
|
|
152
|
+
sbom.addRoot(rootPurl, license);
|
|
127
153
|
this.parseDependencyTree(root, 0, lines.slice(1), sbom);
|
|
128
154
|
return sbom.filterIgnoredDeps(ignoredDeps).getAsJsonString(opts);
|
|
129
155
|
}
|
|
@@ -156,7 +182,8 @@ export default class Java_maven extends Base_java {
|
|
|
156
182
|
let sbom = new Sbom();
|
|
157
183
|
let rootDependency = this.#getRootFromPom(tmpEffectivePom, manifestPath);
|
|
158
184
|
let purlRoot = this.toPurl(rootDependency.groupId, rootDependency.artifactId, rootDependency.version);
|
|
159
|
-
|
|
185
|
+
const license = this.readLicenseFromManifest(manifestPath);
|
|
186
|
+
sbom.addRoot(purlRoot, license);
|
|
160
187
|
dependencies.forEach(dep => {
|
|
161
188
|
let currentPurl = this.toPurl(dep.groupId, dep.artifactId, dep.version);
|
|
162
189
|
sbom.addDependency(purlRoot, currentPurl);
|
|
@@ -209,7 +236,7 @@ export default class Java_maven extends Base_java {
|
|
|
209
236
|
let ignored = [];
|
|
210
237
|
// build xml parser with options
|
|
211
238
|
let parser = new XMLParser({
|
|
212
|
-
commentPropName: '#comment',
|
|
239
|
+
commentPropName: '#comment', // mark comments with #comment
|
|
213
240
|
isArray: (_, jpath) => 'project.dependencies.dependency' === jpath,
|
|
214
241
|
parseTagValue: false
|
|
215
242
|
});
|
|
@@ -15,13 +15,16 @@ export default class Python_controller {
|
|
|
15
15
|
realEnvironment: boolean;
|
|
16
16
|
pathToRequirements: string;
|
|
17
17
|
options: {};
|
|
18
|
+
parser: Promise<import("web-tree-sitter").Parser>;
|
|
19
|
+
requirementsQuery: Promise<import("web-tree-sitter").Query>;
|
|
20
|
+
pinnedVersionQuery: Promise<import("web-tree-sitter").Query>;
|
|
18
21
|
prepareEnvironment(): void;
|
|
19
22
|
/**
|
|
20
23
|
*
|
|
21
24
|
* @param {boolean} includeTransitive - whether to return include in returned object transitive dependencies or not
|
|
22
|
-
* @return {[DependencyEntry]}
|
|
25
|
+
* @return {Promise<[DependencyEntry]>}
|
|
23
26
|
*/
|
|
24
|
-
getDependencies(includeTransitive: boolean): [DependencyEntry]
|
|
27
|
+
getDependencies(includeTransitive: boolean): Promise<[DependencyEntry]>;
|
|
25
28
|
#private;
|
|
26
29
|
}
|
|
27
30
|
export type DependencyEntry = {
|
|
@@ -2,6 +2,7 @@ import fs from "node:fs";
|
|
|
2
2
|
import path from 'node:path';
|
|
3
3
|
import os, { EOL } from "os";
|
|
4
4
|
import { environmentVariableIsPopulated, getCustom, invokeCommand } from "../tools.js";
|
|
5
|
+
import { getParser, getRequirementQuery, getPinnedVersionQuery } from './requirements_parser.js';
|
|
5
6
|
function getPipFreezeOutput() {
|
|
6
7
|
try {
|
|
7
8
|
return environmentVariableIsPopulated("TRUSTIFY_DA_PIP_FREEZE") ? new Buffer.from(process.env["TRUSTIFY_DA_PIP_FREEZE"], 'base64').toString('ascii') : invokeCommand(this.pathToPipBin, ['freeze', '--all']).toString();
|
|
@@ -26,6 +27,9 @@ export default class Python_controller {
|
|
|
26
27
|
realEnvironment;
|
|
27
28
|
pathToRequirements;
|
|
28
29
|
options;
|
|
30
|
+
parser;
|
|
31
|
+
requirementsQuery;
|
|
32
|
+
pinnedVersionQuery;
|
|
29
33
|
/**
|
|
30
34
|
* Constructor to create new python controller instance to interact with pip package manager
|
|
31
35
|
* @param {boolean} realEnvironment - whether to use real environment supplied by client or to create virtual environment
|
|
@@ -41,6 +45,9 @@ export default class Python_controller {
|
|
|
41
45
|
this.prepareEnvironment();
|
|
42
46
|
this.pathToRequirements = pathToRequirements;
|
|
43
47
|
this.options = options;
|
|
48
|
+
this.parser = getParser();
|
|
49
|
+
this.requirementsQuery = getRequirementQuery();
|
|
50
|
+
this.pinnedVersionQuery = getPinnedVersionQuery();
|
|
44
51
|
}
|
|
45
52
|
prepareEnvironment() {
|
|
46
53
|
if (!this.realEnvironment) {
|
|
@@ -86,6 +93,23 @@ export default class Python_controller {
|
|
|
86
93
|
}
|
|
87
94
|
}
|
|
88
95
|
}
|
|
96
|
+
/**
|
|
97
|
+
* Parse the requirements.txt file using tree-sitter and return structured requirement data.
|
|
98
|
+
* @return {Promise<{name: string, version: string|null}[]>}
|
|
99
|
+
*/
|
|
100
|
+
async #parseRequirements() {
|
|
101
|
+
const content = fs.readFileSync(this.pathToRequirements).toString();
|
|
102
|
+
const tree = (await this.parser).parse(content);
|
|
103
|
+
return Promise.all((await this.requirementsQuery).matches(tree.rootNode).map(async (match) => {
|
|
104
|
+
const reqNode = match.captures.find(c => c.name === 'req').node;
|
|
105
|
+
const name = match.captures.find(c => c.name === 'name').node.text;
|
|
106
|
+
const versionMatches = (await this.pinnedVersionQuery).matches(reqNode);
|
|
107
|
+
const version = versionMatches.length > 0
|
|
108
|
+
? versionMatches[0].captures.find(c => c.name === 'version').node.text
|
|
109
|
+
: null;
|
|
110
|
+
return { name, version };
|
|
111
|
+
}));
|
|
112
|
+
}
|
|
89
113
|
#decideIfWindowsOrLinuxPath(fileName) {
|
|
90
114
|
if (os.platform() === "win32") {
|
|
91
115
|
return fileName + ".exe";
|
|
@@ -97,9 +121,9 @@ export default class Python_controller {
|
|
|
97
121
|
/**
|
|
98
122
|
*
|
|
99
123
|
* @param {boolean} includeTransitive - whether to return include in returned object transitive dependencies or not
|
|
100
|
-
* @return {[DependencyEntry]}
|
|
124
|
+
* @return {Promise<[DependencyEntry]>}
|
|
101
125
|
*/
|
|
102
|
-
getDependencies(includeTransitive) {
|
|
126
|
+
async getDependencies(includeTransitive) {
|
|
103
127
|
let startingTime;
|
|
104
128
|
let endingTime;
|
|
105
129
|
if (process.env["TRUSTIFY_DA_DEBUG"] === "true") {
|
|
@@ -124,10 +148,10 @@ export default class Python_controller {
|
|
|
124
148
|
if (matchManifestVersions === "true") {
|
|
125
149
|
throw new Error("Conflicting settings, TRUSTIFY_DA_PYTHON_INSTALL_BEST_EFFORTS=true can only work with MATCH_MANIFEST_VERSIONS=false");
|
|
126
150
|
}
|
|
127
|
-
this.#installingRequirementsOneByOne();
|
|
151
|
+
await this.#installingRequirementsOneByOne();
|
|
128
152
|
}
|
|
129
153
|
}
|
|
130
|
-
let dependencies = this.#getDependenciesImpl(includeTransitive);
|
|
154
|
+
let dependencies = await this.#getDependenciesImpl(includeTransitive);
|
|
131
155
|
this.#cleanEnvironment();
|
|
132
156
|
if (process.env["TRUSTIFY_DA_DEBUG"] === "true") {
|
|
133
157
|
endingTime = new Date();
|
|
@@ -137,16 +161,14 @@ export default class Python_controller {
|
|
|
137
161
|
}
|
|
138
162
|
return dependencies;
|
|
139
163
|
}
|
|
140
|
-
#installingRequirementsOneByOne() {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
requirementsRows.filter((line) => !line.trim().startsWith("#")).filter((line) => line.trim() !== "").forEach((dependency) => {
|
|
144
|
-
let dependencyName = getDependencyName(dependency);
|
|
164
|
+
async #installingRequirementsOneByOne() {
|
|
165
|
+
const requirements = await this.#parseRequirements();
|
|
166
|
+
requirements.forEach(({ name }) => {
|
|
145
167
|
try {
|
|
146
|
-
invokeCommand(this.pathToPipBin, ['install',
|
|
168
|
+
invokeCommand(this.pathToPipBin, ['install', name]);
|
|
147
169
|
}
|
|
148
170
|
catch (error) {
|
|
149
|
-
throw new Error(`Failed in best-effort installing ${
|
|
171
|
+
throw new Error(`Failed in best-effort installing ${name} in virtual python environment`, { cause: error });
|
|
150
172
|
}
|
|
151
173
|
});
|
|
152
174
|
}
|
|
@@ -163,34 +185,26 @@ export default class Python_controller {
|
|
|
163
185
|
}
|
|
164
186
|
}
|
|
165
187
|
}
|
|
166
|
-
#getDependenciesImpl(includeTransitive) {
|
|
167
|
-
let dependencies =
|
|
188
|
+
async #getDependenciesImpl(includeTransitive) {
|
|
189
|
+
let dependencies = [];
|
|
168
190
|
let usePipDepTree = getCustom("TRUSTIFY_DA_PIP_USE_DEP_TREE", "false", this.options);
|
|
169
|
-
let freezeOutput;
|
|
170
|
-
let lines;
|
|
171
|
-
let depNames;
|
|
172
|
-
let pipShowOutput;
|
|
173
191
|
let allPipShowDeps;
|
|
174
192
|
let pipDepTreeJsonArrayOutput;
|
|
175
193
|
if (usePipDepTree !== "true") {
|
|
176
|
-
freezeOutput = getPipFreezeOutput.call(this);
|
|
177
|
-
lines = freezeOutput.split(EOL);
|
|
178
|
-
depNames = lines.map(line => getDependencyName(line));
|
|
194
|
+
const freezeOutput = getPipFreezeOutput.call(this);
|
|
195
|
+
const lines = freezeOutput.split(EOL);
|
|
196
|
+
const depNames = lines.map(line => getDependencyName(line));
|
|
197
|
+
const pipShowOutput = getPipShowOutput.call(this, depNames);
|
|
198
|
+
allPipShowDeps = pipShowOutput.split(EOL + "---" + EOL);
|
|
179
199
|
}
|
|
180
200
|
else {
|
|
181
201
|
pipDepTreeJsonArrayOutput = getDependencyTreeJsonFromPipDepTree(this.pathToPipBin, this.pathToPythonBin);
|
|
182
202
|
}
|
|
183
|
-
if (usePipDepTree !== "true") {
|
|
184
|
-
pipShowOutput = getPipShowOutput.call(this, depNames);
|
|
185
|
-
allPipShowDeps = pipShowOutput.split(EOL + "---" + EOL);
|
|
186
|
-
}
|
|
187
|
-
//debug
|
|
188
|
-
// pipShowOutput = "alternative pip show output goes here for debugging"
|
|
189
203
|
let matchManifestVersions = getCustom("MATCH_MANIFEST_VERSIONS", "true", this.options);
|
|
190
|
-
let
|
|
204
|
+
let parsedRequirements = await this.#parseRequirements();
|
|
191
205
|
let CachedEnvironmentDeps = {};
|
|
192
206
|
if (usePipDepTree !== "true") {
|
|
193
|
-
allPipShowDeps.forEach(
|
|
207
|
+
allPipShowDeps.forEach(record => {
|
|
194
208
|
let dependencyName = getDependencyNameShow(record).toLowerCase();
|
|
195
209
|
CachedEnvironmentDeps[dependencyName] = record;
|
|
196
210
|
CachedEnvironmentDeps[dependencyName.replace("-", "_")] = record;
|
|
@@ -210,40 +224,24 @@ export default class Python_controller {
|
|
|
210
224
|
CachedEnvironmentDeps[packageName.replace("_", "-")] = pipDepTreeEntryForCache;
|
|
211
225
|
});
|
|
212
226
|
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
if (matchManifestVersions === "true") {
|
|
216
|
-
let dependencyName;
|
|
217
|
-
let manifestVersion;
|
|
227
|
+
parsedRequirements.forEach(({ name: depName, version: manifestVersion }) => {
|
|
228
|
+
if (matchManifestVersions === "true" && manifestVersion != null) {
|
|
218
229
|
let installedVersion;
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
manifestVersion = dep.substring(doubleEqualSignPosition + 2).trim();
|
|
223
|
-
if (manifestVersion.includes("#")) {
|
|
224
|
-
let hashCharIndex = manifestVersion.indexOf("#");
|
|
225
|
-
manifestVersion = manifestVersion.substring(0, hashCharIndex);
|
|
230
|
+
if (CachedEnvironmentDeps[depName.toLowerCase()] !== undefined) {
|
|
231
|
+
if (usePipDepTree !== "true") {
|
|
232
|
+
installedVersion = getDependencyVersion(CachedEnvironmentDeps[depName.toLowerCase()]);
|
|
226
233
|
}
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
if (CachedEnvironmentDeps[dependencyName.toLowerCase()] !== undefined) {
|
|
230
|
-
if (usePipDepTree !== "true") {
|
|
231
|
-
installedVersion = getDependencyVersion(CachedEnvironmentDeps[dependencyName.toLowerCase()]);
|
|
232
|
-
}
|
|
233
|
-
else {
|
|
234
|
-
installedVersion = CachedEnvironmentDeps[dependencyName.toLowerCase()].version;
|
|
235
|
-
}
|
|
234
|
+
else {
|
|
235
|
+
installedVersion = CachedEnvironmentDeps[depName.toLowerCase()].version;
|
|
236
236
|
}
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
}
|
|
237
|
+
}
|
|
238
|
+
if (installedVersion) {
|
|
239
|
+
if (manifestVersion.trim() !== installedVersion.trim()) {
|
|
240
|
+
throw new Error(`Can't continue with analysis - versions mismatch for dependency name ${depName} (manifest version=${manifestVersion}, installed version=${installedVersion}).If you want to allow version mismatch for analysis between installed and requested packages, set environment variable/setting MATCH_MANIFEST_VERSIONS=false`);
|
|
241
241
|
}
|
|
242
242
|
}
|
|
243
243
|
}
|
|
244
|
-
let path =
|
|
245
|
-
let depName = getDependencyName(dep);
|
|
246
|
-
//array to track a path for each branch in the dependency tree
|
|
244
|
+
let path = [];
|
|
247
245
|
path.push(depName.toLowerCase());
|
|
248
246
|
bringAllDependencies(dependencies, depName, CachedEnvironmentDeps, includeTransitive, path, usePipDepTree);
|
|
249
247
|
});
|
|
@@ -347,11 +345,11 @@ function bringAllDependencies(dependencies, dependencyName, cachedEnvironmentDep
|
|
|
347
345
|
version = record.version;
|
|
348
346
|
directDeps = record.dependencies;
|
|
349
347
|
}
|
|
350
|
-
let targetDeps =
|
|
348
|
+
let targetDeps = [];
|
|
351
349
|
let entry = { "name": depName, "version": version, "dependencies": [] };
|
|
352
350
|
dependencies.push(entry);
|
|
353
351
|
directDeps.forEach((dep) => {
|
|
354
|
-
let depArray =
|
|
352
|
+
let depArray = [];
|
|
355
353
|
// to avoid infinite loop, check if the dependency not already on current path, before going recursively resolving its dependencies.
|
|
356
354
|
if (!path.includes(dep.toLowerCase())) {
|
|
357
355
|
// send to recurrsion the path + the current dep
|
|
@@ -3,6 +3,7 @@ declare namespace _default {
|
|
|
3
3
|
export { validateLockFile };
|
|
4
4
|
export { provideComponent };
|
|
5
5
|
export { provideStack };
|
|
6
|
+
export { readLicenseFromManifest };
|
|
6
7
|
}
|
|
7
8
|
export default _default;
|
|
8
9
|
export type DependencyEntry = {
|
|
@@ -23,13 +24,19 @@ declare function validateLockFile(): boolean;
|
|
|
23
24
|
* Provide content and content type for python-pip component analysis.
|
|
24
25
|
* @param {string} manifest - path to requirements.txt for component report
|
|
25
26
|
* @param {{}} [opts={}] - optional various options to pass along the application
|
|
26
|
-
* @returns {Provided}
|
|
27
|
+
* @returns {Promise<Provided>}
|
|
27
28
|
*/
|
|
28
|
-
declare function provideComponent(manifest: string, opts?: {}
|
|
29
|
+
declare function provideComponent(manifest: string, opts?: {}): Promise<Provided>;
|
|
29
30
|
/**
|
|
30
31
|
* Provide content and content type for python-pip stack analysis.
|
|
31
32
|
* @param {string} manifest - the manifest path or name
|
|
32
33
|
* @param {{}} [opts={}] - optional various options to pass along the application
|
|
33
|
-
* @returns {Provided}
|
|
34
|
+
* @returns {Promise<Provided>}
|
|
34
35
|
*/
|
|
35
|
-
declare function provideStack(manifest: string, opts?: {}
|
|
36
|
+
declare function provideStack(manifest: string, opts?: {}): Promise<Provided>;
|
|
37
|
+
/**
|
|
38
|
+
* Python requirements.txt has no standard license field
|
|
39
|
+
* @param {string} manifestPath - path to requirements.txt
|
|
40
|
+
* @returns {string|null}
|
|
41
|
+
*/
|
|
42
|
+
declare function readLicenseFromManifest(manifestPath: string): string | null;
|