Skip to content

feat: support unencrypted_regex in SopsSecret metadata - #252

Merged
isindir merged 1 commit into
isindir:masterfrom
bmbferreira:feat/unencrypted-regex-support
Aug 9, 2026
Merged

isindir merged 1 commit into
isindir:masterfrom
bmbferreira:feat/unencrypted-regex-support

Conversation

@bmbferreira

@bmbferreira bmbferreira commented Aug 6, 2026 •

Copy link
Copy Markdown
Contributor

Description

The CRD schema declares encrypted_suffix and encrypted_regex, but not unencrypted_regex. A structural CRD removes the fields which it does not declare, so the operator gets a resource with no crypt rule. sops then tries to decrypt every value, and it stops at the first plain text one:

Error walking tree: Could not decrypt value: Input string isindir.github.com/v1alpha3 does not match sops' data format

This makes it impossible to keep values which are not secret in plain text. Some SopsSecret resources might also hold a log level, a port or a hostname. Today those values need to be encrypted too, so a
reviewer sees only new cipher text in a diff.

unencrypted_regex is an allowlist, so sops still encrypts a new key by default.

Suggested solution

Add the field to SopsMetadata in v1alpha3 and generate the CRD again.

The controller needs no change. The sops library already reads unencrypted_regex from the resource metadata; the field only has to pass through the API server. mac_only_encrypted is in the same structure for the same reason.

The regex must also match apiVersion, kind, metadata, status and name. Kubernetes adds plain text fields such as metadata.uid, and an allowlist encrypts everything it does not match. The README explains this.

Alternative solutions considered

  • encrypted_regex with a list of the secret keys. It works today, but it is a denylist. A new secret goes to git in plain text if somebody forgets to update the regex.
  • unencrypted_comment_regex. Comments are lost before the API server stores the object, so the operator never sees the markers. I tested this, and the decryption failed in the same manner.
  • unencrypted_suffix. sops keeps the suffix in the key, so the child secret gets incorrect key names.

Additional context

make test passes, coverage 78.7%. The new test reads sops.unencrypted_regex back from the API server, in the same manner as the mac_only_encrypted test, so it fails if the schema drops the field.

The chart CRD is a symbolic link to the generated file, so it updates automatically. I did not add the field to v1alpha1 and v1alpha2, which are deprecated, and I did not add the comment regex options, because a user who sets one gets an error which is difficult to diagnose.

AI generated code/design contribution

Yes. An AI assistant found the cause and wrote the code, the test and this description. I reviewed the change, cleaned up the description and ran the tests.

The CRD schema declares encrypted_suffix and encrypted_regex, but it does
not declare unencrypted_regex. The Kubernetes API server removes the fields
which the schema does not declare. The operator then receives a resource
with no crypt rule, and sops tries to decrypt every value. The decryption
stops at the first plain text value with this error:

  Error walking tree: Could not decrypt value: Input string
  isindir.github.com/v1alpha3 does not match sops' data format

This commit adds the unencrypted_regex field to the v1alpha3 SopsMetadata
type and generates the CRD again. The controller needs no change, because
the sops library reads this field from the resource metadata. The field only
needs to pass through the API server. The mac_only_encrypted field is a
precedent for this change.

With this field, the user keeps the values which are not secret in plain
text, and sops encrypts all the other values. A new key is encrypted by
default.

The regex must also match apiVersion, kind, metadata, status and name.
Kubernetes adds plain text fields to the resource, for example metadata.uid.
The README explains this requirement.

Co-authored-by: Cursor <[email protected]>
@bmbferreira
bmbferreira marked this pull request as ready for review August 6, 2026 23:28
@bmbferreira
bmbferreira requested a review from isindir as a code owner August 6, 2026 23:28
@isindir

isindir commented Aug 7, 2026

Copy link
Copy Markdown
Owner

@bmbferreira , I have few considerations / questions :

  • please create an issue which describes the problem - please write use case in your own words and provide more sophisticated examples.
    • I'd assume reviewer is a team member and has the same privs to decrypt as the PR creator.
    • The other option is to decompose configuration of the application into: flags, env variables, config file/-s . This allows to fine tune via strict override rules and not encrypt full configuration file.
    • "encrypted_regex with a list of the secret keys. It works today, but it is a denylist. A new secret goes to git in plain text if somebody forgets to update the regex." - same applies to the opposite, if the unencrypted_regex is constructed incorrectly - it can capture secrets which should be encrypted (for me it is safer to encrypt entire config file in this situation).
  • Have you had real use case, or is it more of AI found artificial use case ? If it was real please describe exact scenario, and how often you experience similar, is it a problem or an inconvenience?

