Skip to content

Implement Grype report ingestion adapter (closes #6) - #123

Merged
rrskris merged 2 commits into
kaaval:mainfrom
Diyaaa-12:feat/grype-ingestion
Aug 13, 2026
Merged

rrskris merged 2 commits into
kaaval:mainfrom
Diyaaa-12:feat/grype-ingestion

Conversation

@Diyaaa-12

Copy link
Copy Markdown
Contributor

What this changes

Implements a pure adapter (control-plane/app/grype_adapter.py) that converts grype -o json output into Kaaval's finding shape, mirroring trivy_adapter.py (#106) exactly — same parse(report_json, context) contract, same finding shape, so ingested findings flow through compute_contextual_score() / build_remediation() unmodified. Reads matches[].vulnerability / .artifact / .fix per docs/trivy-grype-integration.md, adds additive source: "grype" and image fields, and dedups on (cve_id, image, component).

Closes #6

Type

  • New detection rule / finding source
  • Feature
  • Bug fix
  • Docs
  • Refactor / chore

Checklist

  • make test passes (full suite: 108 passed, 1 pre-existing DB-dependent failure unrelated to this change)
  • Added/updated tests for the behavior change
  • New rule ships with a remediation and a verified benchmark citation (N/A — reuses the existing CVE remediation path, no new rule/benchmark citation)
  • Added a fixture to hack/dev/rbac-fixtures.yaml if it's a new rule (N/A — CVE ingestion adapter, not an RBAC rule)
  • Docs updated (no changes needed beyond the existing design doc)
  • Conventional commit messages

How you verified it

Ran python -m pytest tests/test_grype_adapter.py -v — 17/17 passed, mirroring the Trivy adapter's test coverage: core field mapping, dedup across duplicate matches, missing fix/CVSS handling, Title-case→UPPER severity normalization (Grype's severity casing differs from Trivy's), title fallback, description truncation, scoring/remediation attachment via the real compute_contextual_score()/build_remediation(), context forwarding, and image-name fallback (userInput → imageID → "unknown").

Also ran the full suite: 108 passed, 2 skipped, 1 failed (test_smoke.py::test_login_and_self_scan — pre-existing, requires local Postgres at 127.0.0.1:5432, unrelated to this change).

@rrskris rrskris left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

First — this sat ten days without a review, and that's on me, not on you. You opened the project's first-ever PR back on 2026-07-07 and this is your fourth; waiting a week and a half for a first response isn't the deal. I've since swept the queue and I'm fixing the process that let it happen.

Now the review. The adapter is well built and one thing needs changing before it lands.

What's right

I checked this against trivy_adapter.py (#106) as the precedent, since the whole value of a second adapter is that it behaves identically to the first.

  • The finding shape matches exactly. Same dedup key (cve_id, image, component), same -contextual_score sort, same additive source/image fields, same compute_contextual_score() → build_remediation() sequence. Ingested Grype findings will flow through scoring, the PDF builder and the dashboard with nothing downstream changed, which is the actual acceptance bar in docs/trivy-grype-integration.md.
  • Test coverage exceeds the precedent. Trivy's adapter has 15 tests; you wrote 17, and the two extra ones (test_image_name_falls_back_to_image_id_when_no_user_input, test_image_name_defaults_to_unknown_when_source_missing) cover a real difference — Grype's source.target is a dict for images and a bare string for directory scans, where Trivy just has ArtifactName. You found that yourself; it isn't in the design doc.
  • The scope call is correct. You built the adapter and stopped. trivy_adapter isn't wired into any router either, so adapter-only is exactly parity with #106 rather than an omission. 17/17 pass on my machine too.
  • Title-case → UPPER severity normalization is the right Grype-specific catch.

Required: _best_cvss_score() ignores cvss[].version

docs/trivy-grype-integration.md:55 sets the contract for both adapters:

"cvss_score":        highest v3 base score present, else None

Trivy honours it structurally — it reads the V3Score key, so a v2 score can't leak in. The Grype adapter takes max(metrics.baseScore) across every entry regardless of version. Your own docstring names the hazard:

Grype's cvss array can mix v2/v3 metrics from different sources; we take the max baseScore across all of them, same spirit as Trivy's "highest V3Score across vendors" rule.

It isn't the same spirit, though — Trivy's rule maxes across vendors within v3. Maxing across versions mixes two different scales. CVSS v2 and v3.1 are not interchangeable: v2 rates many CVEs higher, so the max is systematically biased toward the v2 number.

I ran the same CVE through both adapters, carrying an NVD v2 of 10.0 and a v3.1 of 7.5 — the shape real Grype output takes for older CVEs:

expected (per the doc)  -> 7.5
trivy_adapter           -> 7.5   OK
grype_adapter           -> 10.0  took the v2 score

And because cvss_score feeds compute_contextual_score(), it doesn't stop at the raw number:

contextual_score via trivy -> 11.25
contextual_score via grype -> 15.0

Same CVE, same image, same package, same context — a third higher just because of which scanner produced the JSON. That defeats the point of a second adapter.

Your 17 tests pass because every cvss[] entry in grype_report_sample.json is either "version": "3.1" or has no version key at all. There's no v2 entry anywhere in the fixture, so nothing exercises the branch. That's the same blind spot #131 hit last week — the test suite is thorough about everything the fixture represents, and the fixture is missing the one case that matters.

The fix

    version = str(entry.get("version") or "")
    if version and not version.startswith("3"):
        continue

I applied exactly that to your branch and ran it: grype_adapter returns 7.5, contextual scores match Trivy at 11.25, and all 17 of your tests still pass — so this costs you no rework.

Two things to add with it:

  1. A fixture entry carrying both a v2 and a v3 score, with a test asserting the v3 wins. Without it the next person can undo this and CI stays green.
  2. Keep the missing-version case included — that's why the condition is if version and not ... rather than requiring a v3 match. An entry with no version key should still count. Under-reporting a CVE score is worse than over-reporting it for a scanner, and it keeps the existing fixture entry on line 124 working. Worth a one-line comment saying that's deliberate, the same way you documented the Title-case severity difference.

Two nits, non-blocking

  • _title(vulnerability, cve_id, pkg_name) never reads vulnerability — the fallback is unconditional. Either drop the parameter or actually look up a title field, so it doesn't read as an unfinished lookup.
  • grype_adapter.py has no trailing newline.

On the test-run note in your description

You reported "1 pre-existing DB-dependent failure (test_smoke.py, requires local Postgres)". That test passes for me on the default config with no Postgres and no Docker — the suite is 129 green on main. Two other contributors hit the same thing this month, so it's likely something shared in the setup docs rather than your machine; I'm looking into it. Nothing to change in this PR.

Make the CVSS change and add the fixture case and I'll merge this. The rest is ready as-is, and the extra image-name coverage is better than what the Trivy adapter shipped with.

What's next

You've now done SARIF output (#10), SARIF security-severity (#32/#56), Trivy ingestion (#106) and Grype ingestion here — you own the output-and-ingestion surface of this project more than anyone.

  • #35 — Kyverno PolicyReport ingestion, admission results as a finding source. Third adapter, same parse() contract, and you've now written two of them.
  • #94 — granted-vs-used reporting, if you'd rather move from ingestion into analysis.

Nothing is assigned to anyone else on either. Comment and I'll assign it same-day.

@Diyaaa-12
Diyaaa-12 force-pushed the feat/grype-ingestion branch from 2ccc1ba to 8f2d534 Compare August 5, 2026 11:33
@Diyaaa-12

Diyaaa-12 commented Aug 5, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the thorough review @rrskris

All changes from the review are done:

  • _best_cvss_score() fixed — v2 entries are now skipped via if version and not version.startswith("3"): continue. Entries with no version key still count (under-reporting is worse). Docstring updated to reflect the deliberate choice, same way the Title-case severity difference is documented.
  • Fixture updated — CVE-2022-3602 in grype_report_sample.json now carries both a v2 (10.0) and a v3.1 (9.8) entry, so the branch is actually exercised.
  • Test added — test_cvss_v3_wins_over_v2_when_both_present asserts 9.8 wins. 18/18 pass locally.
  • _title() parameter — dropped the unused vulnerability param; the fallback was always unconditional so there was nothing to look up.
  • Trailing newline — added.
  • DCO — rebased with --signoff; both commits now carry the Signed-off-by line.

Ready for another look when you get a chance.

@rrskris

rrskris commented Aug 5, 2026

Copy link
Copy Markdown
Collaborator

Following up on the test_smoke.py::test_login_and_self_scan failure you hit, and the local-Postgres requirement you mentioned.

You were right, and our documentation was wrong. I owe you a correction.

CONTRIBUTING.md claimed the test suite needs "neither a cluster nor a database". That is true for the RBAC, scoring and remediation tests, but not for test_smoke.py — it drives the real FastAPI startup path (create_all plus tenant/admin seeding) through TestClient, so it opens a genuine database connection. Its own docstring said "Requires a reachable Postgres instance" the whole time. The two documents contradicted each other and you followed the wrong one.

This got written off earlier on the grounds that it "passes locally with no Postgres". That was my mistake: this machine runs a Postgres on 127.0.0.1:5432, which happens to be the exact application default in app/database.py. CI passes for the same reason — it runs a postgres:16-alpine service container on 5432. Point DATABASE_URL at a closed port and your failure reproduces immediately:

sqlalchemy.exc.OperationalError: (psycopg2.OperationalError)
connection to server at "127.0.0.1", port 55432 failed: Connection refused

There was a second trap underneath it: make db publishes Postgres on 55432, while the application default is 5432. make test sets DATABASE_URL for you, but the command printed in test_smoke.py's docstring did not — so following that docstring after make db failed against the wrong port, with a working database sitting right there.

Fixed in #149: CONTRIBUTING.md now names the one test that needs a database, shows the exact error so it reads as expected rather than as a broken checkout, documents the port mismatch, and gives a --ignore one-liner for running the other 130 tests with no database at all.

Thank you for reporting it, and sorry it took two of you saying the same thing before it was taken at face value.

@Diyaaa-12

Copy link
Copy Markdown
Contributor Author

Thanks for tracking this down properly and fixing the docs and for the correction.
Glad it's resolved for the next person. The port mismatch between make db (55432) and the app default (5432) is exactly where I got stuck, so the new CONTRIBUTING.md note will save a lot of time.

@rrskris
rrskris merged commit 7c039af into kaaval:main Aug 13, 2026
7 checks passed
rrskris added a commit that referenced this pull request Aug 13, 2026
Adds release-notes entries for the five contributor PRs merged today:
Grype ingestion (#123, @Diyaaa-12), combo-escalation remediation and rule
catalog (#133, @Maqbool61), the combination-takeover fixture (#152,
@Maqbool61), the Postgres-unreachable test skip (#151, @HeaTTap — first
contribution), and the CIS 5.1.6 acceptance check moving into CI (#124,
@donkk11). Also records the fixture-comment correction from #157.

Signed-off-by: Vamshi Krishna Santhapuri <[email protected]>
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.

Implement Grype report ingestion

2 participants