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

Improved detection for standalone jar files by using pom.properties file if available #724

Merged

Conversation

Nikemare
Copy link
Contributor

@Nikemare Nikemare commented Nov 17, 2023

Another improvement for standalone jar files

  1. Optimized extract jar to only spawn "META-INF" directory
  2. First checks if pom.properties is available - see: https://maven.apache.org/shared/maven-archiver/index.html
  3. Fallback to old logic if pom.properties is not available

Steps to reproduce:

  1. Download org.jacoco.report-0.8.8.jar, xmlschema-core-2.2.5.jar, jackson-core-2.16.0.jar, junit-4.13.2.jar, wsdl4j-1.6.3.jar and put them into empty folder.
  2. Run cdxgen against this folder

xmlschema-core and org.jacoco.report before my change:

"components": [
    {
      "group": "org.apache",
      "name": "xmlschema-core",
      "version": "2.2.5",
      "purl": "pkg:maven/org.apache/[email protected]?type=jar",
      "type": "framework",
      "bom-ref": "pkg:maven/org.apache/[email protected]?type=jar",
      "evidence": {
        "identity": {
          "field": "purl",
          "confidence": 0.5,
          "methods": [
            {
              "technique": "filename",
              "confidence": 0.5,
              "value": "xmlschema-core-2.2.5.jar"
            }
          ]
        }
      },
      "properties": [
        {
          "name": "SrcFile",
          "value": "xmlschema-core-2.2.5.jar"
        }
      ]
    },
    {
      "group": "org.jacoco.report",
      "name": "org.jacoco.report",
      "version": "0.8.8.202204050719",
      "purl": "pkg:maven/org.jacoco.report/[email protected]?type=jar",
      "type": "library",
      "bom-ref": "pkg:maven/org.jacoco.report/[email protected]?type=jar",
      "evidence": {
        "identity": {
          "field": "purl",
          "confidence": 0.5,
          "methods": [
            {
              "technique": "filename",
              "confidence": 0.5,
              "value": "org.jacoco.report-0.8.8.jar"
            }
          ]
        }
      },
      "properties": [
        {
          "name": "SrcFile",
          "value": "org.jacoco.report-0.8.8.jar"
        }
      ]
    }
]

and after my change with correct group, name and version, which results also in successfully fetched license information:

