fix: repair APT keys before apt-get update in updates script

The script's first command was `sudo apt-get update` under `set -euo
pipefail`. On a system with a broken InfluxData repo, that command exits
100, so the script aborted at line 7 and never reached the InfluxData key
repair further down. The repair code could not run on exactly the systems
it existed to fix.

- Reorder: fetch and install keys before the first apt-get update.
- Drop -e so a failed update cannot abort key repair.
- Verify the fetched key's fingerprint before installing it; a mismatch is
  no longer installed after a warning.
- Reuse an existing keyring only if it contains the correct fingerprint;
  delete a stale/wrong keyring so it is refetched.
- Remove the [trusted=yes] fallback, which silently disabled signature
  verification. If no verifiable key is available, disable the repo
  instead.
- Detect VERSION_CODENAME for the Sury source instead of hardcoding
  bookworm, so trixie hosts get a valid suite.
- Fail loudly with the apt error if update still fails after repair.
This commit is contained in:
wickedyoda committed 2026-09-29 19:31:47 +00:00
1 parent 621c51c1cb
commit 2dc388a6df
1 file changed
+95 -40
+95 -40
View File
@@ -1,60 +1,115 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -euo pipefail # Repair APT repository keys, then upgrade.
#
# ORDER MATTERS: keys are repaired BEFORE the first `apt-get update`.
# A system with a broken repo makes `apt-get update` exit 100, which under
# `set -e` would abort this script before it could fix anything.
set -uo pipefail
echo "==> Fixing APT repository keys and stale indexes" echo "==> Detecting suite"
# shellcheck disable=SC1091
. /etc/os-release
SUITE="${VERSION_CODENAME:-bookworm}"
echo " suite=$SUITE"
# Make sure basic tools exist # --- Preflight: tools must exist, but we must NOT depend on apt working yet ---
sudo apt-get update echo "==> Checking required tools"
sudo apt-get install -y ca-certificates curl gpg MISSING=""
for tool in curl gpg dpkg; do
command -v "$tool" >/dev/null 2>&1 || MISSING="$MISSING $tool"
done
if [ -n "$MISSING" ]; then
echo " missing:$MISSING - attempting install (best effort)"
# Tolerate failure: a broken repo list must not abort the repair.
apt-get update >/dev/null 2>&1 || true
apt-get install -y ca-certificates curl gnupg >/dev/null 2>&1 || true
fi
for tool in curl gpg dpkg; do
command -v "$tool" >/dev/null 2>&1 || { echo "FATAL: $tool unavailable, cannot repair keys"; exit 1; }
done
# Create keyring directory if missing sudo mkdir -p /etc/apt/keyrings /usr/share/keyrings
sudo mkdir -p /etc/apt/keyrings
echo "==> Fixing InfluxData repo key" # --- InfluxData ---
sudo mkdir -p /usr/share/keyrings echo "==> Repairing InfluxData repo key"
INFLUX_FPR_PRIMARY="24C975CBA61A024EE1B631787C3D57159FC2F927"
INFLUX_FPR_SUBKEY="AC10D7449F343ADCEFDDC2B6DA61C26A0585BD3B"
KEYRING="/usr/share/keyrings/influxdata-archive.gpg"
# The InfluxData Archive Key URL is intermittently down; try multiple sources fetch_influx_key() {
# Primary: influxdata-archive.key, Fallback: Ubuntu keyserver # Primary: vendor-hosted key. Fallback: keyserver by subkey fingerprint.
curl --silent --location --fail --max-time 10 -o /tmp/influxdata-archive.key \ curl --silent --location --fail --max-time 15 -o /tmp/influxdata-archive.key \
https://repos.influxdata.com/influxdata-archive.key 2>/dev/null || \ https://repos.influxdata.com/influxdata-archive.key 2>/dev/null && return 0
curl --silent --max-time 10 -o /tmp/influxdata-archive.key \ curl --silent --location --fail --max-time 15 -o /tmp/influxdata-archive.key \
"https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x24C975CBA61A024EE1B631787C3D57159FC2F927" || \ "https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x${INFLUX_FPR_SUBKEY}" 2>/dev/null && return 0
{ echo "WARNING: Could not fetch InfluxData key from any source"; } return 1
}
# Verify official InfluxData fingerprint before installing key # verify_influx_key FILE -> 0 if the primary fingerprint is present
if [ -s /tmp/influxdata-archive.key ]; then verify_influx_key() {
gpg --show-keys --with-fingerprint --with-colons /tmp/influxdata-archive.key 2>&1 \ gpg --show-keys --with-fingerprint --with-colons "$1" 2>/dev/null \
| grep -q 'fpr:\+24C975CBA61A024EE1B631787C3D57159FC2F927' || { | grep -q "fpr:::::::::${INFLUX_FPR_PRIMARY}:"
echo "WARNING: Key fingerprint mismatch — key may be compromised" }
}
gpg --dearmor < /tmp/influxdata-archive.key | sudo tee /usr/share/keyrings/influxdata-archive.gpg >/dev/null INSTALL_INFLUX=0
echo "deb [signed-by=/usr/share/keyrings/influxdata-archive.gpg] https://repos.influxdata.com/debian stable main" \ if fetch_influx_key && [ -s /tmp/influxdata-archive.key ]; then
| sudo tee /etc/apt/sources.list.d/influxdata.list >/dev/null if verify_influx_key /tmp/influxdata-archive.key; then
else gpg --dearmor < /tmp/influxdata-archive.key | sudo tee "$KEYRING" >/dev/null
# Fallback: use existing keyring if already present INSTALL_INFLUX=1
if [ -s /usr/share/keyrings/influxdata-archive.gpg ]; then echo " key fetched and fingerprint verified"
echo "deb [signed-by=/usr/share/keyrings/influxdata-archive.gpg] https://repos.influxdata.com/debian stable main" \
| sudo tee /etc/apt/sources.list.d/influxdata.list >/dev/null
else else
# Last resort: trusted=yes (signature verification disabled) echo " WARNING: fetched key fingerprint mismatch - refusing to install"
echo "deb [trusted=yes] https://repos.influxdata.com/debian stable main" \ fi
| sudo tee /etc/apt/sources.list.d/influxdata.list >/dev/null else
echo " WARNING: could not fetch InfluxData key from any source"
fi
# Reuse an existing keyring only if it actually contains the right key.
if [ "$INSTALL_INFLUX" -eq 0 ] && [ -s "$KEYRING" ]; then
if gpg --show-keys --with-fingerprint --with-colons "$KEYRING" 2>/dev/null \
| grep -q "fpr:::::::::${INFLUX_FPR_PRIMARY}:"; then
echo " reusing verified existing keyring"
else
echo " existing keyring is STALE/WRONG - removing so it is refetched"
sudo rm -f "$KEYRING"
fi fi
fi fi
echo "==> Fixing Sury PHP repo key" if [ -s "$KEYRING" ]; then
curl -sSLo /tmp/debsuryorg-archive-keyring.deb https://packages.sury.org/debsuryorg-archive-keyring.deb echo "deb [signed-by=${KEYRING}] https://repos.influxdata.com/debian stable main" \
sudo dpkg -i /tmp/debsuryorg-archive-keyring.deb | sudo tee /etc/apt/sources.list.d/influxdata.list >/dev/null
else
# No trustworthy key available. Disable the repo rather than falling back to
# [trusted=yes], which would silently accept unsigned packages.
echo " no verifiable InfluxData key - disabling repo (not using trusted=yes)"
sudo rm -f /etc/apt/sources.list.d/influxdata.list
fi
echo "deb [signed-by=/usr/share/keyrings/debsuryorg-archive-keyring.gpg] https://packages.sury.org/php/ bookworm main" \ # --- Sury (PHP) ---
| sudo tee /etc/apt/sources.list.d/php.list >/dev/null echo "==> Repairing Sury PHP repo key"
if curl -sSLo /tmp/debsuryorg-archive-keyring.deb --max-time 20 \
https://packages.sury.org/debsuryorg-archive-keyring.deb 2>/dev/null \
&& [ -s /tmp/debsuryorg-archive-keyring.deb ]; then
sudo dpkg -i /tmp/debsuryorg-archive-keyring.deb >/dev/null 2>&1 || true
# Match the running suite instead of hardcoding bookworm (trixie hosts exist).
echo "deb [signed-by=/usr/share/keyrings/debsuryorg-archive-keyring.gpg] https://packages.sury.org/php/ ${SUITE} main" \
| sudo tee /etc/apt/sources.list.d/php.list >/dev/null
echo " configured for suite=${SUITE}"
else
echo " WARNING: could not fetch Sury keyring - skipping PHP repo"
fi
# --- Now refresh with the repaired keys in place ---
echo "==> Cleaning stale APT metadata" echo "==> Cleaning stale APT metadata"
sudo rm -f /var/lib/apt/lists/*InRelease /var/lib/apt/lists/*Release /var/lib/apt/lists/*Packages sudo rm -f /var/lib/apt/lists/*InRelease /var/lib/apt/lists/*Release /var/lib/apt/lists/*Packages
sudo apt-get clean sudo apt-get clean
echo "==> Rebuilding package lists" echo "==> Rebuilding package lists"
sudo apt-get update if ! sudo apt-get update; then
echo "ERROR: apt-get update still failing after key repair:"
sudo apt-get update 2>&1 | grep -E '^(Err|W:|E:)' | head -20
exit 1
fi
echo "==> Repairing any partial package state" echo "==> Repairing any partial package state"
sudo dpkg --configure -a sudo dpkg --configure -a
@@ -65,4 +120,4 @@ sudo apt-get upgrade -y
sudo apt-get full-upgrade -y sudo apt-get full-upgrade -y
sudo apt-get autoremove -y sudo apt-get autoremove -y
echo "==> Done" echo "==> Done"