feat(matcher/java): add per-request timeout for Maven SHA1 lookups (#1624)#3414
Open
ChrisJr404 wants to merge 1 commit into
Open
feat(matcher/java): add per-request timeout for Maven SHA1 lookups (#1624)#3414ChrisJr404 wants to merge 1 commit into
ChrisJr404 wants to merge 1 commit into
Conversation
Closes anchore#1624 Adds a new `external-sources.maven.timeout` config field that is wired through to the HTTP client used by the Java matcher's Maven SHA1 upstream search. The default is 10s, and a value of 0 disables the limit (preserving the previous behavior of using `http.DefaultClient` with no timeout). When grype encounters a Java archive whose POM data is missing, it falls back to Maven Central via SHA1 lookup. If Maven Central is unresponsive, the scan would previously hang indefinitely with nothing visible in the default UI. With this change a per-request timeout fires and the scan continues with the remaining matchers. Per @tgerla's note on the issue, total time would be a good starting point — this PR ships a per-request timeout as the smallest correct unit (a hung individual request was the failure mode the issue described). A cumulative time budget across all upstream lookups can build on top of this once another external source lands. Adds two test cases: - `timeout aborts hanging upstream` — boots an httptest server that hangs forever and asserts the matcher returns an error within ~2s when configured with a 100ms timeout. - `zero timeout disables the per-request limit` — asserts the underlying `http.Client.Timeout` is zero when the config is unset. Signed-off-by: Chris (ChrisJr404) <11917633+ChrisJr404@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Closes #1624.
Adds a new
external-sources.maven.timeoutconfig field that is wired through to the HTTP client used by the Java matcher's Maven SHA1 upstream search.The default is
10s, and a value of0disables the limit (preserving the previous behavior ofhttp.DefaultClientwith no timeout).Why this fixes the reported pain
The issue describes the scenario where Maven Central (or any configured
base-url) is unresponsive: grype falls back to a SHA1 lookup, the request hangs indefinitely, and the user sees a stuck scan with no console signal until they enable--debug. With this change, a per-request timeout fires, the lookup returns an error, and the scan continues with the remaining matchers.Per-request vs cumulative time
@tgerla suggested in the issue that "total time would be a good starting point" with cascading global + per-source overrides. This PR ships the smallest correct unit first — a per-request timeout — because a single hung request was the actual failure mode in the report. A cumulative time budget can build on top of this once a second external source lands; right now there is only one (Maven), so a global
abort-afterwould be functionally identical to the per-source one.If the project would prefer a different shape (cumulative-only, or
abort-afterinstead oftimeout), happy to revise — small enough patch.Implementation notes
Timeout time.Durationfield on themavenstruct incmd/grype/cli/options/datasources.go, default10s, mapped throughToJavaMatcherConfig()asMavenTimeout.MavenTimeout time.Durationfield onjava.ExternalSearchConfig.NewJavaMatcherconstructs a dedicated*http.Clientwith the configured timeout when it is non-zero, so the timeout does not leak onto unrelated callers ofhttp.DefaultClient. When the timeout is zero, the existinghttp.DefaultClientis used unchanged.Tests
Two new sub-tests in
TestNewJavaMatcherTimeout:timeout aborts hanging upstream— boots anhttptest.Serverwhose handler blocks on a never-closed channel, configures aMavenTimeoutof 100ms, and asserts thatGetMavenPackageByShareturns an error within ~2s.zero timeout disables the per-request limit— asserts the underlyinghttp.Client.Timeoutis0when noMavenTimeoutis configured (so default behavior is preserved).Test plan
go build ./...cleango test ./grype/matcher/java/ -run TestNewJavaMatcherTimeoutpassesgo test ./grype/matcher/java/ ./cmd/grype/cli/options/— no regressionsgrype --helpand YAML config still load with the new field set, default value populated