Skip to content

Instantly share code, notes, and snippets.

@epcim
Created June 29, 2022 11:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save epcim/2738c2d95f62eea9e73772eda574cf50 to your computer and use it in GitHub Desktop.
Save epcim/2738c2d95f62eea9e73772eda574cf50 to your computer and use it in GitHub Desktop.
sops seal/unseal functions
seal-sops: ## SOPS Encrypt all secrets path matching [_sec|secret|config|*.secret*]
@find $(PTH) -path "*/_sec/*" -type f -o -path "*/secret/*" -type f -o -path "*/config/*" -name "*.secret*" -type f |\
egrep -v '(\.enc|\.asc|\.sealed|\.matrix)' |\
while read file; do \
./scripts/seal-sops $$file;\
done;
unseal-sops: ## SOPS Decrypt all secrets (suffix: .enc and .enc.yaml)
@find $(PTH) -name "*.enc" -type f -o -name "*.enc.*" -type f |\
while read file; do \
./scripts/unseal-sops $$file;\
done;
#!/bin/bash -e
# sops-seal, encrypt file if modified (adds .enc before(as) its suffix)
sops-seal() {
file=$1
fullname="${file##*/}"
dirname="${file%/*}"
basename="${fullname%.*}"
extension=".${fullname##*.}"
# If the file is in the same directory with the script,
# path likely will not include any directory seperator.
[[ "$dirname" == "$path" ]] && dirname="."
# If the file has no extension, correct the variable accordingly.
[[ "$extension" == ".$basename" ]] && extension=""
# Destination file
dest="${dirname}/${basename}.enc${extension}";
[[ ! -e "$dest" ]] && {
sops -e --output "$dest" "$file";
} || {
# if changed
diff $file <(sops --config ${SOPS_CONFIG:-.sops.yaml} -d "$dest") > /dev/null ||\
{ rm "$dest"; echo " ${dest}"; sops -e --config ${SOPS_CONFIG:-.sops.yaml} --output "$dest" "$file";};
}
git add -f ${dest}
}
sops-seal $@
#!/bin/bash
# sops-unseal, decrypt files (while removing `.enc.` file.enc.suffix)
sops-unseal() {
for file in $(ls $@); do
ex=".${file##*.}";
fp="${file%.enc*}";
#[[ "$ex" == ".$fp" ]] && ex="" # fix, no filename suffix
dest="$fp${ex#.enc}";
echo " ${dest}";
sops -d --config ${SOPS_CONFIG:-.sops.yaml} --output "$dest" "$file"; \
done
}
sops-unseal $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment