Skip to content

Commit

Permalink
Merge pull request #103 from splunk/reuse_environment
Browse files Browse the repository at this point in the history
Set up splunk container once and reuse it
  • Loading branch information
pyth0n1c authored Feb 16, 2024
2 parents e246e36 + c9b38ac commit a93e097
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ class DetectionTestingInfrastructureContainer(DetectionTestingInfrastructure):
container: docker.models.resource.Model = None

def start(self):
if self.global_config.infrastructure_config.persist_and_reuse_container:
# If we are configured to use the persistent container, then check and see if it's already
# running. If so, just use it without additional configuration.
try:
self.container = self.get_docker_client().containers.get(self.get_name())
return
except Exception:
#We did not find the container running, we will set it up
pass

self.container = self.make_container()
self.container.start()

Expand Down Expand Up @@ -107,7 +117,12 @@ def removeContainer(self, removeVolumes: bool = True, forceRemove: bool = True):
# Container does not exist, no need to try and remove it
return
try:

# If the user wants to persist the container (or use a previously configured container), then DO NOT remove it.
# Emit the following message, which they will see on initial setup and teardown at the end of the test.
if self.global_config.infrastructure_config.persist_and_reuse_container:
print(f"\nContainer [{self.get_name()}] has NOT been terminated because 'contentctl_test.yml ---> infrastructure_config ---> persist_and_reuse_container = True'")
print(f"To remove it, please manually run the following at the command line: `docker container rm -fv {self.get_name()}`\n")
return
# container was found, so now we try to remove it
# v also removes volumes linked to the container
container.remove(v=removeVolumes, force=forceRemove)
Expand Down
8 changes: 8 additions & 0 deletions contentctl/contentctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,13 @@ def main():
help="path to the content path containing the contentctl.yml",
)

parser.add_argument(
"--disable_enrichment",
required=False,
action="store_true",
help="Enrichment is only REQUIRED when building a release (or testing a release). In most cases, it is not required. Disabling enrichment will significantly speed up all contentctl commands."
)

parser.set_defaults(func=lambda _: parser.print_help())
actions_parser = parser.add_subparsers(
title="Splunk content actions", dest="action"
Expand Down Expand Up @@ -640,6 +647,7 @@ def main():
help="Whether integration testing should be enabled, in addition to unit testing (requires a configured Splunk "
"instance with ES installed)"
)

# TODO (cmcginley): add flag for enabling logging for correlation_search logging
# TODO (cmcginley): add flag for changing max_sleep time for integration tests
# TODO (cmcginley): add setting to skip listing skips -> test_config.TestConfig,
Expand Down
4 changes: 3 additions & 1 deletion contentctl/helper/config_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pathlib

from contentctl.input.yml_reader import YmlReader
from contentctl.objects.config import Config, TestConfig
from contentctl.objects.config import Config, TestConfig, ConfigEnrichments
from contentctl.objects.enums import DetectionTestingMode
from typing import Union
import argparse
Expand All @@ -23,6 +23,8 @@ def read_config(cls, args:argparse.Namespace) -> Config:

try:
config = Config.parse_obj(yml_dict)
if args.disable_enrichment:
config.enrichments = ConfigEnrichments(attack_enrichment=False,cve_enrichment=False,splunk_app_enrichment=False)
except Exception as e:
raise Exception(f"Error reading config file: {str(e)}")

Expand Down
3 changes: 3 additions & 0 deletions contentctl/objects/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ class InfrastructureConfig(BaseModel, extra=Extra.forbid, validate_assignment=Tr
default=DetectionTestingTargetInfrastructure.container,
title=f"Control where testing should be launched. Choose one of {DetectionTestingTargetInfrastructure._member_names_}",
)

persist_and_reuse_container:bool = True

full_image_path: str = Field(
default="registry.hub.docker.com/splunk/splunk:latest",
title="Full path to the container image to be used",
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "contentctl"
version = "3.1.0"
version = "3.2.0"
description = "Splunk Content Control Tool"
authors = ["STRT <[email protected]>"]
license = "Apache 2.0"
Expand Down

0 comments on commit a93e097

Please sign in to comment.