Skip to content

Commit 46378f7

Browse files
authored
Merge pull request #371 from splunk/fix_default_match_type
Wrong type for lookup.default_match Since this is a small change and I have quantified the differences I am merging myself.
2 parents 7e59f18 + c18dc0a commit 46378f7

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

contentctl/objects/abstract_security_content_objects/detection_abstract.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ def serialize_model(self):
474474
"name": lookup.name,
475475
"description": lookup.description,
476476
"filename": lookup.filename.name,
477-
"default_match": "true" if lookup.default_match else "false",
477+
"default_match": lookup.default_match,
478478
"case_sensitive_match": "true"
479479
if lookup.case_sensitive_match
480480
else "false",

contentctl/objects/lookup.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
import re
77
from enum import StrEnum, auto
88
from functools import cached_property
9-
from typing import TYPE_CHECKING, Annotated, Any, Literal, Optional, Self
9+
from typing import TYPE_CHECKING, Annotated, Any, Literal, Self
1010

1111
from pydantic import (
12+
BeforeValidator,
1213
Field,
1314
FilePath,
1415
NonNegativeInt,
@@ -69,7 +70,19 @@ class Lookup_Type(StrEnum):
6970

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

89102
# All fields custom to this model
90103
model = {
91-
"default_match": "true" if self.default_match is True else "false",
104+
"default_match": self.default_match,
92105
"match_type": self.match_type_to_conf_format,
93106
"min_matches": self.min_matches,
94107
"max_matches": self.max_matches,

contentctl/output/templates/transforms.j2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ filename = {{ lookup.app_filename.name }}
77
collection = {{ lookup.collection }}
88
external_type = kvstore
99
{% endif %}
10-
{% if lookup.default_match is defined and lookup.default_match != None %}
11-
default_match = {{ lookup.default_match | lower }}
10+
{% if lookup.default_match != '' %}
11+
default_match = {{ lookup.default_match }}
1212
{% endif %}
1313
{% if lookup.case_sensitive_match is defined and lookup.case_sensitive_match != None %}
1414
case_sensitive_match = {{ lookup.case_sensitive_match | lower }}

0 commit comments

Comments
 (0)