chore: block new plaintext Kubernetes secrets
This commit is contained in:
parent
e6c2e10e00
commit
a8806e2fb6
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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 "$@"
|
||||
Loading…
Reference in New Issue