|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Copyright 2022 The Kubernetes Authors. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +set -o nounset |
| 17 | +set -o errexit |
| 18 | +set -o pipefail |
| 19 | + |
| 20 | +#python script to generate official-cve-feed.json |
| 21 | +python3 fetch-official-cve-feed.py |
| 22 | + |
| 23 | +#function to calculate the hash value of official-cve-feed.json |
| 24 | +calculate_hash(){ |
| 25 | + if command -v shasum >/dev/null 2>&1; then |
| 26 | + cat "$@" | shasum -a 256 | cut -d' ' -f1 |
| 27 | + elif command -v sha256sum >/dev/null 2>&1; then |
| 28 | + cat "$@" | sha256sum | cut -d' ' -f1 |
| 29 | + else |
| 30 | + echo "missing shasum tool" 1>&2 |
| 31 | + exit 1 |
| 32 | + fi |
| 33 | +} |
| 34 | + |
| 35 | +#check if official-cve-feed.json blob exists in the bucket |
| 36 | +set -e |
| 37 | +EXIT_CODE=0 |
| 38 | +gsutil ls gs://k8s-cve-feed/official-cve-feed.json >/dev/null 2>&1 || EXIT_CODE=$? |
| 39 | + |
| 40 | +#fetch the hash value of existing official-cve-feed.json json, if differs then upload the new cve feed data to the existing blob. |
| 41 | +if [[ $EXIT_CODE -eq 1 ]]; then |
| 42 | + gsutil cp official-cve-feed.json gs://k8s-cve-feed |
| 43 | + calculate_hash official-cve-feed.json > cve-feed-hash |
| 44 | + echo "$(<cve-feed-hash )" |
| 45 | + gsutil cp cve-feed-hash gs://k8s-cve-feed |
| 46 | +else |
| 47 | + echo "Downloading the old hash blob from gcs bucket" |
| 48 | + gsutil cp gs://k8s-cve-feed/cve-feed-hash cve-feed-hash |
| 49 | + hash=$(<cve-feed-hash ) |
| 50 | + echo "old hash value: $hash" |
| 51 | + echo "Calculate the new hash value of json feed" |
| 52 | + new_hash=$(calculate_hash official-cve-feed.json) |
| 53 | + echo "new hash value : $new_hash " |
| 54 | + printf "$new_hash" > cve-feed-hash |
| 55 | + |
| 56 | + if [[ $hash == $new_hash ]]; then |
| 57 | + printf "Both the hashes have identical contents" |
| 58 | + else |
| 59 | + printf "Both the hash value differ \n" |
| 60 | + echo "Uploading the new json feed and hash value to gcs bucket \n" |
| 61 | + gsutil cp official-cve-feed.json gs://k8s-cve-feed |
| 62 | + gsutil cp cve-feed-hash gs://k8s-cve-feed/cve-feed-hash |
| 63 | + fi |
| 64 | +fi |
| 65 | + |
| 66 | + |
0 commit comments