-
Notifications
You must be signed in to change notification settings - Fork 82
/
out
executable file
·242 lines (208 loc) · 8.88 KB
/
out
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#!/bin/bash
set -e
cd "${1}"
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
payload=$(cat <&0)
timestamp=$(
jq --null-input \
--arg "timestamp" "$(date +%s)" \
'{ version: { timestamp: $timestamp } }'
)
disable="$(jq --raw-output '.source.disable' <<< "${payload}")"
if [[ ${disable} == "true" ]]; then
echo "${timestamp}" >&3
exit 0
fi
webhook_url=$( jq --raw-output '.source.url' <<< "${payload}")
allow_insecure=$(jq --raw-output '.source.insecure // "false"' <<< "${payload}")
raw_ca_certs=$( jq '.source.ca_certs // []' <<< "${payload}")
text_file=$( jq --raw-output '.params.text_file // ""' <<< "${payload}")
text=$( jq --raw-output '.params.text // "${TEXT_FILE_CONTENT}"' <<< "${payload}")
username=$( jq --raw-output '.params.username // null' <<< "${payload}")
icon_url=$( jq --raw-output '.params.icon_url // null' <<< "${payload}")
icon_emoji=$(jq --raw-output '.params.icon_emoji // null' <<< "${payload}")
link_names=$(jq --raw-output '.params.link_names // false' <<< "${payload}")
channels=$( jq --raw-output '.params.channel // null' <<< "${payload}")
channel_file=$( jq --raw-output '.params.channel_file // null' <<< "${payload}")
attachments_file=$(jq --raw-output '.params.attachments_file // ""' <<< "${payload}")
attachments=$( jq --raw-output '.params.attachments // null' <<< "${payload}")
debug=$( jq --raw-output '.params.debug // "false"' <<< "${payload}")
show_metadata=$(jq --raw-output '.params.metadata // "false"' <<< "${payload}")
show_payload=$( jq --raw-output '.params.payload_in_metadata // "false"' <<< "${payload}")
silent=$( jq --raw-output '.params.silent // "false"' <<< "${payload}")
always_notify=$(jq --raw-output '.params.always_notify // "false"' <<< "${payload}")
redact_hook=$( jq --raw-output '.params.redact_hook_url // "true"' <<< "${payload}")
proxy=$( jq --raw-output '.source.proxy // "null"' <<< "${payload}")
proxy_https_tunnel=$(jq --raw-output '.source.proxy_https_tunnel // "false"' <<< "${payload}")
# Read the env_file and export it in the current console
env_file=$(jq --raw-output '.params.env_file // ""' <<< "${payload}")
if [[ -f "${env_file}" ]]; then
# export key=value, when value as space but no quotes
search_key_val='(\w+)=([^\n]+)'
source <(sed -nEe "s/${search_key_val}/export \1=\"\2\"/ p" "${env_file}")
fi
cert_count=$(jq 'length' <<< "${raw_ca_certs}")
if [[ "${cert_count}" -gt 0 ]]; then
cert_dir="/usr/local/share/ca-certificates/"
mkdir -p "${cert_dir}"
for idx in $(seq 0 "$((cert_count - 1))"); do
domain=$(jq --raw-output ".[$idx].domain" <<< "${raw_ca_certs}")
jq --raw-output --arg idx "${idx}" ".[$idx].cert" <<< "${raw_ca_certs}" \
>> "${cert_dir}/ca-cert-${domain}.crt"
done
update-ca-certificates
fi
export TEXT_FILE_CONTENT=""
if [[ -n "${text_file}" && ! -f "${text_file}" ]]; then
text_file=""
fi
if [[ -n "${text_file}" && -f "${text_file}" ]]; then
TEXT_FILE_CONTENT=$(envsubst < "${text_file}")
fi
ATTACHMENTS_FILE_CONTENT=""
if [[ -n "${attachments_file}" && -f "${attachments_file}" ]]; then
ATTACHMENTS_FILE_CONTENT=$(< "${attachments_file}")
fi
if [[ "${attachments}" == "null" && -n "${ATTACHMENTS_FILE_CONTENT}" ]]; then
attachments="${ATTACHMENTS_FILE_CONTENT}"
fi
attachments=$(envsubst <<< "${attachments}")
if [[ -n "${channel_file}" && -f "${channel_file}" ]]; then
channels="${channels} $(< "${channel_file}")"
fi
curl_errors=0
for channel in ${channels}; do
debug_info=""
metadata=""
body=""
declare -a CURL_OPTIONS
if [[ "${allow_insecure}" == "true" ]]; then
CURL_OPTIONS+=("--insecure")
fi
if [[ "${proxy}" != "null" ]]; then
CURL_OPTIONS+=("--proxy" "${proxy}")
fi
if [[ "${proxy_https_tunnel}" == "true" ]]; then
CURL_OPTIONS+=("--proxytunnel")
fi
if [[ "${always_notify}" != "true" && -z "${TEXT_FILE_CONTENT}" && -n "${text_file}" ]]
then
text_interplated=$(jq -R -s . <<< "") # produces '"\n"'
else
if [[ "${attachments}" == "null" ]]; then
TEXT_FILE_CONTENT=${TEXT_FILE_CONTENT:-"_(no notification provided)_"}
fi
text_interpolated=$(envsubst <<< "${text}")
if [[ -z "${text_interpolated}" ]]; then
text_interpolated="_(missing notification text)_"
if [[ -n "${attachments}" ]]; then
text_interpolated="null"
fi
else
text_interpolated=$(jq -R -s . <<< "${text_interpolated}")
fi
[[ "${username}" != "null" ]] && username="$( eval "printf ${username}" | jq -R -s .)"
[[ "${icon_url}" != "null" ]] && icon_url="$( eval "printf ${icon_url}" | jq -R -s .)"
[[ "${icon_emoji}" != "null" ]] && icon_emoji="$(eval "printf ${icon_emoji}" | jq -R -s .)"
[[ "${channel}" != "null" ]] && channel="$( eval "printf \"${channel}\"" | jq -R -s .)"
body=$(cat <<EOF
{
"text": ${text_interpolated},
"username": ${username},
"link_names": ${link_names},
"icon_url": ${icon_url},
"icon_emoji": ${icon_emoji},
"channel": ${channel},
"attachments": ${attachments}
}
EOF
)
# needed for mattermost compatibility as they don't accept link_names
if [[ "${link_names}" == "true" ]]; then
compact_body=$(jq --compact-output '.' <<< "${body}")
else
compact_body=$(
jq --compact-output \
'with_entries(select(.key != "link_names"))' \
<<< "${body}"
)
fi
if [[ "${debug}" == "true" ]]; then
debug_info=$(
jq --null-input \
--arg "webhook_url" "${webhook_url}" \
--argjson "body" "${body}" \
'{
webhook_url: $webhook_url,
body: $body
}'
)
elif [[ "${silent}" == "true" ]]; then
echo "INFO: using silent output"
curl --silent --fail --show-error --location "${CURL_OPTIONS[@]}" \
--request "POST" --url "${webhook_url}" \
--data-raw "${compact_body}"
curl_errors=$(( ${curl_errors} + $? ))
elif [[ "${redact_hook}" == "true" ]]; then
url_path=$(
sed -Ee 's,https?://[^/]*(/[^?&#]*).*,\1,' <<< "${webhook_url}"
)
curl --verbose --silent --fail --show-error --location "${CURL_OPTIONS[@]}" \
--request "POST" --url "${webhook_url}" \
--data-raw "${compact_body}" 2>&1 \
| sed -e "s#${url_path}#***WEBHOOK URL REDACTED***#g"
curl_errors=$(( ${curl_errors} + ${PIPESTATUS[0]} ))
else
curl --verbose --silent --fail --show-error --location "${CURL_OPTIONS[@]}" \
--request "POST" --url "${webhook_url}" \
--data-raw "${compact_body}" \
| sed -e "s#${url_path}#***WEBHOOK URL REDACTED***#g"
curl_errors=$(( ${curl_errors} + ${PIPESTATUS[0]} ))
fi
fi
done
if [[ "${show_metadata}" == "true" ]]; then
redacted_webhook_url=$(
sed -Ee 's|/([^/.]{2})[^/.]{5,}([^/.]{2})|/\1…\2|g' <<< "${webhook_url}"
)
if [[ -n "${text_file}" && -f "${text_file}" ]]; then
text_file_exists="Yes"
else
text_file_exists="No"
fi
metadata=$(cat <<EOF
{
"metadata": [
{"name": "url", "value": $(jq -R . <<< "${redacted_webhook_url}") },
{"name": "channel", "value": "${channels}" },
{"name": "username", "value": ${username} },
{"name": "text", "value": ${text_interpolated} },
{"name": "text_file", "value": $(jq -R . <<< "${text_file}") },
{"name": "text_file_exists", "value": $(jq -R . <<< "${text_file_exists}") },
{"name": "text_file_content", "value": $(jq -R -s . <<< "${TEXT_FILE_CONTENT}") }
]
}
EOF
)
if [[ "${show_payload}" == "true" ]]; then
redacted_payload=$(
jq --compact-output \
'.source.url = "***REDACTED***"' <<< "${payload}")
metadata=$(
jq --arg "payload" "${redacted_payload}" \
'.metadata += [ { name: "payload", value: $payload } ]' \
<<< "${metadata}"
)
fi
fi
jq --slurp 'add' \
<<< "${timestamp} ${metadata} ${debug_info}" \
>&3
if [[ "${curl_errors}" -ne 0 ]]; then
echo "ERROR: sending the message to the Slack webhook has failed"
exit 1
fi