wazuh / qa-system-framework

GNU General Public License v2.0
1 stars 3 forks source link

Group and Agent related methods #71

Closed pro-akim closed 1 year ago

pro-akim commented 1 year ago
Related Issue
#60

Description

This PR includes methods for assign, unassign, create and remove groups as check existence of agent in a group.

Evidences

Running:

1. wh.create_group("test1_group") 2. wh.assign_agent_group("002", "test1_group", method='cmd') 3. wh.assign_agent_group("001", "test1_group", method='api') 4. wh.assign_agents_group(agent, 'test3_group', method='cmd') 5. wh.assign_agents_group(agent, 'test3_group', method='api') 6. wh.check_agent_groups("002", "test3_group", ['manager1']) 7. wh.unassign_agent_group('001', 'test4_group', method='api')

Results:

1. group created 2. agent 002 assigned to test1_group 3. agent 001 assigned to test1_group 4. agent 001 and 002 assigned to test3_group 5. agent 001 and 002 assigned to test3_group 6. agent 002 checked in group test3_group 7. agent 001 unassigned from test4_group
roronoasins commented 1 year ago

We need to discuss if the requests/executions require validations. Checking if the request/execution are valid or verifying the rc/stderr that returns the run_command method and using WazuhAPIResponse.

Also, I think we should support the (groups) GET method and all its endpoints.

Besides, we could add an Endpoint class that contains Authorizations, Query Parameters, Path Parameters, Responses, Request Body schema, etc.

pro-akim commented 1 year ago

Update

Some fixes were done. It will be important to have some communication with the team in order to define if validations are required

These are some Tests performed

- ``` root@ip-172-31-15-43:/home/qa# /var/ossec/bin/agent_groups -l Groups (1): default (2) Unassigned agents: 0. ``` - ``` wh.assign_agent_group('manager1', '001', 'group1', method='api') root@ip-172-31-15-43:/home/qa# /var/ossec/bin/agent_groups -l Groups (2): default (2) group1 (1) Unassigned agents: 0. ``` - ``` wh.assign_agent_group('manager1', '002', 'group1', method='cmd') root@ip-172-31-15-43:/home/qa# /var/ossec/bin/agent_groups -l Groups (2): default (2) group1 (2) Unassigned agents: 0. ``` - ``` wh.unassign_agent_group('manager1', '002', 'group1') root@ip-172-31-15-43:/home/qa# /var/ossec/bin/agent_groups -l Groups (2): default (2) group1 (1) Unassigned agents: 0. ``` - ``` wh.assign_agents_group('manager1', ['001','002'], 'group1', method='cmd') root@ip-172-31-15-43:/home/qa# /var/ossec/bin/agent_groups -l Groups (2): default (2) group1 (2) Unassigned agents: 0. ``` - ``` wh.unassign_agents_group('manager1', ['001','002'], 'group1') root@ip-172-31-15-43:/home/qa# /var/ossec/bin/agent_groups -l Groups (2): default (2) group1 (0) Unassigned agents: 0. ``` - ``` wh.assign_agents_group('manager1', ['001','002'], 'group1', method='api') root@ip-172-31-15-43:/home/qa# /var/ossec/bin/agent_groups -l Groups (2): default (2) group1 (2) Unassigned agents: 0. ``` - ``` wh.check_agent_groups('001', 'group1', ['manager1']) None ```

Status will be left on hold.

pro-akim commented 1 year ago

Update

After a conversation with the team about adding features to check the previous existence of groups or assigned agents, a new parameter was added in each method in order to check or not previous existences. Tests performed OK

