Skip to content

Commit 85babea

Browse files
committed
adjust code and move script to separate file
1 parent 341746d commit 85babea

File tree

2 files changed

+102
-35
lines changed

2 files changed

+102
-35
lines changed

.github/workflows/reusable-build-test-release.yml

+30-35
Original file line numberDiff line numberDiff line change
@@ -406,13 +406,7 @@ jobs:
406406
- build
407407
- setup-workflow
408408
- setup
409-
410-
runs-on: ubuntu-latest
411-
strategy:
412-
fail-fast: false
413-
matrix:
414-
python-version:
415-
- "3.11"
409+
runs-on: large-ubuntu-22.04-32core
416410
permissions:
417411
actions: read
418412
deployments: read
@@ -421,16 +415,24 @@ jobs:
421415
statuses: read
422416
checks: write
423417
steps:
424-
- uses: actions/checkout@v4
425-
- uses: actions/setup-python@v5
418+
- name: Checkout TA
419+
uses: actions/checkout@v4
420+
421+
- name: Checkout Security Content
422+
uses: actions/checkout@v4
426423
with:
427-
python-version: ${{ matrix.python-version }}
424+
repository: splunk/security_content
425+
path: security_content
426+
ref: refs/heads/develop
428427

429-
- name: Install Python Dependencies and ContentCTL
428+
- uses: actions/setup-python@v5
429+
with:
430+
python-version: "3.11"
431+
432+
- name: Install dependencies
430433
run: |
431-
pip install contentctl
432-
git clone https://github.com/splunk/security_content.git
433-
434+
python -m pip install --upgrade pip
435+
pip install contentctl pyyaml
434436
435437
- name: Download TA Build Artifact
436438
uses: actions/download-artifact@v4
@@ -444,7 +446,7 @@ jobs:
444446
TA_BUILD_PATH="${{ github.workspace }}/ta_build/$TA_BUILD"
445447
echo "TA_BUILD_PATH=$TA_BUILD_PATH" >> $GITHUB_ENV
446448
447-
- name: Run Python Script
449+
- name: Filter ESCU Detections and swap TA to actual build
448450
id: filter-detection-files
449451
shell: python
450452
run: |
@@ -460,6 +462,8 @@ jobs:
460462
config.read("package/default/app.conf")
461463
APP_ID = config.get("id", "name")
462464
APP_LABEL = config.get("ui", "label")
465+
466+
print(f"APP_ID = {APP_ID}, APP_LABEL = {APP_LABEL}")
463467
464468
# Read the file and remove trailing backslashes
465469
with open("package/default/props.conf", "r") as f:
@@ -480,18 +484,15 @@ jobs:
480484
# Load the YAML content
481485
with open("security_content/contentctl.yml", "r") as file:
482486
data = yaml.safe_load(file)
483-
484-
found = False
485-
487+
488+
app_found = False
486489
for app in data["apps"]:
487-
if app['appid'] == APP_ID or GITHUB_REPOSITORY in app['hardcoded_path'] or app["title"] == APP_LABEL:
488-
app['hardcoded_path'] = "${{ env.TA_BUILD_PATH }}"
489-
found = True
490-
elif app['appid'] == "PALO_ALTO_NETWORKS_ADD_ON_FOR_SPLUNK" and APP_ID == "Splunk_TA_paloalto_networks":
490+
if app['appid'] == APP_ID or APP_ID in app['hardcoded_path'] or GITHUB_REPOSITORY in app['hardcoded_path'] or app["title"] == APP_LABEL or (app['appid'] == "PALO_ALTO_NETWORKS_ADD_ON_FOR_SPLUNK" and APP_ID == "Splunk_TA_paloalto_networks"):
491491
app['hardcoded_path'] = "${{ env.TA_BUILD_PATH }}"
492-
found = True
493-
494-
if not found:
492+
app_found = True
493+
494+
if not app_found:
495+
print(f"App not found in contentctl.yml file. Exiting.")
495496
exit(127)
496497
497498
@@ -506,14 +507,11 @@ jobs:
506507
for root, dirs, files in os.walk(base_dir):
507508
for file in files:
508509
file_path = os.path.join(root, file)
509-
510510
try:
511-
with open(file_path, "r") as file:
512-
file_content = yaml.safe_load(file)
511+
with open(file_path, "r") as yaml_file:
512+
file_content = yaml.safe_load(yaml_file)
513513
if "deprecated" not in file_path and (file_content["tests"][0]["attack_data"][0]["sourcetype"] in sourcetypes or file_content["tests"][0]["attack_data"][0]["source"] in sourcetypes):
514514
detection_files += file_path.replace("security_content/", "") + " "
515-
516-
517515
except Exception as e:
518516
continue
519517
@@ -525,19 +523,16 @@ jobs:
525523
526524
- name: Run ESCU Tests
527525
run: |
528-
529526
cd security_content
530-
echo "Content of contentctl.yml file"
527+
echo "Content of contentctl.yml file: "
531528
cat contentctl.yml
532-
533529
contentctl test --container-settings.num-containers 8 --post-test-behavior never_pause --disable-tqdm mode:selected --mode.files ${{ steps.filter-detection-files.outputs.DETECTION_FILES }}
534530
535531
- uses: actions/upload-artifact@v4
536532
if: always()
537533
with:
538534
name: escu_test_summary_results
539-
path: |
540-
security_content/test_results/summary.yml
535+
path: security_content/test_results/summary.yml
541536