"components": [
    {
      "publisher": "The Apache Software Foundation",
      "group": "org.apache.ws.xmlschema",
      "name": "xmlschema-core",
      "version": "2.2.5",
      "description": "Commons XMLSchema is a light weight schema object model that can be used to manipulate or\n        generate XML schema.",
      "licenses": [
        {
          "license": {
            "id": "Apache-2.0",
            "url": "https://opensource.org/licenses/Apache-2.0"
          }
        }
      ],
      "purl": "pkg:maven/org.apache.ws.xmlschema/[email protected]?type=jar",
      "externalReferences": [
        {
          "type": "vcs",
          "url": "https://gitbox.apache.org/repos/asf?p=ws-xmlschema.git;a=summary"
        }
      ],
      "type": "framework",
      "bom-ref": "pkg:maven/org.apache.ws.xmlschema/[email protected]?type=jar",
      "evidence": {
        "identity": {
          "field": "purl",
          "confidence": 1,
          "methods": [
            {
              "technique": "manifest-analysis",
              "confidence": 1,
              "value": "xmlschema-core-2.2.5.jar"
            }
          ]
        }
      },
      "properties": [
        {
          "name": "SrcFile",
          "value": "xmlschema-core-2.2.5.jar"
        }
      ]
    },
    {
      "publisher": "Mountainminds GmbH & Co. KG",
      "group": "org.jacoco",
      "name": "org.jacoco.report",
      "version": "0.8.8",
      "description": "JaCoCo Report",
      "licenses": [
        {
          "license": {
            "name": "Eclipse Public License 2.0"
          }
        }
      ],
      "purl": "pkg:maven/org.jacoco/[email protected]?type=jar",
      "externalReferences": [
        {
          "type": "vcs",
          "url": "https://github.com/jacoco/jacoco"
        }
      ],
      "type": "library",
      "bom-ref": "pkg:maven/org.jacoco/[email protected]?type=jar",
      "evidence": {
        "identity": {
          "field": "purl",
          "confidence": 1,
          "methods": [
            {
              "technique": "manifest-analysis",
              "confidence": 1,
              "value": "org.jacoco.report-0.8.8.jar"
            }
          ]
        }
      },
      "properties": [
        {
          "name": "SrcFile",
          "value": "org.jacoco.report-0.8.8.jar"
        }
      ]
    }

In both cases FETCH_LICENSE was set to true. Other components have unchanged results, as they were resolved correctly already before my change.

use pom.properties if available
fallback to old logic if pom properties is not available
extract only META-INF from jar

Signed-off-by: Nikemare <[email protected]>
Signed-off-by: Nikemare <[email protected]>
Signed-off-by: Nikemare <[email protected]>
@prabhu
Copy link
Collaborator

prabhu commented Nov 19, 2023

@Nikemare This is impressive work. Could you kindly add some repotests to test this?

@heubeck could you kindly check how this is looking for your workflows?

@Nikemare
Copy link
Contributor Author

Nikemare commented Nov 19, 2023

@prabhu Thank you. About repotests I'm a little bit lost in the moment, if you can guide me in the right direction I will add it.

I found repotests.yml

  1. I guess I need to add a checkout step, right?
  2. Should I create a repo in my GitHub space?
  3. Then I guess I need to add another line in step 'repotests' which will generate a json

But then, how do I validate the json? Or is the auto-validation via --validate enough?

@prabhu
Copy link
Collaborator

prabhu commented Nov 19, 2023

@Nikemare, it's a good question.

Can we do something similar to our jenkins tests? Download some jars to a directory and run cdxgen?

https://github.com/CycloneDX/cdxgen/blob/master/.github/workflows/repotests.yml#L215

We can just print the sbom with -p for now. In the future, we can add some snapshot based testing.

Copy link
Collaborator

@prabhu prabhu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much!

@Nikemare
Copy link
Contributor Author

@prabhu I made a mistake in repotest, will commit fix in a second.

Missed to specify directory in repotest for standalone jar files

Signed-off-by: Nikemare <[email protected]>
@Nikemare
Copy link
Contributor Author

Step standalone jar files in repotests takes 2 seconds with following output now:

Run mkdir -p standalone-jar-files
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100  125k  100  125k    0     0  1261k      0 --:--:-- --:--:-- --:--:-- 1271k
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100  169k  100  169k    0     0  2169k      0 --:--:-- --:--:-- --:--:-- 2197k
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100  565k  100  565k    0     0  7152k      0 --:--:-- --:--:-- --:--:-- 7250k
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100  375k  100  375k    0     0  5361k      0 --:--:-- --:--:-- --:--:-- 5443k
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
  0  182k    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100  182k  100  182k    0     0  1442k      0 --:--:-- --:--:-- --:--:-- 1436k
╔═══════════════════════════╤═════════════════════════════════════╤═══════════════════════════╤═════════════════╗
║ Group                     │ Name                                │                   Version │ Scope           ║
╚═══════════════════════════╧═════════════════════════════════════╧═══════════════════════════╧═════════════════╝
╟───────────────────────────┼─────────────────────────────────────┼───────────────────────────┼─────────────────╢
║ org.apache.ws.xmlschema   │ xmlschema-core                      │                     2.2.5 │                 ║
╚═══════════════════════════╧═════════════════════════════════════╧═══════════════════════════╧═════════════════╝
╟───────────────────────────┼─────────────────────────────────────┼───────────────────────────┼─────────────────╢
║ wsdl4j                    │ wsdl4j                              │                     1.6.3 │                 ║
╚═══════════════════════════╧═════════════════════════════════════╧═══════════════════════════╧═════════════════╝
╟───────────────────────────┼─────────────────────────────────────┼───────────────────────────┼─────────────────╢
║ org.jacoco                │ org.jacoco.report                   │                     0.8.8 │                 ║
╚═══════════════════════════╧═════════════════════════════════════╧═══════════════════════════╧═════════════════╝
╟───────────────────────────┼─────────────────────────────────────┼───────────────────────────┼─────────────────╢
║ junit                     │ junit                               │                    4.13.2 │                 ║
╚═══════════════════════════╧═════════════════════════════════════╧═══════════════════════════╧═════════════════╝
╟───────────────────────────┼─────────────────────────────────────┼───────────────────────────┼─────────────────╢
║ com.fasterxml.jackson.cor │ jackson-core                        │                    2.16.0 │                 ║
║ e                         │                                     │                           │                 ║
╚═══════════════════════════╧═════════════════════════════════════╧═══════════════════════════╧═════════════════╝
BOM includes 5 components and 0 dependencies

@@ -228,7 +228,7 @@ jobs:
curl --output-dir standalone-jar-files -LO https://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-core/2.16.0/jackson-core-2.16.0.jar
curl --output-dir standalone-jar-files -LO https://repo1.maven.org/maven2/junit/junit/4.13.2/junit-4.13.2.jar
curl --output-dir standalone-jar-files -LO https://repo1.maven.org/maven2/wsdl4j/wsdl4j/1.6.3/wsdl4j-1.6.3.jar
FETCH_LICENSE=true bin/cdxgen.js -p -o bomresults/bom-standalone-jar-files.json --validate
FETCH_LICENSE=true bin/cdxgen.js -p standalone-jar-files -o bomresults/bom-standalone-jar-files.json --validate
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well spotted!

@prabhu prabhu merged commit b0105c9 into CycloneDX:master Nov 19, 2023
17 checks passed
@heubeck
Copy link
Contributor

heubeck commented Nov 20, 2023

Works fine for me - but I'm analyzing only source code repos so far.

if (existsSync(mavenDir)) {
let groupDir = readdirSync(mavenDir);
if (groupDir && groupDir.length) {
let artifactDir = readdirSync(join(mavenDir, groupDir[0]));
Copy link
Collaborator

@prabhu prabhu Nov 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Nikemare, This line is failing while creating an SBOM for container images.

Error: ENOTDIR: not a directory, scandir '/tmp/jar-deps-pgCSWJ/META-INF/maven/extension.xml'
    at readdirSync (node:fs:1516:26)
    at extractJarArchive (file:///mnt/work/CycloneDX/cdxgen/utils.js:6650:31)
    at createJarBom (file:///mnt/work/CycloneDX/cdxgen/index.js:1084:21)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async createMultiXBom (file:///mnt/work/CycloneDX/cdxgen/index.js:4868:15)
    at async createBom (file:///mnt/work/CycloneDX/cdxgen/index.js:5388:21)
    at async file:///mnt/work/CycloneDX/cdxgen/bin/cdxgen.js:366:20 {
  errno: -20,

Below change at 6649 helps

if (
            groupDir &&
            groupDir.length &&
            lstatSync(join(mavenDir, groupDir[0])).isDirectory()
          ) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@prabhu I can provide a fix in the evening (CET), or will you fix it on your own?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The evening sounds good! Thank you so much!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #727

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants