Skip to content

Commit 9e60b54

Browse files
Hardens mongodb key repository download.
1 parent 4ea66a9 commit 9e60b54

1 file changed

Lines changed: 24 additions & 4 deletions

File tree

fixes/install_repositories_and_packages.sh

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,30 @@ 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+
echo "Adding MongoDB Repository v${CURRENT_VER}..."
173+
174+
if curl -fsSL "https://pgp.mongodb.com/server-${CURRENT_VER}.asc" | gpg --dearmor -o /usr/share/keyrings/mongodb.gpg 2>/dev/null; then
175+
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
176+
PACKAGES="${PACKAGES:+$PACKAGES }mongocli"
177+
break # Exit loop once a valid version is found and configured
178+
else
179+
echo "Not found, retrying."
180+
fi
181+
((MINOR--))
182+
done
183+
fi
184+
185+
if [ ! -f /etc/apt/sources.list.d/mongodb.list ]; then
186+
echo "No valid MongoDB repository found."
187+
exit 1
188+
fi
169189
fi
170190

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

0 commit comments

Comments
 (0)