feat: warn when picking up .grype.yaml from CWD#3428
Open
ChrisJr404 wants to merge 1 commit intoanchore:mainfrom
Open
feat: warn when picking up .grype.yaml from CWD#3428ChrisJr404 wants to merge 1 commit intoanchore:mainfrom
ChrisJr404 wants to merge 1 commit intoanchore:mainfrom
Conversation
Grype's config search includes the current working directory, so a hidden .grype.yaml (or .grype/config.yaml) in CWD is silently applied when you run grype. That's surprising if you wandered into a directory that happens to contain one, and it's hard to spot when debugging unexpected scan behaviour. Emit a WARN-level log on startup naming the picked-up file when the implicit CWD finder is what produced the config. Stay quiet when the user passed --config / -c or set GRYPE_CONFIG, and stay quiet when the config came from the user's home directory or an XDG location. Closes anchore#3427. Signed-off-by: ChrisJr404 <chris@hacknow.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.
Closes #3427.
Grype's default fangs config search walks the current working directory before
~/.grype.yamland the XDG locations, so a hidden.grype.yaml(or.grype/config.yaml) sitting in CWD is silently applied. @joshbressers flagged this on the issue: "I'm not entirely comfortable having Grype read a hidden file in the CWD without any sort of warning." This change adds the warning.What this does
On startup, if grype is about to pick up a config file from CWD via the implicit finder, it logs:
The warning suppresses itself when the file came from anywhere explicit:
--config/-c <path>(and the--config=path,-c=path,-cpathshort forms)GRYPE_CONFIGenvironment variable~/.grype.yamlor any XDG config locationIt also stays quiet when there's no CWD config to warn about.
Where it lives
A small
cmd/grype/cli/internal/configwarnpackage owns the detection so the logic is unit-testable without the whole config loader stack. The detector uses the same extension list viper supports (yaml,yml,json,toml,properties, etc.) and checks both finder layouts that fangs uses by default:./.grype.<ext>and./.grype/config.<ext>.The hook fires from the existing
WithInitializersblock incmd/grype/cli/cli.go, right after the logger is hoisted into place, so the warning goes through the regular logging pipeline (respects--quiet, log file routing, redaction, etc.).Manual verification
Built locally and verified the four cases:
Subdir form (
.grype/config.yaml) also fires correctly.Tests
cmd/grype/cli/internal/configwarn/configwarn_test.gocovers: no file present, each supported extension, subdir layout, all--config/-c/--config=/-c=/-cpathforms,GRYPE_CONFIGenv, and a trailing--c-with-no-value edge case (treated as not explicit, so the warning still fires).go test ./...passes for the whole tree minus pre-existing docker-daemon-required integration tests in this environment (test/integration,test/cli,grype/pkgimage fixtures), which fail with "could not build docker image" unrelated to this change.go vet ./cmd/...is clean. The pre-existing vet warnings ongrype/db/v5/differ,grype/db/v6/installation, andgrype/versionare present onmainalready.Notes