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

Wrong type for lookup.default_match #371

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
38 changes: 18 additions & 20 deletions contentctl/input/director.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
import os
import sys
from pathlib import Path
from dataclasses import dataclass, field
from pydantic import ValidationError
from pathlib import Path
from uuid import UUID
from contentctl.input.yml_reader import YmlReader

from contentctl.objects.detection import Detection
from contentctl.objects.story import Story
from pydantic import ValidationError

from contentctl.objects.baseline import Baseline
from contentctl.objects.investigation import Investigation
from contentctl.objects.playbook import Playbook
from contentctl.objects.deployment import Deployment
from contentctl.objects.macro import Macro
from contentctl.objects.lookup import LookupAdapter, Lookup
from contentctl.objects.atomic import AtomicEnrichment
from contentctl.objects.security_content_object import SecurityContentObject
from contentctl.objects.data_source import DataSource
from contentctl.objects.dashboard import Dashboard
from contentctl.enrichments.attack_enrichment import AttackEnrichment
from contentctl.enrichments.cve_enrichment import CveEnrichment

from contentctl.helper.utils import Utils
from contentctl.input.yml_reader import YmlReader
from contentctl.objects.atomic import AtomicEnrichment
from contentctl.objects.baseline import Baseline
from contentctl.objects.config import validate
from contentctl.objects.dashboard import Dashboard
from contentctl.objects.data_source import DataSource
from contentctl.objects.deployment import Deployment
from contentctl.objects.detection import Detection
from contentctl.objects.enums import SecurityContentType
from contentctl.helper.utils import Utils
from contentctl.objects.investigation import Investigation
from contentctl.objects.lookup import Lookup, LookupAdapter
from contentctl.objects.macro import Macro
from contentctl.objects.playbook import Playbook
from contentctl.objects.security_content_object import SecurityContentObject
from contentctl.objects.story import Story


@dataclass
Expand Down Expand Up @@ -113,9 +112,8 @@ def execute(self, input_dto: validate) -> None:
self.createSecurityContent(SecurityContentType.detections)
self.createSecurityContent(SecurityContentType.dashboards)

from contentctl.objects.abstract_security_content_objects.detection_abstract import (
MISSING_SOURCES,
)
from contentctl.objects.abstract_security_content_objects.detection_abstract import \
MISSING_SOURCES

if len(MISSING_SOURCES) > 0:
missing_sources_string = "\n 🟡 ".join(sorted(list(MISSING_SOURCES)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ def serialize_model(self):
"name": lookup.name,
"description": lookup.description,
"filename": lookup.filename.name,
"default_match": "true" if lookup.default_match else "false",
"default_match": lookup.default_match,
"case_sensitive_match": "true"
if lookup.case_sensitive_match
else "false",
Expand Down
19 changes: 16 additions & 3 deletions contentctl/objects/lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
import re
from enum import StrEnum, auto
from functools import cached_property
from typing import TYPE_CHECKING, Annotated, Any, Literal, Optional, Self
from typing import TYPE_CHECKING, Annotated, Any, Literal, Self

from pydantic import (
BeforeValidator,
Field,
FilePath,
NonNegativeInt,
Expand Down Expand Up @@ -69,7 +70,19 @@ class Lookup_Type(StrEnum):

# TODO (#220): Split Lookup into 2 classes
class Lookup(SecurityContentObject, abc.ABC):
default_match: Optional[bool] = None
# We need to make sure that this is converted to a string because we widely
# use the string "False" in our lookup content. However, PyYAML reads this
# as a BOOL and this causes parsing to fail. As such, we will always
# convert this to a string if it is passed as a bool
default_match: Annotated[
str, BeforeValidator(lambda dm: str(dm) if isinstance(dm, bool) else dm)
] = Field(
default="",
description="This field is given a default value of ''"
"because it is the default value specified in the transforms.conf "
"docs. Giving it a type of str rather than str | None simplifies "
"the typing for the field.",
)
# Per the documentation for transforms.conf, EXACT should not be specified in this list,
# so we include only WILDCARD and CIDR
match_type: list[Annotated[str, Field(pattern=r"(^WILDCARD|CIDR)\(.+\)$")]] = Field(
Expand All @@ -88,7 +101,7 @@ def serialize_model(self):

# All fields custom to this model
model = {
"default_match": "true" if self.default_match is True else "false",
"default_match": self.default_match,
"match_type": self.match_type_to_conf_format,
"min_matches": self.min_matches,
"max_matches": self.max_matches,
Expand Down
4 changes: 2 additions & 2 deletions contentctl/output/templates/transforms.j2
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ filename = {{ lookup.app_filename.name }}
collection = {{ lookup.collection }}
external_type = kvstore
{% endif %}
{% if lookup.default_match is defined and lookup.default_match != None %}
default_match = {{ lookup.default_match | lower }}
{% if lookup.default_match != '' %}
default_match = {{ lookup.default_match }}
{% endif %}
{% if lookup.case_sensitive_match is defined and lookup.case_sensitive_match != None %}
case_sensitive_match = {{ lookup.case_sensitive_match | lower }}
Expand Down
Loading