Skip to content

Commit 3742c5b

Browse files
committed
add cve-feed hack scripts
Signed-off-by: Neha Lohia <[email protected]>
1 parent 1e6f14d commit 3742c5b

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#files generated by cve feed prow job
2+
cve-feed-hash
3+
official-cve-feed.json
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
#install requests module
21+
pip3 install requests
22+
23+
#python script to generate official-cve-feed.json
24+
python3 fetch-official-cve-feed.py
25+
26+
#function to calculate the hash value of official-cve-feed.json
27+
calculate_hash(){
28+
if command -v shasum >/dev/null 2>&1; then
29+
cat "$@" | shasum -a 256 | cut -d' ' -f1
30+
elif command -v sha256sum >/dev/null 2>&1; then
31+
cat "$@" | sha256sum | cut -d' ' -f1
32+
else
33+
echo "missing shasum tool" 1>&2
34+
exit 1
35+
fi
36+
}
37+
38+
#check if official-cve-feed.json blob exists in the bucket
39+
set -e
40+
EXIT_CODE=0
41+
gsutil ls gs://k8s-cve-feed/official-cve-feed.json >/dev/null 2>&1 || EXIT_CODE=$?
42+
43+
#fetch the hash value of existing official-cve-feed.json json, if differs then upload the new cve feed data to the existing blob.
44+
if [[ $EXIT_CODE -eq 1 ]]; then
45+
gsutil cp official-cve-feed.json gs://k8s-cve-feed
46+
calculate_hash official-cve-feed.json > cve-feed-hash
47+
echo "$(<cve-feed-hash )"
48+
gsutil cp cve-feed-hash gs://k8s-cve-feed
49+
else
50+
echo "Downloading the old hash blob from gcs bucket"
51+
gsutil cp gs://k8s-cve-feed/cve-feed-hash cve-feed-hash
52+
hash=$(<cve-feed-hash )
53+
echo "old hash value: $hash"
54+
echo "Calculate the new hash value of json feed"
55+
new_hash=$(calculate_hash official-cve-feed.json)
56+
echo "new hash value : $new_hash "
57+
printf "$new_hash" > cve-feed-hash
58+
59+
if [[ $hash == $new_hash ]]; then
60+
printf "Both the hashes have identical contents"
61+
else
62+
printf "Both the hash value differ \n"
63+
echo "Uploading the new json feed and hash value to gcs bucket \n"
64+
gsutil cp official-cve-feed.json gs://k8s-cve-feed
65+
gsutil cp cve-feed-hash gs://k8s-cve-feed/cve-feed-hash
66+
fi
67+
fi
68+
69+
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env python3
2+
3+
# Copyright 2022 The Kubernetes Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
import json
18+
import requests
19+
20+
url = 'https://api.github.com/search/issues?q=is:issue+label:official-cve-feed+\
21+
state:closed+repo:kubernetes/kubernetes'
22+
headers = {'Accept': 'application/vnd.github.v3+json'}
23+
res = requests.get(url, headers=headers)
24+
cve_arr = res.json()
25+
26+
cve_list = []
27+
28+
for item in cve_arr['items']:
29+
cve = {"issue_url": None, "number": None, "cve_id": None,
30+
"summary": None, "cve_url": None, "google_group_url": None}
31+
cve['issue_url'] = item['html_url']
32+
cve['number'] = item['number']
33+
title = item['title'].replace(" -", ":")
34+
title = title.split(": ")
35+
if len(title) == 1:
36+
cve_id = None
37+
cve['cve_id'] = None
38+
cve['cve_url'] = None
39+
cve['summary'] = title[0]
40+
cve['google_group_url'] = None
41+
else:
42+
cve_id = title[0]
43+
cve['cve_id'] = title[0]
44+
if len(title) == 3:
45+
cve['summary'] = title[2]
46+
else:
47+
cve['summary'] = title[1]
48+
cve['cve_url'] = f"https://www.cvedetails.com/cve-details.php?cve_id={cve_id}"
49+
cve['google_group_url'] = \
50+
f"https://groups.google.com/g/kubernetes-announce/search?q={cve_id}"
51+
cve_list.append(cve)
52+
cves = json.dumps(cve_list, sort_keys=True, indent=4)
53+
print(cves)
54+
# write the final cve list to official_cve_feed.json
55+
with open("official-cve-feed.json", "w") as cvejson:
56+
cvejson.write(cves)

0 commit comments

Comments
 (0)