def validation(): """Check if Wazuh configuration is OK. :return: AffectedItemsWazuhResult. """ result = AffectedItemsWazuhResult(**_validation_default_result_kwargs) try: response = validate_ossec_conf() result.affected_items.append({'name': node_id, **response}) result.total_affected_items += 1 except WazuhError as e: result.add_failed_item(id_=node_id, error=e) return result
def update_ossec_conf(new_conf=None): """ Replace wazuh configuration (ossec.conf) with the provided configuration. Parameters ---------- new_conf: str The new configuration to be applied. """ result = AffectedItemsWazuhResult( all_msg=f"Configuration was successfully updated" f"{' in specified node' if node_id != 'manager' else ''}", some_msg='Could not update configuration in some nodes', none_msg=f"Could not update configuration" f"{' in specified node' if node_id != 'manager' else ''}") backup_file = f'{common.ossec_conf}.backup' try: # Check a configuration has been provided if not new_conf: raise WazuhError(1125) # Check if the configuration is valid validate_wazuh_xml(new_conf, config_file=True) # Create a backup of the current configuration before attempting to replace it try: copyfile(common.ossec_conf, backup_file) except IOError: raise WazuhError(1019) # Write the new configuration and validate it write_ossec_conf(new_conf) is_valid = validate_ossec_conf() if not isinstance(is_valid, dict) or ('status' in is_valid and is_valid['status'] != 'OK'): raise WazuhError(1125) else: result.affected_items.append(node_id) exists(backup_file) and remove(backup_file) except WazuhError as e: result.add_failed_item(id_=node_id, error=e) finally: exists(backup_file) and safe_move(backup_file, common.ossec_conf) result.total_affected_items = len(result.affected_items) return result