|
| 1 | +#! /bin/bash |
| 2 | +# This script is designed to report on container images that use a specific UBI version. It is designed to be used |
| 3 | +# with a policy that creates violations for specific versions of the `redhat-release` package. |
| 4 | + |
| 5 | +# To use this image, set ROX_ENDPOINT to the ACS central instance and set ROX_API_TOKEN |
| 6 | +# to an ACS 'admin' token created. |
| 7 | + |
| 8 | +# e.g. export ROX_ENDPOINT=central-acs-central.apps.cluster1.example.com:443 |
| 9 | +# export ROX_API_TOKEN=eyJhbGciOiJSUzI1NiIsImtpZCI6Imp3dGsw... |
| 10 | +# ./ubi-versions.sh images.csv |
| 11 | + |
| 12 | +set -e |
| 13 | + |
| 14 | +if [[ -z "${ROX_ENDPOINT}" ]]; then |
| 15 | + echo >&2 "ROX_ENDPOINT must be set" |
| 16 | + exit 1 |
| 17 | +fi |
| 18 | + |
| 19 | +if [[ -z "${ROX_API_TOKEN}" ]]; then |
| 20 | + echo >&2 "ROX_API_TOKEN must be set" |
| 21 | + exit 1 |
| 22 | +fi |
| 23 | + |
| 24 | +if [[ -z "$1" ]]; then |
| 25 | + echo >&2 "usage: ubi-versions.sh <output filename>" |
| 26 | + exit 1 |
| 27 | +fi |
| 28 | + |
| 29 | +output_file="$1" |
| 30 | +echo '"Cluster Name", "Namespace", "Deployment", "Image", "UBI version"' > "${output_file}" |
| 31 | + |
| 32 | +function curl_central() { |
| 33 | + curl -sk -H "Authorization: Bearer ${ROX_API_TOKEN}" "https://${ROX_ENDPOINT}/$1" |
| 34 | +} |
| 35 | + |
| 36 | +# Collect all alerts |
| 37 | +res="$(curl_central "v1/alerts?query=Policy%3AUBI%20version%20compliance")" |
| 38 | + |
| 39 | +# Iterate over all deployments and get the full deployment |
| 40 | +for deployment_id in $(echo "${res}" | jq -r .alerts[].deployment.id); do |
| 41 | + deployment_res="$(curl_central "v1/deployments/${deployment_id}")" |
| 42 | + if [[ "$(echo "${deployment_res}" | jq -rc .name)" == null ]]; then |
| 43 | + continue; |
| 44 | + fi |
| 45 | + |
| 46 | + if [[ "$(echo "${deployment_res}" | jq '.containers | length')" == "0" ]]; then |
| 47 | + continue; |
| 48 | + fi |
| 49 | + |
| 50 | + export deployment_name="$(echo "${deployment_res}" | jq -rc .name)" |
| 51 | + export namespace="$(echo "${deployment_res}" | jq -rc .namespace)" |
| 52 | + export clusterName="$(echo "${deployment_res}" | jq -rc .clusterName)" |
| 53 | + |
| 54 | + # Iterate over all images within the deployment and render the CSV Lines |
| 55 | + for image_id in $(echo "${deployment_res}" | jq -r 'select(.containers != null) | .containers[].image.id'); do |
| 56 | + if [[ "${image_id}" != "" ]]; then |
| 57 | + image_res="$(curl_central "v1/images/${image_id}" | jq -rc)" |
| 58 | + if [[ "$(echo "${image_res}" | jq -rc .name)" == null ]]; then |
| 59 | + continue; |
| 60 | + fi |
| 61 | + |
| 62 | + image_name="$(echo "${image_res}" | jq -rc '.name.fullName')" |
| 63 | + export image_name |
| 64 | + |
| 65 | + # find the redhat-release version and format lines |
| 66 | + export ubi_version="$(echo "${image_res}" | jq '.scan.components[] | select(.name=="redhat-release") | .version'| grep -o '[0-9]\.[0-9]\+' | head -1 )" |
| 67 | + echo "${clusterName},${namespace},${deployment_name},${image_name},${ubi_version}" >> "${output_file}" |
| 68 | + fi |
| 69 | + done |
| 70 | + done |
0 commit comments