|
| 1 | +# Copyright (c) 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 | + |
| 23 | +import os |
| 24 | +import json |
| 25 | +import tempfile |
| 26 | +from contextlib import contextmanager |
| 27 | + |
| 28 | +from django.core.management.base import BaseCommand |
| 29 | +from django.core.management.base import CommandError |
| 30 | + |
| 31 | +from vulnerabilities import models |
| 32 | + |
| 33 | +# See https://stackoverflow.com/a/24176022 |
| 34 | +@contextmanager |
| 35 | +def cd(newdir): |
| 36 | + prevdir = os.getcwd() |
| 37 | + os.chdir(os.path.expanduser(newdir)) |
| 38 | + try: |
| 39 | + yield |
| 40 | + finally: |
| 41 | + os.chdir(prevdir) |
| 42 | + |
| 43 | + |
| 44 | +def get_vulcodes(): |
| 45 | + |
| 46 | + vulcodes = models.Vulnerability.objects.filter( |
| 47 | + identifier__startswith="VULCODE" |
| 48 | + ).select_related() |
| 49 | + for vuln in vulcodes: |
| 50 | + yield { |
| 51 | + "identifier": vuln.identifier, |
| 52 | + "summary": vuln.summary, |
| 53 | + "references": [ |
| 54 | + { |
| 55 | + "url": ref.url, |
| 56 | + "reference_id": ref.reference_id, |
| 57 | + } |
| 58 | + for ref in vuln.vulnerabilityreference_set.all() |
| 59 | + ], |
| 60 | + "vulnerable_packages": [pkg.package_url for pkg in vuln.vulnerable_to], |
| 61 | + "resolved_packages": [pkg.package_url for pkg in vuln.resolved_to], |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | +class Command(BaseCommand): |
| 66 | + help = "Push all VulCodes to remote repo" |
| 67 | + |
| 68 | + def add_arguments(self, parser): |
| 69 | + parser.add_argument( |
| 70 | + "remote_url", |
| 71 | + help="Example Value :`https://github.com/nexB/vulcodes.git`", |
| 72 | + ) |
| 73 | + |
| 74 | + def handle(self, *args, **options): |
| 75 | + repo_url = options["remote_url"] |
| 76 | + # TODO; Do some validation of `repo_url` here |
| 77 | + push_data(repo_url) |
| 78 | + |
| 79 | + |
| 80 | +def push_data(url): |
| 81 | + repo_location = tempfile.mkdtemp() |
| 82 | + with cd(repo_location): |
| 83 | + os.system(f"git clone {url}") |
| 84 | + # TODO: Don't hardcode `vulcodes` |
| 85 | + os.system("cd vulcodes") |
| 86 | + with cd("vulcodes"): |
| 87 | + for vulcode in get_vulcodes(): |
| 88 | + with open(vulcode["identifier"] + ".json", "w") as f: |
| 89 | + json.dump(vulcode, f, indent=4) |
| 90 | + |
| 91 | + os.system("git add .") |
| 92 | + os.system("git commit -s -m 'Vulcode Sync' ") |
| 93 | + os.system("git push") |
0 commit comments