ฉันกำลังพยายามตั้งค่าตัวกรอง clean / smudgeใน git ให้มีการเข้ารหัสและถอดรหัสไฟล์ที่มีความลับโดยอัตโนมัติผ่านคำสั่งansible-vault
ความผิดปกติของคำสั่ง ansible-vault คือมันไม่ใช่ idempotent (มันสร้างไบนารีที่แตกต่างกันในแต่ละครั้งที่เรียกใช้กับข้อมูลเดียวกัน)
ฉันเริ่มต้นด้วยการดำเนินงานข้อเสนอแนะในหน้าบล็อกนี้ น่าเสียดายที่มันไม่ทำงานอย่างถูกต้องเนื่องจากเมื่อใดก็ตามที่มีการเรียกรอยเปื้อน (ไม่ว่าจะเป็นเช็คเอาต์ git หรือเพียงแค่สถานะ git) ไฟล์ลับดูเหมือนว่าแก้ไขสำหรับ git แม้ว่ามันจะไม่
ดังนั้นฉันสงสัยว่า git จะเปรียบเทียบไบนารี่ที่เขามีในดัชนีกับไฟล์ปัจจุบันที่กรองแล้วหรือไม่และฉันพยายามสร้างสคริปต์เหล่านั้นดังนี้:
#!/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
ความแตกต่างที่นี่ก็คือมันพยายามที่จะเปรียบเทียบไฟล์ลับในปัจจุบันและส่วนหัวของไฟล์ข้อความธรรมดา (ไม่ได้เข้ารหัส) และเฉพาะในกรณีที่พวกมันแตกต่างกันไปส่งออกเป็น binary blob ใหม่ที่เข้ารหัสด้วย ansible-vault
น่าเสียดายที่หลังจากการเปลี่ยนแปลงนี้คอมไพล์ยังคงคิดว่าไฟล์ลับถูกแก้ไขอยู่เสมอ แม้หลังจากgit add
นำไฟล์เข้ามาอีกครั้งเพื่อคำนวณ git blob แล้ว git คิดว่าไฟล์นั้นแตกต่างกันและปล่อยให้การเปลี่ยนแปลงกระทำไป โปรดทราบว่าgit diff
ส่งคืนการเปลี่ยนแปลงที่ว่างเปล่าตามที่ควร
สำหรับการอ้างอิงนี่เป็นรอยเปื้อน:
#!/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
และนี่คือความแตกต่าง:
#!/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
-n
เสียงสะท้อนออกจากรอยเปื้อน แต่นั่นเป็นการคาดเดา ไม่มีตัวเลือกที่ซ่อนสำหรับคอมไพล์บอกให้ละเว้นปลายบรรทัดเดียวหรือไม่