-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathbase_java.js
More file actions
177 lines (159 loc) · 5.65 KB
/
Copy pathbase_java.js
File metadata and controls
177 lines (159 loc) · 5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import path from 'node:path'
import { PackageURL } from 'packageurl-js'
import { getCustomPath, getWrapperPreference, invokeCommand, traverseForWrapper } from "../tools.js"
/** @typedef {import('../provider').Provider} */
/** @typedef {import('../provider').Provided} Provided */
/** @typedef {{name: string, version: string}} Package */
/** @typedef {{groupId: string, artifactId: string, version: string, scope: string, ignore: boolean}} Dependency */
/**
* @type {string} ecosystem for java maven packages.
* @private
*/
export const ecosystem_maven = 'maven'
export const ecosystem_gradle = 'gradle'
export default class Base_Java {
DEP_REGEX = /(([-a-zA-Z0-9._]{2,})|[0-9])/g
CONFLICT_REGEX = /.*omitted for conflict with (\S+)\)/
globalBinary
localWrapper
/**
*
* @param {string} globalBinary name of the global binary
* @param {string} localWrapper name of the local wrapper filename
*/
constructor(globalBinary, localWrapper) {
this.globalBinary = globalBinary
this.localWrapper = localWrapper
}
/**
* Returns the package manager name (e.g. mvn, gradle)
* @returns {string}
*/
packageManagerName() {
return this.globalBinary
}
/**
* Recursively populates the SBOM instance with the parsed graph
* @param {string} src - Source dependency to start the calculations from
* @param {number} srcDepth - Current depth in the graph for the given source
* @param {Array} lines - Array containing the text files being parsed
* @param {Sbom} sbom - The SBOM where the dependencies are being added
*/
parseDependencyTree(src, srcDepth, lines, sbom) {
if (lines.length === 0) {
return;
}
if ((lines.length === 1 && lines[0].trim() === "")) {
return;
}
let index = 0;
let target = lines[index];
let targetDepth = this._getDepth(target);
while (targetDepth > srcDepth && index < lines.length) {
if (targetDepth === srcDepth + 1) {
let from = this.parseDep(src);
let to = this.parseDep(target);
let matchedScope = target.match(/:compile|:provided|:runtime|:test|:system|:import/g)
let matchedScopeSrc = src.match(/:compile|:provided|:runtime|:test|:system|:import/g)
// only add dependency to sbom if it's not with test scope or if it's root
if ((matchedScope && matchedScope[0] !== ":test" && (matchedScopeSrc && matchedScopeSrc[0] !== ":test")) || (srcDepth === 0 && matchedScope && matchedScope[0] !== ":test")) {
sbom.addDependency(from, to)
}
} else {
this.parseDependencyTree(lines[index - 1], this._getDepth(lines[index - 1]), lines.slice(index), sbom)
}
target = lines[++index];
targetDepth = this._getDepth(target);
}
}
/**
* Calculates how deep in the graph is the given line
* @param {string} line - line to calculate the depth from
* @returns {number} The calculated depth
* @protected
*/
_getDepth(line) {
if (!line || line.trim() === '') { return -1; }
if (line.match(/^\w/)) { return 0; }
return ((line.indexOf('-') - 1) / 3) + 1;
}
/**
* Create a PackageURL from any line in a Text Graph dependency tree for a manifest path.
* @param {string} line - line to parse from a dependencies.txt file
* @returns {PackageURL} The parsed packageURL
*/
parseDep(line) {
let match = line.split(':').map(part => part ? part.match(this.DEP_REGEX)[0] : '');
if (!match) {
throw new Error(`Unable generate SBOM from dependency tree. Line: ${line} cannot be parsed into a PackageURL`);
}
let version
if (match.length >= 5 && ['compile', 'provided', 'runtime'].includes(match[5])) {
version = `${match[4]}-${match[3]}`
} else {
version = match[3]
}
let override = line.match(this.CONFLICT_REGEX);
if (override) {
version = override[1];
}
if (match[0].trim() === '') {
throw new Error(`Artifact coordinates should have a non-empty group ID: ${line}`);
}
return this.toPurl(match[0], match[1], version);
}
/**
* Returns a PackageUrl For Java maven dependencies
* @param group
* @param artifact
* @param version
* @return {PackageURL}
*/
toPurl(group, artifact, version) {
if (typeof version === "number") {
version = version.toString()
}
return new PackageURL('maven', group, artifact, version, undefined, undefined);
}
/** This method invokes command string in a process in a synchronous way.
* Exists for stubbing in tests.
* @param bin - the command to be invoked
* @param args - the args to pass to the binary
* @param {import('child_process').ExecFileOptionsWithStringEncoding} [opts={}]
* @protected
*/
_invokeCommand(bin, args, opts={}) { return invokeCommand(bin, args, opts) }
/**
*
* @param {string} manifestPath
* @param {{}} opts
* @returns string
*/
selectToolBinary(manifestPath, opts) {
const manifestDir = path.dirname(manifestPath)
const toolPath = getCustomPath(this.globalBinary, opts)
const useWrapper = getWrapperPreference(this.globalBinary, opts)
if (useWrapper) {
const wrapper = traverseForWrapper(manifestDir, this.localWrapper)
if (wrapper !== undefined) {
try {
this._invokeCommand(wrapper, ['--version'], {cwd: manifestDir})
} catch (error) {
throw new Error(`failed to check for ${this.localWrapper}`, {cause: error})
}
return wrapper
}
}
// verify tool is accessible, if wrapper was not requested or not found
try {
this._invokeCommand(toolPath, ['--version'], {cwd: manifestDir})
} catch (error) {
if (error.code === 'ENOENT') {
throw new Error((useWrapper ? `${this.localWrapper} not found and ` : '') + `${this.globalBinary === 'mvn' ? 'maven' : 'gradle'} not found at ${toolPath}`)
} else {
throw new Error(`failed to check for ${this.globalBinary === 'mvn' ? 'maven' : 'gradle'}`, {cause: error})
}
}
return toolPath
}
}