%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /opt/imunify360/venv/lib/python3.8/site-packages/defence360agent/feature_management/
Upload File :
Create Path :
Current File : //opt/imunify360/venv/lib/python3.8/site-packages/defence360agent/feature_management/hooks.py

"""
This program is free software: you can redistribute it and/or modify it under 
the terms of the GNU General Public License as published by 
the Free Software Foundation, either version 3 of the License, 
or (at your option) any later version.


This program is distributed in the hope that it will be useful, 
but WITHOUT ANY WARRANTY; without even the implied warranty of 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
See the GNU General Public License for more details.


You should have received a copy of the GNU General Public License
 along with this program.  If not, see <https://www.gnu.org/licenses/>.

Copyright © 2019 Cloud Linux Software Inc.

This software is also available under ImunifyAV commercial license,
see <https://www.imunify360.com/legal/eula>


This module contains hook, which are called on feature management permission
changes. Note that hooks are not executed automatically, developer is
responsible to obtain specific hook using get_hook() function and call it.
To add hook, create function with name equal to feature name
"""
import functools
import logging
from typing import Any, Awaitable, Callable, Optional

from defence360agent.contracts.config import ConfigFile
from defence360agent.feature_management.constants import AV, FULL, PROACTIVE

logger = logging.getLogger(__name__)


async def _hook_stub(*_):
    return True


def _result_warn(callback):
    @functools.wraps(callback)
    async def wrap(user, value):
        result = await callback(user, value)
        result or logger.warning("Hook '%s(%s)' failed for user '%s'.",
                                 callback.__name__, value, user)
        return result

    return wrap


@_result_warn
async def antivirus(user: Optional[str], value: Any) -> bool:
    """Called when 'av' feature is changed"""
    if not user:
        return True

    config = ConfigFile()
    config_value = config.get("MALWARE_SCANNING", "default_action")

    user_config = ConfigFile(user)
    user_config_value = user_config.get("MALWARE_SCANNING", "default_action")

    if value == FULL:
        user_config_value = None
    elif user_config_value and user_config_value.startswith('cleanup') and \
            config_value and config_value.startswith('cleanup'):
        user_config_value = 'notify'

    try:
        user_config.set(
            "MALWARE_SCANNING", "default_action", user_config_value
        )
    except Exception:
        return False

    return True


@_result_warn
async def proactive(user: Optional[str], value: Any) -> bool:
    """Called when 'proactive' feature is changed"""
    if not user:
        return True  # do nothing if no user specified

    config_value = None if value == FULL else 'DISABLED'

    try:
        ConfigFile(user).set("PROACTIVE_DEFENCE", "mode", config_value)
    except Exception:
        return False

    return True


HOOKS = {
    AV: antivirus,
    PROACTIVE: proactive,
}


def get_hook(feature: str) -> Callable[[Optional[str], Any], Awaitable[bool]]:
    """
    Get hook for specific feature. If no hook is implemented for this feature,
    return stub function
    :param feature: feature name
    :return: callable hook
    """
    return HOOKS.get(feature, _hook_stub)

Zerion Mini Shell 1.0