Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support using comments to select parts to encrypt #1392

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

felixfontein
Copy link
Contributor

@felixfontein felixfontein commented Dec 27, 2023

This PR adds support to annotate comments with a string (e.g., sops:enc) which can then be matched with a regex. If it matches, the corresponding value (the one which follows the comment) is encrypted while other values are not. (There is also the opposite regex available, to select those values which should not be encrypted.)

This enables the YAML file to have the same structure encrypted and decrypted, without having to add suffixes or manage complex regexes to match keys. See #543 for more discussion.

(This PR currently contains #1389 and will be rebased once that's merged. It is a lot more likely that #1389 will be merged before this PR or any of its variants, so I decided to use that one as a basis.)

This PR continues the work of #974 by rebasing #974 upon #1389 and adding some final touches (see #974 (comment)).

Closes #974.

@felixfontein
Copy link
Contributor Author

@mitar I think the two comments I added address the issues from my review in #974 (review). Can you take a look at the commits?

if ok && tree.Metadata.UnencryptedCommentRegex != "" {
// If an encrypted comment matches tree.Metadata.UnencryptedCommentRegex, decryption will fail
// as the MAC does not match, and the commented value will not be decrypted.
matched, _ := regexp.Match(tree.Metadata.UnencryptedCommentRegex, []byte(in.(string)))
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not use here MatchString?

Didn't we say we will also try to match against EncryptedCommentRegex? So both of them should not match the resulting encrypted string?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I originally implemented this, and then when trying to test it I removed the code again. As opposed to UnencryptedCommentRegex, encrypted comments matching EncryptedCommentRegex aren't a problem since comments are only encrypted/decrypted if they are part of an encrypted subtree that's encrypted due to a comment higher up.

For example, you can encrypt

---
#ENC[AES256_GCM,data:MI3A,iv:c9lHpZCbz3W9ooGuGbmQTcWcXAH7JEL/5JyJdC41dec=,tag:LrQLBEgQayguId5RrNiOCA==,type:comment]
foo:
  bar: barbar #ENC[AES256_GCM,data:MI3A,iv:c9lHpZCbz3W9ooGuGbmQTcWcXAH7JEL/5JyJdC41dec=,tag:LrQLBEgQayguId5RrNiOCA==,type:comment]
# something else
baz:
  bam: bambam #ENC[AES256_GCM,data:MI3A,iv:c9lHpZCbz3W9ooGuGbmQTcWcXAH7JEL/5JyJdC41dec=,tag:LrQLBEgQayguId5RrNiOCA==,type:comment]

with --encrypted-comment-regex ENC and then decrypt it again without any problems.

We could still implement the check to be extra cautious, but it isn't needed (or I'm missing something :) ).

Copy link
Contributor

Choose a reason for hiding this comment

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

OK. I think it is then fine and we can revisit this if anyone finds a realistic case where it is breaking some user flow.

@mitar
Copy link
Contributor

mitar commented Jan 4, 2024

Thanks. I think this looks good as it is already. Thank you for all the work. I made a small comment, but I think it is not critical to do it.

@felixfontein felixfontein marked this pull request as ready for review February 6, 2024 14:03
@felixfontein
Copy link
Contributor Author

Now that #1389 has been merged, this is ready for review! 🎉

if vIsComment {
// If v is a comment, we add it to the slice of active comments.
// This allows us to also encrypt comments themselves by enabling encryption in a prior comment.
commentsStack[len(commentsStack)-1] = append(commentsStack[len(commentsStack)-1], c.Value)
Copy link
Member

Choose a reason for hiding this comment

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

Is there (n)ever a theoretical chance of len(commentsStack) == 0?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That can't happen since in the first line of the function, one element is appended to commentsStack, and during the execution of this function no value is removed from commentsStack.

@Gui13
Copy link

Gui13 commented Apr 7, 2024

This looks good, any idea when this could be merged ?

@felixfontein
Copy link
Contributor Author

It's currently waiting for further reviews / approval by maintainer(s).

@mitar
Copy link
Contributor

mitar commented May 13, 2024

Is there anything to do to push this further?

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.

None yet

4 participants