Skip to content

Commit 8146d5f

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

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,7 @@ Session.vim
3030

3131
# User cluster configs
3232
.kubeconfig
33+
34+
#files generated by cve feed prow job
35+
sig-security-tooling/cve-feed/hack/cve-feed-hash
36+
sig-security-tooling/cve-feed/hack/official-cve-feed.json
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
print(title)
36+
print(len(title))
37+
if len(title) == 1:
38+
cve_id = None
39+
cve['cve_id'] = None
40+
cve['cve_url'] = None
41+
cve['summary'] = title[0]
42+
cve['google_group_url'] = None
43+
else:
44+
cve_id = title[0]
45+
cve['cve_id'] = title[0]
46+
if len(title) == 3:
47+
cve['summary'] = title[2]
48+
else:
49+
cve['summary'] = title[1]
50+
cve['cve_url'] = f"https://www.cvedetails.com/cve-details.php?cve_id={cve_id}"
51+
cve['google_group_url'] = \
52+
f"https://groups.google.com/g/kubernetes-announce/search?q={cve_id}"
53+
cve_list.append(cve)
54+
cves = json.dumps(cve_list, sort_keys=True, indent=4)
55+
print(cves)
56+
# write the final cve list to official_cve_feed.json
57+
with open("official-cve-feed.json", "w") as cvejson:
58+
cvejson.write(cves)

0 commit comments

Comments
 (0)