Theo Chatzimichos 79c2f8
#!/bin/bash
450fa5
STATUS=0
Theo Chatzimichos 79c2f8
Theo Chatzimichos 79c2f8
# Validate the salt-generated nginx configs
Theo Chatzimichos 79c2f8
Christian Boltz b52340
rpm -qa --qf '%{name}\n' | sort > /tmp/packages-before
Christian Boltz b52340
Theo Chatzimichos 4a1ecb
[[ $(whoami) == 'root' ]] || { echo 'Please run this script as root'; exit 1; }
Theo Chatzimichos 79c2f8
0cb66a
# using a container without systemd, but systemd is needed by service.running. replace it with /usr/bin/true to avoid useless error messages.
450fa5
( cd /usr/bin/ || exit 1 ; ln -sf true systemctl )
Christian Boltz b52340
Theo Chatzimichos f8666c
source bin/get_colors.sh
Theo Chatzimichos f8666c
0cb66a
role="$1"
0cb66a
IDFILE="pillar/id/$HOSTNAME.sls"
Theo Chatzimichos 410475
Theo Chatzimichos 8f3b0f
create_fake_certs() {
Theo Chatzimichos 8f3b0f
    # We are replacing both the cert/key pair because:
Theo Chatzimichos 8f3b0f
    # - the key is encrypted and the CI worker can't decrypt it
Theo Chatzimichos 8f3b0f
    # - the nginx validation command tries to match the pair
Theo Chatzimichos 8f3b0f
450fa5
    PRIVATE_KEYS=( $(grep ssl_certificate_key "pillar/role/$role.sls" | cut -d':' -f2) )
450fa5
    for key in "${PRIVATE_KEYS[@]}"; do
b224dd
        if [[ ! ${key##*.} =~ key|pem ]]; then
b224dd
            echo "pillar/role/$role.sls \"ssl_certificate_key: $key\" should have extension .key or .pem"
Theo Chatzimichos 6517ff
            STATUS=1
Theo Chatzimichos 6517ff
        else
b224dd
            mkdir "$(dirname "$key")"
450fa5
            cp test/fixtures/domain.key "$key"
Theo Chatzimichos 6517ff
        fi
Theo Chatzimichos 8f3b0f
    done
Theo Chatzimichos 8f3b0f
450fa5
    PUBLIC_CERTS=( $(grep "ssl_certificate:" "pillar/role/$role.sls" | cut -d':' -f2) )
450fa5
    for cert in "${PUBLIC_CERTS[@]}"; do
b224dd
        if [[ ! ${cert##*.} =~ crt|pem ]]; then
b224dd
            echo "pillar/role/$role.sls \"ssl_certificate: $cert\" should have extension .crt or .pem"
Theo Chatzimichos 6517ff
            STATUS=1
Theo Chatzimichos 6517ff
        else
b224dd
            mkdir "$(dirname "$cert")"
450fa5
            cp test/fixtures/domain.crt "$cert"
Theo Chatzimichos 6517ff
        fi
Theo Chatzimichos 8f3b0f
    done
Theo Chatzimichos 8f3b0f
}
Theo Chatzimichos 8f3b0f
Christian Boltz 00d792
touch_includes() {
0cb66a
    case "$role" in
Christian Boltz 00d792
        mailman3)
Christian Boltz 00d792
            touch /etc/nginx/mails.rewritemap
Christian Boltz 00d792
            touch /etc/nginx/lists.rewritemap
Christian Boltz 00d792
            touch /etc/nginx/feeds.rewritemap
Christian Boltz 00d792
            touch /etc/nginx/mboxs.rewritemap
Christian Boltz 00d792
            touch /etc/nginx/miscs.rewritemap
Christian Boltz 00d792
            ;;
Christian Boltz 00d792
        pagure)
Christian Boltz 00d792
            touch /etc/nginx/acme-challenge
Christian Boltz 00d792
            mkdir -p /etc/ssl/services/letsencrypt
Christian Boltz 00d792
            cat test/fixtures/domain.{crt,key} > /etc/ssl/services/letsencrypt/code.opensuse.org.with.chain_rsa.pem
Christian Boltz 00d792
            cat test/fixtures/domain.{crt,key} > /etc/ssl/services/letsencrypt/code.opensuse.org.with.chain_ecdsa.pem
Christian Boltz 00d792
            sed '/ ssl_dhparam / d' -i /etc/nginx/ssl-config
Christian Boltz 00d792
            ;;
Christian Boltz 00d792
    esac;
Christian Boltz 00d792
}
Christian Boltz 00d792
0cb66a
rolestatus=0
0cb66a
sls_role="salt/role/${role/./\/}.sls"
0cb66a
out="$role.txt"
0cb66a
echo "START OF $role" > "$out"
0cb66a
echo_INFO "Testing role: $role"
0cb66a
450fa5
printf 'roles:\n- %s' "$role" >> "$IDFILE"
0cb66a
0cb66a
# Reset the grains-retrieved IPs to 127.0.0.1, as `nginx -t` actually tries
0cb66a
# to bind to any configured listen IP
450fa5
sed -i -e "s/{{ ip4_.* }}/127.0.0.1/g" "pillar/role/$role.sls"
0cb66a
0cb66a
if grep -q profile "$sls_role"
0cb66a
then
0cb66a
    #for profile in "$(grep -h '\- profile' $sls_role | yq -o t)" // to-do: add yq to container
450fa5
    for profile in $(grep -h '\- profile' "$sls_role" | sed 's/^\s\+ -//' | tr '\n' ' ')
0cb66a
    do
0cb66a
        if [ ! "$profile" == 'profile.web.server.nginx' ]
58e317
        then
0cb66a
            dir_profile="${profile//./\/}"
0cb66a
            if [ -f "salt/$dir_profile/nginx.sls" ]
0cb66a
            then
0cb66a
                state="$profile.nginx"
0cb66a
            elif [ -f "salt/${dir_profile%/*}/nginx.sls" ]
0cb66a
            then
0cb66a
                state="${profile%.*}.nginx"
0cb66a
            fi
0cb66a
            if [ -n "$state" ]
0cb66a
            then
0cb66a
                echo "Applying $state ..." >> "$out"
0cb66a
                salt-call --local state.apply "$state" >> "$out" || rolestatus=1
0cb66a
                echo >> "$out"
0cb66a
                unset state
0cb66a
                break
0cb66a
            fi
Theo Chatzimichos 79c2f8
        fi
0cb66a
    done
0cb66a
fi
0cb66a
0cb66a
echo 'Applying nginx ...' >> "$out"
0cb66a
salt-call --local state.apply nginx >> "$out" || rolestatus=1
b224dd
mkdir /etc/ssl/services
0cb66a
create_fake_certs
450fa5
touch_includes "$role"
0cb66a
0cb66a
printf '\nTesting configuration ...\n' >> "$out"
0cb66a
mispipe 'nginx -tq' "tee -a $out" || rolestatus=1
0cb66a
0cb66a
printf '\nDumping configuration ...\n' >> "$out"
0cb66a
nginx -T >> "$out"
0cb66a
0cb66a
# make sure all vhost config files are named *.conf (without that suffix, they get ignored)
0cb66a
for file in /etc/nginx/vhosts.d/* ; do
0cb66a
    test "$file" == "/etc/nginx/vhosts.d/*" && continue  # skip loop if no file exists in vhosts.d/
0cb66a
    echo "$file" | grep -q '\.conf$' || {
0cb66a
        echo "ERROR: $file is not named *.conf"
0cb66a
        rolestatus=1
0cb66a
    }
Theo Chatzimichos 79c2f8
done
Theo Chatzimichos 79c2f8
0cb66a
if test $rolestatus = 0; then
0cb66a
    echo_PASSED
0cb66a
else
0cb66a
    echo_FAILED
0cb66a
    head -n1000 /etc/nginx/vhosts.d/*
0cb66a
    echo "### end of /etc/nginx/vhosts.d/* for role $role"
0cb66a
    STATUS=1
0cb66a
fi
0cb66a
echo
0cb66a
echo "END OF $role" >> "$out"
Christian Boltz b52340
0cb66a
rpm -qa --qf '%{name}\n' | sort > /tmp/packages-after
Christian Boltz b52340
0cb66a
diff -U0 /tmp/packages-before /tmp/packages-after || echo '=== The packages listed above were installed by one of the roles. Consider to add them to the container image to speed up this test.'
Christian Boltz b52340
450fa5
exit "$STATUS"
Christian Boltz 00d792
450fa5
# vim:expandtab