Skip to content

Commit 9321fe3

Browse files
authored
Merge pull request #218 from splunk/data_source_validation_2
Tweaks to Data Source Validation
2 parents 3e95c17 + e51b688 commit 9321fe3

File tree

3 files changed

+35
-22
lines changed

3 files changed

+35
-22
lines changed

contentctl/actions/validate.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11

22
import pathlib
3+
4+
import urllib3.util
35
from contentctl.input.director import Director, DirectorOutputDto
46
from contentctl.objects.config import validate
57
from contentctl.enrichments.attack_enrichment import AttackEnrichment
@@ -36,10 +38,8 @@ def execute(self, input_dto: validate) -> DirectorOutputDto:
3638
director.execute(input_dto)
3739
self.ensure_no_orphaned_files_in_lookups(input_dto.path, director_output_dto)
3840
if input_dto.data_source_TA_validation:
39-
if self.validate_latest_TA_information(director_output_dto.data_sources) != 1:
40-
print("All TA versions are up to date.")
41-
else:
42-
raise Exception("One or more TA versions are out of date. Please update the data source with the latest version.")
41+
self.validate_latest_TA_information(director_output_dto.data_sources)
42+
4343
return director_output_dto
4444

4545

@@ -81,27 +81,35 @@ def ensure_no_orphaned_files_in_lookups(self, repo_path:pathlib.Path, director_o
8181
return
8282

8383

84-
def validate_latest_TA_information(self, data_sources: list[DataSource]) -> int:
84+
def validate_latest_TA_information(self, data_sources: list[DataSource]) -> None:
8585
validated_TAs: list[tuple[str, str]] = []
86-
error_occurred = False
86+
errors:list[str] = []
8787
print("----------------------")
8888
print("Validating latest TA:")
8989
print("----------------------")
9090
for data_source in data_sources:
9191
for supported_TA in data_source.supported_TA:
92-
ta_identifier = (supported_TA["name"], supported_TA["version"])
92+
ta_identifier = (supported_TA.name, supported_TA.version)
9393
if ta_identifier in validated_TAs:
9494
continue
95-
if "url" in supported_TA:
95+
if supported_TA.url is not None:
9696
validated_TAs.append(ta_identifier)
97-
uid = int(supported_TA["url"].rstrip('/').split("/")[-1])
97+
uid = int(str(supported_TA.url).rstrip('/').split("/")[-1])
9898
try:
9999
splunk_app = SplunkApp(app_uid=uid)
100-
if splunk_app.latest_version != supported_TA["version"]:
101-
raise Exception(f"Version mismatch for TA {supported_TA['name']}: "
102-
f"Latest version on Splunkbase is {splunk_app.latest_version}, "
103-
f"but version {supported_TA['version']} is specified in the data source {data_source.name}.")
100+
if splunk_app.latest_version != supported_TA.version:
101+
errors.append(f"Version mismatch in '{data_source.file_path}' supported TA '{supported_TA.name}'"
102+
f"\n Latest version on Splunkbase : {splunk_app.latest_version}"
103+
f"\n Version specified in data source: {supported_TA.version}")
104104
except Exception as e:
105-
print(f"Error processing TA {supported_TA['name']}: {str(e)}")
106-
error_occurred = True
107-
return 1 if error_occurred else 0
105+
errors.append(f"Error processing checking version of TA {supported_TA.name}: {str(e)}")
106+
107+
if len(errors) > 0:
108+
errorString = '\n\n'.join(errors)
109+
raise Exception(f"[{len(errors)}] or more TA versions are out of date or have other errors."
110+
f"Please update the following data sources with the latest versions of "
111+
f"their supported tas:\n\n{errorString}")
112+
print("All TA versions are up to date.")
113+
114+
115+

contentctl/objects/data_source.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
from __future__ import annotations
22
from typing import Optional, Any
3-
from pydantic import Field, FilePath, model_serializer
3+
from pydantic import Field, HttpUrl, model_serializer, BaseModel
44
from contentctl.objects.security_content_object import SecurityContentObject
55
from contentctl.objects.event_source import EventSource
66

7+
8+
class TA(BaseModel):
9+
name: str
10+
url: HttpUrl | None = None
11+
version: str
712
class DataSource(SecurityContentObject):
813
source: str = Field(...)
914
sourcetype: str = Field(...)
1015
separator: Optional[str] = None
1116
configuration: Optional[str] = None
12-
supported_TA: Optional[list] = None
17+
supported_TA: list[TA] = []
1318
fields: Optional[list] = None
1419
field_mappings: Optional[list] = None
1520
convert_to_log_source: Optional[list] = None

contentctl/output/data_source_writer.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ def writeDataSourceCsv(data_source_objects: List[DataSource], file_path: pathlib
1818
])
1919
# Write the data
2020
for data_source in data_source_objects:
21-
if data_source.supported_TA and isinstance(data_source.supported_TA, list) and len(data_source.supported_TA) > 0:
22-
supported_TA_name = data_source.supported_TA[0].get('name', '')
23-
supported_TA_version = data_source.supported_TA[0].get('version', '')
24-
supported_TA_url = data_source.supported_TA[0].get('url', '')
21+
if len(data_source.supported_TA) > 0:
22+
supported_TA_name = data_source.supported_TA[0].name
23+
supported_TA_version = data_source.supported_TA[0].version
24+
supported_TA_url = data_source.supported_TA[0].url or ''
2525
else:
2626
supported_TA_name = ''
2727
supported_TA_version = ''

0 commit comments

Comments
 (0)