@bmbferreira

Copy link
Copy Markdown
Contributor Author

@bmbferreira , I have few considerations / questions :

* please create an issue which describes the problem - please write use case in your own words and provide more sophisticated examples.
  
  * I'd assume reviewer is a team member and has the same privs to decrypt as the PR creator.
  * The other option is to decompose configuration of the application into: flags, env variables, config file/-s . This allows to fine tune via strict override rules and not encrypt full configuration file.
  * "encrypted_regex with a list of the secret keys. It works today, but it is a denylist. A new secret goes to git in plain text if somebody forgets to update the regex." - same applies to the opposite, if the unencrypted_regex is constructed incorrectly - it can capture secrets which should be encrypted (for me it is safer to encrypt entire config file in this situation).

* Have you had real use case, or is it more of AI found artificial use case ? If it was real please describe exact scenario, and how often you experience similar, is it a problem or an inconvenience?

@isindir Thanks for taking the time to look at this.

On the AI point, yes, the code and the first version of the PR description were AI generated, but I guarantee the motivation is not. This came out of something I have been thinking about for some time.

The concrete scenario: we keep per environment env files (.enc.env) for local dev and CI, and the same variables end up in SopsSecret resources for the clusters. Those files are mixed. Some values are genuinely secret (API keys, DB passwords, service account JSON) and a lot are not (log level, worker concurrency, timeouts, model deployment names, bucket names, the OTLP endpoint). Today all of it is encrypted, so when someone bumps a concurrency value or points a URL somewhere else, the diff is a fresh blob of ciphertext. It is not that a reviewer lacks the keys, they have them, it is that nobody decrypts a file for every config tweak, so those changes go through effectively unreviewed.

How often: something in that category changes most weeks. Honestly it is an inconvenience rather than a problem, nothing breaks. But it is a persistent one, and it quietly weakens review on the config that is safe to read.

On decomposing into a ConfigMap plus a Secret, that is a reasonable design and I did try it. However, IMO not the best devex. Most people adding an env var here are not thinking about Kubernetes internals. Splitting means deciding up front whether the new variable is secret, editing a different file, and wiring another envFrom. One file per environment means adding a variable is one edit in one place either way. Same for the coding agents we use, fewer files to locate and keep in sync. That is really what I am after.

On a badly written unencrypted_regex capturing something that should stay encrypted, you are right that it cuts both ways. My view is that is the general "do not commit secrets" problem rather than something this option introduces, and the layers that already exist for it apply, pre-commit hooks, secret scanning, GitHub Advanced Security, etc. Worth noting the default stays safe, since with an allowlist a newly added key is encrypted unless somebody deliberately adds it to the regex.

One thing I would have preferred is the comment based option (--unencrypted-comment-regex), because a marker sitting next to the value reads far better than a regex in .sops.yaml. I tried that first and it cannot work here. YAML comments are gone by the time the API server stores the object, so the operator never sees the markers and decryption fails on the first plaintext value. That is what pushed me to unencrypted_regex.

Happy to open an issue with this written up properly if you still want it, just say the word. 🙇 Thanks again.

@isindir

isindir commented Aug 9, 2026

Copy link
Copy Markdown
Owner

Approved, however from the example test it is not clear why the feature needed. The unencrypted data blob can easily be added to ConfigMap, which in turn can be mounted to the same pod together with secret. If the usecase is only that straightforward, then I don't see why this feature should be added.

@isindir
isindir merged commit 0520a10 into isindir:master Aug 9, 2026
1 check passed
@bmbferreira

Copy link
Copy Markdown
Contributor Author

@isindir thank you so much for taking the time to review, approve and merge the PR. I understand that this could be easily added to a ConfigMap. However I think it would still be easier for developers to maintain a single file with both encrypted and non-encrypted values and since sops already support this out of the box, I thought it would be a good idea to extend that compatibility to the operator.

Regarding the example tests, I created this PR with more realistic examples 🙇 #254

thank you!

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.

2 participants