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

Add support for comments in secrets files #139

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

ruuda
Copy link
Contributor

@ruuda ruuda commented Feb 22, 2024

Motivation

I shot myself in my foot because of this line in the readme:

Also: comments are allowed if they start with #.

The comment is about vaultenv.conf, not about the secrets file, so it’s entirely my own fault. Still, what happens when you mistakenly think that secrets files support comments, and you feed Vaultenv this file?

VERSION 2
MOUNT secret
FOO=app#foo

# Comment
BAR=app#bar

Then Vaultenv will parse up to the comment, and silently drop the BAR= definition.

Changes

  1. Add a new test for a file that has comments. It fails before changing the parser.
  2. Extend the tests; before this they only tested that the files can be parsed or not parsed, but they don’t check what the parsed result looks like.
  3. Rewrite the parser to support comments.

There was a Megaparsec parser but I found it difficult to introduce comment support. I think using a parser for this creates more confusion (and danger, as it dropped half my input) than a simple split-on-lines, split-on-=, split-on-#. So I rewrote it without Megaparsec. It is now 100 lines smaller and supports comments.

This makes some breaking changes: the version line must be VERSION 2, other whitespace is no longer allowed. I think that’s acceptable but if you prefer to keep it compatible it’s also not too difficult. Either way if you write VERSION 2 that will fail to parse and not silently result in an unexpected secrets list. Also it is now a bit more lax about what path names and key names it accepts.

This test currently fails because Vaultenv drops the second secret. I
need to fix that, but let's add the test first.
Then when one fails, it will print only the failing example, not the
list with all the results.
I want to make sure that touching the parser doesn't break things.
There is a place for true parsers, but the Megaparsec one here was
fragile. I wanted to add support for comment lines to it but it was not
so clear where, and the current parser had this pretty dangerous failure
mode of ignoring half the input. Instead of trying to deal with that,
for Vaultenv's secrets file format, it is so simple, just splitting on
lines and then splitting on = and # should be fine.
goldenTestContents <- Dir.listDirectory "test/golden"
let goldenTestFiles = map ("test/golden/" <>) goldenTestContents
parseResults <- mapM SecretsFile.readSecretsFile goldenTestFiles
parseResults `shouldSatisfy` (all isRight)
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 changed this to have one it per file, so that when the tests fail, it only prints the failing file, instead of a big list with and “all isRight is false for this big list, good luck finding the offending one!”.

@duijf
Copy link
Contributor

duijf commented Feb 23, 2024

(NB, I didn't read the diff or try out the patch, I am just basing this off of your PR description)

than a simple split-on-lines, split-on-=, split-on-#

If I recall correctly, this was also the approach I took in one of the first versions of Vaultenv. Then at some point we had a production outage due to some whitespace in a secrets file, which caused us to move to the parser-combinator based approach that was a bit more lenient with whitespace.

@FPtje
Copy link
Contributor

FPtje commented Feb 28, 2024

Thanks for the PR @ruuda and thanks for the input @duijf!

The production outage chronicled by @duijf seems to be a good argument to keep the megaparsec library. Along with it being a breaking change could mean that an update of vaultenv could start breaking production, causing a parse error on startup.

Perhaps we can take another look at the challenge of implementing the comment parser in Megaparsec. The rest of the PR looks great.

@fatho
Copy link
Member

fatho commented Feb 28, 2024

and danger, as it dropped half my input

Are we perhaps missing a eof at the end of the parser to make sure it either always consumes the whole document or dies trying?

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