Repository navigation
feat: support unencrypted_regex in SopsSecret metadata - #252
Conversation
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 , I have few considerations / questions :
|
@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 One thing I would have preferred is the comment based option ( Happy to open an issue with this written up properly if you still want it, just say the word. 🙇 Thanks again. |
|
Approved, however from the example test it is not clear why the feature needed. The unencrypted data blob can easily be added to |
|
@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 Regarding the example tests, I created this PR with more realistic examples 🙇 #254 thank you! |
Description
The CRD schema declares
encrypted_suffixandencrypted_regex, but notunencrypted_regex. A structural CRD removes the fields which it does not declare, so the operator gets a resource with no crypt rule.sopsthen tries to decrypt every value, and it stops at the first plain text one:This makes it impossible to keep values which are not secret in plain text. Some
SopsSecretresources might also hold a log level, a port or a hostname. Today those values need to be encrypted too, so areviewer sees only new cipher text in a diff.
unencrypted_regexis an allowlist, sosopsstill encrypts a new key by default.Suggested solution
Add the field to
SopsMetadatainv1alpha3and generate the CRD again.The controller needs no change. The
sopslibrary already readsunencrypted_regexfrom the resource metadata; the field only has to pass through the API server.mac_only_encryptedis in the same structure for the same reason.The regex must also match
apiVersion,kind,metadata,statusandname. Kubernetes adds plain text fields such asmetadata.uid, and an allowlist encrypts everything it does not match. The README explains this.Alternative solutions considered
encrypted_regexwith 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.sopskeeps the suffix in the key, so the child secret gets incorrect key names.Additional context
make testpasses, coverage 78.7%. The new test readssops.unencrypted_regexback from the API server, in the same manner as themac_only_encryptedtest, 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
v1alpha1andv1alpha2, 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.