Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix missing version/namespace in maven & npm #105

Merged
merged 3 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import exhort from './index.js'
import { hideBin } from 'yargs/helpers'
import yargs from 'yargs'
import * as path from "path";

Check warning on line 6 in src/cli.js

View workflow job for this annotation

GitHub Actions / Lint and test project (18)

Expected 'all' syntax before 'single' syntax

Check warning on line 6 in src/cli.js

View workflow job for this annotation

GitHub Actions / Lint and test project (latest)

Expected 'all' syntax before 'single' syntax

// command for component analysis take manifest type and content
const component = {
Expand Down Expand Up @@ -116,10 +117,11 @@

// parse and invoke the command
yargs(hideBin(process.argv))
.usage('Usage: $0 {component|stack|validate-token}')
.usage(`Usage: ${process.argv[0].includes("node") ? path.parse(process.argv[1]).base : path.parse(process.argv[0]).base} {component|stack|validate-token}`)
.command(stack)
.command(component)
.command(validateToken)
.scriptName('')
.version(false)
.demandCommand(1)
.parse()
27 changes: 21 additions & 6 deletions src/providers/java_maven.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {XMLParser} from 'fast-xml-parser'
import {execSync} from "node:child_process"
import fs from 'node:fs'
import {getCustomPath,handleSpacesInPath} from "../tools.js";

Check warning on line 4 in src/providers/java_maven.js

View workflow job for this annotation

GitHub Actions / Lint and test project (18)

Expected 'multiple' syntax before 'single' syntax

Check warning on line 4 in src/providers/java_maven.js

View workflow job for this annotation

GitHub Actions / Lint and test project (latest)

Expected 'multiple' syntax before 'single' syntax
import os from 'node:os'
import path from 'node:path'
import Sbom from '../sbom.js'

Check warning on line 7 in src/providers/java_maven.js

View workflow job for this annotation

GitHub Actions / Lint and test project (18)

Imports should be sorted alphabetically

Check warning on line 7 in src/providers/java_maven.js

View workflow job for this annotation

GitHub Actions / Lint and test project (latest)

Imports should be sorted alphabetically
import {PackageURL} from 'packageurl-js'
import {EOL} from 'os'

Expand Down Expand Up @@ -259,7 +259,7 @@
let dependencies = getDependencies(tmpEffectivePom)
.filter(d => !(dependencyIn(d, ignored)) && !(dependencyInExcludingVersion(d, ignored)))
let sbom = new Sbom();
let rootDependency = getRootFromPom(targetPom);
let rootDependency = getRootFromPom(tmpEffectivePom,targetPom);
let purlRoot = toPurl(rootDependency.groupId, rootDependency.artifactId, rootDependency.version)
sbom.addRoot(purlRoot)
let rootComponent = sbom.getRoot();
Expand All @@ -282,16 +282,31 @@

/**
*
* @param pom.xml manifest path
* @param effectivePomManifest effective pom manifest path
* @param originalManifest pom.xml manifest path
* @return {Dependency} returns the root dependency for the pom
* @private
*/
function getRootFromPom(manifest) {
function getRootFromPom(effectivePomManifest) {

let parser = new XMLParser()
let buf = fs.readFileSync(manifest)
let pomStruct = parser.parse(buf.toString())
let pomRoot = pomStruct['project'];
let buf = fs.readFileSync(effectivePomManifest)
let effectivePomStruct = parser.parse(buf.toString())
let pomRoot
if(effectivePomStruct['project']) {
pomRoot = effectivePomStruct['project']
}
// if there is no project root tag, then it's a multi module/submodules aggregator parent POM
else
{
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'],
Expand Down
4 changes: 4 additions & 0 deletions src/providers/javascript_npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export default { isSupported, provideComponent, provideStack, npmInteractions }
* @private
*/
const ecosystem = 'npm'
const defaultVersion = 'v0.0.0'

/**
* @param {string} manifestName - the subject manifest name-type
Expand Down Expand Up @@ -123,6 +124,9 @@ function getSBOM(manifest, opts = {}, includeTransitive) {
let depsObject = JSON.parse(npmOutput);
let rootName = depsObject["name"]
let rootVersion = depsObject["version"]
if(!rootVersion) {
rootVersion = defaultVersion
}
let mainComponent = toPurl(rootName,rootVersion);

let sbom = new Sbom();
Expand Down
5 changes: 3 additions & 2 deletions test/it/end-to-end.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,11 @@ suite('Integration Tests', () => {
// // process.env["EXHORT_PYTHON_VIRTUAL_ENV"] = "true"
// // process.env["EXHORT_PYTHON_INSTALL_BEST_EFFORTS"] = "true"
// // process.env["MATCH_MANIFEST_VERSIONS"] = "false"
// let pomPath = `/home/zgrinber/git/exhort-javascript-api/test/providers/tst_manifests/golang/go_mod_no_ignore/go.mod`
// // let pomPath = `/tmp/070324/package.json`
// let pomPath = `/tmp/artifact-without-version-or-group/sbom-json-traversor/pom.xml`
// // let pomPath = `/home/zgrinber/git/tracing-demos-and-examples/tracing-parent/pom.xml`
// let providedDataForStack;
// providedDataForStack = await index.stackAnalysis(pomPath, false,opts);
// providedDataForStack = await index.componentAnalysis("pom.xml", fs.readFileSync(pomPath),{} ,pomPath);
// // console.log(JSON.stringify(providedDataForStack,null , 4))
// // fs.writeFileSync(`/tmp/301123/report.html`,providedDataForStack)
//
Expand Down
Loading