Skip to content

Commit

Permalink
fix(#3890): fix agent-groups hash calculation
Browse files Browse the repository at this point in the history
* fix(#3890): add func to calculate the hash correctly

* feat(#3890): add func to get the hash in wazuh_db module

* docs(#3890): update changelog

* style(#3890): improve styling
  • Loading branch information
QU3B1M authored Feb 7, 2023
1 parent bd3de19 commit 401c57d
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 109 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ Release report: TBD

### Fixed

- Fix test_sync_agent_groups_get, replace hardcoded hash to a dinamically calculated one ([#3895](https:/wazuh/wazuh-qa/pull/3895)) \- (Framework + Tests)
- Fix test_db_backup for Ubuntu OS ([#3802](https:/wazuh/wazuh-qa/pull/3802)) \- (Tests)
- Fix commit option of the scan module for master case ([#3157](https:/wazuh/wazuh-qa/pull/3157)) \- (Tests)
- Fix Vulnerability Detector IT: test_validate_feed_content yaml cases had wrong extension. ([#3299](https:/wazuh/wazuh-qa/pull/3299)) \- (Tests)
Expand Down
16 changes: 16 additions & 0 deletions deps/wazuh_testing/wazuh_testing/wazuh_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Created by Wazuh, Inc. <[email protected]>.
# This program is free software; you can redistribute it and/or modify it under the terms of GPLv2
import functools
import hashlib
import json
import logging
import socket
Expand Down Expand Up @@ -230,3 +231,18 @@ def remove_agent(agent_id):
"""
data = query_wdb(f"global delete-agent {agent_id}").split()
assert data[0] == 'ok', f"Unable to remove agent {agent_id} - {data[1]}"


def calculate_global_hash():
"""Function that calculates and retrieves the actual global groups hash.
Returns:
str: Actual global groups hash.
"""
GET_GROUP_HASH = '''global sql SELECT group_hash FROM agent WHERE
id > 0 AND group_hash IS NOT NULL ORDER BY id'''

result = query_wdb(GET_GROUP_HASH)
group_hashes = [item['group_hash'] for item in result]

return hashlib.sha1("".join(group_hashes).encode()).hexdigest()
221 changes: 115 additions & 106 deletions tests/integration/test_wazuh_db/data/global/sync_agent_groups_get.yaml
Original file line number Diff line number Diff line change
@@ -1,171 +1,180 @@
---
-
name: "Test sync_status with response"
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']}]}]"
-
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"
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': []}]"
-
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'"
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']}]}]"
-
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'"
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']}]}]"
-
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'"
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']}]}]"
-
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'"
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']}]}]"
-
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"
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'
-
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"
name: Test without condition
test_case:
-
input: 'global sync-agent-groups-get {"last_id":0}'
output: "err Invalid JSON data, missing required 'condition' field"
-
input: global sync-agent-groups-get {"last_id":0}
output: err Invalid JSON data, missing required 'condition' field

-
name: "Test set_synced in True"
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]"
-
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 invalid value - false"
name: Test set_synced with invalid value - false
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]"
-
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 invalid value - String"
name: Test set_synced with invalid value - String
test_case:
-
input: 'global sync-agent-groups-get {"condition":"sync_status", "set_synced":"set"}'
output: "err Invalid JSON data, invalid alternative fields data"
new_status: "syncreq"
agent_id: "[1,2]"
-
input: global sync-agent-groups-get {"condition":"sync_status", "set_synced":"set"}
output: err Invalid JSON data, invalid alternative fields data
new_status: syncreq
agent_id: "[1,2]"

-
name: "Test get_global_hash in true"
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'}]"
-
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': '[GLOBAL_HASH]'}]"

-
name: "Test get_global_hash in false"
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']}]}]"
-
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"
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: "err Invalid JSON data, invalid alternative fields data"
-
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: err Invalid JSON data, invalid alternative fields data

-
name: "Test 'agent_registration_delta' in 0 and sync_status"
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']}]}]"
-
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 "
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']}]}]"
-
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"
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",
"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']}]}]"
-
pre_input:
- global insert-agent {"id":5,"name":"Agent-test5","date_add":1545753642}
- global set-agent-groups {"mode":"append","sync_status":"syncreq","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"
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",
"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']}]}]"
-
pre_input:
- global insert-agent {"id":6,"name":"Agent-test6","date_add":1545753642}
- global set-agent-groups {"mode":"append","sync_status":"syncreq","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"
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']}]}]"
-
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"
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']}]}]"
-
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"
name: Test last_id - with not exist id
test_case:
-
input: 'global sync-agent-groups-get {"last_id":3, "condition":"sync_status"}'
output: "[{'data': []}]"
-
input: global sync-agent-groups-get {"last_id":3, "condition":"sync_status"}
output: "[{'data': []}]"

-
name: "Test last_id - with negative value"
name: Test last_id - with negative value
test_case:
-
input: 'global sync-agent-groups-get {"last_id":-3, "condition":"sync_status"}'
output: "err Invalid JSON data, invalid alternative fields data"
-
input: global sync-agent-groups-get {"last_id":-3, "condition":"sync_status"}
output: err Invalid JSON data, invalid alternative fields data
11 changes: 8 additions & 3 deletions tests/integration/test_wazuh_db/test_sync_agent_groups_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@
- wazuh_db
'''
import os
import yaml
import time
import pytest
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)
from wazuh_testing.wazuh_db import query_wdb, insert_agent_into_group, clean_agents_from_db
from wazuh_testing.wazuh_db import clean_groups_from_db, clean_belongs, calculate_global_hash
from wazuh_testing.modules import TIER0, SERVER, LINUX
from wazuh_testing.tools.file import get_list_of_content_yml


# Marks
pytestmark = [LINUX, TIER0, SERVER]

Expand Down Expand Up @@ -124,6 +124,11 @@ def test_sync_agent_groups(configure_sockets_environment, connect_to_sockets_mod
for command in case_data['pre_input']:
query_wdb(command)

# Check if it requires the global hash.
if '[GLOBAL_HASH]' in output:
global_hash = calculate_global_hash()
output = output.replace('[GLOBAL_HASH]', global_hash)

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

Expand Down

0 comments on commit 401c57d

Please sign in to comment.