def override_wazuh_conf(configuration, set_password):
    # Stop Wazuh
    control_service('stop', daemon='wazuh-authd')
    time.sleep(1)
    check_daemon_status(running=False, daemon='wazuh-authd')
    truncate_file(LOG_FILE_PATH)

    # Configuration for testing
    test_config = set_section_wazuh_conf(configuration.get('sections'))
    # Set new configuration
    write_wazuh_conf(test_config)

    # reset_client_keys
    clean_client_keys_file()
    # reset password
    reset_password(set_password)

    time.sleep(1)
    # Start Wazuh
    control_service('start', daemon='wazuh-authd')
    """Wait until agentd has begun"""
    def callback_agentd_startup(line):
        if 'Accepting connections on port 1515' in line:
            return line
        return None

    log_monitor = FileMonitor(LOG_FILE_PATH)
    log_monitor.start(timeout=30, callback=callback_agentd_startup)
    time.sleep(1)
Example #2
0
def configure_environment(get_configuration, request):
    """Configure a custom environment for testing. Restart Wazuh is needed for applying the configuration."""

    # Save current configuration
    backup_config = get_wazuh_conf()

    # Configuration for testing
    test_config = set_section_wazuh_conf(get_configuration.get('sections'))

    # Create test directories
    if hasattr(request.module, 'test_directories'):
        test_directories = getattr(request.module, 'test_directories')
        for test_dir in test_directories:
            os.makedirs(test_dir, exist_ok=True, mode=0o777)

    # Set new configuration
    write_wazuh_conf(test_config)

    # Change Windows Date format to ensure TimeMachine will work properly
    if sys.platform == 'win32':
        subprocess.call(
            'reg add "HKCU\\Control Panel\\International" /f /v sShortDate /t REG_SZ /d "dd/MM/yyyy" >nul',
            shell=True)

    # Call extra functions before yield
    if hasattr(request.module, 'extra_configuration_before_yield'):
        func = getattr(request.module, 'extra_configuration_before_yield')
        func()

    # Set current configuration
    global_parameters.current_configuration = get_configuration

    yield

    TimeMachine.time_rollback()

    # Remove created folders (parents)
    if sys.platform == 'win32':
        control_service('stop')

    if hasattr(request.module, 'test_directories'):
        for test_dir in test_directories:
            shutil.rmtree(test_dir, ignore_errors=True)

    if sys.platform == 'win32':
        control_service('start')

    # Restore previous configuration
    write_wazuh_conf(backup_config)

    # Call extra functions after yield
    if hasattr(request.module, 'extra_configuration_after_yield'):
        func = getattr(request.module, 'extra_configuration_after_yield')
        func()

    if hasattr(request.module, 'force_restart_after_restoring'):
        if getattr(request.module, 'force_restart_after_restoring'):
            control_service('restart')
Example #3
0
def restart_wazuh_with_new_conf(new_conf, daemon='wazuh-syscheckd'):
    """
    Restart Wazuh service applying a new ossec.conf

    Args:
        new_conf ( ET.ElementTree) : New config file.
        daemon (str, optional): Daemon to restart when applying the configuration.
    """
    write_wazuh_conf(new_conf)
    control_service('restart', daemon=daemon)
Example #4
0
def restart_wazuh_with_new_conf(new_conf, daemon='ossec-syscheckd'):
    """
    Restart Wazuh service applying a new ossec.conf

    Parameters
    ----------
    new_conf : ET.ElementTree
        New config file.
    daemon : str, optional
        Daemon to restart when applying the configuration.
    """
    write_wazuh_conf(new_conf)
    control_service('restart', daemon=daemon)
def override_wazuh_conf(configuration):
    """Apply custom settings on ossec.conf file.

    Settings are obtained from values located under "configuration" section of tests found in a YAML file.
    For this purpose, it stops the wazuh-agentd service, applies the settings and starts it again.

    Args:
        configuration (dict): New parameters to be applied.

    Raises:
        ValueError: If wazuh-agentd daemon cannot be started again.
    """
    # Stop Wazuh
    control_service('stop', daemon='wazuh-agentd')

    # Configuration for testing
    temp = get_temp_yaml(configuration)
    conf = load_wazuh_configurations(
        temp,
        __name__,
    )
    os.remove(temp)

    test_config = set_section_wazuh_conf(conf[0]['sections'])
    # Set new configuration
    write_wazuh_conf(test_config)

    # reset_client_keys
    ag.clean_client_keys_file()
    clean_log_file()
    ag.clean_password_file()
    if configuration.get('password'):
        parser = ag.AgentAuthParser()
        parser.add_password(password=configuration['password']['value'],
                            isFile=True,
                            path=configuration.get('authorization_pass_path'))

    # Start Wazuh
    control_service('start', daemon='wazuh-agentd')