Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
207 commits
Select commit Hold shift + click to select a range
a6c3052
Update all libraries
pyth0n1c Nov 20, 2023
b14dcd6
beginning cleanup and conversion
pyth0n1c Nov 20, 2023
b82e943
Update Lookup Class definition.
pyth0n1c Nov 21, 2023
2ed68d4
Converted some more objects types for pydantic2
pyth0n1c Nov 29, 2023
d694721
Lots of additional progress updating
pyth0n1c Nov 29, 2023
5cf211f
More model update cleanup
pyth0n1c Nov 30, 2023
d253246
Significant updates to the
pyth0n1c Dec 1, 2023
054953e
Lots more progress on pydantic 2 conversion.
pyth0n1c Dec 7, 2023
cb1b854
Many changes to the Detection Object
pyth0n1c Dec 7, 2023
80f3f72
Updates to Investigation Objects.
pyth0n1c Dec 8, 2023
153ec9d
Fix up circular import
pyth0n1c Dec 8, 2023
5784897
Remove UnitTestTest references and ability
pyth0n1c Dec 8, 2023
5f7c2c1
add correct default values (None)
pyth0n1c Dec 11, 2023
c10a8b2
convert all parse_obj to model_validate
pyth0n1c Dec 13, 2023
46758e7
Big cleanup of unused imports and other
pyth0n1c Dec 13, 2023
233d6c0
Lots of playbook/tags cleanup, but need to verify
pyth0n1c Dec 15, 2023
3942ae9
Much better method for enriching
pyth0n1c Dec 20, 2023
8af4a31
Another huge set of changes to improve
pyth0n1c Dec 20, 2023
c9a44ac
Validate asset_type against enum.
pyth0n1c Dec 21, 2023
a618b56
A handful of extra validaitons and
pyth0n1c Dec 22, 2023
cb34319
Add streamlit and streamlit-pydantic as deps.
pyth0n1c Jan 3, 2024
8634686
Filter out comments from
pyth0n1c Jan 3, 2024
358e4a0
Progress on baselines.
pyth0n1c Jan 4, 2024
775486e
Lots more updates to simplify builder
pyth0n1c Jan 5, 2024
9bbf5ee
More cleanup as we reduce code
pyth0n1c Jan 6, 2024
5983f9b
More refinements to intelligently parse
pyth0n1c Jan 12, 2024
ec0b092
Improve build command with object
pyth0n1c Jan 16, 2024
7d7a653
conf writer troubleshooting.
pyth0n1c Jan 25, 2024
a957e37
More work for atomic guid mapping to work
pyth0n1c Jan 26, 2024
c18b6ab
Atomic enrichment working
pyth0n1c Jan 26, 2024
67da3fe
improvements to some fields
pyth0n1c Jan 30, 2024
c6171da
Minor updates to field tpyes
pyth0n1c Jan 30, 2024
88bcc59
Easier enrichment of security content
pyth0n1c Feb 26, 2024
f2a4f40
Add appropriate references in stories and investigations.
pyth0n1c Feb 27, 2024
ac24b9b
Updated template files to work with new
pyth0n1c Feb 27, 2024
d8cdbde
Update YML tempaltes in line
pyth0n1c Feb 27, 2024
c5ec580
fix checkout of art repo
pyth0n1c Feb 27, 2024
c1c8edd
Automatically populate macros for detection.
pyth0n1c Feb 28, 2024
16c97e7
full test
pyth0n1c Feb 28, 2024
b21a1cc
minor workflow update
pyth0n1c Feb 28, 2024
5937637
use init again
pyth0n1c Feb 28, 2024
6befe7c
Rewrite format of app.py object
pyth0n1c Feb 29, 2024
5bdd0cb
improvements to how apps are enumerated
pyth0n1c Feb 29, 2024
c1cd36e
More small changes to app.
pyth0n1c Mar 1, 2024
7bcb6bb
Reshuffled imports.
pyth0n1c Mar 2, 2024
398c6cc
init works - fixed serialization
pyth0n1c Mar 2, 2024
25fd538
Validate mostly working!
pyth0n1c Mar 2, 2024
fdd98b9
resolve import/model construction
pyth0n1c Mar 2, 2024
a920b13
Improve how generate writes app files
pyth0n1c Mar 2, 2024
357caca
More progress on building app
pyth0n1c Mar 2, 2024
4735385
finished updating conf_output.py.
pyth0n1c Mar 2, 2024
87c14d2
Finished main updates to build, but
pyth0n1c Mar 2, 2024
c0fcec4
Fix minor issue with directory creation
pyth0n1c Mar 2, 2024
2596e83
Fix field names for serializing
pyth0n1c Mar 4, 2024
f083ec4
fix case for new content enum cli
pyth0n1c Mar 4, 2024
bcf9adc
Add annotation to appid field
pyth0n1c Mar 4, 2024
f54c2b8
Remove unused DataManipulation class
pyth0n1c Mar 4, 2024
249e793
Rework how init pulls in templates. This way,
pyth0n1c Mar 6, 2024
6a43f79
update generate to build
pyth0n1c Mar 7, 2024
6e7a8fa
Fix broken macro enrichment.
pyth0n1c Mar 9, 2024
1e6b453
add pygit2 to baseline. remove
pyth0n1c Mar 11, 2024
feae56b
continuing to work on cli
pyth0n1c Mar 12, 2024
8fb7dc9
most fix acs deploy parsing
pyth0n1c Mar 12, 2024
d3dc9cd
Migrate INSPECT to its own command
pyth0n1c Mar 25, 2024
6e02e12
Improve enums in Playbooks.
pyth0n1c Apr 1, 2024
a6bc860
Build can now build MULTIPLE outputs
pyth0n1c Apr 3, 2024
8e2e46d
Big updates to build process and validation of text fields. Now we ca…
pyth0n1c Apr 9, 2024
2f4a60c
Minor newline fix in rendering XML file header
pyth0n1c Apr 9, 2024
af4d35d
Continued improvements to remove usage of
pyth0n1c Apr 10, 2024
097a4da
porting MITRE ATT&CK perf improvement
ljstella Apr 11, 2024
fc84382
update pyproject with updated dependencies. now uses newest tyro, res…
pyth0n1c Apr 11, 2024
38338f1
update pyproject with updated dependencies. now uses newest tyro, res…
pyth0n1c Apr 11, 2024
d84e2b8
Fix errors during jinja2
pyth0n1c Apr 12, 2024
0c5d9cf
Add extra annotation to description
pyth0n1c Apr 12, 2024
e25c46d
Improve .conf generation
pyth0n1c Apr 15, 2024
7981187
Tidy up providing_technologies, mitre_attack_enrichment.
pyth0n1c Apr 16, 2024
4fb26dd
Lost a commit
ljstella Apr 16, 2024
2828fe1
Improvements to generate risk-related
pyth0n1c Apr 16, 2024
fa3c7aa
Fix errors during jinja2
pyth0n1c Apr 12, 2024
ec730a6
Add extra annotation to description
pyth0n1c Apr 12, 2024
57ff0e7
Improve .conf generation
pyth0n1c Apr 15, 2024
42cfdd9
Tidy up providing_technologies, mitre_attack_enrichment.
pyth0n1c Apr 16, 2024
63fdf9d
Improvements to generate risk-related
pyth0n1c Apr 16, 2024
375853a
remove --unattended flag from contentctl test
pyth0n1c Apr 16, 2024
0109a99
Merge pull request #133 from splunk/porting-mitre-fix
pyth0n1c Apr 16, 2024
943dd3a
standardization + slight cleanup
ljstella Apr 17, 2024
a1e79a7
Accidentally a line
ljstella Apr 17, 2024
252264f
remove legacy code and move sorting logic
pyth0n1c Apr 17, 2024
70e84ff
Merge branch 'pydantic2' of https://github.com/splunk/contentctl into…
pyth0n1c Apr 17, 2024
e169642
Improve/deduplicate logic for generating annotations
pyth0n1c Apr 17, 2024
e546e23
Added CVE and GROUPS to the StoryTags Schema.
pyth0n1c Apr 18, 2024
9a9d4a7
Fix typo in check for existence of
pyth0n1c Apr 18, 2024
2eace71
Remove legacy builders
pyth0n1c Apr 18, 2024
3192cb8
Finish cleanup of most builders except ssa_builder.
pyth0n1c Apr 19, 2024
3330270
Forgot to include updates to one
pyth0n1c Apr 19, 2024
6923ab5
Improvents around enrichment
pyth0n1c Apr 20, 2024
975fd09
updating to pydantic2
patel-bhavin Apr 22, 2024
cbe1383
Merge pull request #137 from splunk/dashboard_link_2
ljstella Apr 22, 2024
37300d5
Improve serialization to json target
pyth0n1c Apr 22, 2024
64fdffa
Merge branch 'pydantic2' of https://github.com/splunk/contentctl into…
pyth0n1c Apr 22, 2024
b1aadad
comment out story category warning
pyth0n1c Apr 22, 2024
d902a7e
Improve jinja2 templates, serialization
pyth0n1c Apr 22, 2024
82bb4f4
minor improvements in output serialization
pyth0n1c Apr 22, 2024
c25d06e
Merge from all updates in latest commit to main. There will be furthe…
pyth0n1c Apr 22, 2024
ccf1e8e
Significant hand-cleanup to get contentctl init
pyth0n1c Apr 22, 2024
98405b8
Resolve extremely tricky test construction
pyth0n1c Apr 23, 2024
a841880
Fix minor capitalization in KillChain
pyth0n1c Apr 23, 2024
01a18b5
Support for defining custom
pyth0n1c Apr 23, 2024
cfe6178
switch from ConfigParser to RawConfigParser
pyth0n1c Apr 23, 2024
53d7e5d
Add type and version fields
pyth0n1c Apr 23, 2024
d47e278
Fix output of es_investigations.conf
pyth0n1c Apr 23, 2024
9a7fafb
Improve generation of analytic_stories.conf
pyth0n1c Apr 23, 2024
d2a383d
Tidy up macros.j2. Remove
pyth0n1c Apr 23, 2024
a857cba
Update transforms.j2 to maintain
pyth0n1c Apr 23, 2024
4d6d47e
minor updates to deployment
pyth0n1c Apr 23, 2024
0879aa3
improve detection.json
pyth0n1c Apr 23, 2024
0e7b7bf
Improve investigation and lookup
pyth0n1c Apr 23, 2024
3cd7b63
Final tweaks to story serialization
pyth0n1c Apr 23, 2024
ada1e17
stories.json now at parity
pyth0n1c Apr 23, 2024
116a3d5
add back gitpython
pyth0n1c Apr 23, 2024
b5fec06
clean up in prep to merge testing
pyth0n1c Apr 23, 2024
ac6b0d9
Remove extra fields from macros.conf.
pyth0n1c Apr 23, 2024
dd4b150
include metadata from main PR
pyth0n1c Apr 23, 2024
35fc10b
Fix incorrect spacing in macros.conf
pyth0n1c Apr 23, 2024
71e6d89
improvements to TEST workflow
pyth0n1c Apr 23, 2024
b05dee8
More updates to improve test functionality
pyth0n1c Apr 24, 2024
8dfe4e3
Fix up environment string and app + app
pyth0n1c Apr 24, 2024
fba648d
Adding shell tab complete to readme
ljstella Apr 24, 2024
13bb1bc
Ported manual_test logic and filtering on
pyth0n1c Apr 24, 2024
0ffe595
Merge branch 'pydantic2' of https://github.com/splunk/contentctl into…
pyth0n1c Apr 24, 2024
ff71d8e
Improve build speed by around 1 second.
pyth0n1c Apr 24, 2024
5fdf662
fixed mistake causing SSA validates
pyth0n1c Apr 24, 2024
7002948
Validate now running, but throwing errors
pyth0n1c Apr 24, 2024
45e41a3
SSA validation completes after some updates.
pyth0n1c Apr 25, 2024
996df6b
ssa built with no errors!
pyth0n1c Apr 25, 2024
e7dc258
Final cleanup for different
pyth0n1c Apr 25, 2024
907de08
Update to get SSA and app valdiation
pyth0n1c Apr 25, 2024
508f7b3
Allow validate/build to take place
pyth0n1c Apr 25, 2024
ecd5bbb
generic contentctl debug config for vscode
ljstella Apr 25, 2024
f11848f
Better formatting for ART parse errors
pyth0n1c Apr 25, 2024
a08b620
include new launch.json
pyth0n1c Apr 25, 2024
2e3d3e5
Turning missing ART atomics from
pyth0n1c Apr 25, 2024
f019d88
fix launch.json config
pyth0n1c Apr 25, 2024
d602df5
init was failing because we could not
pyth0n1c Apr 25, 2024
5742e72
Make sure that docker is running
pyth0n1c Apr 25, 2024
6871d3d
bump to latest version of splunk-sdk
pyth0n1c Apr 25, 2024
39e6e9d
Let's see if docker on mac works
ljstella Apr 25, 2024
fdc794f
Oopsie
ljstella Apr 25, 2024
3e479db
Let's try this once more
ljstella Apr 25, 2024
3730719
Missed a change
ljstella Apr 25, 2024
7e37af8
Removing docker setup on mac for now
ljstella Apr 25, 2024
a9b5f5a
Let's see if this glob works
ljstella Apr 25, 2024
a01bd09
Curly braces
ljstella Apr 25, 2024
b64721c
let's be explicit
ljstella Apr 25, 2024
549a504
startsWith instead
ljstella Apr 25, 2024
1bb3145
add baisc mitre lookups to
pyth0n1c Apr 25, 2024
1b98a5d
Merge branch 'pydantic2' of https://github.com/splunk/contentctl into…
pyth0n1c Apr 25, 2024
1a09162
Improve support test
pyth0n1c Apr 25, 2024
1434ed8
Account for new location of lookup
ljstella Apr 26, 2024
5042312
Properly support using offline fallback lookup
ljstella Apr 26, 2024
5fd1076
Don't force offline
ljstella Apr 26, 2024
1bbfab4
cleanup of attack_enrichment.py
ljstella Apr 26, 2024
02df1f5
Include apps when writing test
pyth0n1c Apr 26, 2024
8b851b1
Merge branch 'pydantic2' of https://github.com/splunk/contentctl into…
pyth0n1c Apr 26, 2024
9bc9e9a
Move apps to test_common.
pyth0n1c Apr 26, 2024
40e5989
update failed lookup validation
pyth0n1c Apr 29, 2024
ee7d505
Calculate atomic_red_team
pyth0n1c Apr 30, 2024
a063d81
Explicit detection and failure if contentctl.yml
pyth0n1c Apr 30, 2024
15abbf7
Merge branch 'main' into pydantic2.
pyth0n1c Apr 30, 2024
be92944
change default testing mode to all
pyth0n1c Apr 30, 2024
dab8a06
more changes to get default test workflow passing
pyth0n1c Apr 30, 2024
d13540f
Add reporting command support back
pyth0n1c Apr 30, 2024
eba7e10
Enrichment is not required for reporting. Remove that requirement.
pyth0n1c Apr 30, 2024
cc1007a
coverage badge being generated incorrectly
pyth0n1c Apr 30, 2024
349d80d
minor template tweaks to improve
pyth0n1c Apr 30, 2024
313ef36
initial support for env parsing
pyth0n1c May 1, 2024
3b819ca
make sure test_Servers workflow works
pyth0n1c May 1, 2024
c43a873
add more verbose exception print
pyth0n1c May 1, 2024
d2cd8b0
version should not be included in the annotations
pyth0n1c May 1, 2024
a244288
during initialize, make sure that app_template
pyth0n1c May 1, 2024
7082e45
bum pyproject version of
pyth0n1c May 1, 2024
8473fb7
use proper paths for docker apps dir.
pyth0n1c May 1, 2024
a1f538b
clean up lots of dead code
pyth0n1c May 1, 2024
4c0cd06
possible release notes progress
pyth0n1c May 2, 2024
0a7cfa6
Improve type annotations on
pyth0n1c May 2, 2024
1a7a1ee
Release notes working. output was diffed
pyth0n1c May 2, 2024
cfd1e51
make contentctl --help work even if it is missing the contentctl.yml
pyth0n1c May 2, 2024
1613903
improve release notes
pyth0n1c May 2, 2024
0c94040
Generate separate BA and App
pyth0n1c May 2, 2024
3770783
Only download/stage files once before
pyth0n1c May 2, 2024
b489b04
print container info
pyth0n1c May 2, 2024
5acb81e
printed out wrong values.
pyth0n1c May 2, 2024
73dff19
slightly improved print for container start
pyth0n1c May 2, 2024
297d2b0
Add support for suppressing tqdm
pyth0n1c May 2, 2024
2b39c32
If tqdm is suppressed but post_test_behavior
pyth0n1c May 2, 2024
89b0d25
Update workflow so that testing has "never_pause" set
pyth0n1c May 2, 2024
f0be80b
adding deprecated notes
patel-bhavin May 2, 2024
a05dc89
Merge pull request #139 from splunk/deprecated_notes
pyth0n1c May 2, 2024
ffe7240
fix small typo in conifg.pu
pyth0n1c May 2, 2024
fe29f44
change suppress-tqdm to
pyth0n1c May 2, 2024
8829168
updat end to end test after renaming
pyth0n1c May 2, 2024
c608dda
Fix bug where -container-settings.num-containers
pyth0n1c May 3, 2024
bb75f72
Add newline during print for better formatting
pyth0n1c May 3, 2024
d24f893
minor changes to printouts.
pyth0n1c May 3, 2024
163dda9
Switch hard requirement for ES
pyth0n1c May 6, 2024
c2008d1
Removed explicit check for
pyth0n1c May 6, 2024
dee4853
Merge pull request #138 from splunk/pydantic2_bonus_content
patel-bhavin May 7, 2024
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
32 changes: 19 additions & 13 deletions .github/workflows/testEndToEnd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ jobs:
strategy:
fail-fast: false
matrix:
python_version: ["3.9", "3.10", "3.11"]
operating_system: ["ubuntu-20.04", "ubuntu-22.04"]
python_version: ["3.11", "3.12"]
operating_system: ["ubuntu-20.04", "ubuntu-22.04", "macos-latest", "macos-14"]
#operating_system: ["ubuntu-20.04", "ubuntu-22.04", "macos-latest"]


