aboutsummaryrefslogtreecommitdiffstats
path: root/security/scripts/check_versions.sh
blob: aadc58e2511a7db62494241331f6a2a362297a22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env bash

usage() {
  cat <<EOF
Usage: $(basename $0) <k8s-namespace> [-r <recommended versions file>]
    -r: recommended versions file
EOF
  exit ${1:-0}
}

if [ "$#" -lt 1 ]; then
  usage 1
fi

K8S_NAMESPACE=$1
VERSIONS=$(mktemp versions_XXXXXX)

### getopts
while :
do
  case $2 in
    -h|--help|help) usage ;;
    -r) RECOMMENDED_VERSIONS_FILE_PATH=$3;shift ;;
    -*) usage 1 ;;
     *) break ;;
  esac
done

get_recommendation() {
  local component="$1"
  local recommendations="${RECOMMENDED_VERSIONS_FILE_PATH:-recommended_versions.yaml}"

  yq read "$recommendations" "${component}.recommended_versions" | sed 's/^- //' # removes YAML list prefix
}

check_python3_version() {
  local recommended_versions="${1:-$(get_recommendation python3)}"
  local versions="${2:-$VERSIONS}"

  local unrecommended="$(cat $versions)" # to be filtered out according to recommendations
  for rver in $recommended_versions; do
    unrecommended="$(jq --arg rver "$rver" \
      '.[] | select(.versions.python[]!=$rver) | "\(.pod) \(.container) \(.versions.python[])"' \
      <(echo "$unrecommended") \
      | tr -d '"' \
      | sort -u)"
  done

  echo "$unrecommended"
}

check_java11_version() {
  local recommended_versions="${1:-$(get_recommendation java11)}"
  local versions="${2:-$VERSIONS}"

  local unrecommended="$(cat $versions)" # to be filtered out according to recommendations
  for rver in $recommended_versions; do
    unrecommended="$(jq --arg rver "$rver" \
      '.[] | select(.versions.java[]!=$rver) | "\(.pod) \(.container) \(.versions.java[])"' \
      <(echo "$unrecommended") \
      | tr -d '"' \
      | sort -u)"
  done

  echo "$unrecommended"
}

echo "------------------------------------------------------------------------"
echo "--------------------  ONAP Security tests   ----------------------------"
echo "--------------------  Test components versions in pods   ---------------"
echo "------------------------------------------------------------------------"

code=0

# get the components versions list
python3 /check_versions/k8s_bin_versions_inspector.py \
  -i -c /root/.kube/config -f json \
  -s "metadata.namespace==$K8S_NAMESPACE" > "$VERSIONS"

unrecommended_python="$(check_python3_version)"
unrecommended_java="$(check_java11_version)"

if [ -z "$unrecommended_python" -a -z "$unrecommended_java" ]; then
  echo "Test PASS: All components available in recommended versions only"
else
  code=1
  echo "Test FAIL: Components other than recommended versions found"
  cat <(echo POD CONTAINER PYTHON) <(echo "$unrecommended_python") | column -t -s' '
  cat <(echo POD CONTAINER JAVA) <(echo "$unrecommended_java") | column -t -s' '
fi

exit "$code"