Skip to content

Commit

Permalink
Merge pull request #1013 from wazuh/1007-init-conf
Browse files Browse the repository at this point in the history
Deprecate ossec-init.conf
  • Loading branch information
vikman90 committed Feb 5, 2021
2 parents 6b8b5a5 + 1f9b254 commit d7b03e5
Show file tree
Hide file tree
Showing 14 changed files with 125 additions and 143 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ __pycache__

# Python bytecode files
*.pyc
.idea

### macOS ###
# General
Expand Down
64 changes: 40 additions & 24 deletions deps/wazuh_testing/wazuh_testing/tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import os
import sys
import platform
import subprocess

if sys.platform == 'win32':
WAZUH_PATH = os.path.join("C:", os.sep, "Program Files (x86)", "ossec-agent")
Expand All @@ -17,25 +19,24 @@
API_LOG_FILE_PATH = None

else:
if os.path.isfile("/etc/ossec-init.conf"):
with open("/etc/ossec-init.conf") as ossec_init:
WAZUH_PATH = os.path.join(
[item.rstrip().replace("DIRECTORY=", "").replace("\"", "")
for item in ossec_init.readlines() if "DIRECTORY" in item][0])

WAZUH_SOURCES = os.path.join('/', 'wazuh')

if sys.platform == 'darwin':
WAZUH_PATH = os.path.join("/", "Library", "Ossec")
PREFIX = os.path.join('/', 'private', 'var', 'root')
GEN_OSSEC = None
else:
WAZUH_PATH = os.path.join("/", "var", "ossec")
GEN_OSSEC = os.path.join(WAZUH_SOURCES, 'gen_ossec.sh')
PREFIX = os.sep

WAZUH_CONF = os.path.join(WAZUH_PATH, 'etc', 'ossec.conf')
WAZUH_API_CONF = os.path.join(WAZUH_PATH, 'api', 'configuration', 'api.yaml')
WAZUH_SECURITY_CONF = os.path.join(WAZUH_PATH, 'api', 'configuration', 'security', 'security.yaml')
WAZUH_SOURCES = os.path.join('/', 'wazuh')
LOG_FILE_PATH = os.path.join(WAZUH_PATH, 'logs', 'ossec.log')
API_LOG_FILE_PATH = os.path.join(WAZUH_PATH, 'logs', 'api.log')
if sys.platform == 'darwin':
PREFIX = os.path.join('/', 'private', 'var', 'root')
GEN_OSSEC = None
else:
PREFIX = os.sep
GEN_OSSEC = os.path.join(WAZUH_SOURCES, 'gen_ossec.sh')

try:
import grp
import pwd
Expand All @@ -45,18 +46,31 @@
except (ImportError, KeyError, ModuleNotFoundError):
pass

if sys.platform == 'darwin' or sys.platform == 'win32' or sys.platform == 'sunos5':
WAZUH_SERVICE = 'wazuh.agent'
else:
try:
with open(os.path.join(WAZUH_PATH, 'etc/ossec-init.conf'), 'r') as f:
type_ = None
for line in f.readlines():
if 'TYPE' in line:
type_ = line.split('"')[1]
WAZUH_SERVICE = 'wazuh-manager' if type_ == 'server' else 'wazuh-agent'
except FileNotFoundError:
pass

def get_version():

if platform.system() in ['Windows', 'win32']:
with open(os.path.join(WAZUH_PATH, 'VERSION'), 'r') as f:
version = f.read()
return version[:version.rfind('\n')]

else: # Linux, sunos5, darwin, aix...
return subprocess.check_output([
f"{WAZUH_PATH}/bin/wazuh-control", "info", "-v"
], stderr=subprocess.PIPE).decode('utf-8')


def get_service():
if platform.system() in ['Windows', 'win32']:
return 'wazuh-agent'

else: # Linux, sunos5, darwin, aix...
service = subprocess.check_output([
f"{WAZUH_PATH}/bin/wazuh-control", "info", "-t"
], stderr=subprocess.PIPE).decode('utf-8').strip()

