Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail resource if curl to slack webhook fails and general cleanup #97

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ body: {

To build local versions of this, for testing:

$ docker build -t slack-notification-resources
$ docker build -t slack-notification-resources .

Tests will run as part of the build, and be left in the image for
future testing. This adds about 100k to the image.
Expand All @@ -302,7 +302,7 @@ To cut a new officialy release of the


[hub]: https://hub.docker.com/r/cfcommunity/slack-notification-resource
[attach]: https://api.slack.com/docs/message-attachment
[attach]: https://api.slack.com/reference/messaging/attachments
[uidmap]: https://api.slack.com/changelog/2017-09-the-one-about-usernames#mapping
[chans]: https://api.slack.com/docs/message-formatting#linking_to_channels_and_users
[cl2017]: https://api.slack.com/changelog/2017-09-the-one-about-usernames
Expand Down
24 changes: 14 additions & 10 deletions out
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ set -e

cd "${1}"

exec 3>&1
exec 1>&2
exec 3>&1 # make stdout available as fd 3 for the result
exec 1>&2 # redirect all output to stderr for logging
set +x
# for jq
PATH=/usr/local/bin:$PATH
Expand All @@ -25,7 +25,7 @@ fi

webhook_url="$(jq -r '.source.url' < "${payload}")"
allow_insecure="$(jq -r '.source.insecure // "false"' < "${payload}")"
raw_ca_certs=$(jq -r '.source.ca_certs // []' < $payload)
raw_ca_certs=$(jq -r '.source.ca_certs // []' < "${payload}")

text_file="$(jq -r '.params.text_file // ""' < "${payload}")"
text="$(jq -r '(.params.text // "${TEXT_FILE_CONTENT}")' < "${payload}")"
Expand All @@ -37,7 +37,7 @@ link_names="$(jq -r '(.params.link_names // false)' < "${payload}")"
channels="$(jq -r '(.params.channel // null)' < "${payload}")"
channel_file="$(jq -r '(.params.channel_file // null)' < "${payload}")"
attachments_file="$(jq -r '.params.attachments_file // ""' < "${payload}")"
attachments=$(jq -r '(.params.attachments // null)' < $payload)
attachments=$(jq -r '(.params.attachments // null)' < "${payload}")

debug="$(jq -r '.params.debug // "false"' < "${payload}")"
show_metadata="$(jq -r '.params.metadata // "false"' < "${payload}")"
Expand Down Expand Up @@ -90,7 +90,8 @@ do
debug_info=""
metadata=""
body=""

# Start off successful, and set to failure during the run
CURL_RETURN_CODE=0

if [[ "$allow_insecure" == "true" ]]
then
Expand Down Expand Up @@ -164,31 +165,30 @@ EOF
then
echo "Using silent output"
curl -s -X POST -T /tmp/compact_body.json ${CURL_OPTION} "${webhook_url}"
CURL_RETURN_CODE=$?
elif [[ ${redact_hook} == "true" ]]
then
url_path="$(echo ${webhook_url} | sed -e "s/https\{0,1\}:\/\/[^\/]*\(\/[^?&#]*\).*/\1/")"
curl -v -X POST -T /tmp/compact_body.json ${CURL_OPTION} "${webhook_url}" 2>&1 | sed -e "s#${url_path}#***WEBHOOK URL REDACTED***#g"
CURL_RETURN_CODE=${PIPESTATUS[0]}
else
curl -v -X POST -T /tmp/compact_body.json ${CURL_OPTION} "${webhook_url}" | sed -e "s#${url_path}#***WEBHOOK URL REDACTED***#g"
CURL_RETURN_CODE=${PIPESTATUS[0]}
fi
else
text_interplated="$(echo "" | jq -R -s .)"
fi

done

if [[ "$show_metadata" == "true" ]]
then
redacted_webhook_url=$(echo "${webhook_url}" | sed -e 's#/\([^/\.]\{2\}\)[^/.]\{5,\}\([^/.]\{2\}\)#/\1…\2#g' | jq -R .)
escaped_text_file="$(echo $text_file | jq -R -s . )"
if [[ ${redact_hook} == "true" ]]
then
escaped_payload="$(cat $payload | jq -rcM ".source.url = \"***REDACTED***\"" | jq -R -s . )"
else
escaped_payload="$(cat $payload | jq -rcM ".source.url = \"***REDACTED***\"" | jq -R -s . )"
fi

escaped_content="$(echo $TEXT_FILE_CONTENT | jq -R -s . )"
text_file_exists="No" && [[ -n "$text_file" ]] && [[ -f "$text_file" ]] && text_file_exists=Yes
metadata="$(cat <<EOF
{
Expand All @@ -208,7 +208,11 @@ EOF
then
metadata="$( echo ${metadata} | jq -r ".metadata += [{name: \"payload\", value: $escaped_payload}]")"
fi

fi

echo "$timestamp $metadata $debug_info " | jq -s add >&3

if [ "$CURL_RETURN_CODE" -ne 0 ]; then
echo "Sending your message to the slack webhook failed, exiting exceptionally."
exit 1
fi
39 changes: 35 additions & 4 deletions test/all.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

set -e
set -eo pipefail

on_exit() {
exitcode=$?
Expand Down Expand Up @@ -29,13 +29,18 @@ $(cat ${base_dir}/${1}.out)

Output:
EOM

result="$(cd $base_dir && cat ${1}.out | $cmd . 2>&1 | tee /dev/stderr)"
result=$(cd $base_dir && cat ${1}.out | $cmd . 2>&1 | tee /dev/stderr)
echo >&2 ""
echo >&2 "Result:"
echo "$result" # to be passed into jq -e
}

echo_true() {
echo >&2 ""
echo >&2 "Result:"
echo "true"
}

export BUILD_PIPELINE_NAME='my-pipeline'
export BUILD_JOB_NAME='my-job'
export BUILD_NAME='my-build'
Expand All @@ -47,6 +52,32 @@ sample_text="This text came from sample.txt. It could have been generated by a p
missing_text="_(no notification provided)_"

username="concourse"
# Turn off failure on failed command return because we can't exit the script when we EXPECT a failure from a test
set +e

# Run these in a subshell so that the subshell process exits itself. If we don't run in a subshell the exit will exit THIS process
$(test curl_failure)
if [ $? -eq 0 ]; then # Since we EXPECT failure from these tests, assert that they come back with a "bad" non-zero status code.
exit 1
else
echo_true
fi

$(test curl_failure_with_silent)
if [ $? -eq 0 ]; then
exit 1
else
echo_true
fi

$(test curl_failure_without_redact_hook)
if [ $? -eq 0 ]; then
exit 1
else
echo_true
fi

set -e

test combined_text_template_and_file | jq -e "
.webhook_url == $(echo $webhook_url | jq -R .) and
Expand Down Expand Up @@ -203,4 +234,4 @@ test env_file | jq -e "
.body.attachments[1].text == \"<something> - ./path/to/*.jar\" and
( .body.attachments | length == 2 )"

echo -e '\e[32;1m'"All tests passed!"'\e[0m'
echo -e '\e[32;1m'"All tests passed!"'\e[0m'
9 changes: 9 additions & 0 deletions test/curl_failure.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"params": {
"always_notify": "true",
"debug": "false"
},
"source": {
"url": "https://force-failure.url"
}
}
10 changes: 10 additions & 0 deletions test/curl_failure_with_silent.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"params": {
"always_notify": "true",
"debug": "false",
"silent": "true"
},
"source": {
"url": "https://force-failure.url"
}
}
10 changes: 10 additions & 0 deletions test/curl_failure_without_redact_hook.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"params": {
"always_notify": "true",
"debug": "false",
"redact_hook": "false"
},
"source": {
"url": "https://force-failure.url"
}
}