Skip to content

Commit 7952baf

Browse files
Hardens mongodb key repository download.
1 parent 4ea66a9 commit 7952baf

1 file changed

Lines changed: 32 additions & 4 deletions

File tree

fixes/install_repositories_and_packages.sh

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,38 @@ PACKAGES="${PACKAGES:+$PACKAGES }$POSTGRESQL_CLIENT"
162162
if [ -z "$MONGODB_VERSION" ]; then
163163
echo "No MongoDB version found for ${CURRENT_OS_CODENAME}, nothing to add."
164164
else
165-
echo "Adding MongoDB Repository" > /dev/stdout
166-
curl -fsSL https://www.mongodb.org/static/pgp/server-${MONGODB_VERSION}.asc | gpg --dearmor -o /usr/share/keyrings/mongodb.gpg
167-
echo "deb [signed-by=/usr/share/keyrings/mongodb.gpg] http://repo.mongodb.org/apt/debian ${CURRENT_OS_CODENAME}/mongodb-org/${MONGODB_VERSION} main" > /etc/apt/sources.list.d/mongodb.list
168-
PACKAGES="${PACKAGES:+$PACKAGES }mongocli"
165+
if [[ $MONGODB_VERSION =~ ^([0-9]+)\.([0-9]+) ]]; then
166+
MAJOR=${BASH_REMATCH[1]}
167+
MINOR=${BASH_REMATCH[2]}
168+
169+
# Loop backwards from the current Minor version to 0
170+
while [ "$MINOR" -ge 0 ]; do
171+
CURRENT_VER="${MAJOR}.${MINOR}"
172+
URL="https://pgp.mongodb.com/server-${CURRENT_VER}.asc"
173+
174+
echo "Checking $URL"
175+
176+
# 1. Try to download to a temp file. 'if' protects 'set -e'.
177+
if curl -fsSL "$URL" -o /tmp/mongodb.asc; then
178+
# 2. Only if download succeeded, process the key
179+
gpg --dearmor -o /usr/share/keyrings/mongodb.gpg < /tmp/mongodb.asc
180+
181+
echo "deb [signed-by=/usr/share/keyrings/mongodb.gpg] http://repo.mongodb.org/apt/debian ${CURRENT_OS_CODENAME}/mongodb-org/${CURRENT_VER} main" > /etc/apt/sources.list.d/mongodb.list
182+
PACKAGES="${PACKAGES:+$PACKAGES }mongocli"
183+
184+
rm /tmp/mongodb.asc
185+
break
186+
fi
187+
188+
echo "Version ${CURRENT_VER} not found, retrying..."
189+
((MINOR--)) || true
190+
done
191+
fi
192+
193+
if [ ! -f /etc/apt/sources.list.d/mongodb.list ]; then
194+
echo "No valid MongoDB repository found."
195+
exit 1
196+
fi
169197
fi
170198

171199
echo "Installing Database Clients: $PACKAGES" > /dev/stdout

0 commit comments

Comments
 (0)