- wh.check_group('manager1', 'default') True - wh.create_group('manager1', 'grupo1', method='cmd', check_previous=True) root@ip-172-31-7-229:/home/qa# /var/ossec/bin/agent_groups -l Groups (2): default (2) grupo1 (0) Unassigned agents: 0. - wh.create_group('manager1', 'grupo1', method='cmd', check_previous=True) 2023-07-24 14:24:38,751 - INFO - grupo1 already exists [WazuhEnvironmentHandler create_group:883] - wh.create_group('manager1', 'grupo2', method='api', check_previous=True) root@ip-172-31-7-229:/home/qa# /var/ossec/bin/agent_groups -l Groups (3): default (2) grupo1 (0) grupo2 (0) Unassigned agents: 0. - wh.create_group('manager1', 'grupo2', method='api', check_previous=True) 2023-07-24 14:25:49,733 - INFO - grupo2 already exists [WazuhEnvironmentHandler create_group:883] - wh.create_group('manager1', 'grupo3', method='cmd', check_previous=False) root@ip-172-31-7-229:/home/qa# /var/ossec/bin/agent_groups -l Groups (4): default (2) grupo1 (0) grupo2 (0) grupo3 (0) Unassigned agents: 0. - wh.create_group('manager1', 'grupo3', method='cmd', check_previous=False) 2023-07-24 14:29:07,020 - INFO - Creating group grupo3 from manager1 using CMD [WazuhEnvironmentHandler create_group:885] None - wh.create_group('manager1', 'grupo4', method='api', check_previous=False) root@ip-172-31-7-229:/home/qa# /var/ossec/bin/agent_groups -l Groups (5): default (2) grupo1 (0) grupo2 (0) grupo3 (0) grupo4 (0) Unassigned agents: 0. - wh.create_group('manager1', 'grupo4', method='api', check_previous=False) 2023-07-24 14:29:57,480 - INFO - Creating group grupo4 from manager1 using API [WazuhEnvironmentHandler create_group:885] None - wh.check_agent_group('manager1', '001', 'default') True - wh.check_agent_group('manager1', '001', 'No_group') False - wh.delete_group('manager1', 'grupo4', method='cmd', check_previous=True ) root@ip-172-31-7-229:/home/qa# /var/ossec/bin/agent_groups -l Groups (4): default (2) grupo1 (0) grupo2 (0) grupo3 (0) Unassigned agents: 0. - wh.delete_group('manager1', 'grupo4', method='cmd', check_previous=True ) 2023-07-24 14:36:01,965 - INFO - grupo4 does not exists [WazuhEnvironmentHandler delete_group:902] - wh.delete_group('manager1', 'grupo3', method='api', check_previous=True ) root@ip-172-31-7-229:/home/qa# /var/ossec/bin/agent_groups -l Groups (3): default (2) grupo1 (0) grupo2 (0) Unassigned agents: 0. - wh.delete_group('manager1', 'grupo3', method='api', check_previous=True ) 2023-07-24 15:01:52,461 - INFO - grupo3 does not exists [WazuhEnvironmentHandler delete_group:902] - wh.delete_group('manager1', 'grupo2', method='folder', check_previous=True) root@ip-172-31-7-229:/home/qa# /var/ossec/bin/agent_groups -l Groups (2): default (2) grupo1 (0) Unassigned agents: 0. - wh.delete_group('manager1', 'grupo2', method='folder', check_previous=True) 2023-07-24 15:03:30,470 - INFO - grupo2 does not exists [WazuhEnvironmentHandler delete_group:902] - wh.delete_group('manager1', 'grupo1', method='folder', check_previous=False) root@ip-172-31-7-229:/home/qa# /var/ossec/bin/agent_groups -l Groups (1): default (2) Unassigned agents: 0. - wh.delete_group('manager1', 'grupo1', method='folder', check_previous=False) 2023-07-24 15:07:11,828 - INFO - Removing group grupo4 deleting folder [WazuhEnvironmentHandler delete_group:915] - wh.assign_agent_group('manager1', '001', 'grupo1', method='cmd', check_previous=True) root@ip-172-31-7-229:/home/qa# /var/ossec/bin/agent_groups -l Groups (2): default (2) grupo1 (1) Unassigned agents: 0. - wh.assign_agent_group('manager1', '001', 'grupo1', method='cmd', check_previous=True) 2023-07-24 15:12:46,476 - INFO - 001 is assigned to grupo1 [WazuhEnvironmentHandler assign_agent_group:928] - wh.assign_agent_group('manager1', '002', 'grupo1', method='api', check_previous=True) root@ip-172-31-7-229:/home/qa# /var/ossec/bin/agent_groups -l Groups (2): default (2) grupo1 (2) Unassigned agents: 0. - wh.assign_agent_group('manager1', '002', 'grupo1', method='api', check_previous=True) 2023-07-24 15:12:19,539 - INFO - 002 is assigned to grupo1 [WazuhEnvironmentHandler assign_agent_group:928] - wh.unassign_agent_group('manager1', '001', 'grupo1', check_previous=True) root@ip-172-31-7-229:/home/qa# /var/ossec/bin/agent_groups -l Groups (2): default (2) grupo1 (1) Unassigned agents: 0. root@ip-172-31-7-229:/h - wh.unassign_agent_group('manager1', '001', 'grupo1', check_previous=True) 2023-07-24 15:14:14,490 - INFO - 001 is not assigned to grupo1 [WazuhEnvironmentHandler unassign_agent_group:963] - wh.unassign_agents_group('manager1', agent, 'grupo1', check_previous=True) root@ip-172-31-7-229:/home/qa# /var/ossec/bin/agent_groups -l Groups (2): default (2) grupo1 (0) Unassigned agents: 0. - wh.unassign_agents_group('manager1', agent, 'grupo1', check_previous=True) 2023-07-24 15:26:36,505 - INFO - Removing agent 001 from group grupo1 using API [WazuhEnvironmentHandler unassign_agent_group:961] 2023-07-24 15:26:36,505 - INFO - Checking agent 001 in group grupo1 [WazuhEnvironmentHandler check_agent_group:870] 2023-07-24 15:26:43,221 - INFO - 001 is not assigned to grupo1 [WazuhEnvironmentHandler unassign_agent_group:963] 2023-07-24 15:26:43,222 - INFO - Removing agent 002 from group grupo1 using API [WazuhEnvironmentHandler unassign_agent_group:961] 2023-07-24 15:26:43,222 - INFO - Checking agent 002 in group grupo1 [WazuhEnvironmentHandler check_agent_group:870] 2023-07-24 15:26:49,029 - INFO - 002 is not assigned to grupo1 [WazuhEnvironmentHandler unassign_agent_group:963] - wh.assign_agents_group('manager1', agent, 'grupo1', method='cmd', check_previous=True) root@ip-172-31-7-229:/home/qa# /var/ossec/bin/agent_groups -l Groups (2): default (2) grupo1 (2) Unassigned agents: 0. - wh.assign_agents_group('manager1', agent, 'grupo1', method='api', check_previous=True) 2023-07-24 15:27:29,867 - INFO - Assigning agent 001 from group grupo1 from manager1 using CMD [WazuhEnvironmentHandler assign_agents_group:950] 2023-07-24 15:27:29,867 - INFO - Checking agent 001 in group grupo1 [WazuhEnvironmentHandler check_agent_group:870] 2023-07-24 15:27:34,939 - INFO - 001 is assigned to grupo1 [WazuhEnvironmentHandler assign_agent_group:928] 2023-07-24 15:27:34,940 - INFO - Assigning agent 002 from group grupo1 from manager1 using CMD [WazuhEnvironmentHandler assign_agents_group:950] 2023-07-24 15:27:34,941 - INFO - Checking agent 002 in group grupo1 [WazuhEnvironmentHandler check_agent_group:870] 2023-07-24 15:27:41,063 - INFO - 002 is assigned to grupo1 [WazuhEnvironmentHandler assign_agent_group:928]
pro-akim commented 1 year ago

