Skip to content

Commit

Permalink
Revert "Merge pull request #308 from vipyrsec/distro-download-url"
Browse files Browse the repository at this point in the history
This reverts commit f741aa8, reversing
changes made to 5d2d481.
  • Loading branch information
jonathan-d-zhang committed Aug 27, 2024
1 parent b7f843f commit a894124
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 46 deletions.
23 changes: 0 additions & 23 deletions alembic/versions/a62a93704798_add_distributions.py

This file was deleted.

2 changes: 1 addition & 1 deletion src/mainframe/endpoints/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def submit_results(
scan.score = result.score
scan.finished_by = auth.subject
scan.commit_hash = result.commit
scan.distributions = result.distributions
scan.files = result.files

# These are the rules that already have an entry in the database
rules = session.scalars(select(Rule).where(Rule.name.in_(result.rules_matched))).all()
Expand Down
4 changes: 2 additions & 2 deletions src/mainframe/models/orm.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
)

from mainframe.models import Pydantic
from mainframe.models.schemas import Distributions
from mainframe.models.schemas import Files


class Base(MappedAsDataclass, DeclarativeBase, kw_only=True):
Expand Down Expand Up @@ -102,7 +102,7 @@ class Scan(Base):

commit_hash: Mapped[Optional[str]] = mapped_column(default=None)

distributions: Mapped[Optional[Distributions]] = mapped_column(Pydantic(Distributions), default=None)
files: Mapped[Optional[Files]] = mapped_column(Pydantic(Files), default=None)


Index(None, Scan.status, postgresql_where=or_(Scan.status == Status.QUEUED, Scan.status == Status.PENDING))
Expand Down
16 changes: 4 additions & 12 deletions src/mainframe/models/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,7 @@ class File(BaseModel):
matches: list[RuleMatch]


Files = list[File]


class Distribution(BaseModel):
download_url: str
files: Files


Distributions = RootModel[list[Distribution]]
Files = RootModel[list[File]]


class ServerMetadata(BaseModel):
Expand Down Expand Up @@ -96,7 +88,7 @@ class Package(BaseModel):

commit_hash: Optional[str]

distributions: Optional[Distributions]
files: Optional[Files]

@classmethod
def from_db(cls, scan: Scan):
Expand All @@ -118,7 +110,7 @@ def from_db(cls, scan: Scan):
finished_at=scan.finished_at,
finished_by=scan.finished_by,
commit_hash=scan.commit_hash,
distributions=scan.distributions,
files=scan.files,
)

@field_serializer(
Expand Down Expand Up @@ -187,7 +179,7 @@ class PackageScanResult(PackageSpecifier):
score: int = 0
inspector_url: Optional[str] = None
rules_matched: list[str] = []
distributions: Optional[Distributions] = None
files: Optional[Files] = None


class PackageScanResultFail(PackageSpecifier):
Expand Down
13 changes: 5 additions & 8 deletions tests/test_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
from mainframe.json_web_token import AuthenticationData
from mainframe.models.orm import Scan, Status
from mainframe.models.schemas import (
Distribution,
Distributions,
File,
Files,
Match,
Expand Down Expand Up @@ -97,21 +95,21 @@ def test_package_lookup_files(db_session: Session):
rule = RuleMatch(identifier="rule1", patterns=[pattern], metadata={"author": "remmy", "score": 5})
file = File(path="dist1/a/b.py", matches=[rule])
files = Files([file])
distros = Distributions([Distribution(download_url="http://example.com", files=files)])
scan = Scan(
name="abc",
version="1.0.0",
status=Status.FINISHED,
queued_by="remmy",
distributions=distros,
files=files,
)

with db_session.begin():
db_session.add(scan)
db_session.commit()

package = lookup_package_info(db_session, name="abc", version="1.0.0")[0]

assert package.distributions == distros
assert package.files == files


def test_handle_success(db_session: Session, test_data: list[Scan], auth: AuthenticationData, rules_state: Rules):
Expand All @@ -128,7 +126,6 @@ def test_handle_success(db_session: Session, test_data: list[Scan], auth: Authen
rule = RuleMatch(identifier="rule1", patterns=[pattern], metadata={"author": "remmy", "score": 5})
file = File(path="dist1/a/b.py", matches=[rule])
files = Files([file])
distros = Distributions([Distribution(download_url="http://example.com", files=files)])

body = PackageScanResult(
name=job.name,
Expand All @@ -137,7 +134,7 @@ def test_handle_success(db_session: Session, test_data: list[Scan], auth: Authen
score=2,
inspector_url="test inspector url",
rules_matched=["a", "b", "c"],
distributions=distros,
files=files,
)
submit_results(body, db_session, auth)

Expand All @@ -150,7 +147,7 @@ def test_handle_success(db_session: Session, test_data: list[Scan], auth: Authen
assert record.score == 2
assert record.inspector_url == "test inspector url"
assert {rule.name for rule in record.rules} == {"a", "b", "c"}
assert record.distributions == distros
assert record.files == files
else:
assert all(scan.status != Status.QUEUED for scan in test_data)

Expand Down

0 comments on commit a894124

Please sign in to comment.