-
Notifications
You must be signed in to change notification settings - Fork 5
refactoring to allow for slack error posting #24
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,55 +1,99 @@ | ||||||||||||||||||||||||||
| #!/bin/bash | ||||||||||||||||||||||||||
| set -o pipefail | ||||||||||||||||||||||||||
| shopt -s nullglob | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| for f in /opt/mft-automations/puppet_enterprise_support*gz /opt/mft-automations/puppet_enterprise_support*gz.gpg /opt/mft-automations/puppet_enterprise_support*tar /opt/mft-automations/puppet_enterprise_support*tar.gz; do | ||||||||||||||||||||||||||
| # Does the file have a 5 digit ticket number after puppet_enterprise_support_ | ||||||||||||||||||||||||||
| has_ticket=$(echo "$f" | grep -Eo -- 'puppet_enterprise_support_[[:digit:]]+_') | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if [[ $(find "$f" -mmin -2 2>/dev/null) ]]; then | ||||||||||||||||||||||||||
| echo "INFO: $f is still downloading (mtime < 2 minutes). Skipping..." | ||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if ! [[ $has_ticket ]]; then | ||||||||||||||||||||||||||
| echo "ERROR: no ticket ID found in $f" | ||||||||||||||||||||||||||
| mv "$f" /opt/mft-automations/err | ||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if [[ "${f##*.}" == 'gpg' ]]; then | ||||||||||||||||||||||||||
| # Decrypt the file to the same location as the source, stripping the .gpg suffix | ||||||||||||||||||||||||||
| # Delete source file if it decrypts ok, otherwise move it to err/ | ||||||||||||||||||||||||||
| if cat /root/.support_gpg | gpg --pinentry-mode loopback --passphrase-fd 0 --batch --yes --output "${f%.*}" --decrypt "$f"; then | ||||||||||||||||||||||||||
| rm -- "$f" | ||||||||||||||||||||||||||
| f="${f%.*}" | ||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||
| echo "ERROR: failed to decrypt $f" | ||||||||||||||||||||||||||
| mv "$f" /opt/mft-automations/err | ||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if ! tar tf "$f" | grep -q -m 1 'metrics\/.*json'; then | ||||||||||||||||||||||||||
| echo "No metrics found in $f. Skipping" | ||||||||||||||||||||||||||
| rm -- "$f" | ||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||
| # --------------------------- | ||||||||||||||||||||||||||
| # Config | ||||||||||||||||||||||||||
| # --------------------------- | ||||||||||||||||||||||||||
| SEND_SLACK=true # set to false to disable Slack posting | ||||||||||||||||||||||||||
| SLACK_WEBHOOK_URL="${SLACK_WEBHOOK_URL:-}" # must come from environment when SEND_SLACK=true | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| # --------------------------- | ||||||||||||||||||||||||||
| # Helpers | ||||||||||||||||||||||||||
| # --------------------------- | ||||||||||||||||||||||||||
| slack_post() { | ||||||||||||||||||||||||||
| # Usage: slack_post "message" | ||||||||||||||||||||||||||
|
Comment on lines
+14
to
+15
|
||||||||||||||||||||||||||
| slack_post() { | |
| # Usage: slack_post "message" | |
| slack_post() { | |
| # Post a message to Slack. | |
| # Usage: slack_post "message" | |
| # Params: | |
| # $1 - message text to send. | |
| # Behavior / return: | |
| # - If SEND_SLACK is not "true" or SLACK_WEBHOOK_URL is empty, the function | |
| # returns success (exit code 0) without sending anything. | |
| # - Network or curl errors are ignored so callers should not rely on this | |
| # function's exit status to detect delivery failures. |
Copilot
AI
Jan 8, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The JSON escaping implementation is incomplete and could lead to malformed JSON. The current implementation only escapes backslashes, double-quotes, and newlines, but does not handle other JSON control characters such as tabs, carriage returns, backspaces, and form feeds. These characters could break the JSON payload if present in error messages.
| # Minimal JSON escaping for double-quotes, plus newlines -> \n | |
| local esc="${msg//\\/\\\\}" | |
| esc="${esc//\"/\\\"}" | |
| esc="${esc//$'\n'/\\n}" | |
| # Minimal JSON escaping for backslashes, double-quotes, and control chars | |
| local esc="${msg//\\/\\\\}" | |
| esc="${esc//\"/\\\"}" | |
| esc="${esc//$'\n'/\\n}" | |
| esc="${esc//$'\r'/\\r}" | |
| esc="${esc//$'\t'/\\t}" | |
| esc="${esc//$'\b'/\\b}" | |
| esc="${esc//$'\f'/\\f}" |
Copilot
AI
Jan 8, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The curl command's error output is suppressed with '2>&1 || true', which means failures to post to Slack will be silently ignored. While this prevents the script from failing when Slack is unavailable, it provides no feedback about posting failures, making it difficult to diagnose connectivity or webhook configuration issues.
| curl -sS -X POST -H 'Content-type: application/json' \ | |
| --data "{\"text\":\"$esc\"}" \ | |
| "$SLACK_WEBHOOK_URL" >/dev/null 2>&1 || true | |
| if ! curl -sS -X POST -H 'Content-type: application/json' \ | |
| --data "{\"text\":\"$esc\"}" \ | |
| "$SLACK_WEBHOOK_URL" >/dev/null; then | |
| echo "WARN: failed to post message to Slack webhook" >&2 | |
| fi |
Copilot
AI
Jan 8, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not all error paths are using the new log() function. The "No metrics found" message at line 75 is informational rather than an error, but other error conditions use log() with "ERROR:" prefix. For consistency, consider whether this message should also be treated as an error or if the current behavior is intentional.
| log "No metrics found in $f. Skipping" | |
| log "INFO: No metrics found in $f. Skipping" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The SLACK_WEBHOOK_URL is read from an environment variable but there's no validation that it's set when SEND_SLACK is true. If SEND_SLACK is true but SLACK_WEBHOOK_URL is not set, slack_post will silently do nothing. Consider adding validation at startup to fail early with a clear error message if SEND_SLACK is true but SLACK_WEBHOOK_URL is empty.