Skip to content

Commit

Permalink
new: [commands] The dump command can now dump comments (--comments) a…
Browse files Browse the repository at this point in the history
…nd bundles (--bundles). Closes #65.
  • Loading branch information
cedricbonhomme committed Aug 5, 2024
1 parent 7c1d34e commit 4a57712
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
35 changes: 33 additions & 2 deletions bin/dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

from vulnerabilitylookup import VulnerabilityLookup
from vulnerabilitylookup.default import get_config, get_homedir, safe_create_dir
from website.models import Bundle
from website.models import Comment
from website.web.bootstrap import application

logging.config.dictConfig(get_config('logging'))

Expand All @@ -31,19 +34,41 @@ def dump(self, feed: str, /) -> None:
with dest_file.open('a') as f:
json.dump(vuln, f)

def dump_comments(self) -> None:
dest_file = self.root_dumps / 'comments.ndjson'
dest_file.unlink(missing_ok=True)
with application.app_context():
for comment in Comment.query.filter():
with dest_file.open('a') as f:
json.dump(comment.to_dict(), f)

def dump_bundles(self) -> None:
dest_file = self.root_dumps / 'bundles.ndjson'
dest_file.unlink(missing_ok=True)
with application.app_context():
for bundle in Bundle.query.filter():
with dest_file.open('a') as f:
json.dump(bundle.to_dict(), f)


def main() -> None:
parser = argparse.ArgumentParser(
description="Dump vulnerability-lookup storage in NDJSON."
)
parser.add_argument(
"--feed", help="Feed to dump. Default is set to nvd.", default="nvd"
"--feed", help="Feed to dump. Default is set to nvd.", default="csaf_ox"
)
parser.add_argument(
"--all", help="Dump all feeds. Default is false", default=False, action="store_true"
)
parser.add_argument(
"--index", help="Generate an index to publish all the feeds. Default is false", default=False, action="store_true"
"--index", help="Generate an index to publish all the feeds. Default is false.", default=False, action="store_true"
)
parser.add_argument(
"--comments", help="Export the comments. Default is false.", action="store_true"
)
parser.add_argument(
"--bundles", help="Export the bundles. Default is false.", action="store_true"
)
args = parser.parse_args()
d = Dump()
Expand All @@ -68,6 +93,12 @@ def main() -> None:
index = index + f'</ul><br/>Generated on {current_date}</body></html>'
with dest_file.open('w') as f:
f.write(index)
if args.comments:
print('Dumping comments...')
d.dump_comments()
if args.bundles:
print('Dumping bundles...')
d.dump_bundles()

if __name__ == '__main__':
main()
4 changes: 4 additions & 0 deletions website/models/bundle.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import uuid
from typing import Any
from sqlalchemy import func
from sqlalchemy.dialects.postgresql import JSONB, UUID
from sqlalchemy.orm import validates
Expand Down Expand Up @@ -53,3 +54,6 @@ def as_json(self) -> str:
"meta": self.meta,
}
)

def to_dict(self) -> dict[str, Any]:
return json.loads(self.as_json())
22 changes: 22 additions & 0 deletions website/models/comment.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import json
import uuid
from typing import Any
from sqlalchemy import func
from sqlalchemy.dialects.postgresql import JSONB, UUID
from sqlalchemy.orm import validates
Expand Down Expand Up @@ -38,3 +40,23 @@ def validates_description_format(self, key: str, value: str) -> str:
"value must be 'markdown' or 'text'."
)
return value.lower()

def as_json(self) -> str:
return json.dumps(
{
"uuid": str(self.uuid),
"vulnerability_lookup_origin": str(self.vulnerability_lookup_origin),
"title": self.title,
"description": self.description,
"description_format": self.description_format,
"creation_timestamp": self.creation_timestamp.strftime(
"%Y-%m-%dT%H:%M:%S.%fZ"
),
"timestamp": self.timestamp.strftime("%Y-%m-%dT%H:%M:%S.%fZ"),
"related_vulnerabilities": self.related_vulnerabilities,
"meta": self.meta,
}
)

def to_dict(self) -> dict[str, Any]:
return json.loads(self.as_json())

0 comments on commit 4a57712

Please sign in to comment.