Create concrete classes for O-RAN Nodes
[oam.git] / code / container-analysis / container-analysis.sh
1 #!/bin/bash
2
3 ################################################################################
4 # Copyright 2023 highstreet technologies GmbH
5 #
6 # Licensed under the Apache License, Version 2.0 (the 'License');
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 #     http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an 'AS IS' BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 ################################################################################
18
19 # Excluded images is an array containing the name of the docker images we want to exclude from the analysis.
20 # Please modify it according to your needs.
21
22 # Installing syft
23 # curl -sSfL https://raw.githubusercontent.com/anchore/syft/main/install.sh | sh -s -- -b /usr/local/bin
24
25 # Installing grype
26 # curl -sSfL https://raw.githubusercontent.com/anchore/grype/main/install.sh | sh -s -- -b /usr/local/bin
27
28 SYFT=$(which syft)
29 if [ -z "$SYFT" ]; then
30     echo "Unable to find syft. Please install."
31     exit 1
32 fi
33
34 GRYPE=$(which grype)
35 if [ -z "$GRYPE" ]; then
36     echo "Unable to find grype. Please install."
37     exit 1
38 fi
39
40 mkdir -p out
41
42 excluded_images=()
43
44 image_names=($(docker ps --format '{{.Image}}' | tr ' ' '\n' | sort -u | tr '\n' ' '))
45
46 # avoid doublicates
47 for ele in "${excluded_images[@]}"; do
48  image_names=(${image_names[@]/*${ele}*/})
49 done
50
51 echo "Analysing following images: ${image_names[*]}"
52
53 for image in "${image_names[@]}"; do
54   image_name_no_repo="${image##*/}"
55   echo "Creating SBOM for ${image} in ${image_name_no_repo}.sbom.spdx.json..."
56   ${SYFT} -q ${image} -o spdx-json --file out/${image_name_no_repo}.sbom.spdx.json
57   echo "Creating Vulnerabilities for ${image} in ${image_name_no_repo}.vulnerabilities.vex.json..."
58   ${GRYPE} -q ${image} -o embedded-cyclonedx-vex-json --file out/${image_name_no_repo}.vulnerabilities.vex.json
59 done
60
61 echo "Done!"