fix: don't panic when redact.Set replaces an existing store - #5144
Open
ankit090701 wants to merge 1 commit into
Open
fix: don't panic when redact.Set replaces an existing store#5144ankit090701 wants to merge 1 commit into
ankit090701 wants to merge 1 commit into
Conversation
redact.Set panicked if called a second time in the same process, e.g. when a library consumer constructs and executes cli.Command(id) more than once: AppClioSetupConfig registers an initializer that calls redact.Set(state.RedactStore) on every clio Execute() call, and each call gets a fresh, independently-constructed RedactStore from clio - so a second invocation always panicked with "replace existing redaction store (probably unintentional)". The sibling bus.Set and log.Set singleton packages already just overwrite on repeated calls without complaint; redact.Set is now consistent with that. Redactions added to a store before it's replaced no longer apply to output produced afterwards, but that's expected: each independent command invocation gets its own store. Fixes anchore#2285 Signed-off-by: ankit090701 <ankitanku090701@gmail.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
redact.Setpanicked if it was called a second time in the same process. This breaks a documented, intended use case: embedding syft as a library and constructing/executingcli.Command(id)more than once (see the linked issue and https://stackoverflow.com/questions/77387892/unable-to-call-anchore-syft-library-command-multiple-times-when-embedded-in-go-a).AppClioSetupConfig(cmd/syft/internal/clio_setup_config.go) registers a clio initializer that callsredact.Set(state.RedactStore)on everyExecute(). Each call tocli.Command(id)gets its own freshly-constructedclio.State, and thus its own newRedactStore- so the second time a consumer executes a command in the same process,redact.Setalways panicked with"replace existing redaction store (probably unintentional)".Looking at the sibling singleton packages in
internal/,bus.Setandlog.Setboth just overwrite their package-level singleton on repeated calls, no questions asked -redact.Setis the only one of the three that panics on replace. This PR brings it in line with its siblings.I confirmed this is still present on current
mainby reproducing the exact stack trace from the issue viacli.Command(id)+Execute()called twice in a row (executing thescansubcommand against a temp dir, twice) - it panics atinternal/redact/redact.go:11through the identical code path described in the issue (clio.(*application).runInitializers->PostLoad->fangs.postLoad/loadConfig->setupCommand-> cobraExecute).Type of change
Checklist
Testing
TestSet_ReplacingExistingStoreDoesNotPanic(internal/redact/redact_test.go), which sets a store, adds a redaction, replaces the store, and confirms: (a) no panic, (b) the new store doesn't inherit redactions from the replaced one, (c) the new store works normally going forward.redact.gochange: the test fails reproducing the exact panic message from this issue.cli.Command(id)twice, executingscan dir:<tmp> -o jsonboth times: panics with the identical stack trace as the issue on unmodified code; passes cleanly with the fix.internal/...andcmd/syft/...suites. The only failures are pre-existing and environmental (this sandbox has nodockerorzipbinaries available, and runs as root which bypasses a permission-denied test ininternal/cache) - unrelated tointernal/redact, which has no relationship to any of those packages.Issue references
Fixes #2285