mirror of
https://github.com/wickedyoda/public-setupfiles.git
synced 2026-11-16 13:31:56 -06:00
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:
1 parent
621c51c1cb
commit
2dc388a6df
1 file changed
+95
-40
@@ -1,60 +1,115 @@
|
||||
#!/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
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y ca-certificates curl gpg
|
||||
# --- Preflight: tools must exist, but we must NOT depend on apt working yet ---
|
||||
echo "==> Checking required tools"
|
||||
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
|
||||
sudo mkdir -p /etc/apt/keyrings /usr/share/keyrings
|
||||
|
||||
echo "==> Fixing InfluxData repo key"
|
||||
sudo mkdir -p /usr/share/keyrings
|
||||
# --- InfluxData ---
|
||||
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
|
||||
# Primary: influxdata-archive.key, Fallback: Ubuntu keyserver
|
||||
curl --silent --location --fail --max-time 10 -o /tmp/influxdata-archive.key \
|
||||
https://repos.influxdata.com/influxdata-archive.key 2>/dev/null || \
|
||||
curl --silent --max-time 10 -o /tmp/influxdata-archive.key \
|
||||
"https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x24C975CBA61A024EE1B631787C3D57159FC2F927" || \
|
||||
{ echo "WARNING: Could not fetch InfluxData key from any source"; }
|
||||
fetch_influx_key() {
|
||||
# Primary: vendor-hosted key. Fallback: keyserver by subkey fingerprint.
|
||||
curl --silent --location --fail --max-time 15 -o /tmp/influxdata-archive.key \
|
||||
https://repos.influxdata.com/influxdata-archive.key 2>/dev/null && return 0
|
||||
curl --silent --location --fail --max-time 15 -o /tmp/influxdata-archive.key \
|
||||
"https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x${INFLUX_FPR_SUBKEY}" 2>/dev/null && return 0
|
||||
return 1
|
||||
}
|
||||
|
||||
# Verify official InfluxData fingerprint before installing key
|
||||
if [ -s /tmp/influxdata-archive.key ]; then
|
||||
gpg --show-keys --with-fingerprint --with-colons /tmp/influxdata-archive.key 2>&1 \
|
||||
| grep -q 'fpr:\+24C975CBA61A024EE1B631787C3D57159FC2F927' || {
|
||||
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
|
||||
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
|
||||
# Fallback: use existing keyring if already present
|
||||
if [ -s /usr/share/keyrings/influxdata-archive.gpg ]; then
|
||||
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
|
||||
# verify_influx_key FILE -> 0 if the primary fingerprint is present
|
||||
verify_influx_key() {
|
||||
gpg --show-keys --with-fingerprint --with-colons "$1" 2>/dev/null \
|
||||
| grep -q "fpr:::::::::${INFLUX_FPR_PRIMARY}:"
|
||||
}
|
||||
|
||||
INSTALL_INFLUX=0
|
||||
if fetch_influx_key && [ -s /tmp/influxdata-archive.key ]; then
|
||||
if verify_influx_key /tmp/influxdata-archive.key; then
|
||||
gpg --dearmor < /tmp/influxdata-archive.key | sudo tee "$KEYRING" >/dev/null
|
||||
INSTALL_INFLUX=1
|
||||
echo " key fetched and fingerprint verified"
|
||||
else
|
||||
# Last resort: trusted=yes (signature verification disabled)
|
||||
echo "deb [trusted=yes] https://repos.influxdata.com/debian stable main" \
|
||||
| sudo tee /etc/apt/sources.list.d/influxdata.list >/dev/null
|
||||
echo " WARNING: fetched key fingerprint mismatch - refusing to install"
|
||||
fi
|
||||
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
|
||||
|
||||
echo "==> Fixing Sury PHP repo key"
|
||||
curl -sSLo /tmp/debsuryorg-archive-keyring.deb https://packages.sury.org/debsuryorg-archive-keyring.deb
|
||||
sudo dpkg -i /tmp/debsuryorg-archive-keyring.deb
|
||||
if [ -s "$KEYRING" ]; then
|
||||
echo "deb [signed-by=${KEYRING}] https://repos.influxdata.com/debian stable main" \
|
||||
| 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" \
|
||||
| sudo tee /etc/apt/sources.list.d/php.list >/dev/null
|
||||
# --- Sury (PHP) ---
|
||||
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"
|
||||
sudo rm -f /var/lib/apt/lists/*InRelease /var/lib/apt/lists/*Release /var/lib/apt/lists/*Packages
|
||||
sudo apt-get clean
|
||||
|
||||
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"
|
||||
sudo dpkg --configure -a
|
||||
@@ -65,4 +120,4 @@ sudo apt-get upgrade -y
|
||||
sudo apt-get full-upgrade -y
|
||||
sudo apt-get autoremove -y
|
||||
|
||||
echo "==> Done"
|
||||
echo "==> Done"
|
||||
Reference in new issue
Block a user