jambonz / jambonz-infrastructure

packer and cloudformation templates for creating EC2-based jambonz deployments
23 stars 29 forks source link

/packer/jambonz-mini/scripts/* missing sanitization about the exit code #26

Open asarubbo opened 1 year ago

asarubbo commented 1 year ago

Hello Dave,

for now I'm looking at install_freeswitch.sh but the concept in the same for the other scripts.

The script basically executes a series of commands, but if one of them fails, the script goes ahead 'blindly'. A workaround for this issue can be define a function called die that looks at the exit code, see the following POC:

#!/bin/bash

die() {
        if [ "${?}" != "0" ]
        then
                test -n "${1}" && echo -ne "\n\n"${1}"\n\n"
                exit 1
        fi
}

echo "ciao" || die "failed to echo ciao"

run_non_existing_command || die "failed to run the run_non_existing_command"

So, while the code is simply, in short, the || die ensures that the exit code was 0, if not, it hangs immediately. If the die has an argument, then the message is printed, so you know at which command there was the failure.

What means in the practice?

For example, install_freeswitch.sh al line 31 (https://github.com/jambonz/jambonz-infrastructure/blob/main/packer/jambonz-mini/scripts/install_freeswitch.sh#L31), tries to cd into /usr/local/src but in the practice the wasn't an mkdir fo that directory, so it will become:

cd /usr/local/src || die "failed to cd into /usr/local/src"

Another example:

git clone https://github.com/signalwire/freeswitch.git -b ${FREESWITCH_VERSION}

will become:

git clone https://github.com/signalwire/freeswitch.git -b ${FREESWITCH_VERSION} || die "failed to clone freeswitch"

So, if when you are cloning freeswitch.git, you can't reach internet or github, you catch the issue.

The idea comes from here: https://devmanual.gentoo.org/ebuild-writing/error-handling/index.html