Theo Chatzimichos 79c2f8
#!/bin/bash
Theo Chatzimichos 79c2f8
Theo Chatzimichos 79c2f8
# Validate the salt-generated nginx configs
Theo Chatzimichos 79c2f8
Theo Chatzimichos 4a1ecb
[[ $(whoami) == 'root' ]] || { echo 'Please run this script as root'; exit 1; }
Theo Chatzimichos 79c2f8
Theo Chatzimichos f8666c
source bin/get_colors.sh
Theo Chatzimichos f8666c
Theo Chatzimichos 79c2f8
reset_nginx() {
Theo Chatzimichos f6d741
    rm -rf /etc/nginx
Theo Chatzimichos f6d741
    cp -a /etc/nginx_orig /etc/nginx
Theo Chatzimichos 4a1ecb
    printf "roles:\n- $role" > /etc/salt/grains
Theo Chatzimichos 79c2f8
}
Theo Chatzimichos 79c2f8
Theo Chatzimichos 410475
reset_ip() {
Theo Chatzimichos 410475
    # Reset the grains-retrieved IPs to 127.0.0.1, as `nginx -t` actually tries
Theo Chatzimichos 410475
    # to bind to any configured listen IP
Theo Chatzimichos 410475
Theo Chatzimichos 410475
    sed -i -e "s/{{ ip4_.* }}/127.0.0.1/g" pillar/role/$role.sls
Theo Chatzimichos 410475
}
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
Theo Chatzimichos 8f3b0f
    PRIVATE_KEYS=( $(grep ssl_certificate_key pillar/role/$role.sls | cut -d':' -f2) )
Theo Chatzimichos 8f3b0f
    for key in ${PRIVATE_KEYS[@]}; do
Theo Chatzimichos 6517ff
        if [[ ${key##*.} != 'key' ]]; then
Theo Chatzimichos 6517ff
            echo "pillar/role/$role.sls \"ssl_certificate_key: $key\" should have extension .key"
Theo Chatzimichos 6517ff
            STATUS=1
Theo Chatzimichos 6517ff
        else
Theo Chatzimichos 4a1ecb
            cp test/fixtures/domain.key $key
Theo Chatzimichos 6517ff
        fi
Theo Chatzimichos 8f3b0f
    done
Theo Chatzimichos 8f3b0f
Theo Chatzimichos 8f3b0f
    PUBLIC_CERTS=( $(grep "ssl_certificate:" pillar/role/$role.sls | cut -d':' -f2) )
Theo Chatzimichos 8f3b0f
    for cert in ${PUBLIC_CERTS[@]}; do
Theo Chatzimichos 6517ff
        if [[ ${cert##*.} != 'crt' ]]; then
Theo Chatzimichos 6517ff
            echo "pillar/role/$role.sls \"ssl_certificate: $cert\" should have extension .crt"
Theo Chatzimichos 6517ff
            STATUS=1
Theo Chatzimichos 6517ff
        else
Theo Chatzimichos 4a1ecb
            cp test/fixtures/domain.crt $cert
Theo Chatzimichos 6517ff
        fi
Theo Chatzimichos 8f3b0f
    done
Theo Chatzimichos 8f3b0f
}
Theo Chatzimichos 8f3b0f
Theo Chatzimichos 7357db
cp -a /etc/nginx /etc/nginx_orig
Theo Chatzimichos 7357db
Theo Chatzimichos 79c2f8
WEB_ROLES=( $(bin/get_roles.py | grep web_) )
Theo Chatzimichos 79c2f8
Theo Chatzimichos 79c2f8
for role in ${WEB_ROLES[@]}; do
Theo Chatzimichos 79c2f8
    if grep nginx salt/role/$role.sls > /dev/null; then
Theo Chatzimichos f8666c
        echo_INFO "Testing role: $role"
Theo Chatzimichos 79c2f8
        reset_nginx
Theo Chatzimichos 410475
        reset_ip
Theo Chatzimichos 4a1ecb
        salt-call --local -l quiet state.apply role.$role > /dev/null
Theo Chatzimichos 8f3b0f
        create_fake_certs
Theo Chatzimichos 79c2f8
        if $(nginx -tq); then
Theo Chatzimichos f8666c
            echo_PASSED
Theo Chatzimichos 79c2f8
        else
Theo Chatzimichos f8666c
            echo_FAILED
Theo Chatzimichos 79c2f8
            STATUS=1
Theo Chatzimichos 79c2f8
        fi
Theo Chatzimichos 7357db
        echo
Theo Chatzimichos 79c2f8
    fi
Theo Chatzimichos 79c2f8
done
Theo Chatzimichos 79c2f8
Theo Chatzimichos 79c2f8
exit $STATUS