Skip to content

Commit 1ab4154

Browse files
xmunozewdurbin
authored andcommitted
Refactor MalwareCheckBase. Fixes #7091. (#7196)
* Refactor MalwareCheckBase. Fixes #7091. Add Foreign Keys in MalwareVerdicts for other types of objects (Releases, Projects). * Change verdict dict to kwargs.
1 parent f50c5ca commit 1ab4154

File tree

4 files changed

+28
-20
lines changed

4 files changed

+28
-20
lines changed

warehouse/malware/checks/base.py

+15-9
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,29 @@
1010
# See the License for the specific language governing permissions and
1111
# limitations under the License.
1212

13-
from warehouse.malware.models import MalwareCheck, MalwareCheckState
13+
from warehouse.malware.models import MalwareCheck, MalwareCheckState, MalwareVerdict
1414

1515

1616
class MalwareCheckBase:
1717
def __init__(self, db):
1818
self.db = db
1919
self._name = self.__class__.__name__
20-
self._load_check()
20+
self._load_check_id()
21+
self._verdicts = []
22+
23+
def add_verdict(self, **kwargs):
24+
self._verdicts.append(MalwareVerdict(check_id=self.id, **kwargs))
2125

2226
def run(self, obj_id):
2327
"""
24-
Executes the check.
28+
Runs the check and inserts returned verdicts.
29+
"""
30+
self.scan(obj_id)
31+
self.db.add_all(self._verdicts)
32+
33+
def scan(self, obj_id):
34+
"""
35+
Scans the object and returns a verdict.
2536
"""
2637

2738
def backfill(self, sample=1):
@@ -31,12 +42,7 @@ def backfill(self, sample=1):
3142
backfill on the entire corpus.
3243
"""
3344

34-
def update(self):
35-
"""
36-
Update the check definition in the database.
37-
"""
38-
39-
def _load_check(self):
45+
def _load_check_id(self):
4046
self.id = (
4147
self.db.query(MalwareCheck.id)
4248
.filter(MalwareCheck.name == self._name)

warehouse/malware/checks/example.py

+3-9
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@
1111
# limitations under the License.
1212

1313
from warehouse.malware.checks.base import MalwareCheckBase
14-
from warehouse.malware.models import (
15-
MalwareVerdict,
16-
VerdictClassification,
17-
VerdictConfidence,
18-
)
14+
from warehouse.malware.models import VerdictClassification, VerdictConfidence
1915

2016

2117
class ExampleCheck(MalwareCheckBase):
@@ -30,12 +26,10 @@ class ExampleCheck(MalwareCheckBase):
3026
def __init__(self, db):
3127
super().__init__(db)
3228

33-
def run(self, file_id):
34-
verdict = MalwareVerdict(
35-
check_id=self.id,
29+
def scan(self, file_id):
30+
self.add_verdict(
3631
file_id=file_id,
3732
classification=VerdictClassification.benign,
3833
confidence=VerdictConfidence.High,
3934
message="Nothing to see here!",
4035
)
41-
self.db.add(verdict)

warehouse/malware/models.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ class MalwareVerdict(db.Model):
115115
nullable=False,
116116
index=True,
117117
)
118-
file_id = Column(ForeignKey("release_files.id"), nullable=False)
118+
file_id = Column(ForeignKey("release_files.id"), nullable=True)
119+
release_id = Column(ForeignKey("releases.id"), nullable=True)
120+
project_id = Column(ForeignKey("projects.id"), nullable=True)
119121
classification = Column(
120122
Enum(VerdictClassification, values_callable=lambda x: [e.value for e in x]),
121123
nullable=False,
@@ -135,3 +137,5 @@ class MalwareVerdict(db.Model):
135137

136138
check = orm.relationship("MalwareCheck", foreign_keys=[check_id], lazy=True)
137139
release_file = orm.relationship("File", foreign_keys=[file_id], lazy=True)
140+
release = orm.relationship("Release", foreign_keys=[release_id], lazy=True)
141+
project = orm.relationship("Project", foreign_keys=[project_id], lazy=True)

warehouse/migrations/versions/061ff3d24c22_add_malware_detection_tables.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ def upgrade():
7777
"run_date", sa.DateTime(), server_default=sa.text("now()"), nullable=False
7878
),
7979
sa.Column("check_id", postgresql.UUID(as_uuid=True), nullable=False),
80-
sa.Column("file_id", postgresql.UUID(as_uuid=True), nullable=False),
80+
sa.Column("file_id", postgresql.UUID(as_uuid=True), nullable=True),
81+
sa.Column("project_id", postgresql.UUID(as_uuid=True), nullable=True),
82+
sa.Column("release_id", postgresql.UUID(as_uuid=True), nullable=True),
8183
sa.Column("classification", VerdictClassifications, nullable=False,),
8284
sa.Column("confidence", VerdictConfidences, nullable=False,),
8385
sa.Column("message", sa.Text(), nullable=True),
@@ -94,6 +96,8 @@ def upgrade():
9496
["check_id"], ["malware_checks.id"], onupdate="CASCADE", ondelete="CASCADE"
9597
),
9698
sa.ForeignKeyConstraint(["file_id"], ["release_files.id"]),
99+
sa.ForeignKeyConstraint(["release_id"], ["releases.id"]),
100+
sa.ForeignKeyConstraint(["project_id"], ["projects.id"]),
97101
sa.PrimaryKeyConstraint("id"),
98102
)
99103
op.create_index(

0 commit comments

Comments
 (0)