runs-on: ${{ matrix.operating_system }}
steps:
- name: Install Docker for macOS
run: |
brew install docker
#import magic fails on macos runner
brew install libmagic
colima start
# Mapping below is required to get the Python docker library working
sudo ln -sf $HOME/.colima/default/docker.sock /var/run/docker.sock
if: matrix.operating_system == 'macos-latest'
#- name: Install Docker for macOS
# run: |
# brew install docker
# # import magic fails on macos runner
# brew install libmagic colima
# colima start
# # Mapping below is required to get the Python docker library working
# sudo ln -sf $HOME/.colima/default/docker.sock /var/run/docker.sock
# if: matrix.operating_system == 'macos-latest'

#Checkout the current branch
- name: Checkout repo
Expand All @@ -51,7 +51,12 @@ jobs:
- name: Run contentctl init
run: |
cd my_splunk_content_pack
poetry run contentctl init
poetry run contentctl init

- name: Clone the AtomicRedTeam Repo
run: |
cd my_splunk_content_pack
git clone --depth 1 https://github.com/redcanaryco/atomic-red-team

- name: Run contentctl validate
run: |
Expand All @@ -65,9 +70,10 @@ jobs:

#Do not pause on a failed detection
- name: Run contentctl test
if: startsWith(matrix.operating_system, 'ubuntu')
run: |
cd my_splunk_content_pack
poetry run contentctl test --unattended
poetry run contentctl test --disable-tqdm --post-test-behavior never_pause

