-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(#3890): fix agent-groups hash calculation
* 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
Showing
4 changed files
with
140 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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
221
tests/integration/test_wazuh_db/data/global/sync_agent_groups_get.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters