-
Notifications
You must be signed in to change notification settings - Fork 1
/
out
executable file
·214 lines (177 loc) · 6.86 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
#!/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
timestamp="$(jq -n "{version:{timestamp:\"$(date +%s)\"}}")"
disable="$(jq -r '.source.disable' < "${payload}")"
if [[ "$disable" == "true" ]]
then
echo "$timestamp" >&3
exit 0
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)
text_file="$(jq -r '.params.text_file // ""' < "${payload}")"
text="$(jq -r '(.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}")"
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)
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}")"
proxy="$(jq -r '.source.proxy // "null"' < "${payload}")"
proxy_https_tunnel="$(jq -r '.source.proxy_https_tunnel // "false"' < "${payload}")"
# Read the env_file and export it in the current console
env_file="$(jq -r '.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 -E -n -r "s/$search_key_val/export \1=\"\2\"/ p" "$env_file")
fi
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
export TEXT_FILE_CONTENT=""
[[ -n "${text_file}" && ! -f "${text_file}" ]] && text_file=""
[[ -n "${text_file}" && -f "${text_file}" ]] && TEXT_FILE_CONTENT="$(envsubst < "${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
fi
attachments=$(echo "$attachments" | envsubst)
[[ -n "${channel_file}" && -f "${channel_file}" ]] && channels="${channels} $(cat "${channel_file}")"
for channel in ${channels}
do
debug_info=""
metadata=""
body=""
if [[ "$allow_insecure" == "true" ]]
then
CURL_OPTION="${CURL_OPTION} -k"
fi
if [[ "$proxy" != "null" ]]
then
CURL_OPTION="${CURL_OPTION} --proxy ${proxy}"
fi
if [[ "$proxy_https_tunnel" == "true" ]]
then
CURL_OPTION="${CURL_OPTION} --proxytunnel"
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)_}"
fi
text_interpolated=$(echo -n "$text" |envsubst)
if [[ -z "${text_interpolated}" ]]
then
text_interpolated="_(missing notification text)_"
[[ -n "${attachments}" ]] && text_interpolated="null"
else
text_interpolated="$(echo "${text_interpolated}" | 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_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="$(echo "${body}" | jq -c '.')"
else
compact_body="$(echo "${body}" | jq -c 'with_entries(select(.key != "link_names"))')"
fi
echo "$compact_body" > /tmp/compact_body.json
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 -X POST -T /tmp/compact_body.json ${CURL_OPTION} "${webhook_url}"
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"
else
curl -v -X POST -T /tmp/compact_body.json ${CURL_OPTION} "${webhook_url}" | sed -e "s#${url_path}#***WEBHOOK URL REDACTED***#g"
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
{
"metadata": [
{"name": "url", "value": ${redacted_webhook_url}},
{"name": "channel", "value": "${channels}" },
{"name": "username", "value": ${username} },
{"name": "text", "value": ${text_interpolated} },
{"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
echo "$timestamp $metadata $debug_info " | jq -s add >&3