-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathjava_maven.js
335 lines (302 loc) · 11.4 KB
/
java_maven.js
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import { XMLParser } from 'fast-xml-parser'
import fs from 'node:fs'
import { getCustomPath, getGitRootDir, getWrapperPreference } from "../tools.js";
import os from 'node:os'
import path from 'node:path'
import Sbom from '../sbom.js'
import { EOL } from 'os'
import Base_java, { ecosystem_maven } from "./base_java.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 */
export default class Java_maven extends Base_java {
/**
* @param {string} manifestName - the subject manifest name-type
* @returns {boolean} - return true if `pom.xml` is the manifest name-type
*/
isSupported(manifestName) {
return 'pom.xml' === manifestName
}
/**
* @param {string} manifestDir - the directory where the manifest lies
*/
validateLockFile() { return true; }
/**
* Provide content and content type for maven-maven stack analysis.
* @param {string} manifest - the manifest path or name
* @param {{}} [opts={}] - optional various options to pass along the application
* @returns {Provided}
*/
provideStack(manifest, opts = {}) {
return {
ecosystem: ecosystem_maven,
content: this.#createSbomStackAnalysis(manifest, opts),
contentType: 'application/vnd.cyclonedx+json'
}
}
/**
* Provide content and content type for maven-maven component analysis.
* @param {string} manifest - path to the manifest file
* @param {{}} [opts={}] - optional various options to pass along the application
* @returns {Provided}
*/
provideComponent(manifest, opts = {}) {
return {
ecosystem: ecosystem_maven,
content: this.#getSbomForComponentAnalysis(manifest, opts),
contentType: 'application/vnd.cyclonedx+json'
}
}
/**
* Create a Dot Graph dependency tree for a manifest path.
* @param {string} manifest - path for pom.xml
* @param {{}} [opts={}] - optional various options to pass along the application
* @returns {string} the Dot Graph content
* @private
*/
#createSbomStackAnalysis(manifest, opts = {}) {
const mvn = this.#selectMvnRuntime(manifest, opts)
// clean maven target
try {
this._invokeCommand(mvn, ['-q', 'clean', '-f', manifest])
} catch (error) {
throw new Error(`failed to clean maven target`, {cause: error})
}
// create dependency graph in a temp file
let tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'exhort_'))
let tmpDepTree = path.join(tmpDir, 'mvn_deptree.txt')
// build initial command (dot outputType is not available for verbose mode)
let depTreeCmdArgs = ['-q', 'org.apache.maven.plugins:maven-dependency-plugin:3.6.0:tree', '-Dverbose', '-DoutputType=text', `-DoutputFile=${tmpDepTree}`, '-f', manifest]
// exclude ignored dependencies, exclude format is groupId:artifactId:scope:version.
// version and scope are marked as '*' if not specified (we do not use scope yet)
let ignoredDeps = new Array()
this.#getDependencies(manifest).forEach(dep => {
if (dep.ignore) {
depTreeCmdArgs.push(`-Dexcludes=${dep['groupId']}:${dep['artifactId']}:${dep['scope']}:${dep['version']}`)
ignoredDeps.push(this.toPurl(dep.groupId, dep.artifactId, dep.version).toString())
}
})
// execute dependency tree command
try {
this._invokeCommand(mvn, depTreeCmdArgs)
} catch (error) {
throw new Error(`failed creating maven dependency tree`, {cause: error})
}
// read dependency tree from temp file
let content = fs.readFileSync(tmpDepTree)
if (process.env["EXHORT_DEBUG"] === "true") {
console.error("Dependency tree that will be used as input for creating the BOM =>" + EOL + EOL + content.toString())
}
let sbom = this.createSbomFileFromTextFormat(content.toString(), ignoredDeps,opts);
// delete temp file and directory
fs.rmSync(tmpDir, {recursive: true, force: true})
// return dependency graph as string
return sbom
}
/**
*
* @param {String} textGraphList Text graph String of the manifest
* @param {[String]} ignoredDeps List of ignored dependencies to be omitted from sbom
* @return {String} formatted sbom Json String with all dependencies
*/
createSbomFileFromTextFormat(textGraphList, ignoredDeps, opts) {
let lines = textGraphList.split(EOL);
// get root component
let root = lines[0];
let rootPurl = this.parseDep(root);
let sbom = new Sbom();
sbom.addRoot(rootPurl);
this.parseDependencyTree(root, 0, lines.slice(1), sbom);
return sbom.filterIgnoredDepsIncludingVersion(ignoredDeps).getAsJsonString(opts);
}
/**
* Create a dependency list for a manifest content.
* @param {{}} [opts={}] - optional various options to pass along the application
* @returns {[Dependency]} the Dot Graph content
* @private
*/
#getSbomForComponentAnalysis(manifestPath, opts = {}) {
const mvn = this.#selectMvnRuntime(manifestPath, opts)
const tmpEffectivePom = path.resolve(path.join(path.dirname(manifestPath), 'effective-pom.xml'))
const targetPom = manifestPath
// create effective pom and save to temp file
try {
this._invokeCommand(mvn, ['-q', 'help:effective-pom', `-Doutput=${tmpEffectivePom}`, '-f', targetPom])
} catch (error) {
throw new Error(`failed creating maven effective pom`, {cause: error})
}
// iterate over all dependencies in original pom and collect all ignored ones
let ignored = this.#getDependencies(targetPom).filter(d => d.ignore)
// iterate over all dependencies and create a package for every non-ignored one
/** @type [Dependency] */
let dependencies = this.#getDependencies(tmpEffectivePom)
.filter(d => !(this.#dependencyIn(d, ignored)) && !(this.#dependencyInExcludingVersion(d, ignored)))
let sbom = new Sbom();
let rootDependency = this.#getRootFromPom(tmpEffectivePom, targetPom);
let purlRoot = this.toPurl(rootDependency.groupId, rootDependency.artifactId, rootDependency.version)
sbom.addRoot(purlRoot)
let rootComponent = sbom.getRoot();
dependencies.forEach(dep => {
let currentPurl = this.toPurl(dep.groupId, dep.artifactId, dep.version)
sbom.addDependency(rootComponent, currentPurl)
})
fs.rmSync(tmpEffectivePom)
// return dependencies list
return sbom.getAsJsonString(opts)
}
/**
*
* @param effectivePomManifest effective pom manifest path
* @param originalManifest pom.xml manifest path
* @return {Dependency} returns the root dependency for the pom
* @private
*/
#getRootFromPom(effectivePomManifest) {
let parser = new XMLParser()
let buf = fs.readFileSync(effectivePomManifest)
let effectivePomStruct = parser.parse(buf.toString())
let pomRoot
if (effectivePomStruct['project']) {
pomRoot = effectivePomStruct['project']
} else { // if there is no project root tag, then it's a multi module/submodules aggregator parent POM
for (let proj of effectivePomStruct['projects']['project']) {
// need to choose the aggregate POM and not one of the modules.
if (proj.packaging && proj.packaging === 'pom') {
pomRoot = proj
}
}
}
/** @type Dependency */
let rootDependency = {
groupId: pomRoot['groupId'],
artifactId: pomRoot['artifactId'],
version: pomRoot['version'],
scope: '*',
ignore: false
}
return rootDependency
}
#selectMvnRuntime(manifestPath, opts) {
// get custom maven path
let mvn = getCustomPath('mvn', opts)
// check if mvnw is preferred and available
let useMvnw = getWrapperPreference('mvn', opts)
if (useMvnw) {
const mvnw = this.#traverseForMvnw(manifestPath)
if (mvnw !== undefined) {
try {
this._invokeCommand(mvnw, ['--version'])
} catch (error) {
if (error.code === 'ENOENT') {
useMvnw = false
} else {
throw new Error(`failed to check for mvnw`, {cause: error})
}
}
mvn = useMvnw ? mvnw : mvn
}
} else {
// verify maven is accessible
try {
this._invokeCommand(mvn, ['--version'])
} catch (error) {
if (error.code === 'ENOENT') {
throw new Error(`maven not accessible at "${mvn}"`)
} else {
throw new Error(`failed to check for maven`, {cause: error})
}
}
}
return mvn
}
/**
*
* @param {string} startingManifest - the path of the manifest from which to start searching for mvnw
* @param {string} repoRoot - the root of the repository at which point to stop searching for mvnw, derived via git if unset and then fallsback
* to the root of the drive the manifest is on (assumes absolute path is given)
* @returns
*/
#traverseForMvnw(startingManifest, repoRoot = undefined) {
repoRoot = repoRoot || getGitRootDir(path.resolve(path.dirname(startingManifest))) || path.parse(path.resolve(startingManifest)).root
const wrapperName = 'mvnw' + (process.platform === 'win32' ? '.cmd' : '');
const wrapperPath = path.join(path.resolve(path.dirname(startingManifest)), wrapperName);
try {
fs.accessSync(wrapperPath, fs.constants.X_OK)
} catch(error) {
if (error.code === 'ENOENT') {
if (path.resolve(path.dirname(startingManifest)) === repoRoot) {
return undefined
}
return this.#traverseForMvnw(path.resolve(path.dirname(startingManifest)), repoRoot)
}
throw new Error(`failure searching for mvnw`, {cause: error})
}
return wrapperPath
}
/**
* Get a list of dependencies with marking of dependencies commented with <!--exhortignore-->.
* @param {string} manifest - path for pom.xml
* @returns {[Dependency]} an array of dependencies
* @private
*/
#getDependencies(manifest) {
/** @type [Dependency] */
let ignored = []
// build xml parser with options
let parser = new XMLParser({
commentPropName: '#comment', // mark comments with #comment
isArray: (_, jpath) => 'project.dependencies.dependency' === jpath,
numberParseOptions: {
skipLike: /[0-9]+[.]0/
}
})
// read manifest pom.xml file into buffer
let buf = fs.readFileSync(manifest)
// parse manifest pom.xml to json
let pomJson = parser.parse(buf.toString())
// iterate over all dependencies and chery pick dependencies with a exhortignore comment
let pomXml;
// project without modules
if (pomJson['project']) {
if (pomJson['project']['dependencies'] !== undefined) {
pomXml = pomJson['project']['dependencies']['dependency']
} else {
pomXml = []
}
} else { // project with modules
pomXml = pomJson['projects']['project'].filter(project => project.dependencies !== undefined).flatMap(project => project.dependencies.dependency)
}
pomXml.forEach(dep => {
let ignore = false
if (dep['#comment'] && dep['#comment'].includes('exhortignore')) { // #comment is an array or a string
ignore = true
}
if (dep['scope'] !== 'test') {
ignored.push({
groupId: dep['groupId'],
artifactId: dep['artifactId'],
version: dep['version'] ? dep['version'].toString() : '*',
scope: '*',
ignore: ignore
})
}
})
// return list of dependencies
return ignored
}
/**
* Utility function for looking up a dependency in a list of dependencies ignoring the "ignored"
* field
* @param dep {Dependency} dependency to look for
* @param deps {[Dependency]} list of dependencies to look in
* @returns boolean true if found dep in deps
* @private
*/
#dependencyIn(dep, deps) {
return deps.filter(d => dep.artifactId === d.artifactId && dep.groupId === d.groupId && dep.version === d.version && dep.scope === d.scope).length > 0
}
#dependencyInExcludingVersion(dep, deps) {
return deps.filter(d => dep.artifactId === d.artifactId && dep.groupId === d.groupId && dep.scope === d.scope).length > 0
}
}