Skip to content

refactor(api): refactor purl API query code - #2900

Merged
hogo6002 merged 10 commits into
google:masterfrom
hogo6002:fix_purl
Nov 28, 2024
Merged

hogo6002 merged 10 commits into
google:masterfrom
hogo6002:fix_purl

Conversation

@hogo6002

Copy link
Copy Markdown
Contributor

Converted PURL queries to package queries in do_query() to remove unnecessary code. Querying the Datastore directly with PURLs gives the same results as querying by package name, so we can simplify things by treating PURLs like regular package queries after extracting the package/ecosystem/version.

This will also resolve issue #2842 by rewriting the PURL-to-ecosystem logic. For most ecosystems, we can get the name from purl.type, but for Linux distributions, we need to use purl.namespace.

@andrewpollock andrewpollock left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice simplification!

Comment thread osv/purl_helpers.py Resolved

@michaelkedar michaelkedar left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice!

Comment thread osv/purl_helpers.py Resolved
Comment thread osv/purl_helpers.py Resolved

@oliverchang oliverchang 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.

very nice! Is there a way we can do some kind of testing to make sure we're not introducing any regressions on matching behaviour?

i.e. some kind of comprehensive comparison between test (with this deployed) and prod (without this deployed) for specific ecosystems?

Comment thread osv/purl_helpers.py Resolved
@hogo6002

Copy link
Copy Markdown
Contributor Author

very nice! Is there a way we can do some kind of testing to make sure we're not introducing any regressions on matching behaviour?

We have integration tests for the API (LONG_TESTS), which I have manually checked. These tests do not produce any 500 errors. Also, after checking in the test instance, it will be tested by performance testing, and I will monitor for any new 500 errors. I think this change should be safe.

Comment thread gcp/api/server.py Resolved
Comment thread gcp/api/server.py Resolved
Comment thread gcp/api/server.py Resolved
Comment thread osv/purl_helpers.py Resolved
Comment thread osv/purl_helpers.py Resolved
Comment thread osv/purl_helpers.py Resolved
Comment thread osv/purl_helpers.py Resolved
Comment thread gcp/api/server.py Resolved
@hogo6002
hogo6002 requested a review from another-rex November 28, 2024 00:56
Comment thread osv/purl_helpers.py Resolved
@hogo6002 hogo6002 mentioned this pull request Nov 28, 2024
9 of 15 tasks
@hogo6002

Copy link
Copy Markdown
Contributor Author

Merge it now to allow for some bake time on the test instance.

@hogo6002
hogo6002 merged commit 7496fb4 into google:master Nov 28, 2024
hogo6002 added a commit that referenced this pull request Dec 3, 2024
The [OSV spec](https://ossf.github.io/osv-schema/#affectedpackage-field)
requires Alpine packages to have a `:v<RELEASE-NUMBER>` suffix.
Currently, our API has inconsistencies regarding Alpine queries:
- It returns a 400 status code with the error message "Invalid
ecosystem" for all Alpine package/ecosystem queries without a release
number.
- However, for Alpine PURL queries (e.g. 
`pkg:apk/alpine/postgresql14?arch=source`), it returns all matching
vulnerabilities across all releases.

Also we recently switched our PURL query from PURL string matching to
package/ecosystem matching
(#2900). This change causes all
Alpine-related PURL API queries to fail because PURL string doesn't
contain any release info and the PURL spec doesn't define how to include
release numbers.

Since OSV-generated Alpine PURLs (e.g. 
`pkg:apk/alpine/postgresql14?arch=source`) don't contain release
numbers, we should allow Alpine queries without a release number for
consistency. This change would return all matching vulnerabilities
across different releases when no release number is specified.
hogo6002 added a commit that referenced this pull request Dec 5, 2024
We didn't have any PURL ecosystem validation until [PR
2900](#2900) was merged. [PR
2900](#2900) introduced some
[breaking
changes](#2960 (comment))
that affect users querying ecosystems for which we currently lack a PURL
converter, especially those not specified in the PURL specification.

This PR changes the response from a 400 error code to a 200 status code
with an empty response unblock users. A more considered design will be
discussed later with the team and implemented.
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.

5 participants