@@ -179,6 +179,83 @@ helm template chart /path/to/chart \
179179```
180180
181181
182+ ## Scan dependency issues
183+
184+ ### Scan images
185+
186+ ``` bash
187+ #! /bin/bash
188+ export images=$( kubectl get pods --all-namespaces -o jsonpath=" {range .items[]}{.spec.containers[].image}{'\n'}{end}" | sort | uniq)
189+ echo " All images found: $images "
190+ echo " "
191+ echo " "
192+ for image in $images ; do
193+ # Run trivy scan and save JSON output
194+ trivy image --format json --output /tmp/result.json --severity HIGH,CRITICAL " $image " > /dev/null 2>&1
195+ # Extract binary targets that have vulnerabilities
196+ binaries=$( jq -r ' .Results[] | select(.Vulnerabilities != null) | .Target' /tmp/result.json)
197+ if [ -n " $binaries " ]; then
198+ echo " - **Image:** $image "
199+ while IFS= read -r binary; do
200+ echo " - **Binary:** $binary "
201+ jq -r --arg target " $binary " '
202+ .Results[] | select(.Target == $target) | .Vulnerabilities[] |
203+ " - **\(.Title)** (\(.Severity)): Affecting `\(.PkgName)` fixed in version `\(.FixedVersion)` (current version is `\(.InstalledVersion)`)."
204+ ' /tmp/result.json
205+ done <<< " $binaries"
206+ echo " "
207+ echo " "
208+ echo " "
209+ fi
210+ done
211+ ```
212+
213+ ### Scan Helm charts
214+
215+ ``` bash
216+ #! /bin/bash
217+ # scan-helm-charts.sh
218+ # This script lists all Helm releases, renders their manifests,
219+ # and then scans each manifest with Trivy for configuration issues.
220+
221+ # Check that jq is installed
222+ if ! command -v jq & > /dev/null; then
223+ echo " jq is required but not installed. Please install jq and rerun."
224+ exit 1
225+ fi
226+
227+ # List all helm releases and extract namespace and release name
228+ echo " Listing Helm releases..."
229+ helm list --all-namespaces -o json | jq -r ' .[] | "\(.namespace) \(.name)"' > helm_releases.txt
230+
231+ # Check if any releases were found
232+ if [ ! -s helm_releases.txt ]; then
233+ echo " No Helm releases found."
234+ exit 0
235+ fi
236+
237+ # Loop through each Helm release and scan its rendered manifest
238+ while IFS=" " read -r namespace release; do
239+ echo " ---------------------------------------------"
240+ echo " Scanning Helm release '$release ' in namespace '$namespace '..."
241+ # Render the Helm chart manifest
242+ manifest_file=" ${release} -manifest.yaml"
243+ helm get manifest " $release " -n " $namespace " > " $manifest_file "
244+ if [ $? -ne 0 ]; then
245+ echo " Failed to get manifest for $release in $namespace . Skipping."
246+ continue
247+ fi
248+ # Scan the manifest with Trivy (configuration scan)
249+ echo " Running Trivy config scan on $manifest_file ..."
250+ trivy config --severity MEDIUM,HIGH,CRITICAL " $manifest_file "
251+ echo " Completed scan for $release ."
252+ done < helm_releases.txt
253+
254+ echo " ---------------------------------------------"
255+ echo " Helm chart scanning complete."
256+ ```
257+
258+
182259
183260## Tips
184261
0 commit comments