-
Notifications
You must be signed in to change notification settings - Fork 0
/
out
executable file
·188 lines (160 loc) · 6.15 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
#!/bin/bash
set -e
cd "${1}"
exec 3>&1
exec 1>&2
set +x
# for jq
PATH=/usr/local/bin:$PATH
payload=$(mktemp /tmp/resource-in.XXXXXX)
cat > "${payload}" <&0
webhook_url="$(jq -r '.source.url' < "${payload}")"
allow_insecure="$(jq -r '.source.insecure // "false"' < "${payload}")"
raw_ca_certs=$(jq -r '.source.ca_certs // []' < $payload)
text_file="$(jq -r '.params.text_file // ""' < "${payload}")"
text="$(jq '(.params.text // "${TEXT_FILE_CONTENT}")' < "${payload}")"
username="$(jq '(.params.username // null)' < "${payload}")"
icon_url="$(jq '(.params.icon_url // null)' < "${payload}")"
icon_emoji="$(jq '(.params.icon_emoji // null)' < "${payload}")"
channels="$(jq -r '(.params.channel // null)' < "${payload}")"
attachments_file="$(jq -r '.params.attachments_file // ""' < "${payload}")"
attachments=$(jq -r '(.params.attachments // null)' < $payload)
debug="$(jq -r '.params.debug // "false"' < "${payload}")"
show_metadata="$(jq -r '.params.metadata // "false"' < "${payload}")"
show_payload="$(jq -r '.params.payload_in_metadata // "false"' < "${payload}")"
silent="$(jq -r '.params.silent // "false"' < "${payload}")"
always_notify="$(jq -r '.params.always_notify // "false"' < "${payload}")"
redact_hook="$(jq -r '.params.redact_hook_url // "true"' < "${payload}")"
cert_count="$(echo $raw_ca_certs | jq -r '. | length')"
if [[ ${cert_count} -gt 0 ]]
then
cert_dir="/usr/local/share/ca-certificates/"
mkdir -p "$cert_dir"
for i in $(seq 0 $(expr "$cert_count" - 1));
do
echo $raw_ca_certs | jq -r .[$i].cert >> "${cert_dir}/ca-cert-$(echo $raw_ca_certs | jq -r .[$i].domain).crt"
done
update-ca-certificates
fi
TEXT_FILE_CONTENT=""
[[ -n "${text_file}" && -f "${text_file}" ]] && TEXT_FILE_CONTENT="$(cat "${text_file}")"
ATTACHMENTS_FILE_CONTENT=""
[[ -n "${attachments_file}" && -f "${attachments_file}" ]] && ATTACHMENTS_FILE_CONTENT="$(cat "${attachments_file}")"
if [[ "${attachments}" == "null" && -n $ATTACHMENTS_FILE_CONTENT ]]; then
attachments=$ATTACHMENTS_FILE_CONTENT
attachments=$(echo "$attachments" | envsubst)
fi
for channel in ${channels}
do
debug_info=""
metadata=""
body=""
if [[ "$allow_insecure" == "true" ]]
then
CURL_OPTION="${CURL_OPTION} -k"
fi
if [[ "$always_notify" == "true" || -n "$TEXT_FILE_CONTENT" || -z "$text_file" ]]
then
if [[ "${attachments}" == "null" ]]
then
TEXT_FILE_CONTENT="${TEXT_FILE_CONTENT:-_(no notification provided)_}"
else
# we have attachments; better interpolate the `text' field...
workfile=$(mktemp /tmp/attach.XXXXXX)
outfile=$(mktemp /tmp/attach.XXXXXX)
cat >$workfile <<<$attachments
for x in $(seq 1 $(jq -r 'length' < $workfile)); do
x=$(( x - 1 ))
if [[ $x != 0 ]]; then
echo -n "," >> $outfile
fi
attachment_text="$(jq -r ".[$x].text // "'""' < $workfile)"
interpolated='{}'
if [[ -n "$attachment_text" ]]; then
interpolated="$(echo $(eval printf "%b" \""$attachment_text"\" | jq -R '{"text":.}'))"
fi
jq -s '.[0] * .[1]' <(jq -r ".[$x]" <$workfile) <(echo "$interpolated") >> $outfile
done
attachments=$((echo '['; cat $outfile; echo ']') | jq -r .)
fi
text="$(eval printf "%b" ${text} )"
if [[ -z "${text}" ]]
then
text="_(missing notification text)_"
[[ -n "${attachments}" ]] && text="null"
else
text="$(echo "${text}" | jq -R -s .)"
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=\"${channel}\" && channel="$(eval "printf ${channel}" | jq -R -s .)"
body="$(cat <<EOF
{
"text": ${text},
"username": ${username},
"icon_url": ${icon_url},
"icon_emoji": ${icon_emoji},
"channel": ${channel},
"attachments": ${attachments}
}
EOF
)"
compact_body="$(echo "${body}" | jq -c '.')"
if [[ "$debug" == "true" ]]
then
debug_info="$(cat <<EOF
{
"webhook_url": "${webhook_url}",
"body": ${body}
}
EOF
)"
elif [[ "$silent" == "true" ]]
then
echo "Using silent output"
curl -s --data-urlencode "payload=${compact_body}" ${CURL_OPTION} "${webhook_url}"
elif [[ ${redact_hook} == "true" ]]
then
url_path="$(echo ${webhook_url} | sed -e "s/https\{0,1\}:\/\/[^\/]*\(\/[^?&#]*\).*/\1/")"
curl -v --data-urlencode "payload=${compact_body}" ${CURL_OPTION} "${webhook_url}" 2>&1 | sed -e "s#${url_path}#***WEBHOOK URL REDACTED***#g"
else
curl -v --data-urlencode "payload=${compact_body}" ${CURL_OPTION} "${webhook_url}" | sed -e "s#${url_path}#***WEBHOOK URL REDACTED***#g"
fi
else
text="$(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
{
"metadata": [
{"name": "url", "value": ${redacted_webhook_url}},
{"name": "channel", "value": "${channels}" },
{"name": "username", "value": ${username} },
{"name": "text", "value": ${text} },
{"name": "text_file", "value": $( echo "$text_file" | jq -R . ) },
{"name": "text_file_exists", "value": $( echo "$text_file_exists" | jq -R . ) },
{"name": "text_file_content", "value": $( echo "$TEXT_FILE_CONTENT" | jq -R -s . ) }
]
}
EOF
)"
if [[ "${show_payload}" == "true" ]]
then
metadata="$( echo ${metadata} | jq -r ".metadata += [{name: \"payload\", value: $escaped_payload}]")"
fi
fi
timestamp="$(jq -n "{version:{timestamp:\"$(date +%s)\"}}")"
echo "$timestamp $metadata $debug_info " | jq -s add >&3