AstroCom-API/tools/Rotating AstroCom Secret/astrocom_rotate.sh

69 lines
1.5 KiB
Bash

#!/bin/bash
# --- USER CONFIG ---
IAXBLOCK="from-astrocom"
CONF="/etc/asterisk/iax_custom.conf"
# Astrocom API endpoint info
ASTROCOM_URL="https://astrocom.tel/api/v1/user/update" # Shouldn't need to change this!
ASTROCOM_TOKEN=""
# -------------------
# Parse flags
DRYRUN=0
if [[ "$1" == "--dry-run" || "$1" == "-n" ]]; then
DRYRUN=1
fi
# Generate a 32-char alphanumeric secret
NEWSECRET=$(tr -dc 'A-Za-z0-9' </dev/urandom | head -c 32)
# Escape block name for sed search
ESC_BLOCK=$(printf '%s\n' "$IAXBLOCK" | sed 's/[]\/$*.^[]/\\&/g')
# Sed script to modify only the first secret= inside the block
SED_SCRIPT="
/^\[$ESC_BLOCK\]/,/^\[/{
/^\[/!{
s/^secret=.*/secret=$NEWSECRET/
t done
}
}
: done
"
if [[ $DRYRUN -eq 1 ]]; then
echo "=== DRY RUN: Showing updated output only ==="
sed "$SED_SCRIPT" "$CONF"
exit 0
fi
#
# REAL UPDATE EXECUTION
#
echo "Updating secret in file..."
sed -i "$SED_SCRIPT" "$CONF" || { echo "Error updating $CONF"; exit 1; }
echo "New secret: $NEWSECRET"
#
# ASTROCOM API REQUEST
#
echo "Sending updated secret to Astrocom..."
# ----- EDIT THIS TO MATCH YOUR REAL API FORMAT -----
curl -X PATCH "$ASTROCOM_URL" \
-H "Authorization: Bearer $ASTROCOM_TOKEN" \
-H "Content-Type: application/json" \
-d "{\"secret\": \"$NEWSECRET\"}" \
|| echo "Warning: Astrocom API call failed"
# ---------------------------------------------------
#
# RELOAD ASTERISK
#
echo "Reloading Asterisk..."
asterisk -x "core reload"
echo "Update complete."