- uses: actions/upload-artifact@v4
with:
Expand Down
83 changes: 66 additions & 17 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,68 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [

{
"name": "contentctl test",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/splunk_contentctl/contentctl.py",
"cwd": "${workspaceFolder}/splunk_contentctl",
"console": "integratedTerminal",
"justMyCode": true,
"args": ["-p", "tmp", "test"]
}
]
"configurations": [
{
"name": "contentctl init",
"type": "debugpy",
"request": "launch",
"program": "${workspaceFolder}/.venv/bin/contentctl",
"cwd": "${workspaceFolder}/../ddd/",
"args": ["init"]
},
{
"name": "contentctl validate",
"type": "debugpy",
"request": "launch",
"program": "${workspaceFolder}/.venv/bin/contentctl",
"cwd": "${workspaceFolder}/../",
"args": ["validate"]
},
{
"name": "contentctl validate enrich",
"type": "debugpy",
"request": "launch",
"program": "${workspaceFolder}/.venv/bin/contentctl",
"cwd": "${workspaceFolder}/../",
"args": ["validate", "--enrichments"]
},
{
"name": "contentctl build",
"type": "debugpy",
"request": "launch",
"program": "${workspaceFolder}/.venv/bin/contentctl",
"cwd": "${workspaceFolder}/../",
"args": ["build"]
},
{
"name": "contentctl build enrich",
"type": "debugpy",
"request": "launch",
"program": "${workspaceFolder}/.venv/bin/contentctl",
"cwd": "${workspaceFolder}/../",
"args": ["build", "--enrichments"]
},
{
"name": "contentctl test",
"type": "debugpy",
"request": "launch",
"program": "${workspaceFolder}/.venv/bin/contentctl",
"cwd": "${workspaceFolder}/../",
"args": ["test"]
},
{
"name": "contentctl --help",
"type": "debugpy",
"request": "launch",
"program": "${workspaceFolder}/.venv/bin/contentctl",
"cwd": "${workspaceFolder}/../",
"args": ["--help"]
},
{
"name": "contentctl test detection",
"type": "debugpy",
"request": "launch",
"program": "${workspaceFolder}/.venv/bin/contentctl",
"cwd": "${workspaceFolder}/../",
"args": ["test", "mode:selected", "--mode.files", "detections/endpoint/3cx_supply_chain_attack_network_indicators.yml"]
}
]
}
6 changes: 5 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
"python.terminal.activateEnvironment": true,
"python.envFile": "${workspaceFolder}/.env",
"python.testing.cwd": "${workspaceFolder}"
"python.testing.cwd": "${workspaceFolder}",
"python.languageServer": "Pylance",
"python.analysis.typeCheckingMode": "strict"


}
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,28 @@ contentctl test's default mode allows it to quickly test all content with requir
6. **docs** - Create documentation as Markdown
7. **reporting** - Create different reporting files such as a Mitre ATT&CK overlay

