Theo Chatzimichos 40c535
#!/bin/bash
Theo Chatzimichos 40c535
Theo Chatzimichos 40c535
# Script that validates that the pillar/secret/*/*.sls files contain the
Theo Chatzimichos 40c535
# appropriate header, and that none other pillar files contain this header or
Theo Chatzimichos 40c535
# any secrets
Theo Chatzimichos 40c535
ba83c5
set -Ceu
ba83c5
0b8576
HEADER_REGEX='^(#!yaml\|gpg|#!gpg\|yaml|#!jinja\|yaml\|gpg)$'
ba83c5
HEADER_EMPTY='^(# empty)$'
ba83c5
STATUS=0
Theo Chatzimichos 40c535
f7be7b
SECRETS_SLS=( $(find pillar/secrets -name '*.sls' 2> /dev/null) )
f7be7b
if [[ -n "${SECRETS_SLS[0]}" ]];  then
f7be7b
    for secret_sls in "${SECRETS_SLS[@]}"; do
f7be7b
        HEADER_LINE="$(head -n 1 "$secret_sls")"
f7be7b
	if [[ ! "$HEADER_LINE" =~ $HEADER_REGEX && ! ( "$HEADER_LINE" =~ $HEADER_EMPTY && "$(wc -l < "$secret_sls")" == 1 ) ]]; then
ba83c5
            echo "The first line in $secret_sls is not matching \"$HEADER_REGEX\""
Theo Chatzimichos f694fd
            STATUS=1
Theo Chatzimichos f694fd
        fi
Theo Chatzimichos f694fd
    done
Theo Chatzimichos f694fd
fi
Theo Chatzimichos 40c535
f7be7b
# shellcheck disable=SC2044 # looping over find output is reasonable here, since additional if-logic is required
Theo Chatzimichos 40c535
for sls in $(find pillar/ -not -path 'pillar/secrets/*' -name '*.sls'); do
f7be7b
    if grep -Eq "$HEADER_REGEX" "$sls"; then
ba83c5
        echo "$sls matches \"$HEADER_REGEX\", please remove such lines from non-secret pillar files"
Theo Chatzimichos 40c535
        STATUS=1
Theo Chatzimichos 40c535
    fi
f7be7b
    if grep -q "BEGIN GPG MESSAGE" "$sls"; then
Theo Chatzimichos 40c535
        echo "$sls contains secrets. Please move them to pillar/secrets/${sls#*/}"
Theo Chatzimichos 40c535
        STATUS=1
Theo Chatzimichos 40c535
    fi
Theo Chatzimichos 40c535
done
Theo Chatzimichos 40c535
f7be7b
exit "$STATUS"