Don't require a matching creation rule when a key is given explicitly - #2262
Open
Pawansingh3889 wants to merge 1 commit into
Open
Conversation
Fixes getsops#1790: `sops --age <recipient> -e file` (or --kms/--pgp/etc.) fails with "error loading config: no matching creation rules found" whenever a .sops.yaml exists somewhere up the directory tree but none of its creation rules match the target file — even though the explicit key should be sufficient on its own. keyGroups() already handles this correctly: it sources keys straight from CLI flags without consulting the config at all when any are given. But three other call sites load the config unconditionally before keyGroups() ever runs, and treat any loading failure as fatal: - main()'s app.Action (~line 1949), for EncryptedRegex/UnencryptedSuffix/etc. - getEncryptConfig() (~line 2134), for the same settings - shamirThreshold() (~line 2576), for ShamirThreshold Every value these three read from config has a safe zero-value default, so none of them actually need the config to exist -- they only need to not crash when it doesn't apply. Export config.ErrNoMatchingCreationRules as a sentinel for that specific failure, and at each of the three sites: if the error is that sentinel and the user supplied at least one key flag directly (the same check keyGroups() already makes, now factored into hasExplicitKeyFlags), treat it as "no applicable config" instead of a hard error. Verified with a full encrypt/decrypt round-trip against the exact repro (a .sops.yaml with rules, none matching, plus an explicit --age): fails on stock sops 3.13.3, succeeds identically patched. go build ./... and go test ./config/... ./cmd/sops/... both pass unchanged. Signed-off-by: Pawansingh3889 <pawansinghkapkoti@gmail.com>
Pawansingh3889
force-pushed
the
fix/encrypt-honors-explicit-key-flags-without-matching-config
branch
from
July 26, 2026 04:39
ca4f4d1 to
5f6c734
Compare
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.
Addresses #1790 —
sops --age <recipient> -e file(or--kms/--pgp/etc.) fails witherror loading config: no matching creation rules foundwhenever a.sops.yamlexists somewhere up the directory tree but none of its creation rules match the target file, even though the explicit key should be sufficient on its own. Reported against 3.9.4; still reproduces on currentmain/ 3.13.3.(Note: one comment on #1790 also mentions
--outputnot being matched against creation rules — that looks like a separate, related problem this PR doesn't touch.)Root cause
keyGroups()already does the right thing: if the caller passes--age/--kms/etc., it sources keys straight from those flags without consulting the config at all. But three other call sites load the config unconditionally, beforekeyGroups()ever runs, and treat any loading failure as fatal:main()'sapp.Action(~line 1949) — loads config purely to readEncryptedRegex/UnencryptedSuffix/etc.getEncryptConfig()(~line 2134) — re-loads it again, for the same settings.shamirThreshold()(~line 2576) — loads it a third time, just forShamirThreshold.Every value these three read from config has a safe zero-value default — none of them actually need the config to exist, they only need to not crash when it doesn't apply. The command dies in one of these three before ever reaching the code (
keyGroups()) that would have worked fine with just the CLI-supplied key.Fix
Export
config.ErrNoMatchingCreationRulesas a sentinel for that specific failure. At each of the three sites: if the error is that sentinel and the user supplied at least one key flag directly (the same conditionkeyGroups()already checks, now factored into a sharedhasExplicitKeyFlags()), treat it as "no applicable config" instead of a hard error. A config that exists and does match is completely unaffected — this only changes the non-matching + explicit-key case.Verification
go build ./...andgo test ./config/... ./cmd/sops/...both pass unchanged.cmd/sopshas no existing unit tests covering this area ([no test files]) — happy to add table-driven tests forhasExplicitKeyFlags/the three call sites if that's wanted before merge, just say the word on shape/location.