1+ #! /bin/bash
2+
3+ vulnerable_packages=( " pydantic=2.5.3" " joblib=1.3.1" " mistune=3.0.1" " werkzeug=3.0.3" " transformers=4.36.0" " pillow=10.3.0" " aiohttp=3.9.4" \
4+ " cryptography=42.0.4" " gitpython=3.1.41" " jupyter-lsp=2.2.2" " idna=3.7" " jinja2=3.1.4" " scrapy=2.11.2" )
5+
6+ # Define the number of rows (based on the length of vulnerable_packages)
7+ rows=${# vulnerable_packages[@]}
8+
9+ # Define the number of columns
10+ cols=2
11+
12+ # Define the 2D array
13+ declare -A packages_array
14+
15+ # Fill the 2D array
16+ for (( i= 0 ; i< rows; i++ )) ; do
17+ # Split each element of vulnerable_packages by the '=' sign
18+ IFS=' =' read -ra parts <<< " ${vulnerable_packages[$i]}"
19+ # Assign the parts to the 2D array
20+ packages_array[$i ,0]=${parts[0]}
21+ packages_array[$i ,1]=${parts[1]}
22+ done
23+
24+ for (( i= 0 ; i< rows; i++ )) ; do
25+ CURRENT_VERSION=$( pip show " ${packages_array[$i,0]} " --disable-pip-version-check | grep ' ^Version:' | awk ' {print $2}' )
26+ REQUIRED_VERSION=" ${packages_array[$i,1]} "
27+ GREATER_VERSION_A=$(( echo ${REQUIRED_VERSION} ; echo ${CURRENT_VERSION} ) | sort - V | tail - 1 )
28+ # Check if the required_version is greater than current_version
29+ if [[ $CURRENT_VERSION != $GREATER_VERSION_A ]]; then
30+ 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} "
31+ # Check whether conda channel has a greater or equal version available, so install from conda, otherwise use pip package manager
32+ channel_name="anaconda"
33+ CONDA_VERSION=$(conda search --override-channels "${packages_array[$i,0]} " - c "$channel_name " | \
34+ grep -E '^[[:alnum:]]' | \
35+ awk '{print $2 }' | \
36+ sort -V | \
37+ uniq | \
38+ tail -n 2 | \
39+ head -n 1 )
40+ if [[ -z "$CONDA_VERSION " ]]; then
41+ echo "No version for ${packages_array[$i,0]} found in conda channel."
42+ CONDA_VERSION="0 "
43+ fi
44+ GREATER_VERSION_B=$((echo ${REQUIRED_VERSION} ; echo ${CONDA_VERSION} ) | sort - V | tail - 1 )
45+ if [[ $CONDA_VERSION == $GREATER_VERSION_B ]]; then
46+ 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";
47+ echo "Installing ${packages_array[$i,0]} from source from conda channel for v${REQUIRED_VERSION} ..."
48+ conda install "${packages_array[$i,0]} == ${CONDA_VERSION} "
49+ elif [[ $REQUIRED_VERSION == $GREATER_VERSION_B ]]; then
50+ echo -e "Required version: v${REQUIRED_VERSION} is greater than the version found in the Conda channel v${CONDA_VERSION} . \n";
51+ echo "Installing ${packages_array[$i,0]} from source from pip package manager for v${REQUIRED_VERSION} ..."
52+ python3 -m pip install --upgrade --no-cache-dir "${packages_array[$i,0]} == ${REQUIRED_VERSION} "
53+ fi
54+ fi
55+ done
0 commit comments