forked from fabric8-analytics/graph-cve-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vulnerability_metadata.py
92 lines (79 loc) · 3.52 KB
/
vulnerability_metadata.py
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 python3
# Copyright © 2021 Red Hat Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Author: Yusuf Zainee <[email protected]>
#
"""Script which generates the vulnerability metadata file for the air gapped solution."""
from datetime import datetime
import json
from helper import Helper
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
SUPPORTED_ECOSYSTEMS = ['maven', 'npm', 'pypi', 'golang']
today = datetime.now().strftime('%d-%m-%Y')
class Vulnerability:
"""Populate minimal viable vuln information."""
def __init__(self):
"""Init method for Vulnerability class."""
self.helper = Helper()
def get_vuln_data(self):
"""Get data from s3 or default."""
# Will be True if we need the entire feed to run. It will recreate vuln file.
if self.helper.is_complete_vuln_mode():
with open('data/default_vulnerability.json', encoding='utf-8') as f:
data = json.load(f)
else:
# in normal mode will add delta info into existing file.
data = self.helper.read_data_from_s3("vulnerability-data", "snyk-feed/")
return data
def save_vuln_json(self, data):
"""Save the json in the vuln file in S3."""
self.helper.store_json_content(data, "snyk-feed/vulnerability-data.json")
def generate_vuln_json(self, vuln_data, pkg):
"""Generate a json from the vulnerability data."""
# Convert list to string.
ver_str = "" + str(vuln_data['affected']).replace(' ', '')\
.replace('[', '').replace(']', '').replace('\'', '')
return {
"snyk_vuln_id": vuln_data['id'],
"package_name": pkg,
"vulnerable_versions": ver_str,
"severity": vuln_data['severity'],
"title": vuln_data['title'],
"url": vuln_data['url'],
"fixed_in": vuln_data['initiallyFixedIn'],
"updated_on": today
}
def extract_info(self, snyk_data, json_data):
"""Extract the vuln info and modify the existing json."""
for eco in SUPPORTED_ECOSYSTEMS:
if eco in snyk_data:
logger.info("Adding data for {} from the parsed snyk feed.".format(eco))
eco_data = snyk_data[eco]
for pkg in eco_data:
if pkg not in json_data[eco]:
json_data[eco][pkg] = {}
for vuln in eco_data[pkg]['vulnerabilities']:
id = vuln['id']
logger.info("Adding {}.".format(id))
json_data[eco][pkg][id] = self.generate_vuln_json(vuln, pkg)
else:
logger.info("No data for {} present in the parsed snyk feed.".format(eco))
return json_data
def process(self, snyk_data):
"""Process the vulnerability operation for air gapped or cache."""
json_data = self.get_vuln_data()
json_data = self.extract_info(snyk_data, json_data)
self.save_vuln_json(json_data)