Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IT - WDB - 2532- Test sync_agent_groups_get WDB command #2626

Merged
merged 13 commits into from
Mar 7, 2022
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Release report: TBD

### Added

- Test `sync-agens-groups-get` WDB command ([#2626](https://github.com/wazuh/wazuh-qa/pull/2626)
- Test `get-groups-integrity` WDB command ([#2607](https://github.com/wazuh/wazuh-qa/pull/2607))
- Test `set-agent-groups` WDB command ([#2602](https://github.com/wazuh/wazuh-qa/pull/2602))
- Add test fim with file currently open ([#2300](https://github.com/wazuh/wazuh-qa/pull/2300))
Expand Down
41 changes: 40 additions & 1 deletion deps/wazuh_testing/wazuh_testing/wazuh_db.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
# Copyright (C) 2015-2021, Wazuh Inc.
# Copyright (C) 2015-2022, Wazuh Inc.
# Created by Wazuh, Inc. <info@wazuh.com>.
# This program is free software; you can redistribute it and/or modify it under the terms of GPLv2
import functools
import json
import logging
import socket
import sqlite3
import time

from wazuh_testing.tools import GLOBAL_DB_PATH, WAZUH_DB_SOCKET_PATH
from wazuh_testing.tools.monitoring import wazuh_pack, wazuh_unpack
from wazuh_testing.tools.services import control_service


def callback_wazuhdb_response(item):
if isinstance(item, tuple):
data, response = item
Expand Down Expand Up @@ -167,6 +169,28 @@ def clean_agents_from_db():
raise Exception('Unable to clean agents')


def clean_groups_from_db():
"""
Clean groups from DB
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Clean groups from DB
Clean groups table from global.db

"""
command = 'global sql DELETE FROM "group"'
try:
query_wdb(command)
except Exception:
raise Exception('Unable to clean groups')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
raise Exception('Unable to clean groups')
raise Exception('Unable to clean groups table.')



def clean_belongs():
"""
Clean belong DB
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Clean belong DB
Clean belong table from global.db

"""
command = 'global sql DELETE FROM belongs'
try:
query_wdb(command)
except Exception:
raise Exception('Unable to clean belongs')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
raise Exception('Unable to clean belongs')
raise Exception('Unable to clean belongs table.')



def insert_agent_in_db(id=1, name='TestAgent', ip='any', registration_time=0, connection_status="never_connected",
disconnection_time=0):
"""
Expand All @@ -182,6 +206,21 @@ def insert_agent_in_db(id=1, name='TestAgent', ip='any', registration_time=0, co
raise Exception(f"Unable to add agent {id}")


def insert_agent_into_group(amount):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a short description

for i in range(amount):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def insert_agent_into_group(amount):
for i in range(amount):
def insert_agent_into_group(total_agents):
for i in range(total_agents):

id = i + 1
name = 'Agent-test' + str(id)
date = time.time()
command = f'global insert-agent {{"id":{id},"name":"{name}","date_add":{date}}}'
results = query_wdb(command)
assert results == 'ok'

command = f'''global set-agent-groups {{"mode":"append","sync_status":"syncreq",
"source":"remote","data":[{{"id":{id},"groups":["Test_group{id}"]}}]}}'''
results = query_wdb(command)
assert results == 'ok'


def remove_agent(agent_id):
"""Function that wraps the needed queries to remove an agent.

Expand Down
171 changes: 171 additions & 0 deletions tests/integration/test_wazuh_db/data/global/sync_agent_groups_get.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
---
-
name: "Test sync_status with response"
test_case:
-
input: 'global sync-agent-groups-get {"condition":"sync_status"}'
output: "[{'data': [{'id': 1, 'groups': ['Test_group1']}, {'id': 2, 'groups': ['Test_group2']}]}]"

-
name: "Test sync_status without response"
test_case:
-
pre_input: ['global sql UPDATE agent SET group_sync_status="synced"']
input: 'global sync-agent-groups-get {"condition":"sync_status"}'
output: "[{'data': []}]"

-
name: "Test 'all' condition when agent groups are in 'sync_req'"
test_case:
-
input: 'global sync-agent-groups-get {"condition":"all"}'
output: "[{'data': [{'id': 1, 'groups': ['Test_group1']}, {'id': 2, 'groups': ['Test_group2']}]}]"

-
name: "Test 'all' condition when agent groups are in 'synced'"
test_case:
-
pre_input: ['global sql UPDATE agent SET group_sync_status="synced"']
input: 'global sync-agent-groups-get {"condition":"all"}'
output: "[{'data': [{'id': 1, 'groups': ['Test_group1']}, {'id': 2, 'groups': ['Test_group2']}]}]"

-
name: "Test 'sync_status' condition when one agent groups are in 'synced'"
test_case:
-
pre_input: ['global sql UPDATE agent SET group_sync_status="synced" WHERE id = 2']
input: 'global sync-agent-groups-get {"condition":"sync_status"}'
output: "[{'data': [{'id': 1, 'groups': ['Test_group1']}]}]"

-
name: "Test 'all' condition when one agent groups are in 'synced'"
test_case:
-
pre_input: ['global sql UPDATE agent SET group_sync_status="synced" WHERE id = 2']
input: 'global sync-agent-groups-get {"condition":"all"}'
output: "[{'data': [{'id': 1, 'groups': ['Test_group1']}, {'id': 2, 'groups': ['Test_group2']}]}]"

-
name: "Test with and invalid filter in condition"
test_case:
-
input: 'global sync-agent-groups-get {"condition":"testinvalid"}'
output: 'err Could not obtain a response from wdb_global_sync_agent_groups_get'

-
name: "Test without condition"
test_case:
-
input: 'global sync-agent-groups-get {"last_id":0}'
output: 'err Invalid JSON data, missing required fields'

-
name: "Test set_synced in True"
test_case:
-
input: 'global sync-agent-groups-get {"condition":"sync_status", "set_synced":true}'
output: "[{'data': [{'id': 1, 'groups': ['Test_group1']}, {'id': 2, 'groups': ['Test_group2']}]}]"
new_status: "synced"
agent_id: "[1,2]"

-
name: "Test set_synced with invalidad value"
test_case:
-
input: 'global sync-agent-groups-get {"condition":"sync_status", "set_synced":false}'
output: "[{'data': [{'id': 1, 'groups': ['Test_group1']}, {'id': 2, 'groups': ['Test_group2']}]}]"
new_status: "syncreq"
agent_id: "[1,2]"

-
name: "Test set_synced with invalidad value"
test_case:
-
input: 'global sync-agent-groups-get {"condition":"sync_status", "set_synced":"set"}'
output: "[{'data': [{'id': 1, 'groups': ['Test_group1']}, {'id': 2, 'groups': ['Test_group2']}]}]"
new_status: "syncreq"
agent_id: "[1,2]"

-
name: "Test get_global_hash in true"
test_case:
-
pre_input: ['global sql UPDATE agent SET group_hash = "DUMMY"']
input: 'global sync-agent-groups-get {"last_id":0, "condition":"sync_status", "get_global_hash":true}'
output: "[{'data': [{'id': 1, 'groups': ['Test_group1']}, {'id': 2, 'groups': ['Test_group2']}], 'hash': '49087946dd7a587ae30ae89cbc8084cad2cb0bfd'}]"

-
name: "Test get_global_hash in false"
test_case:
-
pre_input: ['global sql UPDATE agent SET group_hash = "DUMMY"']
input: 'global sync-agent-groups-get {"last_id":0, "condition":"sync_status", "get_global_hash":false}'
output: "[{'data': [{'id': 1, 'groups': ['Test_group1']}, {'id': 2, 'groups': ['Test_group2']}]}]"

-
name: "Test get_global_hash with invalid value"
test_case:
-
pre_input: ['global sql UPDATE agent SET group_hash = "DUMMY"']
input: 'global sync-agent-groups-get {"last_id":0, "condition":"sync_status", "get_global_hash":"set"}'
output: "[{'data': [{'id': 1, 'groups': ['Test_group1']}, {'id': 2, 'groups': ['Test_group2']}]}]"

-
name: "Test 'agent_registration_delta' in 0 and sync_status"
test_case:
-
input: 'global sync-agent-groups-get {"condition":"sync_status", "agent_registration_delta":0}'
output: "[{'data': [{'id': 1, 'groups': ['Test_group1']}, {'id': 2, 'groups': ['Test_group2']}]}]"

-
name: "Test 'agent_registration_delta' in 0 and all condition "
test_case:
-
input: 'global sync-agent-groups-get {"condition":"all", "agent_registration_delta":0}'
output: "[{'data': [{'id': 1, 'groups': ['Test_group1']}, {'id': 2, 'groups': ['Test_group2']}]}]"

-
name: "Test 'agent_registration_delta' with delta in 10000 and sync_status"
test_case:
-
pre_input: ['global insert-agent {"id":5,"name":"Agent-test5","date_add":1545753642}', 'global set-agent-groups {"mode":"append","sync_status":"syncreq",
"source":"remote","data":[{"id":5,"groups":["Test_group5"]}]}']
input: 'global sync-agent-groups-get {"condition":"sync_status", "agent_registration_delta":10000}'
output: "[{'data': [{'id': 5, 'groups': ['Test_group5']}]}]"

-
name: "Test 'agent_registration_delta' with delta in 10000 and all"
test_case:
-
pre_input: ['global insert-agent {"id":6,"name":"Agent-test6","date_add":1545753642}', 'global set-agent-groups {"mode":"append","sync_status":"syncreq",
"source":"remote","data":[{"id":6,"groups":["Test_group6"]}]}']
input: 'global sync-agent-groups-get {"condition":"all", "agent_registration_delta":10000}'
output: "[{'data': [{'id': 6, 'groups': ['Test_group6']}]}]"

-
name: "Test last_id - by default"
test_case:
-
input: 'global sync-agent-groups-get {"last_id":0, "condition":"sync_status"}'
output: "[{'data': [{'id': 1, 'groups': ['Test_group1']}, {'id': 2, 'groups': ['Test_group2']}]}]"

-
name: "Test last_id - obtain from second group"
test_case:
-
input: 'global sync-agent-groups-get {"last_id":1, "condition":"sync_status"}'
output: "[{'data': [{'id': 2, 'groups': ['Test_group2']}]}]"

-
name: "Test last_id - with not exist id"
test_case:
-
input: 'global sync-agent-groups-get {"last_id":3, "condition":"sync_status"}'
output: "[{'data': []}]"

-
name: "Test last_id - with negative value"
test_case:
-
input: 'global sync-agent-groups-get {"last_id":50, "condition":"sync_status"}'
output: "[{'data': []}]"
135 changes: 135 additions & 0 deletions tests/integration/test_wazuh_db/test_sync_agent_groups_get.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
'''
copyright: Copyright (C) 2015-2022, Wazuh Inc.
Created by Wazuh, Inc. <info@wazuh.com>.
This program is free software; you can redistribute it and/or modify it under the terms of GPLv2
type: integration
brief: Wazuh-db is the daemon in charge of the databases with all the Wazuh persistent information, exposing a socket
to receive requests and provide information. The Wazuh core uses list-based databases to store information
related to agent keys, and FIM/Rootcheck event data.
This test checks the usage of the sync-agent-groups-get command used to allow the cluster getting the
information to be synchronized..
tier: 0
modules:
- wazuh_db
components:
- manager
daemons:
- wazuh-db
os_platform:
- linux
os_version:
- Arch Linux
- Amazon Linux 2
- Amazon Linux 1
- CentOS 8
- CentOS 7
- CentOS 6
- Ubuntu Focal
- Ubuntu Bionic
- Ubuntu Xenial
- Ubuntu Trusty
- Debian Buster
- Debian Stretch
- Debian Jessie
- Debian Wheezy
- Red Hat 8
- Red Hat 7
- Red Hat 6
references:
- https://documentation.wazuh.com/current/user-manual/reference/daemons/wazuh-db.html
tags:
- wazuh_db
'''
import os
import time
import pytest
import yaml
import json
from wazuh_testing.tools import WAZUH_PATH
from wazuh_testing.wazuh_db import (query_wdb, insert_agent_into_group, clean_agents_from_db,
clean_groups_from_db, clean_belongs)


# Marks
pytestmark = [pytest.mark.linux, pytest.mark.tier(level=0), pytest.mark.server]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you use marks defined on deps\wazuh_testing\wazuh_testing\modules\ __init__.py, please?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in commit aa349


# Configurations
test_data_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data')
messages_file = os.path.join(os.path.join(test_data_path, 'global'), 'sync_agent_groups_get.yaml')
module_tests = []
with open(messages_file) as f:
module_tests.append((yaml.safe_load(f), messages_file.split('_')[0]))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could improve by adding a method with parameters and then calling it.


log_monitor_paths = []
wdb_path = os.path.join(os.path.join(WAZUH_PATH, 'queue', 'db', 'wdb'))
receiver_sockets_params = [(wdb_path, 'AF_UNIX', 'TCP')]
monitored_sockets_params = [('wazuh-db', None, True)]
receiver_sockets = None # Set in the fixtures
agents = ['agent1', 'agent2']


# Fixtures
@pytest.fixture(scope='function')
def pre_insert_agents():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a short description.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confused name because not only insert agents else also clean all tables.

Copy link
Contributor Author

@CamiRomero CamiRomero Mar 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The purpose of this Fixture is to insert agents and assign those agents into a group, the cleans are after a 'yield' and execute after the test to assure that the environment is clean. Maybe I can change the name to pre_insert_agents_into_group but I think that is not necessary to consider the steps inside yield for the name

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct! But not only insert but also remove as you mentioned, so the name should contain the words insert and remove agents. It helps to be representative of the content of the method.

insert_agent_into_group(2)

yield
clean_agents_from_db()
clean_groups_from_db()
clean_belongs()


# Tests
@pytest.mark.parametrize('test_case',
[case['test_case'] for module_data in module_tests for case in module_data[0]],
ids=[f"{module_name}: {case['name']}"
for module_data, module_name in module_tests
for case in module_data]
)
def test_sync_agent_groups(configure_sockets_environment, connect_to_sockets_module, test_case, pre_insert_agents):
'''
description: Check that commands about sync_aget_groups_get works properly.
wazuh_min_version: 4.4.0
parameters:
- configure_sockets_environment:
type: fixture
brief: Configure environment for sockets and MITM.
- connect_to_sockets_module:
type: fixture
brief: Module scope version of 'connect_to_sockets' fixture.
- test_case:
type: fixture
brief: List of test_case stages (dicts with input, output and agent_id and expected_groups keys).
- pre_insert_agents:
type: fixture
brief: fixture in charge of insert agents and groups into DB.
assertions:
- Verify that the socket response matches the expected output.
input_description:
- Test cases are defined in the sync_agent_groups_get.yaml file.
expected_output:
- an array with all the agents that match with the search criteria
tags:
- wazuh_db
- wdb_socket
'''
case_data = test_case[0]
output = case_data["output"]
damarisg marked this conversation as resolved.
Show resolved Hide resolved

if 'pre_input' in case_data:
for command in case_data['pre_input']:
query_wdb(command)
results = query_wdb(command)
damarisg marked this conversation as resolved.
Show resolved Hide resolved

time.sleep(1)
response = query_wdb(case_data["input"])

# validate response
assert str(response) == output

# validate if the status of the group has change
if "new_status" in case_data:
agent_id = json.loads(case_data["agent_id"])
for id in agent_id:
response = query_wdb(f'global get-agent-info {id}')
assert case_data["new_status"] == response[0]['group_sync_status']