return 'wazuh-manager' if service == 'server' else 'wazuh-agent'


_data_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data')
WAZUH_LOGS_PATH = os.path.join(WAZUH_PATH, 'logs')
Expand Down Expand Up @@ -90,3 +104,5 @@
os.path.join(QUEUE_OSSEC_PATH, 'krequest'),
os.path.join(QUEUE_OSSEC_PATH, 'auth')
]


4 changes: 2 additions & 2 deletions deps/wazuh_testing/wazuh_testing/tools/monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ def send(self, message, size=False):
"""
msg_bytes = message.encode() if isinstance(message, str) else message
try:
msg_bytes = wazuh_pack(len(msg_bytes)) + msg_bytes if size is True else msg_bytes
msg_bytes = wazuh_pack(len(msg_bytes)) + msg_bytes if size else msg_bytes
if self.protocol == socket.SOCK_STREAM: # TCP
output = self.sock.sendall(msg_bytes)
else: # UDP
Expand All @@ -323,7 +323,7 @@ def receive(self, size=False):
bytes
Socket message.
"""
if size is True:
if size:
size = wazuh_unpack(self.sock.recv(4, socket.MSG_WAITALL))
output = self.sock.recv(size, socket.MSG_WAITALL)
else:
Expand Down
13 changes: 7 additions & 6 deletions deps/wazuh_testing/wazuh_testing/tools/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
from subprocess import check_call

import psutil
from wazuh_testing.tools import WAZUH_PATH, WAZUH_SERVICE, WAZUH_SOCKETS, QUEUE_DB_PATH, WAZUH_OPTIONAL_SOCKETS

from wazuh_testing.tools import WAZUH_PATH, get_service, WAZUH_SOCKETS, QUEUE_DB_PATH, WAZUH_OPTIONAL_SOCKETS
from wazuh_testing.tools.configuration import write_wazuh_conf