Update

Changes after review done.

It has to be created

Additionally, parallel, check_group functions added and checked.

Testing wh.get_agent_id('agent1') 001 wh.get_agent_name_from_ip('172.31.9.65') agent1 wh.get_agents_id(['agent1','agent2']) ['001', '002'] wh.get_group_list('manager1', method='api') ['default', 'grupo1', 'grupo2'] wh.get_group_list('manager1', method='cmd') ['default', 'grupo1', 'grupo2'] wh.get_agents_names_in_group('manager1', 'grupo1') ['agent1', 'agent2'] wh.check_group('manager1', 'grupo1', method='api') True wh.check_group('manager1', 'grupo1', method='cmd') True wh.check_agent_group('manager1', 'agent1', 'grupo1') True wh.check_agent_group('manager1', 'agent1', 'grupo10') False wh.create_group('manager1', 'grupotest1', method='api', check_group=True) wh.check_group('manager1', 'grupotest1') True wh.create_group('manager1', 'grupotest1', method='api', check_group=True) 2023-08-22 15:51:13,647 - INFO - grupotest1 already exists [WazuhEnvironmentHandler create_group:968] wh.check_group('manager1', 'grupotest1') True wh.create_group('manager1', 'grupotest1', method='api', check_group=False) wh.check_group('manager1', 'grupotest1') True wh.create_group('manager1', 'grupotest2', method='api', check_group=True) wh.check_group('manager1', 'grupotest2') True wh.delete_group('manager1', 'grupotest2', method='cmd', check_group=True) wh.check_group('manager1', 'grupotest2') False wh.delete_group('manager1', 'grupotest2', method='cmd', check_group=True) 2023-08-22 15:55:16,587 - INFO - grupotest2 does not exists [WazuhEnvironmentHandler delete_group:987] wh.check_group('manager1', 'grupotest2') False wh.delete_group('manager1', 'grupotest3', method='folder', check_group=True) wh.check_group('manager1', 'grupotest3') False wh.delete_group('manager1', 'grupotest3', method='folder', check_group=True) 2023-08-22 15:56:46,886 - INFO - grupotest3 does not exists [WazuhEnvironmentHandler delete_group:987] wh.check_group('manager1', 'grupotest3') False wh.assign_agent_group('manager1', 'agent1', group_name='default', check_group=True) wh.check_agent_group('manager1', 'agent1', 'default') True wh.assign_agent_group('manager1', 'agent1', group_name='default', check_group=True) wh.check_agent_group('manager1', 'agent1', 'default') True wh.assign_agent_group('manager1', 'agent1', group_name='default', check_group=True) 2023-08-22 15:58:54,915 - INFO - agent1 is already assigned to default [WazuhEnvironmentHandler assign_agent_group:1013] wh.check_agent_group('manager1', 'agent1', 'default') True wh.assign_agents_group('manager1', ['agent1', 'agent2'], group_name='grupotest1', method='api', check_group=True) wh.check_agent_group('manager1', 'agent1', 'grupotest1') wh.check_agent_group('manager1', 'agent2', 'grupotest1') True True wh.assign_agents_group('manager1', ['agent1', 'agent2'], group_name='grupotest1', method='api', check_group=True) wh.check_agent_group('manager1', 'agent1', 'grupotest1') wh.check_agent_group('manager1', 'agent2', 'grupotest1') 2023-08-22 16:01:47,126 - INFO - agent2 is already assigned to grupotest1 [WazuhEnvironmentHandler assign_agent_group:999] 2023-08-22 16:01:47,128 - INFO - agent1 is already assigned to grupotest1 [WazuhEnvironmentHandler assign_agent_group:999] True True wh.unassign_agent_group('manager1', 'agent1', group_name='default', check_group=True) wh.check_agent_group('manager1', 'agent1', 'default') False wh.unassign_agents_group('manager1', ['agent1', 'agent2'], group_name='grupotest1', check_group=True) wh.check_agent_group('manager1', 'agent1', 'grupotest1') wh.check_agent_group('manager1', 'agent2', 'grupotest1') 2023-08-22 16:04:20,490 - INFO - Removing agent agent2 from group grupotest1 using API [WazuhEnvironmentHandler unassign_agent_group:1039] 2023-08-22 16:04:20,490 - INFO - Removing agent agent1 from group grupotest1 using API [WazuhEnvironmentHandler unassign_agent_group:1039] False False wh.assign_agents_group('manager1', ['agent1', 'agent2'], group_name='grupotest2', method='api', check_group=True, parallel=False) wh.check_agent_group('manager1', 'agent1', 'grupotest1') wh.check_agent_group('manager1', 'agent2', 'grupotest1') True True wh.unassign_agents_group('manager1', ['agent1', 'agent2'], group_name='grupotest1', check_group=True, parallel=False) wh.check_agent_group('manager1', 'agent1', 'grupotest1') wh.check_agent_group('manager1', 'agent2', 'grupotest1') 2023-08-22 16:04:20,490 - INFO - Removing agent agent2 from group grupotest1 using API [WazuhEnvironmentHandler unassign_agent_group:1039] 2023-08-22 16:04:20,490 - INFO - Removing agent agent1 from group grupotest1 using API [WazuhEnvironmentHandler unassign_agent_group:1039] False False
pro-akim commented 1 year ago