542537
run-unit-tests:
543538
name: test-unit-python3-${{ matrix.python-version }}

scripts/filter_escu_detections.py

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import yaml
2+
import os
3+
import configparser
4+
import re
5+
6+
GITHUB_REPOSITORY = os.environ.get("GITHUB_REPOSITORY", "")
7+
8+
# Parse app.conf get the appid of the TA.
9+
config = configparser.ConfigParser(strict=False)
10+
config.read("package/default/app.conf")
11+
APP_ID = config.get("id", "name")
12+
APP_LABEL = config.get("ui", "label")
13+
14+
print(f"APP_ID = {APP_ID}, APP_LABEL = {APP_LABEL}")
15+
16+
# Read the file and remove trailing backslashes
17+
with open("package/default/props.conf", "r") as f:
18+
content = f.read()
19+
20+
# Remove trailing backslashes followed by a newline
21+
updated_content = re.sub(r"\\\n", "", content)
22+
23+
# Write the cleaned content to a new file
24+
with open("package/default/props.conf", "w") as f:
25+
f.write(updated_content)
26+
27+
# Parse props.conf and collect all the sourcetypes in a list.
28+
config = configparser.ConfigParser(strict=False)
29+
config.read("package/default/props.conf")
30+
sourcetypes = config.sections()
31+
32+
# Load the YAML content
33+
with open("security_content/contentctl.yml", "r") as file:
34+
data = yaml.safe_load(file)
35+
36+
app_found = False
37+
for app in data["apps"]:
38+
if app['appid'] == APP_ID or APP_ID in app['hardcoded_path'] or GITHUB_REPOSITORY in app['hardcoded_path'] or app["title"] == APP_LABEL or (app['appid'] == "PALO_ALTO_NETWORKS_ADD_ON_FOR_SPLUNK" and APP_ID == "Splunk_TA_paloalto_networks"):
39+
app['hardcoded_path'] = "${{ env.TA_BUILD_PATH }}"
40+
app_found = True
41+
42+
if not app_found:
43+
print(f"App not found in contentctl.yml file. Exiting.")
44+
exit(127)
45+
46+
# Write the modified data to the contentctl.yml file
47+
with open("security_content/contentctl.yml", "w") as file:
48+
yaml.dump(data, file, sort_keys=False)
49+
50+
# Filter out the detections based on the collected sourcetypes
51+
base_dir = "security_content/detections"
52+
detection_files = ""
53+
54+
for root, dirs, files in os.walk(base_dir):
55+
for file in files:
56+
file_path = os.path.join(root, file)
57+
58+
try:
59+
with open(file_path, "r") as yaml_file:
60+
file_content = yaml.safe_load(yaml_file)
61+
if "deprecated" not in file_path and (
62+
file_content["tests"][0]["attack_data"][0]["sourcetype"] in sourcetypes or file_content["tests"][0]["attack_data"][0]["source"] in sourcetypes):
63+
detection_files += file_path.replace("security_content/", "") + " "
64+
65+
except Exception as e:
66+
continue
67+
68+
# Save detection_files as an output variable
69+
with open(os.getenv('GITHUB_OUTPUT'), 'w') as output_file:
70+
output_file.write(f"DETECTION_FILES={detection_files}")
71+
72+
print(f"Filtered Detection files = {detection_files}")

0 commit comments

Comments
 (0)