-
Notifications
You must be signed in to change notification settings - Fork 4
Implementation of agent remove method in WazuhHandler #50
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
Open
pro-akim
wants to merge
10
commits into
system-refactor
Choose a base branch
from
46-remove-agents
base: system-refactor
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
00d95dc
feat(#46): Remove agents in construction
pro-akim da86fb5
feat(#46): Method tested
pro-akim e694057
feat(#46): Changes after linter
pro-akim f5c1621
feat(#46): import json
pro-akim b15ad7e
feat(#46): More changes after linter
pro-akim 172235f
feat(#46): New changes after linter
pro-akim 508698d
refactor(#46): New changes after review
pro-akim 007d5ac
merge(#46): Merge
pro-akim 75bb26a
fix(#46): Adapting methods after merge
pro-akim cc9476b
fix(#46): Fixes after linter
pro-akim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -9,11 +9,13 @@ | |
from wazuh_qa_framework.generic_modules.logging.base_logger import BaseLogger | ||
from wazuh_qa_framework.global_variables.daemons import WAZUH_ANGENT_WINDOWS_SERVICE_NAME | ||
from wazuh_qa_framework.system.host_manager import HostManager | ||
from wazuh_qa_framework.wazuh_components.api.wazuh_api import WazuhAPI | ||
from wazuh_qa_framework.wazuh_components.api.wazuh_api_request import WazuhAPIRequest | ||
|
||
|
||
DEFAULT_INSTALL_PATH = { | ||
'linux': '/var/ossec', | ||
'windows': 'C:\\Program Files\\ossec-agent', | ||
'windows': 'C:\\Program Files (x86)\\ossec-agent', | ||
'darwin': '/Library/Ossec' | ||
} | ||
|
||
|
@@ -503,13 +505,20 @@ def get_agents_info(self): | |
""" | ||
pass | ||
|
||
def get_agents_id(self, agents_list=None): | ||
"""Get agents id | ||
|
||
Returns: | ||
List: Agents id list | ||
def get_agent_id(self, host, agent): | ||
"""Get agent id | ||
Args: | ||
host (_type_, str): Ansible host name. | ||
agent (_type_, str): Agent name. | ||
Return: | ||
str: agent_id | ||
""" | ||
pass | ||
host_list = WazuhAPI(address=self.get_host_variables(host)['ip']).list_agents()['affected_items'] | ||
for host in host_list: | ||
if host.get('ip') == self.get_host_variables(agent)['ip']: | ||
return host.get('id') | ||
|
||
return None | ||
|
||
def restart_agent(self, host): | ||
"""Restart agent | ||
|
@@ -526,9 +535,7 @@ def restart_agent(self, host): | |
raise ValueError(f'Host {host} is not an agent') | ||
|
||
def restart_agents(self, agent_list=None, parallel=True): | ||
"""Restart list of agents | ||
|
||
Args: | ||
""" Restart list of agents | ||
agent_list (list, optional): Agent list. Defaults to None. | ||
parallel (bool, optional): Parallel execution. Defaults to True. | ||
""" | ||
|
@@ -575,7 +582,7 @@ def stop_agent(self, host): | |
host (str): Hostname | ||
""" | ||
self.logger.debug(f'Stopping agent {host}') | ||
service_name = WAZUH_ANGENT_WINDOWS_SERVICE_NAME if is_windows(host) else 'wazuh-agent' | ||
service_name = WAZUH_ANGENT_WINDOWS_SERVICE_NAME if self.is_windows(host) else 'wazuh-agent' | ||
if self.is_agent(host): | ||
self.control_service(host, service_name, 'stopped') | ||
self.logger.debug(f'Agent {host} stopped successfully') | ||
|
@@ -632,7 +639,7 @@ def start_agent(self, host): | |
host (str): Hostname | ||
""" | ||
self.logger.debug(f'Starting agent {host}') | ||
service_name = WAZUH_ANGENT_WINDOWS_SERVICE_NAME if is_windows(host) else 'wazuh-agent' | ||
service_name = WAZUH_ANGENT_WINDOWS_SERVICE_NAME if self.is_windows(host) else 'wazuh-agent' | ||
if self.is_agent(host): | ||
self.control_service(host, service_name, 'started') | ||
self.logger.debug(f'Agent {host} started successfully') | ||
|
@@ -727,11 +734,11 @@ def stop_environment(self, parallel=True): | |
self.pool.map(self.stop_agent, agent_list) | ||
else: | ||
self.logger.info(message='Stopping environment: Managers') | ||
for manager in get_managers(): | ||
for manager in manager_list: | ||
self.stop_manager(manager) | ||
|
||
self.logger.info(message='Stopping environment: Agents') | ||
for agent in get_agents(): | ||
for agent in agent_list: | ||
self.stop_agent(agent) | ||
|
||
self.logger.info('Stopping environment') | ||
|
@@ -754,11 +761,11 @@ def start_environment(self, parallel=True): | |
self.pool.map(self.start_agent, agent_list) | ||
else: | ||
self.logger.info(message='Starting environment: Managers') | ||
for manager in get_managers(): | ||
for manager in manager_list: | ||
self.start_manager(manager) | ||
|
||
self.logger.info(message='Starting environment: Agents') | ||
for agent in get_agents(): | ||
for agent in agent_list: | ||
self.start_agent(agent) | ||
|
||
self.logger.info('Environment started successfully') | ||
|
@@ -787,26 +794,76 @@ def clean_client_keys(self, hosts=None): | |
""" | ||
pass | ||
|
||
def clean_logs(self, host): | ||
"""Remove host logs | ||
Args: | ||
host (_type_, str): Host. | ||
""" | ||
# Clean ossec.log, api.log and cluster.log | ||
self.logger.info(f'Removing {host} logs') | ||
logs_path = self.get_logs_directory_path(host) | ||
if self.is_windows(host): | ||
self.truncate_file(host, f'{logs_path}/ossec.log', recreate=True, become=False, ignore_errors=False) | ||
else: | ||
self.truncate_file(host, f'{logs_path}/ossec.log', recreate=True, become=True, ignore_errors=False) | ||
host_type = self.get_host_variables(host).get('type') | ||
if 'master' == host_type or 'worker' == host_type: | ||
self.truncate_file(host, f'{logs_path}/api.log', recreate=True, become=True, ignore_errors=False) | ||
self.truncate_file(host, f'{logs_path}/cluster.log', recreate=True, become=True, ignore_errors=False) | ||
|
||
def clean_agents(self, agents=None): | ||
"""Stop agents, remove them from manager and clean their client keys | ||
|
||
Args: | ||
agents (_type_, agents_list): Agents list. Defaults to None. | ||
""" | ||
pass | ||
|
||
def remove_agents_from_manager(self, agents=None, status='all', older_than='0s'): | ||
def remove_agents_from_manager(self, agent_list, manager=None, method='cmd', parallel=True, logs=False, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is not supported the deletion of the client.keys. Also logs is not a meaningful name for a variable There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comments changed in 508698d |
||
restart=False): | ||
"""Remove agents from manager | ||
|
||
Args: | ||
agents (list, optional): Agents list. Defaults to None. | ||
status (str, optional): Agents status. Defaults to 'all'. | ||
older_than (str, optional): Older than parameter. Defaults to '0s'. | ||
agent_list (list, optional): Agents list. Defaults to None. | ||
manager (str, optional): Name of manager. Defaults to None. | ||
method (str): Method to be used to remove agents, Defaults to cmd. | ||
parallel (str): In case that cmd method is used, it defines the use of threads for remove. Defaults to True. | ||
logs (str): Remove logs (ossec.log, api.log) from agents. Defaults to False. | ||
restart (str): Restart agents. Defaults to False. | ||
""" | ||
if manager is None: | ||
manager = 'manager1' | ||
|
||
# Getting agent_ids list | ||
agent_ids = [] | ||
for agent in agent_list: | ||
agent_ids.append(self.get_agent_id(manager, agent)) | ||
|
||
# Remove processes | ||
if method == 'cmd': | ||
self.logger.info(f'Removing agents {agent_list} using cmd') | ||
if parallel: | ||
self.pool.map(lambda id: self.run_command(manager, f"/var/ossec/bin/manage_agents -r {id}", True), | ||
agent_ids) | ||
else: | ||
for id in agent_ids: | ||
self.run_command(manager, f"/var/ossec/bin/manage_agents -r {id}", True) | ||
else: | ||
self.logger.info(f'Removing agents {agent_list} using API') | ||
agent_string = ','.join(agent_ids) | ||
endpoint = f'/agents?pretty=true&older_than=0s&agents_list={agent_string}&status=all' | ||
request = WazuhAPIRequest(endpoint=endpoint, method='DELETE') | ||
request.send(WazuhAPI(address=self.get_host_variables(manager)['ip'])) | ||
|
||
# Remove logs | ||
if logs and not parallel: | ||
for agent in agent_list: | ||
self.clean_logs(agent) | ||
if logs and parallel: | ||
self.pool.map(self.clean_logs, agent_list) | ||
|
||
Returns: | ||
dict: API response | ||
""" | ||
pass | ||
# Restarting agents | ||
if restart: | ||
self.restart_agents(agent_list, parallel=parallel) | ||
|
||
def get_managers(self): | ||
"""Get environment managers names | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Specify which logs are removed