Update

Small fixes done after conflicts were fixed.

pro-akim commented 1 year ago

Tests:

Testing wh.get_agent_id('agent1') 002 wh.get_agent_name_from_ip('172.31.2.54') agent1 wh.get_agents_id('manager1', ['agent1','agent3']) ['001', '002'] wh.get_group_list('manager1', method='api') ['default'] wh.get_group_list('manager1', method='cmd') ['default'] wh.get_agents_names_in_group('manager1', 'default') ['agent1', 'agent3', 'agent2'] wh.check_group('manager1', 'default', method='api') True wh.check_group('manager1', 'default', method='cmd') True wh.check_agent_group('manager1', 'agent1', 'default') True wh.check_agent_group('manager1', 'agent1', 'grupo10') False wh.create_group('manager1', 'grupotest1', method='api', check_group=True) wh.check_group('manager1', 'grupotest1') True wh.create_group('manager1', 'grupotest2', method='api', check_group=True) 2023-11-16 15:51:13,647 - INFO - grupotest1 already exists [WazuhEnvironmentHandler create_group:968] wh.check_group('manager1', 'grupotest1') True wh.create_group('manager1', 'grupotest3', method='api', check_group=False) wh.check_group('manager1', 'grupotest3') True wh.create_group('manager1', 'grupotest2', method='api', check_group=True) wh.check_group('manager1', 'grupotest2') True wh.delete_group('manager1', 'grupotest4', method='cmd', check_group=True) wh.check_group('manager1', 'grupotest4') False wh.delete_group('manager1', 'grupotest4', method='cmd', check_group=True) 2023-11-16 15:54:16,587 - INFO - grupotest4 does not exists [WazuhEnvironmentHandler delete_group:987] wh.check_group('manager1', 'grupotest4') False wh.delete_group('manager1', 'grupotest2', method='folder', check_group=True) wh.check_group('manager1', 'grupotest2') False wh.delete_group('manager1', 'grupotest2', method='folder', check_group=True) 2023-11-16 15:56:46,886 - INFO - grupotest2 does not exists [WazuhEnvironmentHandler delete_group:987] wh.check_group('manager1', 'grupotest2') False wh.assign_agent_group('manager1', 'agent1', group_name='grupotest20', check_group=True) wh.check_agent_group('manager1', 'agent1', 'grupotest20') True wh.assign_agent_group('manager1', 'agent2', group_name='grupotest20', check_group=True) wh.check_agent_group('manager1', 'agent2', 'grupotest20') True wh.assign_agent_group('manager1', 'agent3', group_name='grupotest20', check_group=True) 2023-11-16 15:58:54,915 - INFO - agent3 is already assigned to grupotest20 [WazuhEnvironmentHandler assign_agent_group:1013] wh.check_agent_group('manager1', 'agent3', 'default') True wh.assign_agents_group('manager1', ['agent1', 'agent2'], group_name='grupotest10', method='api', check_group=True) wh.check_agent_group('manager1', 'agent1', 'grupotest10') wh.check_agent_group('manager1', 'agent2', 'grupotest10') True True wh.assign_agents_group('manager1', ['agent1', 'agent2'], group_name='grupotest10', method='api', check_group=True) wh.check_agent_group('manager1', 'agent1', 'grupotest10') wh.check_agent_group('manager1', 'agent2', 'grupotest10') 2023-11-16 16:02:47,126 - INFO - agent2 is already assigned to grupotest10 [WazuhEnvironmentHandler assign_agent_group:999] 2023-11-16 16:02:47,128 - INFO - agent1 is already assigned to grupotest10 [WazuhEnvironmentHandler assign_agent_group:999] True True wh.unassign_agent_group('manager1', 'agent1', group_name='default', check_group=True) wh.check_agent_group('manager1', 'agent1', 'default') False wh.unassign_agents_group('manager1', ['agent1', 'agent2'], group_name='grupotest10', check_group=True) wh.check_agent_group('manager1', 'agent1', 'grupotest10') wh.check_agent_group('manager1', 'agent2', 'grupotest10') 2023-11-16 16:05:20,490 - INFO - Removing agent agent2 from group grupotest10 using API [WazuhEnvironmentHandler unassign_agent_group:1039] 2023-11-16 16:05:20,490 - INFO - Removing agent agent1 from group grupotest10 using API [WazuhEnvironmentHandler unassign_agent_group:1039] False False wh.assign_agents_group('manager1', ['agent1', 'agent2'], group_name='grupotest10', method='api', check_group=True, parallel=False) wh.check_agent_group('manager1', 'agent1', 'grupotest10') wh.check_agent_group('manager1', 'agent2', 'grupotest10') True True wh.unassign_agents_group('manager1', ['agent1', 'agent2'], group_name='grupotest10', check_group=True, parallel=False) wh.check_agent_group('manager1', 'agent1', 'grupotest1') wh.check_agent_group('manager1', 'agent2', 'grupotest1') 2023-11-16 16:06:20,490 - INFO - Removing agent agent2 from group grupotest10 using API [WazuhEnvironmentHandler unassign_agent_group:1039] 2023-11-16 16:06:20,490 - INFO - Removing agent agent1 from group grupotest10 using API [WazuhEnvironmentHandler unassign_agent_group:1039] False False
pro-akim commented 1 year ago

Update

Linter is highlighting untouched script lines.

Rebits commented 1 year ago

We have identified certain instances of misbehavior in the course of this development. The necessary corrections will be addressed through the resolution of the following issue: https://github.com/wazuh/qa-system-framework/issues/76.