# Shell tab-complete

Leveraging the tab completion featureset of the CLI library we're using, you can generate tab completions for `contentctl` automatically, for zsh, bash, and tcsh. For additional details, you can view the docs for the library [here.](https://brentyi.github.io/tyro/tab_completion/)

### Zsh
If you already have a location for your ZSH tab completions, you only need to run the generation line and can skip the folder creation, configuring the rest to fit with your shell config.

```zsh
mkdir -p ~/.zfunc
contentctl --tyro-write-completion zsh ~/.zfunc/_contentctl
echo "fpath+=~/.zfunc" >> ~/.zshrc
echo "autoload -Uz compinit && compinit" >> ~/.zshrc
source ~/.zshrc
```

### Bash

```bash
completion_dir=${BASH_COMPLETION_USER_DIR:-${XDG_DATA_HOME:-$HOME/.local/share}/bash-completion}/completions/
mkdir -p $completion_dir
contentctl --tyro-write-completion bash ${completion_dir}/_contentctl
```

# Acronyms
| Acronym | Meaning| Description |
Expand Down
89 changes: 89 additions & 0 deletions contentctl/actions/build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import sys
import shutil
import os

from dataclasses import dataclass

from contentctl.objects.enums import SecurityContentProduct, SecurityContentType
from contentctl.input.director import Director, DirectorOutputDto
from contentctl.output.conf_output import ConfOutput
from contentctl.output.conf_writer import ConfWriter
from contentctl.output.ba_yml_output import BAYmlOutput
from contentctl.output.api_json_output import ApiJsonOutput
import pathlib
import json
import datetime
from typing import Union

