Skip to content
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

Improve CVE download #15

Merged
merged 2 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 46 additions & 28 deletions greenbone/scap/cve/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
Self,
Sequence,
)
from uuid import uuid4

from pontos.nvd.models.cve import CVE
from sqlalchemy import ColumnElement, and_, delete, func, select
Expand Down Expand Up @@ -455,51 +456,68 @@ async def _insert_configurations(

await connection.execute(delete_statement)

configuration_statement = self._db.insert(ConfigurationModel)
node_statement = self._db.insert(NodeModel)
match_statement = self._db.insert(CPEMatchModel)

configurations = []
matches = []
nodes = []

for cve in cves:
if not cve.configurations:
continue

for configuration in cve.configurations:
statement = (
self._db.insert(ConfigurationModel) # type: ignore[assignment]
.returning(ConfigurationModel.id)
.values(
configuration_id = uuid4()
configurations.append(
dict(
id=configuration_id,
cve_id=cve.id,
operator=configuration.operator,
negate=configuration.negate,
)
)

result = await connection.execute(statement)
configuration_id = result.scalar_one()
if not configuration.nodes:
continue

for node in configuration.nodes:
node_statement = (
self._db.insert(NodeModel)
.returning(NodeModel.id)
.values(
node_id = uuid4()
nodes.append(
dict(
id=node_id,
configuration_id=configuration_id,
operator=node.operator,
negate=node.negate,
)
)
result = await connection.execute(node_statement)
node_id = result.scalar_one()

statement = self._db.insert(CPEMatchModel) # type: ignore[assignment]

matches = [
dict(
node_id=node_id,
match_criteria_id=match.match_criteria_id,
vulnerable=match.vulnerable,
criteria=match.criteria,
version_start_excluding=match.version_start_excluding,
version_start_including=match.version_start_including,
version_end_excluding=match.version_end_excluding,
version_end_including=match.version_end_including,
)
for match in (node.cpe_match or [])
]
if not node.cpe_match:
continue

matches.extend(
[
dict(
node_id=node_id,
match_criteria_id=match.match_criteria_id,
vulnerable=match.vulnerable,
criteria=match.criteria,
version_start_excluding=match.version_start_excluding,
version_start_including=match.version_start_including,
version_end_excluding=match.version_end_excluding,
version_end_including=match.version_end_including,
)
for match in node.cpe_match
]
)

await connection.execute(statement, matches)
if configurations:
await connection.execute(configuration_statement, configurations)
if nodes:
await connection.execute(node_statement, nodes)
if matches:
await connection.execute(match_statement, matches)

@asynccontextmanager
async def session(self) -> AsyncGenerator[AsyncSession, None]:
Expand Down
10 changes: 6 additions & 4 deletions greenbone/scap/cve/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ class VendorCommentModel(Base):
class ConfigurationModel(Base):
__tablename__ = "cve_configurations"

id: Mapped[int] = mapped_column(primary_key=True)
id: Mapped[Uuid] = mapped_column(Uuid(as_uuid=False), primary_key=True)
cve_id: Mapped[cve_fk]
operator: Mapped[str | None]
negate: Mapped[bool | None]
Expand All @@ -207,8 +207,9 @@ class ConfigurationModel(Base):
class NodeModel(Base):
__tablename__ = "cve_nodes"

id: Mapped[int] = mapped_column(primary_key=True)
configuration_id: Mapped[int] = mapped_column(
id: Mapped[Uuid] = mapped_column(Uuid(as_uuid=False), primary_key=True)
configuration_id: Mapped[Uuid] = mapped_column(
Uuid(as_uuid=False),
ForeignKey("cve_configurations.id", ondelete="CASCADE"),
index=True,
)
Expand All @@ -229,7 +230,8 @@ class CPEMatchModel(Base):
match_criteria_id: Mapped[UUID] = mapped_column(
Uuid(as_uuid=False), primary_key=True
)
node_id: Mapped[int] = mapped_column(
node_id: Mapped[UUID] = mapped_column(
Uuid(as_uuid=False),
ForeignKey("cve_nodes.id", ondelete="CASCADE"),
primary_key=True,
index=True,
Expand Down
Loading