23

I am trying to setup clean/smudge filter in git to have automatic encrypting and decrypting of files containing secrets thru ansible-vault command.

Peculiarity of the ansible-vault command is that it is not idempotent (it creates a different binary each time it is invoked on the same data).

I started with the implementation suggested in this blog page. Unfortunately it did not work correctly, as whenever smudge is called (be it a git checkout , or just git status), the secret files looks as modified for git, even if it is not.

So I wondered if git would be comparing the binary he has in the index with the clean filtered current file, and I tried to build on those script like follows:

#!/bin/sh -x
# clean filter, it is invoked with %f

if [ ! -r "$HOME/.vault_password" ]; then
  exit 1
fi

tmp=`mktemp`
cat > $tmp

# get the plain text from the binary in the index
tmphead=`mktemp`
git show HEAD:$1 > $tmphead
contenthead=`echo "embedded" | ansible-vault view $tmphead --vault-password-file=$HOME/.vault_password`
export PAGER=cat
echo -n "$contenthead" | tee $tmphead

# if current and index plain text version differ
if [ "`md5sum $tmp | cut -d' ' -f1`" != "`md5sum $tmphead | cut -d' ' -f1`" ]; then
  tmpcrypt=`mktemp`
  cp $tmp $tmpcrypt
  # generate a new crypted blob
  echo "embedded" | ansible-vault encrypt $tmpcrypt --vault-password-file=$HOME/.vault_password > /dev/null 2>&1
  cat "$tmpcrypt"
else
  # just return the HEAD version
  cat "$tmphead"
fi

rm $tmp $tmphead $tmpcrypt

The difference here is that it tries to compare the current and HEAD versions of the plain text (unencrypted) secret files, and only in case they differ output a new binary blob encrypted with ansible-vault.

Unfortunately, after this change git continues to think the secret file is always modified. Even after git adding the file again, so that the git blob is computed, git thinks the file is different and let the change go into the commit. Note that git diff return empty changes, as it should.

For reference, this is smudge:

#!/bin/sh

if [ ! -r "$HOME/.vault_password" ]; then
  exit 1
fi

tmp=`mktemp`
cat > $tmp

export PAGER='cat'
CONTENT="`echo "embedded" | ansible-vault view "$tmp" --vault-password-file=$HOME/.vault_password 2> /dev/null`"

if echo "$CONTENT" | grep 'ERROR: data is not encrypted' > /dev/null; then
  echo "Looks like one file was commited clear text"
  echo "Please fix this before continuing !"
  exit 1
else
  echo -n "$CONTENT"
fi

rm $tmp

and this is diff:

#!/bin/sh

if [ ! -r "$HOME/.vault_password" ]; then
  exit 1
fi

export PAGER='cat'
CONTENT=`echo "embedded" | ansible-vault view "$1" --vault-password-file=$HOME/.vault_password 2> /dev/null`

if echo "$CONTENT" | grep 'ERROR: data is not encrypted' > /dev/null; then
  cat "$1"
else
  echo "$CONTENT"
fi
4
  • I have updated scripts which behave correctly except when git tries to automerge conflicts on vaults which I'll post shortly Commented Mar 1, 2017 at 10:30
  • 1
    Throwing a bottle at the sea but: could the file be different because of different line endings or different code page ?
    – Tensibai
    Commented Mar 1, 2017 at 13:41
  • I'd try removing the -n from the smudge echo, but that's a guess. No hidden option for git diff telling it to ignore single line endings ?
    – Tensibai
    Commented Mar 1, 2017 at 13:50
  • Yet another idea: github.com/dellis23/ansible-toolkit (I will dig more deeply into this one day)
    – Tensibai
    Commented Mar 10, 2017 at 9:24

1 Answer 1

9

The problem here is caused by the random salt in ansible-vault encryption. You can hack VaultEditor class to pass the salt to it from an argument in ansible-vault. The random salt is generated in lib/ansible/parsing/vault/__init__.py on this line. It is called from lib/ansible/cli/vault.py where you can easily add the argument for fixed salt. If you do change it, please submit an upstream patch to Ansible, I'd love to use it.

This problem is further discussed here on hacker news. And there are other implementations with tools that take fixed salt, namely gitcrypt, transcrypt. Here is also a link to one more implementation using ansible-vault called ansible-vault-tools, but this one has the same salt issue as far as I know.

7
  • If you check the code, I am using a checksum to workaround the variable salt issue, ie. decrypt the HEAD vault in a tmp folder first and compare the checksums of plain text files before generating the new binary blob. That's a bit slow but actually ok. My problem is on merges now; in certain situations it works, in others I get the blob automerged before i can decrypt it and it breaks. Commented Mar 14, 2017 at 8:29
  • If you look into the three examples I have linked, there are some workarounds on the merges as well. And it is being discussed in the hacker news comments too. Commented Mar 14, 2017 at 13:53
  • BTW Merging is tricky. What you need to realize is that in case where you pick all your changes or all changes from upstream during the merge, git would figure that out through hash comparison, which would work if the salt was right. The temp file is not enough on clean/smudge. You would need to do the same on merge and in case of non-conflict merge checkout the correct already encrypted version from git and use that one as opposed to re-encrypting with new random salt. Commented Mar 14, 2017 at 13:58
  • 2
    Since this answer was written, someone did submit a patch to make vault encryption deterministic, but it looks like discussion petered out a while ago.
    – larsks
    Commented Oct 12, 2020 at 18:30
  • 1
    ... the traction the bubbled around a bit, flowed over into a new issue and finally a change made it into 2.1.5 /cc @larsks thx for the reference!
    – hakre
    Commented Feb 13, 2023 at 9:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.