-
-
Notifications
You must be signed in to change notification settings - Fork 328
Scrapes data from scrapers and dump in the database #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
ea36954
b007cb2
7825f98
49d0a2c
f1a1e99
0334beb
9c413dc
58e33db
d3850bb
0a08cc4
4f2ffa3
f6cad6e
cc42000
ce03b49
d5177f7
a23d916
f56a019
bd66de2
6bcf992
184266f
218fffb
931d539
4e4dcff
4698df0
9906a43
a994a23
85bd950
26fa349
20ee1f9
efedb24
1716de9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| # | ||
| # Copyright (c) 2017 nexB Inc. and others. All rights reserved. | ||
| # http://nexb.com and https://github.com/nexB/vulnerablecode/ | ||
| # The VulnerableCode software is licensed under the Apache License version 2.0. | ||
| # Data generated with VulnerableCode require an acknowledgment. | ||
| # | ||
| # You may not use this software except in compliance with the License. | ||
| # You may obtain a copy of the License at: http://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. | ||
| # | ||
| # When you publish or redistribute any data created with VulnerableCode or any VulnerableCode | ||
| # derivative work, you must accompany this data with the following acknowledgment: | ||
| # | ||
| # Generated with VulnerableCode and provided on an "AS IS" BASIS, WITHOUT WARRANTIES | ||
| # OR CONDITIONS OF ANY KIND, either express or implied. No content created from | ||
| # VulnerableCode should be considered or used as legal advice. Consult an Attorney | ||
| # for any legal advice. | ||
| # VulnerableCode is a free software code scanning tool from nexB Inc. and others. | ||
| # Visit https://github.com/nexB/vulnerablecode/ for support and download. | ||
|
|
||
| from vulncode_app.models import Vulnerability | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove un-used imports |
||
| from vulncode_app.models import VulnerabilityReference | ||
| from vulncode_app.models import ImpactedPackage | ||
| from vulncode_app.models import ResolvedPackage | ||
| from vulncode_app.models import Package | ||
| from vulncode_app.models import PackageReference | ||
|
|
||
| from scraper import debian, ubuntu | ||
|
|
||
|
|
||
| def debian_data(): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be in the scraper/debian.py module, for consistency with https://github.com/nexB/vulnerablecode/blob/develop/scraper/ubuntu.py#L50
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tdruez Valid point. 👍 |
||
| """ | ||
| Scrape debian' security tracker. | ||
| """ | ||
| json_data = debian.json_data() | ||
| extract_data = debian.extract_data(json_data) | ||
|
|
||
| return extract_data | ||
|
|
||
|
|
||
| def debian_dump(extract_data): | ||
| """ | ||
| Save data scraped from Debian' security tracker. | ||
| """ | ||
| for data in extract_data: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could use
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, I doubt the following objects creation works, the required fields are not properly set.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tdruez Could you elaborate this? Are you talking in context to
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For example:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tdruez Yes, I get your point. I'll add the updated models along the next commit. |
||
| vulnerability = Vulnerability(summary=data.get('description')) | ||
| vulnerability_reference = VulnerabilityReference(reference_id=data.get('vulnerability_id')) | ||
| package = Package(name=data.get('package_name'), version=data.get('fixed_version')) | ||
|
|
||
| vulnerability.save() | ||
| vulnerability_reference.save() | ||
| package.save() | ||
|
|
||
|
|
||
| def ubuntu_data(): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is un-needed, just call
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tdruez gotcha 👍 |
||
| """ | ||
| Scrape Ubuntu' main security tracker. | ||
| """ | ||
| data = ubuntu.scrape_cves() | ||
| return data | ||
|
|
||
|
|
||
| def ubuntu_dump(): | ||
| """ | ||
| Dump data scraped from Ubuntu's security tracker. | ||
| """ | ||
| for data in extract_data: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You did not run the tests ;) where could extract_data come from?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @pombredanne It is being passed as a parameter in the method?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I cannot see a parameter in https://github.com/nexB/vulnerablecode/pull/25/files#diff-102b650ccd1cc2cb42f5359f5101e8e1R66 |
||
| vulnerability_reference = VulnerabilityReference(reference_id=data.get('cve_id')) | ||
| package = ImpactedPackage(name=data.get('package_name')) | ||
|
|
||
| vulnerability_reference.save() | ||
| package.save() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| # | ||
| # Copyright (c) 2017 nexB Inc. and others. All rights reserved. | ||
| # http://nexb.com and https://github.com/nexB/vulnerablecode/ | ||
| # The VulnerableCode software is licensed under the Apache License version 2.0. | ||
| # Data generated with VulnerableCode require an acknowledgment. | ||
| # | ||
| # You may not use this software except in compliance with the License. | ||
| # You may obtain a copy of the License at: http://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. | ||
| # | ||
| # When you publish or redistribute any data created with VulnerableCode or any VulnerableCode | ||
| # derivative work, you must accompany this data with the following acknowledgment: | ||
| # | ||
| # Generated with VulnerableCode and provided on an "AS IS" BASIS, WITHOUT WARRANTIES | ||
| # OR CONDITIONS OF ANY KIND, either express or implied. No content created from | ||
| # VulnerableCode should be considered or used as legal advice. Consult an Attorney | ||
| # for any legal advice. | ||
| # VulnerableCode is a free software code scanning tool from nexB Inc. and others. | ||
| # Visit https://github.com/nexB/vulnerablecode/ for support and download. | ||
|
|
||
| from django.test import TestCase | ||
|
|
||
| from vulncode_app.models import Vulnerability | ||
| from vulncode_app.models import VulnerabilityReference | ||
| from vulncode_app.models import Package | ||
| from vulncode_app.data_dump import debian_dump | ||
|
|
||
| import json | ||
|
|
||
| from scraper import debian | ||
|
|
||
|
|
||
| class TestDataDump(TestCase): | ||
| def test_data_dump(self): | ||
| """ | ||
| Scrape data from Debian' main tracker, dump it | ||
| in the database and verify entries. | ||
| """ | ||
| with open("tests/test_data/debian.json") as f: | ||
| test_data = json.loads(f.read()) | ||
|
|
||
| extract_data = debian.extract_data(test_data) | ||
| data_dump = debian_dump(extract_data) | ||
|
|
||
| for i in range(3): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why limit yourself to 3?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But why doing three iterations?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @pombredanne I was iterating over
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not a robust approach for testing. Instead use a query, this is an iterable that you can then handle. Never expect that the pk have a specific values.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @pombredanne Got it. 👍 |
||
| self.assertEqual(3, len(Vulnerability.objects.all())) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is the query in the range loop?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also you should not assert on all() but instead with a filter that select what you expect there.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @pombredanne The query shouldn't be in the loop.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @pombredanne Since, we are testing the number of
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My main point was not to make that test three times in a range loop.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gotcha 👍 Well there is no loop anymore. So, the root is gone. |
||
| self.assertEqual(3, len(VulnerabilityReference.objects.all())) | ||
| self.assertEqual(3, len(Package.objects.all())) | ||
| self.assertEqual(extract_data[i].get('description'), | ||
| Vulnerability.objects.get(pk=i+1).summary) | ||
| self.assertEqual(extract_data[i].get('vulnerability_id'), | ||
| VulnerabilityReference.objects.get(pk=i+1).reference_id) | ||
| self.assertEqual(extract_data[i].get('package_name'), | ||
| Package.objects.get(pk=i+1).name) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should do a query, not hope that the PK will be incremented linearly
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updating. 👍 |
||
| self.assertEqual(extract_data[i].get('fixed_version'), | ||
| Package.objects.get(pk=i+1).version) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| # | ||
| # Copyright (c) 2017 nexB Inc. and others. All rights reserved. | ||
| # http://nexb.com and https://github.com/nexB/vulnerablecode/ | ||
| # The VulnerableCode software is licensed under the Apache License version 2.0. | ||
| # Data generated with VulnerableCode require an acknowledgment. | ||
| # | ||
| # You may not use this software except in compliance with the License. | ||
| # You may obtain a copy of the License at: http://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. | ||
| # | ||
| # When you publish or redistribute any data created with VulnerableCode or any VulnerableCode | ||
| # derivative work, you must accompany this data with the following acknowledgment: | ||
| # | ||
| # Generated with VulnerableCode and provided on an "AS IS" BASIS, WITHOUT WARRANTIES | ||
| # OR CONDITIONS OF ANY KIND, either express or implied. No content created from | ||
| # VulnerableCode should be considered or used as legal advice. Consult an Attorney | ||
| # for any legal advice. | ||
| # VulnerableCode is a free software code scanning tool from nexB Inc. and others. | ||
| # Visit https://github.com/nexB/vulnerablecode/ for support and download. | ||
|
|
||
| from django.test import TestCase | ||
|
|
||
| from vulncode_app.models import Vulnerability | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, un-used import and un-used variable in that file. |
||
| from vulncode_app.models import VulnerabilityReference | ||
| from vulncode_app.models import ImpactedPackage | ||
| from vulncode_app.models import ResolvedPackage | ||
| from vulncode_app.models import Package | ||
| from vulncode_app.models import PackageReference | ||
|
|
||
|
|
||
| class TestVulnerability(TestCase): | ||
| def test_vulnerability(self): | ||
| summary_create = Vulnerability.objects.create(summary="Affected package xyz") | ||
| summary_get = Vulnerability.objects.get(pk=summary_create.pk) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Un-used variable.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tdruez Will update this now. 👍 |
||
|
|
||
| self.assertEqual(str(summary_create), "Affected package xyz") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not convert to a string. Instead test the attributes values here and in your other tests
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @pombredanne Do you think we should be keeping
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nope, keep these tests
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and do not convert to str
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @pombredanne Will update. 👍 |
||
|
|
||
|
|
||
| class TestVulnerabilityReference(TestCase): | ||
| def test_vulnerability_reference(self): | ||
| ref_id_create = VulnerabilityReference.objects.create(reference_id="CVE-2017-8564") | ||
| ref_id_get = VulnerabilityReference.objects.get(pk=ref_id_create.pk) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Un-used variable. |
||
|
|
||
| self.assertEqual(str(ref_id_create), "CVE-2017-8564") | ||
|
|
||
|
|
||
| class TestPackage(TestCase): | ||
| def test_package(self): | ||
| package_name_create = Package.objects.create(name="Firefox") | ||
| package_name_get = Package.objects.get(pk=package_name_create.pk) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Un-used variable. |
||
|
|
||
| self.assertEqual(str(package_name_create), "Firefox") | ||
|
|
||
|
|
||
| class TestPackageReference(TestCase): | ||
| def test_package_reference(self): | ||
| platform_create = PackageReference.objects.create(platform="Maven") | ||
| platform_get = PackageReference.objects.get(pk=platform_create.pk) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Un-used variable. |
||
|
|
||
| self.assertEqual(str(platform_create), "Maven") | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| # | ||
| # Copyright (c) 2017 nexB Inc. and others. All rights reserved. | ||
| # http://nexb.com and https://github.com/nexB/vulnerablecode/ | ||
| # The VulnerableCode software is licensed under the Apache License version 2.0. | ||
| # Data generated with VulnerableCode require an acknowledgment. | ||
| # | ||
| # You may not use this software except in compliance with the License. | ||
| # You may obtain a copy of the License at: http://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. | ||
| # | ||
| # When you publish or redistribute any data created with VulnerableCode or any VulnerableCode | ||
| # derivative work, you must accompany this data with the following acknowledgment: | ||
| # | ||
| # Generated with VulnerableCode and provided on an "AS IS" BASIS, WITHOUT WARRANTIES | ||
| # OR CONDITIONS OF ANY KIND, either express or implied. No content created from | ||
| # VulnerableCode should be considered or used as legal advice. Consult an Attorney | ||
| # for any legal advice. | ||
| # VulnerableCode is a free software code scanning tool from nexB Inc. and others. | ||
| # Visit https://github.com/nexB/vulnerablecode/ for support and download. | ||
|
|
||
| from vulncode_app.models import Vulnerability | ||
| from vulncode_app.models import VulnerabilityReference | ||
| from vulncode_app.models import ImpactedPackage | ||
| from vulncode_app.models import ResolvedPackage | ||
| from vulncode_app.models import Package | ||
| from vulncode_app.models import PackageReference | ||
|
|
||
| from scraper import debian | ||
| from scraper import ubuntu | ||
|
|
||
|
|
||
| def debian_dump(): | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add docstring |
||
| json_data = debian.json_data() | ||
| extracted_data = debian.extract_data(json_data) | ||
|
|
||
| for i, v in enumerate(extract_data): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you enumerate here at all? this code makes no sense at all.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You still need to process the comment I made above ;)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @pombredanne I have removed enumerate? |
||
| vulnerability = Vulnerability(summary=extract_data[i].get('description')) | ||
| vulnerability_reference = VulnerabilityReference( | ||
| reference_id=extract_data[i].get('vulnerability_id')) | ||
| package = ImpactedPackage(name=extract_data[i].get('package'), | ||
| version=extract_data[i].get('fixed_version')) | ||
|
|
||
| vulnerability.save() | ||
| vulnerability_reference.save() | ||
| package.save() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the save() need to be in the loop... |
||
|
|
||
|
|
||
| def ubuntu_dump(): | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add some docstring |
||
| data = ubuntu.scrape_cves() | ||
|
|
||
| for i, v in enumerate(extract_data): | ||
| vulnerability_reference = VulnerabilityReference( | ||
| reference_id=data[i].get('cve_id')) | ||
| package = ImpactedPackage(name=data[i].get('package_name')) | ||
|
|
||
| vulnerability_reference.save() | ||
| package.save() | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this blank line?