在我把秘密文件推到Git之前,我正在尝试使用ansible-地下室来保护我项目中的秘密文件。关于如何在一定程度上设置这些文件,有大量的建议,但我所读到的所有内容都是专门用于区分这些文件的,而不是当您将它们放入其中时如何加密它们--看起来,一开始需要手动保存它们。有什么办法可以在空中跳跃吗?
到目前为止,我拥有的内容是:. .git/config:
[diff "ansible-vault"]
textconv = "ansible-vault view"ansible.cfg:
[defaults]
vault_password_file = .vault.gitattributes:
key.pem diff=ansible-vault merge=binary如果我运行以下命令,这会很好:
ansible-vault encrypt key.pem
git add key.pem
ansible-vault decrypt key.pem
git diff --cached我想要自动化在git中的ansible加密/解密命令。有什么想法吗?
发布于 2022-07-23 10:22:38
我只是对我的个人学习经历(家庭实验室)有着同样的渴望。我发现..git/钩子/预提交会有帮助。我没有自动化的过程,所以这里有你的灵感,我有部分。
.git/挂钩/预提交是一个bash脚本。FILE_PATTERN变量确定我要查找和警告的文件不是加密的。FOr me --这将是所有包含我不想共享的变量的yaml文件。
#!/bin/sh
#
# Ansible Vault Secrets Git Hook
#
# Hook to check if an un-encrypted FILE_PATTERN file is being commited. Useful if secrets
# are retained in ansible vault encrypted file(s) that should never be committed to the repository
# un-encrypted. Contact a repository owner for the ansible vault password.
#
# Put this file in .git/hooks/pre-commit
# set -o xtrace
set -o nounset
FILE_PATTERN="my_secrets_file.yaml\|vars/"
ENCRYPTED_PATTERN="\$ANSIBLE_VAULT"
is_encrypted() {
local file=$1
if ! git show :"$file" | grep --quiet "^${ENCRYPTED_PATTERN}"; then
echo "Located a staged file that should be encrypted:\n> ${file}\n"
echo "Please un-stage this file. If you are adding or updating this file, please encrypt it before staging."
echo "Alternatively, you can git checkout the latest encrypted version of the file before committing.\n"
echo "Remember... Only YOU Can Prevent Secret Leakage."
exit 1
fi
}
echo "Running pre-commit checks..."
git diff --cached --name-only | grep "${FILE_PATTERN}" | while IFS= read -r line; do
is_encrypted "${line}"
done这解决了第一部分,也就是让我知道,我有秘密即将得到承诺,并停止在那里的痛苦。一个简单的"git提交“就会被抓到。
第二部分是对必要的文件进行加密。这只是一次查找相关文件并对这些文件进行加密的问题。我制作这个脚本是为了给这个剧本增添一些方便。“file”变量包含用于查找文件的(相同)模式。
#!/bin/bash
# If number of arguments is 0
if [ $# -eq 0 ]
then
echo "This script will encrypt of decrypt all files containing secrets."
echo "There are all files in vars as well as all secrets.yaml files under each service."
echo "Specify 'decrypt' or 'encrypt' as argument"
echo "If you put the vault password in a password file named .vault_password, the script will not ask for a password."
exit 1
fi
files=`find . -type f -name "me_secrets_file.yaml"`
files="$files vars/*"
password_type=--ask-vault-password
if [ -f ".vault_password" ]
then
if [ `stat -c %a .vault_password` != "600" ]
then
echo ".vault_password file has bad permissions; fixing this to 600"
chmod 600 .vault_password
fi
password_type="--vault-password-file .vault_password"
fi
if [ $1 == "encrypt" ]
then
ansible-vault encrypt $password_type $files
for value in $files; do
echo $value;
done
elif [ $1 == "decrypt" ]
then
ansible-vault decrypt $password_type $files
for value in $files; do
echo $value;
done
else
echo "Wrong argument supplied. Run without arguments to see allowed ones."
fi用作:
./vault.sh {encrypt|decrypt}这解决了第2部分-加密/解密所需的文件。
我确信这两个部分可以结合在一起,这样加密就可以作为未加密的任何文件的预提交钩子的一部分进行。通过在~/.中添加-´.vault_password来进行改进,等等。
预提交脚本最初不是我做的,但我没有保存引用。
https://devops.stackexchange.com/questions/16317
复制相似问题