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
Theo Chatzimichos f694fd
SECRETS_SLS=$(find pillar/secrets -name '*.sls' 2> /dev/null)
Theo Chatzimichos f694fd
if [[ -n $SECRETS_SLS ]];  then
Theo Chatzimichos f694fd
    for secret_sls in ${SECRETS_SLS[@]}; do
ba83c5
        HEADER_LINE="$(head -n 1 $secret_sls)"
ba83c5
	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
Theo Chatzimichos 40c535
for sls in $(find pillar/ -not -path 'pillar/secrets/*' -name '*.sls'); do
ba83c5
    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
Theo Chatzimichos 40c535
    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
Theo Chatzimichos 40c535
exit $STATUS