chore: block new plaintext Kubernetes secrets

This commit is contained in:
juvenal.diaz 2026-10-02 08:07:30 -06:00
parent e6c2e10e00
commit a8806e2fb6
2 changed files with 57 additions and 0 deletions

View File

@ -49,6 +49,11 @@ repos:
- repo: local
hooks:
- id: plaintext-k8s-secrets
name: plaintext-k8s-secrets
entry: scripts/check-plaintext-k8s-secrets
language: system
pass_filenames: false
- id: gitleaks
name: gitleaks
entry: gitleaks detect --no-git

View File

@ -0,0 +1,52 @@
#!/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 "$@"