|
| 1 | +import uuid |
| 2 | +import string |
| 3 | +import requests |
| 4 | +import time |
| 5 | +import sys |
| 6 | + |
| 7 | +from pydantic import BaseModel, validator, root_validator, Extra |
| 8 | +from dataclasses import dataclass |
| 9 | +from typing import Union |
| 10 | +from datetime import datetime, timedelta |
| 11 | + |
| 12 | + |
| 13 | +from contentctl.objects.security_content_object import SecurityContentObject |
| 14 | +from contentctl.objects.enums import AnalyticsType |
| 15 | +from contentctl.objects.enums import DataModel |
| 16 | +from contentctl.objects.enums import DetectionStatus |
| 17 | +from contentctl.objects.detection_tags import DetectionTags |
| 18 | +from contentctl.objects.config import ConfigDetectionConfiguration |
| 19 | +from contentctl.objects.unit_test import UnitTest |
| 20 | +from contentctl.objects.macro import Macro |
| 21 | +from contentctl.objects.lookup import Lookup |
| 22 | +from contentctl.objects.baseline import Baseline |
| 23 | +from contentctl.objects.playbook import Playbook |
| 24 | +from contentctl.helper.link_validator import LinkValidator |
| 25 | +from contentctl.objects.enums import SecurityContentType |
| 26 | + |
| 27 | + |
| 28 | +class Detection_Abstract(SecurityContentObject): |
| 29 | + contentType: SecurityContentType = SecurityContentType.detections |
| 30 | + type: str |
| 31 | + status: DetectionStatus |
| 32 | + data_source: list[str] |
| 33 | + search: Union[str, dict] |
| 34 | + how_to_implement: str |
| 35 | + known_false_positives: str |
| 36 | + check_references: bool = False |
| 37 | + references: list |
| 38 | + tags: DetectionTags |
| 39 | + tests: list[UnitTest] = [] |
| 40 | + |
| 41 | + # enrichments |
| 42 | + datamodel: list = None |
| 43 | + deprecated: bool = None |
| 44 | + experimental: bool = None |
| 45 | + deployment: ConfigDetectionConfiguration = None |
| 46 | + annotations: dict = None |
| 47 | + risk: list = None |
| 48 | + playbooks: list[Playbook] = None |
| 49 | + baselines: list[Baseline] = None |
| 50 | + mappings: dict = None |
| 51 | + macros: list[Macro] = None |
| 52 | + lookups: list[Lookup] = None |
| 53 | + cve_enrichment: list = None |
| 54 | + splunk_app_enrichment: list = None |
| 55 | + file_path: str = None |
| 56 | + source: str = None |
| 57 | + nes_fields: str = None |
| 58 | + providing_technologies: list = None |
| 59 | + runtime: str = None |
| 60 | + |
| 61 | + class Config: |
| 62 | + use_enum_values = True |
| 63 | + |
| 64 | + @validator("type") |
| 65 | + def type_valid(cls, v, values): |
| 66 | + if v.lower() not in [el.name.lower() for el in AnalyticsType]: |
| 67 | + raise ValueError("not valid analytics type: " + values["name"]) |
| 68 | + return v |
| 69 | + |
| 70 | + @validator('how_to_implement') |
| 71 | + def encode_error(cls, v, values, field): |
| 72 | + return SecurityContentObject.free_text_field_valid(cls,v,values,field) |
| 73 | + |
| 74 | + # @root_validator |
| 75 | + # def search_validation(cls, values): |
| 76 | + # if 'ssa_' not in values['file_path']: |
| 77 | + # if not '_filter' in values['search']: |
| 78 | + # raise ValueError('filter macro missing in: ' + values["name"]) |
| 79 | + # if any(x in values['search'] for x in ['eventtype=', 'sourcetype=', ' source=', 'index=']): |
| 80 | + # if not 'index=_internal' in values['search']: |
| 81 | + # raise ValueError('Use source macro instead of eventtype, sourcetype, source or index in detection: ' + values["name"]) |
| 82 | + # return values |
| 83 | + |
| 84 | + # disable it because of performance reasons |
| 85 | + # @validator('references') |
| 86 | + # def references_check(cls, v, values): |
| 87 | + # return LinkValidator.check_references(v, values["name"]) |
| 88 | + # return v |
| 89 | + |
| 90 | + |
| 91 | + @validator("search") |
| 92 | + def search_validate(cls, v, values): |
| 93 | + # write search validator |
| 94 | + return v |
| 95 | + |
| 96 | + @validator("tests") |
| 97 | + def tests_validate(cls, v, values): |
| 98 | + if values.get("status","") != DetectionStatus.production and not v: |
| 99 | + raise ValueError( |
| 100 | + "tests value is needed for production detection: " + values["name"] |
| 101 | + ) |
| 102 | + return v |
| 103 | + |
| 104 | + @validator("experimental", always=True) |
| 105 | + def experimental_validate(cls, v, values): |
| 106 | + if DetectionStatus(values.get("status","")) == DetectionStatus.experimental: |
| 107 | + return True |
| 108 | + return False |
| 109 | + |
| 110 | + @validator("deprecated", always=True) |
| 111 | + def deprecated_validate(cls, v, values): |
| 112 | + if DetectionStatus(values.get("status","")) == DetectionStatus.deprecated: |
| 113 | + return True |
| 114 | + return False |
| 115 | + |
| 116 | + @validator("datamodel") |
| 117 | + def datamodel_valid(cls, v, values): |
| 118 | + for datamodel in v: |
| 119 | + if datamodel not in [el.name for el in DataModel]: |
| 120 | + raise ValueError("not valid data model: " + values["name"]) |
| 121 | + return v |
| 122 | + |
| 123 | + def all_tests_successful(self) -> bool: |
| 124 | + if len(self.tests) == 0: |
| 125 | + return False |
| 126 | + for test in self.tests: |
| 127 | + if test.result is None or test.result.success == False: |
| 128 | + return False |
| 129 | + return True |
| 130 | + |
| 131 | + def get_summary( |
| 132 | + self, |
| 133 | + detection_fields: list[str] = ["name", "search"], |
| 134 | + test_model_fields: list[str] = ["success", "message"], |
| 135 | + test_job_fields: list[str] = ["resultCount", "runDuration"], |
| 136 | + ) -> dict: |
| 137 | + summary_dict = {} |
| 138 | + for field in detection_fields: |
| 139 | + summary_dict[field] = getattr(self, field) |
| 140 | + summary_dict["success"] = self.all_tests_successful() |
| 141 | + summary_dict["tests"] = [] |
| 142 | + for test in self.tests: |
| 143 | + result: dict[str, Union[str, bool]] = {"name": test.name} |
| 144 | + if test.result is not None: |
| 145 | + result.update( |
| 146 | + test.result.get_summary_dict( |
| 147 | + model_fields=test_model_fields, |
| 148 | + job_fields=test_job_fields, |
| 149 | + ) |
| 150 | + ) |
| 151 | + else: |
| 152 | + result["success"] = False |
| 153 | + result["message"] = "RESULT WAS NONE" |
| 154 | + |
| 155 | + summary_dict["tests"].append(result) |
| 156 | + |
| 157 | + return summary_dict |
0 commit comments