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

Add statistics to JSON and YAML reports #730

Merged
merged 7 commits into from
Dec 31, 2024

Conversation

egibs
Copy link
Member

@egibs egibs commented Dec 18, 2024

Addresses: #725

This is a first pass at adding statistics to JSON and YAML reports. For now, the values are not formatted or converted like the terminal-based statistics are.

JSON:

$ go run cmd/mal/mal.go --format json --stats --all analyze ~/Downloads/gitlab-rails/srv/gitlab/vendor/project_templates/iosswift.tar.gz | jq .Stats
{
  "RiskStats": [
    {
      "Count": 13,
      "Key": 0,
      "Total": 15,
      "Value": 86.66666666666667
    },
    {
      "Count": 1,
      "Key": 1,
      "Total": 15,
      "Value": 6.666666666666667
    },
    {
      "Count": 1,
      "Key": 2,
      "Total": 15,
      "Value": 6.666666666666667
    }
  ],
  "PkgStats": [
    {
      "Count": 1,
      "Key": "credential/password",
      "Total": 4,
      "Value": 25
    },
    {
      "Count": 1,
      "Key": "credential/sniffer/bpf",
      "Total": 4,
      "Value": 25
    },
    {
      "Count": 1,
      "Key": "net/tcp/ssh",
      "Total": 4,
      "Value": 25
    },
    {
      "Count": 1,
      "Key": "process/chdir",
      "Total": 4,
      "Value": 25
    }
  ],
  "TotalRisks": 15,
  "ProcessedFiles": 15,
  "SkippedFiles": 0,
  "TotalBehaviors": 4
}

YAML:

$ go run cmd/mal/mal.go --format yaml --stats --all analyze ~/Downloads/gitlab-rails/srv/gitlab/vendor/project_templates/iosswift.tar.gz | yq .stats
riskstats:
  - count: 13
    key: 0
    total: 15
    value: 86.66666666666667
  - count: 1
    key: 1
    total: 15
    value: 6.666666666666667
  - count: 1
    key: 2
    total: 15
    value: 6.666666666666667
pkgstats:
  - count: 1
    key: credential/password
    total: 4
    value: 25
  - count: 1
    key: credential/sniffer/bpf
    total: 4
    value: 25
  - count: 1
    key: net/tcp/ssh
    total: 4
    value: 25
  - count: 1
    key: process/chdir
    total: 4
    value: 25
totalrisks: 15
processedfiles: 15
skippedfiles: 0
totalbehaviors: 4

Also, when not using --stats, the Stats fields will not be present in the output.

@egibs egibs requested a review from tstromberg December 18, 2024 21:50
Signed-off-by: egibs <20933572+egibs@users.noreply.github.com>
@egibs egibs force-pushed the add-stats-to-json branch from ee27345 to 14dc8d2 Compare December 19, 2024 16:53
@tstromberg
Copy link
Collaborator

Do you mind adding a test for this?

Signed-off-by: egibs <20933572+egibs@users.noreply.github.com>
@egibs
Copy link
Member Author

egibs commented Dec 19, 2024

Do you mind adding a test for this?

Added in ad5bc03 (#730).

@egibs egibs requested a review from stevebeattie December 27, 2024 13:22
Copy link
Contributor

@stevebeattie stevebeattie left a comment

Choose a reason for hiding this comment

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

This all looks good to me, except that when the diff option is passed, an empty Stats: dictionary is generated and appended, which differs from the terminal and strings renderers.

@egibs
Copy link
Member Author

egibs commented Dec 30, 2024

This all looks good to me, except that when the diff option is passed, an empty Stats: dictionary is generated and appended, which differs from the terminal and strings renderers.

Ah, yea, that's an easy fix. I'll push that shortly.

Signed-off-by: egibs <20933572+egibs@users.noreply.github.com>
@egibs
Copy link
Member Author

egibs commented Dec 30, 2024

This all looks good to me, except that when the diff option is passed, an empty Stats: dictionary is generated and appended, which differs from the terminal and strings renderers.

Fixed in b080ad9 (#730).

Signed-off-by: egibs <20933572+egibs@users.noreply.github.com>
Copy link
Contributor

@stevebeattie stevebeattie left a comment

Choose a reason for hiding this comment

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

Thanks for fixing that up, that works for me.

@egibs egibs merged commit 3b49925 into chainguard-dev:main Dec 31, 2024
8 checks passed
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