from contentctl.objects.config import build

@dataclass(frozen=True)
class BuildInputDto:
director_output_dto: DirectorOutputDto
config:build


class Build:



def execute(self, input_dto: BuildInputDto) -> DirectorOutputDto:
if input_dto.config.build_app:
updated_conf_files:set[pathlib.Path] = set()
conf_output = ConfOutput(input_dto.config)
updated_conf_files.update(conf_output.writeHeaders())
updated_conf_files.update(conf_output.writeObjects(input_dto.director_output_dto.detections, SecurityContentType.detections))
updated_conf_files.update(conf_output.writeObjects(input_dto.director_output_dto.stories, SecurityContentType.stories))
updated_conf_files.update(conf_output.writeObjects(input_dto.director_output_dto.baselines, SecurityContentType.baselines))
updated_conf_files.update(conf_output.writeObjects(input_dto.director_output_dto.investigations, SecurityContentType.investigations))
updated_conf_files.update(conf_output.writeObjects(input_dto.director_output_dto.lookups, SecurityContentType.lookups))
updated_conf_files.update(conf_output.writeObjects(input_dto.director_output_dto.macros, SecurityContentType.macros))
updated_conf_files.update(conf_output.writeAppConf())

#Ensure that the conf file we just generated/update is syntactically valid
for conf_file in updated_conf_files:
ConfWriter.validateConfFile(conf_file)

