From 2c925fa95bb9aa0668a23b8f8aeb52ec7b7d1664 Mon Sep 17 00:00:00 2001 From: Antonio Date: Thu, 18 Apr 2024 16:21:27 +0200 Subject: [PATCH] enhancement(#5229): Enhancement of os validation --- .../modules/testing/tests/helpers/agent.py | 16 ++- .../modules/testing/tests/helpers/generic.py | 6 +- .../agent/vagrant/test-agent-complete.yaml | 110 ++++++++++++++++++ .../examples/agent/vagrant/test.yaml | 34 ++++++ 4 files changed, 157 insertions(+), 9 deletions(-) create mode 100644 deployability/modules/workflow_engine/examples/agent/vagrant/test-agent-complete.yaml create mode 100755 deployability/modules/workflow_engine/examples/agent/vagrant/test.yaml diff --git a/deployability/modules/testing/tests/helpers/agent.py b/deployability/modules/testing/tests/helpers/agent.py index fdb1d2a869..5f6a24b78a 100644 --- a/deployability/modules/testing/tests/helpers/agent.py +++ b/deployability/modules/testing/tests/helpers/agent.py @@ -94,9 +94,11 @@ def register_agent(inventory_path, manager_path): with open(manager_path, 'r') as yaml_file: manager_path = yaml.safe_load(yaml_file) + host = manager_path.get('ansible_host') - if 'linux' in inventory_path: + os_type = HostInformation.get_os_type(inventory_path) + if os_type == 'linux': host_ip = HostInformation.get_internal_ip_from_aws_dns(host) if 'amazonaws' in host else host commands = [ f"sed -i 's/
MANAGER_IP<\/address>/
{host_ip}<\/address>/g' {WAZUH_CONF}", @@ -105,7 +107,7 @@ def register_agent(inventory_path, manager_path): Executor.execute_commands(inventory_path, commands) assert host_ip in Executor.execute_command(inventory_path, f'cat {WAZUH_CONF}'), logger.error(f'Error configuring the Manager IP ({host_ip}) in: {HostInformation.get_os_name_and_version_from_inventory(inventory_path)} agent') - elif 'macos' in inventory_path: + elif os_type == 'macos': host_ip = HostInformation.get_public_ip_from_aws_dns(host) if 'amazonaws' in host else host commands = [ f"sed -i '.bak' 's/
MANAGER_IP<\/address>/
{host_ip}<\/address>/g' /Library/Ossec/etc/ossec.conf", @@ -199,7 +201,7 @@ def perform_action_and_scan(agent_params, action_callback) -> dict: os_name = HostInformation.get_os_name_from_inventory(agent_params) logger.info(f'Applying filters in checkfiles in {HostInformation.get_os_name_and_version_from_inventory(agent_params)}') - if 'linux' in agent_params: + if 'linux' == HostInformation.get_os_type(agent_params): if 'debian' in os_name: filter_data = { '/boot': {'added': [], 'removed': [], 'modified': ['grubenv']}, @@ -235,7 +237,7 @@ def perform_action_and_scan(agent_params, action_callback) -> dict: '/root': {'added': ['trustdb.gpg', 'lesshst'], 'removed': [], 'modified': []}, '/usr/sbin': {'added': [], 'removed': [], 'modified': []} } - elif 'macos' in agent_params: + elif 'macos' == HostInformation.get_os_type(agent_params): filter_data = { '/usr/bin': {'added': [], 'removed': [], 'modified': []}, '/usr/sbin': {'added': [], 'removed': [], 'modified': []} @@ -291,11 +293,13 @@ def assert_results(result, agent_params) -> None: result (dict): result of comparison between pre and post action scan """ - if 'linux' in agent_params: + os_name = HostInformation.get_os_name_from_inventory(agent_params) + if os_name == 'linux': categories = ['/root', '/usr/bin', '/usr/sbin', '/boot'] - elif 'macos' in agent_params: + elif os_name == 'macos': categories = ['/usr/bin', '/usr/sbin'] actions = ['added', 'modified', 'removed'] + # Testing the results for category in categories: for action in actions: diff --git a/deployability/modules/testing/tests/helpers/generic.py b/deployability/modules/testing/tests/helpers/generic.py index 3d33cd0187..316866aff1 100644 --- a/deployability/modules/testing/tests/helpers/generic.py +++ b/deployability/modules/testing/tests/helpers/generic.py @@ -499,13 +499,13 @@ def _checkfiles(inventory_path, os_type, directory, filter= None, hash_algorithm Returns: Dict: dict of directories:hash """ - if 'linux' in os_type: + if 'linux' == os_type: command = f'sudo find {directory} -type f -exec sha256sum {{}} + {filter}' result = Executor.execute_command(inventory_path, command) - elif 'macos' in os_type: + elif 'macos' == os_type: command = f'sudo find {directory} -type f -exec shasum -a 256 {{}} \; {filter}' result = Executor.execute_command(inventory_path, command) - elif 'windows' in os_type: + elif 'windows' == os_type: command = 'dir /a-d /b /s | findstr /v /c:"\\.$" /c:"\\..$"| find /c ":"' else: logger.info(f'Unsupported operating system') diff --git a/deployability/modules/workflow_engine/examples/agent/vagrant/test-agent-complete.yaml b/deployability/modules/workflow_engine/examples/agent/vagrant/test-agent-complete.yaml new file mode 100644 index 0000000000..648ea9788d --- /dev/null +++ b/deployability/modules/workflow_engine/examples/agent/vagrant/test-agent-complete.yaml @@ -0,0 +1,110 @@ +version: 0.1 +description: This workflow is used to test agents' deployment for DDT1 PoC +variables: + agent-os: + - linux-oracle-9-amd64 + + manager-os: linux-ubuntu-22.04-amd64 + infra-provider: vagrant + working-dir: /tmp/dtt1-poc + +tasks: + # Unique manager allocate task + - task: "allocate-manager-{manager-os}" + description: "Allocate resources for the manager." + do: + this: process + with: + path: python3 + args: + - modules/allocation/main.py + - action: create + - provider: "{infra-provider}" + - size: large + - composite-name: "{manager-os}" + - inventory-output: "{working-dir}/manager-{manager-os}/inventory.yaml" + - track-output: "{working-dir}/manager-{manager-os}/track.yaml" + - label-termination-date: "1d" + - label-team: "qa" + on-error: "abort-all" + cleanup: + this: process + with: + path: python3 + args: + - modules/allocation/main.py + - action: delete + - track-output: "{working-dir}/manager-{manager-os}/track.yaml" + + # Unique agent allocate task + - task: "allocate-agent-{agent}" + description: "Allocate resources for the agent." + do: + this: process + with: + path: python3 + args: + - modules/allocation/main.py + - action: create + - provider: "{infra-provider}" + - size: small + - composite-name: "{agent}" + - inventory-output: "{working-dir}/agent-{agent}/inventory.yaml" + - track-output: "{working-dir}/agent-{agent}/track.yaml" + - label-termination-date: "1d" + - label-team: "qa" + foreach: + - variable: agent-os + as: agent + cleanup: + this: process + with: + path: python3 + args: + - modules/allocation/main.py + - action: delete + - track-output: "{working-dir}/agent-{agent}/track.yaml" + depends-on: + - "provision-manager-{manager-os}" + + # Unique manager provision task + - task: "provision-manager-{manager-os}" + description: "Provision the manager." + do: + this: process + with: + path: python3 + args: + - modules/provision/main.py + - inventory: "{working-dir}/manager-{manager-os}/inventory.yaml" + - install: + - component: wazuh-manager + type: assistant + version: 4.7.3 + live: True + depends-on: + - "allocate-manager-{manager-os}" + + + # Generic agent test task + - task: "run-agent-{agent}-tests" + description: "Run tests install for the agent {agent}." + do: + this: process + with: + path: python3 + args: + - modules/testing/main.py + - targets: + - wazuh-1: "{working-dir}/manager-{manager-os}/inventory.yaml" + - agent: "{working-dir}/agent-{agent}/inventory.yaml" + - tests: "install,registration,restart,stop,uninstall" + - component: "agent" + - wazuh-version: "4.7.3" + - wazuh-revision: "40714" + - live: "True" + foreach: + - variable: agent-os + as: agent + depends-on: + - "allocate-agent-{agent}" diff --git a/deployability/modules/workflow_engine/examples/agent/vagrant/test.yaml b/deployability/modules/workflow_engine/examples/agent/vagrant/test.yaml new file mode 100755 index 0000000000..253016fae3 --- /dev/null +++ b/deployability/modules/workflow_engine/examples/agent/vagrant/test.yaml @@ -0,0 +1,34 @@ +version: 0.1 +description: Test agent restart with provisioning agents' with provision module +variables: + agent-os: + - macos-ventura-13.4.1-arm64 + manager-os: linux-ubuntu-18.04-amd64 + infra-provider: aws + working-dir: /tmp/dtt1-poc + +tasks: + + + + # Generic agent test task + - task: "run-agent-{agent}-tests" + description: "Run tests install for the agent {agent}." + do: + this: process + with: + path: python3 + args: + - modules/testing/main.py + - targets: + - wazuh-1: "{working-dir}/manager-{manager-os}/inventory.yaml" + - agent: "{working-dir}/agent-{agent}/inventory.yaml" + - tests: "install,registration,restart,stop,uninstall" + - component: "agent" + - wazuh-version: "4.7.3" + - wazuh-revision: "40714" + - live: "True" + foreach: + - variable: agent-os + as: agent +