Skip to content

Ignore unmatched creation rules with inline keys#2150

Open
lawrence3699 wants to merge 1 commit intogetsops:mainfrom
lawrence3699:fix/inline-key-config-fallback
Open

Ignore unmatched creation rules with inline keys#2150
lawrence3699 wants to merge 1 commit intogetsops:mainfrom
lawrence3699:fix/inline-key-config-fallback

Conversation

@lawrence3699
Copy link
Copy Markdown

Fixes #1790.

Before

When .sops.yaml was present but none of its creation_rules matched the target path, sops -e --age ... file still failed with error loading config: no matching creation rules found.

After

If master keys are provided through CLI flags, unmatched creation rules are treated as non-applicable config and encryption falls back to the inline keys.
Commands without inline keys still fail as before, and other config-loading errors still surface unchanged.

Validation

  • go test ./cmd/sops -run 'TestLoadConfig'
  • go test ./config -run 'TestLoadConfig|TestKeyGroupsForFile|TestLoadEmptyConfigFile|TestLoadConfigFileWithEmptyCreationRules'
  • Reproduced sops --age=... -e mysecret.json succeeding with a non-matching .sops.yaml
  • make test still fails in this environment for unrelated age, hcvault, and kms tests (gpg-agent passphrase IPC error and missing Docker socket)

Copilot AI review requested due to automatic review settings April 19, 2026 09:33
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes the behavior where sops would fail to encrypt when a .sops.yaml exists but none of its creation_rules match the target file path, even if master keys are provided via CLI flags (e.g., --age, --kms). With this change, “no matching creation rules” is treated as a non-applicable config only when inline master keys are present, allowing encryption to proceed using the inline keys.

Changes:

  • Introduce a sentinel error (config.ErrNoMatchingCreationRules) for the “no matching creation rules” condition.
  • Update cmd/sops config loading to ignore that specific error when inline master key flags are provided.
  • Add CLI-level tests to validate the new behavior for matching vs non-matching creation rules with/without inline keys.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
config/config.go Adds a typed/sentinel error for unmatched creation rules and returns it from creation rule parsing.
cmd/sops/main.go Ignores unmatched creation rules when inline master key flags are present, allowing fallback to CLI-provided keys.
cmd/sops/main_test.go Adds regression tests covering the new fallback behavior and ensuring the old error remains without inline keys.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread cmd/sops/main.go
Comment on lines +2520 to +2527
func hasInlineMasterKeyFlags(c *cli.Context) bool {
return c.String("kms") != "" ||
c.String("pgp") != "" ||
c.String("gcp-kms") != "" ||
c.String("hckms") != "" ||
c.String("azure-kv") != "" ||
c.String("hc-vault-transit") != "" ||
c.String("age") != ""
Copy link

Copilot AI Apr 19, 2026

Choose a reason for hiding this comment

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

hasInlineMasterKeyFlags duplicates the same inline-key presence check already implemented in keyGroups (the long c.String(...) == "" condition). This creates a maintenance risk where new master-key flags would need to be updated in multiple places. Consider reusing this helper in keyGroups (or deriving both checks from a shared list of flag names) to keep the logic in sync.

Copilot uses AI. Check for mistakes.
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.

error loading config: no matching creation rules found when encryption with inline key parameters (eg: --age --kms)

2 participants