conf_output.packageApp()

print(f"Build of '{input_dto.config.app.title}' APP successful to {input_dto.config.getPackageFilePath()}")


if input_dto.config.build_api:
shutil.rmtree(input_dto.config.getAPIPath(), ignore_errors=True)
input_dto.config.getAPIPath().mkdir(parents=True)
api_json_output = ApiJsonOutput()
for output_objects, output_type in [(input_dto.director_output_dto.detections, SecurityContentType.detections),
(input_dto.director_output_dto.stories, SecurityContentType.stories),
(input_dto.director_output_dto.baselines, SecurityContentType.baselines),
(input_dto.director_output_dto.investigations, SecurityContentType.investigations),
(input_dto.director_output_dto.lookups, SecurityContentType.lookups),
(input_dto.director_output_dto.macros, SecurityContentType.macros),
(input_dto.director_output_dto.deployments, SecurityContentType.deployments)]:
api_json_output.writeObjects(output_objects, input_dto.config.getAPIPath(), input_dto.config.app.label, output_type )



#create version file for sse api
version_file = input_dto.config.getAPIPath()/"version.json"
utc_time = datetime.datetime.now(datetime.timezone.utc).replace(microsecond=0,tzinfo=None).isoformat()
version_dict = {"version":{"name":f"v{input_dto.config.app.version}","published_at": f"{utc_time}Z" }}
with open(version_file,"w") as version_f:
json.dump(version_dict,version_f)

print(f"Build of '{input_dto.config.app.title}' API successful to {input_dto.config.getAPIPath()}")

if input_dto.config.build_ssa:

srs_path = input_dto.config.getSSAPath() / 'srs'
complex_path = input_dto.config.getSSAPath() / 'complex'
shutil.rmtree(srs_path, ignore_errors=True)
shutil.rmtree(complex_path, ignore_errors=True)
srs_path.mkdir(parents=True)
complex_path.mkdir(parents=True)
ba_yml_output = BAYmlOutput()
ba_yml_output.writeObjects(input_dto.director_output_dto.ssa_detections, str(input_dto.config.getSSAPath()))

print(f"Build of 'SSA' successful to {input_dto.config.getSSAPath()}")

return input_dto.director_output_dto
Loading