|
| 1 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 | +# you may not use this file except in compliance with the License. |
| 3 | +# You may obtain a copy of the License at |
| 4 | +# |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +# See the License for the specific language governing permissions and |
| 11 | +# limitations under the License. |
| 12 | +""" |
| 13 | +Add malware detection tables |
| 14 | +
|
| 15 | +Revision ID: 061ff3d24c22 |
| 16 | +Revises: b5bb5d08543d |
| 17 | +Create Date: 2019-12-18 17:27:00.183542 |
| 18 | +""" |
| 19 | +import citext |
| 20 | +import sqlalchemy as sa |
| 21 | + |
| 22 | +from alembic import op |
| 23 | +from sqlalchemy.dialects import postgresql |
| 24 | + |
| 25 | +revision = "061ff3d24c22" |
| 26 | +down_revision = "b5bb5d08543d" |
| 27 | + |
| 28 | +MalwareCheckTypes = sa.Enum("event_hook", "scheduled", name="malwarechecktypes") |
| 29 | + |
| 30 | +MalwareCheckStates = sa.Enum( |
| 31 | + "enabled", "evaluation", "disabled", "wiped_out", name="malwarecheckstate" |
| 32 | +) |
| 33 | + |
| 34 | +VerdictClassifications = sa.Enum( |
| 35 | + "threat", "indeterminate", "benign", name="verdictclassification" |
| 36 | +) |
| 37 | +VerdictConfidences = sa.Enum("low", "medium", "high", name="verdictconfidence") |
| 38 | + |
| 39 | + |
| 40 | +def upgrade(): |
| 41 | + op.create_table( |
| 42 | + "malware_checks", |
| 43 | + sa.Column( |
| 44 | + "id", |
| 45 | + postgresql.UUID(as_uuid=True), |
| 46 | + server_default=sa.text("gen_random_uuid()"), |
| 47 | + nullable=False, |
| 48 | + ), |
| 49 | + sa.Column("name", citext.CIText(), nullable=False), |
| 50 | + sa.Column("version", sa.Integer(), nullable=False), |
| 51 | + sa.Column("short_description", sa.String(length=128), nullable=False), |
| 52 | + sa.Column("long_description", sa.Text(), nullable=False), |
| 53 | + sa.Column("check_type", MalwareCheckTypes, nullable=False), |
| 54 | + sa.Column("hook_name", sa.String(), nullable=True), |
| 55 | + sa.Column( |
| 56 | + "state", MalwareCheckStates, server_default="disabled", nullable=False, |
| 57 | + ), |
| 58 | + sa.Column( |
| 59 | + "created", sa.DateTime(), server_default=sa.text("now()"), nullable=False |
| 60 | + ), |
| 61 | + sa.PrimaryKeyConstraint("id"), |
| 62 | + sa.UniqueConstraint("name", "version"), |
| 63 | + ) |
| 64 | + op.create_table( |
| 65 | + "malware_verdicts", |
| 66 | + sa.Column( |
| 67 | + "id", |
| 68 | + postgresql.UUID(as_uuid=True), |
| 69 | + server_default=sa.text("gen_random_uuid()"), |
| 70 | + nullable=False, |
| 71 | + ), |
| 72 | + sa.Column( |
| 73 | + "run_date", sa.DateTime(), server_default=sa.text("now()"), nullable=False |
| 74 | + ), |
| 75 | + sa.Column("check_id", postgresql.UUID(as_uuid=True), nullable=False), |
| 76 | + sa.Column("file_id", postgresql.UUID(as_uuid=True), nullable=False), |
| 77 | + sa.Column("classification", VerdictClassifications, nullable=False,), |
| 78 | + sa.Column("confidence", VerdictConfidences, nullable=False,), |
| 79 | + sa.Column("message", sa.Text(), nullable=True), |
| 80 | + sa.Column("details", postgresql.JSONB(astext_type=sa.Text()), nullable=True), |
| 81 | + sa.Column( |
| 82 | + "manually_reviewed", |
| 83 | + sa.Boolean(), |
| 84 | + server_default=sa.text("false"), |
| 85 | + nullable=False, |
| 86 | + ), |
| 87 | + sa.Column("administrator_verdict", VerdictClassifications, nullable=True,), |
| 88 | + sa.Column("full_report_link", sa.String(), nullable=True), |
| 89 | + sa.ForeignKeyConstraint( |
| 90 | + ["check_id"], ["malware_checks.id"], onupdate="CASCADE", ondelete="CASCADE" |
| 91 | + ), |
| 92 | + sa.ForeignKeyConstraint(["file_id"], ["release_files.id"]), |
| 93 | + sa.PrimaryKeyConstraint("id"), |
| 94 | + ) |
| 95 | + op.create_index( |
| 96 | + op.f("ix_malware_verdicts_check_id"), |
| 97 | + "malware_verdicts", |
| 98 | + ["check_id"], |
| 99 | + unique=False, |
| 100 | + ) |
| 101 | + |
| 102 | + |
| 103 | +def downgrade(): |
| 104 | + op.drop_index(op.f("ix_malware_verdicts_check_id"), table_name="malware_verdicts") |
| 105 | + op.drop_table("malware_verdicts") |
| 106 | + op.drop_table("malware_checks") |
| 107 | + MalwareCheckTypes.drop(op.get_bind()) |
| 108 | + MalwareCheckStates.drop(op.get_bind()) |
| 109 | + VerdictClassifications.drop(op.get_bind()) |
| 110 | + VerdictConfidences.drop(op.get_bind()) |
0 commit comments