Expand Down Expand Up @@ -113,7 +114,7 @@ def control_service(action, daemon=None, debug_mode=False):
if sys.platform == 'darwin' or sys.platform == 'sunos5':
result = subprocess.run([f'{WAZUH_PATH}/bin/wazuh-control', action]).returncode
else:
result = subprocess.run(['service', WAZUH_SERVICE, action]).returncode
result = subprocess.run(['service', get_service(), action]).returncode
action == 'stop' and delete_sockets()
else:
if action == 'restart':
Expand Down Expand Up @@ -199,8 +200,8 @@ def check_daemon_status(daemon=None, running=True, timeout=10, extra_sockets=Non
extra_sockets = []
for _ in range(3):
# Check specified daemon/s status
daemon_status = subprocess.run(['service', WAZUH_SERVICE, 'status'], stdout=subprocess.PIPE).stdout.decode()
if f"{daemon if daemon is not None else ''} {'not' if running is True else 'is'} running" not in daemon_status:
daemon_status = subprocess.run(['service', get_service(), 'status'], stdout=subprocess.PIPE).stdout.decode()
if f"{daemon if daemon is not None else ''} {'not' if running else 'is'} running" not in daemon_status:
# Construct set of socket paths to check
if daemon is None:
socket_set = {path for array in WAZUH_SOCKETS.values() for path in array}
Expand All @@ -217,10 +218,10 @@ def check_daemon_status(daemon=None, running=True, timeout=10, extra_sockets=Non
# Finish main for loop if both daemon and socket checks are ok
break

time.sleep(timeout / 3)
time.sleep(timeout/3)
else:
raise TimeoutError(f"{'wazuh-service' if daemon is None else daemon} "
f"{'is not' if running is True else 'is'} running")
f"{'is not' if running else 'is'} running")


def delete_dbs():
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from numpydoc.docscrape import FunctionDoc
from py.xml import html
from wazuh_testing import global_parameters
from wazuh_testing.tools import LOG_FILE_PATH, WAZUH_CONF, WAZUH_SERVICE, ALERT_FILE_PATH
from wazuh_testing.tools import LOG_FILE_PATH, WAZUH_CONF, get_service, ALERT_FILE_PATH
from wazuh_testing.tools.configuration import get_wazuh_conf, set_section_wazuh_conf, write_wazuh_conf
from wazuh_testing.tools.file import truncate_file
from wazuh_testing.tools.monitoring import QueueMonitor, FileMonitor, SocketController, close_sockets
Expand All @@ -40,7 +40,7 @@ def pytest_runtest_setup(item):
if supported_platforms and plat not in supported_platforms:
pytest.skip("Cannot run on platform {}".format(plat))

host_type = 'agent' if 'agent' in WAZUH_SERVICE else 'server'
host_type = 'agent' if 'agent' in get_service() else 'server'
supported_types = HOST_TYPES.intersection(mark.name for mark in item.iter_markers())
if supported_types and host_type not in supported_types:
pytest.skip("Cannot run on wazuh {}".format(host_type))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* SQL Schema rbac tests
* Copyright (C) 2015-2020, Wazuh Inc.
* Copyright (C) 2015-2021, Wazuh Inc.
* Created by Wazuh, Inc. <info@wazuh.com>.
* This program is a free software, you can redistribute it and/or modify it under the terms of GPLv2.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* SQL Schema rbac tests
* Copyright (C) 2015-2020, Wazuh Inc.
* Copyright (C) 2015-2021, Wazuh Inc.
* Created by Wazuh, Inc. <info@wazuh.com>.
* This program is a free software, you can redistribute it and/or modify it under the terms of GPLv2.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
# Created by Wazuh, Inc. <info@wazuh.com>.
# This program is free software; you can redistribute it and/or modify it under the terms of GPLv2

import json
import os
import pytest
import json
import socket
import sys

import pytest
from wazuh_testing import global_parameters
from wazuh_testing.fim import generate_params
from wazuh_testing.tools import LOG_FILE_PATH, WAZUH_PATH
from wazuh_testing.tools import LOG_FILE_PATH, WAZUH_PATH, get_service
from wazuh_testing.tools.configuration import load_wazuh_configurations
from wazuh_testing.tools.monitoring import FileMonitor, SocketController

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def test_load_rules_decoders(test_case):
shutil.chown('/var/ossec/etc/decoders/local_decoder.xml', "ossec", "ossec")

# Create session token
if 'same_session' in test_case and test_case['same_session'] is True:
if 'same_session' in test_case and test_case['same_session']:
session_token = create_dummy_session()

for stage in test_case['test_case']:
Expand All @@ -90,7 +90,7 @@ def test_load_rules_decoders(test_case):

connection = create_connection()
# Generate logtest request
if 'same_session' in test_case and test_case['same_session'] is True:
if 'same_session' in test_case and test_case['same_session']:
request_pattern = """{{ "version":1,
"origin":{{"name":"Integration Test","module":"api"}},
"command":"log_processing",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def test_multiple_providers(clean_vuln_tables, get_configuration, configure_envi
prefix='.*wazuh-modulesd.*'),
error_message=f"OVAL feed {os_feed} from provider {provider} not correctly assigned")

if path_enable is True and provider == 'redhat':
if path_enable and provider == 'redhat':
wazuh_log_monitor.start(timeout=vd.VULN_DETECTOR_GLOBAL_TIMEOUT,
callback=vd.make_vuln_callback(rf"Multi (path|url): '{feed}'.*",
prefix='.*wazuh-modulesd.*'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def test_vulnerabilities_report(get_configuration, configure_environment, restar

for cve, item in vulnerabilities['vulnerabilities'].items():
installed, hotfix = is_hotfix_installed(item[0]['patch'], dep, hotfixes)
if installed is True:
if installed:
wazuh_log_monitor.start(
timeout=vd.VULN_DETECTOR_SCAN_TIMEOUT,
update_position=False,
Expand Down
Loading

0 comments on commit d7b03e5

Please sign in to comment.