Skip to content

Commit 2bff601

Browse files
committed
Rename to . Utilise in RubyDataSource. Add caching in rubyAPI . Fix reference_urls in RubyDataSource
Signed-off-by: Shivam Sandbhor <shivam.sandbhor@gmail.com>
1 parent d391993 commit 2bff601

4 files changed

Lines changed: 91 additions & 11 deletions

File tree

vulnerabilities/importers/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@
2626
from vulnerabilities.importers.npm import NpmDataSource
2727
from vulnerabilities.importers.rust import RustDataSource
2828
from vulnerabilities.importers.safety_db import SafetyDbDataSource
29-
from vulnerabilities.importers.ruby import rubyDataSource
29+
from vulnerabilities.importers.ruby import RubyDataSource

vulnerabilities/importers/ruby.py

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
# Copyright (c) 2017 nexB Inc. and others. All rights reserved.
2+
# http://nexb.com and https://github.com/nexB/vulnerablecode/
3+
# The VulnerableCode software is licensed under the Apache License version 2.0.
4+
# Data generated with VulnerableCode require an acknowledgment.
5+
#
6+
# You may not use this software except in compliance with the License.
7+
# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0
8+
# Unless required by applicable law or agreed to in writing, software distributed
9+
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
10+
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
# specific language governing permissions and limitations under the License.
12+
#
13+
# When you publish or redistribute any data created with VulnerableCode or any VulnerableCode
14+
# derivative work, you must accompany this data with the following acknowledgment:
15+
#
16+
# Generated with VulnerableCode and provided on an "AS IS" BASIS, WITHOUT WARRANTIES
17+
# OR CONDITIONS OF ANY KIND, either express or implied. No content created from
18+
# VulnerableCode should be considered or used as legal advice. Consult an Attorney
19+
# for any legal advice.
20+
# VulnerableCode is a free software code scanning tool from nexB Inc. and others.
21+
# Visit https://github.com/nexB/vulnerablecode/ for support and download.
22+
123
from json import JSONDecodeError
224
from typing import Set
325
from typing import List
@@ -12,17 +34,26 @@
1234
from vulnerabilities.data_source import GitDataSource
1335

1436

15-
class rubyDataSource(GitDataSource):
37+
class RubyDataSource(GitDataSource):
1638

1739
def __enter__(self):
18-
super(rubyDataSource, self).__enter__()
40+
super(RubyDataSource, self).__enter__()
1941

2042
if not getattr(self, '_added_files', None):
2143
self._added_files, self._updated_files = self.file_changes(
2244
recursive=True, file_ext='yml', subdir='./gems')
2345

2446
def updated_advisories(self) -> Set[Advisory]:
25-
files = self._updated_files.union(self._added_files)
47+
files = self._updated_files
48+
advisories = []
49+
for f in files:
50+
processed_data = self.process_file(f)
51+
if processed_data:
52+
advisories.append(processed_data)
53+
return self.batch_advisories(advisories)
54+
55+
def added_advisories(self) -> Set[Advisory]:
56+
files = self._added_files
2657
advisories = []
2758
for f in files:
2859
processed_data = self.process_file(f)
@@ -77,7 +108,7 @@ def process_file(self, path) -> List[Advisory]:
77108
summary=record.get('description', ''),
78109
impacted_package_urls=impacted_purls,
79110
resolved_package_urls=resolved_purls,
80-
reference_urls=record.get('url', ''),
111+
reference_urls=[record.get('url', '')],
81112
cve_id=cve_id
82113
)
83114

@@ -108,6 +139,7 @@ class rubyAPI:
108139

109140
def __init__(self):
110141
self.client = requests.Session()
142+
self.cache = {}
111143

112144
def call_api(self, pkg_name) -> List:
113145
end_pt = self.base_endpt.format(pkg_name)
@@ -120,7 +152,11 @@ def call_api(self, pkg_name) -> List:
120152

121153
def get_all_version_of_package(self, pkg_name) -> Set[str]:
122154
all_versions = set()
155+
if self.cache.get(pkg_name):
156+
return self.cache.get(pkg_name)
157+
123158
json_resp = self.call_api(pkg_name)
124159
for release in json_resp:
125160
all_versions.add(release['number'])
161+
self.cache[pkg_name] = all_versions
126162
return all_versions

