my-homelab-configs/scripts/check-plaintext-k8s-secrets

53 lines
1.3 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
is_encrypted_secret_path() {
case "$1" in
*.secret.yaml | *.secret.yml | *.enc.yaml | *.enc.yml)
return 0
;;
*)
return 1
;;
esac
}
looks_like_kubernetes_secret() {
awk '
/^[[:space:]]*kind:[[:space:]]*Secret[[:space:]]*$/ { found = 1 }
END { exit(found ? 0 : 1) }
' "$1"
}
main() {
local file
local failures=0
local -a staged_files=()
while IFS= read -r file; do
[[ -n "${file}" ]] || continue
staged_files+=("${file}")
done < <(
git -C "${REPO_ROOT}" diff --cached --name-only --diff-filter=ACMR -- \
'*.yaml' '*.yml'
)
for file in "${staged_files[@]}"; do
[[ -f "${REPO_ROOT}/${file}" ]] || continue
is_encrypted_secret_path "${file}" && continue
if looks_like_kubernetes_secret "${REPO_ROOT}/${file}"; then
echo "${file}: plaintext Kubernetes Secret manifests must be SOPS encrypted as *.secret.yaml or *.enc.yaml." >&2
failures=$((failures + 1))
fi
done
if ((failures > 0)); then
echo "Plaintext Kubernetes Secret guard failed for ${failures} staged file(s)." >&2
exit 1
fi
}
main "$@"