Skip to content

Commit 88d2d24

Browse files
[Miniconda] - Auto install vuln packages (devcontainers#1086)
* [Miniconda] - Auto install vuln packages * changes as requested..
1 parent 1459344 commit 88d2d24

File tree

2 files changed

+60
-10
lines changed

2 files changed

+60
-10
lines changed

src/miniconda/.devcontainer/Dockerfile

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
FROM continuumio/miniconda3 as upstream
22

3-
# Temporary: Upgrade python packages due to mentioned CVEs
4-
# They are installed by the base image (continuumio/miniconda3) which does not have the patch.
5-
RUN conda install \
6-
# https://github.com/advisories/GHSA-jjg7-2v4v-x38h
7-
idna==3.7
8-
9-
# Temporary: Upgrade python packages using pip package manager
10-
# RUN python3 -m pip install --upgrade \
11-
# https://github.com/advisories/
12-
# package==version
3+
# Temporary: Upgrade python packages
4+
# COPY ./apply_security_patches.sh /tmp/apply_security_patches.sh
5+
# RUN chmod +x /tmp/apply_security_patches.sh
6+
# RUN /tmp/apply_security_patches.sh
137

148
# Reset and copy updated files with updated privs to keep image size down
159
FROM mcr.microsoft.com/devcontainers/base:1-bullseye
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/bin/bash
2+
3+
# define array of packages for pinning to the patched versions
4+
# vulnerable_packages=( "package1=version1" "package2=version2" "package3=version3" )
5+
vulnerable_packages=( "" )
6+
7+
# Define the number of rows (based on the length of vulnerable_packages)
8+
rows=${#vulnerable_packages[@]}
9+
10+
# Define the number of columns
11+
cols=2
12+
13+
# Define the 2D array
14+
declare -A packages_array
15+
16+
# Fill the 2D array
17+
for ((i=0; i<rows; i++)); do
18+
# Split each element of vulnerable_packages by the '=' sign
19+
IFS='=' read -ra parts <<< "${vulnerable_packages[$i]}"
20+
# Assign the parts to the 2D array
21+
packages_array[$i,0]=${parts[0]}
22+
packages_array[$i,1]=${parts[1]}
23+
done
24+
25+
for ((i=0; i<rows; i++)); do
26+
CURRENT_VERSION=$(pip show "${packages_array[$i,0]}" --disable-pip-version-check | grep '^Version:' | awk '{print $2}')
27+
REQUIRED_VERSION="${packages_array[$i,1]}"
28+
GREATER_VERSION_A=$((echo ${REQUIRED_VERSION}; echo ${CURRENT_VERSION}) | sort -V | tail -1)
29+
# Check if the required_version is greater than current_version
30+
if [[ $CURRENT_VERSION != $GREATER_VERSION_A ]]; then
31+
echo "${packages_array[$i,0]} version v${CURRENT_VERSION} installed by the base image is not greater or equal to the required: v${REQUIRED_VERSION}"
32+
# Check whether conda channel has a greater or equal version available, so install from conda, otherwise use pip package manager
33+
channel_name="anaconda"
34+
CONDA_VERSION=$(conda search --override-channels "${packages_array[$i,0]}" -c "$channel_name" | \
35+
grep -E '^[[:alnum:]]' | \
36+
awk '{print $2}' | \
37+
sort -V | \
38+
uniq | \
39+
tail -n 2 | \
40+
head -n 1)
41+
if [[ -z "$CONDA_VERSION" ]]; then
42+
echo "No version for ${packages_array[$i,0]} found in conda channel."
43+
CONDA_VERSION="0"
44+
fi
45+
GREATER_VERSION_B=$((echo ${REQUIRED_VERSION}; echo ${CONDA_VERSION}) | sort -V | tail -1)
46+
if [[ $CONDA_VERSION == $GREATER_VERSION_B ]]; then
47+
echo -e "Found Version v${CONDA_VERSION} in the Conda channel which is greater than or equal to the required version: v${REQUIRED_VERSION}. \n";
48+
echo "Installing ${packages_array[$i,0]} from source from conda channel for v${REQUIRED_VERSION}..."
49+
conda install "${packages_array[$i,0]}==${CONDA_VERSION}"
50+
elif [[ $REQUIRED_VERSION == $GREATER_VERSION_B ]]; then
51+
echo -e "Required version: v${REQUIRED_VERSION} is greater than the version found in the Conda channel v${CONDA_VERSION}. \n";
52+
echo "Installing ${packages_array[$i,0]} from source from pip package manager for v${REQUIRED_VERSION}..."
53+
python3 -m pip install --upgrade --no-cache-dir "${packages_array[$i,0]}==${REQUIRED_VERSION}"
54+
fi
55+
fi
56+
done

0 commit comments

Comments
 (0)