vulnerabilities/migrations/0008_ruby_importer.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
# Copyright (c) 2017 nexB Inc. and others. All rights reserved.
2+
# http://nexb.com and https://github.com/nexB/vulnerablecode/
3+
# The VulnerableCode software is licensed under the Apache License version 2.0.
4+
# Data generated with VulnerableCode require an acknowledgment.
5+
#
6+
# You may not use this software except in compliance with the License.
7+
# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0
8+
# Unless required by applicable law or agreed to in writing, software distributed
9+
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
10+
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
# specific language governing permissions and limitations under the License.
12+
#
13+
# When you publish or redistribute any data created with VulnerableCode or any VulnerableCode
14+
# derivative work, you must accompany this data with the following acknowledgment:
15+
#
16+
# Generated with VulnerableCode and provided on an "AS IS" BASIS, WITHOUT WARRANTIES
17+
# OR CONDITIONS OF ANY KIND, either express or implied. No content created from
18+
# VulnerableCode should be considered or used as legal advice. Consult an Attorney
19+
# for any legal advice.
20+
# VulnerableCode is a free software code scanning tool from nexB Inc. and others.
21+
# Visit https://github.com/nexB/vulnerablecode/ for support and download.
22+
123
from django.db import migrations
224

325

@@ -8,7 +30,7 @@ def add_ruby_importer(apps, _):
830
name='ruby',
931
license='',
1032
last_run=None,
11-
data_source='rubyDataSource',
33+
data_source='RubyDataSource',
1234
data_source_cfg={
1335
'repository_url': 'https://github.com/rubysec/ruby-advisory-db.git',
1436
},

vulnerabilities/tests/test_ruby.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
# Copyright (c) 2017 nexB Inc. and others. All rights reserved.
2+
# http://nexb.com and https://github.com/nexB/vulnerablecode/
3+
# The VulnerableCode software is licensed under the Apache License version 2.0.
4+
# Data generated with VulnerableCode require an acknowledgment.
5+
#
6+
# You may not use this software except in compliance with the License.
7+
# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0
8+
# Unless required by applicable law or agreed to in writing, software distributed
9+
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
10+
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
# specific language governing permissions and limitations under the License.
12+
#
13+
# When you publish or redistribute any data created with VulnerableCode or any VulnerableCode
14+
# derivative work, you must accompany this data with the following acknowledgment:
15+
#
16+
# Generated with VulnerableCode and provided on an "AS IS" BASIS, WITHOUT WARRANTIES
17+
# OR CONDITIONS OF ANY KIND, either express or implied. No content created from
18+
# VulnerableCode should be considered or used as legal advice. Consult an Attorney
19+
# for any legal advice.
20+
# VulnerableCode is a free software code scanning tool from nexB Inc. and others.
21+
# Visit https://github.com/nexB/vulnerablecode/ for support and download.
22+
123
import os
224
import pathlib
325
from unittest.mock import patch
@@ -6,7 +28,7 @@
628

729
from packageurl import PackageURL
830

9-
from vulnerabilities.importers.ruby import rubyDataSource
31+
from vulnerabilities.importers.ruby import RubyDataSource
1032
from vulnerabilities.data_source import GitDataSourceConfiguration
1133
from vulnerabilities.data_source import Advisory
1234

@@ -20,13 +42,13 @@
2042
MOCK_ADDED_FILES.append(filepath.absolute())
2143

2244

23-
class rubyDataSourceTest(TestCase):
45+
class RubyDataSourceTest(TestCase):
2446

2547
@classmethod
2648
def setUpClass(cls):
2749
data_source_cfg = {
2850
'repository_url': 'https://github.com/rubysec/ruby-advisory-db.git', }
29-
cls.data_src = rubyDataSource(1, config=data_source_cfg)
51+
cls.data_src = RubyDataSource(1, config=data_source_cfg)
3052

3153
@patch('vulnerabilities.importers.ruby.rubyAPI.get_all_version_of_package',
3254
return_value={'1.0.0', '1.8.0', '2.0.3'})
@@ -60,7 +82,7 @@ def test_process_file(self, mock_write):
6082
version='2.0.3',
6183
qualifiers=OrderedDict(),
6284
subpath=None)},
63-
reference_urls='https://github.com/sinatra/sinatra/pull/1379',
85+
reference_urls=['https://github.com/sinatra/sinatra/pull/1379'],
6486
reference_ids=[],
6587
cve_id='CVE-2018-7212'),
6688
Advisory(
@@ -89,7 +111,7 @@ def test_process_file(self, mock_write):
89111
version='2.0.3',
90112
qualifiers=OrderedDict(),
91113
subpath=None)},
92-
reference_urls='https://github.com/sinatra/sinatra/issues/1428',
114+
reference_urls=['https://github.com/sinatra/sinatra/issues/1428'],
93115
reference_ids=[],
94116
cve_id='CVE-2018-11627'),
95117
None}

0 commit